de4dot-cex/de4dot.code/deobfuscators/DeepSea/FieldsRestorer.cs

309 lines
9.4 KiB
C#
Raw Permalink Normal View History

2012-01-25 00:10:11 +08:00
/*
2015-10-30 05:45:26 +08:00
Copyright (C) 2011-2015 de4dot@gmail.com
2012-01-25 00:10:11 +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;
2012-01-25 00:10:11 +08:00
using System.Collections.Generic;
using dnlib.DotNet;
using dnlib.DotNet.Emit;
2012-01-25 00:10:11 +08:00
using de4dot.blocks;
namespace de4dot.code.deobfuscators.DeepSea {
// DS 4.x can move fields from a class to a struct. This class restores the fields.
class FieldsRestorer {
2012-11-09 07:21:45 +08:00
ModuleDefMD module;
2012-11-22 16:14:51 +08:00
TypeDefDict<List<TypeDef>> structToOwners = new TypeDefDict<List<TypeDef>>();
FieldDefAndDeclaringTypeDict<bool> structFieldsToFix = new FieldDefAndDeclaringTypeDict<bool>();
TypeDefDict<FieldDefAndDeclaringTypeDict<FieldDef>> typeToFieldsDict = new TypeDefDict<FieldDefAndDeclaringTypeDict<FieldDef>>();
2012-01-25 00:10:11 +08:00
public List<TypeDef> FieldStructs {
2012-01-25 00:10:11 +08:00
get {
var list = new List<TypeDef>(structToOwners.Count);
2013-01-19 20:03:57 +08:00
foreach (var structType in structToOwners.GetKeys()) {
if (!HasNoMethods(structType))
2012-01-25 00:10:11 +08:00
continue;
list.Add(structType);
}
return list;
}
}
2013-01-19 20:03:57 +08:00
static bool HasNoMethods(TypeDef type) {
2012-05-11 02:20:29 +08:00
if (type.Methods.Count == 0)
return true;
if (type.BaseType == null)
return false;
2012-11-09 07:21:45 +08:00
if (type.BaseType.FullName != "System.Object")
2012-05-11 02:20:29 +08:00
return false;
if (type.Methods.Count != 1)
return false;
var ctor = type.Methods[0];
2012-11-09 07:21:45 +08:00
if (ctor.Name != ".ctor" || ctor.MethodSig.GetParamCount() != 0)
2012-05-11 02:20:29 +08:00
return false;
return true;
}
2012-11-09 07:21:45 +08:00
public FieldsRestorer(ModuleDefMD module) {
2012-01-25 00:10:11 +08:00
this.module = module;
}
2013-01-19 20:03:57 +08:00
public void Initialize() {
foreach (var kv in GetMovedTypes()) {
2012-01-25 00:10:11 +08:00
var structType = kv.Key;
2013-01-19 20:03:57 +08:00
structToOwners.Add(structType, kv.Value);
foreach (var ownerType in kv.Value) {
foreach (var ownerField in ownerType.Fields) {
2013-01-19 20:03:57 +08:00
if (DotNetUtils.GetType(module, ownerField.FieldSig.GetFieldType()) != structType)
continue;
2013-01-19 20:03:57 +08:00
structFieldsToFix.Add(ownerField, true);
break;
}
2012-01-25 00:10:11 +08:00
2012-11-22 16:14:51 +08:00
var fieldsDict = new FieldDefAndDeclaringTypeDict<FieldDef>();
2013-01-19 20:03:57 +08:00
typeToFieldsDict.Add(ownerType, fieldsDict);
foreach (var structField in structType.Fields) {
2012-11-17 06:50:52 +08:00
var newField = module.UpdateRowId(new FieldDefUser(structField.Name, structField.FieldSig.Clone(), structField.Attributes));
ownerType.Fields.Add(newField);
2013-01-19 20:03:57 +08:00
fieldsDict.Add(structField, newField);
}
2012-01-25 00:10:11 +08:00
}
}
}
2013-01-19 20:03:57 +08:00
Dictionary<TypeDef, List<TypeDef>> GetMovedTypes() {
var candidates = new Dictionary<TypeDef, List<TypeDef>>();
var typeToStruct = new Dictionary<TypeDef, TypeDef>();
2012-01-25 00:10:11 +08:00
foreach (var type in module.GetTypes()) {
2013-01-19 20:03:57 +08:00
foreach (var field in GetPossibleFields(type)) {
var fieldType = DotNetUtils.GetType(module, field.FieldSig.GetFieldType());
2012-05-11 02:20:29 +08:00
if (fieldType == null)
continue;
2013-01-19 20:03:57 +08:00
if (!CheckBaseType(fieldType))
2012-01-25 00:10:11 +08:00
continue;
2012-11-17 06:50:52 +08:00
if ((fieldType.Attributes & ~TypeAttributes.Sealed) != TypeAttributes.NestedAssembly)
2012-01-25 00:10:11 +08:00
continue;
if (fieldType.NestedTypes.Count > 0)
continue;
2012-11-17 06:50:52 +08:00
if (fieldType.GenericParameters.Count > 0)
2012-01-25 00:10:11 +08:00
continue;
if (fieldType.Fields.Count == 0)
continue;
if (fieldType.HasEvents || fieldType.HasProperties || fieldType.HasInterfaces)
continue;
2013-01-19 20:03:57 +08:00
if (CheckMethods(fieldType))
2012-01-25 00:10:11 +08:00
continue;
2013-01-19 20:03:57 +08:00
if (!CheckFields(fieldType))
continue;
2012-01-25 00:10:11 +08:00
List<TypeDef> list;
if (!candidates.TryGetValue(fieldType, out list))
candidates[fieldType] = list = new List<TypeDef>();
2012-01-25 00:10:11 +08:00
list.Add(type);
typeToStruct[type] = fieldType;
break;
2012-01-25 00:10:11 +08:00
}
}
foreach (var type in module.GetTypes()) {
TypeDef structType;
typeToStruct.TryGetValue(type, out structType);
2012-01-25 00:10:11 +08:00
foreach (var field in type.Fields) {
2012-11-09 07:21:45 +08:00
if (field.IsStatic || field.FieldSig.GetFieldType().TryGetTypeDef() != structType)
2013-01-19 20:03:57 +08:00
RemoveType(candidates, field.FieldSig.GetFieldType());
2012-01-25 00:10:11 +08:00
}
foreach (var method in type.Methods) {
2013-01-19 20:03:57 +08:00
RemoveType(candidates, method.MethodSig.GetRetType());
2012-11-09 07:21:45 +08:00
foreach (var parameter in method.MethodSig.GetParams())
2013-01-19 20:03:57 +08:00
RemoveType(candidates, parameter);
2012-01-25 00:10:11 +08:00
if (method.Body != null) {
foreach (var local in method.Body.Variables)
2013-01-19 20:03:57 +08:00
RemoveType(candidates, local.Type);
2012-01-25 00:10:11 +08:00
}
}
}
return candidates;
}
2013-01-19 20:03:57 +08:00
IEnumerable<FieldDef> GetPossibleFields(TypeDef type) {
2012-11-22 16:14:51 +08:00
var typeToFields = new TypeDefDict<List<FieldDef>>();
foreach (var field in type.Fields) {
2012-11-17 06:50:52 +08:00
if (field.Attributes != FieldAttributes.Private)
continue;
2013-01-19 20:03:57 +08:00
var fieldType = DotNetUtils.GetType(module, field.FieldSig.GetFieldType());
2012-05-11 02:20:29 +08:00
if (fieldType == null)
continue;
2013-01-19 20:03:57 +08:00
if (!CheckBaseType(fieldType))
continue;
2013-01-19 20:03:57 +08:00
var list = typeToFields.Find(fieldType);
if (list == null)
2013-01-19 20:03:57 +08:00
typeToFields.Add(fieldType, list = new List<FieldDef>());
list.Add(field);
}
2013-01-19 20:03:57 +08:00
foreach (var list in typeToFields.GetValues()) {
if (list.Count == 1)
yield return list[0];
}
}
2013-01-19 20:03:57 +08:00
static bool CheckBaseType(TypeDef type) {
2012-05-11 02:20:29 +08:00
if (type == null || type.BaseType == null)
return false;
2012-11-09 07:21:45 +08:00
var fn = type.BaseType.FullName;
return fn == "System.ValueType" || fn == "System.Object";
2012-05-11 02:20:29 +08:00
}
2013-01-19 20:03:57 +08:00
void RemoveType(Dictionary<TypeDef, List<TypeDef>> candidates, TypeSig type) {
var typeDef = DotNetUtils.GetType(module, type);
2012-01-25 00:10:11 +08:00
if (typeDef == null)
return;
candidates.Remove(typeDef);
}
2013-01-19 20:03:57 +08:00
static bool CheckMethods(TypeDef type) {
2012-01-25 00:10:11 +08:00
foreach (var method in type.Methods) {
if (method.Name == ".cctor")
continue;
2012-11-09 07:21:45 +08:00
if (type.BaseType != null && type.BaseType.FullName == "System.Object" && method.Name == ".ctor" && method.MethodSig.GetParamCount() == 0)
2012-05-11 02:20:29 +08:00
continue;
2012-01-25 00:10:11 +08:00
if (!method.IsStatic)
return true;
2012-11-17 06:50:52 +08:00
if (method.GenericParameters.Count > 0)
2012-01-25 12:47:34 +08:00
return true;
2012-01-25 00:10:11 +08:00
if (method.Body == null)
return true;
2012-11-09 07:21:45 +08:00
if (method.ImplMap != null)
2012-01-25 00:10:11 +08:00
return true;
}
return false;
}
2013-01-19 20:03:57 +08:00
static bool CheckFields(TypeDef type) {
2012-05-11 02:20:29 +08:00
if (type.Fields.Count == 0)
return false;
foreach (var field in type.Fields) {
if (field.IsStatic)
2012-05-11 02:20:29 +08:00
return false;
if (!field.IsAssembly)
return false;
}
2012-05-11 02:20:29 +08:00
return true;
}
2013-01-19 20:03:57 +08:00
public void Deobfuscate(Blocks blocks) {
DeobfuscateNormal(blocks);
FixFieldCtorCalls(blocks);
2012-05-11 02:20:29 +08:00
}
2013-01-19 20:03:57 +08:00
void DeobfuscateNormal(Blocks blocks) {
var instrsToRemove = new List<int>();
2013-01-19 20:03:57 +08:00
foreach (var block in blocks.MethodBlocks.GetAllBlocks()) {
instrsToRemove.Clear();
2012-01-25 00:10:11 +08:00
var instrs = block.Instructions;
for (int i = instrs.Count - 1; i >= 0; i--) {
var instr = instrs[i];
2012-05-11 02:20:29 +08:00
if (instr.OpCode.Code != Code.Ldflda && instr.OpCode.Code != Code.Ldfld)
2012-01-25 00:10:11 +08:00
continue;
2012-11-09 07:21:45 +08:00
var structField = instr.Operand as IField;
2013-01-19 20:03:57 +08:00
if (structField == null || !structFieldsToFix.Find(structField))
2012-01-25 00:10:11 +08:00
continue;
2013-01-19 20:03:57 +08:00
var ldStFld = instrs[FindLdStFieldIndex(instrs, i + 1)];
ldStFld.Operand = GetNewField(structField, ldStFld.Operand as IField);
instrsToRemove.Add(i);
2012-01-25 00:10:11 +08:00
}
if (instrsToRemove.Count > 0)
2013-01-19 20:03:57 +08:00
block.Remove(instrsToRemove);
2012-01-25 00:10:11 +08:00
}
}
2013-01-19 20:03:57 +08:00
void FixFieldCtorCalls(Blocks blocks) {
2012-05-11 02:20:29 +08:00
if (blocks.Method.Name != ".ctor")
return;
2017-01-05 20:46:04 +08:00
//var instrsToRemove = new List<int>();
2013-01-19 20:03:57 +08:00
foreach (var block in blocks.MethodBlocks.GetAllBlocks()) {
2012-05-11 02:20:29 +08:00
var instrs = block.Instructions;
for (int i = 0; i < instrs.Count; i++) {
var stfld = instrs[i];
if (stfld.OpCode.Code != Code.Stfld)
continue;
2012-11-09 07:21:45 +08:00
var field = stfld.Operand as IField;
2012-05-11 02:20:29 +08:00
if (field == null)
continue;
2013-01-19 20:03:57 +08:00
if (!structFieldsToFix.Find(field))
2012-05-11 02:20:29 +08:00
continue;
2013-01-19 20:03:57 +08:00
var instrs2 = ToInstructionList(instrs);
var instrPushes = DotNetUtils.GetArgPushes(instrs2, i);
2012-05-11 02:20:29 +08:00
if (instrPushes == null || instrPushes.Count != 2)
continue;
2013-01-19 20:03:57 +08:00
block.Remove(i, 1);
block.Remove(instrs2.IndexOf(instrPushes[1]), 1);
block.Remove(instrs2.IndexOf(instrPushes[0]), 1);
2012-05-11 02:20:29 +08:00
i -= 3;
}
}
}
2013-01-19 20:03:57 +08:00
static IList<Instruction> ToInstructionList(IEnumerable<Instr> instrs) {
2012-05-11 02:20:29 +08:00
var newInstrs = new List<Instruction>();
foreach (var instr in instrs)
newInstrs.Add(instr.Instruction);
return newInstrs;
}
2013-01-19 20:03:57 +08:00
FieldDef GetNewField(IField structField, IField oldFieldRef) {
var fieldsDict = typeToFieldsDict.Find(structField.DeclaringType);
if (fieldsDict == null)
throw new ApplicationException("Could not find structField declaringType");
2013-01-19 20:03:57 +08:00
var newField = fieldsDict.Find(oldFieldRef);
if (newField == null)
throw new ApplicationException("Could not find new field");
return newField;
}
2013-01-19 20:03:57 +08:00
static int FindLdStFieldIndex(IList<Instr> instrs, int index) {
int stack = 0;
for (int i = index; i < instrs.Count; i++) {
var instr = instrs[i];
if (stack == 0 && (instr.OpCode.Code == Code.Ldfld || instr.OpCode.Code == Code.Ldflda))
return i;
if (stack == 1 && instr.OpCode.Code == Code.Stfld)
return i;
int pushes, pops;
2012-11-09 07:21:45 +08:00
instr.Instruction.CalculateStackUsage(false, out pushes, out pops);
stack -= pops;
if (stack < 0)
break;
stack += pushes;
}
throw new ApplicationException("Could not find ldfld/stfld");
}
2013-01-19 20:03:57 +08:00
public void CleanUp() {
foreach (var field in structFieldsToFix.GetKeys())
field.DeclaringType.Fields.Remove(field);
}
2012-01-25 00:10:11 +08:00
}
}