Skip to content

Commit

Permalink
Update detection of <Module> type
Browse files Browse the repository at this point in the history
  • Loading branch information
wtfsck committed Jan 8, 2012
1 parent d295fa2 commit 0398666
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 11 deletions.
6 changes: 2 additions & 4 deletions AssemblyData/methodsrewriter/MModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,9 @@ void initTokenToType() {
var tmpTokenToTypeDefinition = new Dictionary<int, TypeDefinition>();
foreach (var t in module.GetTypes())
tmpTokenToType[t.MetadataToken] = t;
foreach (var t in moduleDefinition.GetTypes()) {
foreach (var t in moduleDefinition.GetTypes())
tmpTokenToTypeDefinition[t.MetadataToken.ToInt32()] = t;
if (moduleType == null && t.FullName == "<Module>")
moduleType = t;
}
moduleType = DotNetUtils.getModuleType(moduleDefinition);
foreach (var token in tmpTokenToType.Keys) {
var mtype = new MType(tmpTokenToType[token], tmpTokenToTypeDefinition[token]);
tokenToType[token] = mtype;
Expand Down
14 changes: 10 additions & 4 deletions blocks/DotNetUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -281,11 +281,17 @@ public static bool isConditionalBranch(Code code) {
}

public static TypeDefinition getModuleType(ModuleDefinition module) {
foreach (var type in module.Types) {
if (type.FullName == "<Module>")
return type;
if (module.Types.Count == 0)
return null;

if (module.Runtime == TargetRuntime.Net_1_0 || module.Runtime == TargetRuntime.Net_1_1) {
if (module.Types[0].FullName == "<Module>")
return module.Types[0];
return null;
}
return null;

// It's always the first one, no matter what it is named.
return module.Types[0];
}

public static bool isEmpty(MethodDefinition method) {
Expand Down
3 changes: 2 additions & 1 deletion de4dot.code/deobfuscators/DeobfuscatorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,9 @@ public virtual void deobfuscateEnd() {
}

void restoreBaseType() {
var moduleType = DotNetUtils.getModuleType(module);
foreach (var type in module.GetTypes()) {
if (type.BaseType != null || type.IsInterface || type.FullName == "<Module>")
if (type.BaseType != null || type.IsInterface || type == moduleType)
continue;
Log.v("Adding System.Object as base type: {0} ({1:X8})",
Utils.removeNewlines(type),
Expand Down
3 changes: 2 additions & 1 deletion de4dot.code/deobfuscators/SmartAssembly/Deobfuscator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,9 @@ bool hasModuleCctor() {

bool hasEmptyClassesInEveryNamespace() {
var namespaces = new Dictionary<string, int>(StringComparer.Ordinal);
var moduleType = DotNetUtils.getModuleType(module);
foreach (var type in module.Types) {
if (type.FullName == "<Module>")
if (type == moduleType)
continue;
var ns = type.Namespace;
if (!namespaces.ContainsKey(ns))
Expand Down
11 changes: 10 additions & 1 deletion de4dot.code/renamer/TypeInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ TypeInfo getBase() {
return baseInfo;
}

bool isModuleType() {
return type.TypeDefinition == DotNetUtils.getModuleType(type.TypeDefinition.Module);
}

public void prepareRenameTypes(TypeRenamerState state) {
var checker = NameChecker;

Expand All @@ -94,7 +98,12 @@ public void prepareRenameTypes(TypeRenamerState state) {
string origClassName = null;
if (isWinFormsClass())
origClassName = findWindowsFormsClassName(type);
if (oldFullName != "<Module>" && !checker.isValidTypeName(oldName)) {
if (isModuleType()) {
if (oldNamespace != "")
newNamespace = "";
rename("<Module>");
}
else if (!checker.isValidTypeName(oldName)) {
if (origClassName != null && checker.isValidTypeName(origClassName))
rename(state.getTypeName(oldName, origClassName));
else {
Expand Down

0 comments on commit 0398666

Please sign in to comment.