【问题标题】:ASM - Inconsistent stackmap frames at branch targetASM - 分支目标处的堆栈图帧不一致
【发布时间】:2018-01-18 16:20:43
【问题描述】:

我正在尝试做一个简单的 Java 字节码混淆器,它通过用简单的条件跳转替换 GOTO 指令来工作,比如 if 10 != 15 GOTO else throw IllegalStateException。我当前的代码是:

    final AbstractInsnNode[] insns = method.instructions.toArray().clone();

    for (final AbstractInsnNode insn : insns) {
        final int op = insn.getOpcode();

        if ((op == GOTO) || (op == IFLE) || (op == IFGE)) {
            LabelNode l0 = new LabelNode();
            LabelNode l1 = new LabelNode();
            LabelNode l2 = new LabelNode();

            int locals = (method.localVariables == null) ? 0 : method.localVariables.size();
            int params = (method.parameters == null) ? 0 : method.parameters.size();

            int v0index = locals + params;
            int v1index = v0index + 1;
            int exindex = v1index + 1;

            // Init fake conditional fields
            method.instructions.insertBefore(insn, new LdcInsnNode(10F));
            method.instructions.insertBefore(insn, new VarInsnNode(FSTORE, v0index));

            method.instructions.insertBefore(insn, new LdcInsnNode(45F));
            method.instructions.insertBefore(insn, new VarInsnNode(FSTORE, v1index));

            // Crossing jumps
            method.instructions.insertBefore(insn, l1);
            method.instructions.insert(insn, l0);
            method.instructions.insert(l0, l2);

            LabelNode l3 = new LabelNode();
            LabelNode l4 = new LabelNode();

            method.instructions.insert(l2, l3);
            method.instructions.insert(l3, l4);

            // If 'v0!=v1', jump to l0, otherwise goto l3
            method.instructions.insertBefore(l1, new VarInsnNode(FLOAD, v0index));
            method.instructions.insertBefore(l1, new VarInsnNode(FLOAD, v1index));
            method.instructions.insertBefore(l1, new InsnNode(FCMPG));
            method.instructions.insertBefore(l1, new JumpInsnNode(IFNE, l0));
            method.instructions.insertBefore(l1, new JumpInsnNode(GOTO, l3));

            // Jump to l3 results in throwing an exception
            // Create and throw the exception
            method.instructions.insertBefore(l4, new TypeInsnNode(NEW, "java/lang/IllegalStateException"));
            method.instructions.insertBefore(l4, new InsnNode(DUP));
            method.instructions.insertBefore(l4, new MethodInsnNode(INVOKESPECIAL, "java/lang/IllegalStateException", "<init>", "()V", false));
            method.instructions.insertBefore(l4, new InsnNode(ATHROW));

            method.instructions.insertBefore(l0, new JumpInsnNode(GOTO, l2));
            method.instructions.insertBefore(l2, new JumpInsnNode(GOTO, l1));

            // Exception handler
            LabelNode start = new LabelNode();
            LabelNode handler = new LabelNode();
            LabelNode end = new LabelNode();

            method.instructions.insertBefore(l0, start);

            method.instructions.insert(l2, end);
            method.instructions.insert(end, handler);

            // Just throw the exception again
            LabelNode l5 = new LabelNode();

            method.instructions.insert(handler, l5);
            method.instructions.insertBefore(l5, new TypeInsnNode(NEW, "java/lang/IllegalStateException"));
            method.instructions.insertBefore(l5, new InsnNode(DUP));
            method.instructions.insertBefore(l5, new MethodInsnNode(INVOKESPECIAL, "java/lang/IllegalStateException", "<init>", "()V", false));
            method.instructions.insertBefore(l5, new InsnNode(ATHROW));

            // Try/catch
            TryCatchBlockNode tryBlock = new TryCatchBlockNode(start, end, handler, "java/lang/IllegalStateException");
            method.tryCatchBlocks.add(tryBlock);

            // Init local variables
            method.visitLocalVariable("_v0_" + Rand.alphaNumeric(5), "F", null, l0.getLabel(), l2.getLabel(), v0index);
            method.visitLocalVariable("_v1_" + Rand.alphaNumeric(5), "F", null, l0.getLabel(), l2.getLabel(), v1index);
            method.visitLocalVariable("_ex_" + Rand.alphaNumeric(5), "Ljava/lang/IllegalArgumentException;", null, start.getLabel(), handler.getLabel(), exindex);
        }
    }

其中methodMethodNode类型的混淆方法参数,该类实现接口Opcodes

这很好用,但不适用于所有方法(我对字节码很陌生,所以不知道具体情况)。例如,它适用于 main 方法:

原始 Java 代码(在 Procyon 中反编译): https://p.reflex.rip/DLMT.cs

原始字节码: https://p.reflex.rip/ywJt.go

混淆的 Java 代码(在 Procyon 中反编译): https://p.reflex.rip/Er9V.cs

混淆字节码: https://p.reflex.rip/JBAb.go

但是,它破坏了其他方法之一,divMinByMax

原始 Java 代码(在 Procyon 中反编译): https://p.reflex.rip/AW9W.java

原始字节码: https://p.reflex.rip/GX2k.cpp

混淆的 Java 代码(在 Procyon 中反编译,FAILED): https://p.reflex.rip/Eqju.java

混淆字节码: https://p.reflex.rip/isiX.cpp

当我尝试使用java -jar 运行经过混淆的 JAR 时,此方法会导致 VerifyError

Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.VerifyError: Inconsistent stackmap frames at branch target 27
Exception Details:
  Location:
    test/one/HelloRandom.divMinByMax(DD)D @21: goto
  Reason:
    Current frame's stack size doesn't match stackmap.
  Current Frame:
    bci: @21
    flags: { }
    locals: { double, double_2nd, float, float }
    stack: { }
  Stackmap Frame:
    bci: @27
    flags: { }
    locals: { double, double_2nd, float, float }
    stack: { 'java/lang/IllegalStateException' }
  Bytecode:
    0x0000000: 2826 9712 7145 1272 4624 2596 9a00 0ca7
    0x0000010: 0014 9e00 48a7 0006 a7ff fabb 0016 59b7
    0x0000020: 0073 bfbb 0016 59b7 0073 bf00 0000 0000
    0x0000030: 00bf 0000 00bf 0000 0000 0000 00bf 0000
    0x0000040: bf00 00bf 0000 bf00 00bf 0000 0000 0000
    0x0000050: 00bf 0000 0000 0000 00bf 2826 6faf
  Exception Handler Table:
    bci [24, 27] => handler: 27
  Stackmap Table:
    full_frame(@18,{Double,Float,Float},{Integer})
    same_locals_1_stack_item_frame(@24,Integer)
    same_locals_1_stack_item_frame(@27,Object[#22])
    same_locals_1_stack_item_frame(@35,Integer)
    full_frame(@43,{},{Object[#159]})
    same_locals_1_stack_item_frame(@50,Object[#159])
    same_locals_1_stack_item_frame(@54,Object[#159])
    same_locals_1_stack_item_frame(@62,Object[#159])
    same_locals_1_stack_item_frame(@65,Object[#159])
    same_locals_1_stack_item_frame(@68,Object[#159])
    same_locals_1_stack_item_frame(@71,Object[#159])
    same_locals_1_stack_item_frame(@74,Object[#159])
    same_locals_1_stack_item_frame(@82,Object[#159])
    append_frame(@90,Double,Float,Float)
    same_locals_1_stack_item_frame(@93,Double)

我做了很多研究,唯一发现的就是原因:据我了解,问题是@21 上的堆栈(GOTO 跳转到throw new IllegalStateException 的标签):

stack: { }

(为空)与跳转目标标签上@27处的堆栈不匹配:

stack: { 'java/lang/IllegalStateException' } (其中包含应该“抛出”的异常)。

所以基本上,据我所知,错误发生在我尝试执行GOTO &lt;n&gt; 跳转时,其中&lt;n&gt; 是“抛出”IllegalStateException 的标签编号。

我该如何解决这个问题?也许有一种方法可以让@21 的堆栈在跳转之前也包含java/lang/IllegalStateException(以便这两个堆栈,一个在跳转之前和一个在跳转之后,匹配)?或者我可以用它做些什么?

【问题讨论】:

  • 为什么要替换操作码?我以为你只想替换 goto?

标签: java bytecode java-bytecode-asm bytecode-manipulation jvm-bytecode


【解决方案1】:

您在指令之后插入异常处理程序,但当指令是条件分支时,即IFLEIFGE,可能不会采用该分支并且代码流在之后继续指令,运行到异常处理程序中。

这会产生不一致的状态,因为异常处理程序期望堆栈上的 Throwable 在检测指令之后继续代码流时不存在。但是当然,在这种情况下你不想执行异常处理程序,所以你必须插入另一个GOTO,如果我没记错的话,从l2l5

在检测 GOTO 指令时这不是问题,在指令之后永远不会继续。

在这个地方,我会推荐一种不同的编码风格。在不同的参考节点之前和之后插入使得在阅读代码时无法预测实际的代码结构。如果您只使用一个参考节点插入一个线性指令列表,它将更易于维护。

【讨论】:

    猜你喜欢
    • 2016-05-11
    • 1970-01-01
    • 2014-12-31
    • 2014-12-25
    • 2014-10-22
    • 2014-09-19
    • 2021-03-26
    • 1970-01-01
    相关资源
    最近更新 更多