最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Struts2 S2-052漏洞分析

旗下网站admin56浏览0评论

Struts2 S2-052漏洞分析

Struts2 S2-052漏洞分析

0x00 调试环境搭建

使用官方的rest-sample即可,下载2.5.12版本的源码.zip,然后将apps下面的rest-showcase源码脱下来。

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

pom.xml

 

<!-- struts2依赖包 -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.5.12</version>
</dependency>
<!-- struts restful 依赖包 -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-convention-plugin</artifactId>
<version>2.5.12</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-rest-plugin</artifactId>
<version>2.5.12</version>
</dependency>

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

 

 

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
".5.dtd">
<struts>
<!-- Overwrite Convention -->
<constant name="struts.convention.action.suffix" value="Controller"/>
<constant name="struts.convention.action.mapAllMatches" value="true"/>
<constant name="struts.convention.default.parent.package" value="rest-default"/>
<constant name="struts.convention.package.locators" value="action"/>
<constant name="struts.convention.result.path" value="/WEB-INF/"/>
</struts>

 

 

 

其他的action文件、jsp文件复制过来到maven工程的对应目录即可,右键启动项目,然后浏览器可以访问到:http://127.0.0.1:8080/struts2-052/orders,说明调试环境搭建成功。

 

0x01 漏洞分析

根据该漏洞发现者文章,是一个叫ContentHandler的东西有问题。

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

 

在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为例:

 

 

 

这里实际上就是把XML和java对象之间进行转化,比较专业的词汇叫“marshal“和”“unmarshal”。从以往的例子看,这种情况导致的命令执行也不是一次两次了,json转换库如fastjson,jackson都有过漏洞,

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

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

 


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

 

 

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

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

(1)下载源码

(2)maven编译 mvn clean package -DskipTests

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

 

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

 

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

 

0x02 后话

关于如何从XML到命令执行的过程,实际上是Moritz Bechler大神的一个paper,.pdf,这个paper随着marshalsec工具发布。

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

 

参考

 

发布评论

评论列表(0)

  1. 暂无评论