Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Summary


Excerpt

Showcase app vulnerability allows remote command execution



Who should read this

All Struts 2 developers

Impact of vulnerability

Remote command execution

Maximum security rating

Important

Recommendation

Developers should immediately upgrade to Struts 2.3.14.3

Affected Software

Struts Showcase App

...

2.0.0

...

-

...

Struts

...

Showcase App 2.3.14

...

.2 

Reporter

Xgc Kxlzx,

...

Alibaba

...

Security

...

Team

CVE Identifier

CVE-2013-1965

Original Description

Reported directly to security@a.o

Problem

OGNL provides, among other features, extensive expression evaluation capabilities.
A request that included a specially crafted request parameter could be used to inject arbitrary OGNL code into a property, afterward used as request parameter of a redirect address, which will cause a further evaluation.

OGNL evaluation was already addressed in S2-003 and S2-005 and S2-009, but, since it involved just the parameter's name, it turned out that the resulting fixes based on whitelisting acceptable parameter names and denying evaluation of the expression contained in parameter names, closed the vulnerability only partially.

The second evaluation happens when redirect result reads it from the stack and uses the previously injected code as redirect parameter.
This lets malicious users put arbitrary OGNL statements into any unsanitized String variable exposed by an action and have it evaluated as an OGNL expression to enable method execution and execute arbitrary methods, bypassing Struts and OGNL library protections.

Proof of concept

  1. Run struts2-showcase
  2. Open url: http://localhost:8080/struts2-showcase/skill/edit.action?skillName=SPRING-DEV

...

  1. write

...

  1. skill

...

  1. name

...

  1. to

...

  1. %{expr}

...

  1. for

...

  1. example:

...

  1. Code Block

...

  1. %{(#_memberAccess['allowStaticMethodAccess']=true)(#context['xwork.MethodAccessor.denyMethodExecution']=false) #hackedbykxlzx=@org.apache.struts2.ServletActionContext@getResponse().getWriter(),#hackedbykxlzx.println('hacked by kxlzx'),#hackedbykxlzx.close())}
    


  2. submit the form

The issue, in order to work, need a redirect result defined as the following:

Code Block
{code}
# submit the form

The issue, in order to work, need a redirect result defined as the following:
{code}
<action name="save" class="org.apache.struts2.showcase.action.SkillAction" method="save">
    <result type="redirect">edit.action?skillName=${currentSkill.name}</result>
</action>
{code}    

h2. Solution

The regex pattern inside the ParameterInterceptor was changed to provide a more narrow space of acceptable parameter names. 
Furthermore the new setParameter method provided by the value stack will allow no more eval expression inside the param names.


{warning}
*It is strongly recommended to upgrade to [Struts 2.3.1.2|http://struts.apache.org/download.cgi#struts2312], which contains the corrected OGNL and XWork library.*
{warning}

In case an upgrade isn't possible in a particular environment, there is a configuration based mitigation workaround:

h3. Possible Mitigation Workaround: Configure ParametersIntercptor in struts.xml to Exclude Malicious Parameters

The following additional interceptor-ref configuration should mitigate the problem when applied correctly, allowing only simple navigational expression:
{code}
<interceptor-ref name="params">
	<param name="acceptParamNames">\w+((\.\w+)|(\[\d+\])|(\['\w+'\]))*</param>
</interceptor-ref>
{code}
{note:title}
Beware that the above pattern breaks [the type conversion support for collection and map|http://struts.apache.org/2.3.1.1/docs/type-conversion.html#TypeConversion-CollectionandMapSupport] (those parameter names should be attached to acceptParamNames variable).
For this configuration to work correctly, it has to be applied to *any params interceptor ref in any stack an application is using*.
E.g., if an application is configured to use defaultStack as well as paramsPrepareParamsStack, you should copy both stack definitions from struts-default.xml to the application's struts.xml config file and apply the described excludeParams configuration for each params interceptor ref, that is *once for defaultStack and twice for paramsPrepareParamsStack*
{note}

JUnit Version

Code Block
java
java

public void testUnsecureRedirect() {
    final String pwnDir = "/tmp/PWNAGE";
    final Map<String, String> fakeAction = new HashMap<String, String>() {
        {
            put("skillName", "%{(#context['xwork.MethodAccessor.denyMethodExecution']=false)(#_memberAccess['allowStaticMethodAccess']=true)(@java.lang.Runtime@getRuntime().exec('mkdir " + pwnDir + "'))}");
        }
    };

    String location = "/context/edit.action?skillName=true";
    responseMock.expectAndReturn("encodeRedirectURL", C.anyArgs(1), location);
    responseMock.expect("sendRedirect", C.args(C.eq(location)));
    requestMock.expectAndReturn("getAttribute", C.args(C.eq("javax.servlet.include.servlet_path")), location);

    ValueStack stack = ai.getStack();
    stack.push(fakeAction);

    view.setLocation("edit.action?skillName=${skillName}");
    view.setParse(true);


    try {
        view.execute(ai);

        requestMock.verify();

        File pwn = new File(pwnDir);
        boolean exists = pwn.exists();
        FileUtils.deleteDirectory(pwn);
        assertFalse("Remote exploit: The PWN folder has been created", exists);

        Object dme = stack.getContext().get("xwork.MethodAccessor.denyMethodExecution");

        assertTrue("DenyMethodExecution has been disabled", dme == null || BooleanUtils.toBoolean(dme.toString()));

    } catch (Exception e) {
        e.printStackTrace();
        fail();
    }
}

Solution

The OGNLUtil class was changed to deny eval expressions by default.

Warning

It is strongly recommended to upgrade to Struts 2.3.14.3, which contains the corrected OGNL and XWork library.