de4dot-cex/de4dot.code/deobfuscators/SmartAssembly/ResolverInfoBase.cs

232 lines
6.5 KiB
C#
Raw Normal View History

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;
using Mono.Cecil;
using Mono.Cecil.Cil;
2011-09-24 16:26:29 +08:00
using de4dot.blocks;
2011-09-22 10:55:30 +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;
TypeDefinition resolverType;
MethodDefinition callResolverMethod;
2011-11-06 22:24:30 +08:00
public TypeDefinition Type {
2011-09-22 10:55:30 +08:00
get { return resolverType; }
}
public TypeDefinition 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
}
public MethodDefinition CallResolverMethod {
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;
}
bool findTypes(MethodDefinition initMethod) {
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-01-17 09:54:48 +08:00
bool checkAttachAppMethod(MethodDefinition attachAppMethod) {
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;
}
2011-12-22 12:41:28 +08:00
static bool hasLdftn(MethodDefinition method) {
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-01-17 09:54:48 +08:00
bool checkResolverInitMethod(MethodDefinition 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;
}
void deobfuscate(MethodDefinition method) {
simpleDeobfuscator.deobfuscate(method);
simpleDeobfuscator.decryptStrings(method, deob);
}
2012-01-17 09:54:48 +08:00
TypeDefinition getResolverType(MethodDefinition resolveHandler) {
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;
}
2011-09-22 10:55:30 +08:00
protected abstract bool checkResolverType(TypeDefinition type);
protected abstract bool checkHandlerMethod(MethodDefinition handler);
2012-01-17 09:54:48 +08:00
IEnumerable<MethodDefinition> getResolverHandlers(MethodDefinition 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
static bool hasOnlyThisMethod(TypeDefinition type, MethodDefinition method) {
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
}
}