-
-
Save huzpsb/b8ca7caaf5e2b10d09db63059a8d85ce to your computer and use it in GitHub Desktop.
g
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package github.huzpsb.nm.sample; | |
| import github.huzpsb.nm.tools.Hacker; | |
| import org.objectweb.asm.*; | |
| import java.lang.instrument.ClassFileTransformer; | |
| import java.lang.instrument.IllegalClassFormatException; | |
| import java.lang.instrument.Instrumentation; | |
| import java.security.ProtectionDomain; | |
| public class Main { | |
| public static void main(String[] args) throws Exception { | |
| Instrumentation ins = Hacker.getInsn(); | |
| final String tgt = "github/huzpsb/nm/sample/Add"; | |
| ClassFileTransformer ret = new ClassFileTransformer() { | |
| public byte[] transform(ClassLoader loader, | |
| String className, | |
| Class<?> classBeingRedefined, | |
| ProtectionDomain protectionDomain, | |
| byte[] classfileBuffer) | |
| throws IllegalClassFormatException { | |
| if (className.equals(tgt)) { | |
| ClassReader cr = new ClassReader(classfileBuffer); | |
| ClassWriter cw = new ClassWriter(cr, 0); | |
| ClassVisitor cv = new ClassVisitor(Opcodes.ASM9, cw) { | |
| @Override | |
| public MethodVisitor visitMethod(int access, String name, String descriptor, String signature, String[] exceptions) { | |
| if (name.equals("add")) { | |
| System.out.println("Found the target method"); | |
| return null; | |
| } | |
| return super.visitMethod(access, name, descriptor, signature, exceptions); | |
| } | |
| @Override | |
| public void visitEnd() { | |
| // Create the new onCustomPayload method | |
| MethodVisitor mv = cv.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "add", "(II)I", null, null); | |
| mv.visitCode(); | |
| mv.visitVarInsn(Opcodes.ILOAD, 0); | |
| mv.visitVarInsn(Opcodes.ILOAD, 1); | |
| mv.visitInsn(Opcodes.IMUL); | |
| mv.visitInsn(Opcodes.IRETURN); | |
| mv.visitMaxs(2, 2); | |
| mv.visitEnd(); | |
| } | |
| }; | |
| cr.accept(cv, 0); | |
| return cw.toByteArray(); | |
| } | |
| return classfileBuffer; | |
| } | |
| }; | |
| ins.addTransformer(ret, true); | |
| ins.retransformClasses(Add.class); | |
| System.out.println(Add.add(2, 3)); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment