|
| 1 | +package uwu.narumi.deobfuscator.core.other.impl.branchlock; |
| 2 | + |
| 3 | +import org.objectweb.asm.tree.*; |
| 4 | +import uwu.narumi.deobfuscator.api.asm.MethodContext; |
| 5 | +import uwu.narumi.deobfuscator.api.asm.matcher.Match; |
| 6 | +import uwu.narumi.deobfuscator.api.asm.matcher.group.SequenceMatch; |
| 7 | +import uwu.narumi.deobfuscator.api.asm.matcher.impl.JumpMatch; |
| 8 | +import uwu.narumi.deobfuscator.api.asm.matcher.impl.NumberMatch; |
| 9 | +import uwu.narumi.deobfuscator.api.asm.matcher.impl.OpcodeMatch; |
| 10 | +import uwu.narumi.deobfuscator.api.transformer.Transformer; |
| 11 | + |
| 12 | +import java.util.HashSet; |
| 13 | +import java.util.List; |
| 14 | +import java.util.Set; |
| 15 | + |
| 16 | +public class BranchlockFlowTransformer extends Transformer { |
| 17 | + |
| 18 | + Match[] flowDupEquationMatches = new Match[] { |
| 19 | + SequenceMatch.of( |
| 20 | + OpcodeMatch.of(DUP), |
| 21 | + JumpMatch.of().capture("fake-jump"), |
| 22 | + JumpMatch.of().capture("correct-jump"), |
| 23 | + NumberMatch.of() |
| 24 | + ), |
| 25 | + SequenceMatch.of( |
| 26 | + OpcodeMatch.of(DUP), |
| 27 | + OpcodeMatch.of(SWAP), |
| 28 | + JumpMatch.of().capture("fake-jump"), |
| 29 | + JumpMatch.of().capture("correct-jump"), |
| 30 | + NumberMatch.of() |
| 31 | + ) |
| 32 | + }; |
| 33 | + |
| 34 | + Match flowDupJumpMatch = SequenceMatch.of( |
| 35 | + OpcodeMatch.of(DUP), |
| 36 | + JumpMatch.of().capture("fake-jump"), |
| 37 | + OpcodeMatch.of(ASTORE).capture("correct-store"), |
| 38 | + OpcodeMatch.of(ALOAD), |
| 39 | + JumpMatch.of() |
| 40 | + ); |
| 41 | + |
| 42 | + Match flowDupJumpMatch2 = SequenceMatch.of( |
| 43 | + OpcodeMatch.of(DUP), |
| 44 | + JumpMatch.of().capture("fake-jump"), |
| 45 | + OpcodeMatch.of(ASTORE).capture("correct-store") |
| 46 | + ); |
| 47 | + |
| 48 | + Match flowFakeLoop = SequenceMatch.of( |
| 49 | + OpcodeMatch.of(ASTORE).capture("correct-store"), |
| 50 | + OpcodeMatch.of(ALOAD).capture("fake-load"), |
| 51 | + JumpMatch.of() |
| 52 | + ); |
| 53 | + |
| 54 | + Match errorJump = SequenceMatch.of( |
| 55 | + OpcodeMatch.of(ALOAD).capture("loaded-var"), |
| 56 | + JumpMatch.of() |
| 57 | + ); |
| 58 | + |
| 59 | + Match trashHandlers = SequenceMatch.of( |
| 60 | + Match.of(ctx -> (ctx.insn().getOpcode() >= IRETURN && ctx.insn().getOpcode() <= RETURN) || ctx.insn().getOpcode() == ATHROW), |
| 61 | + Match.of(ctx -> (ctx.insn().getOpcode() >= ACONST_NULL && ctx.insn().getOpcode() <= DCONST_1) || ctx.insn().getOpcode() == ALOAD || ctx.insn() instanceof LabelNode) |
| 62 | + ).doNotSkipLabels(); |
| 63 | + |
| 64 | + @Override |
| 65 | + protected void transform() throws Exception { |
| 66 | + scopedClasses().forEach(classWrapper -> classWrapper.methods().forEach(methodNode -> { |
| 67 | + MethodContext methodContext = MethodContext.of(classWrapper, methodNode); |
| 68 | + List<TryCatchBlockNode> tryCatchBlocks = methodNode.tryCatchBlocks; |
| 69 | + if (tryCatchBlocks != null && !tryCatchBlocks.isEmpty()) { |
| 70 | + Set<AbstractInsnNode> toRemove = new HashSet<>(); |
| 71 | + Set<LabelNode> trashLabels = new HashSet<>(); |
| 72 | + for (Match flowDupEquationMatch : flowDupEquationMatches) { |
| 73 | + flowDupEquationMatch.findAllMatches(methodContext).forEach(matchContext -> { |
| 74 | + if (matchContext.captures().containsKey("swap")) System.out.println(matchContext.captures().containsKey("swap")); |
| 75 | + LabelNode labelNode = matchContext.captures().get("fake-jump").insn().asJump().label; |
| 76 | + if (labelNode != null && labelNode.getNext() != null && labelNode.getNext() instanceof FrameNode && labelNode.getNext(2) != null && labelNode.getNext(2).getOpcode() == POP) { |
| 77 | + toRemove.add(labelNode); |
| 78 | + trashLabels.add(labelNode); |
| 79 | + toRemove.add(labelNode.getNext()); |
| 80 | + toRemove.add(labelNode.getNext(2)); |
| 81 | + markChange(); |
| 82 | + } else if (labelNode != null && labelNode.getNext() != null && labelNode.getNext().getOpcode() == POP) { |
| 83 | + toRemove.add(labelNode); |
| 84 | + trashLabels.add(labelNode); |
| 85 | + toRemove.add(labelNode.getNext()); |
| 86 | + markChange(); |
| 87 | + } |
| 88 | + toRemove.addAll(matchContext.collectedInsns()); |
| 89 | + toRemove.remove(matchContext.captures().get("correct-jump").insn()); |
| 90 | + markChange(); |
| 91 | + }); |
| 92 | + } |
| 93 | + flowFakeLoop.findAllMatches(methodContext).forEach(matchContext -> { |
| 94 | + VarInsnNode varInsnNode = (VarInsnNode) matchContext.captures().get("correct-store").insn(); |
| 95 | + VarInsnNode varInsnNode1 = (VarInsnNode) matchContext.captures().get("fake-load").insn(); |
| 96 | + if (varInsnNode.var != varInsnNode1.var) { |
| 97 | + toRemove.addAll(matchContext.collectedInsns()); |
| 98 | + toRemove.remove(matchContext.captures().get("correct-store").insn()); |
| 99 | + markChange(); |
| 100 | + } |
| 101 | + }); |
| 102 | + JumpInsnNode jumpInsnNode = null; |
| 103 | + try { |
| 104 | + if ((methodNode.instructions.get(0) instanceof LabelNode && methodNode.instructions.get(1) instanceof JumpInsnNode)) { |
| 105 | + jumpInsnNode = methodNode.instructions.get(1).asJump(); |
| 106 | + trashLabels.add((LabelNode) methodNode.instructions.get(0)); |
| 107 | + } else if (methodNode.instructions.get(0) instanceof JumpInsnNode) { |
| 108 | + jumpInsnNode = methodNode.instructions.get(0).asJump(); |
| 109 | + } |
| 110 | + if (jumpInsnNode != null) { |
| 111 | + int i = 0; |
| 112 | + if ((jumpInsnNode.label.getNext().isVarLoad() && ((VarInsnNode)jumpInsnNode.label.getNext()).var == 0 || (jumpInsnNode.label.getNext().getOpcode() >= ACONST_NULL && jumpInsnNode.label.getNext().getOpcode() <= DCONST_1))) { |
| 113 | + while (jumpInsnNode.label.getNext(i) != null && !jumpInsnNode.label.getNext(i).isVarStore()) { |
| 114 | + i++; |
| 115 | + } |
| 116 | + VarInsnNode flowVarIndex = (VarInsnNode) jumpInsnNode.label.getNext(i); |
| 117 | + if (flowVarIndex != null) { |
| 118 | + errorJump.findAllMatches(methodContext).forEach(matchContext -> { |
| 119 | + if (((VarInsnNode) matchContext.captures().get("loaded-var").insn()).var == flowVarIndex.var) { |
| 120 | + toRemove.addAll(matchContext.collectedInsns()); |
| 121 | + } |
| 122 | + markChange(); |
| 123 | + }); |
| 124 | + } |
| 125 | + toRemove.add(jumpInsnNode); |
| 126 | + jumpInsnNode = null; |
| 127 | + } |
| 128 | + } |
| 129 | + } catch (Exception exception) { |
| 130 | + exception.printStackTrace(); |
| 131 | + } |
| 132 | + flowDupJumpMatch.findAllMatches(methodContext).forEach(matchContext -> { |
| 133 | + LabelNode labelNode = matchContext.captures().get("fake-jump").insn().asJump().label; |
| 134 | + if (labelNode != null && labelNode.getNext() != null && labelNode.getNext() instanceof FrameNode && labelNode.getNext(2) != null && labelNode.getNext(2).getOpcode() == GOTO) { |
| 135 | + toRemove.add(labelNode); |
| 136 | + trashLabels.add(labelNode); |
| 137 | + toRemove.add(labelNode.getNext()); |
| 138 | + toRemove.add(labelNode.getNext(2)); |
| 139 | + markChange(); |
| 140 | + } |
| 141 | + toRemove.addAll(matchContext.collectedInsns()); |
| 142 | + toRemove.remove(matchContext.captures().get("correct-store").insn()); |
| 143 | + markChange(); |
| 144 | + }); |
| 145 | + flowDupJumpMatch2.findAllMatches(methodContext).forEach(matchContext -> { |
| 146 | + LabelNode labelNode = matchContext.captures().get("fake-jump").insn().asJump().label; |
| 147 | + if (labelNode != null && labelNode.getNext() != null && labelNode.getNext().getOpcode() == GOTO) { |
| 148 | + toRemove.add(labelNode); |
| 149 | + trashLabels.add(labelNode); |
| 150 | + toRemove.add(labelNode.getNext()); |
| 151 | + markChange(); |
| 152 | + } |
| 153 | + toRemove.addAll(matchContext.collectedInsns()); |
| 154 | + toRemove.remove(matchContext.captures().get("correct-store").insn()); |
| 155 | + markChange(); |
| 156 | + }); |
| 157 | + |
| 158 | + try { |
| 159 | + AbstractInsnNode returnNode = trashHandlers.findFirstMatch(methodContext).insn(); |
| 160 | + |
| 161 | + AbstractInsnNode next = returnNode.getNext(); |
| 162 | + while (next != null) { |
| 163 | + if (next instanceof LabelNode label) { |
| 164 | + trashLabels.add(label); |
| 165 | + } |
| 166 | + next = next.getNext(); |
| 167 | + } |
| 168 | + } catch (Exception ignored) {} |
| 169 | + |
| 170 | + toRemove.forEach( |
| 171 | + methodNode.instructions::remove |
| 172 | + ); |
| 173 | + |
| 174 | + boolean changed = true; |
| 175 | + |
| 176 | + if (jumpInsnNode != null) { |
| 177 | + AbstractInsnNode next = jumpInsnNode.label; |
| 178 | + while (next != null) { |
| 179 | + if (next instanceof LabelNode label) { |
| 180 | + trashLabels.add(label); |
| 181 | + } |
| 182 | + next = next.getNext(); |
| 183 | + } |
| 184 | + while (jumpInsnNode.label.getNext() != null && !(jumpInsnNode.label.getNext() instanceof JumpInsnNode)) { |
| 185 | + AbstractInsnNode abstractInsnNode = jumpInsnNode.label.getNext(); |
| 186 | + if (abstractInsnNode instanceof LabelNode) { |
| 187 | + methodNode.instructions.remove(abstractInsnNode); |
| 188 | + continue; |
| 189 | + } |
| 190 | + methodNode.instructions.remove(abstractInsnNode); |
| 191 | + methodNode.instructions.insertBefore(jumpInsnNode, abstractInsnNode); |
| 192 | + markChange(); |
| 193 | + } |
| 194 | + |
| 195 | + methodNode.instructions.remove(jumpInsnNode); |
| 196 | + markChange(); |
| 197 | + } else { |
| 198 | + changed = !toRemove.isEmpty(); |
| 199 | + } |
| 200 | + |
| 201 | + if (!changed) return; |
| 202 | + |
| 203 | + Set<TryCatchBlockNode> tryCatchBlockNodes = new HashSet<>(); |
| 204 | + |
| 205 | + for (TryCatchBlockNode tryCatchBlock : tryCatchBlocks) { |
| 206 | + if (trashLabels.contains(tryCatchBlock.start) || trashLabels.contains(tryCatchBlock.handler) || trashLabels.contains(tryCatchBlock.end)) { |
| 207 | + tryCatchBlockNodes.add(tryCatchBlock); |
| 208 | + markChange(); |
| 209 | + } |
| 210 | + } |
| 211 | + |
| 212 | + tryCatchBlockNodes.forEach( |
| 213 | + methodNode.tryCatchBlocks::remove |
| 214 | + ); |
| 215 | + } |
| 216 | + })); |
| 217 | + } |
| 218 | +} |
0 commit comments