ThinkCMF X2.2.2多处SQL注入漏洞分析

2018-12-12 20:32:3234636人阅读

 1.     漏洞描述

ThinkCMF是一款基于ThinkPHP+MySQL开发的中文内容管理框架,其中X系列基于ThinkPHP 3.2.3开发,最后更新到2.2.2版本。最近刚好在渗透测试项目中遇到这个CMS,便审了下源码发现多处SQL注入漏洞,在Github给项目方提issues后,提交到CVE官方后很快就拿到了分配的多个编号:CVE-2018-19894、CVE-2018-19895、CVE-2018-19896、CVE-2018-19897、CVE-2018-19898。

2.     影响版本

ThinkCMF X2.2.2(https://github.com/thinkcmf/cmfx)

3.     漏洞分析

3.1  CommentadminController.class.php check、delete方法SQL注入(CVE-2018-19894)

漏洞位于/application/Comment/Controller/CommentadminController.class.php的check、delete方法, 以62行为例,$_POST['ids']参数通过join后,传递到where语句中,但并没有使用where语句的in方法,而是直接拼接到SQL语句中,导致SQL注入。

1.png

测试Pyload为

http://127.0.0.1/cmfx/index.php?g=Comment&m=commentadmin&a=check&check=1

POST:  ids[]=1&ids[]=2 and updatexml(1,concat(0x7e,(SELECT user()),0x7e),1)

2.png

3.2 NavController.class.php中edit_post方法SQL注入(CVE-2018-19895)

跟进`application/Admin/Controller/NavController.class.php`,在文件的173行。`$parentid`直接由`$_POST['parentid']`传递进来,随后被直接拼接到where语句中。

3.png

测试Payload为

http://127.0.0.1/cmfx/index.php?g=Admin&m=nav&a=edit_post

POST: parentid=1 and updatexml(1,concat(0x7e,(SELECT user()),0x7e),1)

4.png

3.3 SlideController.class.php delete方法SQL注入(CVE-2018-19896)

在application/Admin/Controller/SlideController.class.php的93行,delete方法中,$_POST['ids']通过implode方法变成字符串,随后直接拼接进入where语句的in子句中。

5.png

测试payload:

http://127.0.0.1/cmfx/index.php?g=Admin&m=slide&a=delete

POST: ids[]=1&ids[]=0 and updatexml(1, concat(0x7e,user(),0x7e),1)

6.png

3.4 AdminbaseController.class.php中_listorders方法存在SQL注入(CVE-2018-19897)

_listorders方法用于排序,在很多地方被调用。这里以LinkController.class.php中的listorders()为例进行分析,这里主要用做友情链接的排序。我们以phpstorm+phpstudy+ xdebug打下断点,一步步追踪。测试payload为:

http://127.0.0.1/cmfx/index.php?g=Admin&m=Link&a=listorders

POST: listorders[key][0]=exp&listorders[key][1]=0 and updatexml(1, concat(0x7e,user(),0),1)

7.png

首先进入application/Admin/Controller/LinkController.class.php 70行的listorders方法,71行调用父类的_listorders()方法。

8.png

跟到application/Admin/Controller/LinkController.class.php 166行的_listorders()方法,$_POST['listorders']为二维数组传递给$ids,经过foreach循环,输入的payload进入$data中,仍然为二维数组,而$data则进入save方法。

9.png

跟进到simplewind/Core/Library/Think/Model.class.php 396行的save方法,该方法为thinkphp的核心函数。由于$data不为空,跳过之前的很多判断直接到452行,$data和$options进入update方法,$data仍然为二维数组不变。

 

10.png

跟进到simplewind/Core/Library/Think/Db/Driver.class.php 893行的update()方法,$data经过parseSet方法后拼接到$sql中。跟到371行parseSet方法的定义,$data经过foreach循环后,$val变为一维数组,$key为键值。而当$val为数组并且数组的第一个元素为exp时,$val[1]会和$key直接用等号拼接传递到$set,387行数组$set被逗号implode后拼接到SET子句中。

 

11.png

返回到update方法,SET子句被拼接到$sql中,最终执行的sql语句为

UPDATE `cmf_links` SET `listorder`=0 and updatexml(1, concat(0x7e,user(),0),1) WHERE `link_id` = 'key'

 

12.png

3.5  ArticleController.class.php edit_post方法SQL注入(CVE-2018-19898)

ThinkCMF X2.2.2是基于ThinkPHP 3.2.3开发的,ThinkPHP 3.x版本之前被爆出存在bind注入,这个漏洞就是ThinkPHP3.x注入的典型案例。漏洞位于前台文章编辑处,测试payload如下:

http://127.0.0.1/cmfx/index.php?g=portal&m=article&a=edit_post

POST: post[id][0]=bind&post[id][1]=0 and updatexml(1, concat(0x7e,user(),0x7e),1)-- -

在application/Portal/Controller/ArticleController.class.php 182行,输入的参数通过I("post.post")传递到$article;跟进到/simplewind/Core/Common/functions.php的I方法定义,在428行,会调用think_filter方法对参数进行过滤。

 13.png

 由于正则字符中没有匹配bind,所以导致了后面的注入漏洞,ThinkPHP官方的修复措施就是在此处匹配时加上了bind。接下来进入典型的数据库更新操作了,$articles为多维数组包含payload。

14.png

我们F7继续跟进,会进入simplewind/Core/Library/Think/Model.class.php的where方法,给$this->options['where']赋值后,返回当前对象。随后进入simplewind/Core/Library/Think/Model.class.php的save方法,随后执行到update方法

 

15.png

继续往下,进入到parseSet方法,可以看到传递的参数在进行参数绑定操作,其中时间字符串被赋于占位符0,此处会进行循环操作,将所有的参数进行绑定。

16.png

随后进入parseWhere函数

17.png

分析parseWhere后,发现会执行 parseWhereItem方法,当$exp=='bind'的时候即$val[0]=='bind',会对$val[1]进行拼接,仔细看这里会多一个`:`,表示为参数绑定时的占位符。

18.png

这里也就理解为什么第二个数组构造的时候需要添加一个数字0,这是由于parseSet方法以及赋值了一个占位符:0,用来替代时间字符串,在随后的SQL语句执行阶段可被用来赋值,否则占位符没被赋值会因语法问题产生报错。

 

19.png

随后可以看到bindValue将:0绑定为时间字符串,实际上这里有三个参数需要绑定,因此第二个数组的首位值可以为0、1、2。

 

20.png

执行时产生XPATH异常报错,得到我们想要的数据。

21.png

 

4.     修复建议

由于ThinkCMF X系列在2.2.2版本后已经不再更新,建议用户及时升级到Think CMF5。


本文来自百度安全SiemPent Team,转载请注明出处及本文链接

0
现金券
0
兑换券
立即领取
领取成功