鬼畜畜
由于近期L10疯狂传播,通过JAR**链攻击感染服务器并执行后门代码,现在正在尝试使用 javassist 类库制作杀毒工具。目前求助通过一种方式运行时修改或删除class文件指定行代码。
尝试了CtMethod的insertAt和insertBefore,但是总是不会修改已有行的代码。


StackOverFlow 上的


代码:

  1. private static void eraseLine(CtMethod method, int lineNumberToReplace){
  2.   // let's erase the sysout "Two"
  3.   // Access the code attribute
  4.   CodeAttribute codeAttribute = method.getMethodInfo().getCodeAttribute();

  5.   // Access the LineNumberAttribute
  6.   LineNumberAttribute lineNumberAttribute = (LineNumberAttribute)    codeAttribute.getAttribute(LineNumberAttribute.tag);

  7.   // Index in bytecode array where the instruction starts
  8.   int startPc = lineNumberAttribute.toStartPc(lineNumberToReplace);

  9.   // Index in the bytecode array where the following instruction starts
  10.   int endPc = lineNumberAttribute.toStartPc(lineNumberToReplace+1);

  11.   // Let's now get the bytecode array
  12.   byte[] code = codeAttribute.getCode();
  13.   for (int i = startPc; i < endPc; i++) {
  14.    // change byte to a no operation code
  15.    code = CodeAttribute.NOP;
  16.   };
  17.     }
Opcode也未能奏效,修改没有生效。最初怀疑应该codeAttribute.set下,但是这是个UnsupportedOperation。
因此特地前来发帖求助。


如果有其他办法可以做到(运行时自动化的,需要是Java原生实现),也可不必捆在javassist上。
谢谢!

3TUSK
写在前面:不要使用来路不明的任何软件,句号。


替换或删除指定行代码或Opcode的方式
运行时修改或删除class文件指定行代码

ASM。
楼主的问题本质上就是Coremod。


所以你是在运行时的什么时候修改的?你当然不能修改一个已经完全加载的类。
你要么在目标类加载之前修改,或者想个办法先卸载目标类,修改后重新加载回去。


指定行代码

直接对 class 操作的时候往往是不看源文件行号的。

鬼畜畜
3TUSK 发表于 2021-6-2 01:49
写在前面:不要使用来路不明的任何软件,句号。

很明显是一个独立与MC服务器的软件,因此Coremod这个概念不适用。


运行时javassist拉入Pool的classpath中,在CtClass+CtMethod获得的类和方法。

第一页 上一页 下一页 最后一页