Update fields restorer since 2+ types can share same struct
This commit is contained in:
parent
ef3b610c92
commit
26a3e14d2c
|
@ -231,6 +231,8 @@ done:
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void deobfuscateEnd() {
|
public override void deobfuscateEnd() {
|
||||||
|
if (options.RestoreFields)
|
||||||
|
fieldsRestorer.cleanUp();
|
||||||
removeInlinedMethods();
|
removeInlinedMethods();
|
||||||
|
|
||||||
if (options.RestoreFields)
|
if (options.RestoreFields)
|
||||||
|
|
|
@ -17,6 +17,7 @@
|
||||||
along with de4dot. If not, see <http://www.gnu.org/licenses/>.
|
along with de4dot. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using Mono.Cecil;
|
using Mono.Cecil;
|
||||||
using Mono.Cecil.Cil;
|
using Mono.Cecil.Cil;
|
||||||
|
@ -26,13 +27,14 @@ namespace de4dot.code.deobfuscators.DeepSea {
|
||||||
// DS 4.x can move fields from a class to a struct. This class restores the fields.
|
// DS 4.x can move fields from a class to a struct. This class restores the fields.
|
||||||
class FieldsRestorer {
|
class FieldsRestorer {
|
||||||
ModuleDefinition module;
|
ModuleDefinition module;
|
||||||
TypeDefinitionDict<TypeDefinition> structToOwner = new TypeDefinitionDict<TypeDefinition>();
|
TypeDefinitionDict<List<TypeDefinition>> structToOwners = new TypeDefinitionDict<List<TypeDefinition>>();
|
||||||
Dictionary<int, bool> oldFieldToken = new Dictionary<int, bool>();
|
FieldDefinitionAndDeclaringTypeDict<bool> structFieldsToFix = new FieldDefinitionAndDeclaringTypeDict<bool>();
|
||||||
|
TypeDefinitionDict<FieldDefinitionAndDeclaringTypeDict<FieldDefinition>> typeToFieldsDict = new TypeDefinitionDict<FieldDefinitionAndDeclaringTypeDict<FieldDefinition>>();
|
||||||
|
|
||||||
public List<TypeDefinition> FieldStructs {
|
public List<TypeDefinition> FieldStructs {
|
||||||
get {
|
get {
|
||||||
var list = new List<TypeDefinition>(structToOwner.Count);
|
var list = new List<TypeDefinition>(structToOwners.Count);
|
||||||
foreach (var structType in structToOwner.getKeys()) {
|
foreach (var structType in structToOwners.getKeys()) {
|
||||||
if (structType.Methods.Count != 0)
|
if (structType.Methods.Count != 0)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
@ -49,33 +51,32 @@ namespace de4dot.code.deobfuscators.DeepSea {
|
||||||
public void initialize() {
|
public void initialize() {
|
||||||
foreach (var kv in getMovedTypes()) {
|
foreach (var kv in getMovedTypes()) {
|
||||||
var structType = kv.Key;
|
var structType = kv.Key;
|
||||||
var ownerType = kv.Value;
|
structToOwners.add(structType, kv.Value);
|
||||||
structToOwner.add(structType, ownerType);
|
|
||||||
|
|
||||||
for (int i = 0; i < ownerType.Fields.Count; i++) {
|
foreach (var ownerType in kv.Value) {
|
||||||
if (DotNetUtils.getType(module, ownerType.Fields[i].FieldType) != structType)
|
foreach (var ownerField in ownerType.Fields) {
|
||||||
continue;
|
if (DotNetUtils.getType(module, ownerField.FieldType) != structType)
|
||||||
oldFieldToken[ownerType.Fields[i].MetadataToken.ToInt32()] = true;
|
continue;
|
||||||
ownerType.Fields.RemoveAt(i);
|
structFieldsToFix.add(ownerField, true);
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
var fieldsDict = new FieldDefinitionAndDeclaringTypeDict<FieldDefinition>();
|
||||||
|
typeToFieldsDict.add(ownerType, fieldsDict);
|
||||||
|
foreach (var structField in structType.Fields) {
|
||||||
|
var newField = DotNetUtils.createFieldDefinition(structField.Name, structField.Attributes, structField.FieldType);
|
||||||
|
ownerType.Fields.Add(newField);
|
||||||
|
fieldsDict.add(structField, newField);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var structTypeFields = new List<FieldDefinition>(structType.Fields);
|
|
||||||
structType.Fields.Clear();
|
|
||||||
foreach (var field in structTypeFields)
|
|
||||||
ownerType.Fields.Add(field);
|
|
||||||
|
|
||||||
// Add a field so peverify won't complain if this type isn't removed
|
|
||||||
var newField = new FieldDefinition("a", FieldAttributes.Public, module.TypeSystem.Byte);
|
|
||||||
newField.MetadataToken = DotNetUtils.nextFieldToken();
|
|
||||||
structType.Fields.Add(newField);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Dictionary<TypeDefinition, TypeDefinition> getMovedTypes() {
|
Dictionary<TypeDefinition, List<TypeDefinition>> getMovedTypes() {
|
||||||
var fieldTypeToTypes = new Dictionary<TypeDefinition, List<TypeDefinition>>();
|
var candidates = new Dictionary<TypeDefinition, List<TypeDefinition>>();
|
||||||
|
var typeToStruct = new Dictionary<TypeDefinition, TypeDefinition>();
|
||||||
foreach (var type in module.GetTypes()) {
|
foreach (var type in module.GetTypes()) {
|
||||||
foreach (var field in type.Fields) {
|
foreach (var field in getPossibleFields(type)) {
|
||||||
var fieldType = DotNetUtils.getType(module, field.FieldType);
|
var fieldType = DotNetUtils.getType(module, field.FieldType);
|
||||||
if (fieldType == null || !fieldType.IsValueType)
|
if (fieldType == null || !fieldType.IsValueType)
|
||||||
continue;
|
continue;
|
||||||
|
@ -87,26 +88,28 @@ namespace de4dot.code.deobfuscators.DeepSea {
|
||||||
continue;
|
continue;
|
||||||
if (fieldType.Fields.Count == 0)
|
if (fieldType.Fields.Count == 0)
|
||||||
continue;
|
continue;
|
||||||
|
if (fieldType.HasEvents || fieldType.HasProperties || fieldType.HasInterfaces)
|
||||||
|
continue;
|
||||||
if (hasNonStaticMethods(fieldType))
|
if (hasNonStaticMethods(fieldType))
|
||||||
continue;
|
continue;
|
||||||
|
if (hasStaticFields(fieldType))
|
||||||
|
continue;
|
||||||
|
|
||||||
List<TypeDefinition> list;
|
List<TypeDefinition> list;
|
||||||
if (!fieldTypeToTypes.TryGetValue(fieldType, out list))
|
if (!candidates.TryGetValue(fieldType, out list))
|
||||||
fieldTypeToTypes[fieldType] = list = new List<TypeDefinition>();
|
candidates[fieldType] = list = new List<TypeDefinition>();
|
||||||
list.Add(type);
|
list.Add(type);
|
||||||
|
typeToStruct[type] = fieldType;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var candidates = new Dictionary<TypeDefinition, TypeDefinition>();
|
|
||||||
foreach (var kv in fieldTypeToTypes) {
|
|
||||||
if (kv.Value.Count != 1)
|
|
||||||
continue;
|
|
||||||
candidates[kv.Key] = kv.Value[0];
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (var type in module.GetTypes()) {
|
foreach (var type in module.GetTypes()) {
|
||||||
|
TypeDefinition structType;
|
||||||
|
typeToStruct.TryGetValue(type, out structType);
|
||||||
|
|
||||||
foreach (var field in type.Fields) {
|
foreach (var field in type.Fields) {
|
||||||
if (field.DeclaringType != type)
|
if (field.IsStatic || field.FieldType != structType)
|
||||||
removeType(candidates, field.FieldType);
|
removeType(candidates, field.FieldType);
|
||||||
}
|
}
|
||||||
foreach (var method in type.Methods) {
|
foreach (var method in type.Methods) {
|
||||||
|
@ -123,7 +126,27 @@ namespace de4dot.code.deobfuscators.DeepSea {
|
||||||
return candidates;
|
return candidates;
|
||||||
}
|
}
|
||||||
|
|
||||||
void removeType(Dictionary<TypeDefinition, TypeDefinition> candidates, TypeReference type) {
|
IEnumerable<FieldDefinition> getPossibleFields(TypeDefinition type) {
|
||||||
|
var typeToFields = new TypeDefinitionDict<List<FieldDefinition>>();
|
||||||
|
foreach (var field in type.Fields) {
|
||||||
|
if (field.Attributes != FieldAttributes.Private)
|
||||||
|
continue;
|
||||||
|
var fieldType = DotNetUtils.getType(module, field.FieldType);
|
||||||
|
if (fieldType == null || !fieldType.IsValueType)
|
||||||
|
continue;
|
||||||
|
var list = typeToFields.find(fieldType);
|
||||||
|
if (list == null)
|
||||||
|
typeToFields.add(fieldType, list = new List<FieldDefinition>());
|
||||||
|
list.Add(field);
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var list in typeToFields.getValues()) {
|
||||||
|
if (list.Count == 1)
|
||||||
|
yield return list[0];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void removeType(Dictionary<TypeDefinition, List<TypeDefinition>> candidates, TypeReference type) {
|
||||||
var typeDef = DotNetUtils.getType(module, type);
|
var typeDef = DotNetUtils.getType(module, type);
|
||||||
if (typeDef == null)
|
if (typeDef == null)
|
||||||
return;
|
return;
|
||||||
|
@ -146,21 +169,69 @@ namespace de4dot.code.deobfuscators.DeepSea {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static bool hasStaticFields(TypeDefinition type) {
|
||||||
|
foreach (var field in type.Fields) {
|
||||||
|
if (field.IsStatic)
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
public void deobfuscate(Blocks blocks) {
|
public void deobfuscate(Blocks blocks) {
|
||||||
|
var instrsToRemove = new List<int>();
|
||||||
foreach (var block in blocks.MethodBlocks.getAllBlocks()) {
|
foreach (var block in blocks.MethodBlocks.getAllBlocks()) {
|
||||||
|
instrsToRemove.Clear();
|
||||||
var instrs = block.Instructions;
|
var instrs = block.Instructions;
|
||||||
for (int i = instrs.Count - 1; i >= 0; i--) {
|
for (int i = instrs.Count - 1; i >= 0; i--) {
|
||||||
var instr = instrs[i];
|
var instr = instrs[i];
|
||||||
if (instr.OpCode.Code != Code.Ldflda)
|
if (instr.OpCode.Code != Code.Ldflda)
|
||||||
continue;
|
continue;
|
||||||
var field = instr.Operand as FieldReference;
|
var structField = instr.Operand as FieldReference;
|
||||||
if (field == null)
|
if (structField == null || !structFieldsToFix.find(structField))
|
||||||
continue;
|
continue;
|
||||||
if (!oldFieldToken.ContainsKey(field.MetadataToken.ToInt32()))
|
|
||||||
continue;
|
var ldStFld = instrs[findLdStFieldIndex(instrs, i + 1)];
|
||||||
instrs.RemoveAt(i);
|
ldStFld.Operand = getNewField(structField, ldStFld.Operand as FieldReference);
|
||||||
|
instrsToRemove.Add(i);
|
||||||
}
|
}
|
||||||
|
if (instrsToRemove.Count > 0)
|
||||||
|
block.remove(instrsToRemove);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
FieldDefinition getNewField(FieldReference structField, FieldReference oldFieldRef) {
|
||||||
|
var fieldsDict = typeToFieldsDict.find(structField.DeclaringType);
|
||||||
|
if (fieldsDict == null)
|
||||||
|
throw new ApplicationException("Could not find structField declaringType");
|
||||||
|
var newField = fieldsDict.find(oldFieldRef);
|
||||||
|
if (newField == null)
|
||||||
|
throw new ApplicationException("Could not find new field");
|
||||||
|
return newField;
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
DotNetUtils.calculateStackUsage(instr.Instruction, false, out pushes, out pops);
|
||||||
|
stack -= pops;
|
||||||
|
if (stack < 0)
|
||||||
|
break;
|
||||||
|
stack += pushes;
|
||||||
|
}
|
||||||
|
throw new ApplicationException("Could not find ldfld/stfld");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void cleanUp() {
|
||||||
|
foreach (var field in structFieldsToFix.getKeys())
|
||||||
|
field.DeclaringType.Fields.Remove(field);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user