Loading

[BUUCTF题解][Zer0pts2020]Can you guess it

### 知识点

PHP特性

过程

收集下信息(扫目录+检查HTTP报文+查看初始页面HTML代码),显然没有找到有用信息(源码出题人都在初始页面提供了)。

image-20220323172120552

页面源码如下:

<?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>

注释指明flag在config.php中,但是如果按照绕过hash_equals的思路来是行不通的,该函数并没有漏洞也没有使用错误。

image-20220323172326238

但是可以留意到显示源码的逻辑部分故作玄虚没有使用简洁的__FILE__而是采用basename函数截取$_SERVER['PHP_SELF'],本题的利用点也就在此处。

image-20220323172626946

$_SERVER['PHP_SELF']会获取我们当前的访问路径,并且PHP在根据URI解析到对应文件后会忽略掉URL中多余的部分,即若访问存在的index.php页面,如下两种UR均会访问到。

/index.php
/index.php/dosent_exist.php

basename可以理解为对传入的参数路径截取最后一段作为返回值,但是该函数发现最后一段为不可见字符时会退取上一层的目录,即:

$var1="/config.php/test"
basename($var1)	=> test
$var2="/config.php/%ff"
basename($var2)	=>	config.php

接下来就显然了,通过构造URI让其包含config.php这个文件名再让basename函数截取出来,之后通过请求参数source就能显示config.php的源码,也就能见到flag了。

image-20220323173656185

获取到flag

posted @ 2022-03-23 17:40  Article_kelp  阅读(222)  评论(0编辑  收藏  举报