2011-09-22 10:55:30 +08:00
|
|
|
|
/*
|
2014-03-12 05:15:43 +08:00
|
|
|
|
Copyright (C) 2011-2014 de4dot@gmail.com
|
2011-09-22 10:55:30 +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-09-24 16:26:29 +08:00
|
|
|
|
using de4dot.blocks;
|
2011-09-22 10:55:30 +08:00
|
|
|
|
|
2011-12-09 16:02:06 +08:00
|
|
|
|
namespace de4dot.code.deobfuscators.SmartAssembly {
|
2011-09-22 10:55:30 +08:00
|
|
|
|
abstract class ResolverInfoBase {
|
2012-11-19 00:07:02 +08:00
|
|
|
|
protected ModuleDefMD module;
|
2011-09-22 10:55:30 +08:00
|
|
|
|
ISimpleDeobfuscator simpleDeobfuscator;
|
|
|
|
|
IDeobfuscator deob;
|
2012-11-02 22:57:11 +08:00
|
|
|
|
TypeDef resolverType;
|
|
|
|
|
MethodDef callResolverMethod;
|
2011-09-22 10:55:30 +08:00
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
public TypeDef Type {
|
2011-09-22 10:55:30 +08:00
|
|
|
|
get { return resolverType; }
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
public TypeDef CallResolverType {
|
2012-01-17 09:54:48 +08:00
|
|
|
|
get {
|
|
|
|
|
if (callResolverMethod == null)
|
|
|
|
|
return null;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (!HasOnlyThisMethod(callResolverMethod.DeclaringType, callResolverMethod))
|
2012-01-17 09:54:48 +08:00
|
|
|
|
return null;
|
|
|
|
|
return callResolverMethod.DeclaringType;
|
|
|
|
|
}
|
2011-09-22 10:55:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
public MethodDef CallResolverMethod {
|
2011-09-22 10:55:30 +08:00
|
|
|
|
get { return callResolverMethod; }
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-19 00:07:02 +08:00
|
|
|
|
public ResolverInfoBase(ModuleDefMD module, ISimpleDeobfuscator simpleDeobfuscator, IDeobfuscator deob) {
|
2011-09-22 10:55:30 +08:00
|
|
|
|
this.module = module;
|
|
|
|
|
this.simpleDeobfuscator = simpleDeobfuscator;
|
|
|
|
|
this.deob = deob;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public bool FindTypes() {
|
2011-09-22 10:55:30 +08:00
|
|
|
|
if (resolverType != null)
|
|
|
|
|
return true;
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (FindTypes(DotNetUtils.GetModuleTypeCctor(module)))
|
2011-12-22 12:41:28 +08:00
|
|
|
|
return true;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (FindTypes(module.EntryPoint))
|
2011-12-22 12:41:28 +08:00
|
|
|
|
return true;
|
2011-09-22 10:55:30 +08:00
|
|
|
|
|
2011-12-22 12:41:28 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
bool FindTypes(MethodDef initMethod) {
|
2011-12-22 12:41:28 +08:00
|
|
|
|
if (initMethod == null)
|
|
|
|
|
return false;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
foreach (var method in DotNetUtils.GetCalledMethods(module, initMethod)) {
|
2011-09-22 10:55:30 +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-09-22 10:55:30 +08:00
|
|
|
|
continue;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (CheckAttachAppMethod(method))
|
2011-09-22 10:55:30 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
bool CheckAttachAppMethod(MethodDef attachAppMethod) {
|
2012-01-17 09:54:48 +08:00
|
|
|
|
callResolverMethod = null;
|
2011-09-22 10:55:30 +08:00
|
|
|
|
if (!attachAppMethod.HasBody)
|
|
|
|
|
return false;
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
foreach (var method in DotNetUtils.GetCalledMethods(module, attachAppMethod)) {
|
2012-07-07 15:09:55 +08:00
|
|
|
|
if (attachAppMethod == method)
|
|
|
|
|
continue;
|
2011-09-22 10:55:30 +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-09-22 10:55:30 +08:00
|
|
|
|
continue;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (!CheckResolverInitMethod(method))
|
2011-09-22 10:55:30 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
callResolverMethod = attachAppMethod;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (HasLdftn(attachAppMethod)) {
|
|
|
|
|
simpleDeobfuscator.Deobfuscate(attachAppMethod);
|
|
|
|
|
foreach (var resolverHandler in GetResolverHandlers(attachAppMethod)) {
|
2011-12-22 12:41:28 +08:00
|
|
|
|
if (!resolverHandler.HasBody)
|
|
|
|
|
continue;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
var resolverTypeTmp = GetResolverType(resolverHandler);
|
2012-01-17 09:54:48 +08:00
|
|
|
|
if (resolverTypeTmp == null)
|
2011-12-22 12:41:28 +08:00
|
|
|
|
continue;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
Deobfuscate(resolverHandler);
|
|
|
|
|
if (CheckHandlerMethod(resolverHandler)) {
|
2011-12-22 12:41:28 +08:00
|
|
|
|
callResolverMethod = attachAppMethod;
|
2012-01-17 09:54:48 +08:00
|
|
|
|
resolverType = resolverTypeTmp;
|
2011-12-22 12:41:28 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-09-22 10:55:30 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
static bool HasLdftn(MethodDef method) {
|
2011-12-22 12:41:28 +08:00
|
|
|
|
if (method == null || method.Body == null)
|
2011-09-22 10:55:30 +08:00
|
|
|
|
return false;
|
2011-12-22 12:41:28 +08:00
|
|
|
|
foreach (var instr in method.Body.Instructions) {
|
|
|
|
|
if (instr.OpCode.Code == Code.Ldftn)
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
bool CheckResolverInitMethod(MethodDef initMethod) {
|
2011-12-22 12:41:28 +08:00
|
|
|
|
resolverType = null;
|
|
|
|
|
if (!initMethod.HasBody)
|
|
|
|
|
return false;
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
Deobfuscate(initMethod);
|
|
|
|
|
foreach (var handlerDef in GetResolverHandlers(initMethod)) {
|
|
|
|
|
Deobfuscate(handlerDef);
|
2012-01-17 09:54:48 +08:00
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
var resolverTypeTmp = GetResolverType(handlerDef);
|
2012-01-17 09:54:48 +08:00
|
|
|
|
if (resolverTypeTmp == null)
|
|
|
|
|
continue;
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (CheckHandlerMethod(handlerDef)) {
|
2012-01-17 09:54:48 +08:00
|
|
|
|
resolverType = resolverTypeTmp;
|
2011-09-22 10:55:30 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
void Deobfuscate(MethodDef method) {
|
|
|
|
|
simpleDeobfuscator.Deobfuscate(method);
|
|
|
|
|
simpleDeobfuscator.DecryptStrings(method, deob);
|
2011-09-22 10:55:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
TypeDef GetResolverType(MethodDef resolveHandler) {
|
2012-01-17 09:54:48 +08:00
|
|
|
|
if (resolveHandler.Body == null)
|
|
|
|
|
return null;
|
|
|
|
|
foreach (var instr in resolveHandler.Body.Instructions) {
|
|
|
|
|
if (instr.OpCode.Code != Code.Ldsfld && instr.OpCode.Code != Code.Stsfld)
|
|
|
|
|
continue;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
var field = DotNetUtils.GetField(module, instr.Operand as IField);
|
2012-01-17 09:54:48 +08:00
|
|
|
|
if (field == null)
|
|
|
|
|
continue;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (!CheckResolverType(field.DeclaringType))
|
2012-01-17 09:54:48 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
return field.DeclaringType;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (CheckResolverType(resolveHandler.DeclaringType))
|
2012-01-17 09:54:48 +08:00
|
|
|
|
return resolveHandler.DeclaringType;
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
protected abstract bool CheckResolverType(TypeDef type);
|
|
|
|
|
protected abstract bool CheckHandlerMethod(MethodDef handler);
|
2011-09-22 10:55:30 +08:00
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
IEnumerable<MethodDef> GetResolverHandlers(MethodDef method) {
|
2011-09-22 10:55:30 +08:00
|
|
|
|
int numHandlers = 0;
|
|
|
|
|
var instructions = method.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.Call, OpCodes.Ldnull, OpCodes.Ldftn, OpCodes.Newobj, OpCodes.Callvirt);
|
2011-09-22 10:55:30 +08:00
|
|
|
|
if (instrs == null)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
var call = instrs[0];
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (!DotNetUtils.IsMethod(call.Operand as IMethod, "System.AppDomain", "()"))
|
2011-09-22 10:55:30 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
var ldftn = instrs[2];
|
2013-01-19 20:03:57 +08:00
|
|
|
|
var handlerDef = DotNetUtils.GetMethod(module, ldftn.Operand as IMethod);
|
2011-09-22 10:55:30 +08:00
|
|
|
|
if (handlerDef == null)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
var newobj = instrs[3];
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (!DotNetUtils.IsMethod(newobj.Operand as IMethod, "System.Void", "(System.Object,System.IntPtr)"))
|
2011-09-22 10:55:30 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
var callvirt = instrs[4];
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (!DotNetUtils.IsMethod(callvirt.Operand as IMethod, "System.Void", "(System.ResolveEventHandler)"))
|
2011-09-22 10:55:30 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
numHandlers++;
|
|
|
|
|
yield return handlerDef;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If no handlers found, it's possible that the method itself is the handler.
|
|
|
|
|
if (numHandlers == 0)
|
|
|
|
|
yield return method;
|
|
|
|
|
}
|
2012-01-17 09:54:48 +08:00
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
static bool HasOnlyThisMethod(TypeDef type, MethodDef method) {
|
2012-01-17 09:54:48 +08:00
|
|
|
|
if (type == null || method == null)
|
|
|
|
|
return false;
|
|
|
|
|
foreach (var m in type.Methods) {
|
|
|
|
|
if (m.Name == ".cctor" || m.Name == ".ctor")
|
|
|
|
|
continue;
|
|
|
|
|
if (m == method)
|
|
|
|
|
continue;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2011-09-22 10:55:30 +08:00
|
|
|
|
}
|
|
|
|
|
}
|