AS2 deobfuscation improvement

This commit is contained in:
honfika@gmail.com
2015-11-01 12:12:04 +01:00
parent e248fc117f
commit 56bd4cd1f6
8 changed files with 77 additions and 333 deletions

View File

@@ -88,7 +88,6 @@ import com.jpexs.decompiler.flash.tags.FileAttributesTag;
import com.jpexs.decompiler.flash.tags.JPEGTablesTag;
import com.jpexs.decompiler.flash.tags.MetadataTag;
import com.jpexs.decompiler.flash.tags.ProtectTag;
import com.jpexs.decompiler.flash.tags.SetBackgroundColorTag;
import com.jpexs.decompiler.flash.tags.ShowFrameTag;
import com.jpexs.decompiler.flash.tags.SymbolClassTag;
import com.jpexs.decompiler.flash.tags.Tag;
@@ -126,7 +125,6 @@ import com.jpexs.decompiler.flash.treeitems.TreeItem;
import com.jpexs.decompiler.flash.types.ColorTransform;
import com.jpexs.decompiler.flash.types.MATRIX;
import com.jpexs.decompiler.flash.types.RECT;
import com.jpexs.decompiler.flash.types.RGB;
import com.jpexs.decompiler.flash.types.SHAPE;
import com.jpexs.decompiler.flash.types.annotations.Internal;
import com.jpexs.decompiler.flash.types.filters.BlendComposite;

View File

