2011-10-22 20:31:38 +08:00
|
|
|
|
/*
|
2012-01-10 06:02:47 +08:00
|
|
|
|
Copyright (C) 2011-2012 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;
|
|
|
|
|
using Mono.Cecil;
|
|
|
|
|
using Mono.Cecil.Cil;
|
|
|
|
|
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 {
|
|
|
|
|
ModuleDefinition module;
|
|
|
|
|
TypeDefinition resolverType;
|
2011-10-23 15:03:00 +08:00
|
|
|
|
MethodDefinition 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; }
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-06 22:24:30 +08:00
|
|
|
|
public TypeDefinition Type {
|
2011-10-23 15:03:00 +08:00
|
|
|
|
get { return resolverType; }
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-06 22:24:30 +08:00
|
|
|
|
public MethodDefinition Method {
|
2011-10-23 15:03:00 +08:00
|
|
|
|
get { return resolverMethod; }
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-22 20:53:24 +08:00
|
|
|
|
public AssemblyResolver(ModuleDefinition module) {
|
2011-10-22 20:31:38 +08:00
|
|
|
|
this.module = module;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void find() {
|
2012-03-15 16:35:44 +08:00
|
|
|
|
var cctor = DotNetUtils.getModuleTypeCctor(module);
|
2011-10-22 20:31:38 +08:00
|
|
|
|
if (cctor == null)
|
|
|
|
|
return;
|
|
|
|
|
|
2012-03-18 03:36:41 +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;
|
|
|
|
|
if (!method.IsStatic || !DotNetUtils.isMethod(method, "System.Void", "()"))
|
|
|
|
|
continue;
|
2012-03-18 03:36:41 +08:00
|
|
|
|
if (checkType(method.DeclaringType, method))
|
2011-10-22 20:31:38 +08:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool checkType(TypeDefinition type, MethodDefinition initMethod) {
|
|
|
|
|
if (DotNetUtils.findFieldType(type, "System.Collections.Hashtable", true) == null)
|
|
|
|
|
return false;
|
|
|
|
|
if (!checkInitMethod(initMethod))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
List<AssemblyInfo> newAssemblyInfos = null;
|
|
|
|
|
foreach (var s in DotNetUtils.getCodeStrings(initMethod)) {
|
|
|
|
|
newAssemblyInfos = initializeEmbeddedAssemblies(s);
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool checkInitMethod(MethodDefinition initMethod) {
|
|
|
|
|
if (!initMethod.HasBody)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
var instructions = initMethod.Body.Instructions;
|
|
|
|
|
for (int i = 0; i < instructions.Count; i++) {
|
|
|
|
|
var instrs = DotNetUtils.getInstructions(instructions, i, OpCodes.Ldnull, OpCodes.Ldftn, OpCodes.Newobj);
|
|
|
|
|
if (instrs == null)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
MethodReference methodRef;
|
|
|
|
|
var ldftn = instrs[1];
|
|
|
|
|
var newobj = instrs[2];
|
|
|
|
|
|
|
|
|
|
methodRef = ldftn.Operand as MethodReference;
|
|
|
|
|
if (methodRef == null || !MemberReferenceHelper.compareTypes(initMethod.DeclaringType, methodRef.DeclaringType))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
methodRef = newobj.Operand as MethodReference;
|
|
|
|
|
if (methodRef == null || methodRef.FullName != "System.Void System.ResolveEventHandler::.ctor(System.Object,System.IntPtr)")
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
List<AssemblyInfo> initializeEmbeddedAssemblies(string s) {
|
|
|
|
|
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];
|
|
|
|
|
var resource = DotNetUtils.getResource(module, resourceName) as EmbeddedResource;
|
|
|
|
|
var symbolsResource = DotNetUtils.getResource(module, resourceName + "#") as EmbeddedResource;
|
|
|
|
|
if (resource == null)
|
|
|
|
|
return null;
|
|
|
|
|
newAssemblyInfos.Add(new AssemblyInfo(assemblyName, resource, symbolsResource));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return newAssemblyInfos;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|