Skip to content

Latest commit

 

History

History
97 lines (57 loc) · 4.15 KB

File metadata and controls

97 lines (57 loc) · 4.15 KB

Struts2远程代码执行(S2-057)漏洞分析

Category Research Language Vuln Component Vuln Type Vuln ID Tag Timestamp Progress

漏洞编号

CVE-2018-11776

补丁分析

在github上进行Tag对比。

01.png

分析patch,定位问题点

02.png

环境搭建

根据漏洞作者的博客描述,直接使用Struts2-2.3.34的showcase项目,修改struts-actionchaining.xml。

03.png

使用${1+1}验证漏洞存在。

04.png

漏洞分析

DefaultActionMapper调用parseNameAndNamespace()解析namespacename。当alwaysSelectFullNamespacetrue时,namespace的值可以通过URL控制。

05.png

Action执行结束时,调用ServletActionRedirectResult.execute()进行重定向Result的解析,通过ActionMapper.getUriFromActionMapping()重组namespacename后,由setLocation()将带namespacelocation放入父类StrutsResultSupport中。

06.png

StrutsResultSupport拿到location后,通过TextParseUtil.translateVariables()调用OgnlTextParser.evaluate()解析执行URL中的OGNL表达式,导致代码执行。

07.png

利用条件

最小条件:

  1. alwaysSelectFullNamespace值为true
  2. Struts2配置文件中,action元素未设置namespace属性,或使用了通配符

漏洞场景:

  • 在Struts2配置文件中,对未正确配置的action元素提供如下三类返回元素:
    1. Redirect Action
    2. Action Chaining
    3. Postback Result

08.png

  • 在模板中使用url元素标记:

09.png 10.png

构造PoC

使用Struts2老版本的PoC无法正常弹出计算器,会在获取#context时得到null值,导致['com.opensymphony.xwork2.ActionContext.container']求值时的source为空,抛出异常。

通过跟踪OGNL底层代码发现,在比较新的版本的OGNL包中,OgnlContext移除了CONTEXT_CONTEXT_KEYCLASS_RESOLVER_CONTEXT_KEYMEMBER_ACCESS_CONTEXT_KEY,使OGNL表达式无法继续使用#context#_classResolver#_memberAccess来获得相应对象。

11.png 12.png

获取不到context就无法进行后续Struts2的沙盒绕过。通过分析,发现在#request域下的struts.valueStack对象中存在context属性,因此对S2-045的Payload进行一下简单改造即可。

13.png

成功弹出计算器。

14.png

另外,由于Struts2.5中,几个excluded的map使用的是unmodified collection,调用clear()时会抛出异常,因此该PoC只适用于Struts2.3环境,2.5可以使用setXXX()覆盖map值。

再多说一嘴,Struts2的showcase项目没有通过大家知道的struts.mapper.alwaysSelectFullNamespace配置项控制alwaysSelectFullNamespace的值,而是通过@Inject进行了IoC。

参考

  1. Apache Struts2 S2-057
  2. CVE-2018-11776: How to find 5 RCEs in Apache Struts with Semmle QL
  3. Semmle Discovers Critical Remote Code Execution Vulnerability in Apache Struts (CVE-2018-11776)