AS3: better deobfuscation

This commit is contained in:
Jindra Petk
2013-04-18 21:17:54 +02:00
parent 1e30c1a7dc
commit 8b29ca20ff
8 changed files with 106 additions and 23 deletions

View File

@@ -2411,27 +2411,32 @@ public class AVM2Code implements Serializable {
public boolean skipUsed = false;
}
private static int removeTraps(boolean secondPass, boolean useVisited, List localData, Stack<GraphTargetItem> stack, List<GraphTargetItem> output, AVM2GraphSource code, int ip, int lastIp, List<Integer> visited, HashMap<Integer, HashMap<Integer, GraphTargetItem>> visitedStates, HashMap<GraphSourceItem, Decision> decisions) {
private static int removeTraps(boolean secondPass, boolean useVisited, List localData, Stack<GraphTargetItem> stack, List<GraphTargetItem> output, AVM2GraphSource code, int ip, int lastIp, HashMap<Integer, Integer> visited, HashMap<Integer, HashMap<Integer, GraphTargetItem>> visitedStates, HashMap<GraphSourceItem, Decision> decisions) {
boolean debugMode = false;
int ret = 0;
iploop:
while ((ip > -1) && ip < code.size()) {
HashMap<Integer, GraphTargetItem> currentState = (HashMap<Integer, GraphTargetItem>) localData.get(2);
if (visitedStates.containsKey(ip)) {
HashMap<Integer, GraphTargetItem> lastState = visitedStates.get(ip);
if (lastState.equals(currentState)) {
if (useVisited) {
if (visited.containsKey(ip)) {
break;
}
}
visitedStates.put(ip, (HashMap<Integer, GraphTargetItem>) currentState.clone());
if (useVisited && visited.contains(ip)) {
break;
}
if (!visited.contains(ip)) {
visited.add(ip);
if (!visited.containsKey(ip)) {
visited.put(ip, 0);
} else {
visited.put(ip, visited.get(ip) + 1);
}
} else {
HashMap<Integer, GraphTargetItem> currentState = (HashMap<Integer, GraphTargetItem>) localData.get(2);
if (visitedStates.containsKey(ip)) {
HashMap<Integer, GraphTargetItem> lastState = visitedStates.get(ip);
if (lastState.equals(currentState)) {
break;
}
}
visitedStates.put(ip, (HashMap<Integer, GraphTargetItem>) currentState.clone());
}
lastIp = ip;
GraphSourceItem ins = code.get(ip);
@@ -2440,7 +2445,7 @@ public class AVM2Code implements Serializable {
continue;
}
if (debugMode) {
System.out.println((useVisited ? "useV " : "") + "Visit " + ip + ": " + ins + " stack:" + Highlighting.stripHilights(stack.toString()));
System.out.println((useVisited ? "useV " : "") + (secondPass ? "secondPass " : "") + "Visit " + ip + ": " + ins + " stack:" + Highlighting.stripHilights(stack.toString()));
}
AVM2Instruction ains = (AVM2Instruction) ins;
if (ains.definition instanceof DupIns) {
@@ -2551,6 +2556,15 @@ public class AVM2Code implements Serializable {
} else {
if (ins.isBranch() && (!ins.isJump())) {
stack.pop();
Decision dec = new Decision();
if (decisions.containsKey(ins)) {
dec = decisions.get(ins);
} else {
decisions.put(ins, dec);
}
dec.jumpUsed = true;
dec.skipUsed = true;
}
for (int b : branches) {
@@ -2576,7 +2590,8 @@ public class AVM2Code implements Serializable {
public static int removeTraps(List localData, AVM2GraphSource code, int addr) {
HashMap<GraphSourceItem, AVM2Code.Decision> decisions = new HashMap<GraphSourceItem, AVM2Code.Decision>();
removeTraps(false, false, localData, new Stack<GraphTargetItem>(), new ArrayList<GraphTargetItem>(), code, code.adr2pos(addr), 0, new ArrayList<Integer>(), new HashMap<Integer, HashMap<Integer, GraphTargetItem>>(), decisions);
return removeTraps(true, false, localData, new Stack<GraphTargetItem>(), new ArrayList<GraphTargetItem>(), code, code.adr2pos(addr), 0, new ArrayList<Integer>(), new HashMap<Integer, HashMap<Integer, GraphTargetItem>>(), decisions);
removeTraps(false, false, localData, new Stack<GraphTargetItem>(), new ArrayList<GraphTargetItem>(), code, code.adr2pos(addr), 0, new HashMap<Integer, Integer>(), new HashMap<Integer, HashMap<Integer, GraphTargetItem>>(), decisions);
localData.set(2, new HashMap<Integer, GraphTargetItem>());
return removeTraps(true, true, localData, new Stack<GraphTargetItem>(), new ArrayList<GraphTargetItem>(), code, code.adr2pos(addr), 0, new HashMap<Integer, Integer>(), new HashMap<Integer, HashMap<Integer, GraphTargetItem>>(), decisions);
}
}

View File

@@ -23,6 +23,7 @@ import com.jpexs.decompiler.flash.abc.avm2.instructions.AVM2Instruction;
import com.jpexs.decompiler.flash.abc.avm2.instructions.InstructionDefinition;
import com.jpexs.decompiler.flash.abc.avm2.treemodel.DecLocalTreeItem;
import com.jpexs.decompiler.flash.abc.avm2.treemodel.IntegerValueTreeItem;
import com.jpexs.decompiler.flash.abc.avm2.treemodel.NotCompileTimeTreeItem;
import com.jpexs.decompiler.flash.abc.avm2.treemodel.operations.SubtractTreeItem;
import com.jpexs.decompiler.flash.abc.types.MethodInfo;
import com.jpexs.decompiler.flash.graph.GraphTargetItem;
@@ -59,7 +60,11 @@ public class DecLocalIIns extends InstructionDefinition {
public void translate(boolean isStatic, int classIndex, java.util.HashMap<Integer, GraphTargetItem> localRegs, Stack<GraphTargetItem> stack, java.util.Stack<GraphTargetItem> scopeStack, ConstantPool constants, AVM2Instruction ins, MethodInfo[] method_info, List<GraphTargetItem> output, com.jpexs.decompiler.flash.abc.types.MethodBody body, com.jpexs.decompiler.flash.abc.ABC abc, HashMap<Integer, String> localRegNames, List<String> fullyQualifiedNames) {
int regIndex = ins.operands[0];
output.add(new DecLocalTreeItem(ins, regIndex));
localRegs.put(regIndex, new SubtractTreeItem(ins, localRegs.get(regIndex), new IntegerValueTreeItem(ins, new Long(1))));
if (localRegs.containsKey(regIndex)) {
localRegs.put(regIndex, new NotCompileTimeTreeItem(ins));
} else {
localRegs.put(regIndex, new SubtractTreeItem(ins, localRegs.get(regIndex), new IntegerValueTreeItem(ins, new Long(1))));
}
}
}

View File

@@ -23,6 +23,7 @@ import com.jpexs.decompiler.flash.abc.avm2.instructions.AVM2Instruction;
import com.jpexs.decompiler.flash.abc.avm2.instructions.InstructionDefinition;
import com.jpexs.decompiler.flash.abc.avm2.treemodel.DecLocalTreeItem;
import com.jpexs.decompiler.flash.abc.avm2.treemodel.IntegerValueTreeItem;
import com.jpexs.decompiler.flash.abc.avm2.treemodel.NotCompileTimeTreeItem;
import com.jpexs.decompiler.flash.abc.avm2.treemodel.operations.SubtractTreeItem;
import com.jpexs.decompiler.flash.abc.types.MethodInfo;
import com.jpexs.decompiler.flash.graph.GraphTargetItem;
@@ -59,6 +60,10 @@ public class DecLocalIns extends InstructionDefinition {
public void translate(boolean isStatic, int classIndex, java.util.HashMap<Integer, GraphTargetItem> localRegs, Stack<GraphTargetItem> stack, java.util.Stack<GraphTargetItem> scopeStack, ConstantPool constants, AVM2Instruction ins, MethodInfo[] method_info, List<GraphTargetItem> output, com.jpexs.decompiler.flash.abc.types.MethodBody body, com.jpexs.decompiler.flash.abc.ABC abc, HashMap<Integer, String> localRegNames, List<String> fullyQualifiedNames) {
int regIndex = ins.operands[0];
output.add(new DecLocalTreeItem(ins, regIndex));
localRegs.put(regIndex, new SubtractTreeItem(ins, localRegs.get(regIndex), new IntegerValueTreeItem(ins, new Long(1))));
if (localRegs.containsKey(regIndex)) {
localRegs.put(regIndex, new NotCompileTimeTreeItem(ins));
} else {
localRegs.put(regIndex, new SubtractTreeItem(ins, localRegs.get(regIndex), new IntegerValueTreeItem(ins, new Long(1))));
}
}
}

View File

@@ -22,6 +22,7 @@ import com.jpexs.decompiler.flash.abc.avm2.instructions.AVM2Instruction;
import com.jpexs.decompiler.flash.abc.avm2.instructions.InstructionDefinition;
import com.jpexs.decompiler.flash.abc.avm2.treemodel.IncLocalTreeItem;
import com.jpexs.decompiler.flash.abc.avm2.treemodel.IntegerValueTreeItem;
import com.jpexs.decompiler.flash.abc.avm2.treemodel.NotCompileTimeTreeItem;
import com.jpexs.decompiler.flash.abc.avm2.treemodel.operations.AddTreeItem;
import com.jpexs.decompiler.flash.abc.types.MethodInfo;
import com.jpexs.decompiler.flash.graph.GraphTargetItem;
@@ -39,6 +40,10 @@ public class IncLocalIIns extends InstructionDefinition {
public void translate(boolean isStatic, int classIndex, java.util.HashMap<Integer, GraphTargetItem> localRegs, Stack<GraphTargetItem> stack, java.util.Stack<GraphTargetItem> scopeStack, ConstantPool constants, AVM2Instruction ins, MethodInfo[] method_info, List<GraphTargetItem> output, com.jpexs.decompiler.flash.abc.types.MethodBody body, com.jpexs.decompiler.flash.abc.ABC abc, HashMap<Integer, String> localRegNames, List<String> fullyQualifiedNames) {
int regIndex = ins.operands[0];
output.add(new IncLocalTreeItem(ins, regIndex));
localRegs.put(regIndex, new AddTreeItem(ins, localRegs.get(regIndex), new IntegerValueTreeItem(ins, new Long(1))));
if (localRegs.containsKey(regIndex)) {
localRegs.put(regIndex, new NotCompileTimeTreeItem(ins));
} else {
localRegs.put(regIndex, new AddTreeItem(ins, localRegs.get(regIndex), new IntegerValueTreeItem(ins, new Long(1))));
}
}
}

View File

@@ -22,6 +22,7 @@ import com.jpexs.decompiler.flash.abc.avm2.instructions.AVM2Instruction;
import com.jpexs.decompiler.flash.abc.avm2.instructions.InstructionDefinition;
import com.jpexs.decompiler.flash.abc.avm2.treemodel.IncLocalTreeItem;
import com.jpexs.decompiler.flash.abc.avm2.treemodel.IntegerValueTreeItem;
import com.jpexs.decompiler.flash.abc.avm2.treemodel.NotCompileTimeTreeItem;
import com.jpexs.decompiler.flash.abc.avm2.treemodel.operations.AddTreeItem;
import com.jpexs.decompiler.flash.abc.types.MethodInfo;
import com.jpexs.decompiler.flash.graph.GraphTargetItem;
@@ -39,6 +40,10 @@ public class IncLocalIns extends InstructionDefinition {
public void translate(boolean isStatic, int classIndex, java.util.HashMap<Integer, GraphTargetItem> localRegs, Stack<GraphTargetItem> stack, java.util.Stack<GraphTargetItem> scopeStack, ConstantPool constants, AVM2Instruction ins, MethodInfo[] method_info, List<GraphTargetItem> output, com.jpexs.decompiler.flash.abc.types.MethodBody body, com.jpexs.decompiler.flash.abc.ABC abc, HashMap<Integer, String> localRegNames, List<String> fullyQualifiedNames) {
int regIndex = ins.operands[0];
output.add(new IncLocalTreeItem(ins, regIndex));
localRegs.put(regIndex, new AddTreeItem(ins, localRegs.get(regIndex), new IntegerValueTreeItem(ins, new Long(1))));
if (localRegs.containsKey(regIndex)) {
localRegs.put(regIndex, new NotCompileTimeTreeItem(ins));
} else {
localRegs.put(regIndex, new AddTreeItem(ins, localRegs.get(regIndex), new IntegerValueTreeItem(ins, new Long(1))));
}
}
}

View File

@@ -26,6 +26,7 @@ import com.jpexs.decompiler.flash.abc.avm2.treemodel.FindPropertyTreeItem;
import com.jpexs.decompiler.flash.abc.avm2.treemodel.IncrementTreeItem;
import com.jpexs.decompiler.flash.abc.avm2.treemodel.LocalRegTreeItem;
import com.jpexs.decompiler.flash.abc.avm2.treemodel.NewActivationTreeItem;
import com.jpexs.decompiler.flash.abc.avm2.treemodel.NotCompileTimeTreeItem;
import com.jpexs.decompiler.flash.abc.avm2.treemodel.PostDecrementTreeItem;
import com.jpexs.decompiler.flash.abc.avm2.treemodel.PostIncrementTreeItem;
import com.jpexs.decompiler.flash.abc.avm2.treemodel.SetLocalTreeItem;
@@ -48,7 +49,11 @@ public abstract class SetLocalTypeIns extends InstructionDefinition implements S
public void translate(boolean isStatic, int classIndex, java.util.HashMap<Integer, GraphTargetItem> localRegs, Stack<GraphTargetItem> stack, java.util.Stack<GraphTargetItem> scopeStack, ConstantPool constants, AVM2Instruction ins, MethodInfo[] method_info, List<GraphTargetItem> output, com.jpexs.decompiler.flash.abc.types.MethodBody body, com.jpexs.decompiler.flash.abc.ABC abc, HashMap<Integer, String> localRegNames, List<String> fullyQualifiedNames) {
int regId = getRegisterId(ins);
GraphTargetItem value = (GraphTargetItem) stack.pop();
localRegs.put(regId, value);
if (localRegs.containsKey(regId)) {
localRegs.put(regId, new NotCompileTimeTreeItem(ins));
} else {
localRegs.put(regId, value);
}
if (value instanceof NewActivationTreeItem) {
return;
}

View File

@@ -0,0 +1,43 @@
/*
* Copyright (C) 2013 JPEXS
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.jpexs.decompiler.flash.abc.avm2.treemodel;
import com.jpexs.decompiler.flash.abc.avm2.ConstantPool;
import com.jpexs.decompiler.flash.graph.GraphSourceItem;
import java.util.HashMap;
import java.util.List;
/**
*
* @author JPEXS
*/
public class NotCompileTimeTreeItem extends TreeItem {
public NotCompileTimeTreeItem(GraphSourceItem instruction) {
super(instruction, NOPRECEDENCE);
}
@Override
public String toString(ConstantPool constants, HashMap<Integer, String> localRegNames, List<String> fullyQualifiedNames) {
return "<NOTCOMPILETIME>";
}
@Override
public boolean isCompileTime() {
return false;
}
}

View File

@@ -77,7 +77,7 @@ public class DetailPanel extends JPanel implements ActionListener {
editButton.setMargin(new Insets(3, 3, 3, 10));
saveButton.setMargin(new Insets(3, 3, 3, 10));
cancelButton.setMargin(new Insets(3, 3, 3, 10));
buttonsPanel = new JPanel();
buttonsPanel.setLayout(new FlowLayout());
saveButton.setActionCommand("SAVEDETAIL");