Loading

ThinkPHP5.x命令执行漏洞分析

0x01 Start
2018.12.10晚上,看到有人发tp5命令执行,第一眼看到poc大致猜到什么原因,后来看到斗鱼src公众号的分析文章。这里分析记录一下。

0x02 简单分析
tp的框架启动不具体说了,这里从App::run开始分析。App.php第116行调用routeCheck函数,该函数返回的内容为:

array(2) { ["type"]=> string(6) "module" ["module"]=> array(3) { [0]=> string(5) "index" [1]=> string(9) "think\app" [2]=> string(14) "invokefunction" } }

在routeCheck调用$request->path();获取兼容模式s传入的模块/控制器/方法
tp5rce-pathinfo

在routeCheck中会读取route.php中的路由并进行匹配传入的路由,不成功会调用Route::parseUrl处理tp5rce-parsepath
,在parseUrl函数1226行调用parseUrlPath函数处理模块控制器方法串,使用/分割
tp5rce-split
parseurl中把parseUrlPath函数返回的数组:

list($path, $var) = self::parseUrlPath($url);
$path = array(3) { [0]=> string(5) "index" [1]=> string(10) "\think\app" [2]=> string(14) "invokefunction" }

$module = Config::get('app_multi_module') ? array_shift($path) : null;
//$module = index

$controller = !empty($path) ? array_shift($path) : null;
//$controller = \think\app

$action = !empty($path) ? array_shift($path) : null;
$action = invokefunction

所以这个地方payload s=module/controller/action
s = index/think\app/invokefunction
controller不使用/而使用\分割成命名空间形式即可,这个地方不要第一个\也可以

然后$route = [$module, $controller, $action]; 封装返回

tp5rce-dopathtp5rce-dopath

这里往下调用处理路由就结束了,会return上面封装那个数组

array(2) { ["type"]=> string(6) "module" ["module"]=> array(3) { [0]=> string(5) "index" [1]=> string(9) "think\app" [2]=> string(14) "invokefunction" } }


回到App.php 141行进行执行阶段$data = self::exec($dispatch, $config);

type为module,case分支执行:

$data = self::module(
   $dispatch['module'],
   $config,
   isset($dispatch['convert']) ? $dispatch['convert'] : null
);

在module函数中578行进行了控制器实例化
tp5rce-instance
在592行进行了函数调用
tp5rce-callfunc

0x03 3.x系列呢
看完5.x我又跑回去看了3.x,从3.2.3一直找到2.2全部由下图代码
3-2-3

posted @ 2019-12-16 16:39  don0t  阅读(796)  评论(0编辑  收藏  举报