|
| 1 | +package dev.skidfuscator.obfuscator.transform.impl.hash; |
| 2 | + |
| 3 | +import dev.skidfuscator.obfuscator.Skidfuscator; |
| 4 | +import dev.skidfuscator.obfuscator.event.annotation.Listen; |
| 5 | +import dev.skidfuscator.obfuscator.event.impl.transform.method.RunMethodTransformEvent; |
| 6 | +import dev.skidfuscator.obfuscator.skidasm.SkidMethodNode; |
| 7 | +import dev.skidfuscator.obfuscator.skidasm.cfg.SkidBlock; |
| 8 | +import dev.skidfuscator.obfuscator.transform.AbstractTransformer; |
| 9 | +import dev.skidfuscator.obfuscator.util.cfg.Variables; |
| 10 | +import org.mapleir.ir.cfg.BasicBlock; |
| 11 | +import org.mapleir.ir.cfg.ControlFlowGraph; |
| 12 | +import org.mapleir.ir.code.Expr; |
| 13 | +import org.mapleir.ir.code.Stmt; |
| 14 | +import org.mapleir.ir.code.expr.ConstantExpr; |
| 15 | +import org.mapleir.ir.code.expr.VarExpr; |
| 16 | +import org.mapleir.ir.code.expr.invoke.InvocationExpr; |
| 17 | +import org.mapleir.ir.code.expr.invoke.StaticInvocationExpr; |
| 18 | +import org.mapleir.ir.code.expr.invoke.VirtualInvocationExpr; |
| 19 | +import sdk.LongHashFunction; |
| 20 | + |
| 21 | +import java.util.HashSet; |
| 22 | + |
| 23 | +public class StringEqualsIgnoreCaseHashTranformer extends AbstractTransformer { |
| 24 | + public StringEqualsIgnoreCaseHashTranformer(final Skidfuscator skidfuscator) { |
| 25 | + super(skidfuscator, "String EqIgCase Hash"); |
| 26 | + } |
| 27 | + |
| 28 | + @Listen |
| 29 | + void handle(final RunMethodTransformEvent event) { |
| 30 | + final SkidMethodNode methodNode = event.getMethodNode(); |
| 31 | + |
| 32 | + if (methodNode.isAbstract() |
| 33 | + || methodNode.isInit()) { |
| 34 | + this.skip(); |
| 35 | + return; |
| 36 | + } |
| 37 | + |
| 38 | + if (methodNode.node.instructions.size() > 10000) { |
| 39 | + this.fail(); |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + final ControlFlowGraph cfg = methodNode.getCfg(); |
| 44 | + |
| 45 | + if (cfg == null) { |
| 46 | + this.fail(); |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + for (BasicBlock vertex : new HashSet<>(cfg.vertices())) { |
| 51 | + if (vertex.isFlagSet(SkidBlock.FLAG_NO_OPAQUE)) |
| 52 | + continue; |
| 53 | + |
| 54 | + if (methodNode.isClinit() && this.heuristicSizeSkip(methodNode, 8.f)) { |
| 55 | + continue; |
| 56 | + } |
| 57 | + |
| 58 | + for (Stmt stmt : new HashSet<>(vertex)) { |
| 59 | + for (Expr expr : stmt.enumerateOnlyChildren()) { |
| 60 | + if (expr instanceof InvocationExpr invocationExpr) { |
| 61 | + final boolean isEqualsMethod = |
| 62 | + invocationExpr.getOwner().equals("java/lang/String") |
| 63 | + && invocationExpr.getName().equals("equalsIgnoreCase") |
| 64 | + && invocationExpr.getDesc().equals("(Ljava/lang/String;)Z"); |
| 65 | + |
| 66 | + if (!isEqualsMethod) |
| 67 | + continue; |
| 68 | + |
| 69 | + System.out.println(String.format("Found %s with matching", expr)); |
| 70 | + |
| 71 | + final Expr[] args = invocationExpr.getArgumentExprs(); |
| 72 | + Expr arg0 = args[0]; |
| 73 | + Expr arg1 = args[1]; |
| 74 | + |
| 75 | + if (arg0 instanceof VarExpr var0) { |
| 76 | + arg0 = Variables.getDefinition(cfg, var0); |
| 77 | + } |
| 78 | + |
| 79 | + if (arg1 instanceof VarExpr var1) { |
| 80 | + arg1 = Variables.getDefinition(cfg, var1); |
| 81 | + } |
| 82 | + |
| 83 | + final boolean isArg0Constant = arg0 instanceof ConstantExpr; |
| 84 | + final boolean isArg1Constant = arg1 instanceof ConstantExpr; |
| 85 | + |
| 86 | + if (isArg0Constant == isArg1Constant) { |
| 87 | + continue; |
| 88 | + } |
| 89 | + |
| 90 | + ConstantExpr constantExpr = isArg0Constant ? (ConstantExpr) arg0 : (ConstantExpr) arg1; |
| 91 | + Expr otherExpr = isArg0Constant ? arg1 : arg0; |
| 92 | + |
| 93 | + constantExpr.setConstant( |
| 94 | + "" + LongHashFunction.xx3().hashChars(((String) constantExpr.getConstant()).toLowerCase()) |
| 95 | + ); |
| 96 | + otherExpr.getParent().overwrite(otherExpr, new StaticInvocationExpr( |
| 97 | + new Expr[] { new VirtualInvocationExpr( |
| 98 | + InvocationExpr.CallType.VIRTUAL, |
| 99 | + new Expr[]{otherExpr.copy()}, |
| 100 | + "java/lang/String", |
| 101 | + "toLowerCase", |
| 102 | + "()Ljava/lang/String;" |
| 103 | + )}, |
| 104 | + "sdk/SDK", |
| 105 | + "hash", |
| 106 | + "(Ljava/lang/String;)Ljava/lang/String;" |
| 107 | + )); |
| 108 | + this.success(); |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | +} |
0 commit comments