`

CVE-2013-0422 分析2

阅读更多

0x01

http://wcf1987.iteye.com/blog/1749088 这是对CVE-2012-0507的分析

http://wcf1987.iteye.com/blog/1768355这是对CVE-2013-0422的分析

本文则是javacve-2013-0422的分析,其实看poc感觉上就跟CVE-2012-0507有几分相似的地方,都是最终利用了defineclass来实现定义一个高权限的类,他面临的挑战也与CVE-2012-0507有几分相似,下面具体分析

 

 

 

0x02 这个poc的特点是新,目前为止orcale没打补丁,所以中招几率非常之大,大家速度关闭浏览器的java设置吧,话说回来,感觉java这几年好像漏洞频发的样子,不知道是我才关注还是oracle的不作为。

 

0x03 poc中有一段字符串,但是这段字符串解码如下

import java.security.AccessController;

import java.security.PrivilegedExceptionAction;

public class B

  implements PrivilegedExceptionAction

{

  public B()

  {

    try

    {

      AccessController.doPrivileged(this); } catch (Exception e) {

    }

  }

 

  public Object run() {

    System.setSecurityManager(null);

    return new Object();

  }

}

可以看到这段代码中就核心功能就一句话System.setSecurityManager(null);,这句话执行成功了,保护机制也就绕过了,poc的大量反射就为了能够高权限的去调用一下这个类B的构造函数,即poc中的localClass3.newInstance();

 

0x04 看这段poc中大部分的反射都集中在了这两个类中

  sun.org.mozilla.javascript.internal.Context

  sun.org.mozilla.javascript.internal.GeneratedClassLoader

  看他的poc逐步突破路径如下:

 

0x5 这里会发现出现了大量的MethodHandleMethodType之类的东西,这些东西都是jdk7新有的特性,和反射很类似,不过似乎效率更高?我个人目前觉得在这个poc中,这些调用方式如果替换成反射应该也是可以的,核心的对权限的突破应该是出现在0x04中提到的这两个类中

 

0x06

MethodHandles.publicLookup() 

Returns a lookup object which is trusted minimally. It can only be used to create method handles to publicly accessible fields and methods.

之所以不用lookup的方法,是因为lookup返回的是所有的方法,而publicLookup只返回属性为public的方法和属性,这样子后面有一步就不会因为权限不够导致错误,

 

MethodType.methodType()

主要是定义方法的参数结构,可以有多个,主要支出各个参数的类型是什么

 

public MethodHandle findVirtual(Class<?> refc,

                       String name,

                       MethodType type)

                         throws NoSuchMethodException,

                                IllegalAccessException

Produces a method handle for a virtual method. The type of the method handle will be that of the method, with the receiver type (usually refc) prepended. The method and all its argument types must be accessible to the lookup class.

When called, the handle will treat the first argument as a receiver and dispatch on the receiver's type to determine which method implementation to enter. (The dispatching action is identical with that performed by aninvokevirtual or invokeinterface instruction.)

The returned method handle will have variable arity if and only if the method's variable arity modifier bit (0x0080) is set.

Because of the general equivalence between invokevirtual instructions and method handles produced by findVirtual, if the class is MethodHandle and the name string is invokeExact or invoke, the resulting method handle is equivalent to one produced by MethodHandles.exactInvoker or MethodHandles.invoker with the same type argument.

这个方法就是去找到一个方法,第一个参数是这个方法是哪个类或者接口的,第二个参数是这个方法名是什么,第三个则是这个方法的参数是什么有几个,分别什么类型。注意这个方法可以去找那些接口的抽象方法,而不一定是具体的实现,通过这三个参数可以唯一的定位出一个方法,返回的就是methodHandle了。

 

public Object invokeWithArguments(List<?> arguments)

                           throws Throwable

Performs a variable arity invocation, passing the arguments in the given array to the method handle, as if via an inexact invoke from a call site which mentions only the type Object, and whose arity is the length of the argument array.

This method is also equivalent to the following code:

 invokeWithArguments(arguments.toArray())

Parameters:

arguments - the arguments to pass to the target

Returns:

the result returned by the target

这个方法就是很简单调用前面返回的methodHandle了,参数与定义时的保持一致即可。返回就是方法执行的结果了

 

0x07 那么原来的复杂的反射调用可以简化为如下(只是表明调用的实质):

       Context a =new sun.org.mozilla.javascript.internal.Context();

       sun.org.mozilla.javascript.internal.GeneratedClassLoader    b=a.createClassLoader(null);

       b.defineClass(XX);

 

0x08 在以前的blog中我们说过java干掉安全机制的一种方法中,最重要的是如何去得到一个ClassLoader,而ClassLoader最大的问题是,在严格限制的低权限环境中肯定是不允许你创建他的子类的实例的,而通过getClass.getClassLoader则虽然可以得到当前类的ClassLoader,但是由于这个一般都是系统类,和我们不在同一个包下面,无法去调用这个protecteddefineClass方法,在这个poc中我们发现似乎两个方面都有空子可以钻,

 

0x09 a.createClassLoader(null);这个方法按道理在低权限下是无法创建ClassLoader的,但是

由于找不到这两个文件的具体源代码,也就无从分析了,但是看上去似乎这里有点小问题,而后的b中又定义了一个publicdefineClass方法估计在这个里面,可能在这个public方法里面去调用了protected的那个defineClass从而导致了安全系统的破坏。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics