Web安全:SqlMap工具

2 篇文章 0 订阅

一、简介

        sqlmap 是一款开源的渗透测试工具,可以自动化进行SQL注入的检测、利用,并能接管数据库服务器。它具有功能强大的检测引擎,为渗透测试人员提供了许多专业的功能并且可以进行组合,其中包括数据库指纹识别、数据读取和访问底层文件系统,甚至可以通过带外数据连接的方式执行系统命令。

官网下载地址:GitHub - sqlmapproject/sqlmap: Automatic SQL injection and database takeover tool

使用方法:

python sqlmap.py 参数(Windows)

sqlmap 参数(Linux)

sqlmap可以运行在python2.6、2.7和3.x的任何平台上。

官方文档:

Usage · sqlmapproject/sqlmap Wiki (github.com)

二、使用方法

Options模块

1、 -h, --help

显示基础帮助信息。

python sqlmap.py -h

2、 -hh

显示高级帮助信息。

python sqlmap.py -h

3、 --version

显示版本信息。

python sqlmap.py -version

4、 -v VERBOSE

指定的输出信息的内容等级0-6

python sqlmap.py -u "http://ctf/sql_labs/Less-1/?id=1" -v 5

运行结果:

Target模块

1、 -u URL, --url=URL 

需要进行注入检测的目标URL。

python sqlmap.py -u "http://ctf/sql_labs/Less-1/?id=1"

2、 -d DIRECT 

通过字符串直连数据库。

争对不同的数据库有不同的连接字符串方式。

 案例:

python sqlmap.py -d "mysql://root:root@127.0.0.1:3306/testdb"

3、 -l LOGFILE 

从Burp或者WebScarab的代理日志中,解析目标。

        指定一个Burp或WebScarab的代理日志文件,Sqlmap将从日志文件中解析出可能的攻击目标,并逐个尝试进行注入。个人没尝试过!

4、 -m BULKFILE 

扫描文本文件提供的多个目标URL目标。

文本文件中包含多个URL目标,针对多个目标时候使用。

案例: 

python sqlmap.py -m url.txt

5、 -r REQUESTFILE 

从文件中加载HTTP请求。

        将一个HTTP请求保存在文件中,然后使用参数“-r”加载该文件,这样,可以跳过其他一些选项的使用(例如设置Cookie,发布数据等),以该文件中HTTP请求目标为攻击目标进行测试。

 案例:

python sqlmap.py -r a.txt

6、 -g GOOGLRFORK :将谷歌查询结果作为目标URL。

将谷歌搜索的前一百条结果,作为检测注入目标(需要科学上网(翻墙))

值:Google hack语句

实例:

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

7、 -c CONFIGFILE :从配置conf文件获取注入目标。

打开配置文件模板,填写需要的参数到模板,使用以下代码进行注入。

python sqlmap.py -c a.conf

Request模块

