AS3 fixed deobfuscation causing incorrect decompilation in some cases

alse Issue #47
This commit is contained in:
Jindra Petk
2013-03-25 20:50:06 +01:00
parent bd69e97f15
commit f843082a97
9 changed files with 59 additions and 43 deletions

View File

@@ -1173,7 +1173,17 @@ public class AVM2Code implements Serializable {
}
visited[ip] = true;
AVM2Instruction ins = code.get(ip);
if(debugMode){
System.out.println("translating ip "+ip+" ins "+ins.toString()+" stack:"+Highlighting.stripHilights(stack.toString())+" scopeStack:"+Highlighting.stripHilights(scopeStack.toString()));
}
if(ins.definition instanceof NewFunctionIns){
if(ip+1<=end){
if(code.get(ip+1).definition instanceof PopIns){
ip+=2;
continue;
}
}
}
if ((ip + 8 < code.size())) { //return in finally clause
if (ins.definition instanceof SetLocalTypeIns) {
if (code.get(ip + 1).definition instanceof PushByteIns) {
@@ -2417,7 +2427,8 @@ public class AVM2Code implements Serializable {
if (ins.isBranch() || ins.isJump()) {
List<Integer> branches = ins.getBranches(code);
if (ins.isBranch() && !stack.isEmpty() && (stack.peek().isCompileTime())) {
if ((ins instanceof AVM2Instruction)&&((AVM2Instruction)ins).definition instanceof IfTypeIns
&& (!(((AVM2Instruction)ins).definition instanceof JumpIns)) && (!stack.isEmpty()) && (stack.peek().isCompileTime())) {
boolean condition = stack.peek().toBoolean();
if (debugMode) {
if (condition) {
@@ -2433,7 +2444,9 @@ public class AVM2Code implements Serializable {
}
GraphTargetItem tar = stack.pop();
for (GraphSourceItemPos pos : tar.getNeededSources()) {
pos.item.setIgnored(true);
if(pos.item!=ins){
pos.item.setIgnored(true);
}
}
ret += removeTraps(localData, stack, output, code, condition ? branches.get(0) : branches.get(1), ip, visited);

View File

@@ -1556,4 +1556,17 @@ public class AVM2Graph extends Graph {
}
return true;
}
@Override
public List prepareBranchLocalData(List localData) {
List ret=new ArrayList();
ret.addAll(localData);
Stack<GraphTargetItem> scopeStack=(Stack<GraphTargetItem>)ret.get(DATA_SCOPESTACK);
Stack<GraphTargetItem> copyScopeStack=new Stack<GraphTargetItem>();
copyScopeStack.addAll(scopeStack);
ret.set(DATA_SCOPESTACK, copyScopeStack);
return ret;
}
}

View File

@@ -29,6 +29,8 @@ import com.jpexs.decompiler.flash.helpers.Highlighting;
import java.util.HashMap;
import java.util.List;
import java.util.Stack;
import java.util.logging.Level;
import java.util.logging.Logger;
public class NewFunctionIns extends InstructionDefinition {
@@ -43,7 +45,11 @@ public class NewFunctionIns extends InstructionDefinition {
String bodyStr = "";
String paramStr = "";
if (mybody != null) {
try{
bodyStr = Highlighting.hilighMethodEnd() + mybody.toString("", false, isStatic, classIndex, abc, constants, method_info, new Stack<GraphTargetItem>()/*scopeStack*/, false, true, fullyQualifiedNames, null) + Highlighting.hilighMethodBegin(body.method_info);
}catch(Exception ex){
Logger.getLogger(NewFunctionIns.class.getName()).log(Level.SEVERE, "error during newfunction", ex);
}
paramStr = method_info[methodIndex].getParamStr(constants, mybody, abc, fullyQualifiedNames);
}

View File

@@ -22,6 +22,8 @@ import com.jpexs.decompiler.flash.abc.avm2.LocalDataArea;
import com.jpexs.decompiler.flash.abc.avm2.instructions.AVM2Instruction;
import com.jpexs.decompiler.flash.abc.avm2.instructions.InstructionDefinition;
import com.jpexs.decompiler.flash.abc.types.MethodInfo;
import com.jpexs.decompiler.flash.graph.GraphSourceItem;
import com.jpexs.decompiler.flash.graph.GraphSourceItemPos;
import com.jpexs.decompiler.flash.graph.GraphTargetItem;
import java.util.HashMap;
import java.util.List;
@@ -45,6 +47,7 @@ public class DupIns extends InstructionDefinition {
GraphTargetItem v = stack.pop();
stack.push(v);
stack.push(v);
v.moreSrc.add(new GraphSourceItemPos(ins, 0));
}

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.types.MethodBody;
import com.jpexs.decompiler.flash.abc.types.MethodInfo;
import com.jpexs.decompiler.flash.graph.GraphSourceItemPos;
import com.jpexs.decompiler.flash.graph.GraphTargetItem;
import java.util.HashMap;
import java.util.List;
@@ -49,7 +50,8 @@ public class SwapIns extends InstructionDefinition {
GraphTargetItem o2 = stack.pop();
stack.push(o1);
stack.push(o2);
o1.moreSrc.add(new GraphSourceItemPos(ins, 0));
o2.moreSrc.add(new GraphSourceItemPos(ins, 0));
}
@Override

View File

@@ -26,6 +26,7 @@ import java.util.List;
public abstract class Trait implements Serializable {
public boolean debugMode = false;
public int name_index;
public int kindType;
public int kindFlags;

View File

@@ -54,6 +54,9 @@ public class TraitMethodGetterSetter extends Trait {
@Override
public String convert(String path, List<DoABCTag> abcTags, ABC abc, boolean isStatic, boolean pcode, int classIndex, boolean highlight, List<String> fullyQualifiedNames) {
if(debugMode){
System.out.println("Decompiling "+path + "." + getName(abc).getName(abc.constants, fullyQualifiedNames));
}
String header = convertHeader(path, abcTags, abc, isStatic, pcode, classIndex, highlight, fullyQualifiedNames);
String bodyStr = "";

View File

@@ -21,6 +21,8 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Stack;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
@@ -440,6 +442,7 @@ public class Graph {
try {
output.addAll(code.translatePart(localData, stack, start, end));
} catch (Exception ex) {
Logger.getLogger(Graph.class.getName()).log(Level.SEVERE, "error during printgraph", ex);
//ex.printStackTrace();
return ret;
}
@@ -976,7 +979,7 @@ public class Graph {
if (debugMode) {
System.err.println("ONTRUE: (inside " + part + ")");
}
onTrue = printGraph(localData, trueStack, allParts, part, part.nextParts.get(1), next == null ? stopPart : next, loops, forFinalCommands);
onTrue = printGraph(prepareBranchLocalData(localData), trueStack, allParts, part, part.nextParts.get(1), next == null ? stopPart : next, loops, forFinalCommands);
if (debugMode) {
System.err.println("/ONTRUE (inside " + part + ")");
}
@@ -988,7 +991,7 @@ public class Graph {
if (debugMode) {
System.err.println("ONFALSE: (inside " + part + ")");
}
onFalse = (((next == part.nextParts.get(0)) || (part.nextParts.get(0).path.equals(part.path) || part.nextParts.get(0).path.length() < part.path.length())) ? new ArrayList<GraphTargetItem>() : printGraph(localData, falseStack, allParts, part, part.nextParts.get(0), next == null ? stopPart : next, loops, forFinalCommands));
onFalse = (((next == part.nextParts.get(0)) || (part.nextParts.get(0).path.equals(part.path) || part.nextParts.get(0).path.length() < part.path.length())) ? new ArrayList<GraphTargetItem>() : printGraph(prepareBranchLocalData(localData), falseStack, allParts, part, part.nextParts.get(0), next == null ? stopPart : next, loops, forFinalCommands));
if (debugMode) {
System.err.println("/ONFALSE (inside " + part + ")");
}
@@ -1048,47 +1051,15 @@ public class Graph {
if ((loopBodyStart != null) && ((ti = checkLoop(loopBodyStart, stopPart, loops)) != null)) {
loopBody.add(ti);
} else {
if (!(doWhile && (loopBodyStart == null))) {
loopBody = printGraph(localData, stack, allParts, part, loopBodyStart != null ? loopBodyStart : part.nextParts.get(reversed ? 1 : 0), stopPart, loops, forFinalCommands);
if (!(doWhile && (loopBodyStart == null))) {
loopBody = printGraph(prepareBranchLocalData(localData), stack, allParts, part, loopBodyStart != null ? loopBodyStart : part.nextParts.get(reversed ? 1 : 0), stopPart, loops, forFinalCommands);
}
}
checkContinueAtTheEnd(loopBody, currentLoop);
finalCommands = forFinalCommands.get(currentLoop);
if (!finalCommands.isEmpty()) {
ret.add(new ForTreeItem(null, currentLoop, new ArrayList<GraphTargetItem>(), expr, finalCommands, loopBody));
} /*else if ((expr instanceof HasNextTreeItem) && ((HasNextTreeItem) expr).collection.getNotCoerced().getThroughRegister() instanceof FilteredCheckTreeItem) {
TreeItem gti = ((HasNextTreeItem) expr).collection.getNotCoerced().getThroughRegister();
boolean found = false;
if ((loopBody.size() == 3) || (loopBody.size() == 4)) {
TreeItem ft = loopBody.get(0);
if (ft instanceof WithTreeItem) {
ft = loopBody.get(1);
if (ft instanceof IfItem) {
IfItem ift = (IfItem) ft;
if (ift.onTrue.size() > 0) {
ft = ift.onTrue.get(0);
if (ft instanceof SetPropertyTreeItem) {
SetPropertyTreeItem spt = (SetPropertyTreeItem) ft;
if (spt.object instanceof LocalRegTreeItem) {
int regIndex = ((LocalRegTreeItem) spt.object).regIndex;
HasNextTreeItem iti = (HasNextTreeItem) expr;
localRegs.put(regIndex, new FilterTreeItem(null, iti.collection.getThroughRegister(), ift.expression));
}
}
}
}
}
}
} else if ((expr instanceof HasNextTreeItem) && (!loopBody.isEmpty()) && (loopBody.get(0) instanceof SetTypeTreeItem) && (((SetTypeTreeItem) loopBody.get(0)).getValue().getNotCoerced() instanceof NextValueTreeItem)) {
TreeItem obj = ((SetTypeTreeItem) loopBody.get(0)).getObject();
loopBody.remove(0);
ret.add(new ForEachInTreeItem(null, currentLoop.id, part.start, new InTreeItem(expr.instruction, obj, ((HasNextTreeItem) expr).collection), loopBody));
} else if ((expr instanceof HasNextTreeItem) && (!loopBody.isEmpty()) && (loopBody.get(0) instanceof SetTypeTreeItem) && (((SetTypeTreeItem) loopBody.get(0)).getValue().getNotCoerced() instanceof NextNameTreeItem)) {
TreeItem obj = ((SetTypeTreeItem) loopBody.get(0)).getObject();
loopBody.remove(0);
ret.add(new ForInTreeItem(null, currentLoop.id, part.start, new InTreeItem(expr.instruction, obj, ((HasNextTreeItem) expr).collection), loopBody));
}*/ else {
} else {
if (doWhile) {
if (stack.isEmpty() || (part.nextParts.size() == 1)) {
expr = new TrueItem(null);
@@ -1097,7 +1068,7 @@ public class Graph {
}
loopBody.addAll(0, output);
if (part.nextParts.size() == 1) {
loopBody.addAll(printGraph(localData, stack, allParts, part, part.nextParts.get(0), stopPart, loops, forFinalCommands));
loopBody.addAll(printGraph(prepareBranchLocalData(localData), stack, allParts, part, part.nextParts.get(0), stopPart, loops, forFinalCommands));
}
checkContinueAtTheEnd(loopBody, currentLoop);
@@ -1509,4 +1480,8 @@ public class Graph {
}
return ret.toString();
}
public List prepareBranchLocalData(List localData){
return localData;
}
}

View File

@@ -69,7 +69,7 @@ public abstract class GraphTargetItem {
@Override
public String toString() {
return toString(new ArrayList());
return this.getClass().getName();
}
public abstract String toString(List localData);