2012-02-03 19:43:24 +08:00
|
|
|
|
/*
|
2014-03-12 05:15:43 +08:00
|
|
|
|
Copyright (C) 2011-2014 de4dot@gmail.com
|
2012-02-03 19:43:24 +08:00
|
|
|
|
|
|
|
|
|
This file is part of de4dot.
|
|
|
|
|
|
|
|
|
|
de4dot is free software: you can redistribute it and/or modify
|
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
|
|
de4dot 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 General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with de4dot. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
using System.Collections.Generic;
|
2012-12-20 09:06:09 +08:00
|
|
|
|
using dnlib.DotNet;
|
|
|
|
|
using dnlib.DotNet.Emit;
|
2012-02-03 19:43:24 +08:00
|
|
|
|
using de4dot.blocks;
|
|
|
|
|
|
|
|
|
|
namespace de4dot.code.deobfuscators.Spices_Net {
|
|
|
|
|
class ResourceNamesRestorer {
|
2012-11-15 02:29:29 +08:00
|
|
|
|
ModuleDefMD module;
|
2012-11-02 22:57:11 +08:00
|
|
|
|
TypeDef resourceManagerType;
|
|
|
|
|
TypeDef componentResourceManagerType;
|
2012-11-22 16:14:51 +08:00
|
|
|
|
MethodDefAndDeclaringTypeDict<IMethod> resourceManagerCtors = new MethodDefAndDeclaringTypeDict<IMethod>();
|
|
|
|
|
MethodDefAndDeclaringTypeDict<IMethod> componentManagerCtors = new MethodDefAndDeclaringTypeDict<IMethod>();
|
2013-11-25 12:45:34 +08:00
|
|
|
|
Dictionary<TypeDef, bool> callsResourceManager = new Dictionary<TypeDef, bool>();
|
2012-02-03 19:43:24 +08:00
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
public TypeDef ResourceManagerType {
|
2012-02-03 19:43:24 +08:00
|
|
|
|
get { return resourceManagerType; }
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
public TypeDef ComponentResourceManagerType {
|
2012-02-03 19:43:24 +08:00
|
|
|
|
get { return componentResourceManagerType; }
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-15 02:29:29 +08:00
|
|
|
|
public ResourceNamesRestorer(ModuleDefMD module) {
|
2012-02-03 19:43:24 +08:00
|
|
|
|
this.module = module;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public void Find() {
|
2012-02-03 19:43:24 +08:00
|
|
|
|
foreach (var type in module.Types) {
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (IsResourceType(type, "System.Resources.ResourceManager"))
|
2012-02-03 19:43:24 +08:00
|
|
|
|
resourceManagerType = type;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
else if (IsResourceType(type, "System.ComponentModel.ComponentResourceManager"))
|
2012-02-03 19:43:24 +08:00
|
|
|
|
componentResourceManagerType = type;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
InitializeCtors(resourceManagerType, resourceManagerCtors);
|
|
|
|
|
InitializeCtors(componentResourceManagerType, componentManagerCtors);
|
2012-02-03 19:43:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
void InitializeCtors(TypeDef manager, MethodDefAndDeclaringTypeDict<IMethod> ctors) {
|
2012-02-03 19:43:24 +08:00
|
|
|
|
if (manager == null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
foreach (var ctor in manager.Methods) {
|
|
|
|
|
if (ctor.Name != ".ctor")
|
|
|
|
|
continue;
|
|
|
|
|
|
2012-11-15 02:29:29 +08:00
|
|
|
|
var newCtor = new MemberRefUser(module, ctor.Name, ctor.MethodSig.Clone(), manager.BaseType);
|
|
|
|
|
module.UpdateRowId(newCtor);
|
2013-01-19 20:03:57 +08:00
|
|
|
|
ctors.Add(ctor, newCtor);
|
2012-02-03 19:43:24 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
static bool IsResourceType(TypeDef type, string baseTypeName) {
|
2012-02-03 19:43:24 +08:00
|
|
|
|
if (type.BaseType == null || type.BaseType.FullName != baseTypeName)
|
|
|
|
|
return false;
|
|
|
|
|
if (type.HasProperties || type.HasEvents || type.HasFields)
|
|
|
|
|
return false;
|
2012-11-17 06:50:52 +08:00
|
|
|
|
if (type.Interfaces.Count > 0)
|
2012-02-03 19:43:24 +08:00
|
|
|
|
return false;
|
2012-11-15 02:29:29 +08:00
|
|
|
|
var method = type.FindMethod("GetResourceFileName");
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (!DotNetUtils.IsMethod(method, "System.String", "(System.Globalization.CultureInfo)"))
|
2012-02-03 19:43:24 +08:00
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public void RenameResources() {
|
2012-02-03 19:43:24 +08:00
|
|
|
|
if (resourceManagerType == null && componentResourceManagerType == null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
var numToResource = new Dictionary<uint, Resource>(module.Resources.Count);
|
|
|
|
|
foreach (var resource in module.Resources) {
|
2012-11-15 02:29:29 +08:00
|
|
|
|
var name = resource.Name.String;
|
2012-02-03 19:43:24 +08:00
|
|
|
|
int index = name.LastIndexOf('.');
|
|
|
|
|
string ext;
|
|
|
|
|
if (index < 0)
|
|
|
|
|
ext = name;
|
|
|
|
|
else
|
|
|
|
|
ext = name.Substring(index + 1);
|
|
|
|
|
uint extNum;
|
|
|
|
|
if (!uint.TryParse(ext, out extNum))
|
|
|
|
|
continue;
|
|
|
|
|
numToResource[extNum] = resource;
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-25 12:45:34 +08:00
|
|
|
|
if (module.Assembly != null)
|
|
|
|
|
Rename(numToResource, "", module.Assembly.Name + ".g");
|
|
|
|
|
|
|
|
|
|
foreach (var type in callsResourceManager.Keys)
|
|
|
|
|
Rename(numToResource, type);
|
|
|
|
|
|
|
|
|
|
if (numToResource.Count != 0) {
|
|
|
|
|
foreach (var type in module.GetTypes()) {
|
|
|
|
|
if (numToResource.Count == 0)
|
|
|
|
|
break;
|
|
|
|
|
if (!IsWinFormType(type))
|
|
|
|
|
continue;
|
|
|
|
|
Rename(numToResource, type);
|
|
|
|
|
}
|
2012-02-03 19:43:24 +08:00
|
|
|
|
}
|
2012-03-18 05:12:51 +08:00
|
|
|
|
|
2013-11-25 12:45:34 +08:00
|
|
|
|
if (numToResource.Count != 0) {
|
|
|
|
|
foreach (var type in module.GetTypes()) {
|
|
|
|
|
if (numToResource.Count == 0)
|
|
|
|
|
break;
|
|
|
|
|
Rename(numToResource, type);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (numToResource.Count != 0)
|
|
|
|
|
Logger.e("Couldn't restore all renamed resource names");
|
2012-02-03 19:43:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-11-25 12:45:34 +08:00
|
|
|
|
static bool IsWinFormType(TypeDef type) {
|
|
|
|
|
for (int i = 0; i < 100; i++) {
|
|
|
|
|
var baseType = type.BaseType;
|
|
|
|
|
if (baseType == null)
|
|
|
|
|
break;
|
|
|
|
|
if (baseType.FullName == "System.Object" ||
|
|
|
|
|
baseType.FullName == "System.ValueType")
|
|
|
|
|
return false;
|
|
|
|
|
// Speed up common cases
|
|
|
|
|
if (baseType.FullName == "System.Windows.Forms.Control" ||
|
|
|
|
|
baseType.FullName == "System.Windows.Forms.Form" ||
|
|
|
|
|
baseType.FullName == "System.Windows.Forms.UserControl")
|
|
|
|
|
return true;
|
|
|
|
|
var resolvedBaseType = baseType.ResolveTypeDef();
|
|
|
|
|
if (resolvedBaseType == null)
|
|
|
|
|
break;
|
|
|
|
|
type = resolvedBaseType;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool Rename(Dictionary<uint, Resource> numToResource, TypeDef type) {
|
|
|
|
|
return Rename(numToResource, "", type.FullName) ||
|
|
|
|
|
Rename(numToResource, "", type.FullName + ".g") ||
|
|
|
|
|
Rename(numToResource, type.Namespace, type.Name) ||
|
|
|
|
|
Rename(numToResource, type.Namespace, type.Name + ".g");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool Rename(Dictionary<uint, Resource> numToResource, string ns, string name) {
|
2012-02-03 19:43:24 +08:00
|
|
|
|
var resourceName = name + ".resources";
|
2013-01-19 20:03:57 +08:00
|
|
|
|
uint hash = GetResourceHash(resourceName);
|
2012-02-03 19:43:24 +08:00
|
|
|
|
Resource resource;
|
|
|
|
|
if (!numToResource.TryGetValue(hash, out resource))
|
2013-11-25 12:45:34 +08:00
|
|
|
|
return false;
|
2012-02-03 19:43:24 +08:00
|
|
|
|
|
2012-11-15 02:29:29 +08:00
|
|
|
|
int index = resource.Name.String.LastIndexOf('.');
|
2012-02-03 19:43:24 +08:00
|
|
|
|
string resourceNamespace, newName;
|
|
|
|
|
if (index < 0) {
|
|
|
|
|
resourceNamespace = "";
|
|
|
|
|
newName = resourceName;
|
|
|
|
|
}
|
|
|
|
|
else {
|
2012-11-15 02:29:29 +08:00
|
|
|
|
resourceNamespace = resource.Name.String.Substring(0, index);
|
2012-02-03 19:43:24 +08:00
|
|
|
|
newName = resourceNamespace + "." + resourceName;
|
|
|
|
|
}
|
|
|
|
|
if (resourceNamespace != ns)
|
2013-11-25 12:45:34 +08:00
|
|
|
|
return false;
|
2012-02-03 19:43:24 +08:00
|
|
|
|
|
2012-11-11 12:31:11 +08:00
|
|
|
|
Logger.v("Restoring resource name: '{0}' => '{1}'",
|
2013-01-19 20:03:57 +08:00
|
|
|
|
Utils.RemoveNewlines(resource.Name),
|
|
|
|
|
Utils.RemoveNewlines(newName));
|
2012-12-16 07:03:56 +08:00
|
|
|
|
resource.Name = newName;
|
2012-02-03 19:43:24 +08:00
|
|
|
|
numToResource.Remove(hash);
|
2013-11-25 12:45:34 +08:00
|
|
|
|
return true;
|
2012-02-03 19:43:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
static uint GetResourceHash(string name) {
|
2012-02-03 19:43:24 +08:00
|
|
|
|
uint hash = 0;
|
|
|
|
|
foreach (var c in name)
|
2013-01-19 20:03:57 +08:00
|
|
|
|
hash = Ror(hash ^ c, 1);
|
2012-02-03 19:43:24 +08:00
|
|
|
|
return hash;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
static uint Ror(uint val, int n) {
|
2012-02-03 19:43:24 +08:00
|
|
|
|
return (val << (32 - n)) + (val >> n);
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public void Deobfuscate(Blocks blocks) {
|
2012-11-15 02:29:29 +08:00
|
|
|
|
if (resourceManagerType == null && componentResourceManagerType == null)
|
|
|
|
|
return;
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
foreach (var block in blocks.MethodBlocks.GetAllBlocks()) {
|
2012-02-03 19:43:24 +08:00
|
|
|
|
var instrs = block.Instructions;
|
|
|
|
|
for (int i = 0; i < instrs.Count; i++) {
|
|
|
|
|
var instr = instrs[i];
|
|
|
|
|
if (instr.OpCode.Code != Code.Newobj)
|
|
|
|
|
continue;
|
2012-11-15 02:29:29 +08:00
|
|
|
|
var ctor = instr.Operand as IMethod;
|
2012-02-03 19:43:24 +08:00
|
|
|
|
if (ctor == null)
|
|
|
|
|
continue;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
var newCtor = resourceManagerCtors.Find(ctor);
|
2012-02-03 19:43:24 +08:00
|
|
|
|
if (newCtor == null)
|
2013-01-19 20:03:57 +08:00
|
|
|
|
newCtor = componentManagerCtors.Find(ctor);
|
2012-02-03 19:43:24 +08:00
|
|
|
|
if (newCtor == null)
|
|
|
|
|
continue;
|
|
|
|
|
instr.Operand = newCtor;
|
2013-11-25 12:45:34 +08:00
|
|
|
|
callsResourceManager[blocks.Method.DeclaringType] = true;
|
2012-02-03 19:43:24 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|