指定http请求的相关参数,感觉可以通过使用-r参数从文件中加载http请求更合适。

  Request:
    These options can be used to specify how to connect to the target URL

    --method=METHOD     Force usage of given HTTP method (e.g. PUT)
    --data=DATA         Data string to be sent through POST (e.g. "id=1")
    --param-del=PARA..  Character used for splitting parameter values (e.g. &)
    --cookie=COOKIE     HTTP Cookie header value (e.g. "PHPSESSID=a8d127e..")
    --cookie-del=COO..  Character used for splitting cookie values (e.g. ;)
    --live-cookies=L..  Live cookies file used for loading up-to-date values
    --load-cookies=L..  File containing cookies in Netscape/wget format
    --drop-set-cookie   Ignore Set-Cookie header from response
    -A AGENT, --user..  HTTP User-Agent header value
    --mobile            Imitate smartphone through HTTP User-Agent header
    --random-agent      Use randomly selected HTTP User-Agent header value
    --host=HOST         HTTP Host header value
    --referer=REFERER   HTTP Referer header value
    --headers=HEADERS   Extra headers (e.g. "Accept-Language: fr\nETag: 123")
    -H HEADER, --hea..  Extra header (e.g. "X-Forwarded-For: 127.0.0.1")
    --auth-type=AUTH..  HTTP authentication type (Basic, Digest, Bearer, ...)
    --auth-cred=AUTH..  HTTP authentication credentials (name:password)
    --auth-file=AUTH..  HTTP authentication PEM cert/private key file
    --abort-code=ABO..  Abort on (problematic) HTTP error code(s) (e.g. 401)
    --ignore-code=IG..  Ignore (problematic) HTTP error code(s) (e.g. 401)
    --ignore-proxy      Ignore system default proxy settings
    --ignore-redirects  Ignore redirection attempts
    --ignore-timeouts   Ignore connection timeouts
    --proxy=PROXY       Use a proxy to connect to the target URL
    --proxy-cred=PRO..  Proxy authentication credentials (name:password)
    --proxy-file=PRO..  Load proxy list from a file
    --proxy-freq=PRO..  Requests between change of proxy from a given list
    --tor               Use Tor anonymity network
    --tor-port=TORPORT  Set Tor proxy port other than default
    --tor-type=TORTYPE  Set Tor proxy type (HTTP, SOCKS4 or SOCKS5 (default))
    --check-tor         Check to see if Tor is used properly
    --delay=DELAY       Delay in seconds between each HTTP request
    --timeout=TIMEOUT   Seconds to wait before timeout connection (default 30)
    --retries=RETRIES   Retries when the connection timeouts (default 3)
    --retry-on=RETRYON  Retry request on regexp matching content (e.g. "drop")
    --randomize=RPARAM  Randomly change value for given parameter(s)
    --safe-url=SAFEURL  URL address to visit frequently during testing
    --safe-post=SAFE..  POST data to send to a safe URL
    --safe-req=SAFER..  Load safe HTTP request from a file
    --safe-freq=SAFE..  Regular requests between visits to a safe URL
    --skip-urlencode    Skip URL encoding of payload data
    --csrf-token=CSR..  Parameter used to hold anti-CSRF token
    --csrf-url=CSRFURL  URL address to visit for extraction of anti-CSRF token
    --csrf-method=CS..  HTTP method to use during anti-CSRF token page visit
    --csrf-data=CSRF..  POST data to send during anti-CSRF token page visit
    --csrf-retries=C..  Retries for anti-CSRF token retrieval (default 0)
    --force-ssl         Force usage of SSL/HTTPS
    --chunked           Use HTTP chunked transfer encoded (POST) requests
    --hpp               Use HTTP parameter pollution method
    --eval=EVALCODE     Evaluate provided Python code before the request (e.g.
                        "import hashlib;id2=hashlib.md5(id).hexdigest()")

1、 --method=METHOD 

指定请求的方法。

        指定HTTP请求的方法。一般情况下,会自动检测请求的方法。在某些特定的情况下需要强制指定方法。

案例:

python sqlmap.py -u “https://www.baidu.com/a.php?id=1” --method=put

2、 --data=DATA 

指定POST请求提交的参数

      当我们使用data参数,则HTTP会使用post方法将参数当作HTTP data提交,同时也会检测此参数有没有注入漏洞。

案例:

python sqlmap.py -u “https://www.baidu.com/a.php?id=1” --data="id=1"

3、 -param-del=PARAM 

指定参数分割符。

        一般情况下,网站会用&作为参数的分隔符,这也是SQLMAP默认使用的分隔符,如果有些web application不使用&作为分隔符的话,那么就使用–param-del去告诉sqlmap分隔符是什么。

 案例:

python sqlmap.py -u “http://www.xxxxx.com/a.php?id=1;food=2” --param-del=”;”

4、 cookie相关 

需要指定Cookie的情况:

        1、网站需要Cookie信息

        2、检测并利用cookie注入。(当–level设置为2或者更高的时候,sqlmap会检测cookie是否存在注入漏洞。)

参数解释
--cookie指定cookie的内容
--cookie-del          指定cookie的分割符
--live-cookies从指定的cookie文件中读取当前的cookie信息
--load-cookies   指定以 Netscape/wget 格式存放 cookies 的文件
--drop-set-cookie         忽略 HTTP 响应中的 Set-Cookie 参数

5、 user-agent相关 

