2011-12-27 03:32:42 +08:00
|
|
|
|
/*
|
2013-01-02 00:03:16 +08:00
|
|
|
|
Copyright (C) 2011-2013 de4dot@gmail.com
|
2011-12-27 03:32:42 +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;
|
2011-12-27 03:32:42 +08:00
|
|
|
|
using de4dot.blocks;
|
|
|
|
|
|
|
|
|
|
namespace de4dot.code.deobfuscators.dotNET_Reactor.v3 {
|
|
|
|
|
// Find the assembly resolver that's used in lib mode (3.8+)
|
|
|
|
|
class LibAssemblyResolver {
|
2012-11-17 22:46:02 +08:00
|
|
|
|
ModuleDefMD module;
|
2012-11-02 22:57:11 +08:00
|
|
|
|
MethodDef initMethod;
|
2011-12-27 03:32:42 +08:00
|
|
|
|
List<EmbeddedResource> resources = new List<EmbeddedResource>();
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
public TypeDef Type {
|
2011-12-27 03:32:42 +08:00
|
|
|
|
get { return initMethod == null ? null : initMethod.DeclaringType; }
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
public MethodDef InitMethod {
|
2011-12-27 03:32:42 +08:00
|
|
|
|
get { return initMethod; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IEnumerable<EmbeddedResource> Resources {
|
|
|
|
|
get { return resources; }
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-17 22:46:02 +08:00
|
|
|
|
public LibAssemblyResolver(ModuleDefMD module) {
|
2011-12-27 03:32:42 +08:00
|
|
|
|
this.module = module;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public void Find(ISimpleDeobfuscator simpleDeobfuscator, IDeobfuscator deob) {
|
|
|
|
|
if (CheckInitMethod(DotNetUtils.GetModuleTypeCctor(module), simpleDeobfuscator, deob))
|
2011-12-27 03:32:42 +08:00
|
|
|
|
return;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (CheckInitMethod(module.EntryPoint, simpleDeobfuscator, deob))
|
2011-12-27 03:32:42 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
bool CheckInitMethod(MethodDef checkMethod, ISimpleDeobfuscator simpleDeobfuscator, IDeobfuscator deob) {
|
2011-12-27 03:32:42 +08:00
|
|
|
|
var requiredFields = new string[] {
|
|
|
|
|
"System.Collections.Hashtable",
|
|
|
|
|
"System.Boolean",
|
|
|
|
|
};
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
foreach (var method in DotNetUtils.GetCalledMethods(module, checkMethod)) {
|
2011-12-27 03:32:42 +08:00
|
|
|
|
if (method.Body == null)
|
|
|
|
|
continue;
|
|
|
|
|
if (!method.IsStatic)
|
|
|
|
|
continue;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (!DotNetUtils.IsMethod(method, "System.Void", "()"))
|
2011-12-27 03:32:42 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
var type = method.DeclaringType;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (!new FieldTypes(type).Exactly(requiredFields))
|
2011-12-27 03:32:42 +08:00
|
|
|
|
continue;
|
2012-11-17 22:46:02 +08:00
|
|
|
|
var ctor = type.FindMethod(".ctor");
|
2011-12-27 03:32:42 +08:00
|
|
|
|
if (ctor == null)
|
|
|
|
|
continue;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
var handler = DeobUtils.GetResolveMethod(ctor);
|
2011-12-27 03:32:42 +08:00
|
|
|
|
if (handler == null)
|
|
|
|
|
continue;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
simpleDeobfuscator.DecryptStrings(handler, deob);
|
|
|
|
|
var resourcePrefix = GetResourcePrefix(handler);
|
2011-12-27 03:32:42 +08:00
|
|
|
|
if (resourcePrefix == null)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
for (int i = 0; ; i++) {
|
2013-01-19 20:03:57 +08:00
|
|
|
|
var resource = DotNetUtils.GetResource(module, resourcePrefix + i.ToString("D5")) as EmbeddedResource;
|
2011-12-27 03:32:42 +08:00
|
|
|
|
if (resource == null)
|
|
|
|
|
break;
|
|
|
|
|
resources.Add(resource);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
initMethod = method;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
string GetResourcePrefix(MethodDef handler) {
|
|
|
|
|
foreach (var s in DotNetUtils.GetCodeStrings(handler)) {
|
|
|
|
|
var resource = DotNetUtils.GetResource(module, s + "00000") as EmbeddedResource;
|
2011-12-27 03:32:42 +08:00
|
|
|
|
if (resource != null)
|
|
|
|
|
return s;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|