diff --git a/de4dot.code/DeobfuscatorContext.cs b/de4dot.code/DeobfuscatorContext.cs index 2b42c90c..3882eced 100644 --- a/de4dot.code/DeobfuscatorContext.cs +++ b/de4dot.code/DeobfuscatorContext.cs @@ -58,20 +58,20 @@ namespace de4dot.code { return type.ElementType; } - public TypeDefinition resolve(TypeReference type) { + public TypeDef resolve(TypeReference type) { if (type == null) return null; - var typeDef = getNonGenericTypeReference(type) as TypeDefinition; + var typeDef = getNonGenericTypeReference(type) as TypeDef; if (typeDef != null) return typeDef; return externalAssemblies.resolve(type); } - public MethodDefinition resolve(MethodReference method) { + public MethodDef resolve(MethodReference method) { if (method == null) return null; - var methodDef = method as MethodDefinition; + var methodDef = method as MethodDef; if (methodDef != null) return methodDef; @@ -87,10 +87,10 @@ namespace de4dot.code { return null; } - public FieldDefinition resolve(FieldReference field) { + public FieldDef resolve(FieldReference field) { if (field == null) return null; - var fieldDef = field as FieldDefinition; + var fieldDef = field as FieldDef; if (fieldDef != null) return fieldDef; diff --git a/de4dot.code/de4dot.code.csproj b/de4dot.code/de4dot.code.csproj index a59afab3..6d624005 100644 --- a/de4dot.code/de4dot.code.csproj +++ b/de4dot.code/de4dot.code.csproj @@ -277,7 +277,7 @@ - + diff --git a/de4dot.code/deobfuscators/Babel_NET/AssemblyResolver.cs b/de4dot.code/deobfuscators/Babel_NET/AssemblyResolver.cs index 1af66548..4ed3d84f 100644 --- a/de4dot.code/deobfuscators/Babel_NET/AssemblyResolver.cs +++ b/de4dot.code/deobfuscators/Babel_NET/AssemblyResolver.cs @@ -19,15 +19,15 @@ using System; using System.IO; -using Mono.Cecil; +using dot10.DotNet; using de4dot.blocks; namespace de4dot.code.deobfuscators.Babel_NET { class AssemblyResolver { ModuleDefinition module; ResourceDecrypter resourceDecrypter; - TypeDefinition resolverType; - MethodDefinition registerMethod; + TypeDef resolverType; + MethodDef registerMethod; EmbeddedResource encryptedResource; EmbeddedAssemblyInfo[] embeddedAssemblyInfos = new EmbeddedAssemblyInfo[0]; @@ -47,11 +47,11 @@ namespace de4dot.code.deobfuscators.Babel_NET { get { return resolverType != null; } } - public TypeDefinition Type { + public TypeDef Type { get { return resolverType; } } - public MethodDefinition InitMethod { + public MethodDef InitMethod { get { return registerMethod; } } @@ -80,7 +80,7 @@ namespace de4dot.code.deobfuscators.Babel_NET { if (!new FieldTypes(type).exactly(requiredTypes)) continue; - MethodDefinition regMethod, handler; + MethodDef regMethod, handler; if (!BabelUtils.findRegisterMethod(type, out regMethod, out handler)) continue; @@ -95,7 +95,7 @@ namespace de4dot.code.deobfuscators.Babel_NET { } } - static MethodDefinition findDecryptMethod(TypeDefinition type) { + static MethodDef findDecryptMethod(TypeDef type) { foreach (var method in type.Methods) { if (!DotNetUtils.isMethod(method, "System.Void", "(System.IO.Stream)")) continue; diff --git a/de4dot.code/deobfuscators/Babel_NET/BabelMethodCallInliner.cs b/de4dot.code/deobfuscators/Babel_NET/BabelMethodCallInliner.cs index 0bc17b71..b90c7ea9 100644 --- a/de4dot.code/deobfuscators/Babel_NET/BabelMethodCallInliner.cs +++ b/de4dot.code/deobfuscators/Babel_NET/BabelMethodCallInliner.cs @@ -18,8 +18,8 @@ */ using System.Collections.Generic; -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using de4dot.blocks; using de4dot.blocks.cflow; @@ -35,12 +35,12 @@ namespace de4dot.code.deobfuscators.Babel_NET { branchEmulator = new BranchEmulator(emulator, this); } - public static List find(ModuleDefinition module, IEnumerable notInlinedMethods) { - var notInlinedMethodsDict = new Dictionary(); + public static List find(ModuleDefinition module, IEnumerable notInlinedMethods) { + var notInlinedMethodsDict = new Dictionary(); foreach (var method in notInlinedMethods) notInlinedMethodsDict[method] = true; - var inlinedMethods = new List(); + var inlinedMethods = new List(); foreach (var type in module.GetTypes()) { foreach (var method in type.Methods) { @@ -83,7 +83,7 @@ namespace de4dot.code.deobfuscators.Babel_NET { return changed; } - static bool canInline(MethodDefinition method) { + static bool canInline(MethodDef method) { if (!DotNetUtils.isMethod(method, "System.Int32", "(System.Int32)")) return false; if (!method.IsAssembly) @@ -94,12 +94,12 @@ namespace de4dot.code.deobfuscators.Babel_NET { return method.IsStatic; } - bool canInline2(MethodDefinition method) { + bool canInline2(MethodDef method) { return canInline(method) && method != blocks.Method; } bool inlineMethod(Instruction callInstr, int instrIndex) { - var methodToInline = callInstr.Operand as MethodDefinition; + var methodToInline = callInstr.Operand as MethodDef; if (methodToInline == null) return false; @@ -124,7 +124,7 @@ namespace de4dot.code.deobfuscators.Babel_NET { return true; } - bool getNewValue(MethodDefinition method, int arg, out int newValue) { + bool getNewValue(MethodDef method, int arg, out int newValue) { newValue = 0; emulator.init(method); emulator.setArg(method.Parameters[0], new Int32Value(arg)); diff --git a/de4dot.code/deobfuscators/Babel_NET/BabelUtils.cs b/de4dot.code/deobfuscators/Babel_NET/BabelUtils.cs index 4052e33c..6bd04a1c 100644 --- a/de4dot.code/deobfuscators/Babel_NET/BabelUtils.cs +++ b/de4dot.code/deobfuscators/Babel_NET/BabelUtils.cs @@ -19,24 +19,24 @@ using System.Collections.Generic; using System.Text; -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using de4dot.blocks; namespace de4dot.code.deobfuscators.Babel_NET { static class BabelUtils { - public static EmbeddedResource findEmbeddedResource(ModuleDefinition module, TypeDefinition decrypterType) { + public static EmbeddedResource findEmbeddedResource(ModuleDefinition module, TypeDef decrypterType) { return findEmbeddedResource(module, decrypterType, (method) => { }); } - public static EmbeddedResource findEmbeddedResource(ModuleDefinition module, TypeDefinition decrypterType, ISimpleDeobfuscator simpleDeobfuscator, IDeobfuscator deob) { + public static EmbeddedResource findEmbeddedResource(ModuleDefinition module, TypeDef decrypterType, ISimpleDeobfuscator simpleDeobfuscator, IDeobfuscator deob) { return findEmbeddedResource(module, decrypterType, (method) => { simpleDeobfuscator.deobfuscate(method); simpleDeobfuscator.decryptStrings(method, deob); }); } - public static EmbeddedResource findEmbeddedResource(ModuleDefinition module, TypeDefinition decrypterType, Action fixMethod) { + public static EmbeddedResource findEmbeddedResource(ModuleDefinition module, TypeDef decrypterType, Action fixMethod) { foreach (var method in decrypterType.Methods) { if (!DotNetUtils.isMethod(method, "System.String", "()")) continue; @@ -50,7 +50,7 @@ namespace de4dot.code.deobfuscators.Babel_NET { return null; } - static EmbeddedResource findEmbeddedResource1(ModuleDefinition module, MethodDefinition method) { + static EmbeddedResource findEmbeddedResource1(ModuleDefinition module, MethodDef method) { foreach (var s in DotNetUtils.getCodeStrings(method)) { var resource = DotNetUtils.getResource(module, s) as EmbeddedResource; if (resource != null) @@ -59,7 +59,7 @@ namespace de4dot.code.deobfuscators.Babel_NET { return null; } - static EmbeddedResource findEmbeddedResource2(ModuleDefinition module, MethodDefinition method) { + static EmbeddedResource findEmbeddedResource2(ModuleDefinition module, MethodDef method) { var strings = new List(DotNetUtils.getCodeStrings(method)); if (strings.Count != 1) return null; @@ -75,7 +75,7 @@ namespace de4dot.code.deobfuscators.Babel_NET { return DotNetUtils.getResource(module, sb.ToString()) as EmbeddedResource; } - static bool getXorKey2(MethodDefinition method, out int xorKey) { + static bool getXorKey2(MethodDef method, out int xorKey) { var instrs = method.Body.Instructions; for (int i = 0; i < instrs.Count - 2; i++) { var ldelem = instrs[i]; @@ -97,7 +97,7 @@ namespace de4dot.code.deobfuscators.Babel_NET { return false; } - public static bool findRegisterMethod(TypeDefinition type, out MethodDefinition regMethod, out MethodDefinition handler) { + public static bool findRegisterMethod(TypeDef type, out MethodDef regMethod, out MethodDef handler) { foreach (var method in type.Methods) { if (!method.IsStatic || method.Body == null) continue; diff --git a/de4dot.code/deobfuscators/Babel_NET/ConstantsDecrypter.cs b/de4dot.code/deobfuscators/Babel_NET/ConstantsDecrypter.cs index 49c1a6ff..2c7bcca7 100644 --- a/de4dot.code/deobfuscators/Babel_NET/ConstantsDecrypter.cs +++ b/de4dot.code/deobfuscators/Babel_NET/ConstantsDecrypter.cs @@ -21,8 +21,8 @@ using System; using System.Collections.Generic; using System.IO; using System.Runtime.Serialization.Formatters.Binary; -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using de4dot.blocks; namespace de4dot.code.deobfuscators.Babel_NET { @@ -30,12 +30,12 @@ namespace de4dot.code.deobfuscators.Babel_NET { ModuleDefinition module; ResourceDecrypter resourceDecrypter; InitializedDataCreator initializedDataCreator; - TypeDefinition decrypterType; - MethodDefinition int32Decrypter; - MethodDefinition int64Decrypter; - MethodDefinition singleDecrypter; - MethodDefinition doubleDecrypter; - MethodDefinition arrayDecrypter; + TypeDef decrypterType; + MethodDef int32Decrypter; + MethodDef int64Decrypter; + MethodDef singleDecrypter; + MethodDef doubleDecrypter; + MethodDef arrayDecrypter; EmbeddedResource encryptedResource; int[] decryptedInts; long[] decryptedLongs; @@ -54,27 +54,27 @@ namespace de4dot.code.deobfuscators.Babel_NET { get { return encryptedResource; } } - public TypeDefinition Type { + public TypeDef Type { get { return decrypterType; } } - public MethodDefinition Int32Decrypter { + public MethodDef Int32Decrypter { get { return int32Decrypter; } } - public MethodDefinition Int64Decrypter { + public MethodDef Int64Decrypter { get { return int64Decrypter; } } - public MethodDefinition SingleDecrypter { + public MethodDef SingleDecrypter { get { return singleDecrypter; } } - public MethodDefinition DoubleDecrypter { + public MethodDef DoubleDecrypter { get { return doubleDecrypter; } } - public MethodDefinition ArrayDecrypter { + public MethodDef ArrayDecrypter { get { return arrayDecrypter; } } @@ -99,7 +99,7 @@ namespace de4dot.code.deobfuscators.Babel_NET { } } - bool isConstantDecrypter(TypeDefinition type) { + bool isConstantDecrypter(TypeDef type) { if (type.HasEvents) return false; if (type.NestedTypes.Count != 1) @@ -131,7 +131,7 @@ namespace de4dot.code.deobfuscators.Babel_NET { "System.Single[]", "System.Double[]", }; - bool checkNestedFields(TypeDefinition nested) { + bool checkNestedFields(TypeDef nested) { if (!new FieldTypes(nested).all(requiredTypes)) return false; foreach (var field in nested.Fields) { @@ -193,11 +193,11 @@ namespace de4dot.code.deobfuscators.Babel_NET { } struct ArrayInfo { - public FieldDefinition encryptedField; + public FieldDef encryptedField; public ArrayType arrayType; public int start, len; - public ArrayInfo(int start, int len, FieldDefinition encryptedField, ArrayType arrayType) { + public ArrayInfo(int start, int len, FieldDef encryptedField, ArrayType arrayType) { this.start = start; this.len = len; this.encryptedField = encryptedField; @@ -232,7 +232,7 @@ namespace de4dot.code.deobfuscators.Babel_NET { var ldtoken = instrs[index++]; if (ldtoken.OpCode.Code != Code.Ldtoken) continue; - var field = ldtoken.Operand as FieldDefinition; + var field = ldtoken.Operand as FieldDef; if (field == null) continue; @@ -255,7 +255,7 @@ namespace de4dot.code.deobfuscators.Babel_NET { if (arrayType == null) continue; if (arrayType.ElementType.GetPrimitiveSize() == -1) { - Log.w("Can't decrypt non-primitive type array in method {0}", blocks.Method.MetadataToken.ToInt32()); + Log.w("Can't decrypt non-primitive type array in method {0}", blocks.Method.MDToken.ToInt32()); continue; } diff --git a/de4dot.code/deobfuscators/Babel_NET/Deobfuscator.cs b/de4dot.code/deobfuscators/Babel_NET/Deobfuscator.cs index da943936..806d8a90 100644 --- a/de4dot.code/deobfuscators/Babel_NET/Deobfuscator.cs +++ b/de4dot.code/deobfuscators/Babel_NET/Deobfuscator.cs @@ -19,7 +19,7 @@ using System.Collections.Generic; using System.Text.RegularExpressions; -using Mono.Cecil; +using dot10.DotNet; using de4dot.blocks; using de4dot.blocks.cflow; @@ -182,7 +182,7 @@ namespace de4dot.code.deobfuscators.Babel_NET { } } - void checkVersion(TypeDefinition attr) { + void checkVersion(TypeDef attr) { var versionField = DotNetUtils.getFieldByName(attr, "Version"); if (versionField != null && versionField.IsLiteral && versionField.Constant != null && versionField.Constant is string) { var val = Regex.Match((string)versionField.Constant, @"^(\d+\.\d+\.\d+\.\d+)$"); @@ -296,7 +296,7 @@ namespace de4dot.code.deobfuscators.Babel_NET { public override IEnumerable getStringDecrypterMethods() { var list = new List(); if (stringDecrypter.DecryptMethod != null) - list.Add(stringDecrypter.DecryptMethod.MetadataToken.ToInt32()); + list.Add(stringDecrypter.DecryptMethod.MDToken.ToInt32()); return list; } } diff --git a/de4dot.code/deobfuscators/Babel_NET/ImageReader.cs b/de4dot.code/deobfuscators/Babel_NET/ImageReader.cs index 59bd5994..c2ef95c9 100644 --- a/de4dot.code/deobfuscators/Babel_NET/ImageReader.cs +++ b/de4dot.code/deobfuscators/Babel_NET/ImageReader.cs @@ -22,8 +22,8 @@ using System.Collections.Generic; using System.Runtime.InteropServices; using System.IO; using System.Text; -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using de4dot.blocks; namespace de4dot.code.deobfuscators.Babel_NET { @@ -104,7 +104,7 @@ namespace de4dot.code.deobfuscators.Babel_NET { initializeTypeReferences(typeReferencesOffset); } - public void restore(string name, MethodDefinition method) { + public void restore(string name, MethodDef method) { var babelMethod = getMethod(name); var body = method.Body; @@ -170,10 +170,10 @@ namespace de4dot.code.deobfuscators.Babel_NET { return memberReferenceConverter.convert(fields[0]); } - static List getFields(TypeDefinition type, string name) { + static List getFields(TypeDef type, string name) { if (type == null) return null; - var fields = new List(); + var fields = new List(); foreach (var field in type.Fields) { if (field.Name == name) fields.Add(field); @@ -215,7 +215,7 @@ namespace de4dot.code.deobfuscators.Babel_NET { return methods[0]; } - List getMethods(TypeDefinition declaringType, BabelMethodreference babelMethodRef) { + List getMethods(TypeDef declaringType, BabelMethodreference babelMethodRef) { var methods = new List(); var git = babelMethodRef.DeclaringType as GenericInstanceType; @@ -257,9 +257,9 @@ namespace de4dot.code.deobfuscators.Babel_NET { return true; } - TypeDefinition resolve(TypeReference type) { - if (type is TypeDefinition) - return (TypeDefinition)type; + TypeDef resolve(TypeReference type) { + if (type is TypeDef) + return (TypeDef)type; if (type.IsGenericInstance) type = ((GenericInstanceType)type).ElementType; @@ -400,7 +400,7 @@ namespace de4dot.code.deobfuscators.Babel_NET { } bool isValueType(TypeReference typeRef) { - var typeDef = typeRef as TypeDefinition; + var typeDef = typeRef as TypeDef; if (typeDef != null) return typeDef.IsValueType; diff --git a/de4dot.code/deobfuscators/Babel_NET/InflaterCreator.cs b/de4dot.code/deobfuscators/Babel_NET/InflaterCreator.cs index 704f8b89..7420f226 100644 --- a/de4dot.code/deobfuscators/Babel_NET/InflaterCreator.cs +++ b/de4dot.code/deobfuscators/Babel_NET/InflaterCreator.cs @@ -18,17 +18,17 @@ */ using ICSharpCode.SharpZipLib.Zip.Compression; -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using de4dot.blocks; namespace de4dot.code.deobfuscators.Babel_NET { class InflaterCreator { - public static Inflater create(MethodDefinition method, bool noHeader) { + public static Inflater create(MethodDef method, bool noHeader) { return create(findInflaterType(method), noHeader); } - public static Inflater create(TypeDefinition inflaterType, bool noHeader) { + public static Inflater create(TypeDef inflaterType, bool noHeader) { if (inflaterType == null) return createNormal(noHeader); var initHeaderMethod = findInitHeaderMethod(inflaterType); @@ -50,13 +50,13 @@ namespace de4dot.code.deobfuscators.Babel_NET { return new Inflater(noHeader); } - static TypeDefinition findInflaterType(MethodDefinition method) { + static TypeDef findInflaterType(MethodDef method) { if (method == null || method.Body == null) return null; foreach (var instr in method.Body.Instructions) { if (instr.OpCode.Code != Code.Call) continue; - var calledMethod = instr.Operand as MethodDefinition; + var calledMethod = instr.Operand as MethodDef; if (calledMethod == null || !calledMethod.IsStatic) continue; @@ -70,7 +70,7 @@ namespace de4dot.code.deobfuscators.Babel_NET { return null; } - static MethodDefinition findInitHeaderMethod(TypeDefinition inflaterType) { + static MethodDef findInitHeaderMethod(TypeDef inflaterType) { foreach (var nested in inflaterType.NestedTypes) { var method = findInitHeaderMethod2(nested); if (method != null) @@ -79,7 +79,7 @@ namespace de4dot.code.deobfuscators.Babel_NET { return null; } - static MethodDefinition findInitHeaderMethod2(TypeDefinition nested) { + static MethodDef findInitHeaderMethod2(TypeDef nested) { foreach (var method in nested.Methods) { if (method.IsStatic || method.Body == null) continue; @@ -92,7 +92,7 @@ namespace de4dot.code.deobfuscators.Babel_NET { return null; } - static int? getMagic(MethodDefinition method) { + static int? getMagic(MethodDef method) { if (method == null || method.Body == null) return null; var instrs = method.Body.Instructions; diff --git a/de4dot.code/deobfuscators/Babel_NET/MemberReferenceConverter.cs b/de4dot.code/deobfuscators/Babel_NET/MemberReferenceConverter.cs index 17239cc7..3664184c 100644 --- a/de4dot.code/deobfuscators/Babel_NET/MemberReferenceConverter.cs +++ b/de4dot.code/deobfuscators/Babel_NET/MemberReferenceConverter.cs @@ -18,7 +18,7 @@ */ using System; -using Mono.Cecil; +using dot10.DotNet; using de4dot.blocks; namespace de4dot.code.deobfuscators.Babel_NET { @@ -35,7 +35,7 @@ namespace de4dot.code.deobfuscators.Babel_NET { public TypeReference convert(TypeReference a) { var newOne = update(a); - if (!(a is GenericParameter) && !MemberReferenceHelper.compareTypes(newOne, a)) + if (!(a is GenericParam) && !MemberReferenceHelper.compareTypes(newOne, a)) throw new ApplicationException("Could not convert type reference"); return newOne; } @@ -46,7 +46,7 @@ namespace de4dot.code.deobfuscators.Babel_NET { var newTypeRef = new TypeReference(a.Namespace, a.Name, Module, memberReferenceConverter.convert(a.Scope), a.IsValueType); foreach (var gp in a.GenericParameters) - newTypeRef.GenericParameters.Add(new GenericParameter(gp.Name, newTypeRef)); + newTypeRef.GenericParameters.Add(new GenericParam(gp.Name, newTypeRef)); newTypeRef.DeclaringType = update(a.DeclaringType); newTypeRef.UpdateElementType(); return newTypeRef; @@ -84,7 +84,7 @@ namespace de4dot.code.deobfuscators.Babel_NET { } public MethodReference convert(MethodReference methodRef) { - if (methodRef.GetType() != typeof(MethodReference) && methodRef.GetType() != typeof(MethodDefinition)) + if (methodRef.GetType() != typeof(MethodReference) && methodRef.GetType() != typeof(MethodDef)) throw new ApplicationException("Invalid method reference type"); if (isInOurModule(methodRef)) return tryGetMethodDefinition(methodRef); @@ -93,7 +93,7 @@ namespace de4dot.code.deobfuscators.Babel_NET { } public MethodReference copy(MethodReference methodRef) { - if (methodRef.GetType() != typeof(MethodReference) && methodRef.GetType() != typeof(MethodDefinition)) + if (methodRef.GetType() != typeof(MethodReference) && methodRef.GetType() != typeof(MethodDef)) throw new ApplicationException("Invalid method reference type"); var newMethodRef = new MethodReference(methodRef.Name, convert(methodRef.MethodReturnType.ReturnType), convert(methodRef.DeclaringType)); @@ -103,7 +103,7 @@ namespace de4dot.code.deobfuscators.Babel_NET { foreach (var param in methodRef.Parameters) newMethodRef.Parameters.Add(new ParameterDefinition(param.Name, param.Attributes, convert(param.ParameterType))); foreach (var gp in methodRef.GenericParameters) - newMethodRef.GenericParameters.Add(new GenericParameter(gp.Name, newMethodRef)); + newMethodRef.GenericParameters.Add(new GenericParam(gp.Name, newMethodRef)); return newMethodRef; } @@ -139,7 +139,7 @@ namespace de4dot.code.deobfuscators.Babel_NET { } public FieldReference tryGetFieldDefinition(FieldReference fieldRef) { - var fieldDef = fieldRef as FieldDefinition; + var fieldDef = fieldRef as FieldDef; if (fieldDef != null) return fieldDef; @@ -150,7 +150,7 @@ namespace de4dot.code.deobfuscators.Babel_NET { } public MethodReference tryGetMethodDefinition(MethodReference methodRef) { - var methodDef = methodRef as MethodDefinition; + var methodDef = methodRef as MethodDef; if (methodDef != null) return methodDef; diff --git a/de4dot.code/deobfuscators/Babel_NET/MethodBodyReader.cs b/de4dot.code/deobfuscators/Babel_NET/MethodBodyReader.cs index b548b285..270fb758 100644 --- a/de4dot.code/deobfuscators/Babel_NET/MethodBodyReader.cs +++ b/de4dot.code/deobfuscators/Babel_NET/MethodBodyReader.cs @@ -19,8 +19,8 @@ using System; using System.IO; -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; namespace de4dot.code.deobfuscators.Babel_NET { class MethodBodyReader : MethodBodyReaderBase { diff --git a/de4dot.code/deobfuscators/Babel_NET/MethodReferenceReader.cs b/de4dot.code/deobfuscators/Babel_NET/MethodReferenceReader.cs index 5988c41d..443d598a 100644 --- a/de4dot.code/deobfuscators/Babel_NET/MethodReferenceReader.cs +++ b/de4dot.code/deobfuscators/Babel_NET/MethodReferenceReader.cs @@ -20,8 +20,8 @@ using System; using System.Collections.Generic; using System.IO; -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using Mono.Collections.Generic; namespace de4dot.code.deobfuscators.Babel_NET { @@ -49,7 +49,7 @@ namespace de4dot.code.deobfuscators.Babel_NET { get { return new Collection(GenericArguments); } } - MetadataToken IMetadataTokenProvider.MetadataToken { + MetadataToken IMetadataTokenProvider.MDToken { get { throw new NotImplementedException(); } set { throw new NotImplementedException(); } } diff --git a/de4dot.code/deobfuscators/Babel_NET/MethodsDecrypter.cs b/de4dot.code/deobfuscators/Babel_NET/MethodsDecrypter.cs index 09075dbb..131d7cdd 100644 --- a/de4dot.code/deobfuscators/Babel_NET/MethodsDecrypter.cs +++ b/de4dot.code/deobfuscators/Babel_NET/MethodsDecrypter.cs @@ -20,8 +20,8 @@ using System; using System.Collections.Generic; using System.IO; -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using de4dot.blocks; namespace de4dot.code.deobfuscators.Babel_NET { @@ -30,9 +30,9 @@ namespace de4dot.code.deobfuscators.Babel_NET { ResourceDecrypter resourceDecrypter; IDeobfuscatorContext deobfuscatorContext; Dictionary imageReaders = new Dictionary(StringComparer.Ordinal); - TypeDefinition methodsDecrypterCreator; - TypeDefinition methodsDecrypter; - MethodDefinition decryptExecuteMethod; + TypeDef methodsDecrypterCreator; + TypeDef methodsDecrypter; + MethodDef decryptExecuteMethod; EmbeddedResource encryptedResource; public bool Detected { @@ -73,7 +73,7 @@ namespace de4dot.code.deobfuscators.Babel_NET { } } - static MethodDefinition findDecryptMethod(TypeDefinition type) { + static MethodDef findDecryptMethod(TypeDef type) { foreach (var method in type.Methods) { var decryptMethod = ResourceDecrypter.findDecrypterMethod(method); if (decryptMethod != null) @@ -82,7 +82,7 @@ namespace de4dot.code.deobfuscators.Babel_NET { return null; } - TypeDefinition findMethodsDecrypterType(TypeDefinition type) { + TypeDef findMethodsDecrypterType(TypeDef type) { foreach (var field in type.Fields) { var fieldType = DotNetUtils.getType(module, field.FieldType); if (fieldType == null) @@ -124,7 +124,7 @@ namespace de4dot.code.deobfuscators.Babel_NET { class EncryptInfo { public string encryptedMethodName; public string feature; - public MethodDefinition method; + public MethodDef method; public string FullName { get { @@ -134,7 +134,7 @@ namespace de4dot.code.deobfuscators.Babel_NET { } } - public EncryptInfo(string encryptedMethodName, string feature, MethodDefinition method) { + public EncryptInfo(string encryptedMethodName, string feature, MethodDef method) { this.encryptedMethodName = encryptedMethodName; this.feature = feature; this.method = method; @@ -142,9 +142,9 @@ namespace de4dot.code.deobfuscators.Babel_NET { public override string ToString() { if (feature != "") - return string.Format("{0}:{1} {2:X8}", feature, encryptedMethodName, method.MetadataToken.ToInt32()); + return string.Format("{0}:{1} {2:X8}", feature, encryptedMethodName, method.MDToken.ToInt32()); else - return string.Format("{0} {1:X8}", encryptedMethodName, method.MetadataToken.ToInt32()); + return string.Format("{0} {1:X8}", encryptedMethodName, method.MDToken.ToInt32()); } } @@ -158,7 +158,7 @@ namespace de4dot.code.deobfuscators.Babel_NET { numNonDecryptedMethods++; continue; } - Log.v("Decrypting method {0:X8}", info.method.MetadataToken.ToInt32()); + Log.v("Decrypting method {0:X8}", info.method.MDToken.ToInt32()); imageReader.restore(info.FullName, info.method); } if (numNonDecryptedMethods > 0) @@ -214,7 +214,7 @@ namespace de4dot.code.deobfuscators.Babel_NET { return infos; } - bool checkEncryptedMethod(MethodDefinition method, out EncryptInfo info) { + bool checkEncryptedMethod(MethodDef method, out EncryptInfo info) { info = null; if (method.Body == null) return false; @@ -237,7 +237,7 @@ namespace de4dot.code.deobfuscators.Babel_NET { return true; } - bool callsExecuteMethod(MethodDefinition method) { + bool callsExecuteMethod(MethodDef method) { foreach (var instr in method.Body.Instructions) { if (instr.OpCode.Code != Code.Call && instr.OpCode.Code != Code.Callvirt) continue; diff --git a/de4dot.code/deobfuscators/Babel_NET/ProxyCallFixer.cs b/de4dot.code/deobfuscators/Babel_NET/ProxyCallFixer.cs index af729d1a..68dc3e11 100644 --- a/de4dot.code/deobfuscators/Babel_NET/ProxyCallFixer.cs +++ b/de4dot.code/deobfuscators/Babel_NET/ProxyCallFixer.cs @@ -19,8 +19,8 @@ using System; using System.Collections.Generic; -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using de4dot.blocks; namespace de4dot.code.deobfuscators.Babel_NET { @@ -54,7 +54,7 @@ namespace de4dot.code.deobfuscators.Babel_NET { get { return true; } } - protected override object checkCctor(TypeDefinition type, MethodDefinition cctor) { + protected override object checkCctor(TypeDef type, MethodDef cctor) { var instructions = cctor.Body.Instructions; for (int i = 0; i < instructions.Count; i++) { TypeReference delegateType; @@ -95,7 +95,7 @@ namespace de4dot.code.deobfuscators.Babel_NET { return null; } - protected override void getCallInfo(object context, FieldDefinition field, out MethodReference calledMethod, out OpCode callOpcode) { + protected override void getCallInfo(object context, FieldDef field, out MethodReference calledMethod, out OpCode callOpcode) { var ctx = (Context)context; switch (ctx.proxyCreatorType) { @@ -141,7 +141,7 @@ namespace de4dot.code.deobfuscators.Babel_NET { } } - ProxyCreatorType getProxyCreatorType(MethodDefinition methodToCheck) { + ProxyCreatorType getProxyCreatorType(MethodDef methodToCheck) { foreach (var calledMethod in DotNetUtils.getCalledMethods(module, methodToCheck)) { if (!calledMethod.IsStatic || calledMethod.Body == null) continue; diff --git a/de4dot.code/deobfuscators/Babel_NET/ResourceDecrypter.cs b/de4dot.code/deobfuscators/Babel_NET/ResourceDecrypter.cs index 0abaad94..602ebdc5 100644 --- a/de4dot.code/deobfuscators/Babel_NET/ResourceDecrypter.cs +++ b/de4dot.code/deobfuscators/Babel_NET/ResourceDecrypter.cs @@ -20,8 +20,8 @@ using System; using System.IO; using ICSharpCode.SharpZipLib.Zip.Compression; -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using de4dot.blocks; namespace de4dot.code.deobfuscators.Babel_NET { @@ -42,7 +42,7 @@ namespace de4dot.code.deobfuscators.Babel_NET { class ResourceDecrypter { ModuleDefinition module; ISimpleDeobfuscator simpleDeobfuscator; - MethodDefinition decryptMethod; + MethodDef decryptMethod; IDecrypter decrypter; public ResourceDecrypter(ModuleDefinition module, ISimpleDeobfuscator simpleDeobfuscator) { @@ -155,7 +155,7 @@ namespace de4dot.code.deobfuscators.Babel_NET { ModuleDefinition module; Inflater inflater; - public Decrypter3(ModuleDefinition module, MethodDefinition decryptMethod) { + public Decrypter3(ModuleDefinition module, MethodDef decryptMethod) { this.module = module; this.inflater = InflaterCreator.create(decryptMethod, true); } @@ -208,7 +208,7 @@ namespace de4dot.code.deobfuscators.Babel_NET { } } - public MethodDefinition DecryptMethod { + public MethodDef DecryptMethod { set { if (value == null) return; @@ -221,14 +221,14 @@ namespace de4dot.code.deobfuscators.Babel_NET { } } - public static MethodDefinition findDecrypterMethod(MethodDefinition method) { + public static MethodDef findDecrypterMethod(MethodDef method) { if (method == null || method.Body == null) return null; foreach (var instr in method.Body.Instructions) { if (instr.OpCode.Code != Code.Call) continue; - var calledMethod = instr.Operand as MethodDefinition; + var calledMethod = instr.Operand as MethodDef; if (calledMethod == null || !calledMethod.IsStatic || calledMethod.Body == null) continue; if (!DotNetUtils.isMethod(calledMethod, "System.IO.MemoryStream", "(System.IO.Stream)")) diff --git a/de4dot.code/deobfuscators/Babel_NET/ResourceResolver.cs b/de4dot.code/deobfuscators/Babel_NET/ResourceResolver.cs index a1f30f3e..4289490d 100644 --- a/de4dot.code/deobfuscators/Babel_NET/ResourceResolver.cs +++ b/de4dot.code/deobfuscators/Babel_NET/ResourceResolver.cs @@ -20,8 +20,8 @@ using System; using System.Collections.Generic; using System.IO; -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using de4dot.blocks; namespace de4dot.code.deobfuscators.Babel_NET { @@ -29,8 +29,8 @@ namespace de4dot.code.deobfuscators.Babel_NET { ModuleDefinition module; ResourceDecrypter resourceDecrypter; ISimpleDeobfuscator simpleDeobfuscator; - TypeDefinition resolverType; - MethodDefinition registerMethod; + TypeDef resolverType; + MethodDef registerMethod; EmbeddedResource encryptedResource; bool hasXorKeys; int xorKey1, xorKey2; @@ -39,11 +39,11 @@ namespace de4dot.code.deobfuscators.Babel_NET { get { return resolverType != null; } } - public TypeDefinition Type { + public TypeDef Type { get { return resolverType; } } - public MethodDefinition InitMethod { + public MethodDef InitMethod { get { return registerMethod; } } @@ -66,7 +66,7 @@ namespace de4dot.code.deobfuscators.Babel_NET { if (!new FieldTypes(type).all(requiredTypes)) continue; - MethodDefinition regMethod, handler; + MethodDef regMethod, handler; if (!BabelUtils.findRegisterMethod(type, out regMethod, out handler)) continue; @@ -87,7 +87,7 @@ namespace de4dot.code.deobfuscators.Babel_NET { } } - static MethodDefinition findDecryptMethod(TypeDefinition type) { + static MethodDef findDecryptMethod(TypeDef type) { foreach (var method in type.Methods) { if (!DotNetUtils.isMethod(method, "System.Reflection.Assembly", "(System.IO.Stream)")) continue; @@ -96,7 +96,7 @@ namespace de4dot.code.deobfuscators.Babel_NET { return null; } - void initXorKeys(MethodDefinition method) { + void initXorKeys(MethodDef method) { simpleDeobfuscator.deobfuscate(method); var ints = new List(); var instrs = method.Body.Instructions; diff --git a/de4dot.code/deobfuscators/Babel_NET/StringDecrypter.cs b/de4dot.code/deobfuscators/Babel_NET/StringDecrypter.cs index e6f0a552..ade3f060 100644 --- a/de4dot.code/deobfuscators/Babel_NET/StringDecrypter.cs +++ b/de4dot.code/deobfuscators/Babel_NET/StringDecrypter.cs @@ -21,8 +21,8 @@ using System; using System.Collections.Generic; using System.IO; using System.Text; -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using de4dot.blocks; using de4dot.blocks.cflow; @@ -31,12 +31,12 @@ namespace de4dot.code.deobfuscators.Babel_NET { ModuleDefinition module; ResourceDecrypter resourceDecrypter; ISimpleDeobfuscator simpleDeobfuscator; - TypeDefinition decrypterType; + TypeDef decrypterType; EmbeddedResource encryptedResource; IDecrypterInfo decrypterInfo; interface IDecrypterInfo { - MethodDefinition Decrypter { get; } + MethodDef Decrypter { get; } bool NeedsResource { get; } void initialize(ModuleDefinition module, EmbeddedResource resource); string decrypt(object[] args); @@ -44,7 +44,7 @@ namespace de4dot.code.deobfuscators.Babel_NET { // Babel .NET 2.x class DecrypterInfoV1 : IDecrypterInfo { - public MethodDefinition Decrypter { get; set; } + public MethodDef Decrypter { get; set; } public bool NeedsResource { get { return false; } } @@ -67,7 +67,7 @@ namespace de4dot.code.deobfuscators.Babel_NET { class DecrypterInfoV2 : IDecrypterInfo { byte[] key; - public MethodDefinition Decrypter { get; set; } + public MethodDef Decrypter { get; set; } public bool NeedsResource { get { return true; } } @@ -97,7 +97,7 @@ namespace de4dot.code.deobfuscators.Babel_NET { InstructionEmulator emulator = new InstructionEmulator(); public IList OffsetCalcInstructions { get; set; } - public MethodDefinition Decrypter { get; set; } + public MethodDef Decrypter { get; set; } public bool NeedsResource { get { return true; } } @@ -113,12 +113,12 @@ namespace de4dot.code.deobfuscators.Babel_NET { offsetToString[getOffset((int)reader.BaseStream.Position)] = reader.ReadString(); } - MethodDefinition dummyMethod; + MethodDef dummyMethod; int getOffset(int offset) { if (OffsetCalcInstructions == null || OffsetCalcInstructions.Count == 0) return offset; if (dummyMethod == null) { - dummyMethod = new MethodDefinition("", 0, new TypeReference("", "", null, null)); + dummyMethod = new MethodDef("", 0, new TypeReference("", "", null, null)); dummyMethod.Body = new MethodBody(dummyMethod); } emulator.init(dummyMethod); @@ -141,11 +141,11 @@ namespace de4dot.code.deobfuscators.Babel_NET { get { return decrypterType != null; } } - public TypeDefinition Type { + public TypeDef Type { get { return decrypterType; } } - public MethodDefinition DecryptMethod { + public MethodDef DecryptMethod { get { return decrypterInfo == null ? null : decrypterInfo.Decrypter; } } @@ -171,7 +171,7 @@ namespace de4dot.code.deobfuscators.Babel_NET { } } - IDecrypterInfo checkDecrypterType(TypeDefinition type) { + IDecrypterInfo checkDecrypterType(TypeDef type) { if (type.HasEvents) return null; if (type.NestedTypes.Count > 2) @@ -188,7 +188,7 @@ namespace de4dot.code.deobfuscators.Babel_NET { return checkDecrypterTypeBabel2x(type); } - IDecrypterInfo checkDecrypterTypeBabel2x(TypeDefinition type) { + IDecrypterInfo checkDecrypterTypeBabel2x(TypeDef type) { if (type.HasEvents || type.HasProperties || type.HasNestedTypes) return null; if (type.HasFields || type.Methods.Count != 1) @@ -200,7 +200,7 @@ namespace de4dot.code.deobfuscators.Babel_NET { return new DecrypterInfoV1 { Decrypter = decrypter }; } - bool checkDecryptMethodBabel2x(MethodDefinition method) { + bool checkDecryptMethodBabel2x(MethodDef method) { if (!method.IsStatic || !method.IsPublic) return false; if (method.Body == null) @@ -242,7 +242,7 @@ namespace de4dot.code.deobfuscators.Babel_NET { return stringLength == 1 && stringToCharArray == 1 && stringCtor == 1; } - IDecrypterInfo checkNested(TypeDefinition type, TypeDefinition nested) { + IDecrypterInfo checkNested(TypeDef type, TypeDef nested) { if (nested.HasProperties || nested.HasEvents) return null; @@ -309,7 +309,7 @@ namespace de4dot.code.deobfuscators.Babel_NET { } class ReflectionToCecilMethodCreator { - MethodDefinition method; + MethodDef method; List instructions = new List(); InstructionEmulator emulator; int index; @@ -330,7 +330,7 @@ namespace de4dot.code.deobfuscators.Babel_NET { get { return instructions; } } - public ReflectionToCecilMethodCreator(MethodDefinition method) { + public ReflectionToCecilMethodCreator(MethodDef method) { this.method = method; this.emulator = new InstructionEmulator(method); } @@ -466,7 +466,7 @@ namespace de4dot.code.deobfuscators.Babel_NET { } } - static List getOffsetCalcInstructions(MethodDefinition method) { + static List getOffsetCalcInstructions(MethodDef method) { var creator = new ReflectionToCecilMethodCreator(method); creator.create(); var instrs = creator.Instructions; @@ -499,7 +499,7 @@ namespace de4dot.code.deobfuscators.Babel_NET { return -1; } - static bool hasFieldType(IEnumerable fields, TypeReference fieldType) { + static bool hasFieldType(IEnumerable fields, TypeReference fieldType) { foreach (var field in fields) { if (MemberReferenceHelper.compareTypes(field.FieldType, fieldType)) return true; @@ -507,7 +507,7 @@ namespace de4dot.code.deobfuscators.Babel_NET { return false; } - static int getOffsetMagic(MethodDefinition method) { + static int getOffsetMagic(MethodDef method) { var instrs = method.Body.Instructions; for (int i = 0; i < instrs.Count - 4; i++) { int index = i; @@ -549,7 +549,7 @@ namespace de4dot.code.deobfuscators.Babel_NET { return 0; } - bool checkFields(TypeDefinition type, string fieldType1, TypeDefinition fieldType2) { + bool checkFields(TypeDef type, string fieldType1, TypeDef fieldType2) { if (type.Fields.Count != 2) return false; if (type.Fields[0].FieldType.FullName != fieldType1 && diff --git a/de4dot.code/deobfuscators/CliSecure/CliSecureRtType.cs b/de4dot.code/deobfuscators/CliSecure/CliSecureRtType.cs index a8cae51a..f894fbc7 100644 --- a/de4dot.code/deobfuscators/CliSecure/CliSecureRtType.cs +++ b/de4dot.code/deobfuscators/CliSecure/CliSecureRtType.cs @@ -20,41 +20,41 @@ using System; using System.Collections.Generic; using System.IO; -using Mono.Cecil; +using dot10.DotNet; using de4dot.blocks; using de4dot.PE; namespace de4dot.code.deobfuscators.CliSecure { class CliSecureRtType { ModuleDefinition module; - TypeDefinition cliSecureRtType; - MethodDefinition postInitializeMethod; - MethodDefinition initializeMethod; - MethodDefinition stringDecrypterMethod; - MethodDefinition loadMethod; + TypeDef cliSecureRtType; + MethodDef postInitializeMethod; + MethodDef initializeMethod; + MethodDef stringDecrypterMethod; + MethodDef loadMethod; bool foundSig; public bool Detected { get { return foundSig || cliSecureRtType != null; } } - public TypeDefinition Type { + public TypeDef Type { get { return cliSecureRtType; } } - public MethodDefinition StringDecrypterMethod { + public MethodDef StringDecrypterMethod { get { return stringDecrypterMethod; } } - public MethodDefinition PostInitializeMethod { + public MethodDef PostInitializeMethod { get { return postInitializeMethod; } } - public MethodDefinition InitializeMethod { + public MethodDef InitializeMethod { get { return initializeMethod; } } - public MethodDefinition LoadMethod { + public MethodDef LoadMethod { get { return loadMethod; } } @@ -165,7 +165,7 @@ namespace de4dot.code.deobfuscators.CliSecure { return false; } - static MethodDefinition findStringDecrypterMethod(TypeDefinition type) { + static MethodDef findStringDecrypterMethod(TypeDef type) { foreach (var method in type.Methods) { if (method.Body == null || !method.IsStatic) continue; @@ -178,7 +178,7 @@ namespace de4dot.code.deobfuscators.CliSecure { return null; } - static MethodDefinition findMethod(TypeDefinition type, string returnType, string name, string parameters) { + static MethodDef findMethod(TypeDef type, string returnType, string name, string parameters) { var methodName = returnType + " " + type.FullName + "::" + name + parameters; foreach (var method in type.Methods) { if (method.Body == null || !method.IsStatic) @@ -192,7 +192,7 @@ namespace de4dot.code.deobfuscators.CliSecure { return null; } - static bool hasInitializeMethod(TypeDefinition type, string name) { + static bool hasInitializeMethod(TypeDef type, string name) { var method = DotNetUtils.getPInvokeMethod(type, name); if (method == null) return false; @@ -239,7 +239,7 @@ namespace de4dot.code.deobfuscators.CliSecure { } } - static bool isOldStringDecrypterMethod(MethodDefinition method) { + static bool isOldStringDecrypterMethod(MethodDef method) { if (method == null || method.Body == null || !method.IsStatic) return false; if (!DotNetUtils.isMethod(method, "System.String", "(System.String)")) diff --git a/de4dot.code/deobfuscators/CliSecure/Deobfuscator.cs b/de4dot.code/deobfuscators/CliSecure/Deobfuscator.cs index a9820940..18ce06f8 100644 --- a/de4dot.code/deobfuscators/CliSecure/Deobfuscator.cs +++ b/de4dot.code/deobfuscators/CliSecure/Deobfuscator.cs @@ -19,7 +19,7 @@ using System; using System.Collections.Generic; -using Mono.Cecil; +using dot10.DotNet; using Mono.MyStuff; using de4dot.blocks; using de4dot.PE; @@ -78,7 +78,7 @@ namespace de4dot.code.deobfuscators.CliSecure { Options options; string obfuscatorName = DeobfuscatorInfo.THE_NAME; - List cliSecureAttributes = new List(); + List cliSecureAttributes = new List(); ProxyCallFixer proxyCallFixer; CliSecureRtType cliSecureRtType; StringDecrypter stringDecrypter; @@ -233,8 +233,8 @@ namespace de4dot.code.deobfuscators.CliSecure { return newOne; } - static List lookup(ModuleDefinition module, List types, string errorMsg) { - var list = new List(types.Count); + static List lookup(ModuleDefinition module, List types, string errorMsg) { + var list = new List(types.Count); foreach (var type in types) list.Add(DeobUtils.lookup(module, type, errorMsg)); return list; @@ -320,7 +320,7 @@ namespace de4dot.code.deobfuscators.CliSecure { public override IEnumerable getStringDecrypterMethods() { var list = new List(); if (stringDecrypter.Method != null) - list.Add(stringDecrypter.Method.MetadataToken.ToInt32()); + list.Add(stringDecrypter.Method.MDToken.ToInt32()); return list; } diff --git a/de4dot.code/deobfuscators/CliSecure/MethodsDecrypter.cs b/de4dot.code/deobfuscators/CliSecure/MethodsDecrypter.cs index 4f73e063..6134f638 100644 --- a/de4dot.code/deobfuscators/CliSecure/MethodsDecrypter.cs +++ b/de4dot.code/deobfuscators/CliSecure/MethodsDecrypter.cs @@ -458,7 +458,7 @@ namespace de4dot.code.deobfuscators.CliSecure { var initMethod = csRtType.InitializeMethod; if (initMethod == null) return null; - uint initToken = initMethod.MetadataToken.ToUInt32(); + uint initToken = initMethod.MDToken.ToUInt32(); var moduleCctorBytes = new byte[6]; moduleCctorBytes[0] = 0x28; // call moduleCctorBytes[1] = (byte)initToken; @@ -542,7 +542,7 @@ namespace de4dot.code.deobfuscators.CliSecure { var dm = new DumpedMethod(); dm.token = 0x06000001 + (uint)i; - var method = (Mono.Cecil.MethodDefinition)module.LookupToken((int)dm.token); + var method = (Mono.Cecil.MethodDef)module.LookupToken((int)dm.token); if (method == null || method.DeclaringType == DotNetUtils.getModuleType(module)) continue; diff --git a/de4dot.code/deobfuscators/CliSecure/ProxyCallFixer.cs b/de4dot.code/deobfuscators/CliSecure/ProxyCallFixer.cs index b7362f02..9ad5f4ec 100644 --- a/de4dot.code/deobfuscators/CliSecure/ProxyCallFixer.cs +++ b/de4dot.code/deobfuscators/CliSecure/ProxyCallFixer.cs @@ -19,8 +19,8 @@ using System; using System.Collections.Generic; -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using de4dot.blocks; namespace de4dot.code.deobfuscators.CliSecure { @@ -49,19 +49,19 @@ namespace de4dot.code.deobfuscators.CliSecure { } } - protected override object checkCctor(ref TypeDefinition type, MethodDefinition cctor) { + protected override object checkCctor(ref TypeDef type, MethodDef cctor) { var instrs = cctor.Body.Instructions; if (instrs.Count != 3) return null; if (!DotNetUtils.isLdcI4(instrs[0].OpCode.Code)) return null; - if (instrs[1].OpCode != OpCodes.Call || !isDelegateCreatorMethod(instrs[1].Operand as MethodDefinition)) + if (instrs[1].OpCode != OpCodes.Call || !isDelegateCreatorMethod(instrs[1].Operand as MethodDef)) return null; if (instrs[2].OpCode != OpCodes.Ret) return null; int delegateToken = 0x02000001 + DotNetUtils.getLdcI4Value(instrs[0]); - if (type.MetadataToken.ToInt32() != delegateToken) { + if (type.MDToken.ToInt32() != delegateToken) { Log.w("Delegate token is not current type"); return null; } @@ -69,7 +69,7 @@ namespace de4dot.code.deobfuscators.CliSecure { return new object(); } - protected override void getCallInfo(object context, FieldDefinition field, out MethodReference calledMethod, out OpCode callOpcode) { + protected override void getCallInfo(object context, FieldDef field, out MethodReference calledMethod, out OpCode callOpcode) { if (memberReferences == null) memberReferences = new List(module.GetMemberReferences()); diff --git a/de4dot.code/deobfuscators/CliSecure/ResourceDecrypter.cs b/de4dot.code/deobfuscators/CliSecure/ResourceDecrypter.cs index 37118bf1..aeb2f34c 100644 --- a/de4dot.code/deobfuscators/CliSecure/ResourceDecrypter.cs +++ b/de4dot.code/deobfuscators/CliSecure/ResourceDecrypter.cs @@ -20,25 +20,25 @@ using System.IO; using System.Security.Cryptography; using System.Text; -using Mono.Cecil; +using dot10.DotNet; using de4dot.blocks; namespace de4dot.code.deobfuscators.CliSecure { class ResourceDecrypter { ModuleDefinition module; - TypeDefinition rsrcType; - MethodDefinition rsrcRrrMethod; - MethodDefinition rsrcResolveMethod; + TypeDef rsrcType; + MethodDef rsrcRrrMethod; + MethodDef rsrcResolveMethod; public bool Detected { get { return rsrcType != null; } } - public TypeDefinition Type { + public TypeDef Type { get { return rsrcType; } } - public MethodDefinition RsrcRrrMethod { + public MethodDef RsrcRrrMethod { get { return rsrcRrrMethod; } } diff --git a/de4dot.code/deobfuscators/CliSecure/StackFrameHelper.cs b/de4dot.code/deobfuscators/CliSecure/StackFrameHelper.cs index dc465d8e..76d760dd 100644 --- a/de4dot.code/deobfuscators/CliSecure/StackFrameHelper.cs +++ b/de4dot.code/deobfuscators/CliSecure/StackFrameHelper.cs @@ -18,16 +18,16 @@ */ using System; -using Mono.Cecil; +using dot10.DotNet; using de4dot.blocks; namespace de4dot.code.deobfuscators.CliSecure { class StackFrameHelper { ModuleDefinition module; - TypeDefinition stackFrameHelperType; + TypeDef stackFrameHelperType; ExceptionLoggerRemover exceptionLoggerRemover = new ExceptionLoggerRemover(); - public TypeDefinition Type { + public TypeDef Type { get { return stackFrameHelperType; } } @@ -46,7 +46,7 @@ namespace de4dot.code.deobfuscators.CliSecure { if (type.Methods.Count > 3) continue; - MethodDefinition errorMethod = null; + MethodDef errorMethod = null; foreach (var method in type.Methods) { if (method.IsRuntimeSpecialName && method.Name == ".ctor" && !method.HasParameters) continue; // .ctor is allowed diff --git a/de4dot.code/deobfuscators/CliSecure/StringDecrypter.cs b/de4dot.code/deobfuscators/CliSecure/StringDecrypter.cs index ef3e5e5c..b8bbd156 100644 --- a/de4dot.code/deobfuscators/CliSecure/StringDecrypter.cs +++ b/de4dot.code/deobfuscators/CliSecure/StringDecrypter.cs @@ -19,29 +19,29 @@ using System; using System.Text; -using Mono.Cecil; +using dot10.DotNet; namespace de4dot.code.deobfuscators.CliSecure { class StringDecrypter { ModuleDefinition module; - TypeDefinition stringDecrypterType; - MethodDefinition stringDecrypterMethod; + TypeDef stringDecrypterType; + MethodDef stringDecrypterMethod; byte[] stringDecrypterKey; public bool Detected { get { return stringDecrypterMethod != null; } } - public TypeDefinition Type { + public TypeDef Type { get { return stringDecrypterType; } } - public MethodDefinition Method { + public MethodDef Method { get { return stringDecrypterMethod; } set { stringDecrypterMethod = value; } } - public StringDecrypter(ModuleDefinition module, MethodDefinition stringDecrypterMethod) { + public StringDecrypter(ModuleDefinition module, MethodDef stringDecrypterMethod) { this.module = module; this.stringDecrypterMethod = stringDecrypterMethod; } diff --git a/de4dot.code/deobfuscators/CliSecure/vm/CilOperandInstructionRestorer.cs b/de4dot.code/deobfuscators/CliSecure/vm/CilOperandInstructionRestorer.cs index 4a1b48e3..32d770a1 100644 --- a/de4dot.code/deobfuscators/CliSecure/vm/CilOperandInstructionRestorer.cs +++ b/de4dot.code/deobfuscators/CliSecure/vm/CilOperandInstructionRestorer.cs @@ -17,8 +17,8 @@ along with de4dot. If not, see . */ -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using Mono.Cecil.Metadata; using de4dot.blocks; @@ -28,9 +28,9 @@ namespace de4dot.code.deobfuscators.CliSecure.vm { // ldobj // stobj class CilOperandInstructionRestorer { - MethodDefinition method; + MethodDef method; - public bool restore(MethodDefinition method) { + public bool restore(MethodDef method) { this.method = method; bool atLeastOneFailed = false; @@ -99,13 +99,13 @@ namespace de4dot.code.deobfuscators.CliSecure.vm { case CecilType.ArrayType: case CecilType.GenericInstanceType: case CecilType.PointerType: - case CecilType.TypeDefinition: + case CecilType.TypeDef: case CecilType.TypeReference: case CecilType.FunctionPointerType: break; - case CecilType.GenericParameter: - var gp = (GenericParameter)type; + case CecilType.GenericParam: + var gp = (GenericParam)type; if (method.DeclaringType != gp.Owner && method != gp.Owner) return false; break; diff --git a/de4dot.code/deobfuscators/CliSecure/vm/Csvm.cs b/de4dot.code/deobfuscators/CliSecure/vm/Csvm.cs index 234ea7cb..7f4f7755 100644 --- a/de4dot.code/deobfuscators/CliSecure/vm/Csvm.cs +++ b/de4dot.code/deobfuscators/CliSecure/vm/Csvm.cs @@ -20,7 +20,7 @@ using System; using System.Collections.Generic; using System.IO; -using Mono.Cecil; +using dot10.DotNet; using de4dot.blocks; namespace de4dot.code.deobfuscators.CliSecure.vm { @@ -98,17 +98,17 @@ namespace de4dot.code.deobfuscators.CliSecure.vm { var converter = new CsvmToCilMethodConverter(deobfuscatorContext, module, opcodeDetector); var methodPrinter = new MethodPrinter(); foreach (var csvmMethod in csvmMethods) { - var cilMethod = module.LookupToken(csvmMethod.Token) as MethodDefinition; + var cilMethod = module.LookupToken(csvmMethod.Token) as MethodDef; if (cilMethod == null) throw new ApplicationException(string.Format("Could not find method {0:X8}", csvmMethod.Token)); converter.convert(cilMethod, csvmMethod); - Log.v("Restored method {0:X8}", cilMethod.MetadataToken.ToInt32()); + Log.v("Restored method {0:X8}", cilMethod.MDToken.ToInt32()); printMethod(methodPrinter, cilMethod); } Log.deIndent(); } - static void printMethod(MethodPrinter methodPrinter, MethodDefinition method) { + static void printMethod(MethodPrinter methodPrinter, MethodDef method) { const Log.LogLevel dumpLogLevel = Log.LogLevel.verbose; if (!Log.isAtLeast(dumpLogLevel)) return; diff --git a/de4dot.code/deobfuscators/CliSecure/vm/CsvmDataReader.cs b/de4dot.code/deobfuscators/CliSecure/vm/CsvmDataReader.cs index b3dd1ccd..b6bd7915 100644 --- a/de4dot.code/deobfuscators/CliSecure/vm/CsvmDataReader.cs +++ b/de4dot.code/deobfuscators/CliSecure/vm/CsvmDataReader.cs @@ -20,7 +20,7 @@ using System; using System.Collections.Generic; using System.IO; -using Mono.Cecil; +using dot10.DotNet; using de4dot.blocks; namespace de4dot.code.deobfuscators.CliSecure.vm { diff --git a/de4dot.code/deobfuscators/CliSecure/vm/CsvmToCilMethodConverter.cs b/de4dot.code/deobfuscators/CliSecure/vm/CsvmToCilMethodConverter.cs index d0179294..2ea5a91f 100644 --- a/de4dot.code/deobfuscators/CliSecure/vm/CsvmToCilMethodConverter.cs +++ b/de4dot.code/deobfuscators/CliSecure/vm/CsvmToCilMethodConverter.cs @@ -20,8 +20,8 @@ using System; using System.Collections.Generic; using System.IO; -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using Mono.Cecil.Metadata; using de4dot.blocks; @@ -38,7 +38,7 @@ namespace de4dot.code.deobfuscators.CliSecure.vm { this.opCodeDetector = opCodeDetector; } - public void convert(MethodDefinition cilMethod, CsvmMethodData csvmMethod) { + public void convert(MethodDef cilMethod, CsvmMethodData csvmMethod) { var newInstructions = readInstructions(cilMethod, csvmMethod); var newLocals = readLocals(cilMethod, csvmMethod); var newExceptions = readExceptions(cilMethod, csvmMethod, newInstructions); @@ -50,7 +50,7 @@ namespace de4dot.code.deobfuscators.CliSecure.vm { DotNetUtils.restoreBody(cilMethod, newInstructions, newExceptions); if (!operandRestorer.restore(cilMethod)) - Log.w("Failed to restore one or more instruction operands in CSVM method {0:X8}", cilMethod.MetadataToken.ToInt32()); + Log.w("Failed to restore one or more instruction operands in CSVM method {0:X8}", cilMethod.MDToken.ToInt32()); restoreConstrainedPrefix(cilMethod); } @@ -129,7 +129,7 @@ namespace de4dot.code.deobfuscators.CliSecure.vm { instr.Operand = operand; } - void fixArgs(IList instrs, MethodDefinition method) { + void fixArgs(IList instrs, MethodDef method) { foreach (var instr in instrs) { var op = instr.Operand as ArgOperand; if (op == null) @@ -206,7 +206,7 @@ namespace de4dot.code.deobfuscators.CliSecure.vm { } } - List readInstructions(MethodDefinition cilMethod, CsvmMethodData csvmMethod) { + List readInstructions(MethodDef cilMethod, CsvmMethodData csvmMethod) { var reader = new BinaryReader(new MemoryStream(csvmMethod.Instructions)); var instrs = new List(); int offset = 0; @@ -230,7 +230,7 @@ namespace de4dot.code.deobfuscators.CliSecure.vm { return instr.OpCode.Size + (op.targetDisplacements.Length + 1) * 4; } - List readLocals(MethodDefinition cilMethod, CsvmMethodData csvmMethod) { + List readLocals(MethodDef cilMethod, CsvmMethodData csvmMethod) { var locals = new List(); var reader = new BinaryReader(new MemoryStream(csvmMethod.Locals)); @@ -303,7 +303,7 @@ namespace de4dot.code.deobfuscators.CliSecure.vm { } } - List readExceptions(MethodDefinition cilMethod, CsvmMethodData csvmMethod, List cilInstructions) { + List readExceptions(MethodDef cilMethod, CsvmMethodData csvmMethod, List cilInstructions) { var reader = new BinaryReader(new MemoryStream(csvmMethod.Exceptions)); var ehs = new List(); @@ -408,7 +408,7 @@ namespace de4dot.code.deobfuscators.CliSecure.vm { return memberRef; } - static void restoreConstrainedPrefix(MethodDefinition method) { + static void restoreConstrainedPrefix(MethodDef method) { if (method == null || method.Body == null) return; diff --git a/de4dot.code/deobfuscators/CliSecure/vm/FieldsInfo.cs b/de4dot.code/deobfuscators/CliSecure/vm/FieldsInfo.cs index f4ba58e3..7dbc409e 100644 --- a/de4dot.code/deobfuscators/CliSecure/vm/FieldsInfo.cs +++ b/de4dot.code/deobfuscators/CliSecure/vm/FieldsInfo.cs @@ -19,7 +19,7 @@ using System; using System.Collections.Generic; -using Mono.Cecil; +using dot10.DotNet; namespace de4dot.code.deobfuscators.CliSecure.vm { class FieldsInfo { @@ -27,13 +27,13 @@ namespace de4dot.code.deobfuscators.CliSecure.vm { Dictionary fieldTypes = new Dictionary(StringComparer.Ordinal); int numEnums = 0; - public FieldsInfo(TypeDefinition type) + public FieldsInfo(TypeDef type) : this(type.Fields) { } - public FieldsInfo(IEnumerable fields) { + public FieldsInfo(IEnumerable fields) { foreach (var field in fields) { - var fieldTypeDef = field.FieldType as TypeDefinition; + var fieldTypeDef = field.FieldType as TypeDef; if (fieldTypeDef != null && fieldTypeDef.IsEnum) addEnum(); else diff --git a/de4dot.code/deobfuscators/CliSecure/vm/OpCodeHandler.cs b/de4dot.code/deobfuscators/CliSecure/vm/OpCodeHandler.cs index f8a43ab5..e8d5e49e 100644 --- a/de4dot.code/deobfuscators/CliSecure/vm/OpCodeHandler.cs +++ b/de4dot.code/deobfuscators/CliSecure/vm/OpCodeHandler.cs @@ -21,8 +21,8 @@ using System; using System.Collections.Generic; using System.IO; using de4dot.blocks; -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using Mono.Cecil.Metadata; namespace de4dot.code.deobfuscators.CliSecure.vm { @@ -439,7 +439,7 @@ namespace de4dot.code.deobfuscators.CliSecure.vm { return isEmptyMethod(info.ReadMethod) && isEmptyMethod(info.ExecuteMethod); } - static bool isEmptyMethod(MethodDefinition method) { + static bool isEmptyMethod(MethodDef method) { foreach (var instr in method.Body.Instructions) { if (instr.OpCode.Code == Code.Ret) return true; diff --git a/de4dot.code/deobfuscators/CliSecure/vm/UnknownHandlerInfo.cs b/de4dot.code/deobfuscators/CliSecure/vm/UnknownHandlerInfo.cs index 9ce21539..40d9a24e 100644 --- a/de4dot.code/deobfuscators/CliSecure/vm/UnknownHandlerInfo.cs +++ b/de4dot.code/deobfuscators/CliSecure/vm/UnknownHandlerInfo.cs @@ -19,24 +19,24 @@ using System; using System.Collections.Generic; -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using de4dot.blocks; namespace de4dot.code.deobfuscators.CliSecure.vm { class UnknownHandlerInfo { - TypeDefinition type; + TypeDef type; CsvmInfo csvmInfo; FieldsInfo fieldsInfo; - MethodDefinition readMethod, executeMethod; + MethodDef readMethod, executeMethod; int numStaticMethods, numInstanceMethods, numVirtualMethods, numCtors; int executeMethodThrows, executeMethodPops; - public MethodDefinition ReadMethod { + public MethodDef ReadMethod { get { return readMethod; } } - public MethodDefinition ExecuteMethod { + public MethodDef ExecuteMethod { get { return executeMethod; } } @@ -64,7 +64,7 @@ namespace de4dot.code.deobfuscators.CliSecure.vm { get { return numCtors; } } - public UnknownHandlerInfo(TypeDefinition type, CsvmInfo csvmInfo) { + public UnknownHandlerInfo(TypeDef type, CsvmInfo csvmInfo) { this.type = type; this.csvmInfo = csvmInfo; fieldsInfo = new FieldsInfo(getFields(type)); @@ -74,11 +74,11 @@ namespace de4dot.code.deobfuscators.CliSecure.vm { executeMethodPops = countPops(executeMethod); } - static internal IEnumerable getFields(TypeDefinition type) { - var typeFields = new FieldDefinitionAndDeclaringTypeDict(); + static internal IEnumerable getFields(TypeDef type) { + var typeFields = new FieldDefinitionAndDeclaringTypeDict(); foreach (var field in type.Fields) typeFields.add(field, field); - var realFields = new Dictionary(); + var realFields = new Dictionary(); foreach (var method in type.Methods) { if (method.Body == null) continue; @@ -132,7 +132,7 @@ namespace de4dot.code.deobfuscators.CliSecure.vm { throw new ApplicationException("Could not find execute method"); } - static int countThrows(MethodDefinition method) { + static int countThrows(MethodDef method) { int count = 0; foreach (var instr in method.Body.Instructions) { if (instr.OpCode.Code == Code.Throw) @@ -141,7 +141,7 @@ namespace de4dot.code.deobfuscators.CliSecure.vm { return count; } - int countPops(MethodDefinition method) { + int countPops(MethodDef method) { int count = 0; foreach (var instr in method.Body.Instructions) { if (instr.OpCode.Code != Code.Call && instr.OpCode.Code != Code.Callvirt) diff --git a/de4dot.code/deobfuscators/CliSecure/vm/VmOpCodeHandlerDetector.cs b/de4dot.code/deobfuscators/CliSecure/vm/VmOpCodeHandlerDetector.cs index 639314e9..db9d3e5a 100644 --- a/de4dot.code/deobfuscators/CliSecure/vm/VmOpCodeHandlerDetector.cs +++ b/de4dot.code/deobfuscators/CliSecure/vm/VmOpCodeHandlerDetector.cs @@ -19,8 +19,8 @@ using System; using System.Collections.Generic; -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using de4dot.blocks; using de4dot.blocks.cflow; @@ -37,10 +37,10 @@ namespace de4dot.code.deobfuscators.CliSecure.vm { } class CsvmInfo { - public TypeDefinition StackValue { get; set; } - public TypeDefinition Stack { get; set; } - public MethodDefinition PopMethod { get; set; } - public MethodDefinition PeekMethod { get; set; } + public TypeDef StackValue { get; set; } + public TypeDef Stack { get; set; } + public MethodDef PopMethod { get; set; } + public MethodDef PeekMethod { get; set; } } class VmOpCodeHandlerDetector { @@ -73,7 +73,7 @@ namespace de4dot.code.deobfuscators.CliSecure.vm { return csvmInfo; } - TypeDefinition findStackValueType() { + TypeDef findStackValueType() { foreach (var type in module.Types) { if (isStackType(type)) return type; @@ -81,14 +81,14 @@ namespace de4dot.code.deobfuscators.CliSecure.vm { return null; } - static bool isStackType(TypeDefinition type) { + static bool isStackType(TypeDef type) { if (type.Fields.Count != 2) return false; int enumTypes = 0; int objectTypes = 0; foreach (var field in type.Fields) { - var fieldType = field.FieldType as TypeDefinition; + var fieldType = field.FieldType as TypeDef; if (fieldType != null && fieldType.IsEnum) enumTypes++; if (field.FieldType.FullName == "System.Object") @@ -100,7 +100,7 @@ namespace de4dot.code.deobfuscators.CliSecure.vm { return true; } - TypeDefinition findStackType(TypeDefinition stackValueType) { + TypeDef findStackType(TypeDef stackValueType) { foreach (var type in module.Types) { if (isStackType(type, stackValueType)) return type; @@ -108,7 +108,7 @@ namespace de4dot.code.deobfuscators.CliSecure.vm { return null; } - bool isStackType(TypeDefinition type, TypeDefinition stackValueType) { + bool isStackType(TypeDef type, TypeDef stackValueType) { if (type.Interfaces.Count != 2) return false; if (!implementsInterface(type, "System.Collections.ICollection")) @@ -137,7 +137,7 @@ namespace de4dot.code.deobfuscators.CliSecure.vm { return true; } - static bool implementsInterface(TypeDefinition type, string ifaceName) { + static bool implementsInterface(TypeDef type, string ifaceName) { foreach (var iface in type.Interfaces) { if (iface.FullName == ifaceName) return true; @@ -156,7 +156,7 @@ namespace de4dot.code.deobfuscators.CliSecure.vm { } } - static bool hasAdd(MethodDefinition method) { + static bool hasAdd(MethodDef method) { foreach (var instr in method.Body.Instructions) { if (instr.OpCode.Code == Code.Add) return true; @@ -164,7 +164,7 @@ namespace de4dot.code.deobfuscators.CliSecure.vm { return false; } - List findVmHandlerTypes() { + List findVmHandlerTypes() { var requiredFields = new string[] { null, "System.Collections.Generic.Dictionary`2", @@ -190,13 +190,13 @@ namespace de4dot.code.deobfuscators.CliSecure.vm { return null; } - static List findVmHandlerTypes(MethodDefinition method) { - var list = new List(); + static List findVmHandlerTypes(MethodDef method) { + var list = new List(); foreach (var instr in method.Body.Instructions) { if (instr.OpCode.Code != Code.Ldtoken) continue; - var type = instr.Operand as TypeDefinition; + var type = instr.Operand as TypeDef; if (type == null) continue; @@ -206,7 +206,7 @@ namespace de4dot.code.deobfuscators.CliSecure.vm { return list; } - void detectHandlers(List handlerTypes, CsvmInfo csvmInfo) { + void detectHandlers(List handlerTypes, CsvmInfo csvmInfo) { opCodeHandlers = new List(); var detected = new List(); diff --git a/de4dot.code/deobfuscators/CodeFort/AssemblyDecrypter.cs b/de4dot.code/deobfuscators/CodeFort/AssemblyDecrypter.cs index 4716a6c2..5425ea44 100644 --- a/de4dot.code/deobfuscators/CodeFort/AssemblyDecrypter.cs +++ b/de4dot.code/deobfuscators/CodeFort/AssemblyDecrypter.cs @@ -23,8 +23,8 @@ using System.IO; using System.Security.Cryptography; using System.Text; using System.Text.RegularExpressions; -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using de4dot.blocks; namespace de4dot.code.deobfuscators.CodeFort { @@ -32,8 +32,8 @@ namespace de4dot.code.deobfuscators.CodeFort { ModuleDefinition module; EmbeddedResource assemblyEncryptedResource; PasswordInfo embedPassword; - MethodDefinition embedInitMethod; - MethodDefinition embedResolverMethod; + MethodDef embedInitMethod; + MethodDef embedResolverMethod; public class AssemblyInfo { public readonly byte[] data; @@ -67,11 +67,11 @@ namespace de4dot.code.deobfuscators.CodeFort { get { return EncryptedDetected || MainAssemblyHasAssemblyResolver; } } - public TypeDefinition Type { + public TypeDef Type { get { return embedInitMethod != null ? embedInitMethod.DeclaringType : null; } } - public MethodDefinition InitMethod { + public MethodDef InitMethod { get { return embedInitMethod; } } @@ -118,10 +118,10 @@ namespace de4dot.code.deobfuscators.CodeFort { return true; } - MethodDefinition checkCalledMethods(MethodDefinition method) { + MethodDef checkCalledMethods(MethodDef method) { int calls = 0; - TypeDefinition type = null; - MethodDefinition initMethod = null; + TypeDef type = null; + MethodDef initMethod = null; foreach (var calledMethod in DotNetUtils.getCalledMethods(module, method)) { calls++; if (type != null && calledMethod.DeclaringType != type) @@ -144,7 +144,7 @@ namespace de4dot.code.deobfuscators.CodeFort { findEmbedded(module.EntryPoint); } - bool findEmbedded(MethodDefinition method) { + bool findEmbedded(MethodDef method) { if (method == null || method.Body == null) return false; foreach (var calledMethod in DotNetUtils.getCalledMethods(module, method)) { @@ -162,7 +162,7 @@ namespace de4dot.code.deobfuscators.CodeFort { return false; } - MethodDefinition checkInitMethod(MethodDefinition method) { + MethodDef checkInitMethod(MethodDef method) { if (method == null || !method.IsStatic || method.Body == null) return null; if (!DotNetUtils.isMethod(method, "System.Void", "()")) @@ -175,7 +175,7 @@ namespace de4dot.code.deobfuscators.CodeFort { return resolver; } - bool checkType(TypeDefinition type) { + bool checkType(TypeDef type) { if (DotNetUtils.getMethod(type, "System.Byte[]", "(System.Byte[],System.String,System.String,System.Int32,System.String,System.Int32)") == null) return false; if (DotNetUtils.getMethod(type, "System.String", "(System.String)") == null) @@ -246,7 +246,7 @@ namespace de4dot.code.deobfuscators.CodeFort { return infos; } - static PasswordInfo getEmbedPassword(MethodDefinition method) { + static PasswordInfo getEmbedPassword(MethodDef method) { var instrs = method.Body.Instructions; for (int i = 0; i < instrs.Count - 3; i++) { int index = i; diff --git a/de4dot.code/deobfuscators/CodeFort/CfMethodCallInliner.cs b/de4dot.code/deobfuscators/CodeFort/CfMethodCallInliner.cs index ba322243..d06ab197 100644 --- a/de4dot.code/deobfuscators/CodeFort/CfMethodCallInliner.cs +++ b/de4dot.code/deobfuscators/CodeFort/CfMethodCallInliner.cs @@ -17,7 +17,7 @@ along with de4dot. If not, see . */ -using Mono.Cecil; +using dot10.DotNet; using de4dot.blocks; using de4dot.blocks.cflow; @@ -30,7 +30,7 @@ namespace de4dot.code.deobfuscators.CodeFort { this.proxyCallFixer = proxyCallFixer; } - protected override bool canInline(MethodDefinition method) { + protected override bool canInline(MethodDef method) { return proxyCallFixer.isProxyTargetMethod(method); } diff --git a/de4dot.code/deobfuscators/CodeFort/Deobfuscator.cs b/de4dot.code/deobfuscators/CodeFort/Deobfuscator.cs index 091ea869..29fd1850 100644 --- a/de4dot.code/deobfuscators/CodeFort/Deobfuscator.cs +++ b/de4dot.code/deobfuscators/CodeFort/Deobfuscator.cs @@ -19,7 +19,7 @@ using System; using System.Collections.Generic; -using Mono.Cecil; +using dot10.DotNet; using Mono.MyStuff; using de4dot.blocks; using de4dot.PE; @@ -173,7 +173,7 @@ namespace de4dot.code.deobfuscators.CodeFort { public override IEnumerable getStringDecrypterMethods() { var list = new List(); if (stringDecrypter.Method != null) - list.Add(stringDecrypter.Method.MetadataToken.ToInt32()); + list.Add(stringDecrypter.Method.MDToken.ToInt32()); return list; } } diff --git a/de4dot.code/deobfuscators/CodeFort/PasswordFinder.cs b/de4dot.code/deobfuscators/CodeFort/PasswordFinder.cs index be4cc4d2..b3428ae8 100644 --- a/de4dot.code/deobfuscators/CodeFort/PasswordFinder.cs +++ b/de4dot.code/deobfuscators/CodeFort/PasswordFinder.cs @@ -22,7 +22,7 @@ using System.Collections.Generic; using System.IO; using System.Reflection; using System.Reflection.Emit; -using Mono.Cecil; +using dot10.DotNet; namespace de4dot.code.deobfuscators.CodeFort { class PasswordInfo { diff --git a/de4dot.code/deobfuscators/CodeFort/ProxyCallFixer.cs b/de4dot.code/deobfuscators/CodeFort/ProxyCallFixer.cs index 8fa60d7d..aefbc0d8 100644 --- a/de4dot.code/deobfuscators/CodeFort/ProxyCallFixer.cs +++ b/de4dot.code/deobfuscators/CodeFort/ProxyCallFixer.cs @@ -19,17 +19,17 @@ using System; using System.Collections.Generic; -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using de4dot.blocks; namespace de4dot.code.deobfuscators.CodeFort { class ProxyCallFixer : ProxyCallFixer3 { IList memberReferences; MethodDefinitionAndDeclaringTypeDict proxyTargetMethods = new MethodDefinitionAndDeclaringTypeDict(); - TypeDefinition proxyMethodsType; + TypeDef proxyMethodsType; - public TypeDefinition ProxyMethodsType { + public TypeDef ProxyMethodsType { get { return proxyMethodsType; } } @@ -52,7 +52,7 @@ namespace de4dot.code.deobfuscators.CodeFort { } } - static MethodDefinition checkType(TypeDefinition type) { + static MethodDef checkType(TypeDef type) { if (type.Fields.Count != 1) return null; if (type.Fields[0].FieldType.FullName != "System.Reflection.Module") @@ -60,11 +60,11 @@ namespace de4dot.code.deobfuscators.CodeFort { return checkMethods(type); } - static MethodDefinition checkMethods(TypeDefinition type) { + static MethodDef checkMethods(TypeDef type) { if (type.Methods.Count != 3) return null; - MethodDefinition creatorMethod = null; + MethodDef creatorMethod = null; foreach (var method in type.Methods) { if (method.Name == ".cctor") continue; @@ -80,7 +80,7 @@ namespace de4dot.code.deobfuscators.CodeFort { return creatorMethod; } - protected override object checkCctor(ref TypeDefinition type, MethodDefinition cctor) { + protected override object checkCctor(ref TypeDef type, MethodDef cctor) { var instrs = cctor.Body.Instructions; if (instrs.Count != 3) return null; @@ -90,15 +90,15 @@ namespace de4dot.code.deobfuscators.CodeFort { var call = instrs[1]; if (call.OpCode.Code != Code.Call) return null; - if (!isDelegateCreatorMethod(call.Operand as MethodDefinition)) + if (!isDelegateCreatorMethod(call.Operand as MethodDef)) return null; int rid = DotNetUtils.getLdcI4Value(ldci4); - if (cctor.DeclaringType.MetadataToken.RID != rid) + if (cctor.DeclaringType.MDToken.RID != rid) throw new ApplicationException("Invalid rid"); return rid; } - protected override void getCallInfo(object context, FieldDefinition field, out MethodReference calledMethod, out OpCode callOpcode) { + protected override void getCallInfo(object context, FieldDef field, out MethodReference calledMethod, out OpCode callOpcode) { if (memberReferences == null) memberReferences = new List(module.GetMemberReferences()); diff --git a/de4dot.code/deobfuscators/CodeFort/StringDecrypter.cs b/de4dot.code/deobfuscators/CodeFort/StringDecrypter.cs index 6575bab6..cdad1a20 100644 --- a/de4dot.code/deobfuscators/CodeFort/StringDecrypter.cs +++ b/de4dot.code/deobfuscators/CodeFort/StringDecrypter.cs @@ -18,24 +18,24 @@ */ using System.Text; -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using de4dot.blocks; namespace de4dot.code.deobfuscators.CodeFort { class StringDecrypter { ModuleDefinition module; - MethodDefinition decryptMethod; + MethodDef decryptMethod; public bool Detected { get { return decryptMethod != null; } } - public MethodDefinition Method { + public MethodDef Method { get { return decryptMethod; } } - public TypeDefinition Type { + public TypeDef Type { get { return decryptMethod == null ? null : decryptMethod.DeclaringType; } } @@ -53,14 +53,14 @@ namespace de4dot.code.deobfuscators.CodeFort { } } - static MethodDefinition checkType(TypeDefinition type) { + static MethodDef checkType(TypeDef type) { if (type.HasFields) return null; return checkMethods(type); } - static MethodDefinition checkMethods(TypeDefinition type) { - MethodDefinition decryptMethod = null; + static MethodDef checkMethods(TypeDef type) { + MethodDef decryptMethod = null; foreach (var method in type.Methods) { if (method.Name == ".cctor") continue; @@ -76,7 +76,7 @@ namespace de4dot.code.deobfuscators.CodeFort { return decryptMethod; } - static bool hasDouble(MethodDefinition method, double value) { + static bool hasDouble(MethodDef method, double value) { if (method == null || method.Body == null) return false; foreach (var instr in method.Body.Instructions) { diff --git a/de4dot.code/deobfuscators/CodeVeil/AssemblyResolver.cs b/de4dot.code/deobfuscators/CodeVeil/AssemblyResolver.cs index 04d8ccd2..5ad69231 100644 --- a/de4dot.code/deobfuscators/CodeVeil/AssemblyResolver.cs +++ b/de4dot.code/deobfuscators/CodeVeil/AssemblyResolver.cs @@ -20,8 +20,8 @@ using System.Collections.Generic; using System.IO; using System.Xml; -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using de4dot.blocks; namespace de4dot.code.deobfuscators.CodeVeil { @@ -29,12 +29,12 @@ namespace de4dot.code.deobfuscators.CodeVeil { ModuleDefinition module; EmbeddedResource bundleData; EmbeddedResource bundleXmlFile; - TypeDefinition bundleType; - TypeDefinition assemblyManagerType; - TypeDefinition bundleStreamProviderIFace; - TypeDefinition xmlParserType; - TypeDefinition bundledAssemblyType; - TypeDefinition streamProviderType; + TypeDef bundleType; + TypeDef assemblyManagerType; + TypeDef bundleStreamProviderIFace; + TypeDef xmlParserType; + TypeDef bundledAssemblyType; + TypeDef streamProviderType; List infos = new List(); public class AssemblyInfo { @@ -66,9 +66,9 @@ namespace de4dot.code.deobfuscators.CodeVeil { } } - public IEnumerable BundleTypes { + public IEnumerable BundleTypes { get { - var list = new List(); + var list = new List(); if (!CanRemoveTypes) return list; @@ -167,7 +167,7 @@ namespace de4dot.code.deobfuscators.CodeVeil { return value; } - TypeDefinition findBundleType() { + TypeDef findBundleType() { foreach (var type in module.Types) { if (type.Namespace != "") continue; @@ -193,7 +193,7 @@ namespace de4dot.code.deobfuscators.CodeVeil { return null; } - MethodDefinition findInitMethod(TypeDefinition type) { + MethodDef findInitMethod(TypeDef type) { foreach (var method in type.Methods) { if (!method.IsStatic || method.Body == null) continue; @@ -208,7 +208,7 @@ namespace de4dot.code.deobfuscators.CodeVeil { return null; } - MethodDefinition findGetTempFilenameMethod(TypeDefinition type) { + MethodDef findGetTempFilenameMethod(TypeDef type) { foreach (var method in type.Methods) { if (method.IsStatic || method.Body == null) continue; @@ -234,7 +234,7 @@ namespace de4dot.code.deobfuscators.CodeVeil { return; foreach (var field in bundleType.Fields) { - var type = field.FieldType as TypeDefinition; + var type = field.FieldType as TypeDef; if (type == null) continue; if (type == bundleType) @@ -245,7 +245,7 @@ namespace de4dot.code.deobfuscators.CodeVeil { var ctor = DotNetUtils.getMethod(type, ".ctor"); if (ctor == null || ctor.Parameters.Count != 2) continue; - var iface = ctor.Parameters[1].ParameterType as TypeDefinition; + var iface = ctor.Parameters[1].ParameterType as TypeDef; if (iface == null || !iface.IsInterface) continue; @@ -259,7 +259,7 @@ namespace de4dot.code.deobfuscators.CodeVeil { if (assemblyManagerType == null) return; foreach (var field in assemblyManagerType.Fields) { - var type = field.FieldType as TypeDefinition; + var type = field.FieldType as TypeDef; if (type == null || type.IsInterface) continue; var ctor = DotNetUtils.getMethod(type, ".ctor"); @@ -274,7 +274,7 @@ namespace de4dot.code.deobfuscators.CodeVeil { continue; if (git.GenericArguments.Count != 1) continue; - var type2 = git.GenericArguments[0] as TypeDefinition; + var type2 = git.GenericArguments[0] as TypeDef; if (type2 == null) continue; @@ -293,7 +293,7 @@ namespace de4dot.code.deobfuscators.CodeVeil { foreach (var instr in ctor.Body.Instructions) { if (instr.OpCode.Code != Code.Newobj) continue; - var newobjCtor = instr.Operand as MethodDefinition; + var newobjCtor = instr.Operand as MethodDef; if (newobjCtor == null) continue; if (newobjCtor.DeclaringType == assemblyManagerType) diff --git a/de4dot.code/deobfuscators/CodeVeil/Deobfuscator.cs b/de4dot.code/deobfuscators/CodeVeil/Deobfuscator.cs index f50b9868..c9b9ea6d 100644 --- a/de4dot.code/deobfuscators/CodeVeil/Deobfuscator.cs +++ b/de4dot.code/deobfuscators/CodeVeil/Deobfuscator.cs @@ -19,7 +19,7 @@ using System; using System.Collections.Generic; -using Mono.Cecil; +using dot10.DotNet; using Mono.MyStuff; using de4dot.blocks; @@ -62,7 +62,7 @@ namespace de4dot.code.deobfuscators.CodeVeil { ProxyCallFixer proxyCallFixer; StringDecrypter stringDecrypter; AssemblyResolver assemblyResolver; - TypeDefinition killType; + TypeDef killType; ResourceDecrypter resourceDecrypter; internal class Options : OptionsBase { @@ -279,7 +279,7 @@ namespace de4dot.code.deobfuscators.CodeVeil { public override IEnumerable getStringDecrypterMethods() { var list = new List(); if (stringDecrypter.DecryptMethod != null) - list.Add(stringDecrypter.DecryptMethod.MetadataToken.ToInt32()); + list.Add(stringDecrypter.DecryptMethod.MDToken.ToInt32()); return list; } } diff --git a/de4dot.code/deobfuscators/CodeVeil/InvalidMethodsFinder.cs b/de4dot.code/deobfuscators/CodeVeil/InvalidMethodsFinder.cs index 880f08d9..a9603fa7 100644 --- a/de4dot.code/deobfuscators/CodeVeil/InvalidMethodsFinder.cs +++ b/de4dot.code/deobfuscators/CodeVeil/InvalidMethodsFinder.cs @@ -18,12 +18,12 @@ */ using System.Collections.Generic; -using Mono.Cecil; +using dot10.DotNet; namespace de4dot.code.deobfuscators.CodeVeil { class InvalidMethodsFinder { - public static List findAll(ModuleDefinition module) { - var list = new List(); + public static List findAll(ModuleDefinition module) { + var list = new List(); foreach (var type in module.GetTypes()) { foreach (var method in type.Methods) { if (isInvalidMethod(method)) @@ -33,14 +33,14 @@ namespace de4dot.code.deobfuscators.CodeVeil { return list; } - public static bool isInvalidMethod(MethodDefinition method) { + public static bool isInvalidMethod(MethodDef method) { if (method == null) return false; if (method.IsStatic) return false; if (method.Parameters.Count != 0) return false; - var retType = method.MethodReturnType.ReturnType as GenericParameter; + var retType = method.MethodReturnType.ReturnType as GenericParam; if (retType == null) return false; diff --git a/de4dot.code/deobfuscators/CodeVeil/MainType.cs b/de4dot.code/deobfuscators/CodeVeil/MainType.cs index d5f3e32a..c3868f61 100644 --- a/de4dot.code/deobfuscators/CodeVeil/MainType.cs +++ b/de4dot.code/deobfuscators/CodeVeil/MainType.cs @@ -18,8 +18,8 @@ */ using System.Collections.Generic; -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using Mono.Cecil.Metadata; using de4dot.blocks; @@ -27,12 +27,12 @@ namespace de4dot.code.deobfuscators.CodeVeil { // Detects the type CV adds to the assembly that gets called from ::.cctor. class MainType { ModuleDefinition module; - TypeDefinition theType; - MethodDefinition initMethod; - MethodDefinition tamperCheckMethod; + TypeDef theType; + MethodDef initMethod; + MethodDef tamperCheckMethod; ObfuscatorVersion obfuscatorVersion = ObfuscatorVersion.Unknown; List rvas = new List(); // _stub and _executive - List otherInitMethods = new List(); + List otherInitMethods = new List(); public bool Detected { get { return theType != null; } @@ -42,19 +42,19 @@ namespace de4dot.code.deobfuscators.CodeVeil { get { return obfuscatorVersion; } } - public TypeDefinition Type { + public TypeDef Type { get { return theType; } } - public MethodDefinition InitMethod { + public MethodDef InitMethod { get { return initMethod; } } - public List OtherInitMethods { + public List OtherInitMethods { get { return otherInitMethods; } } - public MethodDefinition TamperCheckMethod { + public MethodDef TamperCheckMethod { get { return tamperCheckMethod; } } @@ -99,7 +99,7 @@ namespace de4dot.code.deobfuscators.CodeVeil { var call = instrs[i + 2]; if (call.OpCode.Code != Code.Call) continue; - var initMethodTmp = call.Operand as MethodDefinition; + var initMethodTmp = call.Operand as MethodDef; ObfuscatorVersion obfuscatorVersionTmp; if (!checkInitMethod(initMethodTmp, out obfuscatorVersionTmp)) continue; @@ -118,7 +118,7 @@ namespace de4dot.code.deobfuscators.CodeVeil { "System.Collections.Generic.List`1", "System.Runtime.InteropServices.GCHandle", }; - bool checkInitMethod(MethodDefinition initMethod, out ObfuscatorVersion obfuscatorVersionTmp) { + bool checkInitMethod(MethodDef initMethod, out ObfuscatorVersion obfuscatorVersionTmp) { obfuscatorVersionTmp = ObfuscatorVersion.Unknown; if (initMethod == null) @@ -146,7 +146,7 @@ namespace de4dot.code.deobfuscators.CodeVeil { return true; } - static bool hasCodeString(MethodDefinition method, string str) { + static bool hasCodeString(MethodDef method, string str) { foreach (var s in DotNetUtils.getCodeStrings(method)) { if (s == str) return true; @@ -154,7 +154,7 @@ namespace de4dot.code.deobfuscators.CodeVeil { return false; } - bool checkMethodsType(TypeDefinition type) { + bool checkMethodsType(TypeDef type) { rvas = new List(); var fields = getRvaFields(type); @@ -166,8 +166,8 @@ namespace de4dot.code.deobfuscators.CodeVeil { return true; } - static List getRvaFields(TypeDefinition type) { - var fields = new List(); + static List getRvaFields(TypeDef type) { + var fields = new List(); foreach (var field in type.Fields) { if (field.FieldType.EType != ElementType.U1 && field.FieldType.EType != ElementType.U4) continue; @@ -187,7 +187,7 @@ namespace de4dot.code.deobfuscators.CodeVeil { otherInitMethods = findOtherInitMethods(); } - MethodDefinition findTamperCheckMethod() { + MethodDef findTamperCheckMethod() { foreach (var method in theType.Methods) { if (!method.IsStatic || method.Body == null) continue; @@ -200,8 +200,8 @@ namespace de4dot.code.deobfuscators.CodeVeil { return null; } - List findOtherInitMethods() { - var list = new List(); + List findOtherInitMethods() { + var list = new List(); foreach (var method in theType.Methods) { if (!method.IsStatic) continue; @@ -215,7 +215,7 @@ namespace de4dot.code.deobfuscators.CodeVeil { return list; } - public MethodDefinition getInitStringDecrypterMethod(MethodDefinition stringDecrypterInitMethod) { + public MethodDef getInitStringDecrypterMethod(MethodDef stringDecrypterInitMethod) { if (stringDecrypterInitMethod == null) return null; if (theType == null) @@ -230,7 +230,7 @@ namespace de4dot.code.deobfuscators.CodeVeil { return null; } - bool callsMethod(MethodDefinition methodToCheck, MethodDefinition calledMethod) { + bool callsMethod(MethodDef methodToCheck, MethodDef calledMethod) { foreach (var method in DotNetUtils.getCalledMethods(module, methodToCheck)) { if (method == calledMethod) return true; diff --git a/de4dot.code/deobfuscators/CodeVeil/MethodsDecrypter.cs b/de4dot.code/deobfuscators/CodeVeil/MethodsDecrypter.cs index b4520ede..61bf1364 100644 --- a/de4dot.code/deobfuscators/CodeVeil/MethodsDecrypter.cs +++ b/de4dot.code/deobfuscators/CodeVeil/MethodsDecrypter.cs @@ -20,8 +20,8 @@ using System; using System.Collections.Generic; using System.IO; -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using Mono.Cecil.Metadata; using Mono.MyStuff; using de4dot.blocks; diff --git a/de4dot.code/deobfuscators/CodeVeil/ProxyCallFixer.cs b/de4dot.code/deobfuscators/CodeVeil/ProxyCallFixer.cs index e5d3bc1f..df3b3429 100644 --- a/de4dot.code/deobfuscators/CodeVeil/ProxyCallFixer.cs +++ b/de4dot.code/deobfuscators/CodeVeil/ProxyCallFixer.cs @@ -20,8 +20,8 @@ using System; using System.Collections.Generic; using System.IO; -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using Mono.Cecil.Metadata; using de4dot.blocks; @@ -32,12 +32,12 @@ namespace de4dot.code.deobfuscators.CodeVeil { BinaryReader reader; class Info { - public TypeDefinition proxyType; - public MethodDefinition initMethod; - public FieldDefinition dataField; - public TypeDefinition ilgeneratorType; - public TypeDefinition fieldInfoType; - public TypeDefinition methodInfoType; + public TypeDef proxyType; + public MethodDef initMethod; + public FieldDef dataField; + public TypeDef ilgeneratorType; + public TypeDef fieldInfoType; + public TypeDef methodInfoType; } class Context { @@ -61,15 +61,15 @@ namespace de4dot.code.deobfuscators.CodeVeil { } } - public TypeDefinition IlGeneratorType { + public TypeDef IlGeneratorType { get { return info.ilgeneratorType; } } - public TypeDefinition FieldInfoType { + public TypeDef FieldInfoType { get { return info.fieldInfoType; } } - public TypeDefinition MethodInfoType { + public TypeDef MethodInfoType { get { return info.methodInfoType; } } @@ -89,7 +89,7 @@ namespace de4dot.code.deobfuscators.CodeVeil { info.methodInfoType = lookup(oldOne.info.methodInfoType, "Could not find methodInfoType"); } - protected override object checkCctor(ref TypeDefinition type, MethodDefinition cctor) { + protected override object checkCctor(ref TypeDef type, MethodDef cctor) { var instrs = cctor.Body.Instructions; for (int i = 0; i < instrs.Count - 1; i++) { var ldci4 = instrs[i]; @@ -105,14 +105,14 @@ namespace de4dot.code.deobfuscators.CodeVeil { int offset = DotNetUtils.getLdcI4Value(ldci4); reader.BaseStream.Position = offset; int rid = DeobUtils.readVariableLengthInt32(reader); - if (rid != type.MetadataToken.RID) + if (rid != type.MDToken.RID) throw new ApplicationException("Invalid RID"); return string.Empty; // It's non-null } return null; } - protected override void getCallInfo(object context, FieldDefinition field, out MethodReference calledMethod, out OpCode callOpcode) { + protected override void getCallInfo(object context, FieldDef field, out MethodReference calledMethod, out OpCode callOpcode) { byte flags = reader.ReadByte(); int methodToken = 0x06000000 + ((flags & 0x3F) << 24) + DeobUtils.readVariableLengthInt32(reader); @@ -122,7 +122,7 @@ namespace de4dot.code.deobfuscators.CodeVeil { calledMethod = module.LookupToken(methodToken) as MethodReference; if (calledMethod == null) throw new ApplicationException("Could not find method"); - if (genericTypeToken != -1 && calledMethod.DeclaringType.MetadataToken.ToInt32() != genericTypeToken) + if (genericTypeToken != -1 && calledMethod.DeclaringType.MDToken.ToInt32() != genericTypeToken) throw new ApplicationException("Invalid declaring type token"); } @@ -138,7 +138,7 @@ namespace de4dot.code.deobfuscators.CodeVeil { setDelegateCreatorMethod(info.initMethod); } - bool initializeInfo(Info infoTmp, TypeDefinition type) { + bool initializeInfo(Info infoTmp, TypeDef type) { foreach (var dtype in type.NestedTypes) { var cctor = DotNetUtils.getMethod(dtype, ".cctor"); if (cctor == null) @@ -152,7 +152,7 @@ namespace de4dot.code.deobfuscators.CodeVeil { return false; } - bool initProxyType(Info infoTmp, MethodDefinition method) { + bool initProxyType(Info infoTmp, MethodDef method) { foreach (var calledMethod in DotNetUtils.getCalledMethods(module, method)) { if (!calledMethod.IsStatic) continue; @@ -175,7 +175,7 @@ namespace de4dot.code.deobfuscators.CodeVeil { "System.Reflection.Emit.OpCode", "System.Reflection.Emit.OpCode[]", }; - bool checkProxyType(Info infoTmp, TypeDefinition type) { + bool checkProxyType(Info infoTmp, TypeDef type) { if (type.NestedTypes.Count != 1) return false; @@ -196,8 +196,8 @@ namespace de4dot.code.deobfuscators.CodeVeil { return true; } - static List getRvaFields(TypeDefinition type) { - var fields = new List(); + static List getRvaFields(TypeDef type) { + var fields = new List(); foreach (var field in type.Fields) { if (field.RVA != 0) fields.Add(field); @@ -205,9 +205,9 @@ namespace de4dot.code.deobfuscators.CodeVeil { return fields; } - protected override IEnumerable getDelegateTypes() { + protected override IEnumerable getDelegateTypes() { if (!mainType.Detected) - return new List(); + return new List(); return mainType.Type.NestedTypes; } @@ -233,9 +233,9 @@ namespace de4dot.code.deobfuscators.CodeVeil { if (method.Parameters[2].ParameterType.FullName != "System.Type[]") continue; - var methodType = method.Parameters[0].ParameterType as TypeDefinition; - var fieldType = method.Parameters[1].ParameterType as TypeDefinition; - var ilgType = method.Parameters[3].ParameterType as TypeDefinition; + var methodType = method.Parameters[0].ParameterType as TypeDef; + var fieldType = method.Parameters[1].ParameterType as TypeDef; + var ilgType = method.Parameters[3].ParameterType as TypeDef; if (!checkMethodType(methodType)) continue; if (!checkFieldType(fieldType)) @@ -249,7 +249,7 @@ namespace de4dot.code.deobfuscators.CodeVeil { } } - bool checkMethodType(TypeDefinition type) { + bool checkMethodType(TypeDef type) { if (type == null || type.BaseType == null || type.BaseType.EType != ElementType.Object) return false; if (type.Fields.Count != 1) @@ -260,7 +260,7 @@ namespace de4dot.code.deobfuscators.CodeVeil { return true; } - bool checkFieldType(TypeDefinition type) { + bool checkFieldType(TypeDef type) { if (type == null || type.BaseType == null || type.BaseType.EType != ElementType.Object) return false; if (DotNetUtils.getField(type, "System.Reflection.FieldInfo") == null) @@ -269,7 +269,7 @@ namespace de4dot.code.deobfuscators.CodeVeil { return true; } - bool checkIlGeneratorType(TypeDefinition type) { + bool checkIlGeneratorType(TypeDef type) { if (type == null || type.BaseType == null || type.BaseType.EType != ElementType.Object) return false; if (type.Fields.Count != 1) diff --git a/de4dot.code/deobfuscators/CodeVeil/ResourceConverter.cs b/de4dot.code/deobfuscators/CodeVeil/ResourceConverter.cs index 2c79c9f9..9a05275f 100644 --- a/de4dot.code/deobfuscators/CodeVeil/ResourceConverter.cs +++ b/de4dot.code/deobfuscators/CodeVeil/ResourceConverter.cs @@ -19,7 +19,7 @@ using System; using System.IO; -using Mono.Cecil; +using dot10.DotNet; using de4dot.code.resources; namespace de4dot.code.deobfuscators.CodeVeil { diff --git a/de4dot.code/deobfuscators/CodeVeil/ResourceDecrypter.cs b/de4dot.code/deobfuscators/CodeVeil/ResourceDecrypter.cs index c08dcfa2..650cc868 100644 --- a/de4dot.code/deobfuscators/CodeVeil/ResourceDecrypter.cs +++ b/de4dot.code/deobfuscators/CodeVeil/ResourceDecrypter.cs @@ -19,23 +19,23 @@ using System; using System.IO; -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using Mono.Cecil.Metadata; using de4dot.blocks; namespace de4dot.code.deobfuscators.CodeVeil { class ResourceDecrypter { ModuleDefinition module; - TypeDefinition encryptedResourceStreamType; - TypeDefinition encryptedResourceSetType; - MethodDefinition encryptedResourceSet_GetDefaultReader; - TypeDefinition encryptedResourceReaderType; + TypeDef encryptedResourceStreamType; + TypeDef encryptedResourceSetType; + MethodDef encryptedResourceSet_GetDefaultReader; + TypeDef encryptedResourceReaderType; GenericInstanceType encryptedResourceReaderTypeDict; - TypeDefinition resType; - MethodDefinition resTypeCtor; - TypeDefinition resourceFlagsType; - TypeDefinition resourceEnumeratorType; + TypeDef resType; + MethodDef resTypeCtor; + TypeDef resourceFlagsType; + TypeDef resourceEnumeratorType; MethodCallRestorerBase methodsRestorer; public bool CanRemoveTypes { @@ -49,27 +49,27 @@ namespace de4dot.code.deobfuscators.CodeVeil { } } - public TypeDefinition EncryptedResourceStreamType { + public TypeDef EncryptedResourceStreamType { get { return encryptedResourceStreamType; } } - public TypeDefinition EncryptedResourceSetType { + public TypeDef EncryptedResourceSetType { get { return encryptedResourceSetType; } } - public TypeDefinition EncryptedResourceReaderType { + public TypeDef EncryptedResourceReaderType { get { return encryptedResourceReaderType; } } - public TypeDefinition ResType { + public TypeDef ResType { get { return resType; } } - public TypeDefinition ResourceFlagsType { + public TypeDef ResourceFlagsType { get { return resourceFlagsType; } } - public TypeDefinition ResourceEnumeratorType { + public TypeDef ResourceEnumeratorType { get { return resourceEnumeratorType; } } @@ -122,7 +122,7 @@ namespace de4dot.code.deobfuscators.CodeVeil { void findResourceFlags() { if (resTypeCtor == null || resTypeCtor.Parameters.Count != 4) return; - var type = resTypeCtor.Parameters[2].ParameterType as TypeDefinition; + var type = resTypeCtor.Parameters[2].ParameterType as TypeDef; if (type == null || !type.IsEnum) return; @@ -137,7 +137,7 @@ namespace de4dot.code.deobfuscators.CodeVeil { void findResType() { if (encryptedResourceReaderTypeDict == null) return; - var type = encryptedResourceReaderTypeDict.GenericArguments[1] as TypeDefinition; + var type = encryptedResourceReaderTypeDict.GenericArguments[1] as TypeDef; if (type == null) return; if (type.BaseType == null || type.BaseType.EType != ElementType.Object) @@ -175,7 +175,7 @@ namespace de4dot.code.deobfuscators.CodeVeil { encryptedResourceReaderTypeDict = dictType; } - static bool hasInterface(TypeDefinition type, string interfaceFullName) { + static bool hasInterface(TypeDef type, string interfaceFullName) { foreach (var iface in type.Interfaces) { if (iface.FullName == interfaceFullName) return true; @@ -183,7 +183,7 @@ namespace de4dot.code.deobfuscators.CodeVeil { return false; } - static GenericInstanceType getDlxResDict(TypeDefinition type) { + static GenericInstanceType getDlxResDict(TypeDef type) { foreach (var field in type.Fields) { var fieldType = field.FieldType as GenericInstanceType; if (fieldType == null) @@ -194,20 +194,20 @@ namespace de4dot.code.deobfuscators.CodeVeil { continue; if (fieldType.GenericArguments[0].FullName != "System.String") continue; - if (!(fieldType.GenericArguments[1] is TypeDefinition)) + if (!(fieldType.GenericArguments[1] is TypeDef)) continue; return fieldType; } return null; } - static TypeDefinition getTypeFromCode(MethodDefinition method) { + static TypeDef getTypeFromCode(MethodDef method) { if (method == null || method.Body == null) return null; foreach (var instr in method.Body.Instructions) { if (instr.OpCode.Code != Code.Ldtoken) continue; - var type = instr.Operand as TypeDefinition; + var type = instr.Operand as TypeDef; if (type != null) return type; } @@ -260,7 +260,7 @@ namespace de4dot.code.deobfuscators.CodeVeil { if (findXxteaMethod(type) == null) continue; - MethodDefinition getManifestResourceStreamMethodTmp1, getManifestResourceStreamMethodTmp2; + MethodDef getManifestResourceStreamMethodTmp1, getManifestResourceStreamMethodTmp2; if (!findManifestResourceStreamMethods(type, out getManifestResourceStreamMethodTmp1, out getManifestResourceStreamMethodTmp2)) continue; @@ -271,7 +271,7 @@ namespace de4dot.code.deobfuscators.CodeVeil { } } - static MethodDefinition findXxteaMethod(TypeDefinition type) { + static MethodDef findXxteaMethod(TypeDef type) { foreach (var method in type.Methods) { if (!method.IsPrivate || method.IsStatic || method.Body == null) continue; @@ -292,7 +292,7 @@ namespace de4dot.code.deobfuscators.CodeVeil { return null; } - static bool findManifestResourceStreamMethods(TypeDefinition type, out MethodDefinition getManifestResourceStreamMethodTmp1, out MethodDefinition getManifestResourceStreamMethodTmp2) { + static bool findManifestResourceStreamMethods(TypeDef type, out MethodDef getManifestResourceStreamMethodTmp1, out MethodDef getManifestResourceStreamMethodTmp2) { getManifestResourceStreamMethodTmp1 = null; getManifestResourceStreamMethodTmp2 = null; foreach (var method in type.Methods) { diff --git a/de4dot.code/deobfuscators/CodeVeil/StringDecrypter.cs b/de4dot.code/deobfuscators/CodeVeil/StringDecrypter.cs index c959bce4..e01b7a08 100644 --- a/de4dot.code/deobfuscators/CodeVeil/StringDecrypter.cs +++ b/de4dot.code/deobfuscators/CodeVeil/StringDecrypter.cs @@ -19,33 +19,33 @@ using System; using System.IO; -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using de4dot.blocks; namespace de4dot.code.deobfuscators.CodeVeil { class StringDecrypter { ModuleDefinition module; MainType mainType; - TypeDefinition decrypterType; - FieldDefinition stringDataField; - MethodDefinition initMethod; - MethodDefinition decrypterMethod; + TypeDef decrypterType; + FieldDef stringDataField; + MethodDef initMethod; + MethodDef decrypterMethod; string[] decryptedStrings; public bool Detected { get { return decrypterType != null; } } - public TypeDefinition Type { + public TypeDef Type { get { return decrypterType; } } - public MethodDefinition InitMethod { + public MethodDef InitMethod { get { return initMethod; } } - public MethodDefinition DecryptMethod { + public MethodDef DecryptMethod { get { return decrypterMethod; } } @@ -79,7 +79,7 @@ namespace de4dot.code.deobfuscators.CodeVeil { findV5(cctor); } - bool find(MethodDefinition method) { + bool find(MethodDef method) { if (method == null || method.Body == null || !method.IsStatic) return false; @@ -88,7 +88,7 @@ namespace de4dot.code.deobfuscators.CodeVeil { var call = instrs[i]; if (call.OpCode.Code != Code.Call) continue; - var initMethodTmp = call.Operand as MethodDefinition; + var initMethodTmp = call.Operand as MethodDef; if (initMethodTmp == null || initMethodTmp.Body == null || !initMethodTmp.IsStatic) continue; if (!DotNetUtils.isMethod(initMethodTmp, "System.Void", "()")) @@ -105,7 +105,7 @@ namespace de4dot.code.deobfuscators.CodeVeil { } // The main decrypter type calls the string decrypter init method inside its init method - void findV5(MethodDefinition method) { + void findV5(MethodDef method) { if (!mainType.Detected) return; foreach (var calledMethod in DotNetUtils.getCalledMethods(module, mainType.InitMethod)) { @@ -114,7 +114,7 @@ namespace de4dot.code.deobfuscators.CodeVeil { } } - bool checkType(TypeDefinition type) { + bool checkType(TypeDef type) { if (!type.HasNestedTypes) return false; @@ -134,8 +134,8 @@ namespace de4dot.code.deobfuscators.CodeVeil { return true; } - static MethodDefinition getDecrypterMethod(TypeDefinition type) { - MethodDefinition foundMethod = null; + static MethodDef getDecrypterMethod(TypeDef type) { + MethodDef foundMethod = null; foreach (var method in type.Methods) { if (method.Body == null || !method.IsStatic) continue; @@ -155,11 +155,11 @@ namespace de4dot.code.deobfuscators.CodeVeil { "System.String[]", "System.UInt32[]", }; - FieldDefinition checkFields(TypeDefinition type) { + FieldDef checkFields(TypeDef type) { if (!new FieldTypes(type).all(requiredFields)) return null; - FieldDefinition stringData = null; + FieldDef stringData = null; foreach (var field in type.Fields) { if (field.RVA != 0) { if (stringData != null) @@ -193,7 +193,7 @@ namespace de4dot.code.deobfuscators.CodeVeil { stringDataField.InitialValue = new byte[1]; } - static uint[] getKey(MethodDefinition method) { + static uint[] getKey(MethodDef method) { var instrs = method.Body.Instructions; for (int i = 0; i < instrs.Count - 1; i++) { var ldci4 = instrs[i]; diff --git a/de4dot.code/deobfuscators/CodeVeil/TamperDetection.cs b/de4dot.code/deobfuscators/CodeVeil/TamperDetection.cs index b55245c9..92b2df88 100644 --- a/de4dot.code/deobfuscators/CodeVeil/TamperDetection.cs +++ b/de4dot.code/deobfuscators/CodeVeil/TamperDetection.cs @@ -18,8 +18,8 @@ */ using System.Collections.Generic; -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using Mono.Cecil.Metadata; using de4dot.blocks; @@ -27,14 +27,14 @@ namespace de4dot.code.deobfuscators.CodeVeil { class TamperDetection { ModuleDefinition module; MainType mainType; - TypeDefinition tamperDetectionType; - List tamperDetectionMethods = new List(); + TypeDef tamperDetectionType; + List tamperDetectionMethods = new List(); - public TypeDefinition Type { + public TypeDef Type { get { return tamperDetectionType; } } - public List Methods { + public List Methods { get { return tamperDetectionMethods; } } @@ -82,7 +82,7 @@ namespace de4dot.code.deobfuscators.CodeVeil { } } - bool checkTamperDetectionClasses(IEnumerable types) { + bool checkTamperDetectionClasses(IEnumerable types) { foreach (var type in types) { if (!isTamperDetectionClass(type)) return false; @@ -90,13 +90,13 @@ namespace de4dot.code.deobfuscators.CodeVeil { return true; } - bool isTamperDetectionClass(TypeDefinition type) { + bool isTamperDetectionClass(TypeDef type) { if (type.BaseType == null || type.BaseType.EType != ElementType.Object) return false; if ((type.Attributes & ~TypeAttributes.Sealed) != TypeAttributes.NestedAssembly) return false; - MethodDefinition cctor = null, initMethod = null; + MethodDef cctor = null, initMethod = null; foreach (var method in type.Methods) { if (InvalidMethodsFinder.isInvalidMethod(method)) continue; @@ -116,7 +116,7 @@ namespace de4dot.code.deobfuscators.CodeVeil { return true; } - bool callsMainTypeTamperCheckMethod(MethodDefinition method) { + bool callsMainTypeTamperCheckMethod(MethodDef method) { foreach (var calledMethod in DotNetUtils.getCalledMethods(module, method)) { if (calledMethod == mainType.TamperCheckMethod) return true; @@ -142,7 +142,7 @@ namespace de4dot.code.deobfuscators.CodeVeil { } static bool checkInvokeCall(Instruction instr, string returnType, string parameters) { - var method = instr.Operand as MethodDefinition; + var method = instr.Operand as MethodDef; if (method == null) return false; if (method.Name != "Invoke") diff --git a/de4dot.code/deobfuscators/CodeWall/AssemblyDecrypter.cs b/de4dot.code/deobfuscators/CodeWall/AssemblyDecrypter.cs index 6d59bfa7..1e3ed8b0 100644 --- a/de4dot.code/deobfuscators/CodeWall/AssemblyDecrypter.cs +++ b/de4dot.code/deobfuscators/CodeWall/AssemblyDecrypter.cs @@ -22,8 +22,8 @@ using System.Collections.Generic; using System.IO; using System.Security.Cryptography; using System.Text; -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using de4dot.blocks; using de4dot.code.resources; @@ -74,7 +74,7 @@ namespace de4dot.code.deobfuscators.CodeWall { if (!checkEntryPoint(method)) return; - MethodDefinition decryptAssemblyMethod; + MethodDef decryptAssemblyMethod; var mainKey = getMainResourceKey(method, out decryptAssemblyMethod); if (mainKey == null) return; @@ -100,7 +100,7 @@ namespace de4dot.code.deobfuscators.CodeWall { "System.AppDomain", "System.DateTime", }; - bool checkEntryPoint(MethodDefinition method) { + bool checkEntryPoint(MethodDef method) { if (method == null) return false; if (!new LocalTypes(method).all(requiredLocals)) @@ -112,12 +112,12 @@ namespace de4dot.code.deobfuscators.CodeWall { return true; } - void deobfuscateAll(MethodDefinition method) { + void deobfuscateAll(MethodDef method) { simpleDeobfuscator.deobfuscate(method); simpleDeobfuscator.decryptStrings(method, deob); } - string getMainResourceKey(MethodDefinition method, out MethodDefinition decryptAssemblyMethod) { + string getMainResourceKey(MethodDef method, out MethodDef decryptAssemblyMethod) { foreach (var calledMethod in DotNetUtils.getCalledMethods(module, method)) { if (!calledMethod.IsStatic || calledMethod.Body == null) continue; @@ -135,7 +135,7 @@ namespace de4dot.code.deobfuscators.CodeWall { return null; } - string getMainResourceKeyInfo(MethodDefinition method, out MethodDefinition decryptAssemblyMethod) { + string getMainResourceKeyInfo(MethodDef method, out MethodDef decryptAssemblyMethod) { var instrs = method.Body.Instructions; for (int i = 0; i < instrs.Count - 1; i++) { var ldstr = instrs[i]; @@ -144,7 +144,7 @@ namespace de4dot.code.deobfuscators.CodeWall { var call = instrs[i + 1]; if (call.OpCode.Code != Code.Call) continue; - var calledMethod = call.Operand as MethodDefinition; + var calledMethod = call.Operand as MethodDef; if (calledMethod == null) continue; @@ -155,7 +155,7 @@ namespace de4dot.code.deobfuscators.CodeWall { return null; } - EmbeddedResource getResource(MethodDefinition method, out ModuleDefinition theResourceModule) { + EmbeddedResource getResource(MethodDef method, out ModuleDefinition theResourceModule) { string resourceDllFileName = null; theResourceModule = module; foreach (var s in DotNetUtils.getCodeStrings(method)) { @@ -192,7 +192,7 @@ namespace de4dot.code.deobfuscators.CodeWall { } } - bool getPassword(MethodDefinition method, out string password, out string salt) { + bool getPassword(MethodDef method, out string password, out string salt) { var instrs = method.Body.Instructions; for (int i = 0; i < instrs.Count - 1; i++) { var ldstr1 = instrs[i]; diff --git a/de4dot.code/deobfuscators/CodeWall/Deobfuscator.cs b/de4dot.code/deobfuscators/CodeWall/Deobfuscator.cs index b98f1e71..0ee7b916 100644 --- a/de4dot.code/deobfuscators/CodeWall/Deobfuscator.cs +++ b/de4dot.code/deobfuscators/CodeWall/Deobfuscator.cs @@ -19,7 +19,7 @@ using System; using System.Collections.Generic; -using Mono.Cecil; +using dot10.DotNet; using Mono.MyStuff; using de4dot.blocks; using de4dot.PE; @@ -263,7 +263,7 @@ namespace de4dot.code.deobfuscators.CodeWall { public override IEnumerable getStringDecrypterMethods() { var list = new List(); foreach (var info in stringDecrypter.Infos) - list.Add(info.Method.MetadataToken.ToInt32()); + list.Add(info.Method.MDToken.ToInt32()); return list; } } diff --git a/de4dot.code/deobfuscators/CodeWall/MethodsDecrypter.cs b/de4dot.code/deobfuscators/CodeWall/MethodsDecrypter.cs index 672d3dd6..fd0c3fb6 100644 --- a/de4dot.code/deobfuscators/CodeWall/MethodsDecrypter.cs +++ b/de4dot.code/deobfuscators/CodeWall/MethodsDecrypter.cs @@ -18,8 +18,8 @@ */ using System; -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using Mono.MyStuff; using de4dot.PE; using de4dot.blocks; @@ -51,7 +51,7 @@ namespace de4dot.code.deobfuscators.CodeWall { } } - bool checkCctor(MethodDefinition method) { + bool checkCctor(MethodDef method) { if (method == null || method.Body == null) return false; diff --git a/de4dot.code/deobfuscators/CodeWall/StringDecrypter.cs b/de4dot.code/deobfuscators/CodeWall/StringDecrypter.cs index 1d555420..ee0474b9 100644 --- a/de4dot.code/deobfuscators/CodeWall/StringDecrypter.cs +++ b/de4dot.code/deobfuscators/CodeWall/StringDecrypter.cs @@ -21,8 +21,8 @@ using System; using System.Collections.Generic; using System.IO; using System.Text; -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using de4dot.blocks; namespace de4dot.code.deobfuscators.CodeWall { @@ -38,13 +38,13 @@ namespace de4dot.code.deobfuscators.CodeWall { } public class StringEncrypterInfo { - MethodDefinition method; + MethodDef method; - public TypeDefinition Type { + public TypeDef Type { get { return method.DeclaringType; } } - public MethodDefinition Method { + public MethodDef Method { get { return method; } } @@ -54,7 +54,7 @@ namespace de4dot.code.deobfuscators.CodeWall { public int Magic3 { get; set; } public BinaryReader Reader { get; set; } - public StringEncrypterInfo(MethodDefinition method) { + public StringEncrypterInfo(MethodDef method) { this.method = method; } @@ -94,7 +94,7 @@ namespace de4dot.code.deobfuscators.CodeWall { public override string ToString() { return string.Format("{0:X8} M1:{1:X8} M2:{2:X8} M3:{3:X8}", - Method.MetadataToken.ToInt32(), + Method.MDToken.ToInt32(), Magic1, Magic2, Magic3); } } @@ -124,7 +124,7 @@ namespace de4dot.code.deobfuscators.CodeWall { public void find() { foreach (var type in module.Types) { - MethodDefinition decrypterMethod; + MethodDef decrypterMethod; var decrypterVersion = checkType(type, out decrypterMethod); if (decrypterVersion == Version.Unknown) continue; @@ -133,8 +133,8 @@ namespace de4dot.code.deobfuscators.CodeWall { } } - Version checkType(TypeDefinition type, out MethodDefinition decrypterMethod) { - MethodDefinition method; + Version checkType(TypeDef type, out MethodDef decrypterMethod) { + MethodDef method; if ((method = checkType_v30(type)) != null) { decrypterMethod = method; @@ -161,8 +161,8 @@ namespace de4dot.code.deobfuscators.CodeWall { "System.Random", "System.String", }; - MethodDefinition checkType_v30(TypeDefinition type) { - MethodDefinition decrypterMethod = checkMethods_v30(type); + MethodDef checkType_v30(TypeDef type) { + MethodDef decrypterMethod = checkMethods_v30(type); if (decrypterMethod == null) return null; if (!new FieldTypes(type).exactly(requiredTypes_v30)) @@ -173,12 +173,12 @@ namespace de4dot.code.deobfuscators.CodeWall { return decrypterMethod; } - static MethodDefinition checkMethods_v30(TypeDefinition type) { + static MethodDef checkMethods_v30(TypeDef type) { if (type.Methods.Count < 1 || type.Methods.Count > 2) return null; - MethodDefinition decrypterMethod = null; - MethodDefinition cctor = null; + MethodDef decrypterMethod = null; + MethodDef cctor = null; foreach (var method in type.Methods) { if (method.Name == ".cctor") { cctor = method; @@ -208,8 +208,8 @@ namespace de4dot.code.deobfuscators.CodeWall { "System.String", "System.Object", }; - MethodDefinition checkType_v36(TypeDefinition type) { - MethodDefinition decrypterMethod = checkMethods_v36(type); + MethodDef checkType_v36(TypeDef type) { + MethodDef decrypterMethod = checkMethods_v36(type); if (decrypterMethod == null) return null; if (!new FieldTypes(type).exactly(requiredTypes_v36)) @@ -220,12 +220,12 @@ namespace de4dot.code.deobfuscators.CodeWall { return decrypterMethod; } - static MethodDefinition checkMethods_v36(TypeDefinition type) { + static MethodDef checkMethods_v36(TypeDef type) { if (type.Methods.Count != 2) return null; - MethodDefinition decrypterMethod = null; - MethodDefinition cctor = null; + MethodDef decrypterMethod = null; + MethodDef cctor = null; foreach (var method in type.Methods) { if (method.Name == ".cctor") { cctor = method; @@ -249,7 +249,7 @@ namespace de4dot.code.deobfuscators.CodeWall { simpleDeobfuscator.deobfuscate(info.Method); info.Resource = findResource(info.Method); if (info.Resource == null) { - Log.w("Could not find encrypted strings resource (Method {0:X8})", info.Method.MetadataToken.ToInt32()); + Log.w("Could not find encrypted strings resource (Method {0:X8})", info.Method.MDToken.ToInt32()); continue; } info.Magic1 = findMagic1(info.Method); @@ -259,11 +259,11 @@ namespace de4dot.code.deobfuscators.CodeWall { } } - EmbeddedResource findResource(MethodDefinition method) { + EmbeddedResource findResource(MethodDef method) { return DotNetUtils.getResource(module, DotNetUtils.getCodeStrings(method)) as EmbeddedResource; } - static int findMagic1(MethodDefinition method) { + static int findMagic1(MethodDef method) { var instrs = method.Body.Instructions; for (int i = 0; i < instrs.Count - 2; i++) { var ldarg = instrs[i]; @@ -279,7 +279,7 @@ namespace de4dot.code.deobfuscators.CodeWall { throw new ApplicationException("Could not find magic1"); } - static int findMagic2(MethodDefinition method) { + static int findMagic2(MethodDef method) { var instrs = method.Body.Instructions; for (int i = 0; i < instrs.Count - 2; i++) { var ldloc = instrs[i]; @@ -295,7 +295,7 @@ namespace de4dot.code.deobfuscators.CodeWall { throw new ApplicationException("Could not find magic2"); } - static int findMagic3(MethodDefinition method) { + static int findMagic3(MethodDef method) { var instrs = method.Body.Instructions; for (int i = 0; i < instrs.Count - 2; i++) { var ldarg = instrs[i]; @@ -311,7 +311,7 @@ namespace de4dot.code.deobfuscators.CodeWall { throw new ApplicationException("Could not find magic3"); } - public string decrypt(MethodDefinition method, int magic1, int magic2, int magic3) { + public string decrypt(MethodDef method, int magic1, int magic2, int magic3) { var info = stringEncrypterInfos.find(method); return info.decrypt(magic1, magic2, magic3); } diff --git a/de4dot.code/deobfuscators/CryptoObfuscator/AntiDebugger.cs b/de4dot.code/deobfuscators/CryptoObfuscator/AntiDebugger.cs index 7b43c0ee..79838171 100644 --- a/de4dot.code/deobfuscators/CryptoObfuscator/AntiDebugger.cs +++ b/de4dot.code/deobfuscators/CryptoObfuscator/AntiDebugger.cs @@ -17,7 +17,7 @@ along with de4dot. If not, see . */ -using Mono.Cecil; +using dot10.DotNet; using de4dot.blocks; namespace de4dot.code.deobfuscators.CryptoObfuscator { @@ -25,14 +25,14 @@ namespace de4dot.code.deobfuscators.CryptoObfuscator { ModuleDefinition module; ISimpleDeobfuscator simpleDeobfuscator; IDeobfuscator deob; - TypeDefinition antiDebuggerType; - MethodDefinition antiDebuggerMethod; + TypeDef antiDebuggerType; + MethodDef antiDebuggerMethod; - public TypeDefinition Type { + public TypeDef Type { get { return antiDebuggerType; } } - public MethodDefinition Method { + public MethodDef Method { get { return antiDebuggerMethod; } } @@ -49,7 +49,7 @@ namespace de4dot.code.deobfuscators.CryptoObfuscator { return; } - bool find(MethodDefinition methodToCheck) { + bool find(MethodDef methodToCheck) { if (methodToCheck == null) return false; foreach (var method in DotNetUtils.getCalledMethods(module, methodToCheck)) { @@ -79,12 +79,12 @@ namespace de4dot.code.deobfuscators.CryptoObfuscator { return false; } - void deobfuscate(MethodDefinition method) { + void deobfuscate(MethodDef method) { simpleDeobfuscator.deobfuscate(method); simpleDeobfuscator.decryptStrings(method, deob); } - bool containsString(MethodDefinition method, string part) { + bool containsString(MethodDef method, string part) { foreach (var s in DotNetUtils.getCodeStrings(method)) { if (s.Contains(part)) return true; diff --git a/de4dot.code/deobfuscators/CryptoObfuscator/AssemblyResolver.cs b/de4dot.code/deobfuscators/CryptoObfuscator/AssemblyResolver.cs index 1dcfc324..0d7e5ab8 100644 --- a/de4dot.code/deobfuscators/CryptoObfuscator/AssemblyResolver.cs +++ b/de4dot.code/deobfuscators/CryptoObfuscator/AssemblyResolver.cs @@ -20,15 +20,15 @@ using System; using System.Collections.Generic; using System.Text; -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using de4dot.blocks; namespace de4dot.code.deobfuscators.CryptoObfuscator { class AssemblyResolver { ModuleDefinition module; - TypeDefinition resolverType; - MethodDefinition resolverMethod; + TypeDef resolverType; + MethodDef resolverMethod; List assemblyInfos = new List(); public class AssemblyInfo { @@ -54,11 +54,11 @@ namespace de4dot.code.deobfuscators.CryptoObfuscator { get { return assemblyInfos; } } - public TypeDefinition Type { + public TypeDef Type { get { return resolverType; } } - public MethodDefinition Method { + public MethodDef Method { get { return resolverMethod; } } @@ -81,7 +81,7 @@ namespace de4dot.code.deobfuscators.CryptoObfuscator { } } - bool checkType(TypeDefinition type, MethodDefinition initMethod) { + bool checkType(TypeDef type, MethodDef initMethod) { if (DotNetUtils.findFieldType(type, "System.Collections.Hashtable", true) == null) return false; if (!checkInitMethod(initMethod)) @@ -102,7 +102,7 @@ namespace de4dot.code.deobfuscators.CryptoObfuscator { return true; } - bool checkInitMethod(MethodDefinition initMethod) { + bool checkInitMethod(MethodDef initMethod) { if (!initMethod.HasBody) return false; diff --git a/de4dot.code/deobfuscators/CryptoObfuscator/CoUtils.cs b/de4dot.code/deobfuscators/CryptoObfuscator/CoUtils.cs index d3f37981..8e566561 100644 --- a/de4dot.code/deobfuscators/CryptoObfuscator/CoUtils.cs +++ b/de4dot.code/deobfuscators/CryptoObfuscator/CoUtils.cs @@ -20,12 +20,12 @@ using System; using System.Collections.Generic; using System.Text; -using Mono.Cecil; +using dot10.DotNet; using de4dot.blocks; namespace de4dot.code.deobfuscators.CryptoObfuscator { static class CoUtils { - public static EmbeddedResource getResource(ModuleDefinition module, MethodDefinition method) { + public static EmbeddedResource getResource(ModuleDefinition module, MethodDef method) { if (method == null || method.Body == null) return null; return getResource(module, DotNetUtils.getCodeStrings(method)); diff --git a/de4dot.code/deobfuscators/CryptoObfuscator/ConstantsDecrypter.cs b/de4dot.code/deobfuscators/CryptoObfuscator/ConstantsDecrypter.cs index 2240e431..91ac2f6d 100644 --- a/de4dot.code/deobfuscators/CryptoObfuscator/ConstantsDecrypter.cs +++ b/de4dot.code/deobfuscators/CryptoObfuscator/ConstantsDecrypter.cs @@ -20,21 +20,21 @@ using System; using System.Collections.Generic; using System.Text; -using Mono.Cecil; +using dot10.DotNet; using de4dot.blocks; namespace de4dot.code.deobfuscators.CryptoObfuscator { class ConstantsDecrypter { ModuleDefinition module; - TypeDefinition decrypterType; - MethodDefinition methodI4; - MethodDefinition methodI8; - MethodDefinition methodR4; - MethodDefinition methodR8; + TypeDef decrypterType; + MethodDef methodI4; + MethodDef methodI8; + MethodDef methodR4; + MethodDef methodR8; EmbeddedResource encryptedResource; byte[] constantsData; - public TypeDefinition Type { + public TypeDef Type { get { return decrypterType; } } @@ -42,19 +42,19 @@ namespace de4dot.code.deobfuscators.CryptoObfuscator { get { return encryptedResource; } } - public MethodDefinition Int32Decrypter { + public MethodDef Int32Decrypter { get { return methodI4; } } - public MethodDefinition Int64Decrypter { + public MethodDef Int64Decrypter { get { return methodI8; } } - public MethodDefinition SingleDecrypter { + public MethodDef SingleDecrypter { get { return methodR4; } } - public MethodDefinition DoubleDecrypter { + public MethodDef DoubleDecrypter { get { return methodR8; } } @@ -79,7 +79,7 @@ namespace de4dot.code.deobfuscators.CryptoObfuscator { static readonly string[] requiredTypes = new string[] { "System.Byte[]", }; - bool checkType(TypeDefinition type) { + bool checkType(TypeDef type) { if (type.Methods.Count != 7) return false; if (type.Fields.Count < 1 || type.Fields.Count > 2) @@ -92,7 +92,7 @@ namespace de4dot.code.deobfuscators.CryptoObfuscator { return true; } - bool checkMethods(TypeDefinition type) { + bool checkMethods(TypeDef type) { methodI4 = DotNetUtils.getMethod(type, "System.Int32", "(System.Int32)"); methodI8 = DotNetUtils.getMethod(type, "System.Int64", "(System.Int32)"); methodR4 = DotNetUtils.getMethod(type, "System.Single", "(System.Int32)"); diff --git a/de4dot.code/deobfuscators/CryptoObfuscator/Deobfuscator.cs b/de4dot.code/deobfuscators/CryptoObfuscator/Deobfuscator.cs index 6d440bad..07a2e682 100644 --- a/de4dot.code/deobfuscators/CryptoObfuscator/Deobfuscator.cs +++ b/de4dot.code/deobfuscators/CryptoObfuscator/Deobfuscator.cs @@ -20,7 +20,7 @@ using System; using System.Collections.Generic; using System.Text.RegularExpressions; -using Mono.Cecil; +using dot10.DotNet; using de4dot.blocks; namespace de4dot.code.deobfuscators.CryptoObfuscator { @@ -149,7 +149,7 @@ namespace de4dot.code.deobfuscators.CryptoObfuscator { foundObfuscatorUserString = Utils.StartsWith(module.GetUserString(0x70000001), "\u0011\"3D9B94A98B-76A8-4810-B1A0-4BE7C4F9C98D", StringComparison.Ordinal); } - void initializeVersion(TypeDefinition attr) { + void initializeVersion(TypeDef attr) { var s = DotNetUtils.getCustomArgAsString(getAssemblyAttribute(attr), 0); if (s == null) return; @@ -282,7 +282,7 @@ namespace de4dot.code.deobfuscators.CryptoObfuscator { public override IEnumerable getStringDecrypterMethods() { var list = new List(); if (stringDecrypter.Method != null) - list.Add(stringDecrypter.Method.MetadataToken.ToInt32()); + list.Add(stringDecrypter.Method.MDToken.ToInt32()); return list; } } diff --git a/de4dot.code/deobfuscators/CryptoObfuscator/MethodBodyReader.cs b/de4dot.code/deobfuscators/CryptoObfuscator/MethodBodyReader.cs index 22860f01..2230e5a8 100644 --- a/de4dot.code/deobfuscators/CryptoObfuscator/MethodBodyReader.cs +++ b/de4dot.code/deobfuscators/CryptoObfuscator/MethodBodyReader.cs @@ -20,8 +20,8 @@ using System; using System.Collections.Generic; using System.IO; -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using de4dot.blocks; namespace de4dot.code.deobfuscators.CryptoObfuscator { @@ -34,7 +34,7 @@ namespace de4dot.code.deobfuscators.CryptoObfuscator { this.module = module; } - public void read(MethodDefinition method) { + public void read(MethodDef method) { this.parameters = getParameters(method); this.Locals = getLocals(method); @@ -55,7 +55,7 @@ namespace de4dot.code.deobfuscators.CryptoObfuscator { return DotNetUtils.getParameters(method); } - static IList getLocals(MethodDefinition method) { + static IList getLocals(MethodDef method) { if (method.Body == null) return new List(); return new List(method.Body.Variables); @@ -115,7 +115,7 @@ namespace de4dot.code.deobfuscators.CryptoObfuscator { return eh; } - public override void restoreMethod(MethodDefinition method) { + public override void restoreMethod(MethodDef method) { base.restoreMethod(method); method.Body.MaxStackSize = maxStackSize; } diff --git a/de4dot.code/deobfuscators/CryptoObfuscator/MethodsDecrypter.cs b/de4dot.code/deobfuscators/CryptoObfuscator/MethodsDecrypter.cs index d55cd744..2de8c43b 100644 --- a/de4dot.code/deobfuscators/CryptoObfuscator/MethodsDecrypter.cs +++ b/de4dot.code/deobfuscators/CryptoObfuscator/MethodsDecrypter.cs @@ -20,24 +20,24 @@ using System; using System.Collections.Generic; using System.IO; -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using de4dot.blocks; namespace de4dot.code.deobfuscators.CryptoObfuscator { class MethodsDecrypter { ModuleDefinition module; - TypeDefinition decrypterType; - MethodDefinition decryptMethod; - MethodDefinition decrypterCctor; + TypeDef decrypterType; + MethodDef decryptMethod; + MethodDef decrypterCctor; EmbeddedResource resource; - List delegateTypes = new List(); + List delegateTypes = new List(); - public TypeDefinition Type { + public TypeDef Type { get { return decrypterType; } } - public IEnumerable DelegateTypes { + public IEnumerable DelegateTypes { get { return delegateTypes; } } @@ -65,7 +65,7 @@ namespace de4dot.code.deobfuscators.CryptoObfuscator { "System.Collections.Generic.Dictionary`2", "System.ModuleHandle", }; - bool check(TypeDefinition type) { + bool check(TypeDef type) { if (type.NestedTypes.Count != 1) return false; if (type.Fields.Count != 3) @@ -98,7 +98,7 @@ namespace de4dot.code.deobfuscators.CryptoObfuscator { "System.Type", "System.Type[]", }; - static MethodDefinition findDecryptMethod(TypeDefinition type) { + static MethodDef findDecryptMethod(TypeDef type) { foreach (var method in type.Methods) { if (!method.IsStatic || method.Body == null) continue; @@ -136,7 +136,7 @@ namespace de4dot.code.deobfuscators.CryptoObfuscator { } void decrypt(BinaryReader reader, int delegateTypeToken) { - var delegateType = module.LookupToken(delegateTypeToken) as TypeDefinition; + var delegateType = module.LookupToken(delegateTypeToken) as TypeDef; if (delegateType == null) throw new ApplicationException("Couldn't find delegate type"); @@ -148,7 +148,7 @@ namespace de4dot.code.deobfuscators.CryptoObfuscator { var encType = module.LookupToken(encDeclToken) as TypeReference; if (encType == null) throw new ApplicationException("Invalid declaring type token"); - var encMethod = module.LookupToken(encMethToken) as MethodDefinition; + var encMethod = module.LookupToken(encMethToken) as MethodDef; if (encMethod == null) throw new ApplicationException("Invalid encrypted method token"); @@ -157,14 +157,14 @@ namespace de4dot.code.deobfuscators.CryptoObfuscator { bodyReader.restoreMethod(encMethod); Log.v("Restored method {0} ({1:X8}). Instrs:{2}, Locals:{3}, Exceptions:{4}", Utils.removeNewlines(encMethod.FullName), - encMethod.MetadataToken.ToInt32(), + encMethod.MDToken.ToInt32(), encMethod.Body.Instructions.Count, encMethod.Body.Variables.Count, encMethod.Body.ExceptionHandlers.Count); delegateTypes.Add(delegateType); } - bool getTokens(TypeDefinition delegateType, out int delegateToken, out int encMethodToken, out int encDeclaringTypeToken) { + bool getTokens(TypeDef delegateType, out int delegateToken, out int encMethodToken, out int encDeclaringTypeToken) { delegateToken = 0; encMethodToken = 0; encDeclaringTypeToken = 0; @@ -187,7 +187,7 @@ namespace de4dot.code.deobfuscators.CryptoObfuscator { var call = instrs[i + 3]; if (call.OpCode.Code != Code.Call) continue; - var calledMethod = call.Operand as MethodDefinition; + var calledMethod = call.Operand as MethodDef; if (calledMethod == null) continue; if (calledMethod != decryptMethod) diff --git a/de4dot.code/deobfuscators/CryptoObfuscator/ProxyCallFixer.cs b/de4dot.code/deobfuscators/CryptoObfuscator/ProxyCallFixer.cs index 36110571..66eefde6 100644 --- a/de4dot.code/deobfuscators/CryptoObfuscator/ProxyCallFixer.cs +++ b/de4dot.code/deobfuscators/CryptoObfuscator/ProxyCallFixer.cs @@ -19,13 +19,13 @@ using System; using System.Collections.Generic; -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using de4dot.blocks; namespace de4dot.code.deobfuscators.CryptoObfuscator { class ProxyCallFixer : ProxyCallFixer2 { - Dictionary methodToType = new Dictionary(); + Dictionary methodToType = new Dictionary(); public ProxyCallFixer(ModuleDefinition module) : base(module) { @@ -51,7 +51,7 @@ namespace de4dot.code.deobfuscators.CryptoObfuscator { } } - protected override object checkCctor(TypeDefinition type, MethodDefinition cctor) { + protected override object checkCctor(TypeDef type, MethodDef cctor) { var instructions = cctor.Body.Instructions; for (int i = 0; i < instructions.Count; i++) { var instrs = DotNetUtils.getInstructions(instructions, i, OpCodes.Ldc_I4, OpCodes.Ldc_I4, OpCodes.Ldc_I4, OpCodes.Call); @@ -61,7 +61,7 @@ namespace de4dot.code.deobfuscators.CryptoObfuscator { int typeToken = (int)instrs[0].Operand; int methodToken = (int)instrs[1].Operand; int declaringTypeToken = (int)instrs[2].Operand; - var createMethod = instrs[3].Operand as MethodDefinition; + var createMethod = instrs[3].Operand as MethodDef; ProxyCreatorType proxyCreatorType; if (!methodToType.TryGetValue(createMethod, out proxyCreatorType)) @@ -73,7 +73,7 @@ namespace de4dot.code.deobfuscators.CryptoObfuscator { return null; } - protected override void getCallInfo(object context, FieldDefinition field, out MethodReference calledMethod, out OpCode callOpcode) { + protected override void getCallInfo(object context, FieldDef field, out MethodReference calledMethod, out OpCode callOpcode) { var ctx = (Context)context; switch (ctx.proxyCreatorType) { @@ -107,13 +107,13 @@ namespace de4dot.code.deobfuscators.CryptoObfuscator { } } - MethodDefinition getProxyCreateMethod(TypeDefinition type) { + MethodDef getProxyCreateMethod(TypeDef type) { if (DotNetUtils.findFieldType(type, "System.ModuleHandle", true) == null) return null; if (type.Fields.Count < 1 || type.Fields.Count > 12) return null; - MethodDefinition createMethod = null; + MethodDef createMethod = null; foreach (var m in type.Methods) { if (m.Name == ".ctor" || m.Name == ".cctor") continue; @@ -131,7 +131,7 @@ namespace de4dot.code.deobfuscators.CryptoObfuscator { return createMethod; } - ProxyCreatorType getProxyCreatorType(TypeDefinition type, MethodDefinition createMethod) { + ProxyCreatorType getProxyCreatorType(TypeDef type, MethodDef createMethod) { int numCalls = 0, numCallvirts = 0, numNewobjs = 0; foreach (var instr in createMethod.Body.Instructions) { if (instr.OpCode.Code != Code.Ldsfld) diff --git a/de4dot.code/deobfuscators/CryptoObfuscator/ResourceDecrypter.cs b/de4dot.code/deobfuscators/CryptoObfuscator/ResourceDecrypter.cs index 406c9804..d2a08c4a 100644 --- a/de4dot.code/deobfuscators/CryptoObfuscator/ResourceDecrypter.cs +++ b/de4dot.code/deobfuscators/CryptoObfuscator/ResourceDecrypter.cs @@ -22,15 +22,15 @@ using System.Collections.Generic; using System.IO; using System.IO.Compression; using System.Security.Cryptography; -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using de4dot.blocks; namespace de4dot.code.deobfuscators.CryptoObfuscator { class ResourceDecrypter { const int BUFLEN = 0x8000; ModuleDefinition module; - TypeDefinition resourceDecrypterType; + TypeDef resourceDecrypterType; byte[] buffer1 = new byte[BUFLEN]; byte[] buffer2 = new byte[BUFLEN]; byte desEncryptedFlag; @@ -97,7 +97,7 @@ namespace de4dot.code.deobfuscators.CryptoObfuscator { return false; } - bool checkCctor(MethodDefinition cctor) { + bool checkCctor(MethodDef cctor) { if (cctor.Body == null) return false; int stsfldCount = 0; @@ -182,7 +182,7 @@ namespace de4dot.code.deobfuscators.CryptoObfuscator { bitwiseNotEncryptedFlag = 4; } - static bool checkFlipBits(MethodDefinition method) { + static bool checkFlipBits(MethodDef method) { var instrs = method.Body.Instructions; for (int i = 0; i < instrs.Count - 1; i++) { var ldloc = instrs[i]; @@ -202,7 +202,7 @@ namespace de4dot.code.deobfuscators.CryptoObfuscator { return false; } - bool updateFlags(MethodDefinition method, ISimpleDeobfuscator simpleDeobfuscator) { + bool updateFlags(MethodDef method, ISimpleDeobfuscator simpleDeobfuscator) { if (method == null || method.Body == null) return false; @@ -266,7 +266,7 @@ namespace de4dot.code.deobfuscators.CryptoObfuscator { return false; } - static int getHeaderSkipBytes(MethodDefinition method) { + static int getHeaderSkipBytes(MethodDef method) { var instrs = method.Body.Instructions; for (int i = 0; i < instrs.Count - 1; i++) { var ldci4 = instrs[i]; @@ -291,11 +291,11 @@ namespace de4dot.code.deobfuscators.CryptoObfuscator { return false; } - MethodDefinition getDecrypterMethod() { + MethodDef getDecrypterMethod() { return getDecrypterMethod(resourceDecrypterType); } - static MethodDefinition getDecrypterMethod(TypeDefinition type) { + static MethodDef getDecrypterMethod(TypeDef type) { foreach (var method in type.Methods) { if (DotNetUtils.isMethod(method, "System.Byte[]", "(System.IO.Stream)")) return method; diff --git a/de4dot.code/deobfuscators/CryptoObfuscator/ResourceResolver.cs b/de4dot.code/deobfuscators/CryptoObfuscator/ResourceResolver.cs index e73f4194..70b3442a 100644 --- a/de4dot.code/deobfuscators/CryptoObfuscator/ResourceResolver.cs +++ b/de4dot.code/deobfuscators/CryptoObfuscator/ResourceResolver.cs @@ -19,16 +19,16 @@ using System; using System.Collections.Generic; -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using de4dot.blocks; namespace de4dot.code.deobfuscators.CryptoObfuscator { class ResourceResolver { ModuleDefinition module; ResourceDecrypter resourceDecrypter; - TypeDefinition resolverType; - MethodDefinition resolverMethod; + TypeDef resolverType; + MethodDef resolverMethod; ResolverVersion resolverVersion = ResolverVersion.V1; bool mergedIt = false; @@ -38,11 +38,11 @@ namespace de4dot.code.deobfuscators.CryptoObfuscator { V2, } - public TypeDefinition Type { + public TypeDef Type { get { return resolverType; } } - public MethodDefinition Method { + public MethodDef Method { get { return resolverMethod; } } @@ -99,7 +99,7 @@ namespace de4dot.code.deobfuscators.CryptoObfuscator { return names; } - bool checkType(MethodDefinition initMethod) { + bool checkType(MethodDef initMethod) { if (!initMethod.HasBody) return false; if (DotNetUtils.findFieldType(initMethod.DeclaringType, "System.Reflection.Assembly", true) == null) @@ -116,7 +116,7 @@ namespace de4dot.code.deobfuscators.CryptoObfuscator { return true; } - ResolverVersion checkSetupMethod(MethodDefinition setupMethod) { + ResolverVersion checkSetupMethod(MethodDef setupMethod) { var instructions = setupMethod.Body.Instructions; int foundCount = 0; for (int i = 0; i < instructions.Count; i++) { diff --git a/de4dot.code/deobfuscators/CryptoObfuscator/StringDecrypter.cs b/de4dot.code/deobfuscators/CryptoObfuscator/StringDecrypter.cs index a2fbfa0f..96dd017a 100644 --- a/de4dot.code/deobfuscators/CryptoObfuscator/StringDecrypter.cs +++ b/de4dot.code/deobfuscators/CryptoObfuscator/StringDecrypter.cs @@ -19,26 +19,26 @@ using System; using System.Text; -using Mono.Cecil; +using dot10.DotNet; using de4dot.blocks; namespace de4dot.code.deobfuscators.CryptoObfuscator { class StringDecrypter { ModuleDefinition module; EmbeddedResource stringResource; - TypeDefinition stringDecrypterType; - MethodDefinition stringDecrypterMethod; + TypeDef stringDecrypterType; + MethodDef stringDecrypterMethod; byte[] decryptedData; public bool Detected { get { return stringDecrypterType != null; } } - public TypeDefinition Type { + public TypeDef Type { get { return stringDecrypterType; } } - public MethodDefinition Method { + public MethodDef Method { get { return stringDecrypterMethod; } } @@ -51,8 +51,8 @@ namespace de4dot.code.deobfuscators.CryptoObfuscator { } public void find() { - TypeDefinition type; - MethodDefinition method; + TypeDef type; + MethodDef method; if (!findStringDecrypterType(out type, out method)) return; @@ -98,7 +98,7 @@ namespace de4dot.code.deobfuscators.CryptoObfuscator { return Encoding.Unicode.GetString(decryptedData, index, len); } - bool findStringDecrypterType(out TypeDefinition theType, out MethodDefinition theMethod) { + bool findStringDecrypterType(out TypeDef theType, out MethodDef theMethod) { theType = null; theMethod = null; @@ -114,7 +114,7 @@ namespace de4dot.code.deobfuscators.CryptoObfuscator { if (type.NestedTypes.Count > 0) continue; - MethodDefinition method = null; + MethodDef method = null; foreach (var m in type.Methods) { if (m.Name == ".ctor" || m.Name == ".cctor") continue; diff --git a/de4dot.code/deobfuscators/CryptoObfuscator/TamperDetection.cs b/de4dot.code/deobfuscators/CryptoObfuscator/TamperDetection.cs index be7f0e6e..c975df70 100644 --- a/de4dot.code/deobfuscators/CryptoObfuscator/TamperDetection.cs +++ b/de4dot.code/deobfuscators/CryptoObfuscator/TamperDetection.cs @@ -17,25 +17,25 @@ along with de4dot. If not, see . */ -using Mono.Cecil; +using dot10.DotNet; using de4dot.blocks; namespace de4dot.code.deobfuscators.CryptoObfuscator { class TamperDetection { ModuleDefinition module; - TypeDefinition tamperType; - MethodDefinition tamperMethod; + TypeDef tamperType; + MethodDef tamperMethod; FrameworkType frameworkType; public bool Detected { get { return tamperMethod != null; } } - public TypeDefinition Type { + public TypeDef Type { get { return tamperType; } } - public MethodDefinition Method { + public MethodDef Method { get { return tamperMethod; } } @@ -51,7 +51,7 @@ namespace de4dot.code.deobfuscators.CryptoObfuscator { return; } - bool find(MethodDefinition methodToCheck) { + bool find(MethodDef methodToCheck) { if (methodToCheck == null) return false; @@ -79,7 +79,7 @@ namespace de4dot.code.deobfuscators.CryptoObfuscator { return false; } - bool findDesktop(MethodDefinition method) { + bool findDesktop(MethodDef method) { var type = method.DeclaringType; if (!method.IsStatic || !DotNetUtils.isMethod(method, "System.Void", "()")) @@ -107,7 +107,7 @@ namespace de4dot.code.deobfuscators.CryptoObfuscator { "System.Reflection.AssemblyName", "System.String", }; - bool findSilverlight(MethodDefinition method) { + bool findSilverlight(MethodDef method) { if (!new LocalTypes(method).exactly(requiredLocals_sl)) return false; if (!DotNetUtils.callsMethod(method, "System.Int32 System.String::get_Length()")) @@ -136,7 +136,7 @@ namespace de4dot.code.deobfuscators.CryptoObfuscator { "System.Int32", "System.String", }; - bool findCompactFramework(MethodDefinition method) { + bool findCompactFramework(MethodDef method) { if (!new LocalTypes(method).exactly(requiredLocals_cf)) return false; if (!DotNetUtils.callsMethod(method, "System.Int32 System.String::get_Length()")) diff --git a/de4dot.code/deobfuscators/DeepSea/ArrayBlockDeobfuscator.cs b/de4dot.code/deobfuscators/DeepSea/ArrayBlockDeobfuscator.cs index 3cf42f89..a51ea0a8 100644 --- a/de4dot.code/deobfuscators/DeepSea/ArrayBlockDeobfuscator.cs +++ b/de4dot.code/deobfuscators/DeepSea/ArrayBlockDeobfuscator.cs @@ -18,8 +18,8 @@ */ using System.Collections.Generic; -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using de4dot.blocks; using de4dot.blocks.cflow; diff --git a/de4dot.code/deobfuscators/DeepSea/ArrayBlockState.cs b/de4dot.code/deobfuscators/DeepSea/ArrayBlockState.cs index 383129ee..666e25b3 100644 --- a/de4dot.code/deobfuscators/DeepSea/ArrayBlockState.cs +++ b/de4dot.code/deobfuscators/DeepSea/ArrayBlockState.cs @@ -18,8 +18,8 @@ */ using System.Collections.Generic; -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using Mono.Cecil.Metadata; using de4dot.blocks; @@ -29,11 +29,11 @@ namespace de4dot.code.deobfuscators.DeepSea { FieldDefinitionAndDeclaringTypeDict fieldToInfo = new FieldDefinitionAndDeclaringTypeDict(); public class FieldInfo { - public readonly FieldDefinition field; - public readonly FieldDefinition arrayInitField; + public readonly FieldDef field; + public readonly FieldDef arrayInitField; public readonly byte[] array; - public FieldInfo(FieldDefinition field, FieldDefinition arrayInitField) { + public FieldInfo(FieldDef field, FieldDef arrayInitField) { this.field = field; this.arrayInitField = arrayInitField; this.array = (byte[])arrayInitField.InitialValue.Clone(); @@ -52,14 +52,14 @@ namespace de4dot.code.deobfuscators.DeepSea { initializeArrays(simpleDeobfuscator, DotNetUtils.getModuleTypeCctor(module)); } - void initializeArrays(ISimpleDeobfuscator simpleDeobfuscator, MethodDefinition method) { + void initializeArrays(ISimpleDeobfuscator simpleDeobfuscator, MethodDef method) { if (method == null || method.Body == null) return; while (initializeArrays2(simpleDeobfuscator, method)) { } } - bool initializeArrays2(ISimpleDeobfuscator simpleDeobfuscator, MethodDefinition method) { + bool initializeArrays2(ISimpleDeobfuscator simpleDeobfuscator, MethodDef method) { bool foundField = false; simpleDeobfuscator.deobfuscate(method, true); var instructions = method.Body.Instructions; @@ -76,7 +76,7 @@ namespace de4dot.code.deobfuscators.DeepSea { if (arrayType == null || arrayType.EType != ElementType.U1) continue; - var arrayInitField = instrs[2].Operand as FieldDefinition; + var arrayInitField = instrs[2].Operand as FieldDef; if (arrayInitField == null || arrayInitField.InitialValue == null || arrayInitField.InitialValue.Length == 0) continue; @@ -84,7 +84,7 @@ namespace de4dot.code.deobfuscators.DeepSea { if (calledMethod == null || calledMethod.FullName != "System.Void System.Runtime.CompilerServices.RuntimeHelpers::InitializeArray(System.Array,System.RuntimeFieldHandle)") continue; - var targetField = instrs[4].Operand as FieldDefinition; + var targetField = instrs[4].Operand as FieldDef; if (targetField == null) continue; @@ -102,8 +102,8 @@ namespace de4dot.code.deobfuscators.DeepSea { return fieldToInfo.find(fieldRef); } - public IEnumerable cleanUp() { - var removedFields = new List(); + public IEnumerable cleanUp() { + var removedFields = new List(); var moduleCctor = DotNetUtils.getModuleTypeCctor(module); if (moduleCctor == null) return removedFields; diff --git a/de4dot.code/deobfuscators/DeepSea/AssemblyResolver.cs b/de4dot.code/deobfuscators/DeepSea/AssemblyResolver.cs index 49e0a041..6a777f93 100644 --- a/de4dot.code/deobfuscators/DeepSea/AssemblyResolver.cs +++ b/de4dot.code/deobfuscators/DeepSea/AssemblyResolver.cs @@ -21,15 +21,15 @@ using System; using System.Collections.Generic; using System.IO; using System.Text.RegularExpressions; -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using de4dot.blocks; namespace de4dot.code.deobfuscators.DeepSea { class AssemblyResolver : ResolverBase { Version version; List fieldInfos; - MethodDefinition decryptMethod; + MethodDef decryptMethod; enum Version { Unknown, @@ -62,16 +62,16 @@ namespace de4dot.code.deobfuscators.DeepSea { } class FieldInfo { - public FieldDefinition field; + public FieldDef field; public int magic; - public FieldInfo(FieldDefinition field, int magic) { + public FieldInfo(FieldDef field, int magic) { this.field = field; this.magic = magic; } } - public MethodDefinition DecryptMethod { + public MethodDef DecryptMethod { get { return decryptMethod; } } @@ -86,7 +86,7 @@ namespace de4dot.code.deobfuscators.DeepSea { "System.Security.Cryptography.SHA1Managed", "System.Windows.AssemblyPart", }; - protected override bool checkResolverInitMethodSilverlight(MethodDefinition resolverInitMethod) { + protected override bool checkResolverInitMethodSilverlight(MethodDef resolverInitMethod) { if (resolverInitMethod.Body.ExceptionHandlers.Count != 1) return false; @@ -110,7 +110,7 @@ namespace de4dot.code.deobfuscators.DeepSea { return false; } - void updateVersion(MethodDefinition handler) { + void updateVersion(MethodDef handler) { if (isV3Old(handler)) { version = Version.V3Old; return; @@ -125,7 +125,7 @@ namespace de4dot.code.deobfuscators.DeepSea { } } - static bool isV3SL(MethodDefinition handler) { + static bool isV3SL(MethodDef handler) { var instrs = handler.Body.Instructions; for (int i = 0; i < instrs.Count - 3; i++) { if (!DotNetUtils.isLdloc(instrs[i])) @@ -141,7 +141,7 @@ namespace de4dot.code.deobfuscators.DeepSea { return false; } - static bool isV41SL(MethodDefinition handler) { + static bool isV41SL(MethodDef handler) { var instrs = handler.Body.Instructions; for (int i = 0; i < instrs.Count; i++) { if (!DotNetUtils.isLdcI4(instrs[i]) || DotNetUtils.getLdcI4Value(instrs[i]) != 5) @@ -157,18 +157,18 @@ namespace de4dot.code.deobfuscators.DeepSea { return false; } - static bool isV3Old(MethodDefinition method) { + static bool isV3Old(MethodDef method) { return DotNetUtils.callsMethod(method, "System.Int32 System.IO.Stream::Read(System.Byte[],System.Int32,System.Int32)") && !DotNetUtils.callsMethod(method, "System.Int32 System.IO.Stream::ReadByte()") && // Obfuscated System.Int32 System.IO.Stream::ReadByte() !DotNetUtils.callsMethod(method, "System.Int32", "(System.IO.Stream,System.Int32,System.Int32)"); } - protected override bool checkResolverInitMethodInternal(MethodDefinition resolverInitMethod) { + protected override bool checkResolverInitMethodInternal(MethodDef resolverInitMethod) { return DotNetUtils.callsMethod(resolverInitMethod, "System.Void System.AppDomain::add_AssemblyResolve(System.ResolveEventHandler)"); } - protected override bool checkHandlerMethodDesktopInternal(MethodDefinition handler) { + protected override bool checkHandlerMethodDesktopInternal(MethodDef handler) { if (checkHandlerV3(handler) || checkHandlerSL(handler)) { updateVersion(handler); return true; @@ -176,7 +176,7 @@ namespace de4dot.code.deobfuscators.DeepSea { simpleDeobfuscator.deobfuscate(handler); List fieldInfosTmp; - MethodDefinition decryptMethodTmp; + MethodDef decryptMethodTmp; if (checkHandlerV4(handler, out fieldInfosTmp, out decryptMethodTmp)) { version = Version.V4; fieldInfos = fieldInfosTmp; @@ -204,7 +204,7 @@ namespace de4dot.code.deobfuscators.DeepSea { "System.Security.Cryptography.SHA1CryptoServiceProvider", "System.String", }; - static bool checkHandlerV3(MethodDefinition handler) { + static bool checkHandlerV3(MethodDef handler) { return new LocalTypes(handler).all(handlerLocalTypes_NET); } @@ -216,12 +216,12 @@ namespace de4dot.code.deobfuscators.DeepSea { "System.String", "System.Windows.AssemblyPart", }; - static bool checkHandlerSL(MethodDefinition handler) { + static bool checkHandlerSL(MethodDef handler) { return new LocalTypes(handler).all(handlerLocalTypes_SL); } // 4.0.1.18 .. 4.0.3 - bool checkHandlerV4(MethodDefinition handler, out List fieldInfos, out MethodDefinition decryptMethod) { + bool checkHandlerV4(MethodDef handler, out List fieldInfos, out MethodDef decryptMethod) { fieldInfos = new List(); decryptMethod = null; @@ -232,7 +232,7 @@ namespace de4dot.code.deobfuscators.DeepSea { var ldtoken = instrs[index++]; if (ldtoken.OpCode.Code != Code.Ldtoken) continue; - var field = ldtoken.Operand as FieldDefinition; + var field = ldtoken.Operand as FieldDef; if (field == null || field.InitialValue == null || field.InitialValue.Length == 0) return false; @@ -252,7 +252,7 @@ namespace de4dot.code.deobfuscators.DeepSea { call = instrs[index++]; if (call.OpCode.Code != Code.Call) return false; - var decryptMethodTmp = call.Operand as MethodDefinition; + var decryptMethodTmp = call.Operand as MethodDef; if (!DotNetUtils.isMethod(decryptMethodTmp, "System.Reflection.Assembly", "(System.RuntimeFieldHandle,System.Int32,System.Int32)")) return false; @@ -264,7 +264,7 @@ namespace de4dot.code.deobfuscators.DeepSea { } // 4.0.4, 4.1+ - Version checkHandlerV404_41(MethodDefinition handler, out List fieldInfos, out MethodDefinition decryptMethod) { + Version checkHandlerV404_41(MethodDef handler, out List fieldInfos, out MethodDef decryptMethod) { Version version = Version.Unknown; fieldInfos = new List(); decryptMethod = null; @@ -286,7 +286,7 @@ namespace de4dot.code.deobfuscators.DeepSea { var ldtoken = instrs[index++]; if (ldtoken.OpCode.Code != Code.Ldtoken) continue; - var field = ldtoken.Operand as FieldDefinition; + var field = ldtoken.Operand as FieldDef; if (field == null || field.InitialValue == null || field.InitialValue.Length == 0) continue; @@ -302,7 +302,7 @@ namespace de4dot.code.deobfuscators.DeepSea { var args = DsUtils.getArgValues(instrs, callIndex); if (args == null) continue; - var decryptMethodTmp = instrs[callIndex].Operand as MethodDefinition; + var decryptMethodTmp = instrs[callIndex].Operand as MethodDef; if (decryptMethodTmp == null) continue; int magic; @@ -317,7 +317,7 @@ namespace de4dot.code.deobfuscators.DeepSea { return version; } - static bool getMagic(MethodDefinition method, IList args, out Version version, out int magic) { + static bool getMagic(MethodDef method, IList args, out Version version, out int magic) { magic = 0; int magicIndex = getMagicIndex(method, out version); if (magicIndex < 0 || magicIndex >= args.Count) @@ -330,7 +330,7 @@ namespace de4dot.code.deobfuscators.DeepSea { return true; } - static int getMagicIndex(MethodDefinition method, out Version version) { + static int getMagicIndex(MethodDef method, out Version version) { int magicIndex = getMagicIndex404(method); if (magicIndex >= 0) { version = Version.V404; @@ -347,7 +347,7 @@ namespace de4dot.code.deobfuscators.DeepSea { return -1; } - static int getMagicIndex404(MethodDefinition method) { + static int getMagicIndex404(MethodDef method) { var instrs = method.Body.Instructions; for (int i = 0; i < instrs.Count - 4; i++) { int index = i; @@ -368,7 +368,7 @@ namespace de4dot.code.deobfuscators.DeepSea { return -1; } - static int getMagicIndex41Trial(MethodDefinition method) { + static int getMagicIndex41Trial(MethodDef method) { var instrs = method.Body.Instructions; for (int i = 0; i < instrs.Count - 4; i++) { int index = i; diff --git a/de4dot.code/deobfuscators/DeepSea/CastDeobfuscator.cs b/de4dot.code/deobfuscators/DeepSea/CastDeobfuscator.cs index 1bb9c5f6..2458b358 100644 --- a/de4dot.code/deobfuscators/DeepSea/CastDeobfuscator.cs +++ b/de4dot.code/deobfuscators/DeepSea/CastDeobfuscator.cs @@ -21,8 +21,8 @@ using System; using System.Collections.Generic; using de4dot.blocks; using de4dot.blocks.cflow; -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; namespace de4dot.code.deobfuscators.DeepSea { class CastDeobfuscator : IBlocksDeobfuscator { @@ -67,7 +67,7 @@ namespace de4dot.code.deobfuscators.DeepSea { public override string ToString() { if (type == null) return string.Format("{0} - INVALID", local); - return string.Format("{0} - {1:X8} {2}", local, type.MetadataToken.ToInt32(), type.FullName); + return string.Format("{0} - {1:X8} {2}", local, type.MDToken.ToInt32(), type.FullName); } } diff --git a/de4dot.code/deobfuscators/DeepSea/Deobfuscator.cs b/de4dot.code/deobfuscators/DeepSea/Deobfuscator.cs index 0a3bede0..4e679c1b 100644 --- a/de4dot.code/deobfuscators/DeepSea/Deobfuscator.cs +++ b/de4dot.code/deobfuscators/DeepSea/Deobfuscator.cs @@ -18,7 +18,7 @@ */ using System.Collections.Generic; -using Mono.Cecil; +using dot10.DotNet; using de4dot.blocks; using de4dot.blocks.cflow; @@ -292,7 +292,7 @@ done: public override IEnumerable getStringDecrypterMethods() { var list = new List(); foreach (var method in stringDecrypter.DecrypterMethods) - list.Add(method.MetadataToken.ToInt32()); + list.Add(method.MDToken.ToInt32()); return list; } } diff --git a/de4dot.code/deobfuscators/DeepSea/DsConstantsReader.cs b/de4dot.code/deobfuscators/DeepSea/DsConstantsReader.cs index b42aa156..02b94d18 100644 --- a/de4dot.code/deobfuscators/DeepSea/DsConstantsReader.cs +++ b/de4dot.code/deobfuscators/DeepSea/DsConstantsReader.cs @@ -18,7 +18,7 @@ */ using System.Collections.Generic; -using Mono.Cecil.Cil; +using dot10.DotNet.Emit; using de4dot.blocks; namespace de4dot.code.deobfuscators.DeepSea { diff --git a/de4dot.code/deobfuscators/DeepSea/DsInlinedMethodsFinder.cs b/de4dot.code/deobfuscators/DeepSea/DsInlinedMethodsFinder.cs index 57e92ae5..8809bba4 100644 --- a/de4dot.code/deobfuscators/DeepSea/DsInlinedMethodsFinder.cs +++ b/de4dot.code/deobfuscators/DeepSea/DsInlinedMethodsFinder.cs @@ -18,16 +18,16 @@ */ using System.Collections.Generic; -using Mono.Cecil; +using dot10.DotNet; namespace de4dot.code.deobfuscators.DeepSea { static class DsInlinedMethodsFinder { - public static List find(ModuleDefinition module, IEnumerable notInlinedMethods) { - var notInlinedMethodsDict = new Dictionary(); + public static List find(ModuleDefinition module, IEnumerable notInlinedMethods) { + var notInlinedMethodsDict = new Dictionary(); foreach (var method in notInlinedMethods) notInlinedMethodsDict[method] = true; - var inlinedMethods = new List(); + var inlinedMethods = new List(); foreach (var type in module.GetTypes()) { foreach (var method in type.Methods) { diff --git a/de4dot.code/deobfuscators/DeepSea/DsMethodCallInliner.cs b/de4dot.code/deobfuscators/DeepSea/DsMethodCallInliner.cs index 8d3deefe..66bc416c 100644 --- a/de4dot.code/deobfuscators/DeepSea/DsMethodCallInliner.cs +++ b/de4dot.code/deobfuscators/DeepSea/DsMethodCallInliner.cs @@ -18,8 +18,8 @@ */ using System.Collections.Generic; -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using Mono.Cecil.Metadata; using de4dot.blocks; using de4dot.blocks.cflow; @@ -30,7 +30,7 @@ namespace de4dot.code.deobfuscators.DeepSea { List parameters; ParameterDefinition arg1, arg2; Value returnValue; - MethodDefinition methodToInline; + MethodDef methodToInline; CachedCflowDeobfuscator cflowDeobfuscator; public DsMethodCallInliner(CachedCflowDeobfuscator cflowDeobfuscator) { @@ -51,7 +51,7 @@ namespace de4dot.code.deobfuscators.DeepSea { } bool inlineMethod(Instruction callInstr, int instrIndex) { - var method = callInstr.Operand as MethodDefinition; + var method = callInstr.Operand as MethodDef; if (method == null) return false; if (!canInline(method)) @@ -70,7 +70,7 @@ namespace de4dot.code.deobfuscators.DeepSea { return true; } - bool inlineMethod(MethodDefinition methodToInline, int instrIndex, int const1, int const2) { + bool inlineMethod(MethodDef methodToInline, int instrIndex, int const1, int const2) { this.methodToInline = methodToInline = cflowDeobfuscator.deobfuscate(methodToInline); parameters = DotNetUtils.getParameters(methodToInline); @@ -294,7 +294,7 @@ done: return instructionEmulator.stackSize() == 0; } - public static bool canInline(MethodDefinition method) { + public static bool canInline(MethodDef method) { if (method == null || method.Body == null) return false; if (method.Attributes != (MethodAttributes.Assembly | MethodAttributes.Static)) @@ -322,7 +322,7 @@ done: return etype == ElementType.Char || etype == ElementType.I2 || etype == ElementType.I4; } - protected override bool isReturn(MethodDefinition methodToInline, int instrIndex) { + protected override bool isReturn(MethodDef methodToInline, int instrIndex) { int oldIndex = instrIndex; if (base.isReturn(methodToInline, oldIndex)) return true; diff --git a/de4dot.code/deobfuscators/DeepSea/DsUtils.cs b/de4dot.code/deobfuscators/DeepSea/DsUtils.cs index 3366a669..f3734d15 100644 --- a/de4dot.code/deobfuscators/DeepSea/DsUtils.cs +++ b/de4dot.code/deobfuscators/DeepSea/DsUtils.cs @@ -18,8 +18,8 @@ */ using System.Collections.Generic; -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using de4dot.blocks; namespace de4dot.code.deobfuscators.DeepSea { @@ -40,7 +40,7 @@ namespace de4dot.code.deobfuscators.DeepSea { return args; } - public static bool getArgValue(MethodDefinition method, int index, out object arg) { + public static bool getArgValue(MethodDef method, int index, out object arg) { return getArgValue(method.Body.Instructions[index], out arg); } diff --git a/de4dot.code/deobfuscators/DeepSea/FieldsRestorer.cs b/de4dot.code/deobfuscators/DeepSea/FieldsRestorer.cs index dfb5b30f..a15346a7 100644 --- a/de4dot.code/deobfuscators/DeepSea/FieldsRestorer.cs +++ b/de4dot.code/deobfuscators/DeepSea/FieldsRestorer.cs @@ -19,8 +19,8 @@ using System; using System.Collections.Generic; -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using Mono.Cecil.Metadata; using de4dot.blocks; @@ -28,13 +28,13 @@ namespace de4dot.code.deobfuscators.DeepSea { // DS 4.x can move fields from a class to a struct. This class restores the fields. class FieldsRestorer { ModuleDefinition module; - TypeDefinitionDict> structToOwners = new TypeDefinitionDict>(); + TypeDefinitionDict> structToOwners = new TypeDefinitionDict>(); FieldDefinitionAndDeclaringTypeDict structFieldsToFix = new FieldDefinitionAndDeclaringTypeDict(); - TypeDefinitionDict> typeToFieldsDict = new TypeDefinitionDict>(); + TypeDefinitionDict> typeToFieldsDict = new TypeDefinitionDict>(); - public List FieldStructs { + public List FieldStructs { get { - var list = new List(structToOwners.Count); + var list = new List(structToOwners.Count); foreach (var structType in structToOwners.getKeys()) { if (!hasNoMethods(structType)) continue; @@ -45,7 +45,7 @@ namespace de4dot.code.deobfuscators.DeepSea { } } - static bool hasNoMethods(TypeDefinition type) { + static bool hasNoMethods(TypeDef type) { if (type.Methods.Count == 0) return true; if (type.BaseType == null) @@ -77,7 +77,7 @@ namespace de4dot.code.deobfuscators.DeepSea { break; } - var fieldsDict = new FieldDefinitionAndDeclaringTypeDict(); + var fieldsDict = new FieldDefinitionAndDeclaringTypeDict(); typeToFieldsDict.add(ownerType, fieldsDict); foreach (var structField in structType.Fields) { var newField = DotNetUtils.createFieldDefinition(structField.Name, structField.Attributes, structField.FieldType); @@ -88,9 +88,9 @@ namespace de4dot.code.deobfuscators.DeepSea { } } - Dictionary> getMovedTypes() { - var candidates = new Dictionary>(); - var typeToStruct = new Dictionary(); + Dictionary> getMovedTypes() { + var candidates = new Dictionary>(); + var typeToStruct = new Dictionary(); foreach (var type in module.GetTypes()) { foreach (var field in getPossibleFields(type)) { var fieldType = DotNetUtils.getType(module, field.FieldType); @@ -113,9 +113,9 @@ namespace de4dot.code.deobfuscators.DeepSea { if (!checkFields(fieldType)) continue; - List list; + List list; if (!candidates.TryGetValue(fieldType, out list)) - candidates[fieldType] = list = new List(); + candidates[fieldType] = list = new List(); list.Add(type); typeToStruct[type] = fieldType; break; @@ -123,7 +123,7 @@ namespace de4dot.code.deobfuscators.DeepSea { } foreach (var type in module.GetTypes()) { - TypeDefinition structType; + TypeDef structType; typeToStruct.TryGetValue(type, out structType); foreach (var field in type.Fields) { @@ -144,8 +144,8 @@ namespace de4dot.code.deobfuscators.DeepSea { return candidates; } - IEnumerable getPossibleFields(TypeDefinition type) { - var typeToFields = new TypeDefinitionDict>(); + IEnumerable getPossibleFields(TypeDef type) { + var typeToFields = new TypeDefinitionDict>(); foreach (var field in type.Fields) { if (field.Attributes != FieldAttributes.Private) continue; @@ -156,7 +156,7 @@ namespace de4dot.code.deobfuscators.DeepSea { continue; var list = typeToFields.find(fieldType); if (list == null) - typeToFields.add(fieldType, list = new List()); + typeToFields.add(fieldType, list = new List()); list.Add(field); } @@ -166,20 +166,20 @@ namespace de4dot.code.deobfuscators.DeepSea { } } - static bool checkBaseType(TypeDefinition type) { + static bool checkBaseType(TypeDef type) { if (type == null || type.BaseType == null) return false; return type.BaseType.FullName == "System.ValueType" || type.BaseType.EType == ElementType.Object; } - void removeType(Dictionary> candidates, TypeReference type) { + void removeType(Dictionary> candidates, TypeReference type) { var typeDef = DotNetUtils.getType(module, type); if (typeDef == null) return; candidates.Remove(typeDef); } - static bool checkMethods(TypeDefinition type) { + static bool checkMethods(TypeDef type) { foreach (var method in type.Methods) { if (method.Name == ".cctor") continue; @@ -197,7 +197,7 @@ namespace de4dot.code.deobfuscators.DeepSea { return false; } - static bool checkFields(TypeDefinition type) { + static bool checkFields(TypeDef type) { if (type.Fields.Count == 0) return false; foreach (var field in type.Fields) { @@ -270,7 +270,7 @@ namespace de4dot.code.deobfuscators.DeepSea { return newInstrs; } - FieldDefinition getNewField(FieldReference structField, FieldReference oldFieldRef) { + FieldDef getNewField(FieldReference structField, FieldReference oldFieldRef) { var fieldsDict = typeToFieldsDict.find(structField.DeclaringType); if (fieldsDict == null) throw new ApplicationException("Could not find structField declaringType"); diff --git a/de4dot.code/deobfuscators/DeepSea/ResolverBase.cs b/de4dot.code/deobfuscators/DeepSea/ResolverBase.cs index 6648e6fe..04f6b68a 100644 --- a/de4dot.code/deobfuscators/DeepSea/ResolverBase.cs +++ b/de4dot.code/deobfuscators/DeepSea/ResolverBase.cs @@ -19,8 +19,8 @@ using System; using System.Collections.Generic; -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using de4dot.blocks; namespace de4dot.code.deobfuscators.DeepSea { @@ -28,15 +28,15 @@ namespace de4dot.code.deobfuscators.DeepSea { protected ModuleDefinition module; protected ISimpleDeobfuscator simpleDeobfuscator; protected IDeobfuscator deob; - protected MethodDefinition initMethod; - protected MethodDefinition resolveHandler; + protected MethodDef initMethod; + protected MethodDef resolveHandler; protected FrameworkType frameworkType; - public MethodDefinition InitMethod { + public MethodDef InitMethod { get { return initMethod; } } - public MethodDefinition HandlerMethod { + public MethodDef HandlerMethod { get { return resolveHandler; } } @@ -58,7 +58,7 @@ namespace de4dot.code.deobfuscators.DeepSea { return; } - bool checkCalledMethods(MethodDefinition checkMethod) { + bool checkCalledMethods(MethodDef checkMethod) { if (checkMethod == null || checkMethod.Body == null) return false; @@ -75,7 +75,7 @@ namespace de4dot.code.deobfuscators.DeepSea { return false; } - bool checkResolverInitMethod(MethodDefinition resolverInitMethod) { + bool checkResolverInitMethod(MethodDef resolverInitMethod) { if (resolverInitMethod == null || resolverInitMethod.Body == null) return false; if (resolverInitMethod.Body.ExceptionHandlers.Count != 1) @@ -89,7 +89,7 @@ namespace de4dot.code.deobfuscators.DeepSea { } } - bool checkResolverInitMethodDesktop(MethodDefinition resolverInitMethod) { + bool checkResolverInitMethodDesktop(MethodDef resolverInitMethod) { simpleDeobfuscator.deobfuscate(resolverInitMethod); if (!checkResolverInitMethodInternal(resolverInitMethod)) return false; @@ -106,25 +106,25 @@ namespace de4dot.code.deobfuscators.DeepSea { return false; } - protected virtual bool checkResolverInitMethodSilverlight(MethodDefinition resolverInitMethod) { + protected virtual bool checkResolverInitMethodSilverlight(MethodDef resolverInitMethod) { return false; } - protected abstract bool checkResolverInitMethodInternal(MethodDefinition resolverInitMethod); + protected abstract bool checkResolverInitMethodInternal(MethodDef resolverInitMethod); - IEnumerable getLdftnMethods(MethodDefinition method) { - var list = new List(); + IEnumerable getLdftnMethods(MethodDef method) { + var list = new List(); foreach (var instr in method.Body.Instructions) { if (instr.OpCode.Code != Code.Ldftn) continue; - var loadedMethod = instr.Operand as MethodDefinition; + var loadedMethod = instr.Operand as MethodDef; if (loadedMethod != null) list.Add(loadedMethod); } return list; } - bool checkHandlerMethodDesktop(MethodDefinition handler) { + bool checkHandlerMethodDesktop(MethodDef handler) { if (handler == null || handler.Body == null || !handler.IsStatic) return false; if (!DotNetUtils.isMethod(handler, "System.Reflection.Assembly", "(System.Object,System.ResolveEventArgs)")) @@ -132,7 +132,7 @@ namespace de4dot.code.deobfuscators.DeepSea { return checkHandlerMethodDesktopInternal(handler); } - protected abstract bool checkHandlerMethodDesktopInternal(MethodDefinition handler); + protected abstract bool checkHandlerMethodDesktopInternal(MethodDef handler); // 3.0.3.41 - 3.0.4.44 protected static byte[] decryptResourceV3Old(EmbeddedResource resource) { diff --git a/de4dot.code/deobfuscators/DeepSea/ResourceResolver.cs b/de4dot.code/deobfuscators/DeepSea/ResourceResolver.cs index aeeeb64a..a8b99866 100644 --- a/de4dot.code/deobfuscators/DeepSea/ResourceResolver.cs +++ b/de4dot.code/deobfuscators/DeepSea/ResourceResolver.cs @@ -19,8 +19,8 @@ using System; using System.Collections.Generic; -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using de4dot.blocks; namespace de4dot.code.deobfuscators.DeepSea { @@ -42,30 +42,30 @@ namespace de4dot.code.deobfuscators.DeepSea { } class Data40 { - public FieldDefinition resourceField; - public MethodDefinition resolveHandler2; - public MethodDefinition getDataMethod; + public FieldDef resourceField; + public MethodDef resolveHandler2; + public MethodDef getDataMethod; public int magic; } class Data41 { - public FieldDefinition resourceField; - public MethodDefinition resolveHandler2; + public FieldDef resourceField; + public MethodDef resolveHandler2; public int magic; public bool isTrial; } class HandlerInfo { - public MethodDefinition handler; + public MethodDef handler; public IList args; - public HandlerInfo(MethodDefinition handler, IList args) { + public HandlerInfo(MethodDef handler, IList args) { this.handler = handler; this.args = args; } } - public MethodDefinition InitMethod2 { + public MethodDef InitMethod2 { get { if (data40 != null) return data40.resolveHandler2; @@ -75,7 +75,7 @@ namespace de4dot.code.deobfuscators.DeepSea { } } - public MethodDefinition GetDataMethod { + public MethodDef GetDataMethod { get { return data40 != null ? data40.getDataMethod : null; } } @@ -87,11 +87,11 @@ namespace de4dot.code.deobfuscators.DeepSea { : base(module, simpleDeobfuscator, deob) { } - protected override bool checkResolverInitMethodInternal(MethodDefinition resolverInitMethod) { + protected override bool checkResolverInitMethodInternal(MethodDef resolverInitMethod) { return DotNetUtils.callsMethod(resolverInitMethod, "System.Void System.AppDomain::add_ResourceResolve(System.ResolveEventHandler)"); } - protected override bool checkHandlerMethodDesktopInternal(MethodDefinition handler) { + protected override bool checkHandlerMethodDesktopInternal(MethodDef handler) { if (checkHandlerV3(handler)) { version = ResourceVersion.V3; return true; @@ -114,13 +114,13 @@ namespace de4dot.code.deobfuscators.DeepSea { return false; } - HandlerInfo getHandlerArgs41(MethodDefinition handler) { + HandlerInfo getHandlerArgs41(MethodDef handler) { var instrs = handler.Body.Instructions; for (int i = 0; i < instrs.Count; i++) { var instr = instrs[i]; if (instr.OpCode.Code != Code.Call) continue; - var calledMethod = instr.Operand as MethodDefinition; + var calledMethod = instr.Operand as MethodDef; if (calledMethod == null) continue; if (getLdtokenField(calledMethod) == null) @@ -158,7 +158,7 @@ namespace de4dot.code.deobfuscators.DeepSea { return true; } - static int getMagicArgIndex41Retail(MethodDefinition method) { + static int getMagicArgIndex41Retail(MethodDef method) { var instrs = method.Body.Instructions; for (int i = 0; i < instrs.Count - 3; i++) { var add = instrs[i]; @@ -179,7 +179,7 @@ namespace de4dot.code.deobfuscators.DeepSea { return -1; } - static int getMagicArgIndex41Trial(MethodDefinition method) { + static int getMagicArgIndex41Trial(MethodDef method) { var instrs = method.Body.Instructions; for (int i = 0; i < instrs.Count - 2; i++) { var ldarg = instrs[i]; @@ -195,11 +195,11 @@ namespace de4dot.code.deobfuscators.DeepSea { return -1; } - static FieldDefinition getLdtokenField(MethodDefinition method) { + static FieldDef getLdtokenField(MethodDef method) { foreach (var instr in method.Body.Instructions) { if (instr.OpCode.Code != Code.Ldtoken) continue; - var field = instr.Operand as FieldDefinition; + var field = instr.Operand as FieldDef; if (field == null || field.InitialValue == null || field.InitialValue.Length == 0) continue; @@ -219,11 +219,11 @@ namespace de4dot.code.deobfuscators.DeepSea { "System.String", "System.String[]", }; - static bool checkHandlerV3(MethodDefinition handler) { + static bool checkHandlerV3(MethodDef handler) { return new LocalTypes(handler).all(handlerLocalTypes_V3); } - static Data40 checkHandlerV40(MethodDefinition handler) { + static Data40 checkHandlerV40(MethodDef handler) { var data40 = new Data40(); var instrs = handler.Body.Instructions; @@ -236,10 +236,10 @@ namespace de4dot.code.deobfuscators.DeepSea { var ldtoken = instrs[index++]; if (ldtoken.OpCode.Code != Code.Ldtoken) continue; - var field = ldtoken.Operand as FieldDefinition; + var field = ldtoken.Operand as FieldDef; string methodSig = "(System.ResolveEventArgs,System.RuntimeFieldHandle,System.Int32,System.String,System.Int32)"; - var method = ldtoken.Operand as MethodDefinition; + var method = ldtoken.Operand as MethodDef; if (method != null) { // >= 4.0.4 if (!DotNetUtils.isMethod(method, "System.Byte[]", "()")) @@ -273,7 +273,7 @@ namespace de4dot.code.deobfuscators.DeepSea { call = instrs[index++]; if (call.OpCode.Code != Code.Call) continue; - var resolveHandler2 = call.Operand as MethodDefinition; + var resolveHandler2 = call.Operand as MethodDef; if (!DotNetUtils.isMethod(resolveHandler2, "System.Reflection.Assembly", methodSig)) continue; @@ -286,11 +286,11 @@ namespace de4dot.code.deobfuscators.DeepSea { return null; } - static FieldDefinition getResourceField(MethodDefinition method) { + static FieldDef getResourceField(MethodDef method) { foreach (var instr in method.Body.Instructions) { if (instr.OpCode.Code != Code.Ldtoken) continue; - var field = instr.Operand as FieldDefinition; + var field = instr.Operand as FieldDef; if (field == null || field.InitialValue == null || field.InitialValue.Length == 0) continue; return field; @@ -337,11 +337,11 @@ namespace de4dot.code.deobfuscators.DeepSea { } } - bool decryptResource(FieldDefinition resourceField, int magic) { + bool decryptResource(FieldDef resourceField, int magic) { if (resourceField == null) return false; - string name = string.Format("Embedded data field {0:X8} RVA {1:X8}", resourceField.MetadataToken.ToInt32(), resourceField.RVA); + string name = string.Format("Embedded data field {0:X8} RVA {1:X8}", resourceField.MDToken.ToInt32(), resourceField.RVA); DeobUtils.decryptAndAddResources(module, name, () => decryptResourceV4(resourceField.InitialValue, magic)); resourceField.InitialValue = new byte[1]; resourceField.FieldType = module.TypeSystem.Byte; diff --git a/de4dot.code/deobfuscators/DeepSea/StringDecrypter.cs b/de4dot.code/deobfuscators/DeepSea/StringDecrypter.cs index 0399f891..8a9af0ec 100644 --- a/de4dot.code/deobfuscators/DeepSea/StringDecrypter.cs +++ b/de4dot.code/deobfuscators/DeepSea/StringDecrypter.cs @@ -20,8 +20,8 @@ using System; using System.Collections.Generic; using System.Text; -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using Mono.Cecil.Metadata; using de4dot.blocks; @@ -40,18 +40,18 @@ namespace de4dot.code.deobfuscators.DeepSea { interface IDecrypterInfo { DecrypterVersion Version { get; } - MethodDefinition Method { get; } + MethodDef Method { get; } string decrypt(object[] args); void cleanup(); } - static short[] findKey(MethodDefinition initMethod, FieldDefinition keyField) { + static short[] findKey(MethodDef initMethod, FieldDef keyField) { var fields = new FieldDefinitionAndDeclaringTypeDict(); fields.add(keyField, true); return findKey(initMethod, fields); } - static short[] findKey(MethodDefinition initMethod, FieldDefinitionAndDeclaringTypeDict fields) { + static short[] findKey(MethodDef initMethod, FieldDefinitionAndDeclaringTypeDict fields) { var instrs = initMethod.Body.Instructions; for (int i = 0; i < instrs.Count - 2; i++) { var ldci4 = instrs[i]; @@ -84,7 +84,7 @@ namespace de4dot.code.deobfuscators.DeepSea { return null; } - static FieldDefinition getStoreField(MethodDefinition method, int startIndex, VariableDefinition local) { + static FieldDef getStoreField(MethodDef method, int startIndex, VariableDefinition local) { var instrs = method.Body.Instructions; for (int i = 0; i < instrs.Count - 1; i++) { var ldloc = instrs[i]; @@ -96,18 +96,18 @@ namespace de4dot.code.deobfuscators.DeepSea { var stsfld = instrs[i + 1]; if (stsfld.OpCode.Code != Code.Stsfld) continue; - return stsfld.Operand as FieldDefinition; + return stsfld.Operand as FieldDef; } return null; } - static bool findMagic(MethodDefinition method, out int magic) { + static bool findMagic(MethodDef method, out int magic) { int arg1, arg2; return findMagic(method, out arg1, out arg2, out magic); } - static bool findMagic(MethodDefinition method, out int arg1, out int arg2, out int magic) { + static bool findMagic(MethodDef method, out int arg1, out int arg2, out int magic) { var instrs = method.Body.Instructions; for (int i = 0; i < instrs.Count - 3; i++) { if ((arg1 = DotNetUtils.getArgIndex(instrs[i])) < 0) @@ -127,7 +127,7 @@ namespace de4dot.code.deobfuscators.DeepSea { return false; } - static void removeInitializeArrayCall(MethodDefinition method, FieldDefinition field) { + static void removeInitializeArrayCall(MethodDef method, FieldDef field) { var instrs = method.Body.Instructions; for (int i = 0; i < instrs.Count - 1; i++) { var ldtoken = instrs[i]; @@ -151,7 +151,7 @@ namespace de4dot.code.deobfuscators.DeepSea { } class DecrypterInfo41 : IDecrypterInfo { - MethodDefinition cctor; + MethodDef cctor; int magic; int arg1, arg2; FieldDefinitionAndDeclaringTypeDict fields; @@ -164,10 +164,10 @@ namespace de4dot.code.deobfuscators.DeepSea { class ArrayInfo { public int sizeInElems; public TypeReference elementType; - public FieldDefinition initField; - public FieldDefinition field; + public FieldDef initField; + public FieldDef field; - public ArrayInfo(int sizeInElems, TypeReference elementType, FieldDefinition initField, FieldDefinition field) { + public ArrayInfo(int sizeInElems, TypeReference elementType, FieldDef initField, FieldDef field) { this.sizeInElems = sizeInElems; this.elementType = elementType; this.initField = initField; @@ -179,14 +179,14 @@ namespace de4dot.code.deobfuscators.DeepSea { get { return DecrypterVersion.V4_1; } } - public MethodDefinition Method { get; private set; } + public MethodDef Method { get; private set; } - public DecrypterInfo41(MethodDefinition cctor, MethodDefinition method) { + public DecrypterInfo41(MethodDef cctor, MethodDef method) { this.cctor = cctor; Method = method; } - public static bool isPossibleDecrypterMethod(MethodDefinition method) { + public static bool isPossibleDecrypterMethod(MethodDef method) { if (!checkMethodSignature(method)) return false; var fields = getFields(method); @@ -196,7 +196,7 @@ namespace de4dot.code.deobfuscators.DeepSea { return true; } - static bool checkMethodSignature(MethodDefinition method) { + static bool checkMethodSignature(MethodDef method) { if (method.MethodReturnType.ReturnType.EType != ElementType.String) return false; int count = 0; @@ -207,12 +207,12 @@ namespace de4dot.code.deobfuscators.DeepSea { return count >= 2; } - static FieldDefinitionAndDeclaringTypeDict getFields(MethodDefinition method) { + static FieldDefinitionAndDeclaringTypeDict getFields(MethodDef method) { var fields = new FieldDefinitionAndDeclaringTypeDict(); foreach (var instr in method.Body.Instructions) { if (instr.OpCode.Code != Code.Ldsfld && instr.OpCode.Code != Code.Stsfld) continue; - var field = instr.Operand as FieldDefinition; + var field = instr.Operand as FieldDef; if (field == null) continue; if (field.DeclaringType != method.DeclaringType) @@ -248,7 +248,7 @@ namespace de4dot.code.deobfuscators.DeepSea { return true; } - int findKeyShift(MethodDefinition method) { + int findKeyShift(MethodDef method) { var instrs = method.Body.Instructions; for (int i = 0; i < instrs.Count - 3; i++) { int index = i; @@ -276,7 +276,7 @@ namespace de4dot.code.deobfuscators.DeepSea { return -1; } - int findNextFieldUse(MethodDefinition method, int index) { + int findNextFieldUse(MethodDef method, int index) { var instrs = method.Body.Instructions; for (int i = index; i < instrs.Count; i++) { var instr = instrs[i]; @@ -291,7 +291,7 @@ namespace de4dot.code.deobfuscators.DeepSea { return -1; } - ArrayInfo getArrayInfo(MethodDefinition method) { + ArrayInfo getArrayInfo(MethodDef method) { var instructions = method.Body.Instructions; for (int i = 0; i < instructions.Count; i++) { var ldci4_arraySizeInBytes = instructions[i]; @@ -304,8 +304,8 @@ namespace de4dot.code.deobfuscators.DeepSea { int sizeInBytes = DotNetUtils.getLdcI4Value(ldci4_arraySizeInBytes); var elementType = instrs[0].Operand as TypeReference; - var initField = instrs[2].Operand as FieldDefinition; - var field = instrs[4].Operand as FieldDefinition; + var initField = instrs[2].Operand as FieldDef; + var field = instrs[4].Operand as FieldDef; if (elementType == null) continue; if (initField == null || initField.InitialValue == null || initField.InitialValue.Length == 0) @@ -327,7 +327,7 @@ namespace de4dot.code.deobfuscators.DeepSea { return findKey(cctor); } - short[] findKey(MethodDefinition initMethod) { + short[] findKey(MethodDef initMethod) { return StringDecrypter.findKey(initMethod, fields); } @@ -386,27 +386,27 @@ namespace de4dot.code.deobfuscators.DeepSea { } class DecrypterInfo40 : IDecrypterInfo { - MethodDefinition cctor; + MethodDef cctor; int magic; - FieldDefinition cachedStringsField; - FieldDefinition keyField; - FieldDefinition encryptedStringsField; - FieldDefinition encryptedDataField; + FieldDef cachedStringsField; + FieldDef keyField; + FieldDef encryptedStringsField; + FieldDef encryptedDataField; short[] key; ushort[] encryptedData; - public MethodDefinition Method { get; private set; } + public MethodDef Method { get; private set; } public DecrypterVersion Version { get { return DecrypterVersion.V4_0; } } - public DecrypterInfo40(MethodDefinition cctor, MethodDefinition method) { + public DecrypterInfo40(MethodDef cctor, MethodDef method) { this.cctor = cctor; this.Method = method; } - public static bool isPossibleDecrypterMethod(MethodDefinition method) { + public static bool isPossibleDecrypterMethod(MethodDef method) { if (!checkFields(method.DeclaringType.Fields)) return false; return DotNetUtils.isMethod(method, "System.String", "(System.Int32,System.Int32)"); @@ -438,13 +438,13 @@ namespace de4dot.code.deobfuscators.DeepSea { return true; } - List findFields() { - var charArrayFields = new List(); + List findFields() { + var charArrayFields = new List(); foreach (var instr in Method.Body.Instructions) { if (instr.OpCode.Code != Code.Stsfld && instr.OpCode.Code != Code.Ldsfld) continue; - var field = instr.Operand as FieldDefinition; + var field = instr.Operand as FieldDef; if (field == null) continue; if (!MemberReferenceHelper.compareTypes(Method.DeclaringType, field.DeclaringType)) @@ -472,17 +472,17 @@ namespace de4dot.code.deobfuscators.DeepSea { return charArrayFields; } - static FieldDefinition findEncryptedStrings(MethodDefinition initMethod, List ourFields, out FieldDefinition dataField) { + static FieldDef findEncryptedStrings(MethodDef initMethod, List ourFields, out FieldDef dataField) { for (int i = 0; i < initMethod.Body.Instructions.Count; i++) { var instrs = DotNetUtils.getInstructions(initMethod.Body.Instructions, i, OpCodes.Ldtoken, OpCodes.Call, OpCodes.Stsfld); if (instrs == null) continue; - dataField = instrs[0].Operand as FieldDefinition; + dataField = instrs[0].Operand as FieldDef; if (dataField == null || dataField.InitialValue == null || dataField.InitialValue.Length == 0) continue; - var savedField = instrs[2].Operand as FieldDefinition; + var savedField = instrs[2].Operand as FieldDef; if (savedField == null || !matches(ourFields, savedField)) continue; @@ -493,7 +493,7 @@ namespace de4dot.code.deobfuscators.DeepSea { return null; } - static bool matches(IEnumerable ourFields, FieldReference field) { + static bool matches(IEnumerable ourFields, FieldReference field) { foreach (var ourField in ourFields) { if (MemberReferenceHelper.compareFieldReferenceAndDeclaringType(ourField, field)) return true; @@ -510,7 +510,7 @@ namespace de4dot.code.deobfuscators.DeepSea { return findKey(cctor); } - short[] findKey(MethodDefinition initMethod) { + short[] findKey(MethodDef initMethod) { return StringDecrypter.findKey(initMethod, keyField); } @@ -545,26 +545,26 @@ namespace de4dot.code.deobfuscators.DeepSea { } class DecrypterInfo13 : IDecrypterInfo { - MethodDefinition cctor; - FieldDefinition cachedStringsField; - FieldDefinition keyField; + MethodDef cctor; + FieldDef cachedStringsField; + FieldDef keyField; int magic; string[] encryptedStrings; short[] key; - public MethodDefinition Method { get; private set; } + public MethodDef Method { get; private set; } public DecrypterVersion Version { get { return DecrypterVersion.V1_3; } } - public static bool isPossibleDecrypterMethod(MethodDefinition method) { + public static bool isPossibleDecrypterMethod(MethodDef method) { if (!checkFields(method.DeclaringType.Fields)) return false; return DotNetUtils.isMethod(method, "System.String", "(System.Int32)"); } - public DecrypterInfo13(MethodDefinition cctor, MethodDefinition method) { + public DecrypterInfo13(MethodDef cctor, MethodDef method) { this.cctor = cctor; this.Method = method; } @@ -602,7 +602,7 @@ namespace de4dot.code.deobfuscators.DeepSea { foreach (var instr in Method.Body.Instructions) { if (instr.OpCode.Code != Code.Stsfld && instr.OpCode.Code != Code.Ldsfld) continue; - var field = instr.Operand as FieldDefinition; + var field = instr.Operand as FieldDef; if (field == null) continue; if (!MemberReferenceHelper.compareTypes(Method.DeclaringType, field.DeclaringType)) @@ -637,7 +637,7 @@ namespace de4dot.code.deobfuscators.DeepSea { return findKey(cctor); } - short[] findKey(MethodDefinition initMethod) { + short[] findKey(MethodDef initMethod) { return StringDecrypter.findKey(initMethod, keyField); } @@ -650,7 +650,7 @@ namespace de4dot.code.deobfuscators.DeepSea { return key; } - static bool findMagic(MethodDefinition method, out int magic) { + static bool findMagic(MethodDef method, out int magic) { var instrs = method.Body.Instructions; for (int i = 0; i < instrs.Count - 2; i++) { var ldarg = instrs[i]; @@ -668,7 +668,7 @@ namespace de4dot.code.deobfuscators.DeepSea { return false; } - bool findEncryptedStrings(MethodDefinition method) { + bool findEncryptedStrings(MethodDef method) { var switchInstr = getOnlySwitchInstruction(method); if (switchInstr == null) return false; @@ -683,7 +683,7 @@ namespace de4dot.code.deobfuscators.DeepSea { return true; } - static Instruction getOnlySwitchInstruction(MethodDefinition method) { + static Instruction getOnlySwitchInstruction(MethodDef method) { Instruction switchInstr = null; foreach (var instr in method.Body.Instructions) { if (instr.OpCode.Code != Code.Switch) @@ -711,9 +711,9 @@ namespace de4dot.code.deobfuscators.DeepSea { get { return version; } } - public List DecrypterMethods { + public List DecrypterMethods { get { - var methods = new List(methodToInfo.Count); + var methods = new List(methodToInfo.Count); foreach (var info in methodToInfo.getValues()) methods.Add(info.Method); return methods; @@ -765,14 +765,14 @@ namespace de4dot.code.deobfuscators.DeepSea { } } - static void deobfuscateCctor(ISimpleDeobfuscator simpleDeobfuscator, MethodDefinition cctor, ref bool deobfuscatedCctor, bool hasPublicKeyToken) { + static void deobfuscateCctor(ISimpleDeobfuscator simpleDeobfuscator, MethodDef cctor, ref bool deobfuscatedCctor, bool hasPublicKeyToken) { if (deobfuscatedCctor || hasPublicKeyToken) return; simpleDeobfuscator.deobfuscate(cctor); deobfuscatedCctor = true; } - static bool checkFields(IEnumerable fields) { + static bool checkFields(IEnumerable fields) { bool foundCharAry = false, foundStringAry = false; foreach (var field in fields) { if (foundCharAry && foundStringAry) @@ -789,21 +789,21 @@ namespace de4dot.code.deobfuscators.DeepSea { return foundCharAry && foundStringAry; } - DecrypterInfo13 getInfoV13(MethodDefinition cctor, MethodDefinition method) { + DecrypterInfo13 getInfoV13(MethodDef cctor, MethodDef method) { var info = new DecrypterInfo13(cctor, method); if (!info.initialize()) return null; return info; } - DecrypterInfo40 getInfoV40(MethodDefinition cctor, MethodDefinition method) { + DecrypterInfo40 getInfoV40(MethodDef cctor, MethodDef method) { var info = new DecrypterInfo40(cctor, method); if (!info.initialize()) return null; return info; } - DecrypterInfo41 getInfoV41(MethodDefinition cctor, MethodDefinition method) { + DecrypterInfo41 getInfoV41(MethodDef cctor, MethodDef method) { var info = new DecrypterInfo41(cctor, method); if (!info.initialize()) return null; diff --git a/de4dot.code/deobfuscators/Dotfuscator/Deobfuscator.cs b/de4dot.code/deobfuscators/Dotfuscator/Deobfuscator.cs index d5ab2230..25f19d15 100644 --- a/de4dot.code/deobfuscators/Dotfuscator/Deobfuscator.cs +++ b/de4dot.code/deobfuscators/Dotfuscator/Deobfuscator.cs @@ -18,7 +18,7 @@ */ using System.Collections.Generic; -using Mono.Cecil; +using dot10.DotNet; using de4dot.blocks; namespace de4dot.code.deobfuscators.Dotfuscator { @@ -101,7 +101,7 @@ namespace de4dot.code.deobfuscators.Dotfuscator { } } - void initializeVersion(TypeDefinition attr) { + void initializeVersion(TypeDef attr) { var s = DotNetUtils.getCustomArgAsString(getAssemblyAttribute(attr), 0); if (s == null) return; @@ -129,7 +129,7 @@ namespace de4dot.code.deobfuscators.Dotfuscator { public override IEnumerable getStringDecrypterMethods() { var list = new List(); foreach (var method in stringDecrypter.StringDecrypters) - list.Add(method.MetadataToken.ToInt32()); + list.Add(method.MDToken.ToInt32()); return list; } } diff --git a/de4dot.code/deobfuscators/Dotfuscator/StringDecrypter.cs b/de4dot.code/deobfuscators/Dotfuscator/StringDecrypter.cs index 4c707b59..63760e3a 100644 --- a/de4dot.code/deobfuscators/Dotfuscator/StringDecrypter.cs +++ b/de4dot.code/deobfuscators/Dotfuscator/StringDecrypter.cs @@ -18,8 +18,8 @@ */ using System.Collections.Generic; -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using de4dot.blocks; namespace de4dot.code.deobfuscators.Dotfuscator { @@ -28,9 +28,9 @@ namespace de4dot.code.deobfuscators.Dotfuscator { Dictionary stringDecrypterMethods = new Dictionary(); public class StringDecrypterInfo { - public MethodDefinition method; + public MethodDef method; public int magic; - public StringDecrypterInfo(MethodDefinition method, int magic) { + public StringDecrypterInfo(MethodDef method, int magic) { this.method = method; this.magic = magic; } @@ -40,9 +40,9 @@ namespace de4dot.code.deobfuscators.Dotfuscator { get { return stringDecrypterMethods.Count > 0; } } - public IEnumerable StringDecrypters { + public IEnumerable StringDecrypters { get { - var list = new List(stringDecrypterMethods.Count); + var list = new List(stringDecrypterMethods.Count); foreach (var info in stringDecrypterMethods) list.Add(info.Value.method); return list; @@ -62,7 +62,7 @@ namespace de4dot.code.deobfuscators.Dotfuscator { findStringDecrypterMethods(type, simpleDeobfuscator); } - void findStringDecrypterMethods(TypeDefinition type, ISimpleDeobfuscator simpleDeobfuscator) { + void findStringDecrypterMethods(TypeDef type, ISimpleDeobfuscator simpleDeobfuscator) { foreach (var method in DotNetUtils.findMethods(type.Methods, "System.String", new string[] { "System.String", "System.Int32" })) { if (method.Body.HasExceptionHandlers) continue; diff --git a/de4dot.code/deobfuscators/Eazfuscator_NET/AssemblyResolver.cs b/de4dot.code/deobfuscators/Eazfuscator_NET/AssemblyResolver.cs index 767a7b26..20380a60 100644 --- a/de4dot.code/deobfuscators/Eazfuscator_NET/AssemblyResolver.cs +++ b/de4dot.code/deobfuscators/Eazfuscator_NET/AssemblyResolver.cs @@ -22,19 +22,19 @@ using System.Collections.Generic; using System.IO; using System.Text; using System.Text.RegularExpressions; -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using de4dot.blocks; namespace de4dot.code.deobfuscators.Eazfuscator_NET { class AssemblyResolver { ModuleDefinition module; DecrypterType decrypterType; - TypeDefinition resolverType; - MethodDefinition initMethod; - MethodDefinition handlerMethod; - MethodDefinition decryptMethod; - TypeDefinition otherType; + TypeDef resolverType; + MethodDef initMethod; + MethodDef handlerMethod; + MethodDef decryptMethod; + TypeDef otherType; List assemblyInfos = new List(); FrameworkType frameworkType; byte[] decryptKey; @@ -56,15 +56,15 @@ namespace de4dot.code.deobfuscators.Eazfuscator_NET { } } - public TypeDefinition Type { + public TypeDef Type { get { return resolverType; } } - public TypeDefinition OtherType { + public TypeDef OtherType { get { return otherType; } } - public MethodDefinition InitMethod { + public MethodDef InitMethod { get { return initMethod; } } @@ -87,7 +87,7 @@ namespace de4dot.code.deobfuscators.Eazfuscator_NET { checkCalledMethods(DotNetUtils.getModuleTypeCctor(module)); } - bool checkCalledMethods(MethodDefinition method) { + bool checkCalledMethods(MethodDef method) { if (method == null || method.Body == null) return false; @@ -95,7 +95,7 @@ namespace de4dot.code.deobfuscators.Eazfuscator_NET { if (instr.OpCode.Code != Code.Call) continue; - var calledMethod = instr.Operand as MethodDefinition; + var calledMethod = instr.Operand as MethodDef; if (calledMethod == null || !calledMethod.IsStatic || calledMethod.Body == null) continue; if (!DotNetUtils.isMethod(calledMethod, "System.Void", "()")) @@ -119,7 +119,7 @@ namespace de4dot.code.deobfuscators.Eazfuscator_NET { return false; } - bool checkInitMethodSilverlight(MethodDefinition method) { + bool checkInitMethodSilverlight(MethodDef method) { var type = method.DeclaringType; if (type.NestedTypes.Count != 2) return false; @@ -134,11 +134,11 @@ namespace de4dot.code.deobfuscators.Eazfuscator_NET { return true; } - static MethodDefinition getResolveMethodSilverlight(MethodDefinition initMethod) { + static MethodDef getResolveMethodSilverlight(MethodDef initMethod) { foreach (var instr in initMethod.Body.Instructions) { if (instr.OpCode.Code != Code.Call) continue; - var calledMethod = instr.Operand as MethodDefinition; + var calledMethod = instr.Operand as MethodDef; if (calledMethod == null) continue; if (!DotNetUtils.isMethod(calledMethod, "System.Void", "()")) @@ -153,7 +153,7 @@ namespace de4dot.code.deobfuscators.Eazfuscator_NET { return null; } - bool checkInitMethod(MethodDefinition method) { + bool checkInitMethod(MethodDef method) { var type = method.DeclaringType; if (type.NestedTypes.Count < 2 || type.NestedTypes.Count > 6) return false; @@ -173,7 +173,7 @@ namespace de4dot.code.deobfuscators.Eazfuscator_NET { return true; } - MethodDefinition getDecryptMethod() { + MethodDef getDecryptMethod() { foreach (var method in resolverType.Methods) { if (!method.IsStatic || method.Body == null) continue; @@ -198,14 +198,14 @@ namespace de4dot.code.deobfuscators.Eazfuscator_NET { throw new ApplicationException("Could not initialize decrypterType"); } - TypeDefinition getDecrypterType(MethodDefinition method) { + TypeDef getDecrypterType(MethodDef method) { if (method == null || method.Body == null) return null; foreach (var instr in method.Body.Instructions) { if (instr.OpCode.Code != Code.Call) continue; - var calledMethod = instr.Operand as MethodDefinition; + var calledMethod = instr.Operand as MethodDef; if (calledMethod == null || !calledMethod.IsStatic || calledMethod.DeclaringType == resolverType) continue; if (!DotNetUtils.isMethod(calledMethod, "System.Void", "(System.Byte[])")) @@ -467,8 +467,8 @@ namespace de4dot.code.deobfuscators.Eazfuscator_NET { } } - static MethodDefinition getTheOnlyMethod(TypeDefinition type, string typeName, string methodName, string returnType, string parameters) { - MethodDefinition foundMethod = null; + static MethodDef getTheOnlyMethod(TypeDef type, string typeName, string methodName, string returnType, string parameters) { + MethodDef foundMethod = null; foreach (var method in type.Methods) { if (!method.IsStatic || method.Body == null || method.HasGenericParameters) diff --git a/de4dot.code/deobfuscators/Eazfuscator_NET/CodeCompilerMethodCallRestorer.cs b/de4dot.code/deobfuscators/Eazfuscator_NET/CodeCompilerMethodCallRestorer.cs index 1f52459f..1e35e07f 100644 --- a/de4dot.code/deobfuscators/Eazfuscator_NET/CodeCompilerMethodCallRestorer.cs +++ b/de4dot.code/deobfuscators/Eazfuscator_NET/CodeCompilerMethodCallRestorer.cs @@ -17,7 +17,7 @@ along with de4dot. If not, see . */ -using Mono.Cecil; +using dot10.DotNet; namespace de4dot.code.deobfuscators.Eazfuscator_NET { class CodeCompilerMethodCallRestorer : MethodCallRestorerBase { @@ -63,55 +63,55 @@ namespace de4dot.code.deobfuscators.Eazfuscator_NET { : base(module) { } - public void add_CodeDomProvider_CompileAssemblyFromDom(MethodDefinition oldMethod) { + public void add_CodeDomProvider_CompileAssemblyFromDom(MethodDef oldMethod) { if (oldMethod == null) return; add(oldMethod, builder.instanceMethod("CompileAssemblyFromDom", CodeDomProvider, CompilerResults, CompilerParameters, CodeCompileUnitArray)); } - public void add_CodeDomProvider_CompileAssemblyFromFile(MethodDefinition oldMethod) { + public void add_CodeDomProvider_CompileAssemblyFromFile(MethodDef oldMethod) { if (oldMethod == null) return; add(oldMethod, builder.instanceMethod("CompileAssemblyFromFile", CodeDomProvider, CompilerResults, CompilerParameters, StringArray)); } - public void add_CodeDomProvider_CompileAssemblyFromSource(MethodDefinition oldMethod) { + public void add_CodeDomProvider_CompileAssemblyFromSource(MethodDef oldMethod) { if (oldMethod == null) return; add(oldMethod, builder.instanceMethod("CompileAssemblyFromSource", CodeDomProvider, CompilerResults, CompilerParameters, StringArray)); } - public void add_ICodeCompiler_CompileAssemblyFromDom(MethodDefinition oldMethod) { + public void add_ICodeCompiler_CompileAssemblyFromDom(MethodDef oldMethod) { if (oldMethod == null) return; add(oldMethod, builder.instanceMethod("CompileAssemblyFromDom", ICodeCompiler, CompilerResults, CompilerParameters, CodeCompileUnit)); } - public void add_ICodeCompiler_CompileAssemblyFromDomBatch(MethodDefinition oldMethod) { + public void add_ICodeCompiler_CompileAssemblyFromDomBatch(MethodDef oldMethod) { if (oldMethod == null) return; add(oldMethod, builder.instanceMethod("CompileAssemblyFromDomBatch", ICodeCompiler, CompilerResults, CompilerParameters, CodeCompileUnitArray)); } - public void add_ICodeCompiler_CompileAssemblyFromFile(MethodDefinition oldMethod) { + public void add_ICodeCompiler_CompileAssemblyFromFile(MethodDef oldMethod) { if (oldMethod == null) return; add(oldMethod, builder.instanceMethod("CompileAssemblyFromFile", ICodeCompiler, CompilerResults, CompilerParameters, builder.String)); } - public void add_ICodeCompiler_CompileAssemblyFromFileBatch(MethodDefinition oldMethod) { + public void add_ICodeCompiler_CompileAssemblyFromFileBatch(MethodDef oldMethod) { if (oldMethod == null) return; add(oldMethod, builder.instanceMethod("CompileAssemblyFromFileBatch", ICodeCompiler, CompilerResults, CompilerParameters, StringArray)); } - public void add_ICodeCompiler_CompileAssemblyFromSource(MethodDefinition oldMethod) { + public void add_ICodeCompiler_CompileAssemblyFromSource(MethodDef oldMethod) { if (oldMethod == null) return; add(oldMethod, builder.instanceMethod("CompileAssemblyFromSource", ICodeCompiler, CompilerResults, CompilerParameters, builder.String)); } - public void add_ICodeCompiler_CompileAssemblyFromSourceBatch(MethodDefinition oldMethod) { + public void add_ICodeCompiler_CompileAssemblyFromSourceBatch(MethodDef oldMethod) { if (oldMethod == null) return; add(oldMethod, builder.instanceMethod("CompileAssemblyFromSourceBatch", ICodeCompiler, CompilerResults, CompilerParameters, StringArray)); diff --git a/de4dot.code/deobfuscators/Eazfuscator_NET/DecrypterType.cs b/de4dot.code/deobfuscators/Eazfuscator_NET/DecrypterType.cs index f113dfa0..9302219c 100644 --- a/de4dot.code/deobfuscators/Eazfuscator_NET/DecrypterType.cs +++ b/de4dot.code/deobfuscators/Eazfuscator_NET/DecrypterType.cs @@ -20,23 +20,23 @@ using System; using System.Collections.Generic; using System.Text; -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using de4dot.blocks; namespace de4dot.code.deobfuscators.Eazfuscator_NET { class DecrypterType { ModuleDefinition module; ISimpleDeobfuscator simpleDeobfuscator; - TypeDefinition type; - MethodDefinition int64Method; + TypeDef type; + MethodDef int64Method; bool initialized; ulong l1; int i1, i2, i3; int m1_i1, m2_i1, m2_i2, m3_i1; - MethodDefinition[] efConstMethods; + MethodDef[] efConstMethods; - public TypeDefinition Type { + public TypeDef Type { get { return type; } set { if (type == null) @@ -80,7 +80,7 @@ namespace de4dot.code.deobfuscators.Eazfuscator_NET { if (type == null) return false; - efConstMethods = new MethodDefinition[6]; + efConstMethods = new MethodDef[6]; efConstMethods[0] = findEfConstMethodCall(int64Method); efConstMethods[5] = findEfConstMethodCall(efConstMethods[0]); @@ -105,7 +105,7 @@ namespace de4dot.code.deobfuscators.Eazfuscator_NET { return true; } - static int getNumberOfTypeofs(MethodDefinition method) { + static int getNumberOfTypeofs(MethodDef method) { if (method == null) return 0; int count = 0; @@ -116,21 +116,21 @@ namespace de4dot.code.deobfuscators.Eazfuscator_NET { return count; } - MethodDefinition findEfConstMethodCall(MethodDefinition method) { + MethodDef findEfConstMethodCall(MethodDef method) { var list = findEfConstMethodCalls(method); if (list == null || list.Count != 1) return null; return list[0]; } - List findEfConstMethodCalls(MethodDefinition method) { + List findEfConstMethodCalls(MethodDef method) { if (method == null) return null; - var list = new List(); + var list = new List(); foreach (var instr in method.Body.Instructions) { if (instr.OpCode.Code != Code.Call) continue; - var calledMethod = instr.Operand as MethodDefinition; + var calledMethod = instr.Operand as MethodDef; if (calledMethod == null || !calledMethod.IsStatic || calledMethod.Body == null) continue; if (!DotNetUtils.isMethod(calledMethod, "System.Int32", "()")) @@ -143,7 +143,7 @@ namespace de4dot.code.deobfuscators.Eazfuscator_NET { return list; } - MethodDefinition findInt64Method() { + MethodDef findInt64Method() { if (type == null) return null; foreach (var method in type.Methods) { @@ -160,7 +160,7 @@ namespace de4dot.code.deobfuscators.Eazfuscator_NET { return null; } - bool findInt64(MethodDefinition method) { + bool findInt64(MethodDef method) { var instrs = method.Body.Instructions; for (int i = 0; i < instrs.Count - 1; i++) { var ldci8 = instrs[i]; @@ -212,8 +212,8 @@ namespace de4dot.code.deobfuscators.Eazfuscator_NET { return false; } - static List getBinaryIntMethods(TypeDefinition type) { - var list = new List(); + static List getBinaryIntMethods(TypeDef type) { + var list = new List(); foreach (var method in type.Methods) { if (!method.IsStatic || method.Body == null) continue; @@ -225,7 +225,7 @@ namespace de4dot.code.deobfuscators.Eazfuscator_NET { return list; } - bool findMethod1Int(IEnumerable methods) { + bool findMethod1Int(IEnumerable methods) { foreach (var method in methods) { if (countInstructions(method, Code.Ldarg_0) != 1) continue; @@ -239,7 +239,7 @@ namespace de4dot.code.deobfuscators.Eazfuscator_NET { return false; } - bool findMethod2Int(IEnumerable methods) { + bool findMethod2Int(IEnumerable methods) { foreach (var method in methods) { var constants = getConstants(method); if (constants.Count != 2) @@ -252,7 +252,7 @@ namespace de4dot.code.deobfuscators.Eazfuscator_NET { return false; } - bool findMethod3Int(IEnumerable methods) { + bool findMethod3Int(IEnumerable methods) { foreach (var method in methods) { if (countInstructions(method, Code.Ldarg_0) != 2) continue; @@ -266,7 +266,7 @@ namespace de4dot.code.deobfuscators.Eazfuscator_NET { return false; } - static int countInstructions(MethodDefinition method, Code code) { + static int countInstructions(MethodDef method, Code code) { int count = 0; foreach (var instr in method.Body.Instructions) { if (instr.OpCode.Code == code) @@ -275,7 +275,7 @@ namespace de4dot.code.deobfuscators.Eazfuscator_NET { return count; } - static List getConstants(MethodDefinition method) { + static List getConstants(MethodDef method) { var list = new List(); if (method == null) @@ -309,27 +309,27 @@ namespace de4dot.code.deobfuscators.Eazfuscator_NET { } int constMethod1() { - return binOp3(binOp2(efConstMethods[1].DeclaringType.MetadataToken.ToInt32(), binOp3(efConstMethods[0].DeclaringType.MetadataToken.ToInt32(), efConstMethods[4].DeclaringType.MetadataToken.ToInt32())), constMethod6()); + return binOp3(binOp2(efConstMethods[1].DeclaringType.MDToken.ToInt32(), binOp3(efConstMethods[0].DeclaringType.MDToken.ToInt32(), efConstMethods[4].DeclaringType.MDToken.ToInt32())), constMethod6()); } int constMethod2() { - return binOp1(efConstMethods[2].DeclaringType.MetadataToken.ToInt32(), efConstMethods[3].DeclaringType.MetadataToken.ToInt32() ^ binOp2(efConstMethods[1].DeclaringType.MetadataToken.ToInt32(), binOp3(efConstMethods[5].DeclaringType.MetadataToken.ToInt32(), constMethod4()))); + return binOp1(efConstMethods[2].DeclaringType.MDToken.ToInt32(), efConstMethods[3].DeclaringType.MDToken.ToInt32() ^ binOp2(efConstMethods[1].DeclaringType.MDToken.ToInt32(), binOp3(efConstMethods[5].DeclaringType.MDToken.ToInt32(), constMethod4()))); } int constMethod3() { - return binOp3(binOp1(constMethod2() ^ i1, efConstMethods[3].DeclaringType.MetadataToken.ToInt32()), binOp2(efConstMethods[0].DeclaringType.MetadataToken.ToInt32() ^ efConstMethods[5].DeclaringType.MetadataToken.ToInt32(), i2)); + return binOp3(binOp1(constMethod2() ^ i1, efConstMethods[3].DeclaringType.MDToken.ToInt32()), binOp2(efConstMethods[0].DeclaringType.MDToken.ToInt32() ^ efConstMethods[5].DeclaringType.MDToken.ToInt32(), i2)); } int constMethod4() { - return binOp3(efConstMethods[3].DeclaringType.MetadataToken.ToInt32(), binOp1(efConstMethods[0].DeclaringType.MetadataToken.ToInt32(), binOp2(efConstMethods[1].DeclaringType.MetadataToken.ToInt32(), binOp3(efConstMethods[2].DeclaringType.MetadataToken.ToInt32(), binOp1(efConstMethods[4].DeclaringType.MetadataToken.ToInt32(), efConstMethods[5].DeclaringType.MetadataToken.ToInt32()))))); + return binOp3(efConstMethods[3].DeclaringType.MDToken.ToInt32(), binOp1(efConstMethods[0].DeclaringType.MDToken.ToInt32(), binOp2(efConstMethods[1].DeclaringType.MDToken.ToInt32(), binOp3(efConstMethods[2].DeclaringType.MDToken.ToInt32(), binOp1(efConstMethods[4].DeclaringType.MDToken.ToInt32(), efConstMethods[5].DeclaringType.MDToken.ToInt32()))))); } int constMethod5() { - return binOp2(binOp2(constMethod3(), binOp1(efConstMethods[4].DeclaringType.MetadataToken.ToInt32(), constMethod2())), efConstMethods[5].DeclaringType.MetadataToken.ToInt32()); + return binOp2(binOp2(constMethod3(), binOp1(efConstMethods[4].DeclaringType.MDToken.ToInt32(), constMethod2())), efConstMethods[5].DeclaringType.MDToken.ToInt32()); } int constMethod6() { - return binOp1(efConstMethods[5].DeclaringType.MetadataToken.ToInt32(), binOp3(binOp2(efConstMethods[4].DeclaringType.MetadataToken.ToInt32(), efConstMethods[0].DeclaringType.MetadataToken.ToInt32()), binOp3(efConstMethods[2].DeclaringType.MetadataToken.ToInt32() ^ i3, constMethod5()))); + return binOp1(efConstMethods[5].DeclaringType.MDToken.ToInt32(), binOp3(binOp2(efConstMethods[4].DeclaringType.MDToken.ToInt32(), efConstMethods[0].DeclaringType.MDToken.ToInt32()), binOp3(efConstMethods[2].DeclaringType.MDToken.ToInt32() ^ i3, constMethod5()))); } public ulong getMagic() { @@ -343,13 +343,13 @@ namespace de4dot.code.deobfuscators.Eazfuscator_NET { bytes.AddRange(Encoding.Unicode.GetBytes(module.Assembly.Name.Name)); } int cm1 = constMethod1(); - bytes.Add((byte)(type.MetadataToken.ToInt32() >> 24)); + bytes.Add((byte)(type.MDToken.ToInt32() >> 24)); bytes.Add((byte)(cm1 >> 16)); - bytes.Add((byte)(type.MetadataToken.ToInt32() >> 8)); + bytes.Add((byte)(type.MDToken.ToInt32() >> 8)); bytes.Add((byte)cm1); - bytes.Add((byte)(type.MetadataToken.ToInt32() >> 16)); + bytes.Add((byte)(type.MDToken.ToInt32() >> 16)); bytes.Add((byte)(cm1 >> 8)); - bytes.Add((byte)type.MetadataToken.ToInt32()); + bytes.Add((byte)type.MDToken.ToInt32()); bytes.Add((byte)(cm1 >> 24)); ulong magic = 0; diff --git a/de4dot.code/deobfuscators/Eazfuscator_NET/Deobfuscator.cs b/de4dot.code/deobfuscators/Eazfuscator_NET/Deobfuscator.cs index d4f39652..3bdde714 100644 --- a/de4dot.code/deobfuscators/Eazfuscator_NET/Deobfuscator.cs +++ b/de4dot.code/deobfuscators/Eazfuscator_NET/Deobfuscator.cs @@ -18,7 +18,7 @@ */ using System.Collections.Generic; -using Mono.Cecil; +using dot10.DotNet; using de4dot.blocks; namespace de4dot.code.deobfuscators.Eazfuscator_NET { @@ -167,7 +167,7 @@ namespace de4dot.code.deobfuscators.Eazfuscator_NET { public override IEnumerable getStringDecrypterMethods() { var list = new List(); if (stringDecrypter.Method != null) - list.Add(stringDecrypter.Method.MetadataToken.ToInt32()); + list.Add(stringDecrypter.Method.MDToken.ToInt32()); return list; } } diff --git a/de4dot.code/deobfuscators/Eazfuscator_NET/EfConstantsReader.cs b/de4dot.code/deobfuscators/Eazfuscator_NET/EfConstantsReader.cs index e3b2e3f6..fcd09ef8 100644 --- a/de4dot.code/deobfuscators/Eazfuscator_NET/EfConstantsReader.cs +++ b/de4dot.code/deobfuscators/Eazfuscator_NET/EfConstantsReader.cs @@ -17,13 +17,13 @@ along with de4dot. If not, see . */ -using Mono.Cecil; +using dot10.DotNet; using Mono.Cecil.Metadata; using de4dot.blocks; namespace de4dot.code.deobfuscators.Eazfuscator_NET { class EfConstantsReader : ConstantsReader { - public EfConstantsReader(MethodDefinition method) + public EfConstantsReader(MethodDef method) : base(method) { initialize(); } diff --git a/de4dot.code/deobfuscators/Eazfuscator_NET/EfUtils.cs b/de4dot.code/deobfuscators/Eazfuscator_NET/EfUtils.cs index fd3af58a..c6f8fbef 100644 --- a/de4dot.code/deobfuscators/Eazfuscator_NET/EfUtils.cs +++ b/de4dot.code/deobfuscators/Eazfuscator_NET/EfUtils.cs @@ -18,13 +18,13 @@ */ using System.Collections.Generic; -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using de4dot.blocks; namespace de4dot.code.deobfuscators.Eazfuscator_NET { static class EfUtils { - public static int findOpCodeIndex(MethodDefinition method, int index, Code code) { + public static int findOpCodeIndex(MethodDef method, int index, Code code) { for (; index < method.Body.Instructions.Count; index++) { var instr = method.Body.Instructions[index]; if (instr.OpCode.Code != code) @@ -35,7 +35,7 @@ namespace de4dot.code.deobfuscators.Eazfuscator_NET { return -1; } - public static int findOpCodeIndex(MethodDefinition method, int index, Code code, string operandString) { + public static int findOpCodeIndex(MethodDef method, int index, Code code, string operandString) { while (index < method.Body.Instructions.Count) { index = findOpCodeIndex(method, index, code); if (index < 0) @@ -49,7 +49,7 @@ namespace de4dot.code.deobfuscators.Eazfuscator_NET { return -1; } - public static Instruction getNextStore(MethodDefinition method, ref int index) { + public static Instruction getNextStore(MethodDef method, ref int index) { for (; index < method.Body.Instructions.Count; index++) { var instr = method.Body.Instructions[index]; diff --git a/de4dot.code/deobfuscators/Eazfuscator_NET/ResourceMethodsRestorer.cs b/de4dot.code/deobfuscators/Eazfuscator_NET/ResourceMethodsRestorer.cs index 12377401..70be4e2b 100644 --- a/de4dot.code/deobfuscators/Eazfuscator_NET/ResourceMethodsRestorer.cs +++ b/de4dot.code/deobfuscators/Eazfuscator_NET/ResourceMethodsRestorer.cs @@ -17,15 +17,15 @@ along with de4dot. If not, see . */ -using Mono.Cecil; +using dot10.DotNet; using de4dot.blocks; namespace de4dot.code.deobfuscators.Eazfuscator_NET { class ResourceMethodsRestorer : MethodCallRestorerBase { - TypeDefinition getManifestResourceStreamType; + TypeDef getManifestResourceStreamType; EmbeddedResource getManifestResourceStreamTypeResource; - public TypeDefinition Type { + public TypeDef Type { get { return getManifestResourceStreamType; } } @@ -69,7 +69,7 @@ namespace de4dot.code.deobfuscators.Eazfuscator_NET { } } - EmbeddedResource findGetManifestResourceStreamTypeResource(TypeDefinition type, ISimpleDeobfuscator simpleDeobfuscator, IDeobfuscator deob) { + EmbeddedResource findGetManifestResourceStreamTypeResource(TypeDef type, ISimpleDeobfuscator simpleDeobfuscator, IDeobfuscator deob) { foreach (var method in type.Methods) { if (!method.IsPrivate || !method.IsStatic || method.Body == null) continue; @@ -86,8 +86,8 @@ namespace de4dot.code.deobfuscators.Eazfuscator_NET { return null; } - static MethodDefinition getTheOnlyMethod(TypeDefinition type, string returnType, string parameters) { - MethodDefinition foundMethod = null; + static MethodDef getTheOnlyMethod(TypeDef type, string returnType, string parameters) { + MethodDef foundMethod = null; foreach (var method in type.Methods) { if (!method.IsStatic || method.Body == null || method.HasGenericParameters) diff --git a/de4dot.code/deobfuscators/Eazfuscator_NET/ResourceResolver.cs b/de4dot.code/deobfuscators/Eazfuscator_NET/ResourceResolver.cs index 37343c7e..f4415287 100644 --- a/de4dot.code/deobfuscators/Eazfuscator_NET/ResourceResolver.cs +++ b/de4dot.code/deobfuscators/Eazfuscator_NET/ResourceResolver.cs @@ -20,24 +20,24 @@ using System; using System.Collections.Generic; using System.IO; -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using de4dot.blocks; namespace de4dot.code.deobfuscators.Eazfuscator_NET { class ResourceResolver { ModuleDefinition module; AssemblyResolver assemblyResolver; - TypeDefinition resolverType; - MethodDefinition initMethod; - MethodDefinition handlerMethod; + TypeDef resolverType; + MethodDef initMethod; + MethodDef handlerMethod; List resourceInfos = new List(); - public TypeDefinition Type { + public TypeDef Type { get { return resolverType; } } - public MethodDefinition InitMethod { + public MethodDef InitMethod { get { return initMethod; } } @@ -56,14 +56,14 @@ namespace de4dot.code.deobfuscators.Eazfuscator_NET { checkCalledMethods(DotNetUtils.getModuleTypeCctor(module)); } - bool checkCalledMethods(MethodDefinition method) { + bool checkCalledMethods(MethodDef method) { if (method == null || method.Body == null) return false; foreach (var instr in method.Body.Instructions) { if (instr.OpCode.Code != Code.Call) continue; - if (!checkInitMethod(instr.Operand as MethodDefinition)) + if (!checkInitMethod(instr.Operand as MethodDef)) continue; return true; @@ -72,7 +72,7 @@ namespace de4dot.code.deobfuscators.Eazfuscator_NET { return false; } - bool checkInitMethod(MethodDefinition method) { + bool checkInitMethod(MethodDef method) { if (method == null || !method.IsStatic || method.Body == null) return false; if (!DotNetUtils.isMethod(method, "System.Void", "()")) @@ -121,7 +121,7 @@ namespace de4dot.code.deobfuscators.Eazfuscator_NET { return false; } - bool initializeInfos(MethodDefinition method) { + bool initializeInfos(MethodDef method) { foreach (var s in DotNetUtils.getCodeStrings(method)) { if (string.IsNullOrEmpty(s)) continue; diff --git a/de4dot.code/deobfuscators/Eazfuscator_NET/StringDecrypter.cs b/de4dot.code/deobfuscators/Eazfuscator_NET/StringDecrypter.cs index 6590dc48..74a5e717 100644 --- a/de4dot.code/deobfuscators/Eazfuscator_NET/StringDecrypter.cs +++ b/de4dot.code/deobfuscators/Eazfuscator_NET/StringDecrypter.cs @@ -21,17 +21,17 @@ using System; using System.Collections.Generic; using System.IO; using System.Text; -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using Mono.Cecil.Metadata; using de4dot.blocks; namespace de4dot.code.deobfuscators.Eazfuscator_NET { class StringDecrypter { ModuleDefinition module; - TypeDefinition stringType; - MethodDefinition stringMethod; - TypeDefinition dataDecrypterType; + TypeDef stringType; + MethodDef stringMethod; + TypeDef dataDecrypterType; short s1, s2, s3; int i1, i2, i3, i4, i5, i6; bool checkMinus2; @@ -48,10 +48,10 @@ namespace de4dot.code.deobfuscators.Eazfuscator_NET { bool isV32OrLater; class StreamHelperType { - public TypeDefinition type; - public MethodDefinition readInt16Method; - public MethodDefinition readInt32Method; - public MethodDefinition readBytesMethod; + public TypeDef type; + public MethodDef readInt16Method; + public MethodDef readInt32Method; + public MethodDef readBytesMethod; public bool Detected { get { @@ -61,7 +61,7 @@ namespace de4dot.code.deobfuscators.Eazfuscator_NET { } } - public StreamHelperType(TypeDefinition type) { + public StreamHelperType(TypeDef type) { this.type = type; foreach (var method in type.Methods) { @@ -77,7 +77,7 @@ namespace de4dot.code.deobfuscators.Eazfuscator_NET { } } - public TypeDefinition Type { + public TypeDef Type { get { return stringType; } } @@ -85,16 +85,16 @@ namespace de4dot.code.deobfuscators.Eazfuscator_NET { get { return encryptedResource; } } - public IEnumerable Types { + public IEnumerable Types { get { - return new List { + return new List { stringType, dataDecrypterType, }; } } - public MethodDefinition Method { + public MethodDef Method { get { return stringMethod; } } @@ -107,7 +107,7 @@ namespace de4dot.code.deobfuscators.Eazfuscator_NET { this.decrypterType = decrypterType; } - static bool checkIfV32OrLater(TypeDefinition type) { + static bool checkIfV32OrLater(TypeDef type) { int numInts = 0; foreach (var field in type.Fields) { if (field.FieldType.EType == ElementType.I4) @@ -137,7 +137,7 @@ namespace de4dot.code.deobfuscators.Eazfuscator_NET { "System.Byte[]", "System.Int16", }; - bool checkType(TypeDefinition type) { + bool checkType(TypeDef type) { if (!new FieldTypes(type).all(requiredFieldTypes)) return false; if (type.NestedTypes.Count == 0) { @@ -159,9 +159,9 @@ namespace de4dot.code.deobfuscators.Eazfuscator_NET { "System.IO.Stream", "System.Byte[]", }; - static StreamHelperType findStreamHelperType(TypeDefinition type) { + static StreamHelperType findStreamHelperType(TypeDef type) { foreach (var field in type.Fields) { - var nested = field.FieldType as TypeDefinition; + var nested = field.FieldType as TypeDef; if (nested == null) continue; if (nested.DeclaringType != type) @@ -186,7 +186,7 @@ namespace de4dot.code.deobfuscators.Eazfuscator_NET { "System.Reflection.Assembly", "System.String", }; - static bool checkDecrypterMethod(MethodDefinition method) { + static bool checkDecrypterMethod(MethodDef method) { if (method == null || !method.IsStatic || method.Body == null) return false; if (!DotNetUtils.isMethod(method, "System.String", "(System.Int32)")) @@ -344,7 +344,7 @@ namespace de4dot.code.deobfuscators.Eazfuscator_NET { return null; } - static int getFlagsOffset(MethodDefinition method, int index, VariableDefinition local) { + static int getFlagsOffset(MethodDef method, int index, VariableDefinition local) { var instrs = method.Body.Instructions; for (; index < instrs.Count; index++) { var ldloc = instrs[index]; @@ -358,7 +358,7 @@ namespace de4dot.code.deobfuscators.Eazfuscator_NET { return -1; } - static VariableDefinition getFlagsLocal(MethodDefinition method, int index) { + static VariableDefinition getFlagsLocal(MethodDef method, int index) { var instrs = method.Body.Instructions; if (index + 5 >= instrs.Count) return null; @@ -485,17 +485,17 @@ namespace de4dot.code.deobfuscators.Eazfuscator_NET { return lcg * 214013 + 2531011; } - bool findResource(MethodDefinition method) { + bool findResource(MethodDef method) { encryptedResource = findResourceFromCodeString(method) ?? findResourceFromStringBuilder(method); return encryptedResource != null; } - EmbeddedResource findResourceFromCodeString(MethodDefinition method) { + EmbeddedResource findResourceFromCodeString(MethodDef method) { return DotNetUtils.getResource(module, DotNetUtils.getCodeStrings(method)) as EmbeddedResource; } - EmbeddedResource findResourceFromStringBuilder(MethodDefinition method) { + EmbeddedResource findResourceFromStringBuilder(MethodDef method) { int startIndex = EfUtils.findOpCodeIndex(method, 0, Code.Newobj, "System.Void System.Text.StringBuilder::.ctor()"); if (startIndex < 0) return null; @@ -532,11 +532,11 @@ namespace de4dot.code.deobfuscators.Eazfuscator_NET { return DotNetUtils.getResource(module, sb.ToString()) as EmbeddedResource; } - static MethodDefinition findInt64Method(MethodDefinition method) { + static MethodDef findInt64Method(MethodDef method) { foreach (var instr in method.Body.Instructions) { if (instr.OpCode.Code != Code.Call) continue; - var calledMethod = instr.Operand as MethodDefinition; + var calledMethod = instr.Operand as MethodDef; if (calledMethod == null) continue; if (!DotNetUtils.isMethod(calledMethod, "System.Int64", "()")) @@ -547,11 +547,11 @@ namespace de4dot.code.deobfuscators.Eazfuscator_NET { return null; } - static TypeDefinition findDataDecrypterType(MethodDefinition method) { + static TypeDef findDataDecrypterType(MethodDef method) { foreach (var instr in method.Body.Instructions) { if (instr.OpCode.Code != Code.Call) continue; - var calledMethod = instr.Operand as MethodDefinition; + var calledMethod = instr.Operand as MethodDef; if (calledMethod == null) continue; if (!DotNetUtils.isMethod(calledMethod, "System.Byte[]", "(System.Byte[],System.Byte[])")) @@ -617,7 +617,7 @@ namespace de4dot.code.deobfuscators.Eazfuscator_NET { return returnValue; } - static int findInitIntsIndex(MethodDefinition method, out bool initializedAll) { + static int findInitIntsIndex(MethodDef method, out bool initializedAll) { initializedAll = false; var instrs = method.Body.Instructions; @@ -630,13 +630,13 @@ namespace de4dot.code.deobfuscators.Eazfuscator_NET { if (stsfld.OpCode.Code != Code.Stsfld) continue; - var storeField = stsfld.Operand as FieldDefinition; + var storeField = stsfld.Operand as FieldDef; if (storeField == null || storeField.FieldType.FullName != "System.Byte[]") continue; var instr = instrs[i + 2]; if (instr.OpCode.Code == Code.Ldsfld) { - var loadField = instr.Operand as FieldDefinition; + var loadField = instr.Operand as FieldDef; if (loadField == null || loadField.FieldType.EType != ElementType.I4) continue; } @@ -652,7 +652,7 @@ namespace de4dot.code.deobfuscators.Eazfuscator_NET { return -1; } - bool findIntsCctor(MethodDefinition cctor) { + bool findIntsCctor(MethodDef cctor) { int index = 0; if (!findCallGetFrame(cctor, ref index)) return findIntsCctor2(cctor); @@ -683,7 +683,7 @@ namespace de4dot.code.deobfuscators.Eazfuscator_NET { } // Compact Framework doesn't have StackFrame - bool findIntsCctor2(MethodDefinition cctor) { + bool findIntsCctor2(MethodDef cctor) { int index = 0; var instrs = cctor.Body.Instructions; var constantsReader = new EfConstantsReader(cctor); @@ -822,7 +822,7 @@ namespace de4dot.code.deobfuscators.Eazfuscator_NET { } } - static bool callsGetPublicKeyToken(MethodDefinition method) { + static bool callsGetPublicKeyToken(MethodDef method) { int index = 0; return findCall(method, ref index, "System.Byte[] System.Reflection.AssemblyName::GetPublicKeyToken()"); } @@ -839,11 +839,11 @@ namespace de4dot.code.deobfuscators.Eazfuscator_NET { return findCall(stringMethod, ref index, streamHelperType == null ? "System.Byte[] System.IO.BinaryReader::ReadBytes(System.Int32)" : streamHelperType.readBytesMethod.FullName); } - static bool findCallGetFrame(MethodDefinition method, ref int index) { + static bool findCallGetFrame(MethodDef method, ref int index) { return findCall(method, ref index, "System.Diagnostics.StackFrame System.Diagnostics.StackTrace::GetFrame(System.Int32)"); } - static bool findCall(MethodDefinition method, ref int index, string methodFullName) { + static bool findCall(MethodDef method, ref int index, string methodFullName) { for (; index < method.Body.Instructions.Count; index++) { if (!findCallvirt(method, ref index)) return false; @@ -859,7 +859,7 @@ namespace de4dot.code.deobfuscators.Eazfuscator_NET { return false; } - static bool findCallvirt(MethodDefinition method, ref int index) { + static bool findCallvirt(MethodDef method, ref int index) { var instrs = method.Body.Instructions; for (; index < instrs.Count; index++) { var instr = instrs[index]; diff --git a/de4dot.code/deobfuscators/Eazfuscator_NET/VersionDetector.cs b/de4dot.code/deobfuscators/Eazfuscator_NET/VersionDetector.cs index c195d22b..576413e7 100644 --- a/de4dot.code/deobfuscators/Eazfuscator_NET/VersionDetector.cs +++ b/de4dot.code/deobfuscators/Eazfuscator_NET/VersionDetector.cs @@ -19,7 +19,7 @@ using System; using System.Collections.Generic; -using Mono.Cecil; +using dot10.DotNet; using de4dot.blocks; namespace de4dot.code.deobfuscators.Eazfuscator_NET { @@ -38,8 +38,8 @@ namespace de4dot.code.deobfuscators.Eazfuscator_NET { if (decryptStringType == null || decryptStringMethod == null) return null; - var otherMethods = new List(); - MethodDefinition cctor = null; + var otherMethods = new List(); + MethodDef cctor = null; foreach (var method in decryptStringType.Methods) { if (method == decryptStringMethod) continue; @@ -671,7 +671,7 @@ namespace de4dot.code.deobfuscators.Eazfuscator_NET { return null; } - TypeDefinition getNestedType(int n) { + TypeDef getNestedType(int n) { var type = stringDecrypter.Type; int fieldIndex; @@ -684,7 +684,7 @@ namespace de4dot.code.deobfuscators.Eazfuscator_NET { if (fieldIndex >= type.Fields.Count) return null; - var nestedType = type.Fields[fieldIndex].FieldType as TypeDefinition; + var nestedType = type.Fields[fieldIndex].FieldType as TypeDef; if (nestedType == null || type.NestedTypes.IndexOf(nestedType) < 0) return null; diff --git a/de4dot.code/deobfuscators/Goliath_NET/ArrayDecrypter.cs b/de4dot.code/deobfuscators/Goliath_NET/ArrayDecrypter.cs index cff22fdc..ffc10a0b 100644 --- a/de4dot.code/deobfuscators/Goliath_NET/ArrayDecrypter.cs +++ b/de4dot.code/deobfuscators/Goliath_NET/ArrayDecrypter.cs @@ -18,7 +18,7 @@ */ using System; -using Mono.Cecil; +using dot10.DotNet; using de4dot.blocks; namespace de4dot.code.deobfuscators.Goliath_NET { @@ -31,15 +31,15 @@ namespace de4dot.code.deobfuscators.Goliath_NET { "System.Byte[]", "System.Collections.Generic.Dictionary`2", }; - protected override bool checkDecrypterType(TypeDefinition type) { + protected override bool checkDecrypterType(TypeDef type) { return new FieldTypes(type).exactly(requiredFields); } - protected override bool checkDelegateInvokeMethod(MethodDefinition invokeMethod) { + protected override bool checkDelegateInvokeMethod(MethodDef invokeMethod) { return DotNetUtils.isMethod(invokeMethod, "System.Byte[]", "(System.Int32)"); } - public byte[] decrypt(MethodDefinition method) { + public byte[] decrypt(MethodDef method) { var info = getInfo(method); decryptedReader.BaseStream.Position = info.offset; return decryptedReader.ReadBytes(decryptedReader.ReadInt32()); diff --git a/de4dot.code/deobfuscators/Goliath_NET/ArrayValueInliner.cs b/de4dot.code/deobfuscators/Goliath_NET/ArrayValueInliner.cs index a1315ef9..be9bab4c 100644 --- a/de4dot.code/deobfuscators/Goliath_NET/ArrayValueInliner.cs +++ b/de4dot.code/deobfuscators/Goliath_NET/ArrayValueInliner.cs @@ -19,8 +19,8 @@ using System; using System.Collections.Generic; -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using de4dot.blocks; namespace de4dot.code.deobfuscators.Goliath_NET { diff --git a/de4dot.code/deobfuscators/Goliath_NET/DecrypterBase.cs b/de4dot.code/deobfuscators/Goliath_NET/DecrypterBase.cs index 3a917bec..f109f613 100644 --- a/de4dot.code/deobfuscators/Goliath_NET/DecrypterBase.cs +++ b/de4dot.code/deobfuscators/Goliath_NET/DecrypterBase.cs @@ -20,25 +20,25 @@ using System; using System.Collections.Generic; using System.IO; -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using de4dot.blocks; namespace de4dot.code.deobfuscators.Goliath_NET { abstract class DecrypterBase { protected ModuleDefinition module; EmbeddedResource encryptedResource; - TypeDefinition decrypterType; - TypeDefinition delegateType; - TypeDefinition delegateInitType; + TypeDef decrypterType; + TypeDef delegateType; + TypeDef delegateInitType; protected BinaryReader decryptedReader; MethodDefinitionAndDeclaringTypeDict decrypterMethods = new MethodDefinitionAndDeclaringTypeDict(); protected class Info { - public MethodDefinition method; + public MethodDef method; public int offset; public bool referenced = false; - public Info(MethodDefinition method, int offset) { + public Info(MethodDef method, int offset) { this.method = method; this.offset = offset; } @@ -52,21 +52,21 @@ namespace de4dot.code.deobfuscators.Goliath_NET { get { return encryptedResource; } } - public TypeDefinition Type { + public TypeDef Type { get { return decrypterType; } } - public TypeDefinition DelegateInitType { + public TypeDef DelegateInitType { get { return delegateInitType ?? findDelegateInitType();} } - public TypeDefinition DelegateType { + public TypeDef DelegateType { get { return delegateType; } } - public IEnumerable DecrypterTypes { + public IEnumerable DecrypterTypes { get { - var types = new TypeDefinitionDict(); + var types = new TypeDefinitionDict(); foreach (var info in decrypterMethods.getValues()) { if (info.referenced) types.add(info.method.DeclaringType, info.method.DeclaringType); @@ -79,7 +79,7 @@ namespace de4dot.code.deobfuscators.Goliath_NET { this.module = module; } - protected Info getInfo(MethodDefinition method) { + protected Info getInfo(MethodDef method) { var info = decrypterMethods.find(method); if (info == null) return null; @@ -110,7 +110,7 @@ namespace de4dot.code.deobfuscators.Goliath_NET { } } - protected abstract bool checkDecrypterType(TypeDefinition type); + protected abstract bool checkDecrypterType(TypeDef type); void splitTypeName(string fullName, out string ns, out string name) { int index = fullName.LastIndexOf('.'); @@ -171,13 +171,13 @@ namespace de4dot.code.deobfuscators.Goliath_NET { } } - Info getDecrypterInfo(MethodDefinition method, FieldDefinition delegateField) { + Info getDecrypterInfo(MethodDef method, FieldDef delegateField) { try { int index = 0; var instrs = method.Body.Instructions; if (instrs[index].OpCode.Code != Code.Ldsfld) return null; - var field = instrs[index++].Operand as FieldDefinition; + var field = instrs[index++].Operand as FieldDef; if (field != delegateField) return null; @@ -204,12 +204,12 @@ namespace de4dot.code.deobfuscators.Goliath_NET { } } - bool checkCctor(MethodDefinition cctor) { + bool checkCctor(MethodDef cctor) { var ldtokenType = getLdtokenType(cctor); if (!MemberReferenceHelper.compareTypes(ldtokenType, cctor.DeclaringType)) return false; - MethodDefinition initMethod = null; + MethodDef initMethod = null; foreach (var method in DotNetUtils.getCalledMethods(module, cctor)) { if (DotNetUtils.isMethod(method, "System.Void", "(System.Type)")) { initMethod = method; @@ -222,7 +222,7 @@ namespace de4dot.code.deobfuscators.Goliath_NET { return true; } - static TypeReference getLdtokenType(MethodDefinition method) { + static TypeReference getLdtokenType(MethodDef method) { if (method == null || method.Body == null) return null; foreach (var instr in method.Body.Instructions) { @@ -233,7 +233,7 @@ namespace de4dot.code.deobfuscators.Goliath_NET { return null; } - bool checkDelegateType(TypeDefinition type) { + bool checkDelegateType(TypeDef type) { if (!DotNetUtils.derivesFromDelegate(type)) return false; var invoke = DotNetUtils.getMethod(type, "Invoke"); @@ -242,7 +242,7 @@ namespace de4dot.code.deobfuscators.Goliath_NET { return checkDelegateInvokeMethod(invoke); } - protected abstract bool checkDelegateInvokeMethod(MethodDefinition invokeMethod); + protected abstract bool checkDelegateInvokeMethod(MethodDef invokeMethod); byte[] decrypt(byte[] encryptedData) { const int KEY_LEN = 0x100; @@ -265,7 +265,7 @@ namespace de4dot.code.deobfuscators.Goliath_NET { return decryptedData; } - TypeDefinition findDelegateInitType() { + TypeDef findDelegateInitType() { if (delegateType == null) return null; @@ -290,8 +290,8 @@ namespace de4dot.code.deobfuscators.Goliath_NET { return null; } - public IEnumerable getMethods() { - var list = new List(decrypterMethods.Count); + public IEnumerable getMethods() { + var list = new List(decrypterMethods.Count); foreach (var info in decrypterMethods.getValues()) list.Add(info.method); return list; diff --git a/de4dot.code/deobfuscators/Goliath_NET/Deobfuscator.cs b/de4dot.code/deobfuscators/Goliath_NET/Deobfuscator.cs index 1ae6dc3d..059134a2 100644 --- a/de4dot.code/deobfuscators/Goliath_NET/Deobfuscator.cs +++ b/de4dot.code/deobfuscators/Goliath_NET/Deobfuscator.cs @@ -18,7 +18,7 @@ */ using System.Collections.Generic; -using Mono.Cecil; +using dot10.DotNet; using de4dot.blocks; namespace de4dot.code.deobfuscators.Goliath_NET { @@ -162,7 +162,7 @@ namespace de4dot.code.deobfuscators.Goliath_NET { } } - void initializeVersion(TypeDefinition attr) { + void initializeVersion(TypeDef attr) { var s = DotNetUtils.getCustomArgAsString(getAssemblyAttribute(attr), 0); if (s == null) return; @@ -274,7 +274,7 @@ namespace de4dot.code.deobfuscators.Goliath_NET { public override IEnumerable getStringDecrypterMethods() { var list = new List(); foreach (var method in stringDecrypter.getMethods()) - list.Add(method.MetadataToken.ToInt32()); + list.Add(method.MDToken.ToInt32()); return list; } } diff --git a/de4dot.code/deobfuscators/Goliath_NET/IntegerDecrypter.cs b/de4dot.code/deobfuscators/Goliath_NET/IntegerDecrypter.cs index 694771fe..7c277f20 100644 --- a/de4dot.code/deobfuscators/Goliath_NET/IntegerDecrypter.cs +++ b/de4dot.code/deobfuscators/Goliath_NET/IntegerDecrypter.cs @@ -18,7 +18,7 @@ */ using System; -using Mono.Cecil; +using dot10.DotNet; using de4dot.blocks; namespace de4dot.code.deobfuscators.Goliath_NET { @@ -31,15 +31,15 @@ namespace de4dot.code.deobfuscators.Goliath_NET { "System.Byte[]", "System.Collections.Generic.Dictionary`2", }; - protected override bool checkDecrypterType(TypeDefinition type) { + protected override bool checkDecrypterType(TypeDef type) { return new FieldTypes(type).exactly(requiredFields); } - protected override bool checkDelegateInvokeMethod(MethodDefinition invokeMethod) { + protected override bool checkDelegateInvokeMethod(MethodDef invokeMethod) { return DotNetUtils.isMethod(invokeMethod, "System.Object", "(System.Int32)"); } - public int decrypt(MethodDefinition method) { + public int decrypt(MethodDef method) { var info = getInfo(method); decryptedReader.BaseStream.Position = info.offset; int len = decryptedReader.ReadInt32(); diff --git a/de4dot.code/deobfuscators/Goliath_NET/LocalsRestorer.cs b/de4dot.code/deobfuscators/Goliath_NET/LocalsRestorer.cs index 9efb4791..7acd1dbd 100644 --- a/de4dot.code/deobfuscators/Goliath_NET/LocalsRestorer.cs +++ b/de4dot.code/deobfuscators/Goliath_NET/LocalsRestorer.cs @@ -18,8 +18,8 @@ */ using System.Collections.Generic; -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using de4dot.blocks; namespace de4dot.code.deobfuscators.Goliath_NET { @@ -28,18 +28,18 @@ namespace de4dot.code.deobfuscators.Goliath_NET { TypeDefinitionDict typeToInfo = new TypeDefinitionDict(); class Info { - public TypeDefinition type; + public TypeDef type; public TypeReference localType; public bool referenced = false; - public Info(TypeDefinition type, TypeReference localType) { + public Info(TypeDef type, TypeReference localType) { this.type = type; this.localType = localType; } } - public List Types { + public List Types { get { - var list = new List(typeToInfo.Count); + var list = new List(typeToInfo.Count); foreach (var info in typeToInfo.getValues()) { if (info.referenced) list.Add(info.type); @@ -57,7 +57,7 @@ namespace de4dot.code.deobfuscators.Goliath_NET { initialize(type); } - void initialize(TypeDefinition type) { + void initialize(TypeDef type) { if (type.HasEvents || type.HasProperties) return; diff --git a/de4dot.code/deobfuscators/Goliath_NET/LogicalExpressionFixer.cs b/de4dot.code/deobfuscators/Goliath_NET/LogicalExpressionFixer.cs index 20c7027e..f481781a 100644 --- a/de4dot.code/deobfuscators/Goliath_NET/LogicalExpressionFixer.cs +++ b/de4dot.code/deobfuscators/Goliath_NET/LogicalExpressionFixer.cs @@ -17,7 +17,7 @@ along with de4dot. If not, see . */ -using Mono.Cecil.Cil; +using dot10.DotNet.Emit; using de4dot.blocks; namespace de4dot.code.deobfuscators.Goliath_NET { diff --git a/de4dot.code/deobfuscators/Goliath_NET/ProxyCallFixer.cs b/de4dot.code/deobfuscators/Goliath_NET/ProxyCallFixer.cs index 9340b227..05a2bc98 100644 --- a/de4dot.code/deobfuscators/Goliath_NET/ProxyCallFixer.cs +++ b/de4dot.code/deobfuscators/Goliath_NET/ProxyCallFixer.cs @@ -19,8 +19,8 @@ using System; using System.Collections.Generic; -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using de4dot.blocks; namespace de4dot.code.deobfuscators.Goliath_NET { @@ -30,9 +30,9 @@ namespace de4dot.code.deobfuscators.Goliath_NET { } class MyInfo { - public MethodDefinition method; + public MethodDef method; public DelegateInfo delegateInfo; - public MyInfo(MethodDefinition method, DelegateInfo delegateInfo) { + public MyInfo(MethodDef method, DelegateInfo delegateInfo) { this.method = method; this.delegateInfo = delegateInfo; } @@ -56,7 +56,7 @@ namespace de4dot.code.deobfuscators.Goliath_NET { if (infos.Count == 0) continue; - Log.v("Found proxy delegate: {0} ({1:X8})", Utils.removeNewlines(type), type.MetadataToken.ToUInt32()); + Log.v("Found proxy delegate: {0} ({1:X8})", Utils.removeNewlines(type), type.MDToken.ToUInt32()); RemovedDelegateCreatorCalls++; Log.indent(); foreach (var info in infos) { @@ -66,14 +66,14 @@ namespace de4dot.code.deobfuscators.Goliath_NET { Utils.removeNewlines(di.field.Name), di.callOpcode, Utils.removeNewlines(di.methodRef), - di.methodRef.MetadataToken.ToUInt32()); + di.methodRef.MDToken.ToUInt32()); } Log.deIndent(); delegateTypesDict[type] = true; } } - bool checkProxyMethod(MethodDefinition method, out DelegateInfo info) { + bool checkProxyMethod(MethodDef method, out DelegateInfo info) { info = null; if (!method.IsStatic || method.Body == null) return false; @@ -86,7 +86,7 @@ namespace de4dot.code.deobfuscators.Goliath_NET { if (instrs[index].OpCode.Code != Code.Ldsfld) return false; - var field = instrs[index++].Operand as FieldDefinition; + var field = instrs[index++].Operand as FieldDef; if (field == null || !field.IsStatic) return false; if (!MemberReferenceHelper.compareTypes(method.DeclaringType, field.DeclaringType)) @@ -132,11 +132,11 @@ namespace de4dot.code.deobfuscators.Goliath_NET { return true; } - protected override object checkCctor(TypeDefinition type, MethodDefinition cctor) { + protected override object checkCctor(TypeDef type, MethodDef cctor) { throw new System.NotImplementedException(); } - protected override void getCallInfo(object context, FieldDefinition field, out MethodReference calledMethod, out OpCode callOpcode) { + protected override void getCallInfo(object context, FieldDef field, out MethodReference calledMethod, out OpCode callOpcode) { throw new System.NotImplementedException(); } } diff --git a/de4dot.code/deobfuscators/Goliath_NET/StringDecrypter.cs b/de4dot.code/deobfuscators/Goliath_NET/StringDecrypter.cs index c444c8a2..9613f7ca 100644 --- a/de4dot.code/deobfuscators/Goliath_NET/StringDecrypter.cs +++ b/de4dot.code/deobfuscators/Goliath_NET/StringDecrypter.cs @@ -19,16 +19,16 @@ using System; using System.Text; -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using de4dot.blocks; namespace de4dot.code.deobfuscators.Goliath_NET { class StringDecrypter : DecrypterBase { TypeReference delegateReturnType; - FieldDefinition stringStructField; + FieldDef stringStructField; - public TypeDefinition StringStruct { + public TypeDef StringStruct { get { return Detected && stringStructField != null ? stringStructField.DeclaringType : null; } } @@ -40,7 +40,7 @@ namespace de4dot.code.deobfuscators.Goliath_NET { "System.Byte[]", "System.Collections.Generic.Dictionary`2", }; - protected override bool checkDecrypterType(TypeDefinition type) { + protected override bool checkDecrypterType(TypeDef type) { var fields = type.Fields; if (fields.Count != 2) return false; @@ -80,11 +80,11 @@ namespace de4dot.code.deobfuscators.Goliath_NET { return true; } - protected override bool checkDelegateInvokeMethod(MethodDefinition invokeMethod) { + protected override bool checkDelegateInvokeMethod(MethodDef invokeMethod) { return DotNetUtils.isMethod(invokeMethod, delegateReturnType.FullName, "(System.Int32)"); } - public string decrypt(MethodDefinition method) { + public string decrypt(MethodDef method) { var info = getInfo(method); decryptedReader.BaseStream.Position = info.offset; int len = decryptedReader.ReadInt32(); diff --git a/de4dot.code/deobfuscators/Goliath_NET/StrongNameChecker.cs b/de4dot.code/deobfuscators/Goliath_NET/StrongNameChecker.cs index 6aafe804..b59999aa 100644 --- a/de4dot.code/deobfuscators/Goliath_NET/StrongNameChecker.cs +++ b/de4dot.code/deobfuscators/Goliath_NET/StrongNameChecker.cs @@ -17,25 +17,25 @@ along with de4dot. If not, see . */ -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using de4dot.blocks; namespace de4dot.code.deobfuscators.Goliath_NET { class StrongNameChecker { ModuleDefinition module; - TypeDefinition strongNameType; - MethodDefinition strongNameCheckMethod; + TypeDef strongNameType; + MethodDef strongNameCheckMethod; public bool Detected { get { return strongNameType != null;} } - public TypeDefinition Type { + public TypeDef Type { get { return strongNameType; } } - public MethodDefinition CheckerMethod { + public MethodDef CheckerMethod { get { return strongNameCheckMethod; } } @@ -65,7 +65,7 @@ namespace de4dot.code.deobfuscators.Goliath_NET { } } - MethodDefinition getAntiTamperingDetectionMethod(TypeDefinition type) { + MethodDef getAntiTamperingDetectionMethod(TypeDef type) { var requiredLocals = new string[] { "System.Reflection.Assembly", "System.Collections.Generic.Stack`1", @@ -85,7 +85,7 @@ namespace de4dot.code.deobfuscators.Goliath_NET { return null; } - static bool hasThrow(MethodDefinition method) { + static bool hasThrow(MethodDef method) { if (method == null || method.Body == null) return false; foreach (var instr in method.Body.Instructions) { diff --git a/de4dot.code/deobfuscators/ILProtector/Deobfuscator.cs b/de4dot.code/deobfuscators/ILProtector/Deobfuscator.cs index ddd50ca1..7da40551 100644 --- a/de4dot.code/deobfuscators/ILProtector/Deobfuscator.cs +++ b/de4dot.code/deobfuscators/ILProtector/Deobfuscator.cs @@ -19,7 +19,7 @@ using System; using System.Collections.Generic; -using Mono.Cecil; +using dot10.DotNet; using de4dot.blocks; namespace de4dot.code.deobfuscators.ILProtector { diff --git a/de4dot.code/deobfuscators/ILProtector/MainType.cs b/de4dot.code/deobfuscators/ILProtector/MainType.cs index c12e9734..d7f3f571 100644 --- a/de4dot.code/deobfuscators/ILProtector/MainType.cs +++ b/de4dot.code/deobfuscators/ILProtector/MainType.cs @@ -18,26 +18,26 @@ */ using System.Collections.Generic; -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using de4dot.blocks; namespace de4dot.code.deobfuscators.ILProtector { class MainType { ModuleDefinition module; - List protectMethods; - TypeDefinition invokerDelegate; - FieldDefinition invokerInstanceField; + List protectMethods; + TypeDef invokerDelegate; + FieldDef invokerInstanceField; - public IEnumerable ProtectMethods { + public IEnumerable ProtectMethods { get { return protectMethods; } } - public TypeDefinition InvokerDelegate { + public TypeDef InvokerDelegate { get { return invokerDelegate; } } - public FieldDefinition InvokerInstanceField { + public FieldDef InvokerInstanceField { get { return invokerInstanceField; } } @@ -58,7 +58,7 @@ namespace de4dot.code.deobfuscators.ILProtector { "System.IntPtr", "System.Object[]", }; - bool checkMethod(MethodDefinition cctor) { + bool checkMethod(MethodDef cctor) { if (cctor == null || cctor.Body == null) return false; if (!new LocalTypes(cctor).exactly(ilpLocals)) @@ -74,7 +74,7 @@ namespace de4dot.code.deobfuscators.ILProtector { return false; var theField = type.Fields[0]; - var theDelegate = theField.FieldType as TypeDefinition; + var theDelegate = theField.FieldType as TypeDef; if (theDelegate == null || !DotNetUtils.derivesFromDelegate(theDelegate)) return false; @@ -84,8 +84,8 @@ namespace de4dot.code.deobfuscators.ILProtector { return true; } - static List getPinvokeMethods(TypeDefinition type, string name) { - var list = new List(); + static List getPinvokeMethods(TypeDef type, string name) { + var list = new List(); foreach (var method in type.Methods) { if (method.PInvokeInfo != null && method.PInvokeInfo.EntryPoint == name) list.Add(method); diff --git a/de4dot.code/deobfuscators/ILProtector/MethodReader.cs b/de4dot.code/deobfuscators/ILProtector/MethodReader.cs index fbbab94d..1231f902 100644 --- a/de4dot.code/deobfuscators/ILProtector/MethodReader.cs +++ b/de4dot.code/deobfuscators/ILProtector/MethodReader.cs @@ -20,8 +20,8 @@ using System; using System.Collections.Generic; using System.IO; -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using Mono.Cecil.Metadata; using de4dot.blocks; @@ -29,7 +29,7 @@ namespace de4dot.code.deobfuscators.ILProtector { class MethodReader : MethodBodyReaderBase { ModuleDefinition module; MethodFlags flags; - TypeDefinition delegateType; + TypeDef delegateType; [Flags] enum MethodFlags { @@ -39,7 +39,7 @@ namespace de4dot.code.deobfuscators.ILProtector { HasExceptionHandlers = 8, } - public TypeDefinition DelegateType { + public TypeDef DelegateType { get { return delegateType; } } @@ -67,7 +67,7 @@ namespace de4dot.code.deobfuscators.ILProtector { public void read() { flags = (MethodFlags)reader.ReadByte(); - delegateType = resolve(readTypeToken()); + delegateType = resolve(readTypeToken()); if (!DotNetUtils.derivesFromDelegate(delegateType)) throw new ApplicationException("Invalid delegate type"); if (HasLocals) diff --git a/de4dot.code/deobfuscators/ILProtector/MethodsDecrypter.cs b/de4dot.code/deobfuscators/ILProtector/MethodsDecrypter.cs index 15324013..3923b5d2 100644 --- a/de4dot.code/deobfuscators/ILProtector/MethodsDecrypter.cs +++ b/de4dot.code/deobfuscators/ILProtector/MethodsDecrypter.cs @@ -20,8 +20,8 @@ using System; using System.Collections.Generic; using System.IO; -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using de4dot.blocks; namespace de4dot.code.deobfuscators.ILProtector { @@ -36,7 +36,7 @@ namespace de4dot.code.deobfuscators.ILProtector { EmbeddedResource methodsResource; Version ilpVersion; Dictionary methodInfos = new Dictionary(); - List delegateTypes = new List(); + List delegateTypes = new List(); int startOffset; byte[] decryptionKey; int decryptionKeyMod; @@ -60,7 +60,7 @@ namespace de4dot.code.deobfuscators.ILProtector { get { return methodsResource; } } - public IEnumerable DelegateTypes { + public IEnumerable DelegateTypes { get { return delegateTypes; } } @@ -210,7 +210,7 @@ namespace de4dot.code.deobfuscators.ILProtector { if (restoreMethod(method)) { Log.v("Restored method {0} ({1:X8}). Instrs:{2}, Locals:{3}, Exceptions:{4}", Utils.removeNewlines(method.FullName), - method.MetadataToken.ToInt32(), + method.MDToken.ToInt32(), method.Body.Instructions.Count, method.Body.Variables.Count, method.Body.ExceptionHandlers.Count); @@ -223,7 +223,7 @@ namespace de4dot.code.deobfuscators.ILProtector { } const int INVALID_METHOD_ID = -1; - bool restoreMethod(MethodDefinition method) { + bool restoreMethod(MethodDef method) { int methodId = getMethodId(method); if (methodId == INVALID_METHOD_ID) return false; @@ -240,13 +240,13 @@ namespace de4dot.code.deobfuscators.ILProtector { return true; } - static void restoreMethod(MethodDefinition method, MethodReader methodReader) { + static void restoreMethod(MethodDef method, MethodReader methodReader) { // body.MaxStackSize = method.Body.InitLocals = methodReader.InitLocals; methodReader.restoreMethod(method); } - int getMethodId(MethodDefinition method) { + int getMethodId(MethodDef method) { if (method == null || method.Body == null) return INVALID_METHOD_ID; @@ -260,7 +260,7 @@ namespace de4dot.code.deobfuscators.ILProtector { if (!DotNetUtils.isLdcI4(ldci4)) continue; - var field = ldsfld.Operand as FieldDefinition; + var field = ldsfld.Operand as FieldDef; if (field == null || field != mainType.InvokerInstanceField) continue; diff --git a/de4dot.code/deobfuscators/MPRESS/Deobfuscator.cs b/de4dot.code/deobfuscators/MPRESS/Deobfuscator.cs index 3dd063b5..5b74c37f 100644 --- a/de4dot.code/deobfuscators/MPRESS/Deobfuscator.cs +++ b/de4dot.code/deobfuscators/MPRESS/Deobfuscator.cs @@ -21,7 +21,7 @@ using System; using System.Collections.Generic; using System.Security.Cryptography; using System.Text; -using Mono.Cecil; +using dot10.DotNet; using Mono.MyStuff; using de4dot.PE; using de4dot.blocks; @@ -168,8 +168,8 @@ namespace de4dot.code.deobfuscators.MPRESS { return Version.Unknown; } - static bool checkMethods(TypeDefinition type, MethodInfo[] requiredMethods) { - var methods = new List(type.Methods); + static bool checkMethods(TypeDef type, MethodInfo[] requiredMethods) { + var methods = new List(type.Methods); foreach (var info in requiredMethods) { if (!checkMethod(methods, info)) return false; @@ -177,7 +177,7 @@ namespace de4dot.code.deobfuscators.MPRESS { return methods.Count == 0; } - static bool checkMethod(List methods, MethodInfo info) { + static bool checkMethod(List methods, MethodInfo info) { foreach (var method in methods) { if (info.name != null && info.name != method.Name) continue; diff --git a/de4dot.code/deobfuscators/MaxtoCode/Deobfuscator.cs b/de4dot.code/deobfuscators/MaxtoCode/Deobfuscator.cs index b805bd6a..8b4c7be2 100644 --- a/de4dot.code/deobfuscators/MaxtoCode/Deobfuscator.cs +++ b/de4dot.code/deobfuscators/MaxtoCode/Deobfuscator.cs @@ -20,7 +20,7 @@ using System; using System.Collections.Generic; using System.Text; -using Mono.Cecil; +using dot10.DotNet; using Mono.MyStuff; namespace de4dot.code.deobfuscators.MaxtoCode { @@ -215,7 +215,7 @@ namespace de4dot.code.deobfuscators.MaxtoCode { public override IEnumerable getStringDecrypterMethods() { var list = new List(); if (stringDecrypter != null && stringDecrypter.Detected) - list.Add(stringDecrypter.Method.MetadataToken.ToInt32()); + list.Add(stringDecrypter.Method.MDToken.ToInt32()); return list; } } diff --git a/de4dot.code/deobfuscators/MaxtoCode/MainType.cs b/de4dot.code/deobfuscators/MaxtoCode/MainType.cs index 8326503f..20466d55 100644 --- a/de4dot.code/deobfuscators/MaxtoCode/MainType.cs +++ b/de4dot.code/deobfuscators/MaxtoCode/MainType.cs @@ -19,13 +19,13 @@ using System; using System.Collections.Generic; -using Mono.Cecil; +using dot10.DotNet; using de4dot.blocks; namespace de4dot.code.deobfuscators.MaxtoCode { class MainType { ModuleDefinition module; - TypeDefinition mcType; + TypeDef mcType; ModuleReference mcModule1, mcModule2; bool isOld; @@ -33,7 +33,7 @@ namespace de4dot.code.deobfuscators.MaxtoCode { get { return isOld; } } - public TypeDefinition Type { + public TypeDef Type { get { return mcType; } } @@ -48,9 +48,9 @@ namespace de4dot.code.deobfuscators.MaxtoCode { } } - public IEnumerable InitMethods { + public IEnumerable InitMethods { get { - var list = new List(); + var list = new List(); if (mcType == null) return list; foreach (var method in mcType.Methods) { @@ -87,7 +87,7 @@ namespace de4dot.code.deobfuscators.MaxtoCode { } } - bool checkCctor(MethodDefinition cctor) { + bool checkCctor(MethodDef cctor) { foreach (var method in DotNetUtils.getCalledMethods(module, cctor)) { if (method.Name != "Startup") continue; @@ -109,7 +109,7 @@ namespace de4dot.code.deobfuscators.MaxtoCode { return false; } - static bool checkType(TypeDefinition type, out ModuleReference module1, out ModuleReference module2, out bool isOld) { + static bool checkType(TypeDef type, out ModuleReference module1, out ModuleReference module2, out bool isOld) { module1 = module2 = null; isOld = false; @@ -131,22 +131,22 @@ namespace de4dot.code.deobfuscators.MaxtoCode { return true; } - static Dictionary> getPinvokes(TypeDefinition type) { - var pinvokes = new Dictionary>(StringComparer.Ordinal); + static Dictionary> getPinvokes(TypeDef type) { + var pinvokes = new Dictionary>(StringComparer.Ordinal); foreach (var method in type.Methods) { var info = method.PInvokeInfo; if (info == null || info.EntryPoint == null) continue; - List list; + List list; if (!pinvokes.TryGetValue(info.EntryPoint, out list)) - pinvokes[info.EntryPoint] = list = new List(); + pinvokes[info.EntryPoint] = list = new List(); list.Add(method); } return pinvokes; } - static List getPinvokeList(Dictionary> pinvokes, string methodName) { - List list; + static List getPinvokeList(Dictionary> pinvokes, string methodName) { + List list; if (!pinvokes.TryGetValue(methodName, out list)) return null; if (list.Count != 2) diff --git a/de4dot.code/deobfuscators/MaxtoCode/StringDecrypter.cs b/de4dot.code/deobfuscators/MaxtoCode/StringDecrypter.cs index 939d4cc6..512232ad 100644 --- a/de4dot.code/deobfuscators/MaxtoCode/StringDecrypter.cs +++ b/de4dot.code/deobfuscators/MaxtoCode/StringDecrypter.cs @@ -20,17 +20,17 @@ using System; using System.Collections.Generic; using System.Text; -using Mono.Cecil; +using dot10.DotNet; using de4dot.blocks; namespace de4dot.code.deobfuscators.MaxtoCode { class StringDecrypter { DecrypterInfo decrypterInfo; - MethodDefinition decryptMethod; + MethodDef decryptMethod; string[] decryptedStrings; Encoding encoding; - public MethodDefinition Method { + public MethodDef Method { get { return decryptMethod; } } @@ -50,7 +50,7 @@ namespace de4dot.code.deobfuscators.MaxtoCode { return; } - static MethodDefinition findDecryptMethod(TypeDefinition type) { + static MethodDef findDecryptMethod(TypeDef type) { if (type == null) return null; foreach (var method in type.Methods) { diff --git a/de4dot.code/deobfuscators/Rummage/Deobfuscator.cs b/de4dot.code/deobfuscators/Rummage/Deobfuscator.cs index f99473bb..19dfe1fe 100644 --- a/de4dot.code/deobfuscators/Rummage/Deobfuscator.cs +++ b/de4dot.code/deobfuscators/Rummage/Deobfuscator.cs @@ -18,7 +18,7 @@ */ using System.Collections.Generic; -using Mono.Cecil; +using dot10.DotNet; using de4dot.blocks; namespace de4dot.code.deobfuscators.Rummage { diff --git a/de4dot.code/deobfuscators/Rummage/StringDecrypter.cs b/de4dot.code/deobfuscators/Rummage/StringDecrypter.cs index fc390be8..c5d87a7b 100644 --- a/de4dot.code/deobfuscators/Rummage/StringDecrypter.cs +++ b/de4dot.code/deobfuscators/Rummage/StringDecrypter.cs @@ -21,25 +21,25 @@ using System; using System.Collections.Generic; using System.IO; using System.Text; -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using de4dot.blocks; namespace de4dot.code.deobfuscators.Rummage { class StringDecrypter { ModuleDefinition module; - MethodDefinition stringDecrypterMethod; + MethodDef stringDecrypterMethod; FieldDefinitionAndDeclaringTypeDict stringInfos = new FieldDefinitionAndDeclaringTypeDict(); int fileDispl; uint[] key; BinaryReader reader; class StringInfo { - public readonly FieldDefinition field; + public readonly FieldDef field; public readonly int stringId; public string decrypted; - public StringInfo(FieldDefinition field, int stringId) { + public StringInfo(FieldDef field, int stringId) { this.field = field; this.stringId = stringId; } @@ -51,13 +51,13 @@ namespace de4dot.code.deobfuscators.Rummage { } } - public TypeDefinition Type { + public TypeDef Type { get { return stringDecrypterMethod != null ? stringDecrypterMethod.DeclaringType : null; } } - public IEnumerable OtherTypes { + public IEnumerable OtherTypes { get { - var list = new List(stringInfos.Count); + var list = new List(stringInfos.Count); foreach (var info in stringInfos.getValues()) list.Add(info.field.DeclaringType); return list; @@ -93,7 +93,7 @@ namespace de4dot.code.deobfuscators.Rummage { "System.Int32", "System.IO.FileStream", }; - static MethodDefinition checkType(TypeDefinition type) { + static MethodDef checkType(TypeDef type) { if (!new FieldTypes(type).exactly(requiredFields)) return null; var cctor = DotNetUtils.getMethod(type, ".cctor"); @@ -105,8 +105,8 @@ namespace de4dot.code.deobfuscators.Rummage { return checkMethods(type); } - static MethodDefinition checkMethods(TypeDefinition type) { - MethodDefinition cctor = null, decrypterMethod = null; + static MethodDef checkMethods(TypeDef type) { + MethodDef cctor = null, decrypterMethod = null; foreach (var method in type.Methods) { if (!method.IsStatic || method.Body == null) return null; @@ -123,7 +123,7 @@ namespace de4dot.code.deobfuscators.Rummage { return decrypterMethod; } - static bool getDispl(MethodDefinition method, ref int displ) { + static bool getDispl(MethodDef method, ref int displ) { var instrs = method.Body.Instructions; for (int i = 0; i < instrs.Count - 2; i++) { var mul = instrs[i]; @@ -160,7 +160,7 @@ namespace de4dot.code.deobfuscators.Rummage { key[i] = reader.ReadUInt32(); } - void initType(TypeDefinition type) { + void initType(TypeDef type) { var cctor = DotNetUtils.getMethod(type, ".cctor"); if (cctor == null) return; @@ -171,7 +171,7 @@ namespace de4dot.code.deobfuscators.Rummage { stringInfos.add(info.field, info); } - StringInfo getStringInfo(MethodDefinition method) { + StringInfo getStringInfo(MethodDef method) { if (method == null || method.Body == null) return null; var instrs = method.Body.Instructions; @@ -191,7 +191,7 @@ namespace de4dot.code.deobfuscators.Rummage { var stsfld = instrs[i + 2]; if (stsfld.OpCode.Code != Code.Stsfld) continue; - var field = stsfld.Operand as FieldDefinition; + var field = stsfld.Operand as FieldDef; if (field == null) continue; diff --git a/de4dot.code/deobfuscators/Skater_NET/Deobfuscator.cs b/de4dot.code/deobfuscators/Skater_NET/Deobfuscator.cs index 78aceea6..80759bad 100644 --- a/de4dot.code/deobfuscators/Skater_NET/Deobfuscator.cs +++ b/de4dot.code/deobfuscators/Skater_NET/Deobfuscator.cs @@ -18,7 +18,7 @@ */ using System.Collections.Generic; -using Mono.Cecil; +using dot10.DotNet; using de4dot.blocks; namespace de4dot.code.deobfuscators.Skater_NET { diff --git a/de4dot.code/deobfuscators/Skater_NET/EnumClassFinder.cs b/de4dot.code/deobfuscators/Skater_NET/EnumClassFinder.cs index cc4795e9..0e98e01a 100644 --- a/de4dot.code/deobfuscators/Skater_NET/EnumClassFinder.cs +++ b/de4dot.code/deobfuscators/Skater_NET/EnumClassFinder.cs @@ -17,14 +17,14 @@ along with de4dot. If not, see . */ -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using de4dot.blocks; namespace de4dot.code.deobfuscators.Skater_NET { class EnumClassFinder { ModuleDefinition module; - FieldDefinition enumField; + FieldDef enumField; public EnumClassFinder(ModuleDefinition module) { this.module = module; diff --git a/de4dot.code/deobfuscators/Skater_NET/StringDecrypter.cs b/de4dot.code/deobfuscators/Skater_NET/StringDecrypter.cs index 21a392b7..e004bffd 100644 --- a/de4dot.code/deobfuscators/Skater_NET/StringDecrypter.cs +++ b/de4dot.code/deobfuscators/Skater_NET/StringDecrypter.cs @@ -23,15 +23,15 @@ using System.Globalization; using System.Security.Cryptography; using System.Text; using System.Text.RegularExpressions; -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using de4dot.blocks; namespace de4dot.code.deobfuscators.Skater_NET { class StringDecrypter { ModuleDefinition module; - TypeDefinition decrypterType; - MethodDefinition decrypterCctor; + TypeDef decrypterType; + MethodDef decrypterCctor; FieldDefinitionAndDeclaringTypeDict fieldToDecryptedString = new FieldDefinitionAndDeclaringTypeDict(); bool canRemoveType; IDecrypter decrypter; @@ -84,7 +84,7 @@ namespace de4dot.code.deobfuscators.Skater_NET { get { return canRemoveType; } } - public TypeDefinition Type { + public TypeDef Type { get { return decrypterType; } } @@ -131,7 +131,7 @@ namespace de4dot.code.deobfuscators.Skater_NET { continue; if (instrs[i + 4].OpCode.Code != Code.Stsfld) continue; - var field = instrs[i + 4].Operand as FieldDefinition; + var field = instrs[i + 4].Operand as FieldDef; if (field == null) continue; if (!MemberReferenceHelper.compareTypes(field.DeclaringType, decrypterType)) @@ -141,7 +141,7 @@ namespace de4dot.code.deobfuscators.Skater_NET { } } - bool checkType(TypeDefinition type) { + bool checkType(TypeDef type) { foreach (var method in type.Methods) { if (!method.IsStatic || method.Body == null) continue; @@ -157,7 +157,7 @@ namespace de4dot.code.deobfuscators.Skater_NET { return false; } - bool checkMethodV1(MethodDefinition method) { + bool checkMethodV1(MethodDef method) { var salt = getSalt(method); if (salt == null) return false; @@ -181,7 +181,7 @@ namespace de4dot.code.deobfuscators.Skater_NET { "System.String System.String::Concat(System.String,System.String)", "System.Char Microsoft.VisualBasic.Strings::Chr(System.Int32)", }; - bool checkMethodV2(MethodDefinition method) { + bool checkMethodV2(MethodDef method) { if (!DeobUtils.hasInteger(method, ' ')) return false; foreach (var calledMethodName in callsMethodsV2) { @@ -193,7 +193,7 @@ namespace de4dot.code.deobfuscators.Skater_NET { return true; } - static byte[] getSalt(MethodDefinition method) { + static byte[] getSalt(MethodDef method) { foreach (var s in DotNetUtils.getCodeStrings(method)) { var saltAry = fixSalt(s); if (saltAry != null) @@ -224,7 +224,7 @@ namespace de4dot.code.deobfuscators.Skater_NET { return saltAry; } - string getPassword(MethodDefinition decryptMethod) { + string getPassword(MethodDef decryptMethod) { foreach (var method in DotNetUtils.getCalledMethods(module, decryptMethod)) { if (!method.IsStatic || method.Body == null) continue; @@ -258,7 +258,7 @@ namespace de4dot.code.deobfuscators.Skater_NET { return password; } - string getPassword2(MethodDefinition method) { + string getPassword2(MethodDef method) { string password = ""; foreach (var calledMethod in DotNetUtils.getCalledMethods(module, method)) { var s = getPassword3(calledMethod); @@ -270,7 +270,7 @@ namespace de4dot.code.deobfuscators.Skater_NET { return password; } - string getPassword3(MethodDefinition method) { + string getPassword3(MethodDef method) { var strings = new List(DotNetUtils.getCodeStrings(method)); if (strings.Count != 1) return null; diff --git a/de4dot.code/deobfuscators/SmartAssembly/AssemblyResolver.cs b/de4dot.code/deobfuscators/SmartAssembly/AssemblyResolver.cs index a586d829..21faef73 100644 --- a/de4dot.code/deobfuscators/SmartAssembly/AssemblyResolver.cs +++ b/de4dot.code/deobfuscators/SmartAssembly/AssemblyResolver.cs @@ -19,7 +19,7 @@ using System; using System.Collections.Generic; -using Mono.Cecil; +using dot10.DotNet; namespace de4dot.code.deobfuscators.SmartAssembly { class AssemblyResolver { diff --git a/de4dot.code/deobfuscators/SmartAssembly/AssemblyResolverInfo.cs b/de4dot.code/deobfuscators/SmartAssembly/AssemblyResolverInfo.cs index f3bb93a1..23d2e9b5 100644 --- a/de4dot.code/deobfuscators/SmartAssembly/AssemblyResolverInfo.cs +++ b/de4dot.code/deobfuscators/SmartAssembly/AssemblyResolverInfo.cs @@ -20,8 +20,8 @@ using System; using System.Collections.Generic; using System.Text; -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using de4dot.blocks; namespace de4dot.code.deobfuscators.SmartAssembly { @@ -71,10 +71,10 @@ namespace de4dot.code.deobfuscators.SmartAssembly { } class AssemblyResolverInfo : ResolverInfoBase { - MethodDefinition simpleZipTypeMethod; + MethodDef simpleZipTypeMethod; List embeddedAssemblyInfos = new List(); - public MethodDefinition SimpleZipTypeMethod { + public MethodDef SimpleZipTypeMethod { get { return simpleZipTypeMethod; } } @@ -100,7 +100,7 @@ namespace de4dot.code.deobfuscators.SmartAssembly { return ok; } - protected override bool checkResolverType(TypeDefinition type) { + protected override bool checkResolverType(TypeDef type) { if (DotNetUtils.findFieldType(type, "System.Collections.Hashtable", true) != null) return true; @@ -114,7 +114,7 @@ namespace de4dot.code.deobfuscators.SmartAssembly { return true; } - protected override bool checkHandlerMethod(MethodDefinition method) { + protected override bool checkHandlerMethod(MethodDef method) { if (!method.IsStatic || !method.HasBody) return false; @@ -154,7 +154,7 @@ namespace de4dot.code.deobfuscators.SmartAssembly { return true; } - void findSimpleZipType(MethodDefinition method) { + void findSimpleZipType(MethodDef method) { if (method == null || !method.HasBody) return; foreach (var call in method.Body.Instructions) { diff --git a/de4dot.code/deobfuscators/SmartAssembly/AutomatedErrorReportingFinder.cs b/de4dot.code/deobfuscators/SmartAssembly/AutomatedErrorReportingFinder.cs index 3f01b5b4..5ee2340f 100644 --- a/de4dot.code/deobfuscators/SmartAssembly/AutomatedErrorReportingFinder.cs +++ b/de4dot.code/deobfuscators/SmartAssembly/AutomatedErrorReportingFinder.cs @@ -19,8 +19,8 @@ using System; using System.Collections.Generic; -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using de4dot.blocks; namespace de4dot.code.deobfuscators.SmartAssembly { @@ -45,12 +45,12 @@ namespace de4dot.code.deobfuscators.SmartAssembly { if (entryPoint == null) enabled = true; else { - MethodDefinition exceptionMethod; + MethodDef exceptionMethod; enabled = checkMethod(entryPoint, out exceptionMethod); } } - bool checkMethod(MethodDefinition method, out MethodDefinition exceptionMethod) { + bool checkMethod(MethodDef method, out MethodDef exceptionMethod) { exceptionMethod = null; var body = method.Body; diff --git a/de4dot.code/deobfuscators/SmartAssembly/Deobfuscator.cs b/de4dot.code/deobfuscators/SmartAssembly/Deobfuscator.cs index db13f8a1..304f0fbe 100644 --- a/de4dot.code/deobfuscators/SmartAssembly/Deobfuscator.cs +++ b/de4dot.code/deobfuscators/SmartAssembly/Deobfuscator.cs @@ -21,8 +21,8 @@ using System; using System.Collections.Generic; using System.IO; using System.Text; -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using de4dot.blocks; // SmartAssembly can add so much junk that it's very difficult to find and remove all of it. @@ -159,7 +159,7 @@ namespace de4dot.code.deobfuscators.SmartAssembly { } } - void initializeVersion(TypeDefinition attr) { + void initializeVersion(TypeDef attr) { var s = DotNetUtils.getCustomArgAsString(getAssemblyAttribute(attr), 0); if (s == null) return; @@ -230,8 +230,8 @@ namespace de4dot.code.deobfuscators.SmartAssembly { return -1; } - TypeDefinition getTypeIdAttribute() { - Dictionary attrs = null; + TypeDef getTypeIdAttribute() { + Dictionary attrs = null; int counter = 0; foreach (var type in module.GetTypes()) { counter++; @@ -239,11 +239,11 @@ namespace de4dot.code.deobfuscators.SmartAssembly { if (cattrs.Count == 0) return null; - var attrs2 = new Dictionary(); + var attrs2 = new Dictionary(); foreach (var cattr in cattrs) { if (!DotNetUtils.isMethod(cattr.Constructor, "System.Void", "(System.Int32)")) continue; - var attrType = cattr.AttributeType as TypeDefinition; + var attrType = cattr.AttributeType as TypeDef; if (attrType == null) continue; if (attrs != null && !attrs.ContainsKey(attrType)) @@ -352,7 +352,7 @@ namespace de4dot.code.deobfuscators.SmartAssembly { return true; } - MethodDefinition getGlobalSimpleZipTypeMethod() { + MethodDef getGlobalSimpleZipTypeMethod() { if (assemblyResolverInfo.SimpleZipTypeMethod != null) return assemblyResolverInfo.SimpleZipTypeMethod; foreach (var info in stringDecrypterInfos) { @@ -409,7 +409,7 @@ namespace de4dot.code.deobfuscators.SmartAssembly { foreach (var info in stringDecrypterInfos) { if (initd.ContainsKey(info)) continue; - Log.v("String decrypter not initialized. Token {0:X8}", info.StringsEncodingClass.MetadataToken.ToInt32()); + Log.v("String decrypter not initialized. Token {0:X8}", info.StringsEncodingClass.MDToken.ToInt32()); } } @@ -419,7 +419,7 @@ namespace de4dot.code.deobfuscators.SmartAssembly { if (decrypter.CanDecrypt) { staticStringInliner.add(DotNetUtils.getMethod(info.GetStringDelegate, "Invoke"), (method, gim, args) => { var fieldDefinition = DotNetUtils.getField(module, (FieldReference)args[0]); - return decrypter.decrypt(fieldDefinition.MetadataToken.ToInt32(), (int)args[1]); + return decrypter.decrypt(fieldDefinition.MDToken.ToInt32(), (int)args[1]); }); staticStringInliner.add(info.StringDecrypterMethod, (method, gim, args) => { return decrypter.decrypt(0, (int)args[0]); @@ -448,11 +448,11 @@ namespace de4dot.code.deobfuscators.SmartAssembly { base.deobfuscateEnd(); } - TypeDefinition findBigType() { + TypeDef findBigType() { if (approxVersion <= new Version(6, 5, 3, 53)) return null; - TypeDefinition bigType = null; + TypeDef bigType = null; foreach (var type in module.Types) { if (isBigType(type)) { if (bigType == null || type.Methods.Count > bigType.Methods.Count) @@ -462,7 +462,7 @@ namespace de4dot.code.deobfuscators.SmartAssembly { return bigType; } - bool isBigType(TypeDefinition type) { + bool isBigType(TypeDef type) { if (type.Methods.Count < 50) return false; if (type.HasProperties || type.HasEvents) @@ -541,7 +541,7 @@ namespace de4dot.code.deobfuscators.SmartAssembly { public override IEnumerable getStringDecrypterMethods() { var list = new List(); foreach (var method in staticStringInliner.Methods) - list.Add(method.MetadataToken.ToInt32()); + list.Add(method.MDToken.ToInt32()); return list; } } diff --git a/de4dot.code/deobfuscators/SmartAssembly/MemoryManagerInfo.cs b/de4dot.code/deobfuscators/SmartAssembly/MemoryManagerInfo.cs index b6844134..a6b183de 100644 --- a/de4dot.code/deobfuscators/SmartAssembly/MemoryManagerInfo.cs +++ b/de4dot.code/deobfuscators/SmartAssembly/MemoryManagerInfo.cs @@ -17,24 +17,24 @@ along with de4dot. If not, see . */ -using Mono.Cecil; +using dot10.DotNet; using de4dot.blocks; namespace de4dot.code.deobfuscators.SmartAssembly { class MemoryManagerInfo { ModuleDefinition module; - TypeDefinition memoryManagerType; - MethodDefinition attachAppMethod; + TypeDef memoryManagerType; + MethodDef attachAppMethod; public bool Detected { get { return memoryManagerType != null; } } - public TypeDefinition Type { + public TypeDef Type { get { return memoryManagerType; } } - public MethodDefinition CctorInitMethod { + public MethodDef CctorInitMethod { get { return attachAppMethod; } } @@ -50,7 +50,7 @@ namespace de4dot.code.deobfuscators.SmartAssembly { return false; } - bool checkCalledMethods(MethodDefinition checkMethod) { + bool checkCalledMethods(MethodDef checkMethod) { if (checkMethod == null) return false; foreach (var method in DotNetUtils.getCalledMethods(module, checkMethod)) { @@ -68,7 +68,7 @@ namespace de4dot.code.deobfuscators.SmartAssembly { return false; } - bool checkMemoryManagerType(TypeDefinition type, MethodDefinition method) { + bool checkMemoryManagerType(TypeDef type, MethodDef method) { // Only two fields: itself and a long int fields = 0; foreach (var field in type.Fields) { diff --git a/de4dot.code/deobfuscators/SmartAssembly/ProxyCallFixer.cs b/de4dot.code/deobfuscators/SmartAssembly/ProxyCallFixer.cs index 83bb63c4..9b37fe90 100644 --- a/de4dot.code/deobfuscators/SmartAssembly/ProxyCallFixer.cs +++ b/de4dot.code/deobfuscators/SmartAssembly/ProxyCallFixer.cs @@ -19,8 +19,8 @@ using System; using System.Collections.Generic; -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using de4dot.blocks; namespace de4dot.code.deobfuscators.SmartAssembly { @@ -51,7 +51,7 @@ namespace de4dot.code.deobfuscators.SmartAssembly { this.simpleDeobfuscator = simpleDeobfuscator; } - protected override object checkCctor(ref TypeDefinition type, MethodDefinition cctor) { + protected override object checkCctor(ref TypeDef type, MethodDef cctor) { var instrs = cctor.Body.Instructions; if (instrs.Count > 10) return null; @@ -61,13 +61,13 @@ namespace de4dot.code.deobfuscators.SmartAssembly { return null; if (!DotNetUtils.isLdcI4(instrs[0].OpCode.Code)) return null; - if (instrs[1].OpCode != OpCodes.Call || !isDelegateCreatorMethod(instrs[1].Operand as MethodDefinition)) + if (instrs[1].OpCode != OpCodes.Call || !isDelegateCreatorMethod(instrs[1].Operand as MethodDef)) return null; if (instrs[2].OpCode != OpCodes.Ret) return null; int delegateToken = 0x02000001 + DotNetUtils.getLdcI4Value(instrs[0]); - if (type.MetadataToken.ToInt32() != delegateToken) { + if (type.MDToken.ToInt32() != delegateToken) { Log.w("Delegate token is not current type"); return null; } @@ -75,7 +75,7 @@ namespace de4dot.code.deobfuscators.SmartAssembly { return new object(); } - protected override void getCallInfo(object context, FieldDefinition field, out MethodReference calledMethod, out OpCode callOpcode) { + protected override void getCallInfo(object context, FieldDef field, out MethodReference calledMethod, out OpCode callOpcode) { callOpcode = OpCodes.Call; string name = field.Name; @@ -93,7 +93,7 @@ namespace de4dot.code.deobfuscators.SmartAssembly { } if (methodIndex >= memberReferences.Count) { - Log.w("Ignoring invalid methodIndex: {0:X8}, field: {1:X8}", methodIndex, field.MetadataToken.ToInt32()); + Log.w("Ignoring invalid methodIndex: {0:X8}, field: {1:X8}", methodIndex, field.MDToken.ToInt32()); calledMethod = null; return; } diff --git a/de4dot.code/deobfuscators/SmartAssembly/ResolverInfoBase.cs b/de4dot.code/deobfuscators/SmartAssembly/ResolverInfoBase.cs index 9e693da5..158c26ac 100644 --- a/de4dot.code/deobfuscators/SmartAssembly/ResolverInfoBase.cs +++ b/de4dot.code/deobfuscators/SmartAssembly/ResolverInfoBase.cs @@ -18,8 +18,8 @@ */ using System.Collections.Generic; -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using de4dot.blocks; namespace de4dot.code.deobfuscators.SmartAssembly { @@ -27,14 +27,14 @@ namespace de4dot.code.deobfuscators.SmartAssembly { protected ModuleDefinition module; ISimpleDeobfuscator simpleDeobfuscator; IDeobfuscator deob; - TypeDefinition resolverType; - MethodDefinition callResolverMethod; + TypeDef resolverType; + MethodDef callResolverMethod; - public TypeDefinition Type { + public TypeDef Type { get { return resolverType; } } - public TypeDefinition CallResolverType { + public TypeDef CallResolverType { get { if (callResolverMethod == null) return null; @@ -44,7 +44,7 @@ namespace de4dot.code.deobfuscators.SmartAssembly { } } - public MethodDefinition CallResolverMethod { + public MethodDef CallResolverMethod { get { return callResolverMethod; } } @@ -66,7 +66,7 @@ namespace de4dot.code.deobfuscators.SmartAssembly { return false; } - bool findTypes(MethodDefinition initMethod) { + bool findTypes(MethodDef initMethod) { if (initMethod == null) return false; foreach (var method in DotNetUtils.getCalledMethods(module, initMethod)) { @@ -81,7 +81,7 @@ namespace de4dot.code.deobfuscators.SmartAssembly { return false; } - bool checkAttachAppMethod(MethodDefinition attachAppMethod) { + bool checkAttachAppMethod(MethodDef attachAppMethod) { callResolverMethod = null; if (!attachAppMethod.HasBody) return false; @@ -120,7 +120,7 @@ namespace de4dot.code.deobfuscators.SmartAssembly { return false; } - static bool hasLdftn(MethodDefinition method) { + static bool hasLdftn(MethodDef method) { if (method == null || method.Body == null) return false; foreach (var instr in method.Body.Instructions) { @@ -130,7 +130,7 @@ namespace de4dot.code.deobfuscators.SmartAssembly { return false; } - bool checkResolverInitMethod(MethodDefinition initMethod) { + bool checkResolverInitMethod(MethodDef initMethod) { resolverType = null; if (!initMethod.HasBody) return false; @@ -152,12 +152,12 @@ namespace de4dot.code.deobfuscators.SmartAssembly { return false; } - void deobfuscate(MethodDefinition method) { + void deobfuscate(MethodDef method) { simpleDeobfuscator.deobfuscate(method); simpleDeobfuscator.decryptStrings(method, deob); } - TypeDefinition getResolverType(MethodDefinition resolveHandler) { + TypeDef getResolverType(MethodDef resolveHandler) { if (resolveHandler.Body == null) return null; foreach (var instr in resolveHandler.Body.Instructions) { @@ -178,10 +178,10 @@ namespace de4dot.code.deobfuscators.SmartAssembly { return null; } - protected abstract bool checkResolverType(TypeDefinition type); - protected abstract bool checkHandlerMethod(MethodDefinition handler); + protected abstract bool checkResolverType(TypeDef type); + protected abstract bool checkHandlerMethod(MethodDef handler); - IEnumerable getResolverHandlers(MethodDefinition method) { + IEnumerable getResolverHandlers(MethodDef method) { int numHandlers = 0; var instructions = method.Body.Instructions; for (int i = 0; i < instructions.Count; i++) { @@ -215,7 +215,7 @@ namespace de4dot.code.deobfuscators.SmartAssembly { yield return method; } - static bool hasOnlyThisMethod(TypeDefinition type, MethodDefinition method) { + static bool hasOnlyThisMethod(TypeDef type, MethodDef method) { if (type == null || method == null) return false; foreach (var m in type.Methods) { diff --git a/de4dot.code/deobfuscators/SmartAssembly/ResourceDecrypter.cs b/de4dot.code/deobfuscators/SmartAssembly/ResourceDecrypter.cs index 4f1ed6f0..138e550a 100644 --- a/de4dot.code/deobfuscators/SmartAssembly/ResourceDecrypter.cs +++ b/de4dot.code/deobfuscators/SmartAssembly/ResourceDecrypter.cs @@ -20,7 +20,7 @@ using System; using System.IO; using System.Security.Cryptography; -using Mono.Cecil; +using dot10.DotNet; using ICSharpCode.SharpZipLib.Zip.Compression; namespace de4dot.code.deobfuscators.SmartAssembly { diff --git a/de4dot.code/deobfuscators/SmartAssembly/ResourceDecrypterInfo.cs b/de4dot.code/deobfuscators/SmartAssembly/ResourceDecrypterInfo.cs index 07d66909..3052fffa 100644 --- a/de4dot.code/deobfuscators/SmartAssembly/ResourceDecrypterInfo.cs +++ b/de4dot.code/deobfuscators/SmartAssembly/ResourceDecrypterInfo.cs @@ -18,14 +18,14 @@ */ using System.Collections.Generic; -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using de4dot.blocks; namespace de4dot.code.deobfuscators.SmartAssembly { class ResourceDecrypterInfo { ModuleDefinition module; - MethodDefinition simpleZipTypeDecryptMethod; + MethodDef simpleZipTypeDecryptMethod; public byte[] DES_Key { get; private set; } public byte[] DES_IV { get; private set; } @@ -40,19 +40,19 @@ namespace de4dot.code.deobfuscators.SmartAssembly { this.module = module; } - public ResourceDecrypterInfo(ModuleDefinition module, MethodDefinition simpleZipTypeDecryptMethod, ISimpleDeobfuscator simpleDeobfuscator) + public ResourceDecrypterInfo(ModuleDefinition module, MethodDef simpleZipTypeDecryptMethod, ISimpleDeobfuscator simpleDeobfuscator) : this(module) { setSimpleZipType(simpleZipTypeDecryptMethod, simpleDeobfuscator); } - public void setSimpleZipType(MethodDefinition method, ISimpleDeobfuscator simpleDeobfuscator) { + public void setSimpleZipType(MethodDef method, ISimpleDeobfuscator simpleDeobfuscator) { if (simpleZipTypeDecryptMethod != null || method == null) return; simpleZipTypeDecryptMethod = method; init(simpleDeobfuscator, method); } - void init(ISimpleDeobfuscator simpleDeobfuscator, MethodDefinition method) { + void init(ISimpleDeobfuscator simpleDeobfuscator, MethodDef method) { var desList = new List(2); var aesList = new List(2); diff --git a/de4dot.code/deobfuscators/SmartAssembly/ResourceResolver.cs b/de4dot.code/deobfuscators/SmartAssembly/ResourceResolver.cs index 8266f24d..a88a07ff 100644 --- a/de4dot.code/deobfuscators/SmartAssembly/ResourceResolver.cs +++ b/de4dot.code/deobfuscators/SmartAssembly/ResourceResolver.cs @@ -19,7 +19,7 @@ using System; using System.IO; -using Mono.Cecil; +using dot10.DotNet; using de4dot.blocks; namespace de4dot.code.deobfuscators.SmartAssembly { diff --git a/de4dot.code/deobfuscators/SmartAssembly/ResourceResolverInfo.cs b/de4dot.code/deobfuscators/SmartAssembly/ResourceResolverInfo.cs index 1a4c8ad0..01003e2f 100644 --- a/de4dot.code/deobfuscators/SmartAssembly/ResourceResolverInfo.cs +++ b/de4dot.code/deobfuscators/SmartAssembly/ResourceResolverInfo.cs @@ -17,8 +17,8 @@ along with de4dot. If not, see . */ -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using de4dot.blocks; namespace de4dot.code.deobfuscators.SmartAssembly { @@ -35,11 +35,11 @@ namespace de4dot.code.deobfuscators.SmartAssembly { this.assemblyResolverInfo = assemblyResolverInfo; } - protected override bool checkResolverType(TypeDefinition type) { + protected override bool checkResolverType(TypeDef type) { return DotNetUtils.findFieldType(type, "System.Reflection.Assembly", true) != null; } - protected override bool checkHandlerMethod(MethodDefinition method) { + protected override bool checkHandlerMethod(MethodDef method) { if (!method.IsStatic || !method.HasBody) return false; diff --git a/de4dot.code/deobfuscators/SmartAssembly/SimpleZipInfo.cs b/de4dot.code/deobfuscators/SmartAssembly/SimpleZipInfo.cs index 1a6cf445..6af4a1cf 100644 --- a/de4dot.code/deobfuscators/SmartAssembly/SimpleZipInfo.cs +++ b/de4dot.code/deobfuscators/SmartAssembly/SimpleZipInfo.cs @@ -17,13 +17,13 @@ along with de4dot. If not, see . */ -using Mono.Cecil; +using dot10.DotNet; using de4dot.blocks; namespace de4dot.code.deobfuscators.SmartAssembly { class SimpleZipInfo { - public static bool isSimpleZipDecryptMethod_QuickCheck(ModuleDefinition module, MethodReference method, out MethodDefinition simpleZipTypeMethod) { + public static bool isSimpleZipDecryptMethod_QuickCheck(ModuleDefinition module, MethodReference method, out MethodDef simpleZipTypeMethod) { simpleZipTypeMethod = null; if (!DotNetUtils.isMethod(method, "System.Byte[]", "(System.Byte[])")) diff --git a/de4dot.code/deobfuscators/SmartAssembly/StringDecrypterInfo.cs b/de4dot.code/deobfuscators/SmartAssembly/StringDecrypterInfo.cs index fba9ebad..1dd05b36 100644 --- a/de4dot.code/deobfuscators/SmartAssembly/StringDecrypterInfo.cs +++ b/de4dot.code/deobfuscators/SmartAssembly/StringDecrypterInfo.cs @@ -19,8 +19,8 @@ using System; using System.Collections.Generic; -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using de4dot.blocks; namespace de4dot.code.deobfuscators.SmartAssembly { @@ -35,22 +35,22 @@ namespace de4dot.code.deobfuscators.SmartAssembly { class StringDecrypterInfo { ModuleDefinition module; ResourceDecrypter resourceDecrypter; - TypeDefinition stringsEncodingClass; + TypeDef stringsEncodingClass; EmbeddedResource stringsResource; int stringOffset; - MethodDefinition simpleZipTypeMethod; - MethodDefinition stringDecrypterMethod; + MethodDef simpleZipTypeMethod; + MethodDef stringDecrypterMethod; StringDecrypterVersion decrypterVersion; public StringDecrypterVersion DecrypterVersion { get { return decrypterVersion; } } - public TypeDefinition GetStringDelegate { get; set; } - public TypeDefinition StringsType { get; set; } - public MethodDefinition CreateStringDelegateMethod { get; set; } + public TypeDef GetStringDelegate { get; set; } + public TypeDef StringsType { get; set; } + public MethodDef CreateStringDelegateMethod { get; set; } - public TypeDefinition StringsEncodingClass { + public TypeDef StringsEncodingClass { get { return stringsEncodingClass; } } @@ -58,7 +58,7 @@ namespace de4dot.code.deobfuscators.SmartAssembly { get { return resourceDecrypter == null || resourceDecrypter.CanDecrypt; } } - public MethodDefinition SimpleZipTypeMethod { + public MethodDef SimpleZipTypeMethod { get { return simpleZipTypeMethod; } } @@ -74,11 +74,11 @@ namespace de4dot.code.deobfuscators.SmartAssembly { get { return simpleZipTypeMethod != null; } } - public MethodDefinition StringDecrypterMethod { + public MethodDef StringDecrypterMethod { get { return stringDecrypterMethod; } } - public StringDecrypterInfo(ModuleDefinition module, TypeDefinition stringsEncodingClass) { + public StringDecrypterInfo(ModuleDefinition module, TypeDef stringsEncodingClass) { this.module = module; this.stringsEncodingClass = stringsEncodingClass; } @@ -91,7 +91,7 @@ namespace de4dot.code.deobfuscators.SmartAssembly { "System.Byte[]", "System.Int32", }; - StringDecrypterVersion guessVersion(MethodDefinition cctor) { + StringDecrypterVersion guessVersion(MethodDef cctor) { var fieldTypes = new FieldTypes(stringsEncodingClass); if (fieldTypes.exactly(fields2x)) return StringDecrypterVersion.V2; @@ -116,7 +116,7 @@ namespace de4dot.code.deobfuscators.SmartAssembly { return false; if (decrypterVersion <= StringDecrypterVersion.V3) { - MethodDefinition initMethod; + MethodDef initMethod; if (decrypterVersion == StringDecrypterVersion.V3) initMethod = cctor; else if (decrypterVersion == StringDecrypterVersion.V2) @@ -136,7 +136,7 @@ namespace de4dot.code.deobfuscators.SmartAssembly { if (DeobUtils.hasInteger(initMethod, 0xFFFFFF) && DeobUtils.hasInteger(initMethod, 0xFFFF)) { - stringOffset ^= ((stringDecrypterMethod.MetadataToken.ToInt32() & 0xFFFFFF) - 1) % 0xFFFF; + stringOffset ^= ((stringDecrypterMethod.MDToken.ToInt32() & 0xFFFFFF) - 1) % 0xFFFF; } } } @@ -155,7 +155,7 @@ namespace de4dot.code.deobfuscators.SmartAssembly { return true; } - bool callsGetPublicKeyToken(MethodDefinition method) { + bool callsGetPublicKeyToken(MethodDef method) { foreach (var calledMethod in DotNetUtils.getMethodCalls(method)) { if (calledMethod.ToString() == "System.Byte[] System.Reflection.AssemblyName::GetPublicKeyToken()") return true; @@ -163,7 +163,7 @@ namespace de4dot.code.deobfuscators.SmartAssembly { return false; } - bool findStringsResource(IDeobfuscator deob, ISimpleDeobfuscator simpleDeobfuscator, MethodDefinition cctor) { + bool findStringsResource(IDeobfuscator deob, ISimpleDeobfuscator simpleDeobfuscator, MethodDef cctor) { if (stringsResource != null) return true; @@ -181,7 +181,7 @@ namespace de4dot.code.deobfuscators.SmartAssembly { return false; } - bool findStringsResource2(IDeobfuscator deob, ISimpleDeobfuscator simpleDeobfuscator, MethodDefinition initMethod) { + bool findStringsResource2(IDeobfuscator deob, ISimpleDeobfuscator simpleDeobfuscator, MethodDef initMethod) { if (initMethod == null) return false; @@ -204,7 +204,7 @@ namespace de4dot.code.deobfuscators.SmartAssembly { } // Find the embedded resource where all the strings are encrypted - EmbeddedResource findStringResource(MethodDefinition method) { + EmbeddedResource findStringResource(MethodDef method) { foreach (var s in DotNetUtils.getCodeStrings(method)) { if (s == null) continue; @@ -216,7 +216,7 @@ namespace de4dot.code.deobfuscators.SmartAssembly { } // Find the string decrypter string offset value or null if none found - int? findOffsetValue(MethodDefinition method) { + int? findOffsetValue(MethodDef method) { var fieldDict = new FieldDefinitionAndDeclaringTypeDict(); foreach (var field in method.DeclaringType.Fields) fieldDict.add(field, field); @@ -225,10 +225,10 @@ namespace de4dot.code.deobfuscators.SmartAssembly { if (offsetField == null) return null; - return findOffsetValue(method, (FieldDefinition)fieldDict.find(offsetField), fieldDict); + return findOffsetValue(method, (FieldDef)fieldDict.find(offsetField), fieldDict); } - FieldReference findOffsetField(MethodDefinition method) { + FieldReference findOffsetField(MethodDef method) { var instructions = method.Body.Instructions; for (int i = 0; i <= instructions.Count - 2; i++) { var ldsfld = instructions[i]; @@ -253,7 +253,7 @@ namespace de4dot.code.deobfuscators.SmartAssembly { return null; } - int? findOffsetValue(MethodDefinition method, FieldDefinition offsetField, FieldDefinitionAndDeclaringTypeDict fields) { + int? findOffsetValue(MethodDef method, FieldDef offsetField, FieldDefinitionAndDeclaringTypeDict fields) { var instructions = method.Body.Instructions; for (int i = 0; i <= instructions.Count - 2; i++) { var ldstr = instructions[i]; @@ -284,7 +284,7 @@ namespace de4dot.code.deobfuscators.SmartAssembly { if (stringDecrypterMethod != null) return true; - var methods = new List(DotNetUtils.findMethods(stringsEncodingClass.Methods, "System.String", new string[] { "System.Int32" })); + var methods = new List(DotNetUtils.findMethods(stringsEncodingClass.Methods, "System.String", new string[] { "System.Int32" })); if (methods.Count != 1) return false; @@ -292,7 +292,7 @@ namespace de4dot.code.deobfuscators.SmartAssembly { return true; } - MethodDefinition findSimpleZipTypeMethod(MethodDefinition method) { + MethodDef findSimpleZipTypeMethod(MethodDef method) { if (method == null || method.Body == null) return null; var instructions = method.Body.Instructions; @@ -300,7 +300,7 @@ namespace de4dot.code.deobfuscators.SmartAssembly { var call = instructions[i]; if (call.OpCode.Code != Code.Call) continue; - var calledMethod = call.Operand as MethodDefinition; + var calledMethod = call.Operand as MethodDef; if (calledMethod == null) continue; if (!DotNetUtils.isMethod(calledMethod, "System.Byte[]", "(System.Byte[])")) @@ -321,7 +321,7 @@ namespace de4dot.code.deobfuscators.SmartAssembly { return null; } - public IEnumerable getAllStringDelegateFields() { + public IEnumerable getAllStringDelegateFields() { foreach (var type in module.GetTypes()) { foreach (var field in type.Fields) { if (field.FieldType == GetStringDelegate) diff --git a/de4dot.code/deobfuscators/SmartAssembly/StringEncoderClassFinder.cs b/de4dot.code/deobfuscators/SmartAssembly/StringEncoderClassFinder.cs index 1a50bcdb..08024970 100644 --- a/de4dot.code/deobfuscators/SmartAssembly/StringEncoderClassFinder.cs +++ b/de4dot.code/deobfuscators/SmartAssembly/StringEncoderClassFinder.cs @@ -18,20 +18,20 @@ */ using System.Collections.Generic; -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using de4dot.blocks; namespace de4dot.code.deobfuscators.SmartAssembly { class StringsEncoderInfo { // SmartAssembly.HouseOfCards.Strings, the class that creates the string decrypter // delegates - public TypeDefinition StringsType { get; set; } - public TypeDefinition GetStringDelegate { get; set; } - public MethodDefinition CreateStringDelegateMethod { get; set; } + public TypeDef StringsType { get; set; } + public TypeDef GetStringDelegate { get; set; } + public MethodDef CreateStringDelegateMethod { get; set; } // The class that decodes the strings. Called by the strings delegate or normal code. - public TypeDefinition StringDecrypterClass { get; set; } + public TypeDef StringDecrypterClass { get; set; } } class StringEncoderClassFinder { @@ -48,7 +48,7 @@ namespace de4dot.code.deobfuscators.SmartAssembly { this.simpleDeobfuscator = simpleDeobfuscator; } - TypeDefinition getType(TypeReference typeReference) { + TypeDef getType(TypeReference typeReference) { return DotNetUtils.getType(module, typeReference); } @@ -84,7 +84,7 @@ namespace de4dot.code.deobfuscators.SmartAssembly { } } - bool checkDelegateCreatorMethod(TypeDefinition type, MethodDefinition method) { + bool checkDelegateCreatorMethod(TypeDef type, MethodDef method) { simpleDeobfuscator.deobfuscate(method); var getStringDelegate = findGetStringDelegate(method); @@ -106,7 +106,7 @@ namespace de4dot.code.deobfuscators.SmartAssembly { } // Finds the SmartAssembly.Delegates.GetString delegate - TypeDefinition findGetStringDelegate(MethodDefinition stringsCreateDelegateMethod) { + TypeDef findGetStringDelegate(MethodDef stringsCreateDelegateMethod) { if (!stringsCreateDelegateMethod.HasBody) return null; @@ -132,7 +132,7 @@ namespace de4dot.code.deobfuscators.SmartAssembly { // Finds the SmartAssembly.StringsEncoding.Strings class. This class decrypts the // strings in the resources. It gets called by the SmartAssembly.Delegates.GetString // delegate instances which were created by SmartAssembly.HouseOfCards.Strings. - TypeDefinition findStringDecrypterClass(MethodDefinition stringsCreateDelegateMethod) { + TypeDef findStringDecrypterClass(MethodDef stringsCreateDelegateMethod) { if (!stringsCreateDelegateMethod.HasBody) return null; @@ -155,7 +155,7 @@ namespace de4dot.code.deobfuscators.SmartAssembly { } void findStringDecrypterClasses() { - var foundClasses = new Dictionary(); + var foundClasses = new Dictionary(); foreach (var info in stringsEncoderInfos) foundClasses[info.StringDecrypterClass] = true; @@ -181,7 +181,7 @@ namespace de4dot.code.deobfuscators.SmartAssembly { "System.Byte[]", "System.Int32", }; - bool couldBeStringDecrypterClass(TypeDefinition type) { + bool couldBeStringDecrypterClass(TypeDef type) { var fields = new FieldTypes(type); if (fields.exists("System.Collections.Hashtable") || fields.exists("System.Collections.Generic.Dictionary`2") || @@ -194,7 +194,7 @@ namespace de4dot.code.deobfuscators.SmartAssembly { else return false; - var methods = new List(DotNetUtils.getNormalMethods(type)); + var methods = new List(DotNetUtils.getNormalMethods(type)); if (methods.Count != 1) return false; var method = methods[0]; diff --git a/de4dot.code/deobfuscators/SmartAssembly/TamperProtectionRemover.cs b/de4dot.code/deobfuscators/SmartAssembly/TamperProtectionRemover.cs index b8e0ee11..a1da1434 100644 --- a/de4dot.code/deobfuscators/SmartAssembly/TamperProtectionRemover.cs +++ b/de4dot.code/deobfuscators/SmartAssembly/TamperProtectionRemover.cs @@ -19,21 +19,21 @@ using System; using System.Collections.Generic; -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using de4dot.blocks; namespace de4dot.code.deobfuscators.SmartAssembly { class TamperProtectionRemover { ModuleDefinition module; - List pinvokeMethods = new List(); + List pinvokeMethods = new List(); enum Type { V1, V2, } - public IList PinvokeMethods { + public IList PinvokeMethods { get { return pinvokeMethods; } } @@ -68,7 +68,7 @@ namespace de4dot.code.deobfuscators.SmartAssembly { class TamperBlocks { public Type type; - public MethodDefinition pinvokeMethod; + public MethodDef pinvokeMethod; public BlockInfo first; public BlockInfo second; public BlockInfo bad; @@ -296,7 +296,7 @@ namespace de4dot.code.deobfuscators.SmartAssembly { if (tamperBlocks == null) { if (isTamperProtected(allBlocks)) - Log.w("Could not remove tamper protection code: {0} ({1:X8})", Utils.removeNewlines(blocks.Method), blocks.Method.MetadataToken.ToUInt32()); + Log.w("Could not remove tamper protection code: {0} ({1:X8})", Utils.removeNewlines(blocks.Method), blocks.Method.MDToken.ToUInt32()); return false; } diff --git a/de4dot.code/deobfuscators/Spices_Net/Deobfuscator.cs b/de4dot.code/deobfuscators/Spices_Net/Deobfuscator.cs index 54cbd957..f5f25611 100644 --- a/de4dot.code/deobfuscators/Spices_Net/Deobfuscator.cs +++ b/de4dot.code/deobfuscators/Spices_Net/Deobfuscator.cs @@ -18,7 +18,7 @@ */ using System.Collections.Generic; -using Mono.Cecil; +using dot10.DotNet; using de4dot.blocks; using de4dot.blocks.cflow; @@ -213,7 +213,7 @@ namespace de4dot.code.deobfuscators.Spices_Net { public override IEnumerable getStringDecrypterMethods() { var list = new List(); foreach (var info in stringDecrypter.DecrypterInfos) - list.Add(info.method.MetadataToken.ToInt32()); + list.Add(info.method.MDToken.ToInt32()); return list; } } diff --git a/de4dot.code/deobfuscators/Spices_Net/ResourceNamesRestorer.cs b/de4dot.code/deobfuscators/Spices_Net/ResourceNamesRestorer.cs index 68a7b8ec..2026d0ee 100644 --- a/de4dot.code/deobfuscators/Spices_Net/ResourceNamesRestorer.cs +++ b/de4dot.code/deobfuscators/Spices_Net/ResourceNamesRestorer.cs @@ -18,23 +18,23 @@ */ using System.Collections.Generic; -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using de4dot.blocks; namespace de4dot.code.deobfuscators.Spices_Net { class ResourceNamesRestorer { ModuleDefinition module; - TypeDefinition resourceManagerType; - TypeDefinition componentResourceManagerType; + TypeDef resourceManagerType; + TypeDef componentResourceManagerType; MethodDefinitionAndDeclaringTypeDict resourceManagerCtors = new MethodDefinitionAndDeclaringTypeDict(); MethodDefinitionAndDeclaringTypeDict componentManagerCtors = new MethodDefinitionAndDeclaringTypeDict(); - public TypeDefinition ResourceManagerType { + public TypeDef ResourceManagerType { get { return resourceManagerType; } } - public TypeDefinition ComponentResourceManagerType { + public TypeDef ComponentResourceManagerType { get { return componentResourceManagerType; } } @@ -54,7 +54,7 @@ namespace de4dot.code.deobfuscators.Spices_Net { initializeCtors(componentResourceManagerType, componentManagerCtors); } - static void initializeCtors(TypeDefinition manager, MethodDefinitionAndDeclaringTypeDict ctors) { + static void initializeCtors(TypeDef manager, MethodDefinitionAndDeclaringTypeDict ctors) { if (manager == null) return; @@ -70,7 +70,7 @@ namespace de4dot.code.deobfuscators.Spices_Net { } } - static bool isResourceType(TypeDefinition type, string baseTypeName) { + static bool isResourceType(TypeDef type, string baseTypeName) { if (type.BaseType == null || type.BaseType.FullName != baseTypeName) return false; if (type.HasProperties || type.HasEvents || type.HasFields) diff --git a/de4dot.code/deobfuscators/Spices_Net/SpicesMethodCallInliner.cs b/de4dot.code/deobfuscators/Spices_Net/SpicesMethodCallInliner.cs index 739cae6a..0e334455 100644 --- a/de4dot.code/deobfuscators/Spices_Net/SpicesMethodCallInliner.cs +++ b/de4dot.code/deobfuscators/Spices_Net/SpicesMethodCallInliner.cs @@ -18,8 +18,8 @@ */ using System.Collections.Generic; -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using Mono.Cecil.Metadata; using de4dot.blocks; using de4dot.blocks.cflow; @@ -28,7 +28,7 @@ namespace de4dot.code.deobfuscators.Spices_Net { class SpicesMethodCallInliner : MethodCallInliner { ModuleDefinition module; TypeDefinitionDict methodsTypes = new TypeDefinitionDict(); - MethodDefinitionAndDeclaringTypeDict classMethods = new MethodDefinitionAndDeclaringTypeDict(); + MethodDefinitionAndDeclaringTypeDict classMethods = new MethodDefinitionAndDeclaringTypeDict(); public SpicesMethodCallInliner(ModuleDefinition module) : base(false) { @@ -45,11 +45,11 @@ namespace de4dot.code.deobfuscators.Spices_Net { return newType.EType == ElementType.Object; } - public bool checkCanInline(MethodDefinition method) { + public bool checkCanInline(MethodDef method) { return methodsTypes.find(method.DeclaringType); } - protected override bool canInline(MethodDefinition method) { + protected override bool canInline(MethodDef method) { return checkCanInline(method); } @@ -59,9 +59,9 @@ namespace de4dot.code.deobfuscators.Spices_Net { } void restoreMethodBodies() { - var methodToOrigMethods = new MethodDefinitionAndDeclaringTypeDict>(); + var methodToOrigMethods = new MethodDefinitionAndDeclaringTypeDict>(); foreach (var t in module.Types) { - var types = new List(TypeDefinition.GetTypes(new List { t })); + var types = new List(TypeDef.GetTypes(new List { t })); foreach (var type in types) { if (methodsTypes.find(type)) continue; @@ -69,7 +69,7 @@ namespace de4dot.code.deobfuscators.Spices_Net { if (method.Name == ".ctor" || method.Name == ".cctor") continue; - MethodDefinition calledMethod; + MethodDef calledMethod; if (!checkRestoreBody(method, out calledMethod)) continue; if (!checkSameMethods(method, calledMethod)) @@ -81,7 +81,7 @@ namespace de4dot.code.deobfuscators.Spices_Net { var list = methodToOrigMethods.find(calledMethod); if (list == null) - methodToOrigMethods.add(calledMethod, list = new List()); + methodToOrigMethods.add(calledMethod, list = new List()); list.Add(method); } } @@ -92,14 +92,14 @@ namespace de4dot.code.deobfuscators.Spices_Net { var method = list[0]; Log.v("Restored method body {0:X8} from method {1:X8}", - method.MetadataToken.ToInt32(), - calledMethod.MetadataToken.ToInt32()); + method.MDToken.ToInt32(), + calledMethod.MDToken.ToInt32()); DotNetUtils.copyBodyFromTo(calledMethod, method); classMethods.add(calledMethod, method); } } - bool checkRestoreBody(MethodDefinition method, out MethodDefinition calledMethod) { + bool checkRestoreBody(MethodDef method, out MethodDef calledMethod) { calledMethod = null; if (method.Body == null) return false; @@ -122,7 +122,7 @@ namespace de4dot.code.deobfuscators.Spices_Net { return true; } - bool checkRestoreBody2(MethodDefinition instanceMethod, out MethodDefinition calledMethod) { + bool checkRestoreBody2(MethodDef instanceMethod, out MethodDef calledMethod) { calledMethod = null; var instrs = instanceMethod.Body.Instructions; @@ -135,7 +135,7 @@ namespace de4dot.code.deobfuscators.Spices_Net { if (call.OpCode.Code != Code.Call) return false; - calledMethod = call.Operand as MethodDefinition; + calledMethod = call.Operand as MethodDef; if (calledMethod == null) return false; @@ -152,7 +152,7 @@ namespace de4dot.code.deobfuscators.Spices_Net { } } - static bool checkMethodsType(TypeDefinition type) { + static bool checkMethodsType(TypeDef type) { if (!type.IsNested) return false; if ((type.Attributes & ~TypeAttributes.BeforeFieldInit) != TypeAttributes.NestedAssembly) @@ -173,7 +173,7 @@ namespace de4dot.code.deobfuscators.Spices_Net { return true; } - static bool checkMethods(TypeDefinition type) { + static bool checkMethods(TypeDef type) { bool foundCtor = false; int numMethods = 0; @@ -199,8 +199,8 @@ namespace de4dot.code.deobfuscators.Spices_Net { return numMethods > 0 && foundCtor; } - public List getInlinedMethods() { - var list = new List(); + public List getInlinedMethods() { + var list = new List(); foreach (var type in methodsTypes.getKeys()) list.AddRange(type.Methods); @@ -208,7 +208,7 @@ namespace de4dot.code.deobfuscators.Spices_Net { return list; } - public TypeDefinitionDict getInlinedTypes(IEnumerable unusedMethods) { + public TypeDefinitionDict getInlinedTypes(IEnumerable unusedMethods) { var unused = new MethodDefinitionAndDeclaringTypeDict(); foreach (var method in unusedMethods) unused.add(method, true); @@ -221,7 +221,7 @@ namespace de4dot.code.deobfuscators.Spices_Net { return types; } - static bool checkAllMethodsUnused(MethodDefinitionAndDeclaringTypeDict unused, TypeDefinition type) { + static bool checkAllMethodsUnused(MethodDefinitionAndDeclaringTypeDict unused, TypeDef type) { foreach (var method in type.Methods) { if (!unused.find(method)) return false; diff --git a/de4dot.code/deobfuscators/Spices_Net/StringDecrypter.cs b/de4dot.code/deobfuscators/Spices_Net/StringDecrypter.cs index 499c2d56..8d3711fc 100644 --- a/de4dot.code/deobfuscators/Spices_Net/StringDecrypter.cs +++ b/de4dot.code/deobfuscators/Spices_Net/StringDecrypter.cs @@ -20,16 +20,16 @@ using System; using System.Collections.Generic; using System.Text; -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using Mono.Cecil.Metadata; using de4dot.blocks; namespace de4dot.code.deobfuscators.Spices_Net { class StringDecrypter { ModuleDefinition module; - TypeDefinition decrypterType; - FieldDefinition encryptedDataField; + TypeDef decrypterType; + FieldDef encryptedDataField; StringDataFlags stringDataFlags; MethodDefinitionAndDeclaringTypeDict methodToInfo = new MethodDefinitionAndDeclaringTypeDict(); byte[] decryptedData; @@ -45,22 +45,22 @@ namespace de4dot.code.deobfuscators.Spices_Net { } public class DecrypterInfo { - public MethodDefinition method; + public MethodDef method; public int offset; public int length; - public DecrypterInfo(MethodDefinition method, int offset, int length) { + public DecrypterInfo(MethodDef method, int offset, int length) { this.method = method; this.offset = offset; this.length = length; } } - public TypeDefinition EncryptedStringsType { + public TypeDef EncryptedStringsType { get { if (encryptedDataField == null) return null; - var type = encryptedDataField.FieldType as TypeDefinition; + var type = encryptedDataField.FieldType as TypeDef; if (type == null || type.Fields.Count != 1 || type.Fields[0] != encryptedDataField) return null; if (type.HasMethods || type.HasEvents || type.HasProperties || type.HasNestedTypes) @@ -72,7 +72,7 @@ namespace de4dot.code.deobfuscators.Spices_Net { } } - public TypeDefinition Type { + public TypeDef Type { get { return decrypterType; } } @@ -106,7 +106,7 @@ namespace de4dot.code.deobfuscators.Spices_Net { if (cctor == null) continue; - FieldDefinition encryptedDataFieldTmp; + FieldDef encryptedDataFieldTmp; StringDataFlags stringDataFlagsTmp; if (!checkCctor(cctor, out encryptedDataFieldTmp, out stringDataFlagsTmp)) continue; @@ -121,7 +121,7 @@ namespace de4dot.code.deobfuscators.Spices_Net { } } - static bool hasInstanceMethods(TypeDefinition type) { + static bool hasInstanceMethods(TypeDef type) { foreach (var method in type.Methods) { if (!method.IsStatic) return true; @@ -131,7 +131,7 @@ namespace de4dot.code.deobfuscators.Spices_Net { return false; } - bool checkCctor(MethodDefinition cctor, out FieldDefinition compressedDataField, out StringDataFlags flags) { + bool checkCctor(MethodDef cctor, out FieldDef compressedDataField, out StringDataFlags flags) { flags = 0; var instructions = cctor.Body.Instructions; for (int i = 0; i < instructions.Count; i++) { @@ -147,13 +147,13 @@ namespace de4dot.code.deobfuscators.Spices_Net { if (newarr.Operand.ToString() != "System.Byte") continue; - var field = instrs[2].Operand as FieldDefinition; + var field = instrs[2].Operand as FieldDef; if (field == null || field.InitialValue == null || field.InitialValue.Length == 0) continue; int index = i + 1 + instrs.Count; if (index < instructions.Count && instructions[index].OpCode.Code == Code.Call) - flags = getStringDataFlags(instructions[index].Operand as MethodDefinition); + flags = getStringDataFlags(instructions[index].Operand as MethodDef); compressedDataField = field; return true; @@ -163,7 +163,7 @@ namespace de4dot.code.deobfuscators.Spices_Net { return false; } - StringDataFlags getStringDataFlags(MethodDefinition method) { + StringDataFlags getStringDataFlags(MethodDef method) { if (method == null || method.Body == null) return 0; if (method.Parameters.Count != 1) @@ -187,11 +187,11 @@ namespace de4dot.code.deobfuscators.Spices_Net { return flags; } - bool check3DesCreator(MethodDefinition method) { + bool check3DesCreator(MethodDef method) { foreach (var instr in method.Body.Instructions) { if (instr.OpCode.Code != Code.Call) continue; - var calledMethod = instr.Operand as MethodDefinition; + var calledMethod = instr.Operand as MethodDef; if (calledMethod == null) continue; if (calledMethod.MethodReturnType.ReturnType.EType == ElementType.Void) @@ -206,7 +206,7 @@ namespace de4dot.code.deobfuscators.Spices_Net { return false; } - bool get3DesKeyIv(MethodDefinition method, ref byte[] key, ref byte[] iv) { + bool get3DesKeyIv(MethodDef method, ref byte[] key, ref byte[] iv) { if (!new LocalTypes(method).exists("System.Security.Cryptography.TripleDESCryptoServiceProvider")) return false; @@ -223,11 +223,11 @@ namespace de4dot.code.deobfuscators.Spices_Net { return true; } - static bool callsDecompressor(MethodDefinition method) { + static bool callsDecompressor(MethodDef method) { foreach (var instr in method.Body.Instructions) { if (instr.OpCode.Code != Code.Call) continue; - var called = instr.Operand as MethodDefinition; + var called = instr.Operand as MethodDef; if (called == null) continue; if (called.MethodReturnType.ReturnType.EType != ElementType.I4) @@ -249,7 +249,7 @@ namespace de4dot.code.deobfuscators.Spices_Net { return false; } - static bool hasInstruction(MethodDefinition method, Code code) { + static bool hasInstruction(MethodDef method, Code code) { foreach (var instr in method.Body.Instructions) { if (instr.OpCode.Code == code) return true; @@ -265,7 +265,7 @@ namespace de4dot.code.deobfuscators.Spices_Net { return type != null && (type.EType == ElementType.Object || type.EType == ElementType.String); } - bool initializeDecrypterInfos(TypeDefinition type) { + bool initializeDecrypterInfos(TypeDef type) { foreach (var method in type.Methods) { if (!method.IsStatic || method.Body == null) continue; @@ -284,7 +284,7 @@ namespace de4dot.code.deobfuscators.Spices_Net { return methodToInfo.Count != 0; } - DecrypterInfo createInfo(MethodDefinition method) { + DecrypterInfo createInfo(MethodDef method) { var instrs = method.Body.Instructions; for (int i = 0; i < instrs.Count - 1; i++) { var ldci4_1 = instrs[i]; @@ -334,7 +334,7 @@ namespace de4dot.code.deobfuscators.Spices_Net { encryptedDataField.FieldType = module.TypeSystem.Byte; } - public string decrypt(MethodDefinition method) { + public string decrypt(MethodDef method) { var info = methodToInfo.find(method); return Encoding.Unicode.GetString(decryptedData, info.offset, info.length); } diff --git a/de4dot.code/deobfuscators/Xenocode/Deobfuscator.cs b/de4dot.code/deobfuscators/Xenocode/Deobfuscator.cs index ec357b57..13042236 100644 --- a/de4dot.code/deobfuscators/Xenocode/Deobfuscator.cs +++ b/de4dot.code/deobfuscators/Xenocode/Deobfuscator.cs @@ -18,7 +18,7 @@ */ using System.Collections.Generic; -using Mono.Cecil; +using dot10.DotNet; namespace de4dot.code.deobfuscators.Xenocode { public class DeobfuscatorInfo : DeobfuscatorInfoBase { @@ -114,7 +114,7 @@ namespace de4dot.code.deobfuscators.Xenocode { public override IEnumerable getStringDecrypterMethods() { var list = new List(); if (stringDecrypter.Method != null) - list.Add(stringDecrypter.Method.MetadataToken.ToInt32()); + list.Add(stringDecrypter.Method.MDToken.ToInt32()); return list; } } diff --git a/de4dot.code/deobfuscators/Xenocode/StringDecrypter.cs b/de4dot.code/deobfuscators/Xenocode/StringDecrypter.cs index 55e119ba..dc2446f1 100644 --- a/de4dot.code/deobfuscators/Xenocode/StringDecrypter.cs +++ b/de4dot.code/deobfuscators/Xenocode/StringDecrypter.cs @@ -18,25 +18,25 @@ */ using System.Text; -using Mono.Cecil; +using dot10.DotNet; using de4dot.blocks; namespace de4dot.code.deobfuscators.Xenocode { class StringDecrypter { const int STRING_DECRYPTER_KEY_CONST = 1789; ModuleDefinition module; - TypeDefinition stringDecrypterType; - MethodDefinition stringDecrypterMethod; + TypeDef stringDecrypterType; + MethodDef stringDecrypterMethod; public bool Detected { get { return stringDecrypterMethod != null; } } - public TypeDefinition Type { + public TypeDef Type { get { return stringDecrypterType; } } - public MethodDefinition Method { + public MethodDef Method { get { return stringDecrypterMethod; } } @@ -53,7 +53,7 @@ namespace de4dot.code.deobfuscators.Xenocode { if (type.HasProperties || type.HasEvents) continue; - MethodDefinition method = null; + MethodDef method = null; foreach (var m in type.Methods) { if (m.Name == ".ctor" || m.Name == ".cctor") continue; diff --git a/de4dot.code/deobfuscators/dotNET_Reactor/v3/AntiStrongName.cs b/de4dot.code/deobfuscators/dotNET_Reactor/v3/AntiStrongName.cs index 00da2b21..3eb4ed72 100644 --- a/de4dot.code/deobfuscators/dotNET_Reactor/v3/AntiStrongName.cs +++ b/de4dot.code/deobfuscators/dotNET_Reactor/v3/AntiStrongName.cs @@ -18,8 +18,8 @@ */ using System.Collections.Generic; -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using de4dot.blocks; namespace de4dot.code.deobfuscators.dotNET_Reactor.v3 { diff --git a/de4dot.code/deobfuscators/dotNET_Reactor/v3/ApplicationModeDecrypter.cs b/de4dot.code/deobfuscators/dotNET_Reactor/v3/ApplicationModeDecrypter.cs index 692574d9..70dc4c0b 100644 --- a/de4dot.code/deobfuscators/dotNET_Reactor/v3/ApplicationModeDecrypter.cs +++ b/de4dot.code/deobfuscators/dotNET_Reactor/v3/ApplicationModeDecrypter.cs @@ -17,7 +17,7 @@ along with de4dot. If not, see . */ -using Mono.Cecil; +using dot10.DotNet; using de4dot.blocks; using de4dot.blocks.cflow; diff --git a/de4dot.code/deobfuscators/dotNET_Reactor/v3/ApplicationModeUnpacker.cs b/de4dot.code/deobfuscators/dotNET_Reactor/v3/ApplicationModeUnpacker.cs index 3bf94291..0bea84ac 100644 --- a/de4dot.code/deobfuscators/dotNET_Reactor/v3/ApplicationModeUnpacker.cs +++ b/de4dot.code/deobfuscators/dotNET_Reactor/v3/ApplicationModeUnpacker.cs @@ -22,7 +22,7 @@ using System.Collections.Generic; using System.IO; using System.Text; using System.Text.RegularExpressions; -using Mono.Cecil; +using dot10.DotNet; using de4dot.PE; namespace de4dot.code.deobfuscators.dotNET_Reactor.v3 { diff --git a/de4dot.code/deobfuscators/dotNET_Reactor/v3/AssemblyResolver.cs b/de4dot.code/deobfuscators/dotNET_Reactor/v3/AssemblyResolver.cs index bb362aca..e6536bc5 100644 --- a/de4dot.code/deobfuscators/dotNET_Reactor/v3/AssemblyResolver.cs +++ b/de4dot.code/deobfuscators/dotNET_Reactor/v3/AssemblyResolver.cs @@ -18,7 +18,7 @@ */ using System.Collections.Generic; -using Mono.Cecil; +using dot10.DotNet; using de4dot.blocks; using de4dot.blocks.cflow; @@ -38,11 +38,11 @@ namespace de4dot.code.deobfuscators.dotNET_Reactor.v3 { get { return decryptMethod.Detected; } } - public AssemblyResolver(TypeDefinition type, ICflowDeobfuscator cflowDeobfuscator) { + public AssemblyResolver(TypeDef type, ICflowDeobfuscator cflowDeobfuscator) { find(type, cflowDeobfuscator); } - void find(TypeDefinition type, ICflowDeobfuscator cflowDeobfuscator) { + void find(TypeDef type, ICflowDeobfuscator cflowDeobfuscator) { var additionalTypes = new List { "System.IO.BinaryReader", "System.IO.FileStream", diff --git a/de4dot.code/deobfuscators/dotNET_Reactor/v3/DecryptMethod.cs b/de4dot.code/deobfuscators/dotNET_Reactor/v3/DecryptMethod.cs index aa833355..3f2d16fa 100644 --- a/de4dot.code/deobfuscators/dotNET_Reactor/v3/DecryptMethod.cs +++ b/de4dot.code/deobfuscators/dotNET_Reactor/v3/DecryptMethod.cs @@ -19,11 +19,11 @@ using System; using System.Collections.Generic; -using Mono.Cecil; +using dot10.DotNet; namespace de4dot.code.deobfuscators.dotNET_Reactor.v3 { class DecryptMethod { - MethodDefinition decryptionMethod; + MethodDef decryptionMethod; byte[] key; byte[] iv; @@ -39,7 +39,7 @@ namespace de4dot.code.deobfuscators.dotNET_Reactor.v3 { get { return decryptionMethod != null; } } - public static bool couldBeDecryptMethod(MethodDefinition method, IEnumerable additionalTypes) { + public static bool couldBeDecryptMethod(MethodDef method, IEnumerable additionalTypes) { if (method.Body == null) return false; @@ -60,7 +60,7 @@ namespace de4dot.code.deobfuscators.dotNET_Reactor.v3 { return true; } - public bool getKey(MethodDefinition method) { + public bool getKey(MethodDef method) { var tmpKey = ArrayFinder.getInitializedByteArray(method, 32); if (tmpKey == null) return false; diff --git a/de4dot.code/deobfuscators/dotNET_Reactor/v3/DecrypterType.cs b/de4dot.code/deobfuscators/dotNET_Reactor/v3/DecrypterType.cs index 0e49d91b..80c1612c 100644 --- a/de4dot.code/deobfuscators/dotNET_Reactor/v3/DecrypterType.cs +++ b/de4dot.code/deobfuscators/dotNET_Reactor/v3/DecrypterType.cs @@ -20,7 +20,7 @@ using System; using System.Collections.Generic; using System.Text; -using Mono.Cecil; +using dot10.DotNet; using de4dot.blocks; using de4dot.PE; @@ -28,10 +28,10 @@ namespace de4dot.code.deobfuscators.dotNET_Reactor.v3 { // Find the type that decrypts strings and calls the native lib class DecrypterType { ModuleDefinition module; - TypeDefinition decrypterType; - MethodDefinition stringDecrypter1; - MethodDefinition stringDecrypter2; - List initMethods = new List(); + TypeDef decrypterType; + MethodDef stringDecrypter1; + MethodDef stringDecrypter2; + List initMethods = new List(); List moduleReferences = new List(); Resource linkedResource; @@ -39,7 +39,7 @@ namespace de4dot.code.deobfuscators.dotNET_Reactor.v3 { get { return decrypterType != null; } } - public TypeDefinition Type { + public TypeDef Type { get { return decrypterType; } } @@ -47,15 +47,15 @@ namespace de4dot.code.deobfuscators.dotNET_Reactor.v3 { get { return linkedResource; } } - public MethodDefinition StringDecrypter1 { + public MethodDef StringDecrypter1 { get { return stringDecrypter1; } } - public MethodDefinition StringDecrypter2 { + public MethodDef StringDecrypter2 { get { return stringDecrypter2; } } - public IEnumerable InitMethods { + public IEnumerable InitMethods { get { return initMethods; } } @@ -63,9 +63,9 @@ namespace de4dot.code.deobfuscators.dotNET_Reactor.v3 { get { return moduleReferences; } } - public IEnumerable StringDecrypters { + public IEnumerable StringDecrypters { get { - return new List { + return new List { stringDecrypter1, stringDecrypter2, }; @@ -132,7 +132,7 @@ namespace de4dot.code.deobfuscators.dotNET_Reactor.v3 { } } - MethodDefinition getStringDecrypter(TypeDefinition type, string name) { + MethodDef getStringDecrypter(TypeDef type, string name) { var method = DotNetUtils.getMethod(type, name); if (method == null) return null; diff --git a/de4dot.code/deobfuscators/dotNET_Reactor/v3/Deobfuscator.cs b/de4dot.code/deobfuscators/dotNET_Reactor/v3/Deobfuscator.cs index f543fc3c..19921c0b 100644 --- a/de4dot.code/deobfuscators/dotNET_Reactor/v3/Deobfuscator.cs +++ b/de4dot.code/deobfuscators/dotNET_Reactor/v3/Deobfuscator.cs @@ -21,8 +21,8 @@ using System; using System.Collections.Generic; using System.IO; using System.Text.RegularExpressions; -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using Mono.MyStuff; using de4dot.blocks; using de4dot.PE; @@ -280,7 +280,7 @@ namespace de4dot.code.deobfuscators.dotNET_Reactor.v3 { startedDeobfuscating = true; } - void removeInitCall(MethodDefinition initMethod) { + void removeInitCall(MethodDef initMethod) { addCctorInitCallToBeRemoved(initMethod); addCtorInitCallToBeRemoved(initMethod); } @@ -336,7 +336,7 @@ namespace de4dot.code.deobfuscators.dotNET_Reactor.v3 { public override IEnumerable getStringDecrypterMethods() { var list = new List(); foreach (var method in decrypterType.StringDecrypters) - list.Add(method.MetadataToken.ToInt32()); + list.Add(method.MDToken.ToInt32()); return list; } } diff --git a/de4dot.code/deobfuscators/dotNET_Reactor/v3/LibAssemblyResolver.cs b/de4dot.code/deobfuscators/dotNET_Reactor/v3/LibAssemblyResolver.cs index 11a33892..b3183fee 100644 --- a/de4dot.code/deobfuscators/dotNET_Reactor/v3/LibAssemblyResolver.cs +++ b/de4dot.code/deobfuscators/dotNET_Reactor/v3/LibAssemblyResolver.cs @@ -18,22 +18,22 @@ */ using System.Collections.Generic; -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using de4dot.blocks; namespace de4dot.code.deobfuscators.dotNET_Reactor.v3 { // Find the assembly resolver that's used in lib mode (3.8+) class LibAssemblyResolver { ModuleDefinition module; - MethodDefinition initMethod; + MethodDef initMethod; List resources = new List(); - public TypeDefinition Type { + public TypeDef Type { get { return initMethod == null ? null : initMethod.DeclaringType; } } - public MethodDefinition InitMethod { + public MethodDef InitMethod { get { return initMethod; } } @@ -52,7 +52,7 @@ namespace de4dot.code.deobfuscators.dotNET_Reactor.v3 { return; } - bool checkInitMethod(MethodDefinition checkMethod, ISimpleDeobfuscator simpleDeobfuscator, IDeobfuscator deob) { + bool checkInitMethod(MethodDef checkMethod, ISimpleDeobfuscator simpleDeobfuscator, IDeobfuscator deob) { var requiredFields = new string[] { "System.Collections.Hashtable", "System.Boolean", @@ -94,7 +94,7 @@ namespace de4dot.code.deobfuscators.dotNET_Reactor.v3 { return false; } - string getResourcePrefix(MethodDefinition handler) { + string getResourcePrefix(MethodDef handler) { foreach (var s in DotNetUtils.getCodeStrings(handler)) { var resource = DotNetUtils.getResource(module, s + "00000") as EmbeddedResource; if (resource != null) diff --git a/de4dot.code/deobfuscators/dotNET_Reactor/v3/MemoryPatcher.cs b/de4dot.code/deobfuscators/dotNET_Reactor/v3/MemoryPatcher.cs index ab459b16..03f83713 100644 --- a/de4dot.code/deobfuscators/dotNET_Reactor/v3/MemoryPatcher.cs +++ b/de4dot.code/deobfuscators/dotNET_Reactor/v3/MemoryPatcher.cs @@ -20,7 +20,7 @@ using System; using System.Collections.Generic; using System.IO; -using Mono.Cecil; +using dot10.DotNet; using de4dot.blocks; using de4dot.blocks.cflow; using de4dot.PE; @@ -43,11 +43,11 @@ namespace de4dot.code.deobfuscators.dotNET_Reactor.v3 { get { return decryptMethod.Detected; } } - public MemoryPatcher(TypeDefinition type, ICflowDeobfuscator cflowDeobfuscator) { + public MemoryPatcher(TypeDef type, ICflowDeobfuscator cflowDeobfuscator) { find(type, cflowDeobfuscator); } - void find(TypeDefinition type, ICflowDeobfuscator cflowDeobfuscator) { + void find(TypeDef type, ICflowDeobfuscator cflowDeobfuscator) { var additionalTypes = new List { "System.IO.BinaryWriter", }; @@ -65,7 +65,7 @@ namespace de4dot.code.deobfuscators.dotNET_Reactor.v3 { } } - void findPatchData(TypeDefinition type, ICflowDeobfuscator cflowDeobfuscator) { + void findPatchData(TypeDef type, ICflowDeobfuscator cflowDeobfuscator) { var locals = new List { "System.Int32[]", "System.UInt32[]", @@ -86,7 +86,7 @@ namespace de4dot.code.deobfuscators.dotNET_Reactor.v3 { } } - PatchInfo getPatchInfo(MethodDefinition method) { + PatchInfo getPatchInfo(MethodDef method) { int index1 = 0, index2, index3, size1, size2, size3; if (!ArrayFinder.findNewarr(method, ref index1, out size1)) return null; diff --git a/de4dot.code/deobfuscators/dotNET_Reactor/v3/NativeLibSaver.cs b/de4dot.code/deobfuscators/dotNET_Reactor/v3/NativeLibSaver.cs index bc2eb849..78924412 100644 --- a/de4dot.code/deobfuscators/dotNET_Reactor/v3/NativeLibSaver.cs +++ b/de4dot.code/deobfuscators/dotNET_Reactor/v3/NativeLibSaver.cs @@ -18,22 +18,22 @@ */ using System; -using Mono.Cecil; +using dot10.DotNet; using de4dot.blocks; namespace de4dot.code.deobfuscators.dotNET_Reactor.v3 { // Finds the type that saves the native lib (if in resources) to disk class NativeLibSaver { ModuleDefinition module; - TypeDefinition nativeLibCallerType; - MethodDefinition initMethod; + TypeDef nativeLibCallerType; + MethodDef initMethod; Resource nativeFileResource; - public TypeDefinition Type { + public TypeDef Type { get { return nativeLibCallerType; } } - public MethodDefinition InitMethod { + public MethodDef InitMethod { get { return initMethod; } } diff --git a/de4dot.code/deobfuscators/dotNET_Reactor/v4/AntiStrongname.cs b/de4dot.code/deobfuscators/dotNET_Reactor/v4/AntiStrongname.cs index dc050057..8a762c48 100644 --- a/de4dot.code/deobfuscators/dotNET_Reactor/v4/AntiStrongname.cs +++ b/de4dot.code/deobfuscators/dotNET_Reactor/v4/AntiStrongname.cs @@ -18,16 +18,16 @@ */ using System; -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using de4dot.blocks; namespace de4dot.code.deobfuscators.dotNET_Reactor.v4 { class AntiStrongName { - TypeDefinition decrypterType; - MethodDefinition antiStrongNameMethod; + TypeDef decrypterType; + MethodDef antiStrongNameMethod; - public AntiStrongName(TypeDefinition decrypterType) { + public AntiStrongName(TypeDef decrypterType) { this.decrypterType = decrypterType; find(); } @@ -45,7 +45,7 @@ namespace de4dot.code.deobfuscators.dotNET_Reactor.v4 { } } - bool checkType(TypeDefinition type) { + bool checkType(TypeDef type) { var requiredTypes = new string[] { "System.Byte[]", "System.IO.MemoryStream", diff --git a/de4dot.code/deobfuscators/dotNET_Reactor/v4/AssemblyResolver.cs b/de4dot.code/deobfuscators/dotNET_Reactor/v4/AssemblyResolver.cs index def1dfc3..4e46bbd7 100644 --- a/de4dot.code/deobfuscators/dotNET_Reactor/v4/AssemblyResolver.cs +++ b/de4dot.code/deobfuscators/dotNET_Reactor/v4/AssemblyResolver.cs @@ -20,7 +20,7 @@ using System; using System.Collections.Generic; using System.IO; -using Mono.Cecil; +using dot10.DotNet; using de4dot.blocks; namespace de4dot.code.deobfuscators.dotNET_Reactor.v4 { @@ -40,19 +40,19 @@ namespace de4dot.code.deobfuscators.dotNET_Reactor.v4 { class AssemblyResolver { ModuleDefinition module; - TypeDefinition assemblyResolverType; - MethodDefinition assemblyResolverInitMethod; - MethodDefinition assemblyResolverMethod; + TypeDef assemblyResolverType; + MethodDef assemblyResolverInitMethod; + MethodDef assemblyResolverMethod; public bool Detected { get { return assemblyResolverType != null; } } - public TypeDefinition Type { + public TypeDef Type { get { return assemblyResolverType; } } - public MethodDefinition InitMethod { + public MethodDef InitMethod { get { return assemblyResolverInitMethod; } } @@ -80,7 +80,7 @@ namespace de4dot.code.deobfuscators.dotNET_Reactor.v4 { } } - bool checkMethod(ISimpleDeobfuscator simpleDeobfuscator, MethodDefinition methodToCheck) { + bool checkMethod(ISimpleDeobfuscator simpleDeobfuscator, MethodDef methodToCheck) { if (methodToCheck == null) return false; @@ -127,7 +127,7 @@ namespace de4dot.code.deobfuscators.dotNET_Reactor.v4 { return false; } - static bool checkFields(IList fields) { + static bool checkFields(IList fields) { if (fields.Count != 2) return false; @@ -137,7 +137,7 @@ namespace de4dot.code.deobfuscators.dotNET_Reactor.v4 { fieldTypes.count("System.Object") == 1); } - static MethodDefinition findAssemblyResolveMethod(TypeDefinition type) { + static MethodDef findAssemblyResolveMethod(TypeDef type) { foreach (var method in type.Methods) { if (DotNetUtils.isMethod(method, "System.Reflection.Assembly", "(System.Object,System.ResolveEventArgs)")) return method; diff --git a/de4dot.code/deobfuscators/dotNET_Reactor/v4/BooleanDecrypter.cs b/de4dot.code/deobfuscators/dotNET_Reactor/v4/BooleanDecrypter.cs index d3e62d16..fca207f2 100644 --- a/de4dot.code/deobfuscators/dotNET_Reactor/v4/BooleanDecrypter.cs +++ b/de4dot.code/deobfuscators/dotNET_Reactor/v4/BooleanDecrypter.cs @@ -18,7 +18,7 @@ */ using System; -using Mono.Cecil; +using dot10.DotNet; using de4dot.blocks; namespace de4dot.code.deobfuscators.dotNET_Reactor.v4 { @@ -32,11 +32,11 @@ namespace de4dot.code.deobfuscators.dotNET_Reactor.v4 { get { return encryptedResource.Method != null; } } - public TypeDefinition DecrypterType { + public TypeDef DecrypterType { get { return encryptedResource.Type; } } - public MethodDefinition Method { + public MethodDef Method { get { return encryptedResource.Method; } } diff --git a/de4dot.code/deobfuscators/dotNET_Reactor/v4/Deobfuscator.cs b/de4dot.code/deobfuscators/dotNET_Reactor/v4/Deobfuscator.cs index f5ca0c69..b000c6eb 100644 --- a/de4dot.code/deobfuscators/dotNET_Reactor/v4/Deobfuscator.cs +++ b/de4dot.code/deobfuscators/dotNET_Reactor/v4/Deobfuscator.cs @@ -21,8 +21,8 @@ using System; using System.Collections.Generic; using System.IO; using System.Text.RegularExpressions; -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using Mono.MyStuff; using de4dot.blocks; using de4dot.PE; @@ -359,7 +359,7 @@ namespace de4dot.code.deobfuscators.dotNET_Reactor.v4 { return DeobfuscatorInfo.THE_NAME + " 4.3"; } - static bool findString(MethodDefinition method, string s) { + static bool findString(MethodDef method, string s) { foreach (var cs in DotNetUtils.getCodeStrings(method)) { if (cs == s) return true; @@ -504,7 +504,7 @@ namespace de4dot.code.deobfuscators.dotNET_Reactor.v4 { startedDeobfuscating = true; } - void addEntryPointCallToBeRemoved(MethodDefinition methodToBeRemoved) { + void addEntryPointCallToBeRemoved(MethodDef methodToBeRemoved) { var entryPoint = module.EntryPoint; addCallToBeRemoved(entryPoint, methodToBeRemoved); foreach (var calledMethod in DotNetUtils.getCalledMethods(module, entryPoint)) @@ -547,7 +547,7 @@ namespace de4dot.code.deobfuscators.dotNET_Reactor.v4 { Log.v("Removed anti strong name code"); } - TypeDefinition getDecrypterType() { + TypeDef getDecrypterType() { return methodsDecrypter.DecrypterType ?? stringDecrypter.DecrypterType ?? booleanDecrypter.DecrypterType; } @@ -596,9 +596,9 @@ namespace de4dot.code.deobfuscators.dotNET_Reactor.v4 { public override IEnumerable getStringDecrypterMethods() { var list = new List(); foreach (var info in stringDecrypter.DecrypterInfos) - list.Add(info.method.MetadataToken.ToInt32()); + list.Add(info.method.MDToken.ToInt32()); if (stringDecrypter.OtherStringDecrypter != null) - list.Add(stringDecrypter.OtherStringDecrypter.MetadataToken.ToInt32()); + list.Add(stringDecrypter.OtherStringDecrypter.MDToken.ToInt32()); return list; } diff --git a/de4dot.code/deobfuscators/dotNET_Reactor/v4/EmptyClass.cs b/de4dot.code/deobfuscators/dotNET_Reactor/v4/EmptyClass.cs index 705901ce..f472e6f4 100644 --- a/de4dot.code/deobfuscators/dotNET_Reactor/v4/EmptyClass.cs +++ b/de4dot.code/deobfuscators/dotNET_Reactor/v4/EmptyClass.cs @@ -17,20 +17,20 @@ along with de4dot. If not, see . */ -using Mono.Cecil; +using dot10.DotNet; using de4dot.blocks; namespace de4dot.code.deobfuscators.dotNET_Reactor.v4 { // Detect some empty class that is called from most .ctor's class EmptyClass { ModuleDefinition module; - MethodDefinition emptyMethod; + MethodDef emptyMethod; - public MethodDefinition Method { + public MethodDef Method { get { return emptyMethod; } } - public TypeDefinition Type { + public TypeDef Type { get { return emptyMethod != null ? emptyMethod.DeclaringType : null; } } @@ -62,12 +62,12 @@ namespace de4dot.code.deobfuscators.dotNET_Reactor.v4 { } int numCalls; - var theMethod = (MethodDefinition)callCounter.most(out numCalls); + var theMethod = (MethodDef)callCounter.most(out numCalls); if (numCalls >= 10) emptyMethod = theMethod; } - bool isEmptyClass(MethodDefinition emptyMethod) { + bool isEmptyClass(MethodDef emptyMethod) { if (!DotNetUtils.isEmptyObfuscated(emptyMethod)) return false; diff --git a/de4dot.code/deobfuscators/dotNET_Reactor/v4/EncryptedResource.cs b/de4dot.code/deobfuscators/dotNET_Reactor/v4/EncryptedResource.cs index 42b155e3..45e5560d 100644 --- a/de4dot.code/deobfuscators/dotNET_Reactor/v4/EncryptedResource.cs +++ b/de4dot.code/deobfuscators/dotNET_Reactor/v4/EncryptedResource.cs @@ -21,22 +21,22 @@ using System; using System.IO; using System.Collections.Generic; using System.Security.Cryptography; -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using de4dot.blocks; namespace de4dot.code.deobfuscators.dotNET_Reactor.v4 { class EncryptedResource { ModuleDefinition module; - MethodDefinition resourceDecrypterMethod; + MethodDef resourceDecrypterMethod; EmbeddedResource encryptedDataResource; byte[] key, iv; - public TypeDefinition Type { + public TypeDef Type { get { return resourceDecrypterMethod == null ? null : resourceDecrypterMethod.DeclaringType; } } - public MethodDefinition Method { + public MethodDef Method { get { return resourceDecrypterMethod; } set { resourceDecrypterMethod = value; } } @@ -69,11 +69,11 @@ namespace de4dot.code.deobfuscators.dotNET_Reactor.v4 { return DeobUtils.lookup(module, def, errorMessage); } - public bool couldBeResourceDecrypter(MethodDefinition method, IEnumerable additionalTypes) { + public bool couldBeResourceDecrypter(MethodDef method, IEnumerable additionalTypes) { return couldBeResourceDecrypter(method, additionalTypes, true); } - public bool couldBeResourceDecrypter(MethodDefinition method, IEnumerable additionalTypes, bool checkResource) { + public bool couldBeResourceDecrypter(MethodDef method, IEnumerable additionalTypes, bool checkResource) { if (!method.IsStatic) return false; if (method.Body == null) @@ -146,7 +146,7 @@ namespace de4dot.code.deobfuscators.dotNET_Reactor.v4 { return false; } - EmbeddedResource findMethodsDecrypterResource(MethodDefinition method) { + EmbeddedResource findMethodsDecrypterResource(MethodDef method) { foreach (var s in DotNetUtils.getCodeStrings(method)) { var resource = DotNetUtils.getResource(module, s) as EmbeddedResource; if (resource != null) diff --git a/de4dot.code/deobfuscators/dotNET_Reactor/v4/MetadataTokenObfuscator.cs b/de4dot.code/deobfuscators/dotNET_Reactor/v4/MetadataTokenObfuscator.cs index 8d4030a1..cda9a292 100644 --- a/de4dot.code/deobfuscators/dotNET_Reactor/v4/MetadataTokenObfuscator.cs +++ b/de4dot.code/deobfuscators/dotNET_Reactor/v4/MetadataTokenObfuscator.cs @@ -17,8 +17,8 @@ along with de4dot. If not, see . */ -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using de4dot.blocks; namespace de4dot.code.deobfuscators.dotNET_Reactor.v4 { @@ -26,11 +26,11 @@ namespace de4dot.code.deobfuscators.dotNET_Reactor.v4 { // its methods is the original metadata token, which will be different when we save the file. class MetadataTokenObfuscator { ModuleDefinition module; - TypeDefinition type; - MethodDefinition typeMethod; - MethodDefinition fieldMethod; + TypeDef type; + MethodDef typeMethod; + MethodDef fieldMethod; - public TypeDefinition Type { + public TypeDef Type { get { return type; } } @@ -49,7 +49,7 @@ namespace de4dot.code.deobfuscators.dotNET_Reactor.v4 { if (type.HasProperties || type.HasEvents) continue; - MethodDefinition fieldMethod = null, typeMethod = null; + MethodDef fieldMethod = null, typeMethod = null; foreach (var method in type.Methods) { if (method.Parameters.Count != 1) continue; diff --git a/de4dot.code/deobfuscators/dotNET_Reactor/v4/MethodsDecrypter.cs b/de4dot.code/deobfuscators/dotNET_Reactor/v4/MethodsDecrypter.cs index de992d52..f0aff8a3 100644 --- a/de4dot.code/deobfuscators/dotNET_Reactor/v4/MethodsDecrypter.cs +++ b/de4dot.code/deobfuscators/dotNET_Reactor/v4/MethodsDecrypter.cs @@ -20,8 +20,8 @@ using System; using System.Collections.Generic; using System.IO; -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using Mono.MyStuff; using de4dot.blocks; using de4dot.PE; @@ -31,7 +31,7 @@ namespace de4dot.code.deobfuscators.dotNET_Reactor.v4 { ModuleDefinition module; EncryptedResource encryptedResource; Dictionary tokenToNativeMethod = new Dictionary(); - Dictionary methodToNativeMethod = new Dictionary(); + Dictionary methodToNativeMethod = new Dictionary(); int totalEncryptedNativeMethods = 0; long xorKey; @@ -43,11 +43,11 @@ namespace de4dot.code.deobfuscators.dotNET_Reactor.v4 { get { return methodToNativeMethod.Count > 0; } } - public TypeDefinition DecrypterType { + public TypeDef DecrypterType { get { return encryptedResource.Type; } } - public MethodDefinition Method { + public MethodDef Method { get { return encryptedResource.Method; } } @@ -101,7 +101,7 @@ namespace de4dot.code.deobfuscators.dotNET_Reactor.v4 { } } - encryptedResource.Method = (MethodDefinition)callCounter.most(); + encryptedResource.Method = (MethodDef)callCounter.most(); } void xorEncrypt(byte[] data) { @@ -288,7 +288,7 @@ namespace de4dot.code.deobfuscators.dotNET_Reactor.v4 { public void reloaded() { foreach (var pair in tokenToNativeMethod) { int token = (int)pair.Key; - var method = module.LookupToken(token) as MethodDefinition; + var method = module.LookupToken(token) as MethodDef; if (method == null) throw new ApplicationException(string.Format("Could not find method {0:X8}", token)); methodToNativeMethod[method] = pair.Value; @@ -318,13 +318,13 @@ namespace de4dot.code.deobfuscators.dotNET_Reactor.v4 { continue; // method.DeclaringType was removed var code = pair.Value; - uint codeRva = builder.GetMethodBodyRva((int)method.MetadataToken.RID - 1); + uint codeRva = builder.GetMethodBodyRva((int)method.MDToken.RID - 1); if ((codeWriter.ReadByteAtRva(codeRva) & 3) == 2) codeRva++; else codeRva += 4 * (uint)(codeWriter.ReadByteAtRva(codeRva + 1) >> 4); - Log.v("Native method {0:X8}, code RVA {1:X8}", method.MetadataToken.ToInt32(), codeRva); + Log.v("Native method {0:X8}, code RVA {1:X8}", method.MDToken.ToInt32(), codeRva); writer.Write(codeRva); writer.Write(0x70000000 + index++); @@ -340,7 +340,7 @@ namespace de4dot.code.deobfuscators.dotNET_Reactor.v4 { encryptedResource.updateResource(encryptedResource.encrypt(encryptedData)); } - public static MethodDefinition findDnrCompileMethod(TypeDefinition type) { + public static MethodDef findDnrCompileMethod(TypeDef type) { foreach (var method in type.Methods) { if (!method.IsStatic || method.Body == null) continue; diff --git a/de4dot.code/deobfuscators/dotNET_Reactor/v4/ProxyCallFixer.cs b/de4dot.code/deobfuscators/dotNET_Reactor/v4/ProxyCallFixer.cs index 432a0df7..97097e0e 100644 --- a/de4dot.code/deobfuscators/dotNET_Reactor/v4/ProxyCallFixer.cs +++ b/de4dot.code/deobfuscators/dotNET_Reactor/v4/ProxyCallFixer.cs @@ -17,8 +17,8 @@ along with de4dot. If not, see . */ -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using de4dot.blocks; namespace de4dot.code.deobfuscators.dotNET_Reactor.v4 { @@ -44,7 +44,7 @@ namespace de4dot.code.deobfuscators.dotNET_Reactor.v4 { static readonly string[] requiredFields = new string[] { "System.Reflection.Module", }; - static MethodDefinition checkType(TypeDefinition type) { + static MethodDef checkType(TypeDef type) { if (!new FieldTypes(type).exactly(requiredFields)) return null; if (DotNetUtils.getMethod(type, ".cctor") == null) @@ -53,8 +53,8 @@ namespace de4dot.code.deobfuscators.dotNET_Reactor.v4 { return checkMethods(type); } - static MethodDefinition checkMethods(TypeDefinition type) { - MethodDefinition creatorMethod = null; + static MethodDef checkMethods(TypeDef type) { + MethodDef creatorMethod = null; foreach (var method in type.Methods) { if (method.Body == null) return null; @@ -71,7 +71,7 @@ namespace de4dot.code.deobfuscators.dotNET_Reactor.v4 { return creatorMethod; } - protected override object checkCctor(ref TypeDefinition type, MethodDefinition cctor) { + protected override object checkCctor(ref TypeDef type, MethodDef cctor) { simpleDeobfuscator.deobfuscate(cctor); var realType = getDelegateType(cctor); if (realType == null) @@ -81,7 +81,7 @@ namespace de4dot.code.deobfuscators.dotNET_Reactor.v4 { return this; } - TypeDefinition getDelegateType(MethodDefinition method) { + TypeDef getDelegateType(MethodDef method) { var instrs = method.Body.Instructions; for (int i = 0; i < instrs.Count - 1; i++) { var ldci4 = instrs[i]; @@ -91,17 +91,17 @@ namespace de4dot.code.deobfuscators.dotNET_Reactor.v4 { var call = instrs[i + 1]; if (call.OpCode.Code != Code.Call) continue; - var calledMethod = call.Operand as MethodDefinition; + var calledMethod = call.Operand as MethodDef; if (calledMethod == null || !isDelegateCreatorMethod(calledMethod)) continue; - return module.LookupToken(0x02000000 + DotNetUtils.getLdcI4Value(ldci4)) as TypeDefinition; + return module.LookupToken(0x02000000 + DotNetUtils.getLdcI4Value(ldci4)) as TypeDef; } return null; } - protected override void getCallInfo(object context, FieldDefinition field, out MethodReference calledMethod, out OpCode callOpcode) { - calledMethod = module.LookupToken(0x06000000 + field.MetadataToken.ToInt32()) as MethodReference; + protected override void getCallInfo(object context, FieldDef field, out MethodReference calledMethod, out OpCode callOpcode) { + calledMethod = module.LookupToken(0x06000000 + field.MDToken.ToInt32()) as MethodReference; callOpcode = OpCodes.Call; } } diff --git a/de4dot.code/deobfuscators/dotNET_Reactor/v4/ResourceResolver.cs b/de4dot.code/deobfuscators/dotNET_Reactor/v4/ResourceResolver.cs index 42d05725..f1781a93 100644 --- a/de4dot.code/deobfuscators/dotNET_Reactor/v4/ResourceResolver.cs +++ b/de4dot.code/deobfuscators/dotNET_Reactor/v4/ResourceResolver.cs @@ -19,25 +19,25 @@ using System; using System.Collections.Generic; -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using de4dot.blocks; namespace de4dot.code.deobfuscators.dotNET_Reactor.v4 { class ResourceResolver { ModuleDefinition module; EncryptedResource encryptedResource; - MethodDefinition initMethod; + MethodDef initMethod; public bool Detected { get { return encryptedResource.Method != null; } } - public TypeDefinition Type { + public TypeDef Type { get { return encryptedResource.Type; } } - public MethodDefinition InitMethod { + public MethodDef InitMethod { get { return initMethod; } } @@ -79,7 +79,7 @@ namespace de4dot.code.deobfuscators.dotNET_Reactor.v4 { } } - bool checkFields(IList fields) { + bool checkFields(IList fields) { if (fields.Count != 3) return false; @@ -105,7 +105,7 @@ namespace de4dot.code.deobfuscators.dotNET_Reactor.v4 { encryptedResource.init(simpleDeobfuscator); } - MethodDefinition findInitMethod(ISimpleDeobfuscator simpleDeobfuscator) { + MethodDef findInitMethod(ISimpleDeobfuscator simpleDeobfuscator) { var ctor = DotNetUtils.getMethod(Type, ".ctor"); foreach (var method in Type.Methods) { if (!method.IsStatic || method.Body == null) diff --git a/de4dot.code/deobfuscators/dotNET_Reactor/v4/StringDecrypter.cs b/de4dot.code/deobfuscators/dotNET_Reactor/v4/StringDecrypter.cs index 50a4269c..60491a56 100644 --- a/de4dot.code/deobfuscators/dotNET_Reactor/v4/StringDecrypter.cs +++ b/de4dot.code/deobfuscators/dotNET_Reactor/v4/StringDecrypter.cs @@ -20,8 +20,8 @@ using System; using System.Text; using System.Collections.Generic; -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using de4dot.blocks; using de4dot.PE; @@ -30,7 +30,7 @@ namespace de4dot.code.deobfuscators.dotNET_Reactor.v4 { ModuleDefinition module; EncryptedResource encryptedResource; List decrypterInfos = new List(); - MethodDefinition otherStringDecrypter; + MethodDef otherStringDecrypter; byte[] decryptedData; PeImage peImage; byte[] fileData; @@ -43,11 +43,11 @@ namespace de4dot.code.deobfuscators.dotNET_Reactor.v4 { } public class DecrypterInfo { - public MethodDefinition method; + public MethodDef method; public byte[] key; public byte[] iv; - public DecrypterInfo(MethodDefinition method, byte[] key, byte[] iv) { + public DecrypterInfo(MethodDef method, byte[] key, byte[] iv) { this.method = method; this.key = key; this.iv = iv; @@ -58,7 +58,7 @@ namespace de4dot.code.deobfuscators.dotNET_Reactor.v4 { get { return encryptedResource.Method != null; } } - public TypeDefinition DecrypterType { + public TypeDef DecrypterType { get { return encryptedResource.Type; } } @@ -70,7 +70,7 @@ namespace de4dot.code.deobfuscators.dotNET_Reactor.v4 { get { return decrypterInfos; } } - public MethodDefinition OtherStringDecrypter { + public MethodDef OtherStringDecrypter { get { return otherStringDecrypter; } } @@ -133,7 +133,7 @@ namespace de4dot.code.deobfuscators.dotNET_Reactor.v4 { findOtherStringDecrypter(decrypterInfos[0].method.DeclaringType); } - void findOtherStringDecrypter(TypeDefinition type) { + void findOtherStringDecrypter(TypeDef type) { foreach (var method in type.Methods) { if (!method.IsStatic || !method.HasBody) continue; @@ -163,7 +163,7 @@ namespace de4dot.code.deobfuscators.dotNET_Reactor.v4 { decryptedData = encryptedResource.decrypt(); } - void findKeyIv(MethodDefinition method, out byte[] key, out byte[] iv) { + void findKeyIv(MethodDef method, out byte[] key, out byte[] iv) { key = null; iv = null; @@ -188,7 +188,7 @@ namespace de4dot.code.deobfuscators.dotNET_Reactor.v4 { var instr = instructions[i]; if (instr.OpCode.Code != Code.Ldtoken) continue; - var field = instr.Operand as FieldDefinition; + var field = instr.Operand as FieldDef; if (field == null) continue; if (field.InitialValue == null) @@ -208,7 +208,7 @@ namespace de4dot.code.deobfuscators.dotNET_Reactor.v4 { } } - void initializeStringDecrypterVersion(MethodDefinition method) { + void initializeStringDecrypterVersion(MethodDef method) { var localTypes = new LocalTypes(method); if (localTypes.exists("System.IntPtr")) stringDecrypterVersion = StringDecrypterVersion.VER_38; @@ -216,7 +216,7 @@ namespace de4dot.code.deobfuscators.dotNET_Reactor.v4 { stringDecrypterVersion = StringDecrypterVersion.VER_37; } - DecrypterInfo getDecrypterInfo(MethodDefinition method) { + DecrypterInfo getDecrypterInfo(MethodDef method) { foreach (var info in decrypterInfos) { if (info.method == method) return info; @@ -224,7 +224,7 @@ namespace de4dot.code.deobfuscators.dotNET_Reactor.v4 { throw new ApplicationException("Invalid string decrypter method"); } - public string decrypt(MethodDefinition method, int offset) { + public string decrypt(MethodDef method, int offset) { var info = getDecrypterInfo(method); if (info.key == null) { diff --git a/de4dot.code/renamer/DerivedFrom.cs b/de4dot.code/renamer/DerivedFrom.cs index bd3889b2..ef91d8b7 100644 --- a/de4dot.code/renamer/DerivedFrom.cs +++ b/de4dot.code/renamer/DerivedFrom.cs @@ -24,7 +24,7 @@ using de4dot.code.renamer.asmmodules; namespace de4dot.code.renamer { class DerivedFrom { Dictionary classNames = new Dictionary(StringComparer.Ordinal); - Dictionary results = new Dictionary(); + Dictionary results = new Dictionary(); public DerivedFrom(string className) { addName(className); @@ -39,16 +39,16 @@ namespace de4dot.code.renamer { classNames[className] = true; } - public bool check(TypeDef type) { + public bool check(MTypeDef type) { if (results.ContainsKey(type)) return results[type]; bool val; - if (classNames.ContainsKey(type.TypeDefinition.FullName)) + if (classNames.ContainsKey(type.TypeDef.FullName)) val = true; else if (type.baseType == null) { - if (type.TypeDefinition.BaseType != null) - val = classNames.ContainsKey(type.TypeDefinition.BaseType.FullName); + if (type.TypeDef.BaseType != null) + val = classNames.ContainsKey(type.TypeDef.BaseType.FullName); else val = false; } diff --git a/de4dot.code/renamer/MemberInfos.cs b/de4dot.code/renamer/MemberInfos.cs index 6fe69523..c3e29004 100644 --- a/de4dot.code/renamer/MemberInfos.cs +++ b/de4dot.code/renamer/MemberInfos.cs @@ -51,45 +51,45 @@ namespace de4dot.code.renamer { } class GenericParamInfo : MemberInfo { - public GenericParamInfo(GenericParamDef genericParamDef) + public GenericParamInfo(MGenericParamDef genericParamDef) : base(genericParamDef) { } } class PropertyInfo : MemberInfo { - public PropertyInfo(PropertyDef propertyDef) + public PropertyInfo(MPropertyDef propertyDef) : base(propertyDef) { } } class EventInfo : MemberInfo { - public EventInfo(EventDef eventDef) + public EventInfo(MEventDef eventDef) : base(eventDef) { } } class FieldInfo : MemberInfo { - public FieldInfo(FieldDef fieldDef) + public FieldInfo(MFieldDef fieldDef) : base(fieldDef) { } } class MethodInfo : MemberInfo { - public MethodDef MethodDef { - get { return (MethodDef)memberRef; } + public MMethodDef MethodDef { + get { return (MMethodDef)memberRef; } } - public MethodInfo(MethodDef methodDef) + public MethodInfo(MMethodDef methodDef) : base(methodDef) { } } class ParamInfo { - ParamDef paramDef; + MParamDef paramDef; public string oldName; public string newName; - public ParamInfo(ParamDef paramDef) { + public ParamInfo(MParamDef paramDef) { this.paramDef = paramDef; this.oldName = paramDef.ParameterDefinition.Name; this.newName = paramDef.ParameterDefinition.Name; @@ -101,13 +101,13 @@ namespace de4dot.code.renamer { } class MemberInfos { - Dictionary allTypeInfos = new Dictionary(); - Dictionary allPropertyInfos = new Dictionary(); - Dictionary allEventInfos = new Dictionary(); - Dictionary allFieldInfos = new Dictionary(); - Dictionary allMethodInfos = new Dictionary(); - Dictionary allGenericParamInfos = new Dictionary(); - Dictionary allParamInfos = new Dictionary(); + Dictionary allTypeInfos = new Dictionary(); + Dictionary allPropertyInfos = new Dictionary(); + Dictionary allEventInfos = new Dictionary(); + Dictionary allFieldInfos = new Dictionary(); + Dictionary allMethodInfos = new Dictionary(); + Dictionary allGenericParamInfos = new Dictionary(); + Dictionary allParamInfos = new Dictionary(); DerivedFrom checkWinFormsClass; static string[] WINFORMS_CLASSES = new string[] { @@ -210,55 +210,55 @@ namespace de4dot.code.renamer { checkWinFormsClass = new DerivedFrom(WINFORMS_CLASSES); } - public bool isWinFormsClass(TypeDef type) { + public bool isWinFormsClass(MTypeDef type) { return checkWinFormsClass.check(type); } - public TypeInfo type(TypeDef t) { + public TypeInfo type(MTypeDef t) { return allTypeInfos[t]; } - public bool tryGetType(TypeDef t, out TypeInfo info) { + public bool tryGetType(MTypeDef t, out TypeInfo info) { return allTypeInfos.TryGetValue(t, out info); } - public bool tryGetEvent(EventDef e, out EventInfo info) { + public bool tryGetEvent(MEventDef e, out EventInfo info) { return allEventInfos.TryGetValue(e, out info); } - public bool tryGetProperty(PropertyDef p, out PropertyInfo info) { + public bool tryGetProperty(MPropertyDef p, out PropertyInfo info) { return allPropertyInfos.TryGetValue(p, out info); } - public PropertyInfo prop(PropertyDef prop) { + public PropertyInfo prop(MPropertyDef prop) { return allPropertyInfos[prop]; } - public EventInfo evt(EventDef evt) { + public EventInfo evt(MEventDef evt) { return allEventInfos[evt]; } - public FieldInfo field(FieldDef field) { + public FieldInfo field(MFieldDef field) { return allFieldInfos[field]; } - public MethodInfo method(MethodDef method) { + public MethodInfo method(MMethodDef method) { return allMethodInfos[method]; } - public GenericParamInfo gparam(GenericParamDef gparam) { + public GenericParamInfo gparam(MGenericParamDef gparam) { return allGenericParamInfos[gparam]; } - public ParamInfo param(ParamDef param) { + public ParamInfo param(MParamDef param) { return allParamInfos[param]; } - public void add(PropertyDef prop) { + public void add(MPropertyDef prop) { allPropertyInfos[prop] = new PropertyInfo(prop); } - public void add(EventDef evt) { + public void add(MEventDef evt) { allEventInfos[evt] = new EventInfo(evt); } diff --git a/de4dot.code/renamer/NameCreators.cs b/de4dot.code/renamer/NameCreators.cs index 73e49caf..72441866 100644 --- a/de4dot.code/renamer/NameCreators.cs +++ b/de4dot.code/renamer/NameCreators.cs @@ -18,7 +18,7 @@ */ using System.Collections.Generic; -using Mono.Cecil; +using dot10.DotNet; using de4dot.blocks; namespace de4dot.code.renamer { @@ -107,7 +107,7 @@ namespace de4dot.code.renamer { } interface ITypeNameCreator { - string create(TypeDefinition typeDefinition, string newBaseTypeName); + string create(TypeDef typeDefinition, string newBaseTypeName); } class NameInfos { @@ -172,12 +172,12 @@ namespace de4dot.code.renamer { return new NameCreator(prefix); } - public string create(TypeDefinition typeDefinition, string newBaseTypeName) { + public string create(TypeDef typeDefinition, string newBaseTypeName) { var nameCreator = getNameCreator(typeDefinition, newBaseTypeName); return existingNames.getName(typeDefinition.Name, nameCreator); } - NameCreator getNameCreator(TypeDefinition typeDefinition, string newBaseTypeName) { + NameCreator getNameCreator(TypeDef typeDefinition, string newBaseTypeName) { var nameCreator = createUnknownTypeName; if (typeDefinition.IsEnum) nameCreator = createEnumName; diff --git a/de4dot.code/renamer/Renamer.cs b/de4dot.code/renamer/Renamer.cs index 663c75f6..9d04d2d1 100644 --- a/de4dot.code/renamer/Renamer.cs +++ b/de4dot.code/renamer/Renamer.cs @@ -20,8 +20,8 @@ using System; using System.Collections.Generic; using System.Text.RegularExpressions; -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using de4dot.code.renamer.asmmodules; using de4dot.blocks; @@ -108,15 +108,15 @@ namespace de4dot.code.renamer { continue; if (!method.isPublic()) continue; - var overrides = method.MethodDefinition.Overrides; + var overrides = method.MethodDef.Overrides; for (int i = 0; i < overrides.Count; i++) { var overrideMethod = overrides[i]; - if (method.MethodDefinition.Name != overrideMethod.Name) + if (method.MethodDef.Name != overrideMethod.Name) continue; Log.v("Removed useless override from method {0} ({1:X8}), override: {2:X8}", - Utils.removeNewlines(method.MethodDefinition), - method.MethodDefinition.MetadataToken.ToInt32(), - overrideMethod.MetadataToken.ToInt32()); + Utils.removeNewlines(method.MethodDef), + method.MethodDef.MDToken.ToInt32(), + overrideMethod.MDToken.ToInt32()); overrides.RemoveAt(i); i--; } @@ -141,36 +141,36 @@ namespace de4dot.code.renamer { } void removeOneClassNamespaces(Module module) { - var nsToTypes = new Dictionary>(StringComparer.Ordinal); + var nsToTypes = new Dictionary>(StringComparer.Ordinal); foreach (var typeDef in module.getAllTypes()) { - List list; - var ns = typeDef.TypeDefinition.Namespace; + List list; + var ns = typeDef.TypeDef.Namespace; if (string.IsNullOrEmpty(ns)) continue; if (module.ObfuscatedFile.NameChecker.isValidNamespaceName(ns)) continue; if (!nsToTypes.TryGetValue(ns, out list)) - nsToTypes[ns] = list = new List(); + nsToTypes[ns] = list = new List(); list.Add(typeDef); } - var sortedNamespaces = new List>(nsToTypes.Values); + var sortedNamespaces = new List>(nsToTypes.Values); sortedNamespaces.Sort((a, b) => { - return string.CompareOrdinal(a[0].TypeDefinition.Namespace, b[0].TypeDefinition.Namespace); + return string.CompareOrdinal(a[0].TypeDef.Namespace, b[0].TypeDef.Namespace); }); foreach (var list in sortedNamespaces) { const int maxClasses = 1; if (list.Count != maxClasses) continue; - var ns = list[0].TypeDefinition.Namespace; + var ns = list[0].TypeDef.Namespace; Log.v("Removing namespace: {0}", Utils.removeNewlines(ns)); foreach (var type in list) memberInfos.type(type).newNamespace = ""; } } - void renameTypeDefinitions(IEnumerable typeDefs) { + void renameTypeDefinitions(IEnumerable typeDefs) { Log.indent(); foreach (var typeDef in typeDefs) { rename(typeDef); @@ -179,11 +179,11 @@ namespace de4dot.code.renamer { Log.deIndent(); } - void rename(TypeDef type) { - var typeDefinition = type.TypeDefinition; + void rename(MTypeDef type) { + var typeDefinition = type.TypeDef; var info = memberInfos.type(type); - Log.v("Type: {0} ({1:X8})", Utils.removeNewlines(typeDefinition.FullName), typeDefinition.MetadataToken.ToUInt32()); + Log.v("Type: {0} ({1:X8})", Utils.removeNewlines(typeDefinition.FullName), typeDefinition.MDToken.ToUInt32()); Log.indent(); renameGenericParams(type.GenericParams); @@ -203,22 +203,22 @@ namespace de4dot.code.renamer { Log.deIndent(); } - void renameGenericParams(IEnumerable genericParams) { + void renameGenericParams(IEnumerable genericParams) { if (!RenameGenericParams) return; foreach (var param in genericParams) { var info = memberInfos.gparam(param); if (!info.gotNewName()) continue; - param.GenericParameter.Name = info.newName; - Log.v("GenParam: {0} => {1}", Utils.removeNewlines(info.oldFullName), Utils.removeNewlines(param.GenericParameter.FullName)); + param.GenericParam.Name = info.newName; + Log.v("GenParam: {0} => {1}", Utils.removeNewlines(info.oldFullName), Utils.removeNewlines(param.GenericParam.FullName)); } } void renameMemberDefinitions() { Log.v("Renaming member definitions #2"); - var allTypes = new List(modules.AllTypes); + var allTypes = new List(modules.AllTypes); allTypes.Sort((a, b) => Utils.compareInt32(a.Index, b.Index)); Log.indent(); @@ -227,10 +227,10 @@ namespace de4dot.code.renamer { Log.deIndent(); } - void renameMembers(TypeDef type) { + void renameMembers(MTypeDef type) { var info = memberInfos.type(type); - Log.v("Type: {0}", Utils.removeNewlines(info.type.TypeDefinition.FullName)); + Log.v("Type: {0}", Utils.removeNewlines(info.type.TypeDef.FullName)); Log.indent(); renameFields(info); @@ -248,11 +248,11 @@ namespace de4dot.code.renamer { var fieldInfo = memberInfos.field(fieldDef); if (!fieldInfo.gotNewName()) continue; - fieldDef.FieldDefinition.Name = fieldInfo.newName; + fieldDef.FieldDef.Name = fieldInfo.newName; Log.v("Field: {0} ({1:X8}) => {2}", Utils.removeNewlines(fieldInfo.oldFullName), - fieldDef.FieldDefinition.MetadataToken.ToUInt32(), - Utils.removeNewlines(fieldDef.FieldDefinition.FullName)); + fieldDef.FieldDef.MDToken.ToUInt32(), + Utils.removeNewlines(fieldDef.FieldDef.FullName)); } } @@ -263,11 +263,11 @@ namespace de4dot.code.renamer { var propInfo = memberInfos.prop(propDef); if (!propInfo.gotNewName()) continue; - propDef.PropertyDefinition.Name = propInfo.newName; + propDef.PropertyDef.Name = propInfo.newName; Log.v("Property: {0} ({1:X8}) => {2}", Utils.removeNewlines(propInfo.oldFullName), - propDef.PropertyDefinition.MetadataToken.ToUInt32(), - Utils.removeNewlines(propDef.PropertyDefinition.FullName)); + propDef.PropertyDef.MDToken.ToUInt32(), + Utils.removeNewlines(propDef.PropertyDef.FullName)); } } @@ -278,11 +278,11 @@ namespace de4dot.code.renamer { var eventInfo = memberInfos.evt(eventDef); if (!eventInfo.gotNewName()) continue; - eventDef.EventDefinition.Name = eventInfo.newName; + eventDef.EventDef.Name = eventInfo.newName; Log.v("Event: {0} ({1:X8}) => {2}", Utils.removeNewlines(eventInfo.oldFullName), - eventDef.EventDefinition.MetadataToken.ToUInt32(), - Utils.removeNewlines(eventDef.EventDefinition.FullName)); + eventDef.EventDef.MDToken.ToUInt32(), + Utils.removeNewlines(eventDef.EventDef.FullName)); } } @@ -291,14 +291,14 @@ namespace de4dot.code.renamer { return; foreach (var methodDef in info.type.AllMethodsSorted) { var methodInfo = memberInfos.method(methodDef); - Log.v("Method {0} ({1:X8})", Utils.removeNewlines(methodInfo.oldFullName), methodDef.MethodDefinition.MetadataToken.ToUInt32()); + Log.v("Method {0} ({1:X8})", Utils.removeNewlines(methodInfo.oldFullName), methodDef.MethodDef.MDToken.ToUInt32()); Log.indent(); renameGenericParams(methodDef.GenericParams); if (RenameMethods && methodInfo.gotNewName()) { - methodDef.MethodDefinition.Name = methodInfo.newName; - Log.v("Name: {0} => {1}", Utils.removeNewlines(methodInfo.oldFullName), Utils.removeNewlines(methodDef.MethodDefinition.FullName)); + methodDef.MethodDef.Name = methodInfo.newName; + Log.v("Name: {0} => {1}", Utils.removeNewlines(methodInfo.oldFullName), Utils.removeNewlines(methodDef.MethodDef.FullName)); } if (RenameMethodArgs) { @@ -352,7 +352,7 @@ namespace de4dot.code.renamer { var renamedTypes = new List(); foreach (var type in module.getAllTypes()) { var info = memberInfos.type(type); - if (info.oldFullName != info.type.TypeDefinition.FullName) + if (info.oldFullName != info.type.TypeDef.FullName) renamedTypes.Add(info); } if (renamedTypes.Count == 0) @@ -369,7 +369,7 @@ namespace de4dot.code.renamer { fixClsTypeNames(null, type); } - void fixClsTypeNames(TypeDef nesting, TypeDef nested) { + void fixClsTypeNames(MTypeDef nesting, MTypeDef nested) { int nestingCount = nesting == null ? 0 : nesting.GenericParams.Count; int arity = nested.GenericParams.Count - nestingCount; var nestedInfo = memberInfos.type(nested); @@ -379,7 +379,7 @@ namespace de4dot.code.renamer { fixClsTypeNames(nested, nestedType); } - void prepareRenameTypes(IEnumerable types, TypeRenamerState state) { + void prepareRenameTypes(IEnumerable types, TypeRenamerState state) { foreach (var typeDef in types) { memberInfos.type(typeDef).prepareRenameTypes(state); prepareRenameTypes(typeDef.derivedTypes, state); @@ -415,7 +415,7 @@ namespace de4dot.code.renamer { if (!this.RenameProperties) return; foreach (var group in allGroups) { - PropertyDef prop = null; + MPropertyDef prop = null; foreach (var method in group.Methods) { if (method.Property == null) continue; @@ -431,7 +431,7 @@ namespace de4dot.code.renamer { continue; if (method.Property == null) continue; - memberInfos.prop(method.Property).rename(prop.PropertyDefinition.Name); + memberInfos.prop(method.Property).rename(prop.PropertyDef.Name); } } } @@ -440,7 +440,7 @@ namespace de4dot.code.renamer { if (!this.RenameEvents) return; foreach (var group in allGroups) { - EventDef evt = null; + MEventDef evt = null; foreach (var method in group.Methods) { if (method.Event == null) continue; @@ -456,7 +456,7 @@ namespace de4dot.code.renamer { continue; if (method.Event == null) continue; - memberInfos.evt(method.Event).rename(evt.EventDefinition.Name); + memberInfos.evt(method.Event).rename(evt.EventDef.Name); } } } @@ -476,7 +476,7 @@ namespace de4dot.code.renamer { var propMethod = group.Methods[0]; if (propMethod.Property != null) return; - if (propMethod.MethodDefinition.Overrides.Count == 0) + if (propMethod.MethodDef.Overrides.Count == 0) return; var theProperty = getOverriddenProperty(propMethod); @@ -490,12 +490,12 @@ namespace de4dot.code.renamer { if (group.Methods.Count <= 1 || !group.hasProperty()) return; - PropertyDef prop = null; - List missingProps = null; + MPropertyDef prop = null; + List missingProps = null; foreach (var method in group.Methods) { if (method.Property == null) { if (missingProps == null) - missingProps = new List(); + missingProps = new List(); missingProps.Add(method); } else if (prop == null || !method.Owner.HasModule) @@ -510,12 +510,12 @@ namespace de4dot.code.renamer { createProperty(prop, method, ""); } - void createProperty(PropertyDef propDef, MethodDef methodDef, string overridePrefix) { + void createProperty(MPropertyDef propDef, MMethodDef methodDef, string overridePrefix) { if (!methodDef.Owner.HasModule) return; - var newPropertyName = overridePrefix + propDef.PropertyDefinition.Name; - if (!DotNetUtils.hasReturnValue(methodDef.MethodDefinition)) + var newPropertyName = overridePrefix + propDef.PropertyDef.Name; + if (!DotNetUtils.hasReturnValue(methodDef.MethodDef)) createPropertySetter(newPropertyName, methodDef); else createPropertyGetter(newPropertyName, methodDef); @@ -527,7 +527,7 @@ namespace de4dot.code.renamer { foreach (var group in allGroups) { var groupMethod = group.Methods[0]; - var methodName = groupMethod.MethodDefinition.Name; + var methodName = groupMethod.MethodDef.Name; bool onlyRenamableMethods = !group.hasNonRenamableMethod(); if (Utils.StartsWith(methodName, "get_", StringComparison.Ordinal)) { @@ -554,7 +554,7 @@ namespace de4dot.code.renamer { continue; // Virtual methods are in allGroups, so already fixed above if (method.Property != null) continue; - var methodName = method.MethodDefinition.Name; + var methodName = method.MethodDef.Name; if (Utils.StartsWith(methodName, "get_", StringComparison.Ordinal)) createPropertyGetter(methodName.Substring(4), method); else if (Utils.StartsWith(methodName, "set_", StringComparison.Ordinal)) @@ -563,7 +563,7 @@ namespace de4dot.code.renamer { } } - PropertyDef createPropertyGetter(string name, MethodDef propMethod) { + MPropertyDef createPropertyGetter(string name, MMethodDef propMethod) { if (string.IsNullOrEmpty(name)) return null; var ownerType = propMethod.Owner; @@ -572,25 +572,25 @@ namespace de4dot.code.renamer { if (propMethod.Property != null) return null; - var method = propMethod.MethodDefinition; + var method = propMethod.MethodDef; var propType = method.MethodReturnType.ReturnType; - var propDef = createProperty(ownerType, name, propType, propMethod.MethodDefinition, null); + var propDef = createProperty(ownerType, name, propType, propMethod.MethodDef, null); if (propDef == null) return null; if (propDef.GetMethod != null) return null; Log.v("Restoring property getter {0} ({1:X8}), Property: {2} ({3:X8})", Utils.removeNewlines(propMethod), - propMethod.MethodDefinition.MetadataToken.ToInt32(), - Utils.removeNewlines(propDef.PropertyDefinition), - propDef.PropertyDefinition.MetadataToken.ToInt32()); - propDef.PropertyDefinition.GetMethod = propMethod.MethodDefinition; + propMethod.MethodDef.MDToken.ToInt32(), + Utils.removeNewlines(propDef.PropertyDef), + propDef.PropertyDef.MDToken.ToInt32()); + propDef.PropertyDef.GetMethod = propMethod.MethodDef; propDef.GetMethod = propMethod; propMethod.Property = propDef; return propDef; } - PropertyDef createPropertySetter(string name, MethodDef propMethod) { + MPropertyDef createPropertySetter(string name, MMethodDef propMethod) { if (string.IsNullOrEmpty(name)) return null; var ownerType = propMethod.Owner; @@ -599,27 +599,27 @@ namespace de4dot.code.renamer { if (propMethod.Property != null) return null; - var method = propMethod.MethodDefinition; + var method = propMethod.MethodDef; if (method.Parameters.Count == 0) return null; var propType = method.Parameters[method.Parameters.Count - 1].ParameterType; - var propDef = createProperty(ownerType, name, propType, null, propMethod.MethodDefinition); + var propDef = createProperty(ownerType, name, propType, null, propMethod.MethodDef); if (propDef == null) return null; if (propDef.SetMethod != null) return null; Log.v("Restoring property setter {0} ({1:X8}), Property: {2} ({3:X8})", Utils.removeNewlines(propMethod), - propMethod.MethodDefinition.MetadataToken.ToInt32(), - Utils.removeNewlines(propDef.PropertyDefinition), - propDef.PropertyDefinition.MetadataToken.ToInt32()); - propDef.PropertyDefinition.SetMethod = propMethod.MethodDefinition; + propMethod.MethodDef.MDToken.ToInt32(), + Utils.removeNewlines(propDef.PropertyDef), + propDef.PropertyDef.MDToken.ToInt32()); + propDef.PropertyDef.SetMethod = propMethod.MethodDef; propDef.SetMethod = propMethod; propMethod.Property = propDef; return propDef; } - PropertyDef createProperty(TypeDef ownerType, string name, TypeReference propType, MethodDefinition getter, MethodDefinition setter) { + MPropertyDef createProperty(MTypeDef ownerType, string name, TypeReference propType, MethodDef getter, MethodDef setter) { if (string.IsNullOrEmpty(name) || propType.FullName == "System.Void") return null; var newProp = DotNetUtils.createPropertyDefinition(name, propType, getter, setter); @@ -656,10 +656,10 @@ namespace de4dot.code.renamer { var eventMethod = group.Methods[0]; if (eventMethod.Event != null) return; - if (eventMethod.MethodDefinition.Overrides.Count == 0) + if (eventMethod.MethodDef.Overrides.Count == 0) return; - MethodDef overriddenMethod; + MMethodDef overriddenMethod; var theEvent = getOverriddenEvent(eventMethod, out overriddenMethod); if (theEvent == null) return; @@ -672,12 +672,12 @@ namespace de4dot.code.renamer { return; EventMethodType methodType = EventMethodType.None; - EventDef evt = null; - List missingEvents = null; + MEventDef evt = null; + List missingEvents = null; foreach (var method in group.Methods) { if (method.Event == null) { if (missingEvents == null) - missingEvents = new List(); + missingEvents = new List(); missingEvents.Add(method); } else if (evt == null || !method.Owner.HasModule) { @@ -694,11 +694,11 @@ namespace de4dot.code.renamer { createEvent(evt, method, methodType, ""); } - void createEvent(EventDef eventDef, MethodDef methodDef, EventMethodType methodType, string overridePrefix) { + void createEvent(MEventDef eventDef, MMethodDef methodDef, EventMethodType methodType, string overridePrefix) { if (!methodDef.Owner.HasModule) return; - var newEventName = overridePrefix + eventDef.EventDefinition.Name; + var newEventName = overridePrefix + eventDef.EventDef.Name; switch (methodType) { case EventMethodType.Adder: createEventAdder(newEventName, methodDef); @@ -709,7 +709,7 @@ namespace de4dot.code.renamer { } } - static EventMethodType getEventMethodType(MethodDef method) { + static EventMethodType getEventMethodType(MMethodDef method) { var evt = method.Event; if (evt == null) return EventMethodType.None; @@ -728,7 +728,7 @@ namespace de4dot.code.renamer { foreach (var group in allGroups) { var groupMethod = group.Methods[0]; - var methodName = groupMethod.MethodDefinition.Name; + var methodName = groupMethod.MethodDef.Name; bool onlyRenamableMethods = !group.hasNonRenamableMethod(); if (Utils.StartsWith(methodName, "add_", StringComparison.Ordinal)) { @@ -755,7 +755,7 @@ namespace de4dot.code.renamer { continue; // Virtual methods are in allGroups, so already fixed above if (method.Event != null) continue; - var methodName = method.MethodDefinition.Name; + var methodName = method.MethodDef.Name; if (Utils.StartsWith(methodName, "add_", StringComparison.Ordinal)) createEventAdder(methodName.Substring(4), method); else if (Utils.StartsWith(methodName, "remove_", StringComparison.Ordinal)) @@ -764,7 +764,7 @@ namespace de4dot.code.renamer { } } - EventDef createEventAdder(string name, MethodDef eventMethod) { + MEventDef createEventAdder(string name, MMethodDef eventMethod) { if (string.IsNullOrEmpty(name)) return null; var ownerType = eventMethod.Owner; @@ -773,7 +773,7 @@ namespace de4dot.code.renamer { if (eventMethod.Event != null) return null; - var method = eventMethod.MethodDefinition; + var method = eventMethod.MethodDef; var eventDef = createEvent(ownerType, name, getEventType(method)); if (eventDef == null) return null; @@ -781,16 +781,16 @@ namespace de4dot.code.renamer { return null; Log.v("Restoring event adder {0} ({1:X8}), Event: {2} ({3:X8})", Utils.removeNewlines(eventMethod), - eventMethod.MethodDefinition.MetadataToken.ToInt32(), - Utils.removeNewlines(eventDef.EventDefinition), - eventDef.EventDefinition.MetadataToken.ToInt32()); - eventDef.EventDefinition.AddMethod = eventMethod.MethodDefinition; + eventMethod.MethodDef.MDToken.ToInt32(), + Utils.removeNewlines(eventDef.EventDef), + eventDef.EventDef.MDToken.ToInt32()); + eventDef.EventDef.AddMethod = eventMethod.MethodDef; eventDef.AddMethod = eventMethod; eventMethod.Event = eventDef; return eventDef; } - EventDef createEventRemover(string name, MethodDef eventMethod) { + MEventDef createEventRemover(string name, MMethodDef eventMethod) { if (string.IsNullOrEmpty(name)) return null; var ownerType = eventMethod.Owner; @@ -799,7 +799,7 @@ namespace de4dot.code.renamer { if (eventMethod.Event != null) return null; - var method = eventMethod.MethodDefinition; + var method = eventMethod.MethodDef; var eventDef = createEvent(ownerType, name, getEventType(method)); if (eventDef == null) return null; @@ -807,10 +807,10 @@ namespace de4dot.code.renamer { return null; Log.v("Restoring event remover {0} ({1:X8}), Event: {2} ({3:X8})", Utils.removeNewlines(eventMethod), - eventMethod.MethodDefinition.MetadataToken.ToInt32(), - Utils.removeNewlines(eventDef.EventDefinition), - eventDef.EventDefinition.MetadataToken.ToInt32()); - eventDef.EventDefinition.RemoveMethod = eventMethod.MethodDefinition; + eventMethod.MethodDef.MDToken.ToInt32(), + Utils.removeNewlines(eventDef.EventDef), + eventDef.EventDef.MDToken.ToInt32()); + eventDef.EventDef.RemoveMethod = eventMethod.MethodDef; eventDef.RemoveMethod = eventMethod; eventMethod.Event = eventDef; return eventDef; @@ -824,7 +824,7 @@ namespace de4dot.code.renamer { return method.Parameters[0].ParameterType; } - EventDef createEvent(TypeDef ownerType, string name, TypeReference eventType) { + MEventDef createEvent(MTypeDef ownerType, string name, TypeReference eventType) { if (string.IsNullOrEmpty(name) || eventType == null || eventType.FullName == "System.Void") return null; var newEvent = DotNetUtils.createEventDefinition(name, eventType); @@ -910,9 +910,9 @@ namespace de4dot.code.renamer { } string[] getValidArgNames(MethodNameGroup group) { - var methods = new List(group.Methods); + var methods = new List(group.Methods); foreach (var method in group.Methods) { - foreach (var overrideRef in method.MethodDefinition.Overrides) { + foreach (var overrideRef in method.MethodDef.Overrides) { var overrideDef = modules.resolve(overrideRef); if (overrideDef == null) { var typeDef = modules.resolve(overrideRef.DeclaringType) ?? modules.resolveOther(overrideRef.DeclaringType); @@ -941,12 +941,12 @@ namespace de4dot.code.renamer { } class PrepareHelper { - Dictionary prepareMethodCalled = new Dictionary(); + Dictionary prepareMethodCalled = new Dictionary(); MemberInfos memberInfos; Action func; - IEnumerable allTypes; + IEnumerable allTypes; - public PrepareHelper(MemberInfos memberInfos, IEnumerable allTypes) { + public PrepareHelper(MemberInfos memberInfos, IEnumerable allTypes) { this.memberInfos = memberInfos; this.allTypes = allTypes; } @@ -958,7 +958,7 @@ namespace de4dot.code.renamer { prepare(typeDef); } - void prepare(TypeDef type) { + void prepare(MTypeDef type) { if (prepareMethodCalled.ContainsKey(type)) return; prepareMethodCalled[type] = true; @@ -982,13 +982,13 @@ namespace de4dot.code.renamer { class GroupHelper { MemberInfos memberInfos; - Dictionary visited = new Dictionary(); - Dictionary methodToGroup; + Dictionary visited = new Dictionary(); + Dictionary methodToGroup; List groups = new List(); - IEnumerable allTypes; + IEnumerable allTypes; Action func; - public GroupHelper(MemberInfos memberInfos, IEnumerable allTypes) { + public GroupHelper(MemberInfos memberInfos, IEnumerable allTypes) { this.memberInfos = memberInfos; this.allTypes = allTypes; } @@ -1001,7 +1001,7 @@ namespace de4dot.code.renamer { this.func = func; visited.Clear(); - methodToGroup = new Dictionary(); + methodToGroup = new Dictionary(); foreach (var group in groups) { foreach (var method in group.Methods) methodToGroup[method] = group; @@ -1011,7 +1011,7 @@ namespace de4dot.code.renamer { visit(type); } - void visit(TypeDef type) { + void visit(MTypeDef type) { if (visited.ContainsKey(type)) return; visited[type] = true; @@ -1037,17 +1037,17 @@ namespace de4dot.code.renamer { } static readonly Regex removeGenericsArityRegex = new Regex(@"`[0-9]+"); - static string getOverridePrefix(MethodNameGroup group, MethodDef method) { - if (method == null || method.MethodDefinition.Overrides.Count == 0) + static string getOverridePrefix(MethodNameGroup group, MMethodDef method) { + if (method == null || method.MethodDef.Overrides.Count == 0) return ""; if (group.Methods.Count > 1) { // Don't use an override prefix if the group has an iface method. foreach (var m in group.Methods) { - if (m.Owner.TypeDefinition.IsInterface) + if (m.Owner.TypeDef.IsInterface) return ""; } } - var overrideMethod = method.MethodDefinition.Overrides[0]; + var overrideMethod = method.MethodDef.Overrides[0]; var name = overrideMethod.DeclaringType.FullName.Replace('/', '.'); name = removeGenericsArityRegex.Replace(name, ""); return name + "."; @@ -1108,7 +1108,7 @@ namespace de4dot.code.renamer { if (memberInfos.tryGetEvent(overriddenEventDef, out info)) oldEventName = getRealName(info.newName); else - oldEventName = getRealName(overriddenEventDef.EventDefinition.Name); + oldEventName = getRealName(overriddenEventDef.EventDef.Name); } } @@ -1134,13 +1134,13 @@ namespace de4dot.code.renamer { return newEventName; } - EventDef getOverriddenEvent(MethodDef overrideMethod) { - MethodDef overriddenMethod; + MEventDef getOverriddenEvent(MMethodDef overrideMethod) { + MMethodDef overriddenMethod; return getOverriddenEvent(overrideMethod, out overriddenMethod); } - EventDef getOverriddenEvent(MethodDef overrideMethod, out MethodDef overriddenMethod) { - var theMethod = overrideMethod.MethodDefinition.Overrides[0]; + MEventDef getOverriddenEvent(MMethodDef overrideMethod, out MMethodDef overriddenMethod) { + var theMethod = overrideMethod.MethodDef.Overrides[0]; overriddenMethod = modules.resolve(theMethod); if (overriddenMethod != null) return overriddenMethod.Event; @@ -1158,7 +1158,7 @@ namespace de4dot.code.renamer { return null; } - MethodDef getEventMethod(MethodNameGroup group) { + MMethodDef getEventMethod(MethodNameGroup group) { foreach (var method in group.Methods) { if (method.Event != null) return method; @@ -1218,7 +1218,7 @@ namespace de4dot.code.renamer { if (memberInfos.tryGetProperty(overriddenPropDef, out info)) oldPropName = getRealName(info.newName); else - oldPropName = getRealName(overriddenPropDef.PropertyDefinition.Name); + oldPropName = getRealName(overriddenPropDef.PropertyDef.Name); } } @@ -1260,8 +1260,8 @@ namespace de4dot.code.renamer { return false; } - PropertyDef getOverriddenProperty(MethodDef overrideMethod) { - var theMethod = overrideMethod.MethodDefinition.Overrides[0]; + MPropertyDef getOverriddenProperty(MMethodDef overrideMethod) { + var theMethod = overrideMethod.MethodDef.Overrides[0]; var overriddenMethod = modules.resolve(theMethod); if (overriddenMethod != null) return overriddenMethod.Property; @@ -1279,7 +1279,7 @@ namespace de4dot.code.renamer { return null; } - MethodDef getPropertyMethod(MethodNameGroup group) { + MMethodDef getPropertyMethod(MethodNameGroup group) { foreach (var method in group.Methods) { if (method.Property != null) return method; @@ -1306,7 +1306,7 @@ namespace de4dot.code.renamer { return defaultVal; var elementType = propType.GetElementType(); - if (propType is GenericInstanceType || elementType is GenericParameter) + if (propType is GenericInstanceType || elementType is GenericParam) return defaultVal; var prefix = getPrefix(propType); @@ -1342,8 +1342,8 @@ namespace de4dot.code.renamer { Setter, } - static PropertyMethodType getPropertyMethodType(MethodDef method) { - if (DotNetUtils.hasReturnValue(method.MethodDefinition)) + static PropertyMethodType getPropertyMethodType(MMethodDef method) { + if (DotNetUtils.hasReturnValue(method.MethodDef)) return PropertyMethodType.Getter; if (method.ParamDefs.Count > 0) return PropertyMethodType.Setter; @@ -1362,7 +1362,7 @@ namespace de4dot.code.renamer { if (methodType == PropertyMethodType.Setter) propType = propMethod.ParamDefs[propMethod.ParamDefs.Count - 1].ParameterDefinition.ParameterType; else - propType = propMethod.MethodDefinition.MethodReturnType.ReturnType; + propType = propMethod.MethodDef.MethodReturnType.ReturnType; if (type == null) type = propType; else if (!MemberReferenceHelper.compareTypes(type, propType)) @@ -1371,9 +1371,9 @@ namespace de4dot.code.renamer { return type; } - MethodDef getOverrideMethod(MethodNameGroup group) { + MMethodDef getOverrideMethod(MethodNameGroup group) { foreach (var method in group.Methods) { - if (method.MethodDefinition.Overrides.Count > 0) + if (method.MethodDef.Overrides.Count > 0) return method; } return null; @@ -1384,7 +1384,7 @@ namespace de4dot.code.renamer { return; if (hasDelegateOwner(group)) { - switch (group.Methods[0].MethodDefinition.Name) { + switch (group.Methods[0].MethodDef.Name) { case "Invoke": case "BeginInvoke": case "EndInvoke": @@ -1404,7 +1404,7 @@ namespace de4dot.code.renamer { var overrideInfo = memberInfos.method(overrideMethod); var overriddenMethod = getOverriddenMethod(overrideMethod); if (overriddenMethod == null) - newMethodName = getRealName(overrideMethod.MethodDefinition.Overrides[0].Name); + newMethodName = getRealName(overrideMethod.MethodDef.Overrides[0].Name); else newMethodName = getRealName(memberInfos.method(overriddenMethod).newName); } @@ -1432,7 +1432,7 @@ namespace de4dot.code.renamer { class MergeStateHelper { MemberInfos memberInfos; MergeStateFlags flags; - Dictionary visited = new Dictionary(); + Dictionary visited = new Dictionary(); public MergeStateHelper(MemberInfos memberInfos) { this.memberInfos = memberInfos; @@ -1445,7 +1445,7 @@ namespace de4dot.code.renamer { merge(method.Owner); } - void merge(TypeDef type) { + void merge(MTypeDef type) { if (visited.ContainsKey(type)) return; visited[type] = true; @@ -1465,7 +1465,7 @@ namespace de4dot.code.renamer { merge(info, ifaceInfo.typeDef); } - void merge(TypeInfo info, TypeDef other) { + void merge(TypeInfo info, MTypeDef other) { TypeInfo otherInfo; if (!memberInfos.tryGetType(other, out otherInfo)) return; @@ -1479,8 +1479,8 @@ namespace de4dot.code.renamer { } } - MethodDef getOverriddenMethod(MethodDef overrideMethod) { - return modules.resolve(overrideMethod.MethodDefinition.Overrides[0]); + MMethodDef getOverriddenMethod(MMethodDef overrideMethod) { + return modules.resolve(overrideMethod.MethodDef.Overrides[0]); } string getSuggestedMethodName(MethodNameGroup group) { diff --git a/de4dot.code/renamer/ResourceKeysRenamer.cs b/de4dot.code/renamer/ResourceKeysRenamer.cs index 6ad77116..b0c5e6f4 100644 --- a/de4dot.code/renamer/ResourceKeysRenamer.cs +++ b/de4dot.code/renamer/ResourceKeysRenamer.cs @@ -22,8 +22,8 @@ using System.Collections.Generic; using System.IO; using System.Text; using System.Text.RegularExpressions; -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using de4dot.blocks; using de4dot.code.resources; @@ -78,7 +78,7 @@ namespace de4dot.code.renamer { return null; } - static string getResourceName(TypeDefinition type) { + static string getResourceName(TypeDef type) { foreach (var method in type.Methods) { if (method.Body == null) continue; @@ -121,7 +121,7 @@ namespace de4dot.code.renamer { } } - void rename(TypeDefinition type, EmbeddedResource resource) { + void rename(TypeDef type, EmbeddedResource resource) { newNames.Clear(); var resourceSet = ResourceReader.read(module, resource.GetResourceStream()); var renamed = new List(); @@ -149,7 +149,7 @@ namespace de4dot.code.renamer { module.Resources[resourceIndex] = newResource; } - void rename(TypeDefinition type, List renamed) { + void rename(TypeDef type, List renamed) { var nameToInfo = new Dictionary(StringComparer.Ordinal); foreach (var info in renamed) nameToInfo[info.element.Name] = info; diff --git a/de4dot.code/renamer/ResourceRenamer.cs b/de4dot.code/renamer/ResourceRenamer.cs index cb92a9c5..7e60afc4 100644 --- a/de4dot.code/renamer/ResourceRenamer.cs +++ b/de4dot.code/renamer/ResourceRenamer.cs @@ -19,8 +19,8 @@ using System; using System.Collections.Generic; -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using de4dot.blocks; using de4dot.code.renamer.asmmodules; @@ -77,7 +77,7 @@ namespace de4dot.code.renamer { TypeInfo typeInfo; if (!oldNameToTypeInfo.TryGetValue(codeString, out typeInfo)) continue; - var newName = typeInfo.type.TypeDefinition.FullName; + var newName = typeInfo.type.TypeDef.FullName; bool renameCodeString = module.ObfuscatedFile.RenameResourcesInCode || isCallingResourceManagerCtor(instrs, i, typeInfo); @@ -98,7 +98,7 @@ namespace de4dot.code.renamer { var ldtoken = instrs[index++]; if (ldtoken.OpCode.Code != Code.Ldtoken) return false; - if (!MemberReferenceHelper.compareTypes(typeInfo.type.TypeDefinition, ldtoken.Operand as TypeReference)) + if (!MemberReferenceHelper.compareTypes(typeInfo.type.TypeDef, ldtoken.Operand as TypeReference)) return false; if (!checkCalledMethod(instrs[index++], "System.Type", "(System.RuntimeTypeHandle)")) @@ -151,7 +151,7 @@ namespace de4dot.code.renamer { continue; if (newNames.ContainsKey(resource)) continue; - var newTypeName = info.type.TypeDefinition.FullName; + var newTypeName = info.type.TypeDef.FullName; var newName = newTypeName + resource.Name.Substring(oldFullName.Length); newNames[resource] = new RenameInfo(resource, info, newName); diff --git a/de4dot.code/renamer/TypeInfo.cs b/de4dot.code/renamer/TypeInfo.cs index ff0d1b19..178eb097 100644 --- a/de4dot.code/renamer/TypeInfo.cs +++ b/de4dot.code/renamer/TypeInfo.cs @@ -20,8 +20,8 @@ using System; using System.Collections.Generic; using System.Text.RegularExpressions; -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using de4dot.code.renamer.asmmodules; using de4dot.blocks; @@ -30,45 +30,45 @@ namespace de4dot.code.renamer { public string oldNamespace; public string newNamespace; public VariableNameState variableNameState = VariableNameState.create(); - public TypeDef type; + public MTypeDef type; MemberInfos memberInfos; public INameChecker NameChecker { get { return type.Module.ObfuscatedFile.NameChecker; } } - public TypeInfo(TypeDef typeDef, MemberInfos memberInfos) + public TypeInfo(MTypeDef typeDef, MemberInfos memberInfos) : base(typeDef) { this.type = typeDef; this.memberInfos = memberInfos; - oldNamespace = typeDef.TypeDefinition.Namespace; + oldNamespace = typeDef.TypeDef.Namespace; } bool isWinFormsClass() { return memberInfos.isWinFormsClass(type); } - public PropertyInfo prop(PropertyDef prop) { + public PropertyInfo prop(MPropertyDef prop) { return memberInfos.prop(prop); } - public EventInfo evt(EventDef evt) { + public EventInfo evt(MEventDef evt) { return memberInfos.evt(evt); } - public FieldInfo field(FieldDef field) { + public FieldInfo field(MFieldDef field) { return memberInfos.field(field); } - public MethodInfo method(MethodDef method) { + public MethodInfo method(MMethodDef method) { return memberInfos.method(method); } - public GenericParamInfo gparam(GenericParamDef gparam) { + public GenericParamInfo gparam(MGenericParamDef gparam) { return memberInfos.gparam(gparam); } - public ParamInfo param(ParamDef param) { + public ParamInfo param(MParamDef param) { return memberInfos.param(param); } @@ -82,17 +82,17 @@ namespace de4dot.code.renamer { } bool isModuleType() { - return type.TypeDefinition == DotNetUtils.getModuleType(type.TypeDefinition.Module); + return type.TypeDef == DotNetUtils.getModuleType(type.TypeDef.Module); } public void prepareRenameTypes(TypeRenamerState state) { var checker = NameChecker; if (newNamespace == null && oldNamespace != "") { - if (type.TypeDefinition.IsNested) + if (type.TypeDef.IsNested) newNamespace = ""; else if (!checker.isValidNamespaceName(oldNamespace)) - newNamespace = state.createNamespace(this.type.TypeDefinition, oldNamespace); + newNamespace = state.createNamespace(this.type.TypeDef, oldNamespace); } string origClassName = null; @@ -114,7 +114,7 @@ namespace de4dot.code.renamer { TypeInfo baseInfo = getBase(); if (baseInfo != null && baseInfo.renamed) newBaseType = baseInfo.newName; - rename(nameCreator.create(type.TypeDefinition, newBaseType)); + rename(nameCreator.create(type.TypeDef, newBaseType)); } } @@ -128,7 +128,7 @@ namespace de4dot.code.renamer { mergeState(type.baseType.typeDef); } - void mergeState(TypeDef other) { + void mergeState(MTypeDef other) { if (other == null) return; TypeInfo otherInfo; @@ -164,7 +164,7 @@ namespace de4dot.code.renamer { void prepareRenameFields() { var checker = NameChecker; - if (type.TypeDefinition.IsEnum) { + if (type.TypeDef.IsEnum) { var instanceFields = getInstanceFields(); if (instanceFields.Count == 1) field(instanceFields[0]).rename("value__"); @@ -175,7 +175,7 @@ namespace de4dot.code.renamer { var fieldInfo = field(fieldDef); if (fieldInfo.renamed) continue; - if (!fieldDef.FieldDefinition.IsStatic || !fieldDef.FieldDefinition.IsLiteral) + if (!fieldDef.FieldDef.IsStatic || !fieldDef.FieldDef.IsLiteral) continue; if (!checker.isValidFieldName(fieldInfo.oldName)) fieldInfo.rename(string.Format(nameFormat, i)); @@ -187,21 +187,21 @@ namespace de4dot.code.renamer { if (fieldInfo.renamed) continue; if (!checker.isValidFieldName(fieldInfo.oldName)) - fieldInfo.rename(fieldInfo.suggestedName ?? variableNameState.getNewFieldName(fieldDef.FieldDefinition)); + fieldInfo.rename(fieldInfo.suggestedName ?? variableNameState.getNewFieldName(fieldDef.FieldDef)); } } - List getInstanceFields() { - var fields = new List(); + List getInstanceFields() { + var fields = new List(); foreach (var fieldDef in type.AllFields) { - if (!fieldDef.FieldDefinition.IsStatic) + if (!fieldDef.FieldDef.IsStatic) fields.Add(fieldDef); } return fields; } bool hasFlagsAttribute() { - foreach (var attr in type.TypeDefinition.CustomAttributes) { + foreach (var attr in type.TypeDef.CustomAttributes) { if (attr.AttributeType.FullName == "System.FlagsAttribute") return true; } @@ -216,7 +216,7 @@ namespace de4dot.code.renamer { } } - void prepareRenameProperty(PropertyDef propDef) { + void prepareRenameProperty(MPropertyDef propDef) { if (propDef.isVirtual()) throw new ApplicationException("Can't rename virtual props here"); var propInfo = prop(propDef); @@ -230,7 +230,7 @@ namespace de4dot.code.renamer { if (propDef.isItemProperty()) propName = "Item"; else - propName = variableNameState.getNewPropertyName(propDef.PropertyDefinition); + propName = variableNameState.getNewPropertyName(propDef.PropertyDef); } variableNameState.addPropertyName(propName); propInfo.rename(propName); @@ -247,7 +247,7 @@ namespace de4dot.code.renamer { } } - void prepareRenameEvent(EventDef eventDef) { + void prepareRenameEvent(MEventDef eventDef) { if (eventDef.isVirtual()) throw new ApplicationException("Can't rename virtual events here"); var eventInfo = evt(eventDef); @@ -258,7 +258,7 @@ namespace de4dot.code.renamer { if (!NameChecker.isValidEventName(eventName)) eventName = eventInfo.suggestedName; if (!NameChecker.isValidEventName(eventName)) - eventName = variableNameState.getNewEventName(eventDef.EventDefinition); + eventName = variableNameState.getNewEventName(eventDef.EventDef); variableNameState.addEventName(eventName); eventInfo.rename(eventName); @@ -267,7 +267,7 @@ namespace de4dot.code.renamer { renameSpecialMethod(eventDef.RaiseMethod, "raise_" + eventName); } - void renameSpecialMethod(MethodDef methodDef, string newName) { + void renameSpecialMethod(MMethodDef methodDef, string newName) { if (methodDef == null) return; if (methodDef.isVirtual()) @@ -292,7 +292,7 @@ namespace de4dot.code.renamer { } } - void prepareRenameMethodArgs(MethodDef methodDef) { + void prepareRenameMethodArgs(MMethodDef methodDef) { if (methodDef.ParamDefs.Count > 0) { if (isEventHandler(methodDef)) { ParamInfo info; @@ -327,14 +327,14 @@ namespace de4dot.code.renamer { } } - bool canRenameMethod(MethodDef methodDef) { + bool canRenameMethod(MMethodDef methodDef) { var methodInfo = method(methodDef); if (methodDef.isStatic()) { if (methodInfo.oldName == ".cctor") return false; } else if (methodDef.isVirtual()) { - if (DotNetUtils.derivesFromDelegate(type.TypeDefinition)) { + if (DotNetUtils.derivesFromDelegate(type.TypeDef)) { switch (methodInfo.oldName) { case "BeginInvoke": case "EndInvoke": @@ -350,7 +350,7 @@ namespace de4dot.code.renamer { return true; } - public void renameMethod(MethodDef methodDef, string methodName) { + public void renameMethod(MMethodDef methodDef, string methodName) { if (!canRenameMethod(methodDef)) return; var methodInfo = method(methodDef); @@ -358,7 +358,7 @@ namespace de4dot.code.renamer { methodInfo.rename(methodName); } - void renameMethod(MethodDef methodDef) { + void renameMethod(MMethodDef methodDef) { if (methodDef.isVirtual()) throw new ApplicationException("Can't rename virtual methods here"); if (!canRenameMethod(methodDef)) @@ -371,10 +371,10 @@ namespace de4dot.code.renamer { var checker = NameChecker; // PInvoke methods' EntryPoint is always valid. It has to, so always rename. - if (!NameChecker.isValidMethodName(info.oldName) || methodDef.MethodDefinition.PInvokeInfo != null) { + if (!NameChecker.isValidMethodName(info.oldName) || methodDef.MethodDef.PInvokeInfo != null) { INameCreator nameCreator = null; string newName = info.suggestedName; - if (methodDef.MethodDefinition.PInvokeInfo != null) + if (methodDef.MethodDef.PInvokeInfo != null) newName = getPinvokeName(methodDef); else if (methodDef.isStatic()) nameCreator = variableNameState.staticMethodNameCreator; @@ -386,30 +386,30 @@ namespace de4dot.code.renamer { } } - string getPinvokeName(MethodDef methodDef) { - var entryPoint = methodDef.MethodDefinition.PInvokeInfo.EntryPoint; + string getPinvokeName(MMethodDef methodDef) { + var entryPoint = methodDef.MethodDef.PInvokeInfo.EntryPoint; if (Regex.IsMatch(entryPoint, @"^#\d+$")) - entryPoint = DotNetUtils.getDllName(methodDef.MethodDefinition.PInvokeInfo.Module.Name) + "_" + entryPoint.Substring(1); + entryPoint = DotNetUtils.getDllName(methodDef.MethodDef.PInvokeInfo.Module.Name) + "_" + entryPoint.Substring(1); return entryPoint; } - static bool isEventHandler(MethodDef methodDef) { - if (methodDef.MethodDefinition.Parameters.Count != 2) + static bool isEventHandler(MMethodDef methodDef) { + if (methodDef.MethodDef.Parameters.Count != 2) return false; - if (methodDef.MethodDefinition.MethodReturnType.ReturnType.FullName != "System.Void") + if (methodDef.MethodDef.MethodReturnType.ReturnType.FullName != "System.Void") return false; - if (methodDef.MethodDefinition.Parameters[0].ParameterType.FullName != "System.Object") + if (methodDef.MethodDef.Parameters[0].ParameterType.FullName != "System.Object") return false; - if (!methodDef.MethodDefinition.Parameters[1].ParameterType.FullName.Contains("EventArgs")) + if (!methodDef.MethodDef.Parameters[1].ParameterType.FullName.Contains("EventArgs")) return false; return true; } - void prepareRenameGenericParams(IEnumerable genericParams, INameChecker checker) { + void prepareRenameGenericParams(IEnumerable genericParams, INameChecker checker) { prepareRenameGenericParams(genericParams, checker, null); } - void prepareRenameGenericParams(IEnumerable genericParams, INameChecker checker, IEnumerable otherGenericParams) { + void prepareRenameGenericParams(IEnumerable genericParams, INameChecker checker, IEnumerable otherGenericParams) { var usedNames = new Dictionary(StringComparer.Ordinal); var nameCreator = new GenericParamNameCreator(); @@ -436,19 +436,19 @@ namespace de4dot.code.renamer { void initializeWindowsFormsFieldsAndProps() { var checker = NameChecker; - var ourFields = new FieldDefinitionAndDeclaringTypeDict(); + var ourFields = new FieldDefinitionAndDeclaringTypeDict(); foreach (var fieldDef in type.AllFields) - ourFields.add(fieldDef.FieldDefinition, fieldDef); - var ourMethods = new MethodDefinitionAndDeclaringTypeDict(); + ourFields.add(fieldDef.FieldDef, fieldDef); + var ourMethods = new MethodDefinitionAndDeclaringTypeDict(); foreach (var methodDef in type.AllMethods) - ourMethods.add(methodDef.MethodDefinition, methodDef); + ourMethods.add(methodDef.MethodDef, methodDef); foreach (var methodDef in type.AllMethods) { - if (methodDef.MethodDefinition.Body == null) + if (methodDef.MethodDef.Body == null) continue; - if (methodDef.MethodDefinition.IsStatic || methodDef.MethodDefinition.IsVirtual) + if (methodDef.MethodDef.IsStatic || methodDef.MethodDef.IsVirtual) continue; - var instructions = methodDef.MethodDefinition.Body.Instructions; + var instructions = methodDef.MethodDef.Body.Instructions; for (int i = 2; i < instructions.Count; i++) { var call = instructions[i]; if (call.OpCode.Code != Code.Call && call.OpCode.Code != Code.Callvirt) @@ -472,7 +472,7 @@ namespace de4dot.code.renamer { var calledMethodDef = ourMethods.find(calledMethod); if (calledMethodDef == null) continue; - fieldRef = getFieldReference(calledMethodDef.MethodDefinition); + fieldRef = getFieldReference(calledMethodDef.MethodDef); var propDef = calledMethodDef.Property; if (propDef == null) @@ -500,7 +500,7 @@ namespace de4dot.code.renamer { } } - static FieldReference getFieldReference(MethodDefinition method) { + static FieldReference getFieldReference(MethodDef method) { if (method == null || method.Body == null) return null; var instructions = method.Body.Instructions; @@ -529,12 +529,12 @@ namespace de4dot.code.renamer { } public void initializeEventHandlerNames() { - var ourFields = new FieldDefinitionAndDeclaringTypeDict(); + var ourFields = new FieldDefinitionAndDeclaringTypeDict(); foreach (var fieldDef in type.AllFields) - ourFields.add(fieldDef.FieldDefinition, fieldDef); - var ourMethods = new MethodDefinitionAndDeclaringTypeDict(); + ourFields.add(fieldDef.FieldDef, fieldDef); + var ourMethods = new MethodDefinitionAndDeclaringTypeDict(); foreach (var methodDef in type.AllMethods) - ourMethods.add(methodDef.MethodDefinition, methodDef); + ourMethods.add(methodDef.MethodDef, methodDef); initVbEventHandlers(ourFields, ourMethods); initFieldEventHandlers(ourFields, ourMethods); @@ -543,7 +543,7 @@ namespace de4dot.code.renamer { // VB initializes the handlers in the property setter, where it first removes the handler // from the previous control, and then adds the handler to the new control. - void initVbEventHandlers(FieldDefinitionAndDeclaringTypeDict ourFields, MethodDefinitionAndDeclaringTypeDict ourMethods) { + void initVbEventHandlers(FieldDefinitionAndDeclaringTypeDict ourFields, MethodDefinitionAndDeclaringTypeDict ourMethods) { var checker = NameChecker; foreach (var propDef in type.AllProperties) { @@ -552,7 +552,7 @@ namespace de4dot.code.renamer { continue; string eventName; - var handler = getVbHandler(setterDef.MethodDefinition, out eventName); + var handler = getVbHandler(setterDef.MethodDef, out eventName); if (handler == null) continue; var handlerDef = ourMethods.find(handler); @@ -566,7 +566,7 @@ namespace de4dot.code.renamer { } } - static MethodReference getVbHandler(MethodDefinition method, out string eventName) { + static MethodReference getVbHandler(MethodDef method, out string eventName) { eventName = null; if (method.Body == null) return null; @@ -657,15 +657,15 @@ namespace de4dot.code.renamer { return -1; } - void initFieldEventHandlers(FieldDefinitionAndDeclaringTypeDict ourFields, MethodDefinitionAndDeclaringTypeDict ourMethods) { + void initFieldEventHandlers(FieldDefinitionAndDeclaringTypeDict ourFields, MethodDefinitionAndDeclaringTypeDict ourMethods) { var checker = NameChecker; foreach (var methodDef in type.AllMethods) { - if (methodDef.MethodDefinition.Body == null) + if (methodDef.MethodDef.Body == null) continue; - if (methodDef.MethodDefinition.IsStatic) + if (methodDef.MethodDef.IsStatic) continue; - var instructions = methodDef.MethodDefinition.Body.Instructions; + var instructions = methodDef.MethodDef.Body.Instructions; for (int i = 0; i < instructions.Count - 6; i++) { // We're looking for this code pattern: // ldarg.0 @@ -736,15 +736,15 @@ namespace de4dot.code.renamer { } } - void initTypeEventHandlers(FieldDefinitionAndDeclaringTypeDict ourFields, MethodDefinitionAndDeclaringTypeDict ourMethods) { + void initTypeEventHandlers(FieldDefinitionAndDeclaringTypeDict ourFields, MethodDefinitionAndDeclaringTypeDict ourMethods) { var checker = NameChecker; foreach (var methodDef in type.AllMethods) { - if (methodDef.MethodDefinition.Body == null) + if (methodDef.MethodDef.Body == null) continue; - if (methodDef.MethodDefinition.IsStatic) + if (methodDef.MethodDef.IsStatic) continue; - var method = methodDef.MethodDefinition; + var method = methodDef.MethodDef; var instructions = method.Body.Instructions; for (int i = 0; i < instructions.Count - 5; i++) { // ldarg.0 @@ -822,13 +822,13 @@ namespace de4dot.code.renamer { return type.FullName.EndsWith("EventHandler", StringComparison.Ordinal); } - string findWindowsFormsClassName(TypeDef type) { + string findWindowsFormsClassName(MTypeDef type) { foreach (var methodDef in type.AllMethods) { - if (methodDef.MethodDefinition.Body == null) + if (methodDef.MethodDef.Body == null) continue; - if (methodDef.MethodDefinition.IsStatic || methodDef.MethodDefinition.IsVirtual) + if (methodDef.MethodDef.IsStatic || methodDef.MethodDef.IsVirtual) continue; - var instructions = methodDef.MethodDefinition.Body.Instructions; + var instructions = methodDef.MethodDef.Body.Instructions; for (int i = 2; i < instructions.Count; i++) { var call = instructions[i]; if (call.OpCode.Code != Code.Call && call.OpCode.Code != Code.Callvirt) @@ -853,16 +853,16 @@ namespace de4dot.code.renamer { return null; } - void findInitializeComponentMethod(TypeDef type, MethodDef possibleInitMethod) { + void findInitializeComponentMethod(MTypeDef type, MMethodDef possibleInitMethod) { foreach (var methodDef in type.AllMethods) { - if (methodDef.MethodDefinition.Name != ".ctor") + if (methodDef.MethodDef.Name != ".ctor") continue; - if (methodDef.MethodDefinition.Body == null) + if (methodDef.MethodDef.Body == null) continue; - foreach (var instr in methodDef.MethodDefinition.Body.Instructions) { + foreach (var instr in methodDef.MethodDef.Body.Instructions) { if (instr.OpCode.Code != Code.Call && instr.OpCode.Code != Code.Callvirt) continue; - if (!MemberReferenceHelper.compareMethodReferenceAndDeclaringType(possibleInitMethod.MethodDefinition, instr.Operand as MethodReference)) + if (!MemberReferenceHelper.compareMethodReferenceAndDeclaringType(possibleInitMethod.MethodDef, instr.Operand as MethodReference)) continue; memberInfos.method(possibleInitMethod).suggestedName = "InitializeComponent"; diff --git a/de4dot.code/renamer/TypeNames.cs b/de4dot.code/renamer/TypeNames.cs index 91a4d94f..47dcb463 100644 --- a/de4dot.code/renamer/TypeNames.cs +++ b/de4dot.code/renamer/TypeNames.cs @@ -19,7 +19,7 @@ using System; using System.Collections.Generic; -using Mono.Cecil; +using dot10.DotNet; namespace de4dot.code.renamer { abstract class TypeNames { @@ -40,7 +40,7 @@ namespace de4dot.code.renamer { string prefix = getPrefix(typeRef); var elementType = typeRef.GetElementType(); - if (elementType is GenericParameter) + if (elementType is GenericParam) return genericParamNameCreator.create(); NameCreator nc; diff --git a/de4dot.code/renamer/TypeRenamerState.cs b/de4dot.code/renamer/TypeRenamerState.cs index a248a06c..0c3053f2 100644 --- a/de4dot.code/renamer/TypeRenamerState.cs +++ b/de4dot.code/renamer/TypeRenamerState.cs @@ -19,7 +19,7 @@ using System; using System.Collections.Generic; -using Mono.Cecil; +using dot10.DotNet; namespace de4dot.code.renamer { class TypeRenamerState { @@ -45,7 +45,7 @@ namespace de4dot.code.renamer { return existingNames.getName(oldName, new NameCreator2(newName)); } - public string createNamespace(TypeDefinition type, string ns) { + public string createNamespace(TypeDef type, string ns) { string newName; string asmFullName; diff --git a/de4dot.code/renamer/VariableNameState.cs b/de4dot.code/renamer/VariableNameState.cs index a877a2fa..6bd901f7 100644 --- a/de4dot.code/renamer/VariableNameState.cs +++ b/de4dot.code/renamer/VariableNameState.cs @@ -17,7 +17,7 @@ along with de4dot. If not, see . */ -using Mono.Cecil; +using dot10.DotNet; namespace de4dot.code.renamer { class VariableNameState { @@ -86,7 +86,7 @@ namespace de4dot.code.renamer { existingEventNames.merge(other.existingEventNames); } - public string getNewPropertyName(PropertyDefinition propertyDefinition) { + public string getNewPropertyName(PropertyDef propertyDefinition) { var propType = propertyDefinition.PropertyType; string newName; if (isGeneric(propType)) @@ -99,7 +99,7 @@ namespace de4dot.code.renamer { static bool isGeneric(TypeReference type) { while (true) { - if (type is GenericParameter) + if (type is GenericParam) return true; var ts = type as TypeSpecification; if (ts == null) @@ -108,7 +108,7 @@ namespace de4dot.code.renamer { } } - public string getNewEventName(EventDefinition eventDefinition) { + public string getNewEventName(EventDef eventDefinition) { string newName = eventNameCreator.create(); addEventName(newName); return newName; @@ -146,7 +146,7 @@ namespace de4dot.code.renamer { return existingEventNames.exists(eventName); } - public string getNewFieldName(FieldDefinition field) { + public string getNewFieldName(FieldDef field) { return existingVariableNames.getName(field.Name, () => variableNameCreator.create(field.FieldType)); } diff --git a/de4dot.code/renamer/asmmodules/EventDef.cs b/de4dot.code/renamer/asmmodules/EventDef.cs index 2ea9047a..14ea1742 100644 --- a/de4dot.code/renamer/asmmodules/EventDef.cs +++ b/de4dot.code/renamer/asmmodules/EventDef.cs @@ -18,31 +18,31 @@ */ using System.Collections.Generic; -using Mono.Cecil; +using dot10.DotNet; namespace de4dot.code.renamer.asmmodules { - class EventDef : Ref { - public MethodDef AddMethod { get; set; } - public MethodDef RemoveMethod { get; set; } - public MethodDef RaiseMethod { get; set; } + class MEventDef : Ref { + public MMethodDef AddMethod { get; set; } + public MMethodDef RemoveMethod { get; set; } + public MMethodDef RaiseMethod { get; set; } - public EventDefinition EventDefinition { - get { return (EventDefinition)memberReference; } + public EventDef EventDef { + get { return (EventDef)memberReference; } } - public EventDef(EventDefinition eventDefinition, TypeDef owner, int index) + public MEventDef(EventDef eventDefinition, MTypeDef owner, int index) : base(eventDefinition, owner, index) { } - public IEnumerable methodDefinitions() { - if (EventDefinition.AddMethod != null) - yield return EventDefinition.AddMethod; - if (EventDefinition.RemoveMethod != null) - yield return EventDefinition.RemoveMethod; - if (EventDefinition.InvokeMethod != null) - yield return EventDefinition.InvokeMethod; - if (EventDefinition.OtherMethods != null) { - foreach (var m in EventDefinition.OtherMethods) + public IEnumerable methodDefinitions() { + if (EventDef.AddMethod != null) + yield return EventDef.AddMethod; + if (EventDef.RemoveMethod != null) + yield return EventDef.RemoveMethod; + if (EventDef.InvokeMethod != null) + yield return EventDef.InvokeMethod; + if (EventDef.OtherMethods != null) { + foreach (var m in EventDef.OtherMethods) yield return m; } } diff --git a/de4dot.code/renamer/asmmodules/FieldDef.cs b/de4dot.code/renamer/asmmodules/FieldDef.cs index 3e16a003..13049071 100644 --- a/de4dot.code/renamer/asmmodules/FieldDef.cs +++ b/de4dot.code/renamer/asmmodules/FieldDef.cs @@ -17,15 +17,15 @@ along with de4dot. If not, see . */ -using Mono.Cecil; +using dot10.DotNet; namespace de4dot.code.renamer.asmmodules { - class FieldDef : Ref { - public FieldDefinition FieldDefinition { - get { return (FieldDefinition)memberReference; } + class MFieldDef : Ref { + public FieldDef FieldDef { + get { return (FieldDef)memberReference; } } - public FieldDef(FieldDefinition fieldDefinition, TypeDef owner, int index) + public MFieldDef(FieldDef fieldDefinition, MTypeDef owner, int index) : base(fieldDefinition, owner, index) { } } diff --git a/de4dot.code/renamer/asmmodules/GenericParamDef.cs b/de4dot.code/renamer/asmmodules/GenericParamDef.cs index ca2833cd..c7a7c992 100644 --- a/de4dot.code/renamer/asmmodules/GenericParamDef.cs +++ b/de4dot.code/renamer/asmmodules/GenericParamDef.cs @@ -18,25 +18,25 @@ */ using System.Collections.Generic; -using Mono.Cecil; +using dot10.DotNet; namespace de4dot.code.renamer.asmmodules { - class GenericParamDef : Ref { - public GenericParameter GenericParameter { - get { return (GenericParameter)memberReference; } + class MGenericParamDef : Ref { + public GenericParam GenericParam { + get { return (GenericParam)memberReference; } } - public GenericParamDef(GenericParameter genericParameter, int index) + public MGenericParamDef(GenericParam genericParameter, int index) : base(genericParameter, null, index) { } - public static List createGenericParamDefList(IEnumerable parameters) { - var list = new List(); + public static List createGenericParamDefList(IEnumerable parameters) { + var list = new List(); if (parameters == null) return list; int i = 0; foreach (var param in parameters) - list.Add(new GenericParamDef(param, i++)); + list.Add(new MGenericParamDef(param, i++)); return list; } } diff --git a/de4dot.code/renamer/asmmodules/IResolver.cs b/de4dot.code/renamer/asmmodules/IResolver.cs index d6401cb2..927445ab 100644 --- a/de4dot.code/renamer/asmmodules/IResolver.cs +++ b/de4dot.code/renamer/asmmodules/IResolver.cs @@ -17,12 +17,12 @@ along with de4dot. If not, see . */ -using Mono.Cecil; +using dot10.DotNet; namespace de4dot.code.renamer.asmmodules { interface IResolver { - TypeDef resolve(TypeReference typeReference); - MethodDef resolve(MethodReference methodReference); - FieldDef resolve(FieldReference fieldReference); + MTypeDef resolveType(TypeRef typeReference); + MMethodDef resolveMethod(MemberRef methodReference); + MFieldDef resolveField(MemberRef fieldReference); } } diff --git a/de4dot.code/renamer/asmmodules/MemberRefFinder.cs b/de4dot.code/renamer/asmmodules/MemberRefFinder.cs index f986f24e..a9ff6e9b 100644 --- a/de4dot.code/renamer/asmmodules/MemberRefFinder.cs +++ b/de4dot.code/renamer/asmmodules/MemberRefFinder.cs @@ -19,675 +19,666 @@ using System; using System.Collections.Generic; -using Mono.Cecil; -using Mono.Cecil.Cil; +using dot10.DotNet; +using dot10.DotNet.Emit; using de4dot.blocks; namespace de4dot.code.renamer.asmmodules { + enum ObjectType { + Unknown, + EventDef, + FieldDef, + GenericParam, + MemberRef, + MethodDef, + MethodSpec, + PropertyDef, + TypeDef, + TypeRef, + TypeSig, + TypeSpec, + ExportedType, + } + class MemberRefFinder { - public Dictionary eventDefinitions = new Dictionary(); - public Dictionary fieldReferences = new Dictionary(); - public Dictionary fieldDefinitions = new Dictionary(); - public Dictionary methodReferences = new Dictionary(); - public Dictionary methodDefinitions = new Dictionary(); - public Dictionary genericInstanceMethods = new Dictionary(); - public Dictionary propertyDefinitions = new Dictionary(); - public Dictionary typeReferences = new Dictionary(); - public Dictionary typeDefinitions = new Dictionary(); - public Dictionary genericParameters = new Dictionary(); - public Dictionary arrayTypes = new Dictionary(); - public Dictionary functionPointerTypes = new Dictionary(); - public Dictionary genericInstanceTypes = new Dictionary(); - public Dictionary optionalModifierTypes = new Dictionary(); - public Dictionary requiredModifierTypes = new Dictionary(); - public Dictionary pinnedTypes = new Dictionary(); - public Dictionary pointerTypes = new Dictionary(); - public Dictionary byReferenceTypes = new Dictionary(); - public Dictionary sentinelTypes = new Dictionary(); public Dictionary customAttributes = new Dictionary(); + public Dictionary eventDefs = new Dictionary(); + public Dictionary fieldDefs = new Dictionary(); + public Dictionary genericParams = new Dictionary(); + public Dictionary memberRefs = new Dictionary(); + public Dictionary methodDefs = new Dictionary(); + public Dictionary methodSpecs = new Dictionary(); + public Dictionary propertyDefs = new Dictionary(); + public Dictionary typeDefs = new Dictionary(); + public Dictionary typeRefs = new Dictionary(); + public Dictionary typeSigs = new Dictionary(); + public Dictionary typeSpecs = new Dictionary(); + public Dictionary exportedTypes = new Dictionary(); - Stack memberRefStack; - ModuleDefinition validModule; + Stack objectStack; + ModuleDef validModule; - public void removeTypeDefinition(TypeDefinition td) { - if (!typeDefinitions.Remove(td)) - throw new ApplicationException(string.Format("Could not remove TypeDefinition: {0}", td)); + public void removeTypeDef(TypeDef td) { + if (!typeDefs.Remove(td)) + throw new ApplicationException(string.Format("Could not remove TypeDef: {0}", td)); } - public void removeEventDefinition(EventDefinition ed) { - if (!eventDefinitions.Remove(ed)) - throw new ApplicationException(string.Format("Could not remove EventDefinition: {0}", ed)); + public void removeEventDef(EventDef ed) { + if (!eventDefs.Remove(ed)) + throw new ApplicationException(string.Format("Could not remove EventDef: {0}", ed)); } - public void removeFieldDefinition(FieldDefinition fd) { - if (!fieldDefinitions.Remove(fd)) - throw new ApplicationException(string.Format("Could not remove FieldDefinition: {0}", fd)); + public void removeFieldDef(FieldDef fd) { + if (!fieldDefs.Remove(fd)) + throw new ApplicationException(string.Format("Could not remove FieldDef: {0}", fd)); } - public void removeMethodDefinition(MethodDefinition md) { - if (!methodDefinitions.Remove(md)) - throw new ApplicationException(string.Format("Could not remove MethodDefinition: {0}", md)); + public void removeMethodDef(MethodDef md) { + if (!methodDefs.Remove(md)) + throw new ApplicationException(string.Format("Could not remove MethodDef: {0}", md)); } - public void removePropertyDefinition(PropertyDefinition pd) { - if (!propertyDefinitions.Remove(pd)) - throw new ApplicationException(string.Format("Could not remove PropertyDefinition: {0}", pd)); + public void removePropertyDef(PropertyDef pd) { + if (!propertyDefs.Remove(pd)) + throw new ApplicationException(string.Format("Could not remove PropertyDef: {0}", pd)); } - public void findAll(ModuleDefinition module, IEnumerable types) { + public void findAll(ModuleDef module) { validModule = module; // This needs to be big. About 2048 entries should be enough for most though... - memberRefStack = new Stack(0x1000); + objectStack = new Stack(0x1000); - foreach (var type in types) - pushMember(type); - - addModule(module); + add(module); processAll(); - memberRefStack = null; + objectStack = null; } - Dictionary exceptionMessages = new Dictionary(StringComparer.Ordinal); - void access(Action action) { - string exMessage = null; - try { - action(); - } - catch (ResolutionException ex) { - exMessage = ex.Message; - } - catch (AssemblyResolutionException ex) { - exMessage = ex.Message; - } - if (exMessage != null) { - if (!exceptionMessages.ContainsKey(exMessage)) { - exceptionMessages[exMessage] = true; - Log.w("Could not resolve a reference. ERROR: {0}", exMessage); - } - } - } - - void pushMember(MemberReference memberReference) { - if (memberReference == null) + void push(object mr) { + if (mr == null) return; - if (memberReference.Module != validModule) - return; - memberRefStack.Push(memberReference); - } - - void addModule(ModuleDefinition module) { - pushMember(module.EntryPoint); - access(() => addCustomAttributes(module.CustomAttributes)); - if (module.Assembly != null && module == module.Assembly.MainModule) { - var asm = module.Assembly; - access(() => addCustomAttributes(asm.CustomAttributes)); - addSecurityDeclarations(asm.SecurityDeclarations); - } + objectStack.Push(mr); } void processAll() { - while (memberRefStack.Count > 0) - process(memberRefStack.Pop()); - } - - void process(MemberReference memberRef) { - if (memberRef == null) - return; - - var type = MemberReferenceHelper.getMemberReferenceType(memberRef); - switch (type) { - case CecilType.ArrayType: - doArrayType((ArrayType)memberRef); - break; - case CecilType.ByReferenceType: - doByReferenceType((ByReferenceType)memberRef); - break; - case CecilType.EventDefinition: - doEventDefinition((EventDefinition)memberRef); - break; - case CecilType.FieldDefinition: - doFieldDefinition((FieldDefinition)memberRef); - break; - case CecilType.FieldReference: - doFieldReference((FieldReference)memberRef); - break; - case CecilType.FunctionPointerType: - doFunctionPointerType((FunctionPointerType)memberRef); - break; - case CecilType.GenericInstanceMethod: - doGenericInstanceMethod((GenericInstanceMethod)memberRef); - break; - case CecilType.GenericInstanceType: - doGenericInstanceType((GenericInstanceType)memberRef); - break; - case CecilType.GenericParameter: - doGenericParameter((GenericParameter)memberRef); - break; - case CecilType.MethodDefinition: - doMethodDefinition((MethodDefinition)memberRef); - break; - case CecilType.MethodReference: - doMethodReference((MethodReference)memberRef); - break; - case CecilType.OptionalModifierType: - doOptionalModifierType((OptionalModifierType)memberRef); - break; - case CecilType.PinnedType: - doPinnedType((PinnedType)memberRef); - break; - case CecilType.PointerType: - doPointerType((PointerType)memberRef); - break; - case CecilType.PropertyDefinition: - doPropertyDefinition((PropertyDefinition)memberRef); - break; - case CecilType.RequiredModifierType: - doRequiredModifierType((RequiredModifierType)memberRef); - break; - case CecilType.SentinelType: - doSentinelType((SentinelType)memberRef); - break; - case CecilType.TypeDefinition: - doTypeDefinition((TypeDefinition)memberRef); - break; - case CecilType.TypeReference: - doTypeReference((TypeReference)memberRef); - break; - default: - throw new ApplicationException(string.Format("Unknown cecil type {0}", type)); + while (objectStack.Count > 0) { + var o = objectStack.Pop(); + switch (getObjectType(o)) { + case ObjectType.Unknown: break; + case ObjectType.EventDef: add((EventDef)o); break; + case ObjectType.FieldDef: add((FieldDef)o); break; + case ObjectType.GenericParam: add((GenericParam)o); break; + case ObjectType.MemberRef: add((MemberRef)o); break; + case ObjectType.MethodDef: add((MethodDef)o); break; + case ObjectType.MethodSpec: add((MethodSpec)o); break; + case ObjectType.PropertyDef:add((PropertyDef)o); break; + case ObjectType.TypeDef: add((TypeDef)o); break; + case ObjectType.TypeRef: add((TypeRef)o); break; + case ObjectType.TypeSig: add((TypeSig)o); break; + case ObjectType.TypeSpec: add((TypeSpec)o); break; + case ObjectType.ExportedType: add((ExportedType)o); break; + default: throw new InvalidOperationException(string.Format("Unknown type: {0}", o.GetType())); + } } } - void addCustomAttributes(IEnumerable attributes) { - if (attributes == null) - return; - foreach (var attr in attributes) - addCustomAttribute(attr); + readonly Dictionary toObjectType = new Dictionary(); + ObjectType getObjectType(object o) { + if (o == null) + return ObjectType.Unknown; + var type = o.GetType(); + ObjectType mrType; + if (toObjectType.TryGetValue(type, out mrType)) + return mrType; + mrType = getObjectType2(o); + toObjectType[type] = mrType; + return mrType; } - void addCustomAttributeArguments(IEnumerable args) { + + static ObjectType getObjectType2(object o) { + if (o is EventDef) return ObjectType.EventDef; + if (o is FieldDef) return ObjectType.FieldDef; + if (o is GenericParam) return ObjectType.GenericParam; + if (o is MemberRef) return ObjectType.MemberRef; + if (o is MethodDef) return ObjectType.MethodDef; + if (o is MethodSpec) return ObjectType.MethodSpec; + if (o is PropertyDef) return ObjectType.PropertyDef; + if (o is TypeDef) return ObjectType.TypeDef; + if (o is TypeRef) return ObjectType.TypeRef; + if (o is TypeSig) return ObjectType.TypeSig; + if (o is TypeSpec) return ObjectType.TypeSpec; + if (o is ExportedType) return ObjectType.ExportedType; + return ObjectType.Unknown; + } + + void add(ModuleDef mod) { + push(mod.EntryPoint); + add(mod.CustomAttributes); + add(mod.Types); + add(mod.ExportedTypes); + if (mod.IsManifestModule) + add(mod.Assembly); + } + + void add(AssemblyDef asm) { + if (asm == null) + return; + add(asm.DeclSecurities); + add(asm.CustomAttributes); + } + + void add(CallingConventionSig sig) { + if (sig == null) + return; + + var fs = sig as FieldSig; + if (fs != null) { + add(fs); + return; + } + + var mbs = sig as MethodBaseSig; + if (mbs != null) { + add(mbs); + return; + } + + var ls = sig as LocalSig; + if (ls != null) { + add(ls); + return; + } + + var gims = sig as GenericInstMethodSig; + if (gims != null) { + add(gims); + return; + } + } + + void add(FieldSig sig) { + if (sig == null) + return; + add(sig.Type); + } + + void add(MethodBaseSig sig) { + if (sig == null) + return; + add(sig.RetType); + add(sig.Params); + add(sig.ParamsAfterSentinel); + } + + void add(LocalSig sig) { + if (sig == null) + return; + add(sig.Locals); + } + + void add(GenericInstMethodSig sig) { + if (sig == null) + return; + add(sig.GenericArguments); + } + + void add(IEnumerable cas) { + if (cas == null) + return; + foreach (var ca in cas) + add(ca); + } + + void add(CustomAttribute ca) { + if (ca == null || customAttributes.ContainsKey(ca)) + return; + customAttributes[ca] = true; + push(ca.Ctor); + add(ca.Arguments); + add(ca.NamedArguments); + } + + void add(IEnumerable args) { if (args == null) return; foreach (var arg in args) - addCustomAttributeArgument(arg); + add(arg); } - void addCustomAttributeNamedArguments(IEnumerable args) { + + void add(CAArgument arg) { + // It's a struct so can't be null + add(arg.Type); + } + + void add(IEnumerable args) { if (args == null) return; foreach (var arg in args) - addCustomAttributeNamedArgument(arg); + add(arg); } - void addParameterDefinitions(IEnumerable parameters) { - if (parameters == null) + + void add(CANamedArgument arg) { + if (arg == null) return; - foreach (var param in parameters) - addParameterDefinition(param); + add(arg.Type); + add(arg.Argument); } - void addSecurityDeclarations(IEnumerable decls) { + + void add(IEnumerable decls) { if (decls == null) return; foreach (var decl in decls) - addSecurityDeclaration(decl); + add(decl); } - void addSecurityAttributes(IEnumerable attrs) { - if (attrs == null) + + void add(DeclSecurity decl) { + if (decl == null) return; - foreach (var attr in attrs) - addSecurityAttribute(attr); + add(decl.CustomAttributes); } - void addExceptionHandlers(IEnumerable handlers) { - if (handlers == null) + + void add(IEnumerable eds) { + if (eds == null) return; - foreach (var h in handlers) - addExceptionHandler(h); + foreach (var ed in eds) + add(ed); } - void addVariableDefinitions(IEnumerable vars) { - if (vars == null) + + void add(EventDef ed) { + if (ed == null || eventDefs.ContainsKey(ed)) return; - foreach (var v in vars) - addVariableDefinition(v); - } - void addScopes(IEnumerable scopes) { - if (scopes == null) + if (ed.DeclaringType != null && ed.DeclaringType.OwnerModule != validModule) return; - foreach (var s in scopes) - addScope(s); + eventDefs[ed] = true; + push(ed.Type); + add(ed.CustomAttributes); + add(ed.AddMethod); + add(ed.InvokeMethod); + add(ed.RemoveMethod); + add(ed.OtherMethods); + add(ed.DeclaringType); } - void addInstructions(IEnumerable instrs) { + + void add(IEnumerable fds) { + if (fds == null) + return; + foreach (var fd in fds) + add(fd); + } + + void add(FieldDef fd) { + if (fd == null || fieldDefs.ContainsKey(fd)) + return; + if (fd.DeclaringType != null && fd.DeclaringType.OwnerModule != validModule) + return; + fieldDefs[fd] = true; + add(fd.CustomAttributes); + add(fd.Signature); + add(fd.DeclaringType); + } + + void add(IEnumerable gps) { + if (gps == null) + return; + foreach (var gp in gps) + add(gp); + } + + void add(GenericParam gp) { + if (gp == null || genericParams.ContainsKey(gp)) + return; + genericParams[gp] = true; + push(gp.Owner); + push(gp.Kind); + add(gp.GenericParamConstraints); + add(gp.CustomAttributes); + } + + void add(IEnumerable gpcs) { + if (gpcs == null) + return; + foreach (var gpc in gpcs) + add(gpc); + } + + void add(GenericParamConstraint gpc) { + if (gpc == null) + return; + add(gpc.Owner); + push(gpc.Constraint); + add(gpc.CustomAttributes); + } + + void add(MemberRef mr) { + if (mr == null || memberRefs.ContainsKey(mr)) + return; + if (mr.OwnerModule != validModule) + return; + memberRefs[mr] = true; + push(mr.Class); + add(mr.Signature); + add(mr.CustomAttributes); + } + + void add(IEnumerable methods) { + if (methods == null) + return; + foreach (var m in methods) + add(m); + } + + void add(MethodDef md) { + if (md == null || methodDefs.ContainsKey(md)) + return; + if (md.DeclaringType != null && md.DeclaringType.OwnerModule != validModule) + return; + methodDefs[md] = true; + add(md.Signature); + add(md.ParamList); + add(md.GenericParams); + add(md.DeclSecurities); + add(md.MethodBody); + add(md.CustomAttributes); + add(md.Overrides); + add(md.DeclaringType); + } + + void add(MethodBody mb) { + var cb = mb as CilBody; + if (cb != null) + add(cb); + } + + void add(CilBody cb) { + if (cb == null) + return; + add(cb.Instructions); + add(cb.ExceptionHandlers); + add(cb.LocalList); + } + + void add(IEnumerable instrs) { if (instrs == null) return; - foreach (var instr in instrs) { - switch (instr.OpCode.OperandType) { - case OperandType.InlineTok: - case OperandType.InlineType: - case OperandType.InlineMethod: - case OperandType.InlineField: - pushMember(instr.Operand as MemberReference); + foreach (var instr in instrs) + add(instr); + } + + void add(Instruction instr) { + if (instr == null) + return; + switch (instr.OpCode.OperandType) { + case OperandType.InlineTok: + case OperandType.InlineType: + case OperandType.InlineMethod: + case OperandType.InlineField: + push(instr.Operand); + break; + + case OperandType.InlineSig: + add(instr.Operand as CallingConventionSig); + break; + + case OperandType.InlineVar: + case OperandType.ShortInlineVar: + var local = instr.Operand as Local; + if (local != null) { + add(local); break; - case OperandType.InlineSig: - addCallSite(instr.Operand as CallSite); + } + var arg = instr.Operand as Parameter; + if (arg != null) { + add(arg); break; - case OperandType.InlineVar: - case OperandType.ShortInlineVar: - addVariableDefinition(instr.Operand as VariableDefinition); + } + break; + } + } + + void add(IEnumerable ehs) { + if (ehs == null) + return; + foreach (var eh in ehs) + push(eh.CatchType); + } + + void add(IEnumerable locals) { + if (locals == null) + return; + foreach (var local in locals) + add(local); + } + + void add(Local local) { + if (local == null) + return; + add(local.Type); + } + + void add(IEnumerable ps) { + if (ps == null) + return; + foreach (var p in ps) + add(p); + } + + void add(Parameter param) { + if (param == null) + return; + add(param.Type); + add(param.Method); + } + + void add(IEnumerable pds) { + if (pds == null) + return; + foreach (var pd in pds) + add(pd); + } + + void add(ParamDef pd) { + if (pd == null) + return; + add(pd.DeclaringMethod); + add(pd.CustomAttributes); + } + + void add(IEnumerable mos) { + if (mos == null) + return; + foreach (var mo in mos) + add(mo); + } + + void add(MethodOverride mo) { + // It's a struct so can't be null + push(mo.MethodBody); + push(mo.MethodDeclaration); + } + + void add(MethodSpec ms) { + if (ms == null || methodSpecs.ContainsKey(ms)) + return; + if (ms.Method != null && ms.Method.DeclaringType != null && ms.Method.DeclaringType.OwnerModule != validModule) + return; + methodSpecs[ms] = true; + push(ms.Method); + add(ms.Instantiation); + add(ms.CustomAttributes); + } + + void add(IEnumerable pds) { + if (pds == null) + return; + foreach (var pd in pds) + add(pd); + } + + void add(PropertyDef pd) { + if (pd == null || propertyDefs.ContainsKey(pd)) + return; + if (pd.DeclaringType != null && pd.DeclaringType.OwnerModule != validModule) + return; + propertyDefs[pd] = true; + add(pd.Type); + add(pd.CustomAttributes); + add(pd.GetMethod); + add(pd.SetMethod); + add(pd.OtherMethods); + add(pd.DeclaringType); + } + + void add(IEnumerable tds) { + if (tds == null) + return; + foreach (var td in tds) + add(td); + } + + void add(TypeDef td) { + if (td == null || typeDefs.ContainsKey(td)) + return; + if (td.OwnerModule != validModule) + return; + typeDefs[td] = true; + push(td.BaseType); + add(td.Fields); + add(td.Methods); + add(td.GenericParams); + add(td.InterfaceImpls); + add(td.DeclSecurities); + add(td.DeclaringType); + add(td.Events); + add(td.Properties); + add(td.NestedTypes); + add(td.CustomAttributes); + } + + void add(IEnumerable iis) { + if (iis == null) + return; + foreach (var ii in iis) + add(ii); + } + + void add(InterfaceImpl ii) { + if (ii == null) + return; + push(ii.Interface); + add(ii.CustomAttributes); + } + + void add(TypeRef tr) { + if (tr == null || typeRefs.ContainsKey(tr)) + return; + if (tr.OwnerModule != validModule) + return; + typeRefs[tr] = true; + push(tr.ResolutionScope); + add(tr.CustomAttributes); + } + + void add(IEnumerable tss) { + if (tss == null) + return; + foreach (var ts in tss) + add(ts); + } + + void add(TypeSig ts) { + if (ts == null || typeSigs.ContainsKey(ts)) + return; + if (ts.OwnerModule != validModule) + return; + typeSigs[ts] = true; + + for (; ts != null; ts = ts.Next) { + switch (ts.ElementType) { + case ElementType.Void: + case ElementType.Boolean: + case ElementType.Char: + case ElementType.I1: + case ElementType.U1: + case ElementType.I2: + case ElementType.U2: + case ElementType.I4: + case ElementType.U4: + case ElementType.I8: + case ElementType.U8: + case ElementType.R4: + case ElementType.R8: + case ElementType.String: + case ElementType.ValueType: + case ElementType.Class: + case ElementType.TypedByRef: + case ElementType.I: + case ElementType.U: + case ElementType.Object: + var tdrs = (TypeDefOrRefSig)ts; + push(tdrs.TypeDefOrRef); break; - case OperandType.InlineArg: - case OperandType.ShortInlineArg: - addParameterDefinition(instr.Operand as ParameterDefinition); + + case ElementType.FnPtr: + var fps = (FnPtrSig)ts; + add(fps.Signature); + break; + + case ElementType.GenericInst: + var gis = (GenericInstSig)ts; + add(gis.GenericArguments); + break; + + case ElementType.CModReqd: + case ElementType.CModOpt: + var ms = (ModifierSig)ts; + push(ms.Modifier); + break; + + case ElementType.End: + case ElementType.Ptr: + case ElementType.ByRef: + case ElementType.Var: + case ElementType.Array: + case ElementType.ValueArray: + case ElementType.R: + case ElementType.SZArray: + case ElementType.MVar: + case ElementType.Internal: + case ElementType.Module: + case ElementType.Sentinel: + case ElementType.Pinned: + default: break; } } } - void addTypeReferences(IEnumerable types) { - if (types == null) - return; - foreach (var typeRef in types) - pushMember(typeRef); - } - void addTypeDefinitions(IEnumerable types) { - if (types == null) - return; - foreach (var type in types) - pushMember(type); - } - void addMethodReferences(IEnumerable methodRefs) { - if (methodRefs == null) - return; - foreach (var m in methodRefs) - pushMember(m); - } - void addMethodDefinitions(IEnumerable methods) { - if (methods == null) - return; - foreach (var m in methods) - pushMember(m); - } - void addGenericParameters(IEnumerable parameters) { - if (parameters == null) - return; - foreach (var param in parameters) - pushMember(param); - } - void addFieldDefinitions(IEnumerable fields) { - if (fields == null) - return; - foreach (var f in fields) - pushMember(f); - } - void addEventDefinitions(IEnumerable events) { - if (events == null) - return; - foreach (var e in events) - pushMember(e); - } - void addPropertyDefinitions(IEnumerable props) { - if (props == null) - return; - foreach (var p in props) - pushMember(p); - } - void addMemberReference(MemberReference memberReference) { - if (memberReference == null) - return; - pushMember(memberReference.DeclaringType); - } - void addEventReference(EventReference eventReference) { - if (eventReference == null) - return; - addMemberReference(eventReference); - pushMember(eventReference.EventType); - } - void addEventDefinition(EventDefinition eventDefinition) { - if (eventDefinition == null) - return; - addEventReference(eventDefinition); - pushMember(eventDefinition.AddMethod); - pushMember(eventDefinition.InvokeMethod); - pushMember(eventDefinition.RemoveMethod); - addMethodDefinitions(eventDefinition.OtherMethods); - access(() => addCustomAttributes(eventDefinition.CustomAttributes)); - } - void addCustomAttribute(CustomAttribute attr) { - if (attr == null) - return; - customAttributes[attr] = true; - pushMember(attr.Constructor); - // Some obfuscators don't rename custom ctor arguments to the new name, causing - // Mono.Cecil to use a null reference. - try { access(() => addCustomAttributeArguments(attr.ConstructorArguments)); } catch (NullReferenceException) { } - try { access(() => addCustomAttributeNamedArguments(attr.Fields)); } catch (NullReferenceException) { } - try { access(() => addCustomAttributeNamedArguments(attr.Properties)); } catch (NullReferenceException) { } - } - void addCustomAttributeArgument(CustomAttributeArgument arg) { - pushMember(arg.Type); - } - void addCustomAttributeNamedArgument(CustomAttributeNamedArgument field) { - addCustomAttributeArgument(field.Argument); - } - void addFieldReference(FieldReference fieldReference) { - if (fieldReference == null) + void add(TypeSpec ts) { + if (ts == null || typeSpecs.ContainsKey(ts)) return; - addMemberReference(fieldReference); - pushMember(fieldReference.FieldType); - } - void addFieldDefinition(FieldDefinition fieldDefinition) { - if (fieldDefinition == null) + if (ts.OwnerModule != validModule) return; - addFieldReference(fieldDefinition); - access(() => addCustomAttributes(fieldDefinition.CustomAttributes)); - } - void addMethodReference(MethodReference methodReference) { - if (methodReference == null) - return; - addMemberReference(methodReference); - addParameterDefinitions(methodReference.Parameters); - addMethodReturnType(methodReference.MethodReturnType); - addGenericParameters(methodReference.GenericParameters); - } - void addParameterReference(ParameterReference param) { - if (param == null) - return; - pushMember(param.ParameterType); - } - void addParameterDefinition(ParameterDefinition param) { - if (param == null) - return; - addParameterReference(param); - pushMember(param.Method as MemberReference); - access(() => addCustomAttributes(param.CustomAttributes)); - } - void addMethodReturnType(MethodReturnType methodReturnType) { - if (methodReturnType == null) - return; - pushMember(methodReturnType.Method as MemberReference); - pushMember(methodReturnType.ReturnType); - addParameterDefinition(methodReturnType.Parameter); - } - void addGenericParameter(GenericParameter param) { - if (param == null) - return; - addTypeReference(param); - pushMember(param.Owner as MemberReference); - access(() => addCustomAttributes(param.CustomAttributes)); - addTypeReferences(param.Constraints); - } - void addTypeReference(TypeReference typeReference) { - if (typeReference == null) - return; - addMemberReference(typeReference); - addGenericParameters(typeReference.GenericParameters); - } - void addMethodDefinition(MethodDefinition methodDefinition) { - if (methodDefinition == null) - return; - addMethodReference(methodDefinition); - access(() => addCustomAttributes(methodDefinition.CustomAttributes)); - addSecurityDeclarations(methodDefinition.SecurityDeclarations); - addMethodReferences(methodDefinition.Overrides); - addMethodBody(methodDefinition.Body); - } - void addSecurityDeclaration(SecurityDeclaration decl) { - if (decl == null) - return; - access(() => addSecurityAttributes(decl.SecurityAttributes)); - } - void addSecurityAttribute(SecurityAttribute attr) { - if (attr == null) - return; - pushMember(attr.AttributeType); - addCustomAttributeNamedArguments(attr.Fields); - addCustomAttributeNamedArguments(attr.Properties); - } - void addMethodBody(MethodBody body) { - if (body == null) - return; - pushMember(body.Method); - addParameterDefinition(body.ThisParameter); - addExceptionHandlers(body.ExceptionHandlers); - addVariableDefinitions(body.Variables); - addScope(body.Scope); - addInstructions(body.Instructions); - } - void addExceptionHandler(ExceptionHandler handler) { - if (handler == null) - return; - pushMember(handler.CatchType); - } - void addVariableDefinition(VariableDefinition v) { - if (v == null) - return; - addVariableReference(v); - } - void addVariableReference(VariableReference v) { - if (v == null) - return; - pushMember(v.VariableType); - } - void addScope(Scope scope) { - if (scope == null) - return; - addVariableDefinitions(scope.Variables); - addScopes(scope.Scopes); - } - void addGenericInstanceMethod(GenericInstanceMethod genericInstanceMethod) { - if (genericInstanceMethod == null) - return; - addMethodSpecification(genericInstanceMethod); - addTypeReferences(genericInstanceMethod.GenericArguments); - } - void addMethodSpecification(MethodSpecification methodSpecification) { - if (methodSpecification == null) - return; - addMethodReference(methodSpecification); - pushMember(methodSpecification.ElementMethod); - } - void addPropertyReference(PropertyReference propertyReference) { - if (propertyReference == null) - return; - addMemberReference(propertyReference); - pushMember(propertyReference.PropertyType); - } - void addPropertyDefinition(PropertyDefinition propertyDefinition) { - if (propertyDefinition == null) - return; - addPropertyReference(propertyDefinition); - access(() => addCustomAttributes(propertyDefinition.CustomAttributes)); - pushMember(propertyDefinition.GetMethod); - pushMember(propertyDefinition.SetMethod); - addMethodDefinitions(propertyDefinition.OtherMethods); - } - void addTypeDefinition(TypeDefinition typeDefinition) { - if (typeDefinition == null) - return; - addTypeReference(typeDefinition); - pushMember(typeDefinition.BaseType); - addTypeReferences(typeDefinition.Interfaces); - addTypeDefinitions(typeDefinition.NestedTypes); - addMethodDefinitions(typeDefinition.Methods); - addFieldDefinitions(typeDefinition.Fields); - addEventDefinitions(typeDefinition.Events); - addPropertyDefinitions(typeDefinition.Properties); - access(() => addCustomAttributes(typeDefinition.CustomAttributes)); - addSecurityDeclarations(typeDefinition.SecurityDeclarations); - } - void addTypeSpecification(TypeSpecification ts) { - if (ts == null) - return; - addTypeReference(ts); - pushMember(ts.ElementType); - } - void addArrayType(ArrayType at) { - if (at == null) - return; - addTypeSpecification(at); - } - void addFunctionPointerType(FunctionPointerType fpt) { - if (fpt == null) - return; - addTypeSpecification(fpt); - - // It's an anon MethodReference created by the class. Not useful to us. - //pushMember(fpt.function); - } - void addGenericInstanceType(GenericInstanceType git) { - if (git == null) - return; - addTypeSpecification(git); - addTypeReferences(git.GenericArguments); - } - void addOptionalModifierType(OptionalModifierType omt) { - if (omt == null) - return; - addTypeSpecification(omt); - pushMember(omt.ModifierType); - } - void addRequiredModifierType(RequiredModifierType rmt) { - if (rmt == null) - return; - addTypeSpecification(rmt); - pushMember(rmt.ModifierType); - } - void addPinnedType(PinnedType pt) { - if (pt == null) - return; - addTypeSpecification(pt); - } - void addPointerType(PointerType pt) { - if (pt == null) - return; - addTypeSpecification(pt); - } - void addByReferenceType(ByReferenceType brt) { - if (brt == null) - return; - addTypeSpecification(brt); - } - void addSentinelType(SentinelType st) { - if (st == null) - return; - addTypeSpecification(st); - } - void addCallSite(CallSite cs) { - if (cs == null) - return; - pushMember(cs.signature); + typeSpecs[ts] = true; + add(ts.TypeSig); + add(ts.CustomAttributes); } - void doEventDefinition(EventDefinition eventDefinition) { - if (eventDefinitions.ContainsKey(eventDefinition)) + void add(IEnumerable ets) { + if (ets == null) return; - eventDefinitions[eventDefinition] = true; - addEventDefinition(eventDefinition); + foreach (var et in ets) + add(et); } - void doFieldReference(FieldReference fieldReference) { - if (fieldReferences.ContainsKey(fieldReference)) + + void add(ExportedType et) { + if (et == null || exportedTypes.ContainsKey(et)) return; - fieldReferences[fieldReference] = true; - addFieldReference(fieldReference); - } - void doFieldDefinition(FieldDefinition fieldDefinition) { - if (fieldDefinitions.ContainsKey(fieldDefinition)) + if (et.OwnerModule != validModule) return; - fieldDefinitions[fieldDefinition] = true; - addFieldDefinition(fieldDefinition); - } - void doMethodReference(MethodReference methodReference) { - if (methodReferences.ContainsKey(methodReference)) - return; - methodReferences[methodReference] = true; - addMethodReference(methodReference); - } - void doMethodDefinition(MethodDefinition methodDefinition) { - if (methodDefinitions.ContainsKey(methodDefinition)) - return; - methodDefinitions[methodDefinition] = true; - addMethodDefinition(methodDefinition); - } - void doGenericInstanceMethod(GenericInstanceMethod genericInstanceMethod) { - if (genericInstanceMethods.ContainsKey(genericInstanceMethod)) - return; - genericInstanceMethods[genericInstanceMethod] = true; - addGenericInstanceMethod(genericInstanceMethod); - } - void doPropertyDefinition(PropertyDefinition propertyDefinition) { - if (propertyDefinitions.ContainsKey(propertyDefinition)) - return; - propertyDefinitions[propertyDefinition] = true; - addPropertyDefinition(propertyDefinition); - } - void doTypeReference(TypeReference typeReference) { - if (typeReferences.ContainsKey(typeReference)) - return; - typeReferences[typeReference] = true; - addTypeReference(typeReference); - } - void doTypeDefinition(TypeDefinition typeDefinition) { - if (typeDefinitions.ContainsKey(typeDefinition)) - return; - typeDefinitions[typeDefinition] = true; - addTypeDefinition(typeDefinition); - } - void doGenericParameter(GenericParameter genericParameter) { - if (genericParameters.ContainsKey(genericParameter)) - return; - genericParameters[genericParameter] = true; - addGenericParameter(genericParameter); - } - void doArrayType(ArrayType arrayType) { - if (arrayTypes.ContainsKey(arrayType)) - return; - arrayTypes[arrayType] = true; - addArrayType(arrayType); - } - void doFunctionPointerType(FunctionPointerType functionPointerType) { - if (functionPointerTypes.ContainsKey(functionPointerType)) - return; - functionPointerTypes[functionPointerType] = true; - addFunctionPointerType(functionPointerType); - } - void doGenericInstanceType(GenericInstanceType genericInstanceType) { - if (genericInstanceTypes.ContainsKey(genericInstanceType)) - return; - genericInstanceTypes[genericInstanceType] = true; - addGenericInstanceType(genericInstanceType); - } - void doOptionalModifierType(OptionalModifierType optionalModifierType) { - if (optionalModifierTypes.ContainsKey(optionalModifierType)) - return; - optionalModifierTypes[optionalModifierType] = true; - addOptionalModifierType(optionalModifierType); - } - void doRequiredModifierType(RequiredModifierType requiredModifierType) { - if (requiredModifierTypes.ContainsKey(requiredModifierType)) - return; - requiredModifierTypes[requiredModifierType] = true; - addRequiredModifierType(requiredModifierType); - } - void doPinnedType(PinnedType pinnedType) { - if (pinnedTypes.ContainsKey(pinnedType)) - return; - pinnedTypes[pinnedType] = true; - addPinnedType(pinnedType); - } - void doPointerType(PointerType pointerType) { - if (pointerTypes.ContainsKey(pointerType)) - return; - pointerTypes[pointerType] = true; - addPointerType(pointerType); - } - void doByReferenceType(ByReferenceType byReferenceType) { - if (byReferenceTypes.ContainsKey(byReferenceType)) - return; - byReferenceTypes[byReferenceType] = true; - addByReferenceType(byReferenceType); - } - void doSentinelType(SentinelType sentinelType) { - if (sentinelTypes.ContainsKey(sentinelType)) - return; - sentinelTypes[sentinelType] = true; - addSentinelType(sentinelType); + exportedTypes[et] = true; + add(et.CustomAttributes); + push(et.Implementation); } } } diff --git a/de4dot.code/renamer/asmmodules/MethodDef.cs b/de4dot.code/renamer/asmmodules/MethodDef.cs index 29518dce..81419f43 100644 --- a/de4dot.code/renamer/asmmodules/MethodDef.cs +++ b/de4dot.code/renamer/asmmodules/MethodDef.cs @@ -18,51 +18,51 @@ */ using System.Collections.Generic; -using Mono.Cecil; +using dot10.DotNet; namespace de4dot.code.renamer.asmmodules { - class MethodDef : Ref { - IList genericParams; - IList paramDefs = new List(); + class MMethodDef : Ref { + IList genericParams; + IList paramDefs = new List(); - public PropertyDef Property { get; set; } - public EventDef Event { get; set; } + public MPropertyDef Property { get; set; } + public MEventDef Event { get; set; } - public IList ParamDefs { + public IList ParamDefs { get { return paramDefs; } } - public IList GenericParams { + public IList GenericParams { get { return genericParams; } } - public MethodDefinition MethodDefinition { - get { return (MethodDefinition)memberReference; } + public MethodDef MethodDef { + get { return (MethodDef)memberReference; } } - public MethodDef(MethodDefinition methodDefinition, TypeDef owner, int index) + public MMethodDef(MethodDef methodDefinition, MTypeDef owner, int index) : base(methodDefinition, owner, index) { - genericParams = GenericParamDef.createGenericParamDefList(MethodDefinition.GenericParameters); + genericParams = MGenericParamDef.createGenericParamDefList(MethodDef.GenericParams); for (int i = 0; i < methodDefinition.Parameters.Count; i++) { var param = methodDefinition.Parameters[i]; - paramDefs.Add(new ParamDef(param, i)); + paramDefs.Add(new MParamDef(param, i)); } } public bool isPublic() { - return MethodDefinition.IsPublic; + return MethodDef.IsPublic; } public bool isVirtual() { - return MethodDefinition.IsVirtual; + return MethodDef.IsVirtual; } public bool isNewSlot() { - return MethodDefinition.IsNewSlot; + return MethodDef.IsNewSlot; } public bool isStatic() { - return MethodDefinition.IsStatic; + return MethodDef.IsStatic; } } } diff --git a/de4dot.code/renamer/asmmodules/MethodNameGroups.cs b/de4dot.code/renamer/asmmodules/MethodNameGroups.cs index aa10957c..eadb760e 100644 --- a/de4dot.code/renamer/asmmodules/MethodNameGroups.cs +++ b/de4dot.code/renamer/asmmodules/MethodNameGroups.cs @@ -22,9 +22,9 @@ using System.Collections.Generic; namespace de4dot.code.renamer.asmmodules { class MethodNameGroup { - List methods = new List(); + List methods = new List(); - public List Methods { + public List Methods { get { return methods; } } @@ -32,7 +32,7 @@ namespace de4dot.code.renamer.asmmodules { get { return methods.Count; } } - public void add(MethodDef method) { + public void add(MMethodDef method) { methods.Add(method); } @@ -52,7 +52,7 @@ namespace de4dot.code.renamer.asmmodules { public bool hasInterfaceMethod() { foreach (var method in methods) { - if (method.Owner.TypeDefinition.IsInterface) + if (method.Owner.TypeDef.IsInterface) return true; } return false; @@ -102,17 +102,17 @@ namespace de4dot.code.renamer.asmmodules { } class MethodNameGroups { - Dictionary methodGroups = new Dictionary(); + Dictionary methodGroups = new Dictionary(); - public void same(MethodDef a, MethodDef b) { + public void same(MMethodDef a, MMethodDef b) { merge(get(a), get(b)); } - public void add(MethodDef methodDef) { + public void add(MMethodDef methodDef) { get(methodDef); } - public MethodNameGroup get(MethodDef method) { + public MethodNameGroup get(MMethodDef method) { if (!method.isVirtual()) throw new ApplicationException("Not a virtual method"); MethodNameGroup group; diff --git a/de4dot.code/renamer/asmmodules/Module.cs b/de4dot.code/renamer/asmmodules/Module.cs index 1b94bd76..ff4093f7 100644 --- a/de4dot.code/renamer/asmmodules/Module.cs +++ b/de4dot.code/renamer/asmmodules/Module.cs @@ -19,7 +19,7 @@ using System; using System.Collections.Generic; -using Mono.Cecil; +using dot10.DotNet; using de4dot.blocks; namespace de4dot.code.renamer.asmmodules { @@ -27,12 +27,12 @@ namespace de4dot.code.renamer.asmmodules { IObfuscatedFile obfuscatedFile; TypeDefDict types = new TypeDefDict(); MemberRefFinder memberRefFinder; - IList> typeRefsToRename = new List>(); - IList> methodRefsToRename = new List>(); - IList> fieldRefsToRename = new List>(); + IList> typeRefsToRename = new List>(); + IList> methodRefsToRename = new List>(); + IList> fieldRefsToRename = new List>(); List customAttributeFieldReferences = new List(); List customAttributePropertyReferences = new List(); - List allMethods; + List allMethods; public class CustomAttributeReference { public CustomAttribute cattr; @@ -54,15 +54,15 @@ namespace de4dot.code.renamer.asmmodules { } } - public IEnumerable> TypeRefsToRename { + public IEnumerable> TypeRefsToRename { get { return typeRefsToRename; } } - public IEnumerable> MethodRefsToRename { + public IEnumerable> MethodRefsToRename { get { return methodRefsToRename; } } - public IEnumerable> FieldRefsToRename { + public IEnumerable> FieldRefsToRename { get { return fieldRefsToRename; } } @@ -82,43 +82,43 @@ namespace de4dot.code.renamer.asmmodules { get { return obfuscatedFile.Filename; } } - public ModuleDefinition ModuleDefinition { - get { return obfuscatedFile.ModuleDefinition; } + public ModuleDefMD ModuleDefMD { + get { return obfuscatedFile.ModuleDefMD; } } public Module(IObfuscatedFile obfuscatedFile) { this.obfuscatedFile = obfuscatedFile; } - public IEnumerable getAllTypes() { + public IEnumerable getAllTypes() { return types.getValues(); } - public IEnumerable getAllMethods() { + public IEnumerable getAllMethods() { return allMethods; } public void findAllMemberReferences(ref int typeIndex) { memberRefFinder = new MemberRefFinder(); - memberRefFinder.findAll(ModuleDefinition, ModuleDefinition.Types); - allMethods = new List(memberRefFinder.methodDefinitions.Keys); + memberRefFinder.findAll(ModuleDefMD, ModuleDefMD.Types); + allMethods = new List(memberRefFinder.methodDefinitions.Keys); - var allTypesList = new List(); + var allTypesList = new List(); foreach (var type in memberRefFinder.typeDefinitions.Keys) { - var typeDef = new TypeDef(type, this, typeIndex++); + var typeDef = new MTypeDef(type, this, typeIndex++); types.add(typeDef); allTypesList.Add(typeDef); typeDef.addMembers(); } - var allTypesCopy = new List(allTypesList); - var typeToIndex = new Dictionary(); + var allTypesCopy = new List(allTypesList); + var typeToIndex = new Dictionary(); for (int i = 0; i < allTypesList.Count; i++) - typeToIndex[allTypesList[i].TypeDefinition] = i; + typeToIndex[allTypesList[i].TypeDef] = i; foreach (var typeDef in allTypesList) { - if (typeDef.TypeDefinition.NestedTypes == null) + if (typeDef.TypeDef.NestedTypes == null) continue; - foreach (var nestedTypeDefinition in typeDef.TypeDefinition.NestedTypes) { + foreach (var nestedTypeDefinition in typeDef.TypeDef.NestedTypes) { int index = typeToIndex[nestedTypeDefinition]; var nestedTypeDef = allTypesCopy[index]; allTypesCopy[index] = null; @@ -132,26 +132,26 @@ namespace de4dot.code.renamer.asmmodules { public void resolveAllRefs(IResolver resolver) { foreach (var typeRef in memberRefFinder.typeReferences.Keys) { - var typeDef = resolver.resolve(typeRef); + var typeDef = resolver.resolveType(typeRef); if (typeDef != null) - typeRefsToRename.Add(new RefToDef(typeRef, typeDef.TypeDefinition)); + typeRefsToRename.Add(new RefToDef(typeRef, typeDef.TypeDef)); } foreach (var methodRef in memberRefFinder.methodReferences.Keys) { - var methodDef = resolver.resolve(methodRef); + var methodDef = resolver.resolveMethod(methodRef); if (methodDef != null) - methodRefsToRename.Add(new RefToDef(methodRef, methodDef.MethodDefinition)); + methodRefsToRename.Add(new RefToDef(methodRef, methodDef.MethodDef)); } foreach (var fieldRef in memberRefFinder.fieldReferences.Keys) { - var fieldDef = resolver.resolve(fieldRef); + var fieldDef = resolver.resolveField(fieldRef); if (fieldDef != null) - fieldRefsToRename.Add(new RefToDef(fieldRef, fieldDef.FieldDefinition)); + fieldRefsToRename.Add(new RefToDef(fieldRef, fieldDef.FieldDef)); } foreach (var cattr in memberRefFinder.customAttributes.Keys) { try { - var typeDef = resolver.resolve(cattr.AttributeType); + var typeDef = resolver.resolveType(cattr.AttributeType); if (typeDef == null) continue; @@ -161,12 +161,12 @@ namespace de4dot.code.renamer.asmmodules { if (fieldDef == null) { Log.w("Could not find field {0} in attribute {1} ({2:X8})", Utils.toCsharpString(field.Name), - Utils.toCsharpString(typeDef.TypeDefinition.Name), - typeDef.TypeDefinition.MetadataToken.ToInt32()); + Utils.toCsharpString(typeDef.TypeDef.Name), + typeDef.TypeDef.MDToken.ToInt32()); continue; } - customAttributeFieldReferences.Add(new CustomAttributeReference(cattr, i, fieldDef.FieldDefinition)); + customAttributeFieldReferences.Add(new CustomAttributeReference(cattr, i, fieldDef.FieldDef)); } for (int i = 0; i < cattr.Properties.Count; i++) { @@ -175,12 +175,12 @@ namespace de4dot.code.renamer.asmmodules { if (propDef == null) { Log.w("Could not find property {0} in attribute {1} ({2:X8})", Utils.toCsharpString(prop.Name), - Utils.toCsharpString(typeDef.TypeDefinition.Name), - typeDef.TypeDefinition.MetadataToken.ToInt32()); + Utils.toCsharpString(typeDef.TypeDef.Name), + typeDef.TypeDef.MDToken.ToInt32()); continue; } - customAttributePropertyReferences.Add(new CustomAttributeReference(cattr, i, propDef.PropertyDefinition)); + customAttributePropertyReferences.Add(new CustomAttributeReference(cattr, i, propDef.PropertyDef)); } } catch { @@ -188,10 +188,10 @@ namespace de4dot.code.renamer.asmmodules { } } - static FieldDef findFieldByName(TypeDef typeDef, string name) { + static MFieldDef findFieldByName(MTypeDef typeDef, string name) { while (typeDef != null) { foreach (var fieldDef in typeDef.AllFields) { - if (fieldDef.FieldDefinition.Name == name) + if (fieldDef.FieldDef.Name == name) return fieldDef; } @@ -202,10 +202,10 @@ namespace de4dot.code.renamer.asmmodules { return null; } - static PropertyDef findPropertyByName(TypeDef typeDef, string name) { + static MPropertyDef findPropertyByName(MTypeDef typeDef, string name) { while (typeDef != null) { foreach (var propDef in typeDef.AllProperties) { - if (propDef.PropertyDefinition.Name == name) + if (propDef.PropertyDef.Name == name) return propDef; } @@ -234,18 +234,18 @@ namespace de4dot.code.renamer.asmmodules { return type.ElementType; } - public TypeDef resolve(TypeReference typeReference) { + public MTypeDef resolveType(TypeReference typeReference) { return this.types.find(getNonGenericTypeReference(typeReference)); } - public MethodDef resolve(MethodReference methodReference) { + public MMethodDef resolveMethod(MethodReference methodReference) { var typeDef = this.types.find(getNonGenericTypeReference(methodReference.DeclaringType)); if (typeDef == null) return null; return typeDef.find(methodReference); } - public FieldDef resolve(FieldReference fieldReference) { + public MFieldDef resolveField(FieldReference fieldReference) { var typeDef = this.types.find(getNonGenericTypeReference(fieldReference.DeclaringType)); if (typeDef == null) return null; diff --git a/de4dot.code/renamer/asmmodules/Modules.cs b/de4dot.code/renamer/asmmodules/Modules.cs index 9cc0bd1d..3d435032 100644 --- a/de4dot.code/renamer/asmmodules/Modules.cs +++ b/de4dot.code/renamer/asmmodules/Modules.cs @@ -19,7 +19,7 @@ using System; using System.Collections.Generic; -using Mono.Cecil; +using dot10.DotNet; using de4dot.blocks; namespace de4dot.code.renamer.asmmodules { @@ -27,26 +27,26 @@ namespace de4dot.code.renamer.asmmodules { bool initializeCalled = false; IDeobfuscatorContext deobfuscatorContext; List modules = new List(); - Dictionary modulesDict = new Dictionary(); + Dictionary modulesDict = new Dictionary(); AssemblyHash assemblyHash = new AssemblyHash(); - List allTypes = new List(); - List baseTypes = new List(); - List nonNestedTypes; + List allTypes = new List(); + List baseTypes = new List(); + List nonNestedTypes; public IList TheModules { get { return modules; } } - public IEnumerable AllTypes { + public IEnumerable AllTypes { get { return allTypes; } } - public IEnumerable BaseTypes { + public IEnumerable BaseTypes { get { return baseTypes; } } - public List NonNestedTypes { + public List NonNestedTypes { get { return nonNestedTypes; } } @@ -62,9 +62,9 @@ namespace de4dot.code.renamer.asmmodules { } string getModuleKey(Module module) { - if (module.ModuleDefinition.Assembly != null) - return module.ModuleDefinition.Assembly.ToString(); - return Utils.getBaseName(module.ModuleDefinition.FullyQualifiedName); + if (module.ModuleDef.Assembly != null) + return module.ModuleDef.Assembly.ToString(); + return Utils.getBaseName(module.ModuleDef.FullyQualifiedName); } public ModuleHash lookup(string assemblyName) { @@ -80,16 +80,16 @@ namespace de4dot.code.renamer.asmmodules { Module mainModule = null; public void add(Module module) { - var asm = module.ModuleDefinition.Assembly; - if (asm != null && ReferenceEquals(asm.MainModule, module.ModuleDefinition)) { + var asm = module.ModuleDef.Assembly; + if (asm != null && ReferenceEquals(asm.MainModule, module.ModuleDef)) { if (mainModule != null) { throw new UserException(string.Format( "Two modules in the same assembly are main modules.\n" + "Is one 32-bit and the other 64-bit?\n" + " Module1: \"{0}\"" + " Module2: \"{1}\"", - module.ModuleDefinition.FullyQualifiedName, - mainModule.ModuleDefinition.FullyQualifiedName)); + module.ModuleDef.FullyQualifiedName, + mainModule.ModuleDef.FullyQualifiedName)); } mainModule = module; } @@ -110,7 +110,7 @@ namespace de4dot.code.renamer.asmmodules { IDictionary modulesDict = new Dictionary(StringComparer.Ordinal); public void add(Module module) { - var moduleName = module.ModuleDefinition.Name; + var moduleName = module.ModuleDef.Name; if (lookup(moduleName) != null) throw new ApplicationException(string.Format("Module \"{0}\" was found twice", moduleName)); modulesDict[moduleName] = module; @@ -140,9 +140,9 @@ namespace de4dot.code.renamer.asmmodules { if (initializeCalled) throw new ApplicationException("initialize() has been called"); Module otherModule; - if (modulesDict.TryGetValue(module.ModuleDefinition, out otherModule)) + if (modulesDict.TryGetValue(module.ModuleDef, out otherModule)) return; - modulesDict[module.ModuleDefinition] = module; + modulesDict[module.ModuleDef] = module; modules.Add(module); assemblyHash.add(module); } @@ -181,22 +181,22 @@ namespace de4dot.code.renamer.asmmodules { foreach (var module in modules) allTypes.AddRange(module.getAllTypes()); - var typeToTypeDef = new Dictionary(allTypes.Count); + var typeToTypeDef = new Dictionary(allTypes.Count); foreach (var typeDef in allTypes) - typeToTypeDef[typeDef.TypeDefinition] = typeDef; + typeToTypeDef[typeDef.TypeDef] = typeDef; // Initialize Owner foreach (var typeDef in allTypes) { - if (typeDef.TypeDefinition.DeclaringType != null) - typeDef.Owner = typeToTypeDef[typeDef.TypeDefinition.DeclaringType]; + if (typeDef.TypeDef.DeclaringType != null) + typeDef.Owner = typeToTypeDef[typeDef.TypeDef.DeclaringType]; } // Initialize baseType and derivedTypes foreach (var typeDef in allTypes) { - var baseType = typeDef.TypeDefinition.BaseType; + var baseType = typeDef.TypeDef.BaseType; if (baseType == null) continue; - var baseTypeDef = resolve(baseType) ?? resolveOther(baseType); + var baseTypeDef = resolveType(baseType) ?? resolveOther(baseType); if (baseTypeDef != null) { typeDef.addBaseType(baseTypeDef, baseType); baseTypeDef.derivedTypes.Add(typeDef); @@ -205,24 +205,24 @@ namespace de4dot.code.renamer.asmmodules { // Initialize interfaces foreach (var typeDef in allTypes) { - if (typeDef.TypeDefinition.Interfaces == null) + if (typeDef.TypeDef.Interfaces == null) continue; - foreach (var iface in typeDef.TypeDefinition.Interfaces) { - var ifaceTypeDef = resolve(iface) ?? resolveOther(iface); + foreach (var iface in typeDef.TypeDef.Interfaces) { + var ifaceTypeDef = resolveType(iface) ?? resolveOther(iface); if (ifaceTypeDef != null) typeDef.addInterface(ifaceTypeDef, iface); } } // Find all non-nested types - var allTypesDict = new Dictionary(); + var allTypesDict = new Dictionary(); foreach (var t in allTypes) allTypesDict[t] = true; foreach (var t in allTypes) { foreach (var t2 in t.NestedTypes) allTypesDict.Remove(t2); } - nonNestedTypes = new List(allTypesDict.Keys); + nonNestedTypes = new List(allTypesDict.Keys); foreach (var typeDef in allTypes) { if (typeDef.baseType == null || !typeDef.baseType.typeDef.HasModule) @@ -327,13 +327,13 @@ namespace de4dot.code.renamer.asmmodules { } } - AssemblyKeyDictionary typeToTypeDefDict = new AssemblyKeyDictionary(); - public TypeDef resolveOther(TypeReference type) { + AssemblyKeyDictionary typeToTypeDefDict = new AssemblyKeyDictionary(); + public MTypeDef resolveOther(TypeReference type) { if (type == null) return null; type = type.GetElementType(); - TypeDef typeDef; + MTypeDef typeDef; if (typeToTypeDefDict.tryGetValue(type, out typeDef)) return typeDef; @@ -352,17 +352,17 @@ namespace de4dot.code.renamer.asmmodules { typeToTypeDefDict[type] = null; // In case of a circular reference typeToTypeDefDict[typeDefinition] = null; - typeDef = new TypeDef(typeDefinition, null, 0); + typeDef = new MTypeDef(typeDefinition, null, 0); typeDef.addMembers(); - foreach (var iface in typeDef.TypeDefinition.Interfaces) { + foreach (var iface in typeDef.TypeDef.Interfaces) { var ifaceDef = resolveOther(iface); if (ifaceDef == null) continue; typeDef.addInterface(ifaceDef, iface); } - var baseDef = resolveOther(typeDef.TypeDefinition.BaseType); + var baseDef = resolveOther(typeDef.TypeDef.BaseType); if (baseDef != null) - typeDef.addBaseType(baseDef, typeDef.TypeDefinition.BaseType); + typeDef.addBaseType(baseDef, typeDef.TypeDef.BaseType); typeToTypeDefDict[type] = typeDef; if (type != typeDefinition) @@ -396,8 +396,8 @@ namespace de4dot.code.renamer.asmmodules { if (scope is AssemblyNameReference) return findModules((AssemblyNameReference)scope); - if (scope is ModuleDefinition) { - var modules = findModules((ModuleDefinition)scope); + if (scope is ModuleDef) { + var modules = findModules((ModuleDef)scope); if (modules != null) return modules; } @@ -432,7 +432,7 @@ namespace de4dot.code.renamer.asmmodules { return null; } - IEnumerable findModules(ModuleDefinition moduleDefinition) { + IEnumerable findModules(ModuleDef moduleDefinition) { Module module; if (modulesDict.TryGetValue(moduleDefinition, out module)) return new List { module }; @@ -443,12 +443,12 @@ namespace de4dot.code.renamer.asmmodules { return typeReference is ArrayType || typeReference is PointerType || typeReference is FunctionPointerType; } - public TypeDef resolve(TypeReference typeReference) { + public MTypeDef resolveType(TypeReference typeReference) { var modules = findModules(typeReference); if (modules == null) return null; foreach (var module in modules) { - var rv = module.resolve(typeReference); + var rv = module.resolveType(typeReference); if (rv != null) return rv; } @@ -456,20 +456,20 @@ namespace de4dot.code.renamer.asmmodules { return null; Log.e("Could not resolve TypeReference {0} ({1:X8}) (from {2} -> {3})", Utils.removeNewlines(typeReference), - typeReference.MetadataToken.ToInt32(), + typeReference.MDToken.ToInt32(), typeReference.Module, typeReference.Scope); return null; } - public MethodDef resolve(MethodReference methodReference) { + public MMethodDef resolveMethod(MethodReference methodReference) { if (methodReference.DeclaringType == null) return null; var modules = findModules(methodReference.DeclaringType); if (modules == null) return null; foreach (var module in modules) { - var rv = module.resolve(methodReference); + var rv = module.resolveMethod(methodReference); if (rv != null) return rv; } @@ -477,20 +477,20 @@ namespace de4dot.code.renamer.asmmodules { return null; Log.e("Could not resolve MethodReference {0} ({1:X8}) (from {2} -> {3})", Utils.removeNewlines(methodReference), - methodReference.MetadataToken.ToInt32(), + methodReference.MDToken.ToInt32(), methodReference.DeclaringType.Module, methodReference.DeclaringType.Scope); return null; } - public FieldDef resolve(FieldReference fieldReference) { + public MFieldDef resolveField(FieldReference fieldReference) { if (fieldReference.DeclaringType == null) return null; var modules = findModules(fieldReference.DeclaringType); if (modules == null) return null; foreach (var module in modules) { - var rv = module.resolve(fieldReference); + var rv = module.resolveField(fieldReference); if (rv != null) return rv; } @@ -498,7 +498,7 @@ namespace de4dot.code.renamer.asmmodules { return null; Log.e("Could not resolve FieldReference {0} ({1:X8}) (from {2} -> {3})", Utils.removeNewlines(fieldReference), - fieldReference.MetadataToken.ToInt32(), + fieldReference.MDToken.ToInt32(), fieldReference.DeclaringType.Module, fieldReference.DeclaringType.Scope); return null; diff --git a/de4dot.code/renamer/asmmodules/ParamDef.cs b/de4dot.code/renamer/asmmodules/ParamDef.cs index 4302da06..252ff377 100644 --- a/de4dot.code/renamer/asmmodules/ParamDef.cs +++ b/de4dot.code/renamer/asmmodules/ParamDef.cs @@ -17,14 +17,14 @@ along with de4dot. If not, see . */ -using Mono.Cecil; +using dot10.DotNet; namespace de4dot.code.renamer.asmmodules { - class ParamDef { - public ParameterDefinition ParameterDefinition { get; set; } + class MParamDef { + public Parameter ParameterDefinition { get; set; } public int Index { get; private set; } - public ParamDef(ParameterDefinition parameterDefinition, int index) { + public MParamDef(Parameter parameterDefinition, int index) { this.ParameterDefinition = parameterDefinition; Index = index; } diff --git a/de4dot.code/renamer/asmmodules/PropertyDef.cs b/de4dot.code/renamer/asmmodules/PropertyDef.cs index 44e7e60f..0f2a4682 100644 --- a/de4dot.code/renamer/asmmodules/PropertyDef.cs +++ b/de4dot.code/renamer/asmmodules/PropertyDef.cs @@ -18,28 +18,28 @@ */ using System.Collections.Generic; -using Mono.Cecil; +using dot10.DotNet; namespace de4dot.code.renamer.asmmodules { - class PropertyDef : Ref { - public MethodDef GetMethod { get; set; } - public MethodDef SetMethod { get; set; } + class MPropertyDef : Ref { + public MMethodDef GetMethod { get; set; } + public MMethodDef SetMethod { get; set; } - public PropertyDefinition PropertyDefinition { - get { return (PropertyDefinition)memberReference; } + public PropertyDef PropertyDef { + get { return (PropertyDef)memberReference; } } - public PropertyDef(PropertyDefinition propertyDefinition, TypeDef owner, int index) + public MPropertyDef(PropertyDef propertyDefinition, MTypeDef owner, int index) : base(propertyDefinition, owner, index) { } - public IEnumerable methodDefinitions() { - if (PropertyDefinition.GetMethod != null) - yield return PropertyDefinition.GetMethod; - if (PropertyDefinition.SetMethod != null) - yield return PropertyDefinition.SetMethod; - if (PropertyDefinition.OtherMethods != null) { - foreach (var m in PropertyDefinition.OtherMethods) + public IEnumerable methodDefinitions() { + if (PropertyDef.GetMethod != null) + yield return PropertyDef.GetMethod; + if (PropertyDef.SetMethod != null) + yield return PropertyDef.SetMethod; + if (PropertyDef.OtherMethods != null) { + foreach (var m in PropertyDef.OtherMethods) yield return m; } } diff --git a/de4dot.code/renamer/asmmodules/Ref.cs b/de4dot.code/renamer/asmmodules/Ref.cs index c19ab949..1b2120df 100644 --- a/de4dot.code/renamer/asmmodules/Ref.cs +++ b/de4dot.code/renamer/asmmodules/Ref.cs @@ -17,15 +17,15 @@ along with de4dot. If not, see . */ -using Mono.Cecil; +using dot10.DotNet; namespace de4dot.code.renamer.asmmodules { abstract class Ref { - public readonly MemberReference memberReference; + public readonly ICodedToken memberReference; public int Index { get; set; } - public TypeDef Owner { get; set; } + public MTypeDef Owner { get; set; } - protected Ref(MemberReference memberReference, TypeDef owner, int index) { + protected Ref(ICodedToken memberReference, MTypeDef owner, int index) { this.memberReference = memberReference; Owner = owner; Index = index; diff --git a/de4dot.code/renamer/asmmodules/RefDict.cs b/de4dot.code/renamer/asmmodules/RefDict.cs index 1ba85b5c..3d378713 100644 --- a/de4dot.code/renamer/asmmodules/RefDict.cs +++ b/de4dot.code/renamer/asmmodules/RefDict.cs @@ -18,65 +18,64 @@ */ using System.Collections.Generic; -using Mono.Cecil; using de4dot.blocks; namespace de4dot.code.renamer.asmmodules { static class DictHelper { public static IEnumerable getSorted(IEnumerable values) where T : Ref { var list = new List(values); - list.Sort((a, b) => Utils.compareInt32(a.Index, b.Index)); + list.Sort((a, b) => a.Index.CompareTo(b.Index)); return list; } } - class TypeDefDict : TypeDefinitionDict { - public IEnumerable getSorted() { + class TypeDefDict : TypeDefinitionDict { + public IEnumerable getSorted() { return DictHelper.getSorted(getValues()); } - public void add(TypeDef typeDef) { - add(typeDef.TypeDefinition, typeDef); + public void add(MTypeDef typeDef) { + add(typeDef.TypeDef, typeDef); } } - class FieldDefDict : FieldDefinitionDict { - public IEnumerable getSorted() { + class FieldDefDict : FieldDefinitionDict { + public IEnumerable getSorted() { return DictHelper.getSorted(getValues()); } - public void add(FieldDef fieldDef) { - add(fieldDef.FieldDefinition, fieldDef); + public void add(MFieldDef fieldDef) { + add(fieldDef.FieldDef, fieldDef); } } - class MethodDefDict : MethodDefinitionDict { - public IEnumerable getSorted() { + class MethodDefDict : MethodDefinitionDict { + public IEnumerable getSorted() { return DictHelper.getSorted(getValues()); } - public void add(MethodDef methodDef) { - add(methodDef.MethodDefinition, methodDef); + public void add(MMethodDef methodDef) { + add(methodDef.MethodDef, methodDef); } } - class PropertyDefDict : PropertyDefinitionDict { - public IEnumerable getSorted() { + class PropertyDefDict : PropertyDefinitionDict { + public IEnumerable getSorted() { return DictHelper.getSorted(getValues()); } - public void add(PropertyDef propDef) { - add(propDef.PropertyDefinition, propDef); + public void add(MPropertyDef propDef) { + add(propDef.PropertyDef, propDef); } } - class EventDefDict : EventDefinitionDict { - public IEnumerable getSorted() { + class EventDefDict : EventDefinitionDict { + public IEnumerable getSorted() { return DictHelper.getSorted(getValues()); } - public void add(EventDef eventDef) { - add(eventDef.EventDefinition, eventDef); + public void add(MEventDef eventDef) { + add(eventDef.EventDef, eventDef); } } } diff --git a/de4dot.code/renamer/asmmodules/TypeDef.cs b/de4dot.code/renamer/asmmodules/TypeDef.cs index 7572365c..4cc6cbbb 100644 --- a/de4dot.code/renamer/asmmodules/TypeDef.cs +++ b/de4dot.code/renamer/asmmodules/TypeDef.cs @@ -19,14 +19,14 @@ using System; using System.Collections.Generic; -using Mono.Cecil; +using dot10.DotNet; using de4dot.blocks; namespace de4dot.code.renamer.asmmodules { class TypeInfo { public TypeReference typeReference; - public TypeDef typeDef; - public TypeInfo(TypeReference typeReference, TypeDef typeDef) { + public MTypeDef typeDef; + public TypeInfo(TypeReference typeReference, MTypeDef typeDef) { this.typeReference = typeReference; this.typeDef = typeDef; } @@ -55,29 +55,29 @@ namespace de4dot.code.renamer.asmmodules { } class MethodDefKey { - public readonly MethodDef methodDef; + public readonly MMethodDef methodDef; - public MethodDefKey(MethodDef methodDef) { + public MethodDefKey(MMethodDef methodDef) { this.methodDef = methodDef; } public override int GetHashCode() { - return MemberReferenceHelper.methodReferenceAndDeclaringTypeHashCode(methodDef.MethodDefinition); + return MemberReferenceHelper.methodReferenceAndDeclaringTypeHashCode(methodDef.MethodDef); } public override bool Equals(object obj) { var other = obj as MethodDefKey; if (other == null) return false; - return MemberReferenceHelper.compareMethodReferenceAndDeclaringType(methodDef.MethodDefinition, other.methodDef.MethodDefinition); + return MemberReferenceHelper.compareMethodReferenceAndDeclaringType(methodDef.MethodDef, other.methodDef.MethodDef); } } class MethodInst { - public MethodDef origMethodDef; - public MethodReference methodReference; + public MMethodDef origMethodDef; + public IMethod methodReference; - public MethodInst(MethodDef origMethodDef, MethodReference methodReference) { + public MethodInst(MMethodDef origMethodDef, IMethod methodReference) { this.origMethodDef = origMethodDef; this.methodReference = methodReference; } @@ -121,13 +121,13 @@ namespace de4dot.code.renamer.asmmodules { // Keeps track of which methods of an interface that have been implemented class InterfaceMethodInfo { TypeInfo iface; - Dictionary ifaceMethodToClassMethod = new Dictionary(); + Dictionary ifaceMethodToClassMethod = new Dictionary(); public TypeInfo IFace { get { return iface; } } - public Dictionary IfaceMethodToClassMethod { + public Dictionary IfaceMethodToClassMethod { get { return ifaceMethodToClassMethod; } } @@ -154,18 +154,18 @@ namespace de4dot.code.renamer.asmmodules { } // Returns the previous method, or null if none - public MethodDef addMethod(MethodDef ifaceMethod, MethodDef classMethod) { + public MMethodDef addMethod(MMethodDef ifaceMethod, MMethodDef classMethod) { var ifaceKey = new MethodDefKey(ifaceMethod); if (!ifaceMethodToClassMethod.ContainsKey(ifaceKey)) throw new ApplicationException("Could not find interface method"); - MethodDef oldMethod; + MMethodDef oldMethod; ifaceMethodToClassMethod.TryGetValue(ifaceKey, out oldMethod); ifaceMethodToClassMethod[ifaceKey] = classMethod; return oldMethod; } - public void addMethodIfEmpty(MethodDef ifaceMethod, MethodDef classMethod) { + public void addMethodIfEmpty(MMethodDef ifaceMethod, MMethodDef classMethod) { if (ifaceMethodToClassMethod[new MethodDefKey(ifaceMethod)] == null) addMethod(ifaceMethod, classMethod); } @@ -203,12 +203,12 @@ namespace de4dot.code.renamer.asmmodules { } // Returns the previous classMethod, or null if none - public MethodDef addMethod(TypeInfo iface, MethodDef ifaceMethod, MethodDef classMethod) { + public MMethodDef addMethod(TypeInfo iface, MMethodDef ifaceMethod, MMethodDef classMethod) { return addMethod(iface.typeReference, ifaceMethod, classMethod); } // Returns the previous classMethod, or null if none - public MethodDef addMethod(TypeReference iface, MethodDef ifaceMethod, MethodDef classMethod) { + public MMethodDef addMethod(TypeReference iface, MMethodDef ifaceMethod, MMethodDef classMethod) { InterfaceMethodInfo info; var key = new TypeReferenceKey(iface); if (!interfaceMethods.TryGetValue(key, out info)) @@ -216,7 +216,7 @@ namespace de4dot.code.renamer.asmmodules { return info.addMethod(ifaceMethod, classMethod); } - public void addMethodIfEmpty(TypeInfo iface, MethodDef ifaceMethod, MethodDef classMethod) { + public void addMethodIfEmpty(TypeInfo iface, MMethodDef ifaceMethod, MMethodDef classMethod) { InterfaceMethodInfo info; var key = new TypeReferenceKey(iface.typeReference); if (!interfaceMethods.TryGetValue(key, out info)) @@ -225,16 +225,16 @@ namespace de4dot.code.renamer.asmmodules { } } - class TypeDef : Ref { + class MTypeDef : Ref { EventDefDict events = new EventDefDict(); FieldDefDict fields = new FieldDefDict(); MethodDefDict methods = new MethodDefDict(); PropertyDefDict properties = new PropertyDefDict(); TypeDefDict types = new TypeDefDict(); - List genericParams; + List genericParams; internal TypeInfo baseType = null; internal IList interfaces = new List(); // directly implemented interfaces - internal IList derivedTypes = new List(); + internal IList derivedTypes = new List(); Module module; bool initializeVirtualMembersCalled = false; @@ -251,153 +251,153 @@ namespace de4dot.code.renamer.asmmodules { get { return module != null; } } - public IList GenericParams { + public IList GenericParams { get { return genericParams; } } - public IEnumerable NestedTypes { + public IEnumerable NestedTypes { get { return types.getSorted(); } } - public TypeDef NestingType { get; set; } + public MTypeDef NestingType { get; set; } - public TypeDefinition TypeDefinition { - get { return (TypeDefinition)memberReference; } + public TypeDef TypeDef { + get { return (TypeDef)memberReference; } } - public IEnumerable AllEvents { + public IEnumerable AllEvents { get { return events.getValues(); } } - public IEnumerable AllFields { + public IEnumerable AllFields { get { return fields.getValues(); } } - public IEnumerable AllMethods { + public IEnumerable AllMethods { get { return methods.getValues(); } } - public IEnumerable AllProperties { + public IEnumerable AllProperties { get { return properties.getValues(); } } - public IEnumerable AllEventsSorted { + public IEnumerable AllEventsSorted { get { return events.getSorted(); } } - public IEnumerable AllFieldsSorted { + public IEnumerable AllFieldsSorted { get { return fields.getSorted(); } } - public IEnumerable AllMethodsSorted { + public IEnumerable AllMethodsSorted { get { return methods.getSorted(); } } - public IEnumerable AllPropertiesSorted { + public IEnumerable AllPropertiesSorted { get { return properties.getSorted(); } } - public TypeDef(TypeDefinition typeDefinition, Module module, int index) + public MTypeDef(TypeDef typeDefinition, Module module, int index) : base(typeDefinition, null, index) { this.module = module; - genericParams = GenericParamDef.createGenericParamDefList(TypeDefinition.GenericParameters); + genericParams = MGenericParamDef.createGenericParamDefList(TypeDef.GenericParams); } - public void addInterface(TypeDef ifaceDef, TypeReference iface) { + public void addInterface(MTypeDef ifaceDef, TypeReference iface) { if (ifaceDef == null || iface == null) return; interfaces.Add(new TypeInfo(iface, ifaceDef)); } - public void addBaseType(TypeDef baseDef, TypeReference baseRef) { + public void addBaseType(MTypeDef baseDef, TypeReference baseRef) { if (baseDef == null || baseRef == null) return; baseType = new TypeInfo(baseRef, baseDef); } - public void add(EventDef e) { + public void add(MEventDef e) { events.add(e); } - public void add(FieldDef f) { + public void add(MFieldDef f) { fields.add(f); } - public void add(MethodDef m) { + public void add(MMethodDef m) { methods.add(m); } - public void add(PropertyDef p) { + public void add(MPropertyDef p) { properties.add(p); } - public void add(TypeDef t) { + public void add(MTypeDef t) { types.add(t); } - public MethodDef find(MethodReference mr) { + public MMethodDef find(MethodReference mr) { return methods.find(mr); } - public MethodDef findAny(MethodReference mr) { + public MMethodDef findAny(MethodReference mr) { return methods.findAny(mr); } - public FieldDef find(FieldReference fr) { + public MFieldDef find(FieldReference fr) { return fields.find(fr); } - public FieldDef findAny(FieldReference fr) { + public MFieldDef findAny(FieldReference fr) { return fields.findAny(fr); } - public PropertyDef find(PropertyReference pr) { + public MPropertyDef find(PropertyReference pr) { return properties.find(pr); } - public PropertyDef findAny(PropertyReference pr) { + public MPropertyDef findAny(PropertyReference pr) { return properties.findAny(pr); } - public EventDef find(EventReference er) { + public MEventDef find(EventReference er) { return events.find(er); } - public EventDef findAny(EventReference er) { + public MEventDef findAny(EventReference er) { return events.findAny(er); } - public PropertyDef create(PropertyDefinition newProp) { + public MPropertyDef create(PropertyDef newProp) { if (findAny(newProp) != null) throw new ApplicationException("Can't add a property when it's already been added"); - var propDef = new PropertyDef(newProp, this, properties.Count); + var propDef = new MPropertyDef(newProp, this, properties.Count); add(propDef); - TypeDefinition.Properties.Add(newProp); + TypeDef.Properties.Add(newProp); return propDef; } - public EventDef create(EventDefinition newEvent) { + public MEventDef create(EventDef newEvent) { if (findAny(newEvent) != null) throw new ApplicationException("Can't add an event when it's already been added"); - var eventDef = new EventDef(newEvent, this, events.Count); + var eventDef = new MEventDef(newEvent, this, events.Count); add(eventDef); - TypeDefinition.Events.Add(newEvent); + TypeDef.Events.Add(newEvent); return eventDef; } public void addMembers() { - var type = TypeDefinition; + var type = TypeDef; for (int i = 0; i < type.Events.Count; i++) - add(new EventDef(type.Events[i], this, i)); + add(new MEventDef(type.Events[i], this, i)); for (int i = 0; i < type.Fields.Count; i++) - add(new FieldDef(type.Fields[i], this, i)); + add(new MFieldDef(type.Fields[i], this, i)); for (int i = 0; i < type.Methods.Count; i++) - add(new MethodDef(type.Methods[i], this, i)); + add(new MMethodDef(type.Methods[i], this, i)); for (int i = 0; i < type.Properties.Count; i++) - add(new PropertyDef(type.Properties[i], this, i)); + add(new MPropertyDef(type.Properties[i], this, i)); foreach (var propDef in properties.getValues()) { foreach (var method in propDef.methodDefinitions()) { @@ -405,9 +405,9 @@ namespace de4dot.code.renamer.asmmodules { if (methodDef == null) throw new ApplicationException("Could not find property method"); methodDef.Property = propDef; - if (method == propDef.PropertyDefinition.GetMethod) + if (method == propDef.PropertyDef.GetMethod) propDef.GetMethod = methodDef; - if (method == propDef.PropertyDefinition.SetMethod) + if (method == propDef.PropertyDef.SetMethod) propDef.SetMethod = methodDef; } } @@ -418,11 +418,11 @@ namespace de4dot.code.renamer.asmmodules { if (methodDef == null) throw new ApplicationException("Could not find event method"); methodDef.Event = eventDef; - if (method == eventDef.EventDefinition.AddMethod) + if (method == eventDef.EventDef.AddMethod) eventDef.AddMethod = methodDef; - if (method == eventDef.EventDefinition.RemoveMethod) + if (method == eventDef.EventDef.RemoveMethod) eventDef.RemoveMethod = methodDef; - if (method == eventDef.EventDefinition.InvokeMethod) + if (method == eventDef.EventDef.InvokeMethod) eventDef.RaiseMethod = methodDef; } } @@ -442,8 +442,8 @@ namespace de4dot.code.renamer.asmmodules { public bool isGlobalType() { if (!isNested()) - return TypeDefinition.IsPublic; - var mask = TypeDefinition.Attributes & TypeAttributes.VisibilityMask; + return TypeDef.IsPublic; + var mask = TypeDef.Attributes & TypeAttributes.VisibilityMask; switch (mask) { case TypeAttributes.NestedPrivate: case TypeAttributes.NestedAssembly: @@ -500,7 +500,7 @@ namespace de4dot.code.renamer.asmmodules { void initializeInterfaceMethods(MethodNameGroups groups) { initializeAllInterfaces(); - if (TypeDefinition.IsInterface) + if (TypeDef.IsInterface) return; //--- Partition II 12.2 Implementing virtual methods on interfaces: @@ -512,7 +512,7 @@ namespace de4dot.code.renamer.asmmodules { //--- virtual functions. // Done. See initializeAllInterfaces(). - var methodsDict = new Dictionary(); + var methodsDict = new Dictionary(); //--- * If this class explicitly specifies that it implements the interface (i.e., the //--- interfaces that appear in this class‘ InterfaceImpl table, §22.23) @@ -524,7 +524,7 @@ namespace de4dot.code.renamer.asmmodules { foreach (var method in methods.getValues()) { if (!method.isPublic() || !method.isVirtual() || !method.isNewSlot()) continue; - methodsDict[new MethodReferenceKey(method.MethodDefinition)] = method; + methodsDict[new MethodReferenceKey(method.MethodDef)] = method; } foreach (var ifaceInfo in interfaces) { @@ -536,7 +536,7 @@ namespace de4dot.code.renamer.asmmodules { if (!ifaceMethod.isVirtual()) continue; var ifaceMethodReference = MethodReferenceInstance.make(methodInst.methodReference, ifaceInfo.typeReference as GenericInstanceType); - MethodDef classMethod; + MMethodDef classMethod; var key = new MethodReferenceKey(ifaceMethodReference); if (!methodsDict.TryGetValue(key, out classMethod)) continue; @@ -570,8 +570,8 @@ namespace de4dot.code.renamer.asmmodules { var ifaceMethod = methodsList[0].origMethodDef; if (!ifaceMethod.isVirtual()) continue; - var ifaceMethodRef = MethodReferenceInstance.make(ifaceMethod.MethodDefinition, ifaceInfo.typeReference as GenericInstanceType); - MethodDef classMethod; + var ifaceMethodRef = MethodReferenceInstance.make(ifaceMethod.MethodDef, ifaceInfo.typeReference as GenericInstanceType); + MMethodDef classMethod; var key = new MethodReferenceKey(ifaceMethodRef); if (!methodsDict.TryGetValue(key, out classMethod)) continue; @@ -583,21 +583,21 @@ namespace de4dot.code.renamer.asmmodules { //--- explicitly specified virtual methods into the interface in preference to those //--- inherited or chosen by name matching. methodsDict.Clear(); - var ifaceMethodsDict = new Dictionary(); + var ifaceMethodsDict = new Dictionary(); foreach (var ifaceInfo in allImplementedInterfaces.Keys) { var git = ifaceInfo.typeReference as GenericInstanceType; foreach (var ifaceMethod in ifaceInfo.typeDef.methods.getValues()) { - MethodReference ifaceMethodReference = ifaceMethod.MethodDefinition; + MethodReference ifaceMethodReference = ifaceMethod.MethodDef; if (git != null) - ifaceMethodReference = simpleClone(ifaceMethod.MethodDefinition, git); + ifaceMethodReference = simpleClone(ifaceMethod.MethodDef, git); ifaceMethodsDict[new MethodReferenceAndDeclaringTypeKey(ifaceMethodReference)] = ifaceMethod; } } foreach (var classMethod in methods.getValues()) { if (!classMethod.isVirtual()) continue; - foreach (var overrideMethod in classMethod.MethodDefinition.Overrides) { - MethodDef ifaceMethod; + foreach (var overrideMethod in classMethod.MethodDef.Overrides) { + MMethodDef ifaceMethod; var key = new MethodReferenceAndDeclaringTypeKey(overrideMethod); if (!ifaceMethodsDict.TryGetValue(key, out ifaceMethod)) { // We couldn't find the interface method (eg. interface not resolved) or @@ -620,14 +620,14 @@ namespace de4dot.code.renamer.asmmodules { if (!resolvedAllInterfaces() || !resolvedBaseClasses()) continue; // Ignore if COM class - if (!TypeDefinition.IsImport && + if (!TypeDef.IsImport && !hasAttribute("System.Runtime.InteropServices.ComImportAttribute") && !hasAttribute("System.Runtime.InteropServices.TypeLibTypeAttribute")) { Log.w("Could not find interface method {0} ({1:X8}). Type: {2} ({3:X8})", - Utils.removeNewlines(pair.Key.methodDef.MethodDefinition), - pair.Key.methodDef.MethodDefinition.MetadataToken.ToInt32(), - Utils.removeNewlines(TypeDefinition), - TypeDefinition.MetadataToken.ToInt32()); + Utils.removeNewlines(pair.Key.methodDef.MethodDef), + pair.Key.methodDef.MethodDef.MDToken.ToInt32(), + Utils.removeNewlines(TypeDef), + TypeDef.MDToken.ToInt32()); } } } @@ -636,7 +636,7 @@ namespace de4dot.code.renamer.asmmodules { foreach (var pair in info.IfaceMethodToClassMethod) { if (pair.Value == null) continue; - if (pair.Key.methodDef.MethodDefinition.Name != pair.Value.MethodDefinition.Name) + if (pair.Key.methodDef.MethodDef.Name != pair.Value.MethodDef.Name) continue; groups.same(pair.Key.methodDef, pair.Value); } @@ -644,7 +644,7 @@ namespace de4dot.code.renamer.asmmodules { } bool hasAttribute(string name) { - foreach (var attr in TypeDefinition.CustomAttributes) { + foreach (var attr in TypeDef.CustomAttributes) { if (attr.AttributeType.FullName == name) return true; } @@ -661,7 +661,7 @@ namespace de4dot.code.renamer.asmmodules { return resolvedAllInterfacesResult.Value; } bool resolvedAllInterfacesInternal() { - if (TypeDefinition.Interfaces.Count != interfaces.Count) + if (TypeDef.InterfaceImpls.Count != interfaces.Count) return false; foreach (var ifaceInfo in interfaces) { if (!ifaceInfo.typeDef.resolvedAllInterfaces()) @@ -680,7 +680,7 @@ namespace de4dot.code.renamer.asmmodules { return resolvedBaseClassesResult.Value; } bool resolvedBaseClassesInternal() { - if (TypeDefinition.BaseType == null) + if (TypeDef.BaseType == null) return true; if (baseType == null) return false; @@ -696,12 +696,12 @@ namespace de4dot.code.renamer.asmmodules { foreach (var p in methodReference.Parameters) m.Parameters.Add(new ParameterDefinition(p.Name, p.Attributes, p.ParameterType)); foreach (var gp in methodReference.GenericParameters) - m.GenericParameters.Add(new GenericParameter(declaringType)); + m.GenericParameters.Add(new GenericParam(declaringType)); return m; } void instantiateVirtualMembers(MethodNameGroups groups) { - if (!TypeDefinition.IsInterface) { + if (!TypeDef.IsInterface) { if (baseType != null) virtualMethodInstances.initializeFrom(baseType.typeDef.virtualMethodInstances, baseType.typeReference as GenericInstanceType); @@ -709,7 +709,7 @@ namespace de4dot.code.renamer.asmmodules { foreach (var methodDef in methods.getValues()) { if (!methodDef.isVirtual() || methodDef.isNewSlot()) continue; - var methodInstList = virtualMethodInstances.lookup(methodDef.MethodDefinition); + var methodInstList = virtualMethodInstances.lookup(methodDef.MethodDef); if (methodInstList == null) continue; foreach (var methodInst in methodInstList) @@ -720,7 +720,7 @@ namespace de4dot.code.renamer.asmmodules { foreach (var methodDef in methods.getValues()) { if (!methodDef.isVirtual()) continue; - virtualMethodInstances.add(new MethodInst(methodDef, methodDef.MethodDefinition)); + virtualMethodInstances.add(new MethodInst(methodDef, methodDef.MethodDef)); } } }