设置用户代理

        默认情况下,sqlmap使用的User-Agent是sqlmap/1.0-dev-xxxxxxx (http://sqlmap.org) 。(当 --level设置为3或者更高时,sqlmap会自动检测user-agent是否存在注入漏洞)

参数解释
-A  AGENT自定义User-Agent
--random-agent    从sqlmap自带的文本文件中随机选择一个user-agent。这个文件是 ./txt/user-agents.txt
--mobile模拟手机发送请求

6、 header相关 

参数解释
--host=HOST指定HTTP请求的HOST字段
--referer=REFERER指定HTTP请求的REFERER字段
--headers=HEADERS添加额外的HTTP请求字段信息

 案例:

python sqlmap.py -u “http://www.xxx.com/a.php?id=1” --headers=”X-A : A \nX-B : B”

7、 --auth相关 

    --auth-type=AUTH..  HTTP authentication type (Basic, Digest, Bearer, ...)
    --auth-cred=AUTH..  HTTP authentication credentials (name:password)
    --auth-file=AUTH..  HTTP authentication PEM cert/private key file

后端 Web 服务器实现的 HTTP 协议认证时,所有向目标程序发起 HTTP 请求都需要有效凭据来访问。这种情况可以通过使用以下选项来指定有效凭证信息。

参数解释
--auth-type=AUTHHTTP请求认证类型(Basic,Digest,Bearer)
--auth-cred=AUTHHTTP请求认证证书,格式:name:password
--auth-file=AUTHHTTP请求认证文件PEM格式

案例: 

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

8、 --igore相关 

    --abort-code=ABO..  Abort on (problematic) HTTP error code(s) (e.g. 401)
    --ignore-code=IG..  Ignore (problematic) HTTP error code(s) (e.g. 401)
    --ignore-proxy      Ignore system default proxy settings
    --ignore-redirects  Ignore redirection attempts
    --ignore-timeouts   Ignore connection timeouts

参数解释
--abort-code=ABO遇到指定HTTP错误码中断注入
--ignore-code=IG忽略指定的HTTP错误码
--ignore-proxy忽略系统默认代理设置
--ignore-redirects忽略重定向请求
--ignore-timeouts忽略连接超时

9、 --proxy相关 

    --proxy=PROXY       Use a proxy to connect to the target URL
    --proxy-cred=PRO..  Proxy authentication credentials (name:password)
    --proxy-file=PRO..  Load proxy list from a file
    --proxy-freq=PRO..  Requests between change of proxy from a given list

参数解释
--proxy=PROXY使用代理连接目标URL
--proxy-cred=PRO指定代理认证证书
--proxy-file=PRO

指定一个包含有代理列表的文件,在连接时,会依次使用文件里面的代理

当代理无效时,会自动调到下一个代理

--proxy-freq=PRO指定请求代理的频率

10、 --tor相关 

        假如因为相关原因需要保持匿名,可以根据 Tor 安装指南配置一个 Tor 客户端Privoxy(或类似的)进行代理,而不是使用单个预定义的 HTTP(S) 代理服务器。接着就可以使用开关 --tor 来让 sqlmap 尝试自动设置 Tor 代理连接。

        如果你想手动设置 Tor 代理的类型和端口,可以使用选项 --tor-type --tor-port(例如:--tor-type=SOCKS5 --tor-port=9050)。

        强烈建议偶尔使用 --check-tor 来确保一切设置正确。有些情况下 Tor 包(例如:Vidalia(译者注:Vidalia 是 Tor 的图形界面管理工具,官方已经移除对它的支持))配置错误(或重置了以前的配置)会使你以为已经成功匿名。使用这个开关,sqlmap 将在对任何目标发起请求之前发送一个请求到你正在使用 Tor?这个官方页面检查一切配置是否正常。如果检查失败,sqlmap 将警告你并直接退出。

    --tor                                     Use Tor anonymity network
    --tor-port=TORPORT          Set Tor proxy port other than default
    --tor-type=TORTYPE          Set Tor proxy type (HTTP, SOCKS4 or SOCKS5 (default))
    --check-tor                          Check to see if Tor is used properly

参数解释
--tor自动设置Tor代理连接
--tor-port指定端口
--tor-type指定类型
--check-tor检查Tor设置是否正确

11、 连接时间相关 

    --delay=DELAY       Delay in seconds between each HTTP request
    --timeout=TIMEOUT   Seconds to wait before timeout connection (default 30)
    --retries=RETRIES   Retries when the connection timeouts (default 3)
    --retry-on=RETRYON  Retry request on regexp matching content (e.g. "drop")

参数解释
--delay=DELAY每次HTTP请求间隔多少秒
--timeout=TIMEOUT指定超时连接的等待时间,默认30s
--retries=RETRIES指定超时连接的重连次数
--retry-on=RETRYON指定正则匹配的字符串,匹配成功,进行重连

案例:

sqlmap -u "http://example.com/vulnerable.php?id=1" --risk=3 --level=5 --retry-on="timeout,error" --threads=5

12、 --randomize=RPARAM 

        随机更改参数。可以指定请求参数名称,这些参数在请求期间根据原始长度和类型随机更改。这个命令会随机改变 id 参数的值,以便在测试时模拟不同的输入。

案例:

python sqlmap.py -u "http://example.com/vulnerable.php?id=1&name=test" --randomize=id

13、 --safe相关 

        Sqlmap的盲注测试会产生大量错误请求。为了避免被限制,我们可以每隔一段时间来访问正确的url。使用以下参数,sqlmap将会每隔一段时间访问一个正确的URL,并且不会对其进行任何注入。

    --safe-url=SAFEURL  URL address to visit frequently during testing
    --safe-post=SAFE..  POST data to send to a safe URL
    --safe-req=SAFER..  Load safe HTTP request from a file
    --safe-freq=SAFE..  Regular requests between visits to a safe URL

参数解释
--safe-url=SAFEURL指定安全链接URL
--safe-post=SAFE指定安全连接端口
--safe-req=SAFER从文件加载安全请求
--safe-freq=SAFE指定访问安全链接的频率

14、 --skip-urlencode 

        根据参数的位置(例如:GET),其值可能会被默认进行 URL 编码。在某些情况下,后端 Web 服务器不遵循 RFC 标准,并要求以原始非编码形式发送参数值。在这种情况下可以使用 --skip-urlencode

案例:

python sqlmap.py -u "http://example.com/vulnerable.php?id=1" --skip-urlencode

15、 --csrf相关 

        许多站点有使用 token 的反 CSRF 防护,在每个页面的响应随机设置隐藏字段值。sqlmap 将自动尝试识别并绕过这种防护,同时支持 --csrf-token--csrf-url 等选项用来做进一步调整。

        选项 --csrf-token 用于设置包含随机 token 的隐藏字段的名称。这在网站对这些字段使用非标准名称的情况下是非常有用的。选项 --csrf-url 用于从任意有效的 URL 地址获取 token 值。这在目标网址在初始地不包含必需的 token 值,而需要从其他地方提取时是非常有用的。


    --csrf-token=CSR..           Parameter used to hold anti-CSRF token
    --csrf-url=CSRFURL        URL address to visit for extraction of anti-CSRF token
    --csrf-method=CS..          HTTP method to use during anti-CSRF token page visit
    --csrf-data=CSRF..           POST data to send during anti-CSRF token page visit
    --csrf-retries=C..               Retries for anti-CSRF token retrieval (default 0)
 

参数解释
--csrf-token=CSR指定包含token的参数
--csrf-url=CSRFURL指定URL
--csrf-method=CS指定请求方法
--csrf-data=CSRF指定POST方法请求的数据
--csrf-retries=C指定重试次数

16、 杂项 

  --force-ssl                          Force usage of SSL/HTTPS
  --chunked                          Use HTTP chunked transfer encoded (POST) requests
  --hpp                                  Use HTTP parameter pollution method
  --eval=EVALCODE            Evaluate provided Python code before the request (e.g.  "import                                                 hashlib;id2=hashlib.md5(id).hexdigest()")  

参数解释
--force-ssl强制SSL连接
--chunked使用分块传输编码请求
--hpp使用参数混淆
--eval=EVALCODE在请求之前执行一段Python代码

案例:使用当前 GET 请求中的 id 参数值重新计算出对应的 MD5 哈希值,从而替换掉原来的 hash 参数值。

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

Enumeration模块

该模块用于导出数据库的信息。

 Enumeration:
    These options can be used to enumerate the back-end database
    management system information, structure and data contained in the
    tables

    -a, --all           Retrieve everything
    -b, --banner        Retrieve DBMS banner
    --current-user      Retrieve DBMS current user
    --current-db        Retrieve DBMS current database
    --hostname          Retrieve DBMS server hostname
    --is-dba            Detect if the DBMS current user is DBA
    --users             Enumerate DBMS users
    --passwords         Enumerate DBMS users password hashes
    --privileges        Enumerate DBMS users privileges
    --roles             Enumerate DBMS users roles
    --dbs               Enumerate DBMS databases
    --tables            Enumerate DBMS database tables
    --columns           Enumerate DBMS database table columns
    --schema            Enumerate DBMS schema
    --count             Retrieve number of entries for table(s)
    --dump              Dump DBMS database table entries
    --dump-all          Dump all DBMS databases tables entries
    --search            Search column(s), table(s) and/or database name(s)
    --comments          Check for DBMS comments during enumeration
    --statements        Retrieve SQL statements being run on DBMS
    -D DB               DBMS database to enumerate
    -T TBL              DBMS database table(s) to enumerate
    -C COL              DBMS database table column(s) to enumerate
    -X EXCLUDE          DBMS database identifier(s) to not enumerate
    -U USER             DBMS user to enumerate
    --exclude-sysdbs    Exclude DBMS system databases when enumerating tables
    --pivot-column=P..  Pivot column name
    --where=DUMPWHERE   Use WHERE condition while table dumping
    --start=LIMITSTART  First dump table entry to retrieve
    --stop=LIMITSTOP    Last dump table entry to retrieve
    --first=FIRSTCHAR   First query output word character to retrieve
    --last=LASTCHAR     Last query output word character to retrieve
    --sql-query=SQLQ..  SQL statement to be executed
    --sql-shell         Prompt for an interactive SQL shell
    --sql-file=SQLFILE  Execute SQL statements from given file(s)

1、 -a, --all 获取所有数据  

python sqlmap.py -u "http://ctf/sql_labs/Less-1/?id=1"  -a
python sqlmap.py -u "http://ctf/sql_labs/Less-1/?id=1"  --all

2、 -b, --banner 获取数据库,服务器和中间件版本号  

python sqlmap.py -u "http://ctf/sql_labs/Less-1/?id=1"  -b

3、 获取注入页面数据库管理员信息 

    --current-user      Retrieve DBMS current user
    --current-db        Retrieve DBMS current database
    --hostname          Retrieve DBMS server hostname
    --is-dba            Detect if the DBMS current user is DBA 

参数解释
--current-user获得网页DBMS的当前用户
--current-db获得网页DBMS的使用数据库
--hostname获得网页主机名
--is-dba检查当前用户是否是数据库管理员

4、 列出数据库的指定信息 

        指定要列出的信息,可根据-U、-T、-D指定列出数据的条件,也可根据  --exclude-sysdbs   来忽略一些系统表。

    --users                         Enumerate DBMS users
    --passwords                 Enumerate DBMS users password hashes
    --privileges                   Enumerate DBMS users privileges
    --roles                          Enumerate DBMS users roles
    --dbs                            Enumerate DBMS databases
    --tables                        Enumerate DBMS database tables
    --columns                    Enumerate DBMS database table columns
    --schema                     Enumerate DBMS schema

参数解释
--users

列出所有DBMS用户

--passwords

列出所有DBMS用户密码的哈希值

-U 选项来指定要枚举出角色的用户

--privileges

列出所有DBMS用户权限

-U 选项来指定要枚举出角色的用户

--roles

列出所有DBMS用户角色

-U 选项来指定要枚举出角色的用户

--dbs列出 DBMS 所有数据库
--tables列出 DBMS 所有数据表
--columns列出 DBMS 所有列
--schema列出DBMS 所有模式

 

5、 --count 获取表的条目数 

 --count             Retrieve number of entries for table(s)

6、 拖库处理 


    --dump              Dump DBMS database table entries
    --dump-all          Dump all DBMS databases tables entries

    --exclude-sysdbs    Exclude DBMS system databases when enumerating tables

7、 --search 搜索列、表和数据库 

8、 自定义SQL 

    --sql-query=SQLQ..  SQL statement to be executed
    --sql-shell         Prompt for an interactive SQL shell
    --sql-file=SQLFILE  Execute SQL statements from given file(s)
 

9、 设置转储条件 

  --pivot-column=P..  Pivot column name
    --where=DUMPWHERE   Use WHERE condition while table dumping
    --start=LIMITSTART  First dump table entry to retrieve
    --stop=LIMITSTOP    Last dump table entry to retrieve
    --first=FIRSTCHAR   First query output word character to retrieve
    --last=LASTCHAR     Last query output word character to retrieve

10、 指定参数 

    -D DB                      DBMS database to enumerate
    -T TBL                     DBMS database table(s) to enumerate
    -C COL                    DBMS database table column(s) to enumerate
    -X EXCLUDE          DBMS database identifier(s) to not enumerate
    -U USER                 DBMS user to enumerate


参考资料:

sqlmap 用户手册 (highlight.ink)

  • 9
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值