2011-09-22 10:55:30 +08:00
|
|
|
|
/*
|
2012-01-10 06:02:47 +08:00
|
|
|
|
Copyright (C) 2011-2012 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-11-02 22:57:11 +08:00
|
|
|
|
using dot10.DotNet;
|
|
|
|
|
using dot10.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 {
|
|
|
|
|
protected ModuleDefinition module;
|
|
|
|
|
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;
|
|
|
|
|
if (!hasOnlyThisMethod(callResolverMethod.DeclaringType, callResolverMethod))
|
|
|
|
|
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; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ResolverInfoBase(ModuleDefinition module, ISimpleDeobfuscator simpleDeobfuscator, IDeobfuscator deob) {
|
|
|
|
|
this.module = module;
|
|
|
|
|
this.simpleDeobfuscator = simpleDeobfuscator;
|
|
|
|
|
this.deob = deob;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool findTypes() {
|
|
|
|
|
if (resolverType != null)
|
|
|
|
|
return true;
|
|
|
|
|
|
2012-01-22 18:15:14 +08:00
|
|
|
|
if (findTypes(DotNetUtils.getModuleTypeCctor(module)))
|
2011-12-22 12:41:28 +08:00
|
|
|
|
return true;
|
|
|
|
|
if (findTypes(module.EntryPoint))
|
|
|
|
|
return true;
|
2011-09-22 10:55:30 +08:00
|
|
|
|
|
2011-12-22 12:41:28 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
bool findTypes(MethodDef initMethod) {
|
2011-12-22 12:41:28 +08:00
|
|
|
|
if (initMethod == null)
|
|
|
|
|
return false;
|
2012-03-18 03:36:41 +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;
|
|
|
|
|
if (!method.IsStatic || !DotNetUtils.isMethod(method, "System.Void", "()"))
|
|
|
|
|
continue;
|
2012-01-17 09:54:48 +08:00
|
|
|
|
if (checkAttachAppMethod(method))
|
2011-09-22 10:55:30 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +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;
|
|
|
|
|
|
2012-03-18 03:36:41 +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;
|
|
|
|
|
if (!method.IsStatic || !DotNetUtils.isMethod(method, "System.Void", "()"))
|
|
|
|
|
continue;
|
2012-01-17 09:54:48 +08:00
|
|
|
|
if (!checkResolverInitMethod(method))
|
2011-09-22 10:55:30 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
callResolverMethod = attachAppMethod;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2011-12-22 12:41:28 +08:00
|
|
|
|
if (hasLdftn(attachAppMethod)) {
|
|
|
|
|
simpleDeobfuscator.deobfuscate(attachAppMethod);
|
2012-01-17 09:54:48 +08:00
|
|
|
|
foreach (var resolverHandler in getResolverHandlers(attachAppMethod)) {
|
2011-12-22 12:41:28 +08:00
|
|
|
|
if (!resolverHandler.HasBody)
|
|
|
|
|
continue;
|
2012-01-17 09:54:48 +08:00
|
|
|
|
var resolverTypeTmp = getResolverType(resolverHandler);
|
|
|
|
|
if (resolverTypeTmp == null)
|
2011-12-22 12:41:28 +08:00
|
|
|
|
continue;
|
|
|
|
|
deobfuscate(resolverHandler);
|
|
|
|
|
if (checkHandlerMethod(resolverHandler)) {
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +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;
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
bool checkResolverInitMethod(MethodDef initMethod) {
|
2011-12-22 12:41:28 +08:00
|
|
|
|
resolverType = null;
|
|
|
|
|
if (!initMethod.HasBody)
|
|
|
|
|
return false;
|
|
|
|
|
|
2011-09-22 10:55:30 +08:00
|
|
|
|
deobfuscate(initMethod);
|
2012-01-17 09:54:48 +08:00
|
|
|
|
foreach (var handlerDef in getResolverHandlers(initMethod)) {
|
2011-09-22 10:55:30 +08:00
|
|
|
|
deobfuscate(handlerDef);
|
2012-01-17 09:54:48 +08:00
|
|
|
|
|
|
|
|
|
var resolverTypeTmp = getResolverType(handlerDef);
|
|
|
|
|
if (resolverTypeTmp == null)
|
|
|
|
|
continue;
|
|
|
|
|
|
2011-09-22 10:55:30 +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;
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
void deobfuscate(MethodDef method) {
|
2011-09-22 10:55:30 +08:00
|
|
|
|
simpleDeobfuscator.deobfuscate(method);
|
|
|
|
|
simpleDeobfuscator.decryptStrings(method, deob);
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +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;
|
|
|
|
|
var field = DotNetUtils.getField(module, instr.Operand as FieldReference);
|
|
|
|
|
if (field == null)
|
|
|
|
|
continue;
|
|
|
|
|
if (!checkResolverType(field.DeclaringType))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
return field.DeclaringType;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (checkResolverType(resolveHandler.DeclaringType))
|
|
|
|
|
return resolveHandler.DeclaringType;
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
protected abstract bool checkResolverType(TypeDef type);
|
|
|
|
|
protected abstract bool checkHandlerMethod(MethodDef handler);
|
2011-09-22 10:55:30 +08:00
|
|
|
|
|
2012-11-02 22:57:11 +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++) {
|
|
|
|
|
var instrs = DotNetUtils.getInstructions(instructions, i, OpCodes.Call, OpCodes.Ldnull, OpCodes.Ldftn, OpCodes.Newobj, OpCodes.Callvirt);
|
|
|
|
|
if (instrs == null)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
var call = instrs[0];
|
|
|
|
|
if (!DotNetUtils.isMethod(call.Operand as MethodReference, "System.AppDomain", "()"))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
var ldftn = instrs[2];
|
2012-01-17 09:54:48 +08:00
|
|
|
|
var handlerDef = DotNetUtils.getMethod(module, ldftn.Operand as MethodReference);
|
2011-09-22 10:55:30 +08:00
|
|
|
|
if (handlerDef == null)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
var newobj = instrs[3];
|
|
|
|
|
if (!DotNetUtils.isMethod(newobj.Operand as MethodReference, "System.Void", "(System.Object,System.IntPtr)"))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
var callvirt = instrs[4];
|
|
|
|
|
if (!DotNetUtils.isMethod(callvirt.Operand as MethodReference, "System.Void", "(System.ResolveEventHandler)"))
|
|
|
|
|
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
|
|
|
|
|
2012-11-02 22:57:11 +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
|
|
|
|
}
|
|
|
|
|
}
|