2012-02-02 17:55:30 +08:00
|
|
|
|
/*
|
2014-03-12 05:15:43 +08:00
|
|
|
|
Copyright (C) 2011-2014 de4dot@gmail.com
|
2012-02-02 17: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/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2012-02-03 13:33:54 +08:00
|
|
|
|
using System.Collections.Generic;
|
2012-12-20 09:06:09 +08:00
|
|
|
|
using dnlib.DotNet;
|
|
|
|
|
using dnlib.DotNet.Emit;
|
2012-02-03 10:03:19 +08:00
|
|
|
|
using de4dot.blocks;
|
2012-02-02 17:55:30 +08:00
|
|
|
|
using de4dot.blocks.cflow;
|
|
|
|
|
|
|
|
|
|
namespace de4dot.code.deobfuscators.Spices_Net {
|
|
|
|
|
class SpicesMethodCallInliner : MethodCallInliner {
|
2012-11-15 02:29:29 +08:00
|
|
|
|
ModuleDefMD module;
|
2012-11-22 16:14:51 +08:00
|
|
|
|
TypeDefDict<bool> methodsTypes = new TypeDefDict<bool>();
|
|
|
|
|
MethodDefAndDeclaringTypeDict<MethodDef> classMethods = new MethodDefAndDeclaringTypeDict<MethodDef>();
|
2012-02-03 10:03:19 +08:00
|
|
|
|
|
2012-11-15 02:29:29 +08:00
|
|
|
|
public SpicesMethodCallInliner(ModuleDefMD module)
|
2012-02-02 17:55:30 +08:00
|
|
|
|
: base(false) {
|
2012-02-03 10:03:19 +08:00
|
|
|
|
this.module = module;
|
2012-02-02 17:55:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
protected override bool IsCompatibleType(int paramIndex, IType origType, IType newType) {
|
2012-11-15 04:44:57 +08:00
|
|
|
|
if (new SigComparer(SigComparerOptions.IgnoreModifiers).Equals(origType, newType))
|
2012-02-03 16:44:35 +08:00
|
|
|
|
return true;
|
|
|
|
|
if (paramIndex == -1) {
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (IsValueType(newType) || IsValueType(origType))
|
2012-02-03 16:44:35 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2012-11-15 02:29:29 +08:00
|
|
|
|
return newType.FullName == "System.Object";
|
2012-02-03 16:44:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public bool CheckCanInline(MethodDef method) {
|
|
|
|
|
return methodsTypes.Find(method.DeclaringType);
|
2012-02-02 17:55:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
protected override bool CanInline(MethodDef method) {
|
|
|
|
|
return CheckCanInline(method);
|
2012-02-03 10:03:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-09-29 01:51:25 +08:00
|
|
|
|
public void Initialize(ISimpleDeobfuscator simpleDeobfuscator) {
|
2013-01-19 20:03:57 +08:00
|
|
|
|
InitializeMethodsTypes();
|
2013-09-29 01:51:25 +08:00
|
|
|
|
RestoreMethodBodies(simpleDeobfuscator);
|
2012-02-03 14:16:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-09-29 01:51:25 +08:00
|
|
|
|
void RestoreMethodBodies(ISimpleDeobfuscator simpleDeobfuscator) {
|
2012-11-22 16:14:51 +08:00
|
|
|
|
var methodToOrigMethods = new MethodDefAndDeclaringTypeDict<List<MethodDef>>();
|
2012-02-03 14:16:55 +08:00
|
|
|
|
foreach (var t in module.Types) {
|
2012-11-15 02:29:29 +08:00
|
|
|
|
var types = new List<TypeDef>(AllTypesHelper.Types(new List<TypeDef> { t }));
|
2012-02-03 17:05:31 +08:00
|
|
|
|
foreach (var type in types) {
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (methodsTypes.Find(type))
|
2012-02-03 14:16:55 +08:00
|
|
|
|
continue;
|
|
|
|
|
foreach (var method in type.Methods) {
|
|
|
|
|
if (method.Name == ".ctor" || method.Name == ".cctor")
|
|
|
|
|
continue;
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
MethodDef calledMethod;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (!CheckRestoreBody(method, out calledMethod))
|
2012-02-03 14:16:55 +08:00
|
|
|
|
continue;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (!CheckSameMethods(method, calledMethod))
|
2012-02-03 21:22:58 +08:00
|
|
|
|
continue;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (!methodsTypes.Find(calledMethod.DeclaringType))
|
2012-02-03 21:22:58 +08:00
|
|
|
|
continue;
|
2012-02-03 17:05:31 +08:00
|
|
|
|
if (types.IndexOf(calledMethod.DeclaringType) < 0)
|
|
|
|
|
continue;
|
2012-02-03 14:16:55 +08:00
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
var list = methodToOrigMethods.Find(calledMethod);
|
2012-02-03 14:16:55 +08:00
|
|
|
|
if (list == null)
|
2013-01-19 20:03:57 +08:00
|
|
|
|
methodToOrigMethods.Add(calledMethod, list = new List<MethodDef>());
|
2012-02-03 14:16:55 +08:00
|
|
|
|
list.Add(method);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
foreach (var calledMethod in methodToOrigMethods.GetKeys()) {
|
|
|
|
|
var list = methodToOrigMethods.Find(calledMethod);
|
2012-02-03 14:16:55 +08:00
|
|
|
|
var method = list[0];
|
|
|
|
|
|
2012-11-11 12:31:11 +08:00
|
|
|
|
Logger.v("Restored method body {0:X8} from method {1:X8}",
|
2012-11-02 22:57:11 +08:00
|
|
|
|
method.MDToken.ToInt32(),
|
|
|
|
|
calledMethod.MDToken.ToInt32());
|
2013-01-19 20:03:57 +08:00
|
|
|
|
DotNetUtils.CopyBodyFromTo(calledMethod, method);
|
|
|
|
|
classMethods.Add(calledMethod, method);
|
2013-09-29 01:51:25 +08:00
|
|
|
|
simpleDeobfuscator.MethodModified(method);
|
2012-02-03 14:16:55 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
bool CheckRestoreBody(MethodDef method, out MethodDef calledMethod) {
|
2012-02-03 14:16:55 +08:00
|
|
|
|
calledMethod = null;
|
|
|
|
|
if (method.Body == null)
|
|
|
|
|
return false;
|
2012-11-20 00:58:34 +08:00
|
|
|
|
if (method.Body.Variables.Count > 0)
|
2012-02-03 14:16:55 +08:00
|
|
|
|
return false;
|
|
|
|
|
if (method.Body.ExceptionHandlers.Count > 0)
|
|
|
|
|
return false;
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (!CheckRestoreBody2(method, out calledMethod))
|
2012-02-03 14:16:55 +08:00
|
|
|
|
return false;
|
|
|
|
|
if (calledMethod == method)
|
|
|
|
|
return false;
|
2012-02-03 21:22:58 +08:00
|
|
|
|
if (!calledMethod.IsStatic)
|
|
|
|
|
return false;
|
2012-11-17 06:50:52 +08:00
|
|
|
|
if (calledMethod.GenericParameters.Count > 0)
|
2012-02-03 23:21:59 +08:00
|
|
|
|
return false;
|
2012-02-03 18:10:48 +08:00
|
|
|
|
if (calledMethod.Body == null || calledMethod.Body.Instructions.Count == 0)
|
|
|
|
|
return false;
|
2012-02-03 14:16:55 +08:00
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
bool CheckRestoreBody2(MethodDef instanceMethod, out MethodDef calledMethod) {
|
2012-02-03 14:16:55 +08:00
|
|
|
|
calledMethod = null;
|
|
|
|
|
|
|
|
|
|
var instrs = instanceMethod.Body.Instructions;
|
|
|
|
|
int index;
|
|
|
|
|
for (index = 0; index < instrs.Count; index++) {
|
2012-11-15 02:29:29 +08:00
|
|
|
|
if (instrs[index].GetParameterIndex() != index)
|
2012-02-03 14:16:55 +08:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
var call = instrs[index++];
|
|
|
|
|
if (call.OpCode.Code != Code.Call)
|
|
|
|
|
return false;
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
calledMethod = call.Operand as MethodDef;
|
2012-02-03 14:16:55 +08:00
|
|
|
|
if (calledMethod == null)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
if (instrs[index++].OpCode.Code != Code.Ret)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
void InitializeMethodsTypes() {
|
2012-02-03 10:03:19 +08:00
|
|
|
|
foreach (var type in module.GetTypes()) {
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (CheckMethodsType(type))
|
|
|
|
|
methodsTypes.Add(type, true);
|
2012-02-03 10:03:19 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
static bool CheckMethodsType(TypeDef type) {
|
2012-02-03 14:16:55 +08:00
|
|
|
|
if (!type.IsNested)
|
|
|
|
|
return false;
|
2012-11-17 06:50:52 +08:00
|
|
|
|
if ((type.Attributes & ~TypeAttributes.BeforeFieldInit) != TypeAttributes.NestedAssembly)
|
2012-02-03 10:03:19 +08:00
|
|
|
|
return false;
|
|
|
|
|
if (type.HasProperties || type.HasEvents || type.HasFields || type.HasNestedTypes)
|
|
|
|
|
return false;
|
2012-11-17 06:50:52 +08:00
|
|
|
|
if (type.GenericParameters.Count > 0)
|
2012-02-03 10:03:19 +08:00
|
|
|
|
return false;
|
|
|
|
|
if (type.IsValueType || type.IsInterface)
|
|
|
|
|
return false;
|
2012-11-15 02:29:29 +08:00
|
|
|
|
if (type.BaseType == null || type.BaseType.FullName != "System.Object")
|
2012-02-03 10:03:19 +08:00
|
|
|
|
return false;
|
2012-11-17 06:50:52 +08:00
|
|
|
|
if (type.Interfaces.Count > 0)
|
2012-02-03 10:03:19 +08:00
|
|
|
|
return false;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (!CheckMethods(type))
|
2012-02-02 17:55:30 +08:00
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2012-02-03 10:03:19 +08:00
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
static bool CheckMethods(TypeDef type) {
|
2012-02-03 10:03:19 +08:00
|
|
|
|
bool foundCtor = false;
|
|
|
|
|
int numMethods = 0;
|
|
|
|
|
|
|
|
|
|
foreach (var method in type.Methods) {
|
|
|
|
|
if (method.Name == ".cctor")
|
|
|
|
|
return false;
|
|
|
|
|
if (method.Name == ".ctor") {
|
2012-11-15 02:29:29 +08:00
|
|
|
|
if (method.MethodSig.GetParamCount() != 0)
|
2012-02-03 10:03:19 +08:00
|
|
|
|
return false;
|
|
|
|
|
foundCtor = true;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2012-11-17 06:50:52 +08:00
|
|
|
|
if (method.Attributes != (MethodAttributes.Assembly | MethodAttributes.Static | MethodAttributes.HideBySig))
|
2012-02-03 10:03:19 +08:00
|
|
|
|
return false;
|
2012-11-15 02:29:29 +08:00
|
|
|
|
if (method.ImplMap != null)
|
2012-02-03 10:03:19 +08:00
|
|
|
|
return false;
|
2012-11-17 06:50:52 +08:00
|
|
|
|
if (method.GenericParameters.Count > 0)
|
2012-02-03 10:03:19 +08:00
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
numMethods++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return numMethods > 0 && foundCtor;
|
|
|
|
|
}
|
2012-02-03 11:26:55 +08:00
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public List<MethodDef> GetInlinedMethods() {
|
2012-11-02 22:57:11 +08:00
|
|
|
|
var list = new List<MethodDef>();
|
2012-02-03 13:33:54 +08:00
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
foreach (var type in methodsTypes.GetKeys())
|
2012-02-03 13:33:54 +08:00
|
|
|
|
list.AddRange(type.Methods);
|
|
|
|
|
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public TypeDefDict<bool> GetInlinedTypes(IEnumerable<MethodDef> unusedMethods) {
|
2012-11-22 16:14:51 +08:00
|
|
|
|
var unused = new MethodDefAndDeclaringTypeDict<bool>();
|
2012-02-03 13:33:54 +08:00
|
|
|
|
foreach (var method in unusedMethods)
|
2013-01-19 20:03:57 +08:00
|
|
|
|
unused.Add(method, true);
|
2012-02-03 13:33:54 +08:00
|
|
|
|
|
2012-11-22 16:14:51 +08:00
|
|
|
|
var types = new TypeDefDict<bool>();
|
2013-01-19 20:03:57 +08:00
|
|
|
|
foreach (var type in methodsTypes.GetKeys()) {
|
|
|
|
|
if (CheckAllMethodsUnused(unused, type))
|
|
|
|
|
types.Add(type, true);
|
2012-02-03 13:33:54 +08:00
|
|
|
|
}
|
|
|
|
|
return types;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
static bool CheckAllMethodsUnused(MethodDefAndDeclaringTypeDict<bool> unused, TypeDef type) {
|
2012-02-03 13:33:54 +08:00
|
|
|
|
foreach (var method in type.Methods) {
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (!unused.Find(method))
|
2012-02-03 13:33:54 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2012-02-03 14:16:55 +08:00
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public void Deobfuscate(Blocks blocks) {
|
|
|
|
|
foreach (var block in blocks.MethodBlocks.GetAllBlocks()) {
|
2012-02-03 14:16:55 +08:00
|
|
|
|
var instrs = block.Instructions;
|
|
|
|
|
for (int i = 0; i < instrs.Count; i++) {
|
|
|
|
|
var call = instrs[i];
|
|
|
|
|
if (call.OpCode.Code != Code.Call)
|
|
|
|
|
continue;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
var realInstanceMethod = classMethods.Find(call.Operand as IMethod);
|
2012-02-03 14:16:55 +08:00
|
|
|
|
if (realInstanceMethod == null)
|
|
|
|
|
continue;
|
|
|
|
|
call.Operand = realInstanceMethod;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-02-02 17:55:30 +08:00
|
|
|
|
}
|
|
|
|
|
}
|