启动靶机,发现了这么个玩意
查看一手源代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<!--source.php-->
<br><img src="https://i.loli.net/2018/11/01/5bdb0d93dc794.jpg" /></body>
</html>
发现了一个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\" />";
}
?>
(看了一下大佬的wp才知道这是一个phpmyadmin 4.8.1 远程文件包含漏洞(CVE-2018-12613),有兴趣的自己去看一下)
好的,代码审计跑不了了,看到白名单里还有一个hint.php,也打开看一下
告诉了我们flag的位置,但是目前还没什么用,回去source.php找一下关键代码
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\" />";
}
可以看到有个include,应该就是文件包含漏洞了,如果以request方式传入的file参数通过了if,就能通过include函数包含文件。empty和is_string函数作用不大,用来检测是否为空和是否为字符串的,关键是它的checkfile函数,如果它的结果是true的话就能通过if了。所以我们回去看看它定义的checkfile函数
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;
}
可以看到,有三个地方可以return true
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;
}
第一个是传入的file参数值在白名单(source.php,hint.php)里面就能返回true,payload为?file=source.php或者?file=hint.php
这样虽然通过了if但并不能包含我们想要的flag文件,所以我们看一下第二个
这里出现了两个陌生函数mb_substr和mb_strpos,查了一手资料,两者结合在这里的意思就是在参数值后面加一个问号,然后截取第一个问号前面的字符串和白名单进行比对,通过就能返回true,所以我们构造payload:?file=source.php?/../../../../../ffffllllaaaagggg或者?file=hint.php?/../../../../../ffffllllaaaagggg,通过目录穿越拿到flag
那么这里就会有人问了,include里面包含的是整个file的值,为什么source.php?/../../../../../ffffllllaaaagggg这个形式的文件可以被成功包含呢?这就涉及到一个特殊的目录穿越机制了,我们以cmd的cd命令为例
dir列出当前目录
然后我们试着进入一个不存在的目录,并通过/../返回上一层
可以看到,它并没有报错 ,dir看一下
可以看到,这个命令并没有检查123?这个目录是否存在,它只是通过后面的/../知道你的目的是访问一个目录并返回,所以它并没有改变我们当前的位置,与我们这道题的目录穿越有共同之处,所以payload用的就是这个原理 ,当payload为?file=source.php?/../source.php时,就可以成功包含source.php
(原本的source.php界面下多了一个source.php)
所以source.php?/../的作用除了能成功通过白名单返回true外,还能让我们回到当前的一个目录下,然后再根据hint.php的提示,大胆猜想还要返回四个目录,也就是我们一开始的payload
(但是有大佬说自己搭建的靶场不能通过这个payload拿到flag,因为include函数里面好像不能有?这样的特殊字符,但是buu这个不知道为什么就可以,所以我们直接去看一下第三种情况)
第三种情况也是截取问号前面的字符串与白名单对比,但是它多了一步urldecode,它会识别url编码并且进行解码,所以我们将问号进行一次url编码得到%3F,放入payload中,也就是?file=source.php%3F/../../../../../ffffllllaaaagggg或者将source换成hint。但是问题又来了,因为浏览器会自动识别url编码并进行一次解码,所以设置的payload经过了浏览器解码后又变成了回到了第二个return true,拿到flag
所以,第三种情况的正确玩法应该是这样的,我们对问号进行二次编码(%253F),因为浏览器会自动识别url编码并进行一次解码, 所以我们传入?file=source.php%253F/../../../../../ffffllllaaaagggg,浏览器自动解码变成?file=source.php%3F/../../../../../ffffllllaaaagggg,经过urldecode后变成?file=source.php?/../../../../../ffffllllaaaagggg,这样就能通过白名单返回true,并且include包含的内容经过了浏览器的一次解码后变成source.php%3F/../../../../../ffffllllaaaagggg,没有特殊字符,就可以成功包含拿到flag(这就是大佬自建靶场唯一一个能拿到flag的情况)