由于近期L10疯狂传播,通过JAR**链攻击感染服务器并执行后门代码,现在正在尝试使用 javassist 类库制作杀毒工具。目前求助通过一种方式运行时修改或删除class文件指定行代码。
尝试了CtMethod的insertAt和insertBefore,但是总是不会修改已有行的代码。
StackOverFlow 上的
Opcode也未能奏效,修改没有生效。最初怀疑应该codeAttribute.set下,但是这是个UnsupportedOperation。
因此特地前来发帖求助。
如果有其他办法可以做到(运行时自动化的,需要是Java原生实现),也可不必捆在javassist上。
谢谢!
尝试了CtMethod的insertAt和insertBefore,但是总是不会修改已有行的代码。
StackOverFlow 上的
代码:
- private static void eraseLine(CtMethod method, int lineNumberToReplace){
- // let's erase the sysout "Two"
- // Access the code attribute
- CodeAttribute codeAttribute = method.getMethodInfo().getCodeAttribute();
- // Access the LineNumberAttribute
- LineNumberAttribute lineNumberAttribute = (LineNumberAttribute) codeAttribute.getAttribute(LineNumberAttribute.tag);
- // Index in bytecode array where the instruction starts
- int startPc = lineNumberAttribute.toStartPc(lineNumberToReplace);
- // Index in the bytecode array where the following instruction starts
- int endPc = lineNumberAttribute.toStartPc(lineNumberToReplace+1);
- // Let's now get the bytecode array
- byte[] code = codeAttribute.getCode();
- for (int i = startPc; i < endPc; i++) {
- // change byte to a no operation code
- code = CodeAttribute.NOP;
- };
- }
因此特地前来发帖求助。
如果有其他办法可以做到(运行时自动化的,需要是Java原生实现),也可不必捆在javassist上。
谢谢!
写在前面:不要使用来路不明的任何软件,句号。
ASM。
楼主的问题本质上就是Coremod。
所以你是在运行时的什么时候修改的?你当然不能修改一个已经完全加载的类。
你要么在目标类加载之前修改,或者想个办法先卸载目标类,修改后重新加载回去。
直接对 class 操作的时候往往是不看源文件行号的。
替换或删除指定行代码或Opcode的方式
运行时修改或删除class文件指定行代码
ASM。
楼主的问题本质上就是Coremod。
所以你是在运行时的什么时候修改的?你当然不能修改一个已经完全加载的类。
你要么在目标类加载之前修改,或者想个办法先卸载目标类,修改后重新加载回去。
指定行代码
直接对 class 操作的时候往往是不看源文件行号的。
3TUSK 发表于 2021-6-2 01:49
写在前面:不要使用来路不明的任何软件,句号。
写在前面:不要使用来路不明的任何软件,句号。
很明显是一个独立与MC服务器的软件,因此Coremod这个概念不适用。
运行时javassist拉入Pool的classpath中,在CtClass+CtMethod获得的类和方法。