Jump to content
Nytro

Java 0day analysis (CVE-2012-4681)

Recommended Posts

Posted (edited)

Java 0day analysis (CVE-2012-4681)

Tuesday, August 28, 2012

(This post brought to you by Esteban, Immunity's in-house Java friend)

Summary

A couple of days ago, a Java 0day was found running like crazy in the wild. While a lot of defense bunnies where asking "WWMAD" (What will my Antivirus do?), we decide to dive into Java for the details of the vulnerability and as we expected, the unpatched vulnerabilities used in the Gondvv exploit were more than one (When we said, "dive deep into Java", we actually meant open our new Infiltrate 2013 Master Class slide deck which will include a full day of Java auditing).

The first bug was used to get a reference to sun.awt.SunToolkit class that is restricted to applets while the second bug invokes the getField public static method on SunToolkit using reflection with a trusted immediate caller bypassing a security check.

The beauty of this bug class is that it provides 100% reliability and is multiplatform. Hence this will shortly become the penetration test Swiss knife for the next couple of years (as did its older brother CVE-2008-5353).

As a final note, the bug was introduced in Java 7.0 released in July 28, 2011. While you are feeling the rush of blood going through your veins while by getting all those shell being pop, think that somewhere not far way (Probably a 10hs flight from some of the major airports in Norte Americana) was enjoying it non-stop for quite some time now.

Introduction

As the “Secure Coding Guidelines” document [1] states, it is important to understand how the permissions are checked in the Java security model. (Please readguideline 9-1).

Many operations in the JDK perform permission checks before executing. Whenever a call to java.security.AccessController.checkPermission method is performed, the complete call stack that exists at that moment is analyzed.

If any of the callers in the stack do not have the required privileged an exception is raised.

When we are running code in an Applet in our browser, there is a context that has very restricted permissions.

This means that if there is any caller in the stack that is part of our applet, the permission checks will fail (unless there is a doPrivileged code block, but let's leave that out for now).

Section “9 – Access Control” in the “Secure Coding Guidelines” document [1] together with the “Java Security Architecture” document [2] will give you a complete insight on how all this works.

The Gondvv exploit

A PoC for this 0 day exploit quickly began to spread when Joshua J. Drake posted it on Twitter https://twitter.com/jduck1337/status/239875285913317376.

By analyzing this implementation we can clearly see how the exploitation is done and where the vulnerabilities are really located.

The first thing we notice, is that most of the online analysis talks about one vulnerability where we saw two 2 vulnerabilities being exploited to achieve full execution on a target.

Basically the exploit is creating an java.security.AccessControlContext instance with a java.security.ProtectionDomain that has full permissions and then replace the actual AccessControlContext of a java.beans.Statement instance to be able to execute code with full privileges.

So let's take a better look at each part to understand what is happening under the hood.

In the java.beans.Statement implementation we can see that the AccessControlContext instance is a private final field and it gets its value by calling AccessController.getContext().


[TABLE="width: 100%"]
[TR]
[TD="width: 100%"] [FONT=Monospace][B]public class[/B] Statement {[/FONT]

[FONT=Monospace][B]private static[/B] Object[] [I]emptyArray[/I] = [B]new[/B] Object[]{};[/FONT]

[FONT=Monospace][B]static[/B] ExceptionListener [I]defaultExceptionListener[/I] = [B]new[/B] ExceptionListener() {[/FONT]
[FONT=Monospace][B]public void[/B] exceptionThrown(Exception e) {[/FONT]
[FONT=Monospace]System.[I]err[/I].println(e);[/FONT]
[FONT=Monospace]System.[I]err[/I].println("Continuing ...");[/FONT]
[FONT=Monospace]}[/FONT]
[FONT=Monospace]};[/FONT]


[FONT=Monospace][B]private final[/B] AccessControlContext acc = AccessController.[I]getContext[/I]();[/FONT]
[FONT=Monospace][B]private final[/B] Object target;[/FONT]
[FONT=Monospace][B]private final[/B] String methodName;[/FONT]
[FONT=Monospace][B]private final[/B] Object[] arguments;[/FONT]
[FONT=Monospace]ClassLoader loader;[/FONT]


[FONT=Monospace][...][/FONT]


[FONT=Monospace]}[/FONT]
[/TD]
[/TR]
[/TABLE]

That call to the getContext method will set the AccessControlContext to an applet context that has restrictions with a limited ProtectionDomain which of course is not privileged at all.

Back in 2010 Sami Koivu published information about a Java vulnerability (CVE-2010-0840) that built a “trusted method chain” [4].

You can see in the article that the exploitation of that vulnerability also made use of the java.beans.Statement class.

The article also explains that the fix was to add an AccessControlContext field to the Statement class setting its value to the applet context when creating an instance thus avoiding the full trusted chain.

This AccessControllContext field added in that fix is exactly what this new 0 day exploit is replacing in order to be able to execute code with full permissions.

But how can a private field be changed?

The trick here is to use the sun.awt.SunToolkit class which contains a very interesting public static method:

[TABLE="width: 100%"]
[TR]
[TD="width: 100%"] [FONT=Monospace][B]public static[/B] Field getField([B]final[/B] Class klass, [B]final[/B] String fieldName) {[/FONT]
[FONT=Monospace][B]return[/B] AccessController.[I]doPrivileged[/I]([B]new[/B] PrivilegedAction<Field>() {[/FONT]
[FONT=Monospace][B]public[/B] Field run() {[/FONT]
[FONT=Monospace][B]try[/B] {[/FONT]
[FONT=Monospace]Field field = klass.getDeclaredField(fieldName);[/FONT]
[FONT=Monospace][B]assert[/B] (field != [B]null[/B]);[/FONT]
[FONT=Monospace]field.setAccessible([B]true[/B]);[/FONT]
[FONT=Monospace][B]return[/B] field;[/FONT]
[FONT=Monospace]} [B]catch[/B] (SecurityException e) {[/FONT]
[FONT=Monospace][B]assert false[/B];[/FONT]
[FONT=Monospace]} [B]catch[/B] (NoSuchFieldException e) {[/FONT]
[FONT=Monospace][B]assert false[/B];[/FONT]
[FONT=Monospace]}[/FONT]
[FONT=Monospace][B]return null[/B];[/FONT]
[FONT=Monospace]}[/FONT]
[FONT=Monospace]});[/FONT]
[FONT=Monospace]}[/FONT]
[/TD]
[/TR]
[/TABLE]

We can see that it is using reflection to get fields and the complete implementation is inside a doPrivileged block. This getField method can be used to get any field on a class and the good thing is that it can even retrieve private ones.

Well this is of course very useful, it's important to notice that the classes that are part of certain packages are restricted for applets and cannot be accessed or used. Such packages are:

  • com.sun.deploy.*
  • com.sun.imageio.*
  • com.sun.javaws.*
  • com.sun.jnlp.*
  • com.sun.xml.internal.bind.*
  • com.sun.xml.internal.ws.*
  • sun.*

Trying to instantiate or use classes in these packages will result in an AccessControlException.

This means that we are not able to get a reference to the sun.awt.SunToolkit class from our applet.

But the basic thing we need to know is that calls to certain methods can potentially bypass the SecurityManager checks depending on the immediate caller's class loader.

This is explained in detail in the “Secure Coding Guidelines” document by Sun [1] in guidelines 9-8 and 9-9.

This exploit is abusing this situation taking advantage of the immediate caller to bypass security checks.

Vulnerabilities

There are 2 different zero-day vulnerabilities used in this exploit: one is used to obtain a reference to the sun.awt.SunToolkit class and the other is used to invoke the public getField method on that class.

The exploit is making use of the java.beans.Expression which is a java.beans.Statement subclass.

There are 2 Expression instances that are used to trigger these 2 different bugs.

When the Expression.execute method is called it ends up calling Statement.invokeInternal method, so let's check the implementation:


[TABLE="width: 100%"]
[TR]
[TD="width: 100%"] [FONT=Monospace][B]private[/B] Object invokeInternal() [B]throws[/B] Exception {[/FONT]
[FONT=Monospace]Object target = getTarget();[/FONT]
[FONT=Monospace]String methodName = getMethodName();[/FONT]


[FONT=Monospace][B]if[/B] (target == [B]null[/B] || methodName == [B]null[/B]) {[/FONT]
[FONT=Monospace][B]throw new[/B] NullPointerException((target == [B]null[/B] ? "target" :[/FONT]
[FONT=Monospace]"methodName") + " should not be null");[/FONT]
[FONT=Monospace]}[/FONT]


[FONT=Monospace]Object[] arguments = getArguments();[/FONT]
[FONT=Monospace][B]if[/B] (arguments == [B]null[/B]) {[/FONT]
[FONT=Monospace]arguments = [I]emptyArray[/I];[/FONT]
[FONT=Monospace]}[/FONT]
[FONT=Monospace]// Class.forName() won't load classes outside[/FONT]
[FONT=Monospace]// of core from a class inside core. Special[/FONT]
[FONT=Monospace]// case this method.[/FONT]
[FONT=Monospace][B]if[/B] (target == Class.[B]class[/B] && methodName.equals("forName")) {[/FONT]
[FONT=Monospace][B]return[/B] ClassFinder.[I]resolveClass[/I]((String)arguments[0], [B]this[/B].loader);[/FONT]
[FONT=Monospace]}[/FONT]
[FONT=Monospace]Class[] argClasses = [B]new[/B] Class[arguments.length];[/FONT]
[FONT=Monospace][B]for[/B]([B]int[/B] i = 0; i < arguments.length; i++) {[/FONT]
[FONT=Monospace]argClasses[i] = (arguments[i] == [B]null[/B]) ? [B]null[/B] : arguments[i].getClass();[/FONT]
[FONT=Monospace]}[/FONT]


[FONT=Monospace]AccessibleObject m = [B]null[/B];[/FONT]
[FONT=Monospace][B]if[/B] (target [B]instanceof[/B] Class) {[/FONT]
[FONT=Monospace]/*[/FONT]
[FONT=Monospace]For class methods, simluate the effect of a meta class[/FONT]
[FONT=Monospace]by taking the union of the static methods of the[/FONT]
[FONT=Monospace]actual class, with the instance methods of "Class.class"[/FONT]
[FONT=Monospace]and the overloaded "newInstance" methods defined by the[/FONT]
[FONT=Monospace]constructors.[/FONT]
[FONT=Monospace]This way "System.class", for example, will perform both[/FONT]
[FONT=Monospace]the static method getProperties() and the instance method[/FONT]
[FONT=Monospace]getSuperclass() defined in "Class.class".[/FONT]
[FONT=Monospace]*/[/FONT]
[FONT=Monospace][B]if[/B] (methodName.equals("new")) {[/FONT]
[FONT=Monospace]methodName = "newInstance";[/FONT]
[FONT=Monospace]}[/FONT]
[FONT=Monospace]// Provide a short form for array instantiation by faking an nary-constructor.[/FONT]
[FONT=Monospace][B]if[/B] (methodName.equals("newInstance") && ((Class)target).isArray()) {[/FONT]
[FONT=Monospace]Object result = Array.[I]newInstance[/I](((Class)target).getComponentType(), arguments.length);[/FONT]
[FONT=Monospace][B]for[/B]([B]int[/B] i = 0; i < arguments.length; i++) {[/FONT]
[FONT=Monospace]Array.[I]set[/I](result, i, arguments[i]);[/FONT]
[FONT=Monospace]}[/FONT]
[FONT=Monospace][B]return[/B] result;[/FONT]
[FONT=Monospace]}[/FONT]
[FONT=Monospace][B]if[/B] (methodName.equals("newInstance") && arguments.length != 0) {[/FONT]
[FONT=Monospace]// The Character class, as of 1.4, does not have a constructor[/FONT]
[FONT=Monospace]// which takes a String. All of the other "wrapper" classes[/FONT]
[FONT=Monospace]// for Java's primitive types have a String constructor so we[/FONT]
[FONT=Monospace]// fake such a constructor here so that this special case can be[/FONT]
[FONT=Monospace]// ignored elsewhere.[/FONT]
[FONT=Monospace][B]if[/B] (target == Character.[B]class[/B] && arguments.length == 1 &&[/FONT]
[FONT=Monospace]argClasses[0] == String.[B]class[/B]) {[/FONT]
[FONT=Monospace][B]returnnew[/B] Character(((String)arguments[0]).charAt(0));[/FONT]
[FONT=Monospace]}[/FONT]
[FONT=Monospace][B]try[/B] {[/FONT]
[FONT=Monospace]m = ConstructorFinder.[I]findConstructor[/I]((Class)target, argClasses);[/FONT]
[FONT=Monospace]}[/FONT]
[FONT=Monospace][B]catch[/B] (NoSuchMethodException exception) {[/FONT]
[FONT=Monospace]m = [B]null[/B];[/FONT]
[FONT=Monospace]}[/FONT]
[FONT=Monospace]}[/FONT]
[FONT=Monospace][B]if[/B] (m == [B]null[/B] && target != Class.[B]class[/B]) {[/FONT]
[FONT=Monospace]m = [I]getMethod[/I]((Class)target, methodName, argClasses);[/FONT]
[FONT=Monospace]}[/FONT]
[FONT=Monospace][B]if[/B] (m == [B]null[/B]) {[/FONT]
[FONT=Monospace]m = [I]getMethod[/I](Class.[B]class[/B], methodName, argClasses);[/FONT]
[FONT=Monospace]}[/FONT]
[FONT=Monospace]}[/FONT]
[FONT=Monospace][B]else[/B] {[/FONT]
[FONT=Monospace]/*[/FONT]
[FONT=Monospace]This special casing of arrays is not necessary, but makes files[/FONT]
[FONT=Monospace]involving arrays much shorter and simplifies the archiving infrastrcure.[/FONT]
[FONT=Monospace]The Array.set() method introduces an unusual idea - that of a static method[/FONT]
[FONT=Monospace]changing the state of an instance. Normally statements with side[/FONT]
[FONT=Monospace]effects on objects are instance methods of the objects themselves[/FONT]
[FONT=Monospace]and we reinstate this rule (perhaps temporarily) by special-casing arrays.[/FONT]
[FONT=Monospace]*/[/FONT]
[FONT=Monospace][B]if[/B] (target.getClass().isArray() &&[/FONT]
[FONT=Monospace](methodName.equals("set") || methodName.equals("get"))) {[/FONT]
[FONT=Monospace][B]int[/B] index = ((Integer)arguments[0]).intValue();[/FONT]
[FONT=Monospace][B]if[/B] (methodName.equals("get")) {[/FONT]
[FONT=Monospace][B]return[/B] Array.[I]get[/I](target, index);[/FONT]
[FONT=Monospace]}[/FONT]
[FONT=Monospace][B]else[/B] {[/FONT]
[FONT=Monospace]Array.[I]set[/I](target, index, arguments[1]);[/FONT]
[FONT=Monospace][B]returnnull[/B];[/FONT]
[FONT=Monospace]}[/FONT]
[FONT=Monospace]}[/FONT]
[FONT=Monospace]m = [I]getMethod[/I](target.getClass(), methodName, argClasses);[/FONT]
[FONT=Monospace]}[/FONT]
[FONT=Monospace][B]if[/B] (m != [B]null[/B]) {[/FONT]
[FONT=Monospace][B]try[/B] {[/FONT]
[FONT=Monospace][B]if[/B] (m [B]instanceof[/B] Method) {[/FONT]
[FONT=Monospace][B]return[/B] MethodUtil.[I]invoke[/I]((Method)m, target, arguments);[/FONT]
[FONT=Monospace]}[/FONT]
[FONT=Monospace][B]else[/B] {[/FONT]
[FONT=Monospace][B]return[/B] ((Constructor)m).newInstance(arguments);[/FONT]
[FONT=Monospace]}[/FONT]
[FONT=Monospace]}[/FONT]
[FONT=Monospace][B]catch[/B] (IllegalAccessException iae) {[/FONT]
[FONT=Monospace][B]throw new[/B] Exception("Statement cannot invoke: " +[/FONT]
[FONT=Monospace]methodName + " on " + target.getClass(),[/FONT]
[FONT=Monospace]iae);[/FONT]
[FONT=Monospace]}[/FONT]
[FONT=Monospace][B]catch[/B] (InvocationTargetException ite) {[/FONT]
[FONT=Monospace]Throwable te = ite.getTargetException();[/FONT]
[FONT=Monospace][B]if[/B] (te [B]instanceof[/B] Exception) {[/FONT]
[FONT=Monospace][B]throw[/B] (Exception)te;[/FONT]
[FONT=Monospace]}[/FONT]
[FONT=Monospace][B]else[/B] {[/FONT]
[FONT=Monospace][B]throw[/B] ite;[/FONT]
[FONT=Monospace]}[/FONT]
[FONT=Monospace]}[/FONT]
[FONT=Monospace]}[/FONT]
[FONT=Monospace][B]throw new[/B] NoSuchMethodException(toString());[/FONT]
[FONT=Monospace]}[/FONT]
[/TD]
[/TR]
[/TABLE]

And the Statement.getMethod implementation is:

[TABLE=width: 100%]

[TR]

[TD=width: 100%] static Method getMethod(Class<?> type, String name, Class<?>... args) {

try {

return MethodFinder.findMethod(type, name, args);

}

catch (NoSuchMethodException exception) {

return null;

}

}

[/TD]

[/TR]

[/TABLE]

Highlighted in the code you'll see the calls to com.sun.beans.finder.ClassFinder.resolveClass and com.sun.beans.finder.MethodFinder.findMethod methods.

com.sun.beans.finder.ClassFinder.findClass vulnerability

The Statement.invokeInternal method is calling com.sun.beans.finder.ClassFinder.resolveClass and if we take a look a its implementation we'll see that it ends up calling the com.sun.beans.finder.ClassFinder.findClass method:

[TABLE="width: 100%"]
[TR]
[TD="width: 100%"] [FONT=Monospace][B]public static[/B] Class<?> resolveClass(String name, ClassLoader loader) [B]throws[/B] ClassNotFoundException {[/FONT]
[FONT=Monospace]Class<?> type = PrimitiveTypeMap.[I]getType[/I](name);[/FONT]
[FONT=Monospace][B]return[/B] (type == [B]null[/B]) ? [I]findClass[/I](name, loader): type;[/FONT]
[FONT=Monospace]}[/FONT]
[/TD]
[/TR]
[/TABLE]

[TABLE="width: 100%"]
[TR]
[TD="width: 100%"] [FONT=Monospace][B]public[/B] [B]static[/B] Class<?> findClass(String name) [B]throws[/B] ClassNotFoundException {[/FONT]
[FONT=Monospace][B]try[/B] {[/FONT]
[FONT=Monospace]ClassLoader loader = Thread.[I]currentThread[/I]().getContextClassLoader();[/FONT]
[FONT=Monospace][B]if[/B] (loader == [B]null[/B]) {[/FONT]
[FONT=Monospace]loader = ClassLoader.[I]getSystemClassLoader[/I]();[/FONT]
[FONT=Monospace]}[/FONT]
[FONT=Monospace][B]if[/B] (loader != [B]null[/B]) {[/FONT]
[FONT=Monospace][B]return[/B] Class.[I]forName[/I](name, [B]false[/B], loader);[/FONT]
[FONT=Monospace]}[/FONT]
[FONT=Monospace]} [B]catch[/B] (ClassNotFoundException exception) {[/FONT]
[FONT=Monospace]// use current class loader instead[/FONT]
[FONT=Monospace]} [B]catch[/B] (SecurityException exception) {[/FONT]
[FONT=Monospace]// use current class loader instead[/FONT]
[FONT=Monospace]}[/FONT]
[FONT=Monospace][B]return[/B] Class.[I]forName[/I](name);[/FONT]
[FONT=Monospace]}[/FONT]
[/TD]
[/TR]
[/TABLE]

This code shows that if an exception is captured, then the default is to simply call Class.forName and this is exaclty what happens here.

As it is explained in the Guideline 9-9: “Safely invoke standard APIs that perform tasks using the immediate caller's class loader instance” on the “Secure Code Guidelines” documentation [1], a call to Class.forName will use the immediate caller ClassLoader and in this case the caller is part of the JDK which is trusted thus allowing us to get any class on any package.

The caller's stack can be seen by simply debugging the applet:

1.png

MethodFinder.findMethod vulnerability

According to the “Secure Code Guidelines” document in guideline 9.8 the java.lang.Class.getMethod and java.lang.Class.getMethods only take the immediate caller into account when performing security checks.

These methods can be used to get a Method reference via reflection but only “public” ones.

Even though we have a reference to the sun.awt.SunToolkit class we cannot call any of its methods directly because is part of a restricted package and a security exception will be raised.

What is needed here is a way of getting a method reference via reflection but having a “trusted” immediate caller in the stack in order to bypass security checks.

The implementation of com.sun.beans.finder.MethodFinder.findMethod is this:


[TABLE="width: 100%"]
[TR]
[TD="width: 100%"] [FONT=Monospace][B]public static[/B] Method findMethod(Class<?> type, String name, Class<?>...args) [B]throws[/B] NoSuchMethodException {[/FONT]

[FONT=Monospace][B]if[/B] (name == [B]null[/B]) {[/FONT]
[FONT=Monospace][B]throw new[/B] IllegalArgumentException("Method name is not set[/FONT]
[FONT=Monospace]}[/FONT]
[FONT=Monospace]PrimitiveWrapperMap.[I]replacePrimitivesWithWrappers[/I](args);[/FONT]
[FONT=Monospace]Signature signature = [B]new[/B] Signature(type, name, args);[/FONT]


[FONT=Monospace]Method method = [I]CACHE[/I].get(signature);[/FONT]
[FONT=Monospace][B]if[/B] (method != [B]null[/B]) {[/FONT]
[FONT=Monospace][B]return[/B] method;[/FONT]
[FONT=Monospace]}[/FONT]
[FONT=Monospace]method = [I]findAccessibleMethod[/I]([B]new[/B] MethodFinder(name, args).find(type.getMethods()));[/FONT]
[FONT=Monospace][I]CACHE[/I].put(signature, method);[/FONT]
[FONT=Monospace][B]return[/B] method;[/FONT]
[FONT=Monospace]}[/FONT]
[/TD]
[/TR]
[/TABLE]

The call to findAccessibleMethod ends up calling java.lang.Class.getMethods and the immediate caller in the stack is com.sun.beans.finder.MethodFinder which is trusted since is part of the JDK thus bypassing the security checks.

Once again we can see the callers stack by debugging the applet:

2.png

Affected Versions

The com.sun.beans.finder.MethodFinder and com.sun.beans.finder.ClassFinder classes are available only since JDK 7.

Putting all together

So this exploit is performing the following steps:

  • Creates a Statement instance that will call System.setSecurityManager(null) method using reflection.
  • Creates a custom AccessControlContext with full permissions.
  • With one bug it gets a reference to the sun.awt.SunToolkit class that is restricted to applets.
  • With the other bug it invokes the getField public static method on sun.awt.SunToolkit using reflection with a trusted immediate caller that bypasses the security checks.
  • With the getField method it is getting a reference to Statement.acc private field and setting its value to the custom AccessControlContext instance previously created.
  • Finally it executes the Statement that will disable the Security Manager bypassing all security checks because it has full permissions set in its AccessControlContext.

Author

Esteban Guillardoy

esteban@immunityinc.com

twitter: @sagar38

References

[1] - Secure Coding Guidelines for the Java Programming Language, Version 4.0 - Secure Coding Guidelines for the Java Programming Language, Version 4.0

[2] - Java SE 7 Security Architecture - Java Security Architecture: -

[3] - Java SE 7 Security Documents - http://docs.oracle.com/javase/7/docs/technotes/guides/security/

[4] - Sami Koivu Blog - Java Trusted Method Chaining (CVE-2010-0840/ZDI-10-056) - (Slightly) Random Broken Thoughts: Java Trusted Method Chaining (CVE-2010-0840/ZDI-10-056)

Posted by Nico Waisman at 11:15 AM

Sursa: Immunity Products: Java 0day analysis (CVE-2012-4681)

Edited by Nytro

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...