daily pastebin goal
81%
SHARE
TWEET

Untitled

a guest Jul 13th, 2018 63 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import org.apache.bcel.classfile.ClassParser;
  2. import org.apache.bcel.classfile.Method;
  3. import org.apache.bcel.generic.*;
  4. import org.apache.bcel.Constants;
  5.  
  6. import java.io.ByteArrayInputStream;
  7. import java.io.IOException;
  8. import java.lang.instrument.ClassFileTransformer;
  9. import java.lang.instrument.IllegalClassFormatException;
  10. import java.lang.instrument.Instrumentation;
  11. import java.security.ProtectionDomain;
  12.  
  13. public class HookCheckerBorker implements ClassFileTransformer {
  14.  
  15.     public byte[] transform(ClassLoader loader, String className, Class<?> redefinedClass, ProtectionDomain domain,
  16.                             byte[] classBuffer)
  17.             throws IllegalClassFormatException {
  18.         /* logic here */
  19.         if (className.equals("java.lang.Class")) {
  20.             try {
  21.                 ClassGen cgen = new ClassGen(new ClassParser(new ByteArrayInputStream(classBuffer), className).parse());
  22.                 Method gdm = cgen.containsMethod("getDeclaredMethod",
  23.                         "(Ljava/lang/String;[Ljava/lang/Class;)Ljava/lang/reflect/Method;");
  24.                 MethodGen gdmGen = new MethodGen(gdm, cgen.getClassName(),
  25.                         cgen.getConstantPool());
  26.                 InstructionList injected = new InstructionList();
  27.                 InstructionFactory factory = new InstructionFactory(cgen, cgen.getConstantPool());
  28.                 injected.append(factory.createInvoke("java.lang.Thread", "currentThread",
  29.                         new ObjectType("java.lang.Thread"), new Type[0], Constants.INVOKESTATIC));
  30.                 injected.append(factory.createInvoke("java.lang.Thread", "getStackTrace",
  31.                         new ArrayType(new ObjectType("java.lang.StackTraceElement"), 1), new Type[0],
  32.                         Constants.INVOKEVIRTUAL));
  33.                 injected.append(new ICONST(3));
  34.                 injected.append(new AALOAD());
  35.                 injected.append(factory.createInvoke("java.lang.StackTraceElement", "getClassName", Type.STRING,
  36.                         new Type[0], Constants.INVOKEVIRTUAL));
  37.                 injected.append(factory.createConstant("run.checkHooked"));
  38.                 injected.append(factory.createInvoke("java.lang.String", "equals", Type.BOOLEAN,
  39.                         new Type[] { Type.OBJECT }, Constants.INVOKEVIRTUAL));
  40.                 BranchHandle ifEqBranch = injected.append(new IFEQ(null));
  41.                 injected.append(factory.createNew(new ObjectType("java.lang.NoSuchMethodException")));
  42.                 injected.append(new DUP());
  43.                 injected.append(factory.createInvoke("java.lang.NoSuchMethodException", "<init>", Type.VOID,
  44.                         new Type[0], Constants.INVOKESPECIAL));
  45.                 injected.append(new ATHROW());
  46.                 InstructionHandle ldThisHandle = null;
  47.                 InstructionList gdmCodeList = gdmGen.getInstructionList();
  48.                 for (InstructionHandle handle : gdmCodeList.getInstructionHandles()) {
  49.                     if (handle.getInstruction() instanceof ALOAD
  50.                             && handle.getPrev().getInstruction() instanceof ALOAD) {
  51.                         ALOAD aload = (ALOAD) handle.getInstruction();
  52.                         ALOAD aloadThis = (ALOAD) handle.getPrev().getInstruction();
  53.                         if (aload.getIndex() == 2 && aloadThis.getIndex() == 0) {
  54.                             ldThisHandle = handle.getPrev();
  55.                             break;
  56.                         }
  57.                     }
  58.                 }
  59.                 if (ldThisHandle != null) {
  60.                     ifEqBranch.setTarget(ldThisHandle);
  61.                     gdmCodeList.insert(ldThisHandle, injected);
  62.                     gdmCodeList.setPositions();
  63.                     gdmGen.setInstructionList(gdmCodeList); //redundant -- gdmCodeList == gdmGen.getInstructionList()
  64.                     gdmGen.setMaxLocals();
  65.                     gdmGen.setMaxStack();
  66.                     cgen.replaceMethod(gdm, gdmGen.getMethod());
  67.                     return cgen.getJavaClass().getBytes();
  68.                 }
  69.             } catch (IOException ioex) {
  70.                 ioex.printStackTrace();
  71.             }
  72.         }
  73.         return null;
  74.     }
  75.  
  76.  
  77.     public static void premain(String args, Instrumentation instr) {
  78.         instr.addTransformer(new HookCheckerBorker());
  79.     }
  80. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top