Merge pull request #4 from yelochick/main

feat: hook Class.forName
This commit is contained in:
novice88 2024-04-18 15:15:00 +08:00 committed by GitHub
commit 5194243b35
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 42 additions and 0 deletions

View File

@ -0,0 +1,37 @@
package com.novitechie;
import com.janetfilter.core.plugin.MyTransformer;
import jdk.internal.org.objectweb.asm.ClassReader;
import jdk.internal.org.objectweb.asm.ClassWriter;
import jdk.internal.org.objectweb.asm.tree.*;
import static jdk.internal.org.objectweb.asm.Opcodes.*;
/**
* @author YeloChick
*/
public class ClassTransformer implements MyTransformer {
@Override
public String getHookClassName() {
return "java.lang.Class";
}
@Override
public byte[] transform(String className, byte[] classBytes, int order) throws Exception {
ClassReader reader = new ClassReader(classBytes);
ClassNode node = new ClassNode(ASM5);
reader.accept(node, 0);
for (MethodNode m : node.methods) {
if ("forName".equals(m.name)) {
InsnList list = new InsnList();
list.add(new VarInsnNode(ALOAD, 0));
list.add(new MethodInsnNode(INVOKESTATIC, "com/novitechie/LoadClassRule", "check", "(Ljava/lang/String;)V", false));
m.instructions.insert(list);
}
}
ClassWriter writer = new SafeClassWriter(null, null, ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
node.accept(writer);
return writer.toByteArray();
}
}

View File

@ -5,5 +5,10 @@ public class LoadClassRule {
if (name.startsWith("com.janetfilter")) {
throw new ClassNotFoundException(name);
}
if (name.contains("jdk.internal.org.objectweb.asm.Type")) {
if (StackTraceRule.check()) {
throw new ClassNotFoundException(name);
}
}
}
}