2011-10-22 20:31:38 +08:00
|
|
|
|
/*
|
2013-01-02 00:03:16 +08:00
|
|
|
|
Copyright (C) 2011-2013 de4dot@gmail.com
|
2011-10-22 20:31:38 +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;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Text;
|
2012-12-20 09:06:09 +08:00
|
|
|
|
using dnlib.DotNet;
|
|
|
|
|
using dnlib.DotNet.Emit;
|
2011-10-22 20:31:38 +08:00
|
|
|
|
using de4dot.blocks;
|
|
|
|
|
|
2011-12-09 16:02:06 +08:00
|
|
|
|
namespace de4dot.code.deobfuscators.CryptoObfuscator {
|
2011-10-22 20:31:38 +08:00
|
|
|
|
class AssemblyResolver {
|
2012-11-09 05:24:13 +08:00
|
|
|
|
ModuleDefMD module;
|
2012-11-02 22:57:11 +08:00
|
|
|
|
TypeDef resolverType;
|
|
|
|
|
MethodDef resolverMethod;
|
2011-10-22 20:55:43 +08:00
|
|
|
|
List<AssemblyInfo> assemblyInfos = new List<AssemblyInfo>();
|
2011-10-22 20:31:38 +08:00
|
|
|
|
|
|
|
|
|
public class AssemblyInfo {
|
|
|
|
|
public string assemblyName;
|
|
|
|
|
public EmbeddedResource resource;
|
|
|
|
|
public EmbeddedResource symbolsResource;
|
|
|
|
|
public AssemblyInfo(string assemblyName, EmbeddedResource resource, EmbeddedResource symbolsResource) {
|
|
|
|
|
this.assemblyName = assemblyName;
|
|
|
|
|
this.resource = resource;
|
|
|
|
|
this.symbolsResource = symbolsResource;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override string ToString() {
|
|
|
|
|
return string.Format("{{{0} => {1}}}", assemblyName, resource.Name);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-21 21:11:14 +08:00
|
|
|
|
public bool Detected {
|
|
|
|
|
get { return resolverType != null; }
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-22 20:31:38 +08:00
|
|
|
|
public List<AssemblyInfo> AssemblyInfos {
|
|
|
|
|
get { return assemblyInfos; }
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
public TypeDef Type {
|
2011-10-23 15:03:00 +08:00
|
|
|
|
get { return resolverType; }
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
public MethodDef Method {
|
2011-10-23 15:03:00 +08:00
|
|
|
|
get { return resolverMethod; }
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-09 05:24:13 +08:00
|
|
|
|
public AssemblyResolver(ModuleDefMD module) {
|
2011-10-22 20:31:38 +08:00
|
|
|
|
this.module = module;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public void Find() {
|
|
|
|
|
var cctor = DotNetUtils.GetModuleTypeCctor(module);
|
2011-10-22 20:31:38 +08:00
|
|
|
|
if (cctor == null)
|
|
|
|
|
return;
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
foreach (var method in DotNetUtils.GetCalledMethods(module, cctor)) {
|
2011-10-22 20:31:38 +08:00
|
|
|
|
if (method.Name == ".cctor" || method.Name == ".ctor")
|
|
|
|
|
continue;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (!method.IsStatic || !DotNetUtils.IsMethod(method, "System.Void", "()"))
|
2011-10-22 20:31:38 +08:00
|
|
|
|
continue;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (CheckType(method.DeclaringType, method))
|
2011-10-22 20:31:38 +08:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
bool CheckType(TypeDef type, MethodDef initMethod) {
|
|
|
|
|
if (DotNetUtils.FindFieldType(type, "System.Collections.Hashtable", true) == null)
|
2011-10-22 20:31:38 +08:00
|
|
|
|
return false;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (!CheckInitMethod(initMethod))
|
2011-10-22 20:31:38 +08:00
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
List<AssemblyInfo> newAssemblyInfos = null;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
foreach (var s in DotNetUtils.GetCodeStrings(initMethod)) {
|
|
|
|
|
newAssemblyInfos = InitializeEmbeddedAssemblies(s);
|
2011-10-22 20:31:38 +08:00
|
|
|
|
if (newAssemblyInfos != null)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (newAssemblyInfos == null)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
resolverType = type;
|
2011-10-23 15:03:00 +08:00
|
|
|
|
resolverMethod = initMethod;
|
2011-10-22 20:31:38 +08:00
|
|
|
|
assemblyInfos = newAssemblyInfos;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
bool CheckInitMethod(MethodDef initMethod) {
|
2011-10-22 20:31:38 +08:00
|
|
|
|
if (!initMethod.HasBody)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
var instructions = initMethod.Body.Instructions;
|
|
|
|
|
for (int i = 0; i < instructions.Count; i++) {
|
2013-01-19 20:03:57 +08:00
|
|
|
|
var instrs = DotNetUtils.GetInstructions(instructions, i, OpCodes.Ldnull, OpCodes.Ldftn, OpCodes.Newobj);
|
2011-10-22 20:31:38 +08:00
|
|
|
|
if (instrs == null)
|
|
|
|
|
continue;
|
|
|
|
|
|
2012-11-09 05:24:13 +08:00
|
|
|
|
IMethod methodRef;
|
2011-10-22 20:31:38 +08:00
|
|
|
|
var ldftn = instrs[1];
|
|
|
|
|
var newobj = instrs[2];
|
|
|
|
|
|
2012-11-09 05:24:13 +08:00
|
|
|
|
methodRef = ldftn.Operand as IMethod;
|
|
|
|
|
if (methodRef == null || !new SigComparer().Equals(initMethod.DeclaringType, methodRef.DeclaringType))
|
2011-10-22 20:31:38 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
2012-11-09 05:24:13 +08:00
|
|
|
|
methodRef = newobj.Operand as IMethod;
|
2011-10-22 20:31:38 +08:00
|
|
|
|
if (methodRef == null || methodRef.FullName != "System.Void System.ResolveEventHandler::.ctor(System.Object,System.IntPtr)")
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
List<AssemblyInfo> InitializeEmbeddedAssemblies(string s) {
|
2011-10-22 20:31:38 +08:00
|
|
|
|
var sb = new StringBuilder(s.Length);
|
|
|
|
|
foreach (var c in s)
|
|
|
|
|
sb.Append((char)~c);
|
|
|
|
|
var tmpAssemblyInfos = sb.ToString().Split(new string[] { "##" }, StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
|
if (tmpAssemblyInfos.Length == 0 || (tmpAssemblyInfos.Length & 1) == 1)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
var newAssemblyInfos = new List<AssemblyInfo>(tmpAssemblyInfos.Length / 2);
|
|
|
|
|
for (int i = 0; i < tmpAssemblyInfos.Length; i += 2) {
|
|
|
|
|
var assemblyName = tmpAssemblyInfos[i];
|
|
|
|
|
var resourceName = tmpAssemblyInfos[i + 1];
|
2013-01-19 20:03:57 +08:00
|
|
|
|
var resource = DotNetUtils.GetResource(module, resourceName) as EmbeddedResource;
|
|
|
|
|
var symbolsResource = DotNetUtils.GetResource(module, resourceName + "#") as EmbeddedResource;
|
2011-10-22 20:31:38 +08:00
|
|
|
|
if (resource == null)
|
|
|
|
|
return null;
|
|
|
|
|
newAssemblyInfos.Add(new AssemblyInfo(assemblyName, resource, symbolsResource));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return newAssemblyInfos;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|