CVE-2018-12613 的一些思考

復現 CVE-2018-12613 的一些思考,關於文件包含路徑的問題php

漏洞

/index.php 第 55 行sql

$target_blacklist = array (
    'import.php', 'export.php'
);

if (! empty($_REQUEST['target'])
    && is_string($_REQUEST['target'])
    && ! preg_match('/^index/', $_REQUEST['target'])
    && ! in_array($_REQUEST['target'], $target_blacklist)
    && Core::checkPageValidity($_REQUEST['target'])
) {
    include $_REQUEST['target'];
    exit;
}

傳入參數 target 須要知足cookie

  1. 不以 index.php 開頭
  2. 不在 target_blacklist 中
  3. 經過 checkPageValidity() 函數檢驗

checkPageValidity() 函數函數

public static function checkPageValidity(&$page, array $whitelist = [])
    {
        if (empty($whitelist)) {
            $whitelist = self::$goto_whitelist;
        }
        if (! isset($page) || !is_string($page)) {
            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;
        }

        return false;
    }

第一個返回 True 的地方,直接將 page 與 whitelist 比較,傳入的必須是白名單裏的文件名,沒法繞過編碼

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

第二個返回 True 的地方,mb_strpos($x, $y) 函數查找 $y 在 $x 中首次出現的位置。mb_substr($str, $start, $length) 函數從 $str 中,截取從 $start 位置開始,長度爲 $length 的字符串。url

可是在這裏若是直接構造 payload : ?target=db_sql.php?/../../../cookie.txt 並不能跨路徑包含,? 後面的字符串會被當作傳入 db_sql.php 的參數,這就要利用後面的 urldecode 了.net

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

第三個返回 True 的地方,能夠利用雙重編碼繞過,將 ? 通過兩次編碼 %253f 就能夠繞過白名單驗證。%253f 傳入時,首先會被自動解碼一次,變成 %3f,而後urldecode() 再解碼一次,就變成了 ?3d

此時的 payload : ?target=db_sql.php%253f/../../../cookie.txtcode

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

問題

  1. include 'db_sql.php%253f/../../../cookie.txt' 爲何只會包含 cookie.txt 而不會包含 db_sql.phpblog

  2. 傳入 db_sql.php%253f/../../../cookie.txt 爲何會在 in_array($_page, $whitelist) 處返回 True

  3. 如圖,z.php 中 include 兩個 ../ 能夠包含,y.php 中一個 include 也能夠包含

  4. 在 php 的 include 中,include 'hint.php?/../cookie.txt'; 會報錯,include 'hint.php%3f/../cookie.txt'; 不會報錯,且能夠成功包含

一些解釋

在 include 中,舉個例子,假設 x.php 代碼包含 include '1source.phps/../cookie.txt'; ,假設 1source.phps 不存在,那麼這個文件包含等同於 : 在 1source.phps 文件夾目錄下的上一級中的 cookie.txt ,也就是和 x.php 在同一目錄下的 cookie.txt ,若是 1source.phps 存在,而且它是一個文件,那麼確定會報錯,若是它是一個文件夾,也會成功包含 cookie.txt 。若是變爲 include '1source.phps/./cookie.txt'; ,道理和上面相同

從新思考

  • 代碼以下 :

    <?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\" />";
        }  
    ?>
    • 傳入 file=hint.php ,在第一個 in_array 處會返回 true,而後直接包含 hint.php
    • 傳入 file=hint.php?/../cookie.txt ,在第二個 in_array 處會返回 true,第二個 in_array 中的 _pagehint.php ,而後包含 hint.php?/../cookie.txt ,可是這裏的 ? 起到傳遞參數的做用而不是破壞路徑
    • 傳入 file=hint.php%253f/../cookie.txt ,在第三個 in_array 處會返回 true ,第三個 in_array 中的 _pagehint.php ,而後包含 hint.php%3f/../cookie.txt ,這裏的 %3f? ,破壞了路徑,前面部分的路徑不存在,能夠包含後面的文件
相關文章
相關標籤/搜索