官方说明 : https://blog.thinkphp.cn/869075
漏洞阐述
漏洞原因:
tp在没有配置路由的情况下的访问方式如下
module/controller/action
当controller设置为tp内的app类时,即\think\app时,可以执行\think\app内的任一方法,而在\think\app类中存在
invokeFunction方法,可以用字符串形式传入参数,并将字符串转化为函数执行,从而产生写入漏洞。
攻击者可以利用这个漏洞,写入.php后缀的后门入口文件。
官方建议的修复方式:
ThinkPHP5.*版本发布安全更新
if ($controller && !preg_match('/^[A-Za-z](\w|\.)*$/', $controller)) {
throw new HttpException(404, 'controller not exists:' . $controller);
}官方的修复原理:http请求的controller名称中如果有斜线则报错,即在没有斜线的情况下,会拼接应用命名控制,这样就防止了直接访问think\app类。PS: 这个正则过滤的杀伤范围有点大。但是如果在应用内或其它地方使用了Loader::controller()方法,以参数方式传入完整类名,依然可以实现实例化任意已经加载的类。
官方的5.1内核修复版本 5.1.31
我建议的修复方法
修复方法
修改Loader类中的controller方法
由
if (false !== strpos($name, '\\')) {
$class = $name;
$module = Request::instance()->module();
} else {
if (strpos($name, '/')) {
list($module, $name) = explode('/', $name);
} else {
$module = Request::instance()->module();
}
$class = self::parseClass($module, $layer, $name, $appendSuffix);
}改为if (strpos($name, '/')) {
list($module, $name) = explode('/', $name);
} else {
$module = Request::instance()->module();
}
$class = self::parseClass($module, $layer, $name, $appendSuffix);修复原理修改后,http的调用只能使用app内的controller,即命名空间限定在app中,不能实例化其它类。
强调:这样改的缺点是Loader类的controller方法只能实例化当前应用内的控制器类,即限定了类的命名空间。
修复示例
github-commit : https://github.com/AxiosCros/tpr-fr
不修改内核的改动思路
老版本不想更新内核的话,可以监听app_begin行为,判断请求的controller名称中没有斜线就行了。
