开个坑吧,把buu上的审计题好好做一遍
平台:buuoj.cn
1.[HCTF 2018]WarmUp
buu上solves最多的一道题。
开局一张滑稽图,F12查看源码,发现提示:source.php
查看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\" />";
}
?>
有个hint.php,打开看下:
得知flag在ffffllllaaaagggg内,先留着。
开始分析代码:
先看这个类:
class emmm
{
public static function checkFile(&$page)//将传入的参数赋给$page
{
$whitelist = ["source"=>"source.php","hint"=>"hint.php"];//声明白名单$whitelist
if (! isset($page) || !is_string($page)) {
//判断$page是否不存在或者不是字符串
echo "you can't see it";
return false;
}
if (in_array($page, $whitelist)) {
//判断$page是否在白名单内
return true;
}
$_page = mb_substr(
//如果$page里有"?",截取$page中'?'前面部分,若无则截取整个$page
$page,
0,
mb_strpos($page .'?', '?')//mb_strpos():返回要查找的字符串在另一个字符串中首次出现的位置
);
if (in_array($_page, $whitelist)) {
//判断截取之后的$_page是否在白名单内
return true;
}
$_page = urldecode($page);//url解码$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'])//file不为空
&& is_string($_REQUEST['file'])//file是字符串
&& emmm::checkFile($_REQUEST['file'])//通过emmm类的checkfile检测
) {
include $_REQUEST['file'];//包含该文件
exit;
} else {
echo "<br><img src=\"https://i.loli.net/2018/11/01/5bdb0d93dc794.jpg\" />";
//否则滑稽
}
分析一下,前面已经提示了flag在ffffllllaaaagggg内,再结合第二部分的流程,因此如果我们能让ffffllllaaaagggg通过checkfile的检测,那么我们就可以包含这个文件从而得到flag,所以重点在checkfile上。
来看checkfile,要通过checkfile就要使返回值为true,也就是最后截断后的_page返回为true,_page经过三次白名单检测,两次"?“截断,和一次url解码,一层一层来。
第一次白名单:source.php或者index.php都行,以source.php为例
第一次截断加上截断后第二次白名单检测:source.php?,截断后source.php在白名单内检测通过
利用file包含:source.php?file=(包含文件)
第二次截断加白名单:source.php?file=source.php?,截断后source.php在白名单内检测通过
把”?“url编码,这里有个隐藏点,PHP中_GET、_POST、_REQUEST这类函数在提取参数值时会URL解码一次,所以我们应当对”?"进行两次编码,source.php?file=source.php%253F
然后把ffffllllaaaagggg写上去,source.php?file=source.php%253F…/…/…/…/ffffllllaaaagggg,这里的…/我一开始以为是试出来的,后来参考这位师傅得知这里是php在解析时将 source.php%253F/ 视作了一个文件夹,因此再加上var/www/html三级目录,自然就是四次…/
因此最后得到payload为
/source.php?file=source.php%253F/../../../../ffffllllaaaagggg
得到flag:
不知道为啥这题sloves最多,师傅们戏称的buu劝退题很真实。