From ac6e6d040f1bb62bf7fe1f542e2d8215b7063ca3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jindra=20Pet=C5=99=C3=ADk?= Date: Sun, 22 Nov 2015 06:02:11 +0100 Subject: [PATCH] Putting in old AVM2 deobfuscator Issue #1033 and other: Fix inclocal/declocal deobfuscation --- .../jpexs/decompiler/flash/abc/avm2/AVM2Code.java | 8 ++++---- .../deobfuscation/AVM2DeobfuscatorRegisters.java | 9 +++++---- .../deobfuscation/AVM2DeobfuscatorRegistersOld.java | 13 +++++++------ .../flash/abc/avm2/instructions/debug/DebugIns.java | 2 +- .../abc/avm2/instructions/other2/DecLocalPIns.java | 2 +- .../abc/avm2/instructions/other2/IncLocalPIns.java | 2 +- 6 files changed, 19 insertions(+), 17 deletions(-) diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/AVM2Code.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/AVM2Code.java index 5e97ee298..b45671fe8 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/AVM2Code.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/AVM2Code.java @@ -24,6 +24,7 @@ import com.jpexs.decompiler.flash.abc.CopyOutputStream; import com.jpexs.decompiler.flash.abc.avm2.deobfuscation.AVM2DeobfuscatorGetSet; import com.jpexs.decompiler.flash.abc.avm2.deobfuscation.AVM2DeobfuscatorJumps; import com.jpexs.decompiler.flash.abc.avm2.deobfuscation.AVM2DeobfuscatorRegisters; +import com.jpexs.decompiler.flash.abc.avm2.deobfuscation.AVM2DeobfuscatorRegistersOld; import com.jpexs.decompiler.flash.abc.avm2.deobfuscation.AVM2DeobfuscatorSimple; import com.jpexs.decompiler.flash.abc.avm2.exceptions.AVM2ExecutionException; import com.jpexs.decompiler.flash.abc.avm2.exceptions.AVM2VerifyErrorException; @@ -349,8 +350,7 @@ public class AVM2Code implements Cloneable { public static final int DAT_DEBUG_TYPE = OPT_U8 + 0x05; - public static final int DAT_REGISTER_INDEX = OPT_U8 + 0x06; - + //public static final int DAT_REGISTER_INDEX = OPT_U8 + 0x06; public static final int DAT_LINENUM = OPT_U30 + 0x07; public static final int DAT_LOCAL_REG_INDEX = OPT_U30 + 0x08; @@ -2335,7 +2335,7 @@ public class AVM2Code implements Cloneable { new AVM2DeobfuscatorSimple().avm2CodeRemoveTraps(path, classIndex, isStatic, scriptIndex, abc, trait, methodInfo, body); } try (Statistics s = new Statistics("AVM2DeobfuscatorRegisters")) { - new AVM2DeobfuscatorRegisters().avm2CodeRemoveTraps(path, classIndex, isStatic, scriptIndex, abc, trait, methodInfo, body); + new AVM2DeobfuscatorRegistersOld().avm2CodeRemoveTraps(path, classIndex, isStatic, scriptIndex, abc, trait, methodInfo, body); } try (Statistics s = new Statistics("AVM2DeobfuscatorJumps")) { new AVM2DeobfuscatorJumps().avm2CodeRemoveTraps(path, classIndex, isStatic, scriptIndex, abc, trait, methodInfo, body); @@ -2399,7 +2399,7 @@ public class AVM2Code implements Cloneable { } else { for (int i = 0; i < ins.definition.operands.length; i++) { int op = ins.definition.operands[i]; - if (op == DAT_REGISTER_INDEX/* || op == DAT_LOCAL_REG_INDEX ???*/) { + if (op == DAT_LOCAL_REG_INDEX) { handleRegister(stats, ins.operands[i]); } } diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/deobfuscation/AVM2DeobfuscatorRegisters.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/deobfuscation/AVM2DeobfuscatorRegisters.java index da59bf986..cd9830597 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/deobfuscation/AVM2DeobfuscatorRegisters.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/deobfuscation/AVM2DeobfuscatorRegisters.java @@ -25,8 +25,10 @@ import com.jpexs.decompiler.flash.abc.avm2.exceptions.AVM2ExecutionException; import com.jpexs.decompiler.flash.abc.avm2.instructions.AVM2Instruction; import com.jpexs.decompiler.flash.abc.avm2.instructions.DeobfuscatePopIns; import com.jpexs.decompiler.flash.abc.avm2.instructions.InstructionDefinition; +import com.jpexs.decompiler.flash.abc.avm2.instructions.debug.DebugIns; import com.jpexs.decompiler.flash.abc.avm2.instructions.jumps.JumpIns; import com.jpexs.decompiler.flash.abc.avm2.instructions.localregs.GetLocalTypeIns; +import com.jpexs.decompiler.flash.abc.avm2.instructions.localregs.KillIns; import com.jpexs.decompiler.flash.abc.avm2.instructions.localregs.SetLocalTypeIns; import com.jpexs.decompiler.flash.abc.avm2.instructions.other.ReturnValueIns; import com.jpexs.decompiler.flash.abc.avm2.instructions.other.ReturnVoidIns; @@ -74,7 +76,7 @@ public class AVM2DeobfuscatorRegisters extends AVM2DeobfuscatorSimple { } else { for (int p = 0; p < ins.definition.operands.length; p++) { int op = ins.definition.operands[p]; - if (op == AVM2Code.DAT_REGISTER_INDEX || op == AVM2Code.DAT_LOCAL_REG_INDEX) { + if (op == AVM2Code.DAT_LOCAL_REG_INDEX) { int regId = ins.operands[p]; regs.add(regId); } @@ -233,7 +235,6 @@ public class AVM2DeobfuscatorRegisters extends AVM2DeobfuscatorSimple { Logger.getLogger(AVM2DeobfuscatorRegisters.class.getName()).log(Level.SEVERE, null, ex); } - //if (!(def instanceof KillIns)) if (def instanceof SetLocalTypeIns) { int regId = ((SetLocalTypeIns) def).getRegisterId(ins); if (!ignored.contains(regId)) { @@ -246,10 +247,10 @@ public class AVM2DeobfuscatorRegisters extends AVM2DeobfuscatorSimple { assignment.setVal(ins); return regId; } - } else { + } else if (!(def instanceof KillIns) && !(def instanceof DebugIns)) { for (int p = 0; p < ins.definition.operands.length; p++) { int op = ins.definition.operands[p]; - if (op == AVM2Code.DAT_REGISTER_INDEX/* || op == AVM2Code.DAT_LOCAL_REG_INDEX ???*/) { + if (op == AVM2Code.DAT_LOCAL_REG_INDEX) { int regId = ins.operands[p]; if (!ignored.contains(regId)) { assignment.setVal(ins); diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/deobfuscation/AVM2DeobfuscatorRegistersOld.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/deobfuscation/AVM2DeobfuscatorRegistersOld.java index a52bfad12..8c593bb95 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/deobfuscation/AVM2DeobfuscatorRegistersOld.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/deobfuscation/AVM2DeobfuscatorRegistersOld.java @@ -24,8 +24,10 @@ import com.jpexs.decompiler.flash.abc.avm2.AVM2ConstantPool; import com.jpexs.decompiler.flash.abc.avm2.instructions.AVM2Instruction; import com.jpexs.decompiler.flash.abc.avm2.instructions.DeobfuscatePopIns; import com.jpexs.decompiler.flash.abc.avm2.instructions.InstructionDefinition; +import com.jpexs.decompiler.flash.abc.avm2.instructions.debug.DebugIns; import com.jpexs.decompiler.flash.abc.avm2.instructions.jumps.JumpIns; import com.jpexs.decompiler.flash.abc.avm2.instructions.localregs.GetLocalTypeIns; +import com.jpexs.decompiler.flash.abc.avm2.instructions.localregs.KillIns; import com.jpexs.decompiler.flash.abc.avm2.instructions.localregs.SetLocalTypeIns; import com.jpexs.decompiler.flash.abc.avm2.instructions.other.ReturnValueIns; import com.jpexs.decompiler.flash.abc.avm2.instructions.other.ReturnVoidIns; @@ -72,7 +74,7 @@ public class AVM2DeobfuscatorRegistersOld extends AVM2DeobfuscatorSimpleOld { } else { for (int p = 0; p < ins.definition.operands.length; p++) { int op = ins.definition.operands[p]; - if (op == AVM2Code.DAT_REGISTER_INDEX || op == AVM2Code.DAT_LOCAL_REG_INDEX) { + if (op == AVM2Code.DAT_LOCAL_REG_INDEX) { int regId = ins.operands[p]; regs.add(regId); } @@ -217,7 +219,7 @@ public class AVM2DeobfuscatorRegistersOld extends AVM2DeobfuscatorSimpleOld { AVM2Instruction ins = code.code.get(idx); InstructionDefinition def = ins.definition; - //System.err.println("" + idx + ": " + ins + " stack:" + stack.size()); + System.err.println("" + idx + ": " + ins + " stack:" + stack.size()); // do not throw EmptyStackException, much faster int requiredStackSize = ins.getStackPopCount(localData); @@ -226,8 +228,6 @@ public class AVM2DeobfuscatorRegistersOld extends AVM2DeobfuscatorSimpleOld { } ins.translate(localData, stack, output, Graph.SOP_USE_STATIC, ""); - - //if (!(def instanceof KillIns)) if (def instanceof SetLocalTypeIns) { int regId = ((SetLocalTypeIns) def).getRegisterId(ins); if (!ignored.contains(regId)) { @@ -240,10 +240,11 @@ public class AVM2DeobfuscatorRegistersOld extends AVM2DeobfuscatorSimpleOld { assignment.setVal(ins); return regId; } - } else { + } else if (!(def instanceof KillIns) && !(def instanceof DebugIns)) { + //can be inclocal, declocal, hasnext... for (int p = 0; p < ins.definition.operands.length; p++) { int op = ins.definition.operands[p]; - if (op == AVM2Code.DAT_REGISTER_INDEX/* || op == AVM2Code.DAT_LOCAL_REG_INDEX ???*/) { + if (op == AVM2Code.DAT_LOCAL_REG_INDEX) { int regId = ins.operands[p]; if (!ignored.contains(regId)) { assignment.setVal(ins); diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/instructions/debug/DebugIns.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/instructions/debug/DebugIns.java index 768b623ff..d8d907661 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/instructions/debug/DebugIns.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/instructions/debug/DebugIns.java @@ -25,7 +25,7 @@ import com.jpexs.decompiler.flash.abc.avm2.instructions.InstructionDefinition; public class DebugIns extends InstructionDefinition { public DebugIns() { - super(0xef, "debug", new int[]{AVM2Code.DAT_DEBUG_TYPE, AVM2Code.DAT_STRING_INDEX, AVM2Code.DAT_REGISTER_INDEX, AVM2Code.OPT_U30}, false); + super(0xef, "debug", new int[]{AVM2Code.DAT_DEBUG_TYPE, AVM2Code.DAT_STRING_INDEX, AVM2Code.DAT_LOCAL_REG_INDEX, AVM2Code.OPT_U30}, false); } @Override diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/instructions/other2/DecLocalPIns.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/instructions/other2/DecLocalPIns.java index 2f7ea9bd2..376a0916b 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/instructions/other2/DecLocalPIns.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/instructions/other2/DecLocalPIns.java @@ -27,7 +27,7 @@ import com.jpexs.decompiler.flash.abc.avm2.instructions.InstructionDefinition; public class DecLocalPIns extends InstructionDefinition { public DecLocalPIns() { - super(0x9F, "declocal_p", new int[]{AVM2Code.DAT_DECIMAL_PARAMS, AVM2Code.DAT_REGISTER_INDEX}, false /*?*/); + super(0x9F, "declocal_p", new int[]{AVM2Code.DAT_DECIMAL_PARAMS, AVM2Code.DAT_LOCAL_REG_INDEX}, false /*?*/); } @Override diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/instructions/other2/IncLocalPIns.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/instructions/other2/IncLocalPIns.java index 9e062a856..949a41f6c 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/instructions/other2/IncLocalPIns.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/instructions/other2/IncLocalPIns.java @@ -27,7 +27,7 @@ import com.jpexs.decompiler.flash.abc.avm2.instructions.InstructionDefinition; public class IncLocalPIns extends InstructionDefinition { public IncLocalPIns() { - super(0x9D, "inclocal_p", new int[]{AVM2Code.DAT_DECIMAL_PARAMS, AVM2Code.DAT_REGISTER_INDEX}, true /*?*/); + super(0x9D, "inclocal_p", new int[]{AVM2Code.DAT_DECIMAL_PARAMS, AVM2Code.DAT_LOCAL_REG_INDEX}, true /*?*/); } @Override