Struts2 S2-052漏洞分析

Track-宁邪   ·   发表于 2018-03-05 17:40:47   ·   漏洞文章

漏洞概述:

漏洞编号:CVE-2017-9805(S2-052)

影响版本:Struts 2.5 - Struts 2.5.12

漏洞概述:问题出现在struts2-rest-plugin插件XStreamHandler处理器中的toObject()方法,其中未对传入的值进行任何限制,在使用XStream反序列化转换成对象时,导致任意代码执行漏洞。

0x00 调试环境搭建

使用官方的rest-sample即可,下载2.5.12版本的源码

https://github.com/apache/struts/archive/STRUTS_2_5_12.zip

然后将apps下面的rest-showcase源码脱下来。

Eclipse中新建一个maven工程,web.xml,pom.xml和struts.xml如下:

pom.xml

01<!-- struts2依赖包 --> 
02<dependency
03<groupId>org.apache.struts</groupId
04<artifactId>struts2-core</artifactId
05<version>2.5.12</version
06</dependency
07<!-- struts restful 依赖包 --> 
08<dependency
09<groupId>org.apache.struts</groupId
10<artifactId>struts2-convention-plugin</artifactId
11<version>2.5.12</version
12</dependency
13<dependency
14<groupId>org.apache.struts</groupId
15<artifactId>struts2-rest-plugin</artifactId
16<version>2.5.12</version
17</dependency

struts.xml(src/main/resources/下)

01<?xml version="1.0" encoding="UTF-8" ?> 
02<!DOCTYPE struts PUBLIC 
03"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" 
05<struts
06<!-- Overwrite Convention --> 
07<constant name="struts.convention.action.suffix" value="Controller"/> 
08<constant name="struts.convention.action.mapAllMatches" value="true"/> 
09<constant name="struts.convention.default.parent.package" value="rest-default"/> 
10<constant name="struts.convention.package.locators" value="action"/> 
11<constant name="struts.convention.result.path" value="/WEB-INF/"/> 
12</struts

其他的action文件、jsp文件复制过来到maven工程的对应目录即可,右键启动项目,然后浏览器可以访问到:

http://127.0.0.1:8080/struts2-052/orders,说明调试环境搭建成功。

0x01 漏洞分析

根据该漏洞发现者文章https://lgtm.com/blog/apache_struts_CVE-2017-9805所述,是一个叫ContentHandler的东西有问题。

在2.5.12源码中搜索这个字符串:

strutss2052-1

在struts-plugin.xml配置了很多的bean,这些bean按照content-type进行分类,并唯一指定一个具体的Handler。

这些Handler都实现了ContentTypeHandler接口。

从API DOC上描述

Handles transferring content to and from objects for a specific content type

来看,这个ContentTypeHandler实际上是按照Content-type的不同,将请求的数据丢给指定的子类进行处理,具体是怎么处理的呢,以XStreamHandler为例:

strutss2052-2

这里实际上就是把XML和java对象之间进行转化,比较专业的词汇叫“marshal“和”“unmarshal”。

从以往的例子看,这种情况导致的命令执行也不是一次两次了,json转换库如fastjson,jackson都有过漏洞,这次换成了Struts2里的XML的对象转换。

其实就是XStreamHandler的toObject方法中触发了漏洞,我们就先在这行代码下断点,执行poc之后,会发现断点生效了。

我们来看看调用函数流程信息:

strutss2052-3

在Restful模式下,对Action的路由处理是使用Rest系列的代码,这里是ContentTypeInterceptor类调用的XStreamHandler方法。我们来看看上层代码中的intercept方法:

strutss2052-4

首先是从HttpServletRequest里判断ContentType,可以很清晰的看到,通过ContentType将request的字节流分发给对应的Handler进行处理。当ContentType为application/xml的时候,

很自然的就分发给了XStreamHandler这个类来处理,这个类没有进行任何校验,直接进行了转换。我们可以用marsshalsec工具来生成payload。

(1)下载源码https://github.com/mbechler/marshalsec

(2)maven编译 mvn clean package -DskipTests

(3)去target目录下找到jar文件,执行:

1java -cp marshalsec-0.0.1-SNAPSHOT-all.jar marshalsec.XStream ImageIO "calc" > 1.txt 

然后将这段XML用POST发给struts2-rest,当然,ContentType要设置为xml的,然后就可以触发了。当命令中有空格时,提交多个节点即可。

0x02 后话

关于如何从XML到命令执行的过程,实际上是Moritz Bechler大神的一个paper,

https://github.com/mbechler/marshalsec/blob/master/marshalsec.pdf

这个paper随着marshalsec工具发布。

这里只分析Struts2的漏洞原因,关于XML->RCE过程,大家可以仔细阅读这个paper进行深入了解。


打赏我,让我更有动力~

0 条回复   |  直到 2018-3-5 | 1829 次浏览
登录后才可发表内容
返回顶部 投诉反馈

© 2016 - 2024 掌控者 All Rights Reserved.