@@ -234,66 +234,67 @@ public class ActionDeobfuscator implements SWFDecompilerListener {
}
}
int unreachableCount = actions.getUnreachableActionCount(actionItem, result.item);
/*int unreachableCount = result.minSkippedInstructions;
if (unreachableCount <= newIstructionCount && newIstructionCount < result.maxSkippedInstructions) {
unreachableCount = actions.getUnreachableActionCount(actionItem, result.item);
}*/
if (allValueValid && newIstructionCount < unreachableCount) {
if (result.stack.isEmpty() && result.variables.isEmpty() && result.constantPool == null && actionItem.action instanceof ActionJump) {
actionItem.setJumpTarget(result.item);
} else {
ActionItem prevActionItem = actionItem.prev;
if (result.constantPool != null) {
ActionConstantPool constantPool2 = new ActionConstantPool(new ArrayList<>(result.constantPool.constantPool));
ActionItem constantPoolItem = new ActionItem(constantPool2);
iterator.addBefore(constantPoolItem);
}
for (String variableName : result.variables.keySet()) {
Object value = result.variables.get(variableName);
ActionPush push = new ActionPush(variableName);
ActionItem pushItem = new ActionItem(push);
iterator.addBefore(pushItem);
push = new ActionPush(value);
pushItem = new ActionItem(push);
iterator.addBefore(pushItem);
if (result.defines.contains(variableName)) {
ActionDefineLocal defineLocal = new ActionDefineLocal();
ActionItem defineLocalItem = new ActionItem(defineLocal);
iterator.addBefore(defineLocalItem);
} else {
ActionSetVariable setVariable = new ActionSetVariable();
ActionItem setVariableItem = new ActionItem(setVariable);
iterator.addBefore(setVariableItem);
}
}
if (!result.stack.isEmpty()) {
for (Object obj : result.stack) {
ActionPush push = new ActionPush(obj);
ActionItem pushItem = new ActionItem(push);
iterator.addBefore(pushItem);
}
}
ActionJump jump = new ActionJump(0);
ActionItem jumpItem = new ActionItem(jump);
jumpItem.setJumpTarget(result.item);
iterator.addBefore(jumpItem);
actions.replaceJumpTargets(actionItem, prevActionItem.next);
if (allValueValid && newIstructionCount < result.maxSkippedInstructions) {
int unreachableCount = result.minSkippedInstructions;
if (newIstructionCount >= result.minSkippedInstructions) {
unreachableCount = actions.getUnreachableActionCount(actionItem, result.item);
}
ActionItem prevItem = actionItem.prev;
if (newIstructionCount < unreachableCount) {
if (result.stack.isEmpty() && result.variables.isEmpty() && result.constantPool == null && actionItem.action instanceof ActionJump) {
actionItem.setJumpTarget(result.item);
} else {
ActionItem prevActionItem = actionItem.prev;
if (result.constantPool != null) {
ActionConstantPool constantPool2 = new ActionConstantPool(new ArrayList<>(result.constantPool.constantPool));
ActionItem constantPoolItem = new ActionItem(constantPool2);
iterator.addBefore(constantPoolItem);
}
actions.removeUnreachableActions();
actions.removeZeroJumps();
for (String variableName : result.variables.keySet()) {
Object value = result.variables.get(variableName);
ActionPush push = new ActionPush(variableName);
ActionItem pushItem = new ActionItem(push);
iterator.addBefore(pushItem);
push = new ActionPush(value);
pushItem = new ActionItem(push);
iterator.addBefore(pushItem);
iterator.setCurrent(prevItem.next.next);
ret = true;
if (result.defines.contains(variableName)) {
ActionDefineLocal defineLocal = new ActionDefineLocal();
ActionItem defineLocalItem = new ActionItem(defineLocal);
iterator.addBefore(defineLocalItem);
} else {
ActionSetVariable setVariable = new ActionSetVariable();
ActionItem setVariableItem = new ActionItem(setVariable);
iterator.addBefore(setVariableItem);
}
}
if (!result.stack.isEmpty()) {
for (Object obj : result.stack) {
ActionPush push = new ActionPush(obj);
ActionItem pushItem = new ActionItem(push);
iterator.addBefore(pushItem);
}
}
ActionJump jump = new ActionJump(0);
ActionItem jumpItem = new ActionItem(jump);
jumpItem.setJumpTarget(result.item);
iterator.addBefore(jumpItem);
actions.replaceJumpTargets(actionItem, prevActionItem.next);
}
ActionItem prevItem = actionItem.prev;
actions.removeUnreachableActions();
actions.removeZeroJumps();
iterator.setCurrent(prevItem.next.next);
ret = true;
}
}
}
@@ -601,9 +602,9 @@ public class ActionDeobfuscator implements SWFDecompilerListener {
instructionsProcessed++;
if (!jumpFound) {
boolean isJumpTarget = prevItem.isJumpTarget();
boolean isJumpTarget = prevItem.isJumpTarget() || prevItem.prev.isContainerLastAction();
if (isJumpTarget) {
if (prevJumpedHere && prevItem.jumpsHere.size() == 1) {
if (prevJumpedHere && prevItem.jumpsHereSize() == 1) {
isJumpTarget = false;
}
}
@@ -612,12 +613,14 @@ public class ActionDeobfuscator implements SWFDecompilerListener {
jumpFound = true;
} else {
skippedInstructions++;
if (action instanceof ActionIf) {
jumpFound = true;
}
}
if (jumpedHere) {
jumpFound = true;
}
}
}
@@ -647,212 +650,6 @@ public class ActionDeobfuscator implements SWFDecompilerListener {
}
}
private void executeActionsOld(ActionItem item, LocalDataArea localData, ActionConstantPool constantPool, ExecutionResult result, Map<String, Object> fakeFunctions, boolean useVariables, boolean allowGetUninitializedVariables) throws InterruptedException {
FixItemCounterStack stack = (FixItemCounterStack) localData.stack;
int instructionsProcessed = 0;
ActionConstantPool lastConstantPool = null;
while (true) {
if (item.isExcluded()) {
break;
}
if (Thread.currentThread().isInterrupted()) {
throw new InterruptedException();
}
if (instructionsProcessed > executionLimit) {
break;
}
Action action = item.action;
/*System.out.print(action.getASMSource(actions, new ArrayList<Long>(), ScriptExportMode.PCODE));
for (int j = 0; j < stack.size(); j++) {
System.out.print(" '" + stack.get(j).getResult() + "'");
}
System.out.println();*/
if (action instanceof ActionConstantPool) {
lastConstantPool = (ActionConstantPool) action;
}
if (action instanceof ActionDefineLocal) {
if (stack.size() < 2) {
break;
}
String variableName = EcmaScript.toString(stack.peek(2));
result.defines.add(variableName);
}
if (action instanceof ActionGetVariable) {
if (stack.isEmpty()) {
break;
}
String variableName = stack.peek().toString();
if (!localData.localVariables.containsKey(variableName)
&& (!allowGetUninitializedVariables || !isFakeName(variableName))) {
break;
}
}
if (action instanceof ActionSetVariable) {
if (stack.size() < 2) {
break;
}
}
if (action instanceof ActionCallFunction) {
if (stack.size() < 2) {
break;
}
String functionName = stack.pop().toString();
long numArgs = EcmaScript.toUint32(stack.pop());
if (numArgs == 0) {
if (fakeFunctions != null && fakeFunctions.containsKey(functionName)) {
stack.push(fakeFunctions.get(functionName));
} else {
break;
}
} else {
break;
}
} else {
Object o = null;
if (action instanceof ActionIf) {
if (!stack.isEmpty()) {
o = stack.peek();
}
}
if (!action.execute(localData)) {
break;
}
if (o != null) {
stack.push(o);
}
}
if (!useVariables && (action instanceof ActionDefineLocal
|| action instanceof ActionGetVariable
|| action instanceof ActionSetVariable
|| action instanceof ActionConstantPool
|| action instanceof ActionCallFunction
|| action instanceof ActionReturn
|| action instanceof ActionEnd)) {
break;
}
if (!(action instanceof ActionPush
|| action instanceof ActionPushDuplicate
//|| action instanceof ActionPop
|| action instanceof ActionAsciiToChar
|| action instanceof ActionCharToAscii
|| action instanceof ActionDecrement
|| action instanceof ActionIncrement
|| action instanceof ActionNot
|| action instanceof ActionToInteger
|| action instanceof ActionToNumber
|| action instanceof ActionToString
|| action instanceof ActionTypeOf
|| action instanceof ActionStringLength
|| action instanceof ActionMBAsciiToChar
|| action instanceof ActionMBStringLength
|| action instanceof ActionAnd
|| action instanceof ActionAdd
|| action instanceof ActionAdd2
|| action instanceof ActionBitAnd
|| action instanceof ActionBitLShift
|| action instanceof ActionBitOr
|| action instanceof ActionBitRShift
|| action instanceof ActionBitURShift
|| action instanceof ActionBitXor
|| action instanceof ActionDivide
|| action instanceof ActionEquals
|| action instanceof ActionEquals2
|| action instanceof ActionGreater
|| action instanceof ActionLess
|| action instanceof ActionLess2 // todo: fix (tz.swf/frame_6/DoAction: _loc3_.icon.gotoAndStop((Number(item.cost) || 0) >= 0?1:2)
|| action instanceof ActionModulo
|| action instanceof ActionMultiply
|| action instanceof ActionOr
|| action instanceof ActionStringAdd
|| action instanceof ActionStringEquals
|| action instanceof ActionStringGreater
|| action instanceof ActionStringLess
|| action instanceof ActionSubtract
|| action instanceof ActionIf
|| action instanceof ActionJump
|| action instanceof ActionDefineLocal
|| action instanceof ActionGetVariable
|| action instanceof ActionSetVariable
|| action instanceof ActionConstantPool
|| action instanceof ActionCallFunction
|| action instanceof ActionReturn
|| action instanceof ActionEnd)) {
break;
}
if (action instanceof ActionPush) {
ActionPush push = (ActionPush) action;
boolean ok = true;
instructionsProcessed += push.values.size() - 1;
for (Object value : push.values) {
if ((constantPool == null && value instanceof ConstantIndex) || value instanceof RegisterNumber) {
ok = false;
break;
}
}
if (!ok) {
break;
}
}
ActionItem prevItem = item;
if (action instanceof ActionJump) {
item = item.getJumpTarget();
} else if (action instanceof ActionIf) {
if (stack.isEmpty()) {
break;
}
if (EcmaScript.toBoolean(stack.pop())) {
item = item.getJumpTarget();
} else {
item = item.next;
}
} else {
item = item.next;
}
instructionsProcessed++;
if ((stack.allItemsFixed() || action instanceof ActionEnd) && !(action instanceof ActionPush)) {
result.item = item;
result.instructionsProcessed = instructionsProcessed;
result.constantPool = lastConstantPool;
result.variables.clear();
for (String variableName : localData.localVariables.keySet()) {
Object value = localData.localVariables.get(variableName);
result.variables.put(variableName, value);
}
result.stack.clear();
result.stack.addAll(stack);
}
if (action instanceof ActionReturn) {
result.resultValue = localData.returnValue;
break;
} else if (action instanceof ActionEnd) {
result.item = prevItem;
break;
}
}
}
@Override
public void actionTreeCreated(List<GraphTargetItem> tree, SWF swf) {
if (tree.size() > 0) {

View File

@@ -1,59 +0,0 @@
/*
* Copyright (C) 2010-2015 JPEXS, All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3.0 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library.
*/
package com.jpexs.decompiler.flash.action.deobfuscation;
import com.jpexs.decompiler.graph.GraphTargetItem;
import com.jpexs.decompiler.graph.TranslateStack;
/**
*
* @author JPEXS
*/
public class FixItemCounterTranslateStack extends TranslateStack {
private int fixItemCount = Integer.MAX_VALUE;
public FixItemCounterTranslateStack(String path) {
super(null); //null path => do not add PushItems
}
@Override
public GraphTargetItem pop() {
GraphTargetItem result = super.pop();
int itemCount = size();
if (itemCount < fixItemCount) {
fixItemCount = itemCount;
}
return result;
}
@Override
public synchronized GraphTargetItem remove(int index) {
if (index < fixItemCount) {
fixItemCount = index;
}
return super.remove(index);
}
public boolean allItemsFixed() {
return size() <= fixItemCount;
}
public int getFixItemCount() {
return fixItemCount;
}
}

View File

@@ -54,6 +54,10 @@ public class ActionItem {
return jumpsHere != null && !jumpsHere.isEmpty();
}
public int jumpsHereSize() {
return jumpsHere == null ? 0 : jumpsHere.size();
}
public boolean isContainerLastAction() {
return lastActionOf != null && !lastActionOf.isEmpty();
}

View File

@@ -70,6 +70,8 @@ public class Configuration {
public static final Level logLevel;
public static boolean showStat;
@ConfigurationDefaultBoolean(true)
@ConfigurationCategory("ui")
public static final ConfigurationItem<Boolean> openMultipleFiles = null;

View File

@@ -16,6 +16,7 @@
*/
package com.jpexs.helpers.stat;
import com.jpexs.decompiler.flash.configuration.Configuration;
import com.jpexs.helpers.Stopwatch;
import java.util.HashMap;
import java.util.Map;
@@ -94,12 +95,14 @@ public class Statistics implements AutoCloseable {
public Statistics(String name) {
this.name = name;
sw = Stopwatch.startNew();
sw = Configuration.showStat ? Stopwatch.startNew() : null;
}
@Override
public void close() {
sw.stop();
addTime(name, sw.getElapsedNanoseconds());
if (sw != null) {
sw.stop();
addTime(name, sw.getElapsedNanoseconds());
}
}
}