[Zer0pts2020]Can you guess it?

知识点

  • basename函数

WP

进入页面,审一下源码:

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

去查了一下hash_equals,基本没可能从这里入手,只可能从上面那段代码入手了:

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();
}

比如$path是/var/www/html/index.php,那么basename($path);得到的就是index.php。
因此要从这里来highlight_file这个config.php。但是存在正则匹配,需要绕过。这里的绕过也是利用了basename这个点。写一个代码来fuzz一下:

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

for($i=0;$i<255;$i++){
    $str="/index.php/config.php/".chr($i);
    if(!check($str)){
        echo $i.":".basename($str);
        echo "<br>";
    }
}

在这里插入图片描述
可以看到,从%81开始,basename就会忽略了,既绕过了正则匹配,而且basename得到的也是config.php,因此就可以直接读到flag了。
但是经过测试,其实%80同样可以做到。

其实好好了解一下这个函数,会发现它会去掉文件名开头的非ASCII值。也就是这个函数在实际使用的时候会出的一个问题,如果文件的开头是中文,他只会得到中文后面的部分。因此结合实际的问题,也可以解决这个题目:
在这里插入图片描述
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值