Fixed #1970 AS2 Renaming invalid identifiers for direct strings (no constant indices)

Fixed #1970 AS2 Renaming invalid identifiers IndexOutOfBounds on invalid constant index (obfuscated code, etc.)
This commit is contained in:
Jindra Petřík
2023-02-25 19:39:02 +01:00
parent 5ff41c89dd
commit 851dc7e922
3 changed files with 22 additions and 33 deletions
@@ -2747,6 +2747,7 @@ public final class SWF implements SWFContainerItem, Timelined, Openable {
}
private int renameAS2Identifiers(RenameType renameType, Map<DottedChain, DottedChain> selected) throws InterruptedException {
boolean wrongConstantIndices = false;
HashMap<ASMSource, ActionList> actionsMap = new HashMap<>();
List<GraphSourceItem> allFunctions = new ArrayList<>();
List<MyEntry<DirectValueActionItem, ConstantPool>> allVariableNames = new ArrayList<>();
@@ -2963,7 +2964,11 @@ public final class SWF implements SWFContainerItem, Timelined, Openable {
pool.constants.add(changed);
ci.index = pool.constants.size() - 1;
} else {
pool.constants.set(ci.index, changed);
if (ci.index >= pool.constants.size()) {
wrongConstantIndices = true;
} else {
pool.constants.set(ci.index, changed);
}
}
} else {
pu.replacement.set(it.getKey().pos, changed);
@@ -2983,6 +2988,9 @@ public final class SWF implements SWFContainerItem, Timelined, Openable {
}
deobfuscation.deobfuscateInstanceNames(false, deobfuscated, renameType, getTags(), selected);
if (wrongConstantIndices) {
logger.warning("Cannot properly rename some invalid AS2 identifiers as there exist unresolved constant indices. It might be fixed by turning Deobfuscation on and try to rename identifiers again.");
}
return ret;
}
@@ -128,7 +128,11 @@ public class ActionPush extends Action {
@Override
protected void getContentBytes(SWFOutputStream sos) throws IOException {
for (Object o : values) {
List<Object> vals = values;
if (replacement != null) {
vals = replacement;
}
for (Object o : vals) {
if (o instanceof String) {
sos.writeUI8(0);
sos.writeString((String) o);
@@ -179,8 +183,12 @@ public class ActionPush extends Action {
*/
@Override
protected int getContentBytesLength() {
List<Object> vals = values;
if (replacement != null) {
vals = replacement;
}
int res = 0;
for (Object o : values) {
for (Object o : vals) {
if (o instanceof String) {
res += Utf8Helper.getBytesLength((String) o) + 2;
} else if (o instanceof Float) {
@@ -402,36 +410,7 @@ public class ActionPush extends Action {
} else {
o = constantPool.get(((ConstantIndex) o).index);
}
}
/*if (o instanceof RegisterNumber) {
if (regNames.containsKey(((RegisterNumber) o).number)) {
((RegisterNumber) o).name = regNames.get(((RegisterNumber) o).number);
} else if (output.size() >= 2) { //chained assignments:, ignore for class prototype assignment
GraphTargetItem last = output.get(output.size() - 1);
GraphTargetItem prev = output.get(output.size() - 2);
if (last instanceof SetTypeActionItem) {
if (prev instanceof StoreRegisterActionItem) {
StoreRegisterActionItem str = (StoreRegisterActionItem) prev;
if (str.register.number == ((RegisterNumber) o).number) {
SetTypeActionItem stt = (SetTypeActionItem) last;
stt.setTempRegister(((RegisterNumber) o).number);
if ((stt.getValue() instanceof IncrementActionItem) && (((IncrementActionItem) stt.getValue()).object.equals(stt.getObject()))) {
stack.push(new PreIncrementActionItem(this, lineStartAction, stt.getObject()));
} else if ((stt.getValue() instanceof DecrementActionItem) && (((DecrementActionItem) stt.getValue()).object.equals(stt.getObject()))) {
stack.push(new PreDecrementActionItem(this, lineStartAction, stt.getObject()));
} else {
//stack.push(last);
continue;
}
output.remove(output.size() - 1);
output.remove(output.size() - 1);
pos++;
continue;
}
}
}
}
}*/
}
if (o instanceof Boolean) {
Boolean b = (Boolean) o;
if (b) {