From 50eb86acfd9d7a674c2b9706f17e35448101c7b7 Mon Sep 17 00:00:00 2001 From: de4dot Date: Sat, 16 Nov 2013 20:04:23 +0100 Subject: [PATCH] Detect ILProtector version --- de4dot.code/de4dot.code.csproj | 1 + .../deobfuscators/ILProtector/Deobfuscator.cs | 25 ++- .../deobfuscators/ILProtector/MainType.cs | 23 ++- .../ILProtector/RuntimeFileInfo.cs | 164 ++++++++++++++++++ 4 files changed, 204 insertions(+), 9 deletions(-) create mode 100644 de4dot.code/deobfuscators/ILProtector/RuntimeFileInfo.cs diff --git a/de4dot.code/de4dot.code.csproj b/de4dot.code/de4dot.code.csproj index 1cee413e..17114906 100644 --- a/de4dot.code/de4dot.code.csproj +++ b/de4dot.code/de4dot.code.csproj @@ -237,6 +237,7 @@ + diff --git a/de4dot.code/deobfuscators/ILProtector/Deobfuscator.cs b/de4dot.code/deobfuscators/ILProtector/Deobfuscator.cs index 0594e3da..0f3187c8 100644 --- a/de4dot.code/deobfuscators/ILProtector/Deobfuscator.cs +++ b/de4dot.code/deobfuscators/ILProtector/Deobfuscator.cs @@ -94,8 +94,17 @@ namespace de4dot.code.deobfuscators.ILProtector { if (mainType.Detected && !staticMethodsDecrypter.Detected) dynamicMethodsRestorer = new DynamicMethodsRestorer(module, mainType); - if (mainType.Detected && staticMethodsDecrypter.Detected && staticMethodsDecrypter.Version != null) - obfuscatorName += " " + staticMethodsDecrypter.Version; + if (mainType.Detected) { + if (staticMethodsDecrypter.Detected) + UpdateObfuscatorNameWith(staticMethodsDecrypter.Version); + else + UpdateObfuscatorNameWith(mainType.GetRuntimeVersionString()); + } + } + + void UpdateObfuscatorNameWith(string version) { + if (!string.IsNullOrEmpty(version)) + obfuscatorName += " " + version; } public override void DeobfuscateBegin() { @@ -107,6 +116,14 @@ namespace de4dot.code.deobfuscators.ILProtector { RemoveObfuscatorJunk(staticMethodsDecrypter); } else if (dynamicMethodsRestorer != null) { + Logger.v("Runtime file versions:"); + Logger.Instance.Indent(); + foreach (var info in mainType.RuntimeFileInfos) { + var version = info.GetVersion(); + Logger.v("Version: {0} ({1})", version == null ? "UNKNOWN" : version.ToString(), info.PathName); + } + Logger.Instance.DeIndent(); + dynamicMethodsRestorer.Decrypt(); RemoveObfuscatorJunk(dynamicMethodsRestorer); } @@ -120,8 +137,8 @@ namespace de4dot.code.deobfuscators.ILProtector { AddResourceToBeRemoved(methodsDecrypter.Resource, "Encrypted methods resource"); AddTypeToBeRemoved(mainType.InvokerDelegate, "Invoker delegate type"); AddFieldToBeRemoved(mainType.InvokerInstanceField, "Invoker delegate instance field"); - foreach (var pm in mainType.ProtectMethods) - AddMethodToBeRemoved(pm, "Obfuscator 'Protect' init method"); + foreach (var info in mainType.RuntimeFileInfos) + AddMethodToBeRemoved(info.ProtectMethod, "Obfuscator 'Protect' init method"); mainType.CleanUp(); } diff --git a/de4dot.code/deobfuscators/ILProtector/MainType.cs b/de4dot.code/deobfuscators/ILProtector/MainType.cs index 92b8a399..bb026d4d 100644 --- a/de4dot.code/deobfuscators/ILProtector/MainType.cs +++ b/de4dot.code/deobfuscators/ILProtector/MainType.cs @@ -25,12 +25,12 @@ using de4dot.blocks; namespace de4dot.code.deobfuscators.ILProtector { class MainType { ModuleDefMD module; - List protectMethods; + List runtimeFileInfos; TypeDef invokerDelegate; FieldDef invokerInstanceField; - public IEnumerable ProtectMethods { - get { return protectMethods; } + public List RuntimeFileInfos { + get { return runtimeFileInfos; } } public TypeDef InvokerDelegate { @@ -42,7 +42,7 @@ namespace de4dot.code.deobfuscators.ILProtector { } public bool Detected { - get { return protectMethods != null; } + get { return runtimeFileInfos != null; } } public MainType(ModuleDefMD module) { @@ -81,7 +81,9 @@ namespace de4dot.code.deobfuscators.ILProtector { if (!GetDelegate(type, out invokerInstanceField, out invokerDelegate)) return false; - protectMethods = methods; + runtimeFileInfos = new List(methods.Count); + foreach (var method in methods) + runtimeFileInfos.Add(new RuntimeFileInfo(method)); return true; } @@ -109,6 +111,17 @@ namespace de4dot.code.deobfuscators.ILProtector { return list; } + public string GetRuntimeVersionString() { + if (runtimeFileInfos == null) + return null; + foreach (var info in runtimeFileInfos) { + var version = info.GetVersion(); + if (version != null) + return version.ToString(); + } + return null; + } + public void CleanUp() { var cctor = DotNetUtils.GetModuleTypeCctor(module); if (cctor != null) { diff --git a/de4dot.code/deobfuscators/ILProtector/RuntimeFileInfo.cs b/de4dot.code/deobfuscators/ILProtector/RuntimeFileInfo.cs new file mode 100644 index 00000000..e4da4f20 --- /dev/null +++ b/de4dot.code/deobfuscators/ILProtector/RuntimeFileInfo.cs @@ -0,0 +1,164 @@ +/* + Copyright (C) 2011-2013 de4dot@gmail.com + + This file is part of de4dot. + + de4dot is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + de4dot is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with de4dot. If not, see . +*/ + +using System; +using System.IO; +using System.Security.Cryptography; +using dnlib.DotNet; + +namespace de4dot.code.deobfuscators.ILProtector { + class RuntimeFileInfo { + const uint HASH_FILE_OFFSET = 0x00040000; + const int HASH_SIZE = 0x1000; + + public MethodDef ProtectMethod { get; private set; } + public string PathName { get; private set; } + public string Name { get; private set; } + Version runtimeVersion; + bool runtimeVersionInitialized; + + class VersionInfo { + public Version Version { get; private set; } + public uint FileOffset { get; private set; } + public byte[] Hash { get; private set; } + + public VersionInfo(Version version, byte[] hash) { + this.Version = version; + this.Hash = hash; + } + } + + static readonly VersionInfo[] versionInfo32 = new VersionInfo[] { + new VersionInfo(new Version(1, 0, 7, 1), new byte[] { 0x8D, 0xCE, 0xAB, 0x0C, 0xA6, 0xA9, 0x4A, 0xA2, 0xCE, 0x43, 0xDE, 0x38, 0xEA, 0xDE, 0x0D, 0x3A }), + new VersionInfo(new Version(1, 0, 8, 0), new byte[] { 0x44, 0x09, 0x33, 0xCE, 0x90, 0x43, 0xF9, 0xC2, 0x2F, 0x11, 0x40, 0x1D, 0x18, 0xDA, 0x63, 0x3B }), + new VersionInfo(new Version(2, 0, 0, 0), new byte[] { 0xFD, 0x1D, 0x2B, 0x73, 0x2F, 0xD8, 0x27, 0x5F, 0xA3, 0x83, 0x76, 0x36, 0x29, 0x8E, 0x51, 0xA9 }), + new VersionInfo(new Version(2, 0, 1, 0), new byte[] { 0x0D, 0x5E, 0xC2, 0xA5, 0x0D, 0x7C, 0xE1, 0x8B, 0x57, 0x9F, 0xC8, 0x16, 0x25, 0x95, 0x70, 0xEB }), + new VersionInfo(new Version(2, 0, 2, 0), new byte[] { 0x9C, 0xED, 0x53, 0x3A, 0x97, 0x4B, 0x9E, 0xC7, 0xF4, 0x82, 0xE9, 0x84, 0xB4, 0x9A, 0xEB, 0xA6 }), + new VersionInfo(new Version(2, 0, 3, 0), new byte[] { 0x0C, 0xDD, 0xCF, 0x04, 0x20, 0x6E, 0x7A, 0x48, 0x26, 0x8B, 0x97, 0x8E, 0x58, 0x17, 0x9B, 0x51 }), + new VersionInfo(new Version(2, 0, 4, 0), new byte[] { 0x1D, 0x41, 0xE7, 0x6D, 0x17, 0xED, 0x51, 0x37, 0xA0, 0xFC, 0x98, 0x5F, 0x19, 0x97, 0xEF, 0x9D }), + new VersionInfo(new Version(2, 0, 5, 0), new byte[] { 0x60, 0xD8, 0x1D, 0x1C, 0x0C, 0xBF, 0x46, 0x82, 0x9C, 0xE3, 0x73, 0x8D, 0x88, 0x2E, 0x0E, 0xBA }), + new VersionInfo(new Version(2, 0, 6, 0), new byte[] { 0x85, 0x89, 0x66, 0x39, 0xC3, 0x04, 0x3D, 0x3F, 0xFD, 0xBC, 0xFA, 0x70, 0x1D, 0x04, 0x59, 0x89 }), + new VersionInfo(new Version(2, 0, 7, 0), new byte[] { 0x16, 0x5C, 0xE1, 0xA4, 0x30, 0xF7, 0x55, 0x26, 0x45, 0xB8, 0x43, 0x99, 0x2A, 0xC9, 0x6F, 0xD9 }), + new VersionInfo(new Version(2, 0, 7, 5), new byte[] { 0x11, 0x50, 0x25, 0x2C, 0x26, 0x03, 0x8B, 0xDA, 0xBC, 0x98, 0x7E, 0x81, 0x97, 0x3E, 0xCE, 0x31 }), + new VersionInfo(new Version(2, 0, 7, 6), new byte[] { 0x6F, 0x9C, 0xF2, 0xDB, 0x83, 0x76, 0x92, 0x6A, 0xC4, 0xAA, 0xAE, 0xFA, 0x3D, 0xB3, 0x69, 0x59 }), + new VersionInfo(new Version(2, 0, 8, 0), new byte[] { 0x96, 0x84, 0xA0, 0x32, 0x3A, 0xB5, 0xAD, 0x89, 0xD7, 0xCD, 0x69, 0x34, 0xF1, 0x5D, 0xF3, 0x3A }), + new VersionInfo(new Version(2, 0, 8, 5), new byte[] { 0xB8, 0xCF, 0xEA, 0xBA, 0xB6, 0xAD, 0xE5, 0xCC, 0xFB, 0xA4, 0xE4, 0xFE, 0x1A, 0x83, 0xE5, 0x85 }), + new VersionInfo(new Version(2, 0, 9, 0), new byte[] { 0xA4, 0x68, 0x70, 0xA2, 0x1E, 0xBB, 0x99, 0xAB, 0xDD, 0x8C, 0xCA, 0x55, 0x32, 0x12, 0x95, 0xB5 }), + new VersionInfo(new Version(2, 0, 10, 0), new byte[] { 0xC5, 0xDC, 0x27, 0x22, 0x8F, 0x09, 0xFB, 0x56, 0x84, 0x6E, 0x07, 0x62, 0x4E, 0xBF, 0x71, 0xA6 }), + new VersionInfo(new Version(2, 0, 11, 0), new byte[] { 0x57, 0xAD, 0xBC, 0xB0, 0x7F, 0x80, 0xEF, 0xEA, 0xC3, 0xB7, 0x9F, 0x27, 0x87, 0x0A, 0x4B, 0xFF }), + }; + + static readonly VersionInfo[] versionInfo64 = new VersionInfo[] { + new VersionInfo(new Version(1, 0, 7, 1), new byte[] { 0x68, 0x4C, 0xC0, 0xC2, 0x55, 0x75, 0x72, 0x09, 0x12, 0x10, 0xA4, 0xF3, 0xFE, 0x95, 0x4D, 0x7A }), + new VersionInfo(new Version(1, 0, 8, 0), new byte[] { 0x99, 0xAF, 0x3D, 0x39, 0x16, 0xC2, 0xD6, 0x10, 0x6E, 0x09, 0x64, 0xDE, 0xA4, 0xB3, 0x30, 0xE5 }), + new VersionInfo(new Version(2, 0, 0, 0), new byte[] { 0x02, 0x90, 0xDA, 0xBD, 0x37, 0xEE, 0x20, 0x86, 0xA7, 0x30, 0x31, 0x6D, 0x92, 0xEF, 0xB3, 0x01 }), + new VersionInfo(new Version(2, 0, 1, 0), new byte[] { 0xE8, 0xE7, 0x11, 0x6B, 0x52, 0x60, 0x5A, 0x4D, 0x5A, 0xC6, 0x76, 0xF5, 0xD3, 0x64, 0xB1, 0x03 }), + new VersionInfo(new Version(2, 0, 2, 0), new byte[] { 0xD3, 0x37, 0xA1, 0x69, 0xF4, 0x25, 0x86, 0x19, 0xC6, 0x89, 0x70, 0x82, 0x9A, 0x3E, 0xCC, 0x04 }), + new VersionInfo(new Version(2, 0, 3, 0), new byte[] { 0xB1, 0xF8, 0xCB, 0xFD, 0x2D, 0x47, 0x36, 0xF1, 0x8A, 0x1D, 0xEF, 0xA7, 0x07, 0x0C, 0xD9, 0x74 }), + new VersionInfo(new Version(2, 0, 4, 0), new byte[] { 0x53, 0xC7, 0xA8, 0x3F, 0xD8, 0x9D, 0xA1, 0x85, 0x04, 0x50, 0x1D, 0x19, 0xF4, 0x1F, 0xF4, 0x44 }), + new VersionInfo(new Version(2, 0, 5, 0), new byte[] { 0x32, 0x82, 0x4B, 0xC7, 0xBB, 0x32, 0xBF, 0x9F, 0xB7, 0x9B, 0x06, 0x90, 0x7B, 0xB0, 0x7B, 0x7C }), + new VersionInfo(new Version(2, 0, 6, 0), new byte[] { 0x4F, 0x02, 0x86, 0xA6, 0xCA, 0xFD, 0xC1, 0x47, 0xA1, 0xDB, 0x2F, 0x73, 0x94, 0x38, 0x2B, 0xA7 }), + new VersionInfo(new Version(2, 0, 7, 0), new byte[] { 0x43, 0xA4, 0xC1, 0xA8, 0xC7, 0x36, 0x55, 0xEB, 0x3F, 0xFF, 0xA0, 0xB3, 0x85, 0x00, 0x21, 0x99 }), + new VersionInfo(new Version(2, 0, 7, 5), new byte[] { 0x6A, 0xA8, 0x2B, 0x28, 0x0B, 0xEE, 0x4C, 0xF0, 0x57, 0x3F, 0x43, 0xD4, 0xFB, 0xBC, 0x5E, 0x8C }), + new VersionInfo(new Version(2, 0, 7, 6), new byte[] { 0xE5, 0xD7, 0x81, 0xBD, 0x85, 0x02, 0x10, 0xAC, 0x92, 0x4A, 0xF0, 0x35, 0xD2, 0x4C, 0x50, 0xA5 }), + new VersionInfo(new Version(2, 0, 8, 0), new byte[] { 0x14, 0x44, 0xD1, 0x34, 0x69, 0x4D, 0xC7, 0xB7, 0x4D, 0x91, 0x0E, 0x6C, 0x4C, 0x9B, 0x46, 0x8E }), + new VersionInfo(new Version(2, 0, 8, 5), new byte[] { 0x5C, 0xEE, 0x47, 0x36, 0x2B, 0x10, 0xAD, 0x5F, 0x66, 0x6D, 0x3F, 0xD4, 0xF4, 0x4A, 0xFF, 0xA1 }), + new VersionInfo(new Version(2, 0, 9, 0), new byte[] { 0x2B, 0x80, 0x93, 0x78, 0x22, 0x29, 0x8C, 0xA1, 0xED, 0xE4, 0x59, 0x1A, 0xEC, 0x5B, 0xAF, 0xA7 }), + new VersionInfo(new Version(2, 0, 10, 0), new byte[] { 0xE3, 0x56, 0xB4, 0x98, 0x38, 0x27, 0x29, 0x27, 0x56, 0x87, 0x88, 0xAD, 0xA3, 0x50, 0x61, 0x24 }), + new VersionInfo(new Version(2, 0, 11, 0), new byte[] { 0x08, 0xDD, 0x40, 0x28, 0x08, 0x61, 0xDA, 0xD3, 0xD1, 0x38, 0x2F, 0x8A, 0xE0, 0x21, 0x0E, 0xE9 }), + }; + + public RuntimeFileInfo(MethodDef protectMethod) { + this.ProtectMethod = protectMethod; + Name = !protectMethod.HasImplMap ? "<>" : protectMethod.ImplMap.Module.Name.String; + PathName = Path.Combine(Path.GetDirectoryName(Utils.GetFullPath(protectMethod.Module.Location)), Name); + } + + public Version GetVersion() { + if (runtimeVersionInitialized) + return runtimeVersion; + runtimeVersion = GetVersion2(); + runtimeVersionInitialized = true; + return runtimeVersion; + } + + Version GetVersion2() { + try { + var hash = GetHash(PathName); + var info = GetVersionInfo(hash, versionInfo32); + if (info != null) + return info.Version; + info = GetVersionInfo(hash, versionInfo64); + if (info != null) + return info.Version; + } + catch { + } + return null; + } + + static VersionInfo GetVersionInfo(byte[] hash, VersionInfo[] infos) { + foreach (var info in infos) { + if (Equals(hash, info.Hash)) + return info; + } + return null; + } + + static byte[] GetHash(string fullPath) { + try { + using (var reader = new BinaryReader(new FileStream(fullPath, FileMode.Open, FileAccess.Read, FileShare.Read))) { + reader.BaseStream.Position = HASH_FILE_OFFSET; + var bytes = reader.ReadBytes(HASH_SIZE); + if (bytes.Length != HASH_SIZE) + return null; + + using (var hasher = MD5.Create()) { + using (var outStream = new NullStream()) { + using (var csStream = new CryptoStream(outStream, hasher, CryptoStreamMode.Write)) + new BinaryWriter(csStream).Write(bytes); + } + return hasher.Hash; + } + } + } + catch { + } + return null; + } + + static bool Equals(byte[] a, byte[] b) { + if (a == null && b == null) + return true; + if (a == null || b == null) + return false; + if (a.Length != b.Length) + return false; + for (int i = 0; i < a.Length; i++) { + if (a[i] != b[i]) + return false; + } + return true; + } + + public override string ToString() { + return PathName; + } + } +}