0%

Zer0pts2020 Can you guess it

[Zer0pts2020]Can you guess it

外国比赛的签到题,攻击点给的很明显吧,还有一个小小的有意思的地方

源码

<?php
include 'config.php'; // FLAG is defined in config.php

if (preg_match('/config\.php\/*$/i', $_SERVER['PHP_SELF'])) {
  exit("I don't know what you are thinking, but I won't let you read it :)");
}

if (isset($_GET['source'])) {
  highlight_file(basename($_SERVER['PHP_SELF']));
  exit();
}

$secret = bin2hex(random_bytes(64));
if (isset($_POST['guess'])) {
  $guess = (string) $_POST['guess'];
  if (hash_equals($secret, $guess)) {
    $message = 'Congratulations! The flag is: ' . FLAG;
  } else {
    $message = 'Wrong.';
  }
}
?>
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Can you guess it?</title>
  </head>
  <body>
    <h1>Can you guess it?</h1>
    <p>If your guess is correct, I'll give you the flag.</p>
    <p><a href="?source">Source</a></p>
    <hr>
<?php if (isset($message)) { ?>
    <p><?= $message ?></p>
<?php } ?>
    <form action="index.php" method="POST">
      <input type="text" name="guess">
      <input type="submit">
    </form>
  </body>
</html>

两个点,一个是$_SERVER[‘PHP_SELF’],这个是返回PHP当前目录与根目录的相对路径,但是访问路径被修改的话这个变量的内容也会对应的更改,如果我们访问的内容是url/index.php/config.php的话,$_SERVER[‘PHP_SELF’]的内容就是index.php/config.php,然后再用basename()获取到config.php,但是这样子过不了正则

第二个点是猜字符,无懈可击,除非暴力破解,没机会了,这里有一个有意思的点,就是使用的对比是hash_equals函数,而不是===,搜了一下,hash_equals相较于===,它对比使用的时间是固定的,而===是逐位比较,这个函数可以防止攻击者通过时间来猜测字符串长度

题解

所以唯一可能的攻击点就是通过$_SERVER[‘PHP_SELF’]获取源码,需要绕过basename和正则两部分,这里看PHPmanual可以看到一个点

basename() is locale aware, so for it to see the correct basename with multibyte character paths, the matching locale must be set using the setlocale() function.

basename在处理多比特编码的字符串时会出现一些问题(比如中文,百度这个函数的时候全是说获取中文的问题),那么使用一个超出ascii码范围的字符就可以绕过

payload: http://de3fca4a-7648-4d0e-844e-60cfc5cb2a63.node3.buuoj.cn/index.php/config.php/%aa?source