Unverified Commit a01c379c authored by skylot's avatar skylot Committed by GitHub

Merge pull request #171 from daramos/deobfuscation_work

deobfuscation fixes
parents c9b781d5 bf3863d1
...@@ -339,6 +339,10 @@ public class ClassGen { ...@@ -339,6 +339,10 @@ public class ClassGen {
continue; continue;
} }
annotationGen.addForField(code, f); annotationGen.addForField(code, f);
if(f.getFieldInfo().isRenamed()) {
code.startLine("/* renamed from: ").add(f.getName()).add(" */");
}
code.startLine(f.getAccessFlags().makeString()); code.startLine(f.getAccessFlags().makeString());
useType(code, f.getType()); useType(code, f.getType());
code.add(' '); code.add(' ');
...@@ -586,9 +590,8 @@ public class ClassGen { ...@@ -586,9 +590,8 @@ public class ClassGen {
private void insertRenameInfo(CodeWriter code, ClassNode cls) { private void insertRenameInfo(CodeWriter code, ClassNode cls) {
ClassInfo classInfo = cls.getClassInfo(); ClassInfo classInfo = cls.getClassInfo();
if (classInfo.isRenamed() if (classInfo.isRenamed()) {
&& !cls.getShortName().equals(cls.getAlias().getShortName())) { code.startLine("/* renamed from: ").add(classInfo.getType().getObject()).add(" */");
code.startLine("/* renamed from: ").add(classInfo.getFullName()).add(" */");
} }
} }
......
...@@ -80,6 +80,10 @@ public class MethodGen { ...@@ -80,6 +80,10 @@ public class MethodGen {
if (clsAccFlags.isAnnotation()) { if (clsAccFlags.isAnnotation()) {
ai = ai.remove(AccessFlags.ACC_PUBLIC); ai = ai.remove(AccessFlags.ACC_PUBLIC);
} }
if(mth.getMethodInfo().isRenamed()) {
code.startLine("/* renamed from: ").add(mth.getName()).add(" */");
}
code.startLineWithNum(mth.getSourceLine()); code.startLineWithNum(mth.getSourceLine());
code.add(ai.makeString()); code.add(ai.makeString());
......
...@@ -164,6 +164,7 @@ public class NameGen { ...@@ -164,6 +164,7 @@ public class NameGen {
if (vName != null) { if (vName != null) {
return vName; return vName;
} }
return StringUtils.escape(shortName.toLowerCase());
} }
return StringUtils.escape(type.toString()); return StringUtils.escape(type.toString());
} }
......
...@@ -16,7 +16,6 @@ import java.io.File; ...@@ -16,7 +16,6 @@ import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
...@@ -111,22 +110,23 @@ public class Deobfuscator { ...@@ -111,22 +110,23 @@ public class Deobfuscator {
private void postProcess() { private void postProcess() {
int id = 1; int id = 1;
for (OverridedMethodsNode o : ovrd) { for (OverridedMethodsNode o : ovrd) {
boolean aliasFromPreset = false;
Iterator<MethodInfo> it = o.getMethods().iterator(); String aliasToUse = null;
if (it.hasNext()) { for(MethodInfo mth : o.getMethods()){
MethodInfo mth = it.next(); if(mth.isAliasFromPreset()) {
aliasToUse = mth.getAlias();
if (mth.isRenamed() && !mth.isAliasFromPreset()) { aliasFromPreset = true;
mth.setAlias(String.format("mo%d%s", id, makeName(mth.getName())));
} }
String firstMethodAlias = mth.getAlias(); }
for(MethodInfo mth : o.getMethods()){
while (it.hasNext()) { if(aliasToUse == null) {
mth = it.next(); if (mth.isRenamed() && !mth.isAliasFromPreset()) {
if (!mth.getAlias().equals(firstMethodAlias)) { mth.setAlias(String.format("mo%d%s", id, makeName(mth.getName())));
mth.setAlias(firstMethodAlias);
} }
aliasToUse = mth.getAlias();
} }
mth.setAlias(aliasToUse);
mth.setAliasFromPreset(aliasFromPreset);
} }
id++; id++;
......
...@@ -35,12 +35,12 @@ public final class ClassInfo { ...@@ -35,12 +35,12 @@ public final class ClassInfo {
if (type.isArray()) { if (type.isArray()) {
type = ArgType.OBJECT; type = ArgType.OBJECT;
} }
ClassInfo cls = dex.getInfoStorage().getCls(type); ClassInfo cls = dex.root().getInfoStorage().getCls(type);
if (cls != null) { if (cls != null) {
return cls; return cls;
} }
cls = new ClassInfo(dex, type); cls = new ClassInfo(dex, type);
return dex.getInfoStorage().putCls(cls); return dex.root().getInfoStorage().putCls(cls);
} }
public static ClassInfo fromDex(DexNode dex, int clsIndex) { public static ClassInfo fromDex(DexNode dex, int clsIndex) {
...@@ -89,6 +89,10 @@ public final class ClassInfo { ...@@ -89,6 +89,10 @@ public final class ClassInfo {
int sep = clsName.lastIndexOf('$'); int sep = clsName.lastIndexOf('$');
if (canBeInner && sep > 0 && sep != clsName.length() - 1) { if (canBeInner && sep > 0 && sep != clsName.length() - 1) {
String parClsName = pkg + "." + clsName.substring(0, sep); String parClsName = pkg + "." + clsName.substring(0, sep);
if(pkg.length() == 0) {
parClsName = clsName.substring(0, sep);
}
parentClass = fromName(dex, parClsName); parentClass = fromName(dex, parClsName);
clsName = clsName.substring(sep + 1); clsName = clsName.substring(sep + 1);
} else { } else {
......
...@@ -22,7 +22,7 @@ public final class FieldInfo { ...@@ -22,7 +22,7 @@ public final class FieldInfo {
public static FieldInfo from(DexNode dex, ClassInfo declClass, String name, ArgType type) { public static FieldInfo from(DexNode dex, ClassInfo declClass, String name, ArgType type) {
FieldInfo field = new FieldInfo(declClass, name, type); FieldInfo field = new FieldInfo(declClass, name, type);
return dex.getInfoStorage().getField(field); return dex.root().getInfoStorage().getField(field);
} }
public static FieldInfo fromDex(DexNode dex, int index) { public static FieldInfo fromDex(DexNode dex, int index) {
......
package jadx.core.dex.info; package jadx.core.dex.info;
import jadx.core.dex.instructions.args.ArgType; import jadx.core.dex.instructions.args.ArgType;
import jadx.core.dex.nodes.DexNode;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
...@@ -22,13 +23,17 @@ public class InfoStorage { ...@@ -22,13 +23,17 @@ public class InfoStorage {
} }
} }
public MethodInfo getMethod(int mtdId) { private int generateMethodLookupId(DexNode dex, int mthId) {
return methods.get(mtdId); return (dex.getDexId()<<16)|mthId;
} }
public MethodInfo putMethod(int mthId, MethodInfo mth) { public MethodInfo getMethod(DexNode dex, int mtdId) {
return methods.get(generateMethodLookupId(dex,mtdId));
}
public MethodInfo putMethod(DexNode dex, int mthId, MethodInfo mth) {
synchronized (methods) { synchronized (methods) {
MethodInfo prev = methods.put(mthId, mth); MethodInfo prev = methods.put(generateMethodLookupId(dex,mthId), mth);
return prev == null ? mth : prev; return prev == null ? mth : prev;
} }
} }
......
...@@ -34,12 +34,12 @@ public final class MethodInfo { ...@@ -34,12 +34,12 @@ public final class MethodInfo {
} }
public static MethodInfo fromDex(DexNode dex, int mthIndex) { public static MethodInfo fromDex(DexNode dex, int mthIndex) {
MethodInfo mth = dex.getInfoStorage().getMethod(mthIndex); MethodInfo mth = dex.root().getInfoStorage().getMethod(dex, mthIndex);
if (mth != null) { if (mth != null) {
return mth; return mth;
} }
mth = new MethodInfo(dex, mthIndex); mth = new MethodInfo(dex, mthIndex);
return dex.getInfoStorage().putMethod(mthIndex, mth); return dex.root().getInfoStorage().putMethod(dex, mthIndex, mth);
} }
public String makeSignature(boolean includeRetType) { public String makeSignature(boolean includeRetType) {
......
...@@ -35,16 +35,16 @@ public class DexNode implements IDexNode { ...@@ -35,16 +35,16 @@ public class DexNode implements IDexNode {
private final RootNode root; private final RootNode root;
private final Dex dexBuf; private final Dex dexBuf;
private final DexFile file; private final DexFile file;
private final int dexId;
private final List<ClassNode> classes = new ArrayList<ClassNode>(); private final List<ClassNode> classes = new ArrayList<ClassNode>();
private final Map<ClassInfo, ClassNode> clsMap = new HashMap<ClassInfo, ClassNode>(); private final Map<ClassInfo, ClassNode> clsMap = new HashMap<ClassInfo, ClassNode>();
private final InfoStorage infoStorage = new InfoStorage(); public DexNode(RootNode root, DexFile input, int dexId) {
public DexNode(RootNode root, DexFile input) {
this.root = root; this.root = root;
this.file = input; this.file = input;
this.dexBuf = input.getDexBuf(); this.dexBuf = input.getDexBuf();
this.dexId = dexId;
} }
public void loadClasses() throws DecodeException { public void loadClasses() throws DecodeException {
...@@ -153,10 +153,6 @@ public class DexNode implements IDexNode { ...@@ -153,10 +153,6 @@ public class DexNode implements IDexNode {
return null; return null;
} }
public InfoStorage getInfoStorage() {
return infoStorage;
}
public DexFile getDexFile() { public DexFile getDexFile() {
return file; return file;
} }
...@@ -214,6 +210,10 @@ public class DexNode implements IDexNode { ...@@ -214,6 +210,10 @@ public class DexNode implements IDexNode {
return this; return this;
} }
public int getDexId() {
return dexId;
}
@Override @Override
public String toString() { public String toString() {
return "DEX"; return "DEX";
......
...@@ -7,6 +7,7 @@ import jadx.api.ResourcesLoader; ...@@ -7,6 +7,7 @@ import jadx.api.ResourcesLoader;
import jadx.core.clsp.ClspGraph; import jadx.core.clsp.ClspGraph;
import jadx.core.dex.info.ClassInfo; import jadx.core.dex.info.ClassInfo;
import jadx.core.dex.info.ConstStorage; import jadx.core.dex.info.ConstStorage;
import jadx.core.dex.info.InfoStorage;
import jadx.core.utils.ErrorsCounter; import jadx.core.utils.ErrorsCounter;
import jadx.core.utils.StringUtils; import jadx.core.utils.StringUtils;
import jadx.core.utils.android.AndroidResourcesUtils; import jadx.core.utils.android.AndroidResourcesUtils;
...@@ -34,6 +35,7 @@ public class RootNode { ...@@ -34,6 +35,7 @@ public class RootNode {
private final IJadxArgs args; private final IJadxArgs args;
private final StringUtils stringUtils; private final StringUtils stringUtils;
private final ConstStorage constValues; private final ConstStorage constValues;
private final InfoStorage infoStorage = new InfoStorage();
private List<DexNode> dexNodes; private List<DexNode> dexNodes;
@Nullable @Nullable
...@@ -53,7 +55,7 @@ public class RootNode { ...@@ -53,7 +55,7 @@ public class RootNode {
for (DexFile dexFile : input.getDexFiles()) { for (DexFile dexFile : input.getDexFiles()) {
try { try {
LOG.debug("Load: {}", dexFile); LOG.debug("Load: {}", dexFile);
DexNode dexNode = new DexNode(this, dexFile); DexNode dexNode = new DexNode(this, dexFile, dexNodes.size());
dexNodes.add(dexNode); dexNodes.add(dexNode);
} catch (Exception e) { } catch (Exception e) {
throw new DecodeException("Error decode file: " + dexFile, e); throw new DecodeException("Error decode file: " + dexFile, e);
...@@ -197,4 +199,9 @@ public class RootNode { ...@@ -197,4 +199,9 @@ public class RootNode {
public ConstStorage getConstValues() { public ConstStorage getConstValues() {
return constValues; return constValues;
} }
public InfoStorage getInfoStorage() {
return infoStorage;
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment