From 413b64766e69afc250adfdfd90ba23443b4a380b Mon Sep 17 00:00:00 2001 From: de4dot Date: Fri, 15 Nov 2013 21:51:37 +0100 Subject: [PATCH 01/14] Update error message --- de4dot.cui/Program.cs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/de4dot.cui/Program.cs b/de4dot.cui/Program.cs index d203e2e1..df7daac1 100644 --- a/de4dot.cui/Program.cs +++ b/de4dot.cui/Program.cs @@ -95,12 +95,8 @@ namespace de4dot.cui { else { Logger.Instance.LogErrorDontIgnore("\n\n"); Logger.Instance.LogErrorDontIgnore("Hmmmm... something didn't work. Try the latest version."); - Logger.Instance.LogErrorDontIgnore(" EX: {0} : message: {1}", ex.GetType(), ex.Message); - Logger.Instance.LogErrorDontIgnore("If it's a supported obfuscator, it could be a bug or a new obfuscator version."); - Logger.Instance.LogErrorDontIgnore("If it's an unsupported obfuscator, make sure the methods are decrypted!"); } - Logger.Instance.LogErrorDontIgnore("Send bug reports to de4dot@gmail.com or go to https://bitbucket.org/0xd4d/de4dot/issues"); - Logger.Instance.LogErrorDontIgnore("I will need the original files, so email me a link to the installer or a zip/rar file."); + Logger.Instance.LogErrorDontIgnore("Send a link to the installer or a zip file of all files to de4dot@gmail.com"); exitCode = 1; } From 5d308cc5df552e943079876fb111e7929e5adeec Mon Sep 17 00:00:00 2001 From: de4dot Date: Fri, 15 Nov 2013 21:55:09 +0100 Subject: [PATCH 02/14] Rename Main2() -> Main() --- AssemblyData/AssemblyServer.cs | 2 +- AssemblyServer-CLR20-x64/Program.cs | 2 +- AssemblyServer-CLR20/Program.cs | 2 +- AssemblyServer-CLR40-x64/Program.cs | 2 +- AssemblyServer-CLR40/Program.cs | 2 +- AssemblyServer-x64/Program.cs | 2 +- AssemblyServer/Program.cs | 2 +- de4dot-x64/Program.cs | 2 +- de4dot.cui/Program.cs | 2 +- de4dot/Program.cs | 2 +- 10 files changed, 10 insertions(+), 10 deletions(-) diff --git a/AssemblyData/AssemblyServer.cs b/AssemblyData/AssemblyServer.cs index a9110bd1..b677664f 100644 --- a/AssemblyData/AssemblyServer.cs +++ b/AssemblyData/AssemblyServer.cs @@ -26,7 +26,7 @@ using AssemblyData; namespace AssemblyServer { public static class Start { - public static int Main2(string[] args) { + public static int Main(string[] args) { if (args.Length != 3) Environment.Exit(1); var serviceType = (AssemblyServiceType)int.Parse(args[0]); diff --git a/AssemblyServer-CLR20-x64/Program.cs b/AssemblyServer-CLR20-x64/Program.cs index 0db8f1ad..b3ede556 100644 --- a/AssemblyServer-CLR20-x64/Program.cs +++ b/AssemblyServer-CLR20-x64/Program.cs @@ -20,7 +20,7 @@ namespace AssemblyServer_CLR20_x64 { class Program { static int Main(string[] args) { - return AssemblyServer.Start.Main2(args); + return AssemblyServer.Start.Main(args); } } } diff --git a/AssemblyServer-CLR20/Program.cs b/AssemblyServer-CLR20/Program.cs index c580a88d..535460d5 100644 --- a/AssemblyServer-CLR20/Program.cs +++ b/AssemblyServer-CLR20/Program.cs @@ -20,7 +20,7 @@ namespace AssemblyServer_CLR20 { class Program { static int Main(string[] args) { - return AssemblyServer.Start.Main2(args); + return AssemblyServer.Start.Main(args); } } } diff --git a/AssemblyServer-CLR40-x64/Program.cs b/AssemblyServer-CLR40-x64/Program.cs index 30f37320..60d617fc 100644 --- a/AssemblyServer-CLR40-x64/Program.cs +++ b/AssemblyServer-CLR40-x64/Program.cs @@ -20,7 +20,7 @@ namespace AssemblyServer_CLR40_x64 { class Program { static int Main(string[] args) { - return AssemblyServer.Start.Main2(args); + return AssemblyServer.Start.Main(args); } } } diff --git a/AssemblyServer-CLR40/Program.cs b/AssemblyServer-CLR40/Program.cs index 433a8c72..bcaa220c 100644 --- a/AssemblyServer-CLR40/Program.cs +++ b/AssemblyServer-CLR40/Program.cs @@ -20,7 +20,7 @@ namespace AssemblyServer_CLR40 { class Program { static int Main(string[] args) { - return AssemblyServer.Start.Main2(args); + return AssemblyServer.Start.Main(args); } } } diff --git a/AssemblyServer-x64/Program.cs b/AssemblyServer-x64/Program.cs index b58d0c0a..0ee64169 100644 --- a/AssemblyServer-x64/Program.cs +++ b/AssemblyServer-x64/Program.cs @@ -20,7 +20,7 @@ namespace AssemblyServer_x64 { class Program { static int Main(string[] args) { - return AssemblyServer.Start.Main2(args); + return AssemblyServer.Start.Main(args); } } } diff --git a/AssemblyServer/Program.cs b/AssemblyServer/Program.cs index 01f60a63..13075930 100644 --- a/AssemblyServer/Program.cs +++ b/AssemblyServer/Program.cs @@ -20,7 +20,7 @@ namespace AssemblyServer_x86 { class Program { static int Main(string[] args) { - return AssemblyServer.Start.Main2(args); + return AssemblyServer.Start.Main(args); } } } diff --git a/de4dot-x64/Program.cs b/de4dot-x64/Program.cs index d470a2b5..c94d19d2 100644 --- a/de4dot-x64/Program.cs +++ b/de4dot-x64/Program.cs @@ -20,7 +20,7 @@ namespace de4dot_x64 { class Program { static int Main(string[] args) { - return de4dot.cui.Program.Main2(args); + return de4dot.cui.Program.Main(args); } } } diff --git a/de4dot.cui/Program.cs b/de4dot.cui/Program.cs index df7daac1..13efbe19 100644 --- a/de4dot.cui/Program.cs +++ b/de4dot.cui/Program.cs @@ -61,7 +61,7 @@ namespace de4dot.cui { }; } - public static int Main2(string[] args) { + public static int Main(string[] args) { int exitCode = 0; const string showAllMessagesEnvName = "SHOWALLMESSAGES"; diff --git a/de4dot/Program.cs b/de4dot/Program.cs index dc63c8a0..e03518cf 100644 --- a/de4dot/Program.cs +++ b/de4dot/Program.cs @@ -20,7 +20,7 @@ namespace de4dot_x86 { class Program { static int Main(string[] args) { - return de4dot.cui.Program.Main2(args); + return de4dot.cui.Program.Main(args); } } } From 50eb86acfd9d7a674c2b9706f17e35448101c7b7 Mon Sep 17 00:00:00 2001 From: de4dot Date: Sat, 16 Nov 2013 20:04:23 +0100 Subject: [PATCH 03/14] 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; + } + } +} From 120289d12bba93dcf402f10c8f2baebddb64463f Mon Sep 17 00:00:00 2001 From: de4dot Date: Sat, 16 Nov 2013 20:04:38 +0100 Subject: [PATCH 04/14] Write a warning message if runtime files are missing --- .../deobfuscators/ILProtector/DynamicMethodsRestorer.cs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/de4dot.code/deobfuscators/ILProtector/DynamicMethodsRestorer.cs b/de4dot.code/deobfuscators/ILProtector/DynamicMethodsRestorer.cs index ecbff882..c54f007e 100644 --- a/de4dot.code/deobfuscators/ILProtector/DynamicMethodsRestorer.cs +++ b/de4dot.code/deobfuscators/ILProtector/DynamicMethodsRestorer.cs @@ -19,6 +19,7 @@ using System; using System.Collections.Generic; +using System.IO; using dnlib.DotNet; using AssemblyData; using de4dot.code.AssemblyClient; @@ -31,6 +32,7 @@ namespace de4dot.code.deobfuscators.ILProtector { } protected override void DecryptInternal() { + CheckRuntimeFiles(); IList decryptedData; var serverVersion = NewProcessAssemblyClientFactory.GetServerClrVersion(module); using (var client = new NewProcessAssemblyClientFactory(serverVersion).Create(AssemblyServiceType.Generic)) { @@ -49,6 +51,13 @@ namespace de4dot.code.deobfuscators.ILProtector { methodInfos[info.id] = info; } + void CheckRuntimeFiles() { + foreach (var info in mainType.RuntimeFileInfos) { + if (!File.Exists(info.PathName)) + Logger.w(string.Format("ILProtector runtime file '{0}' is missing.", info.PathName)); + } + } + IList GetMethodIds() { var ids = new List(); From a6eee49ff0d616c38f957426f9c21f5ee0a0e6ca Mon Sep 17 00:00:00 2001 From: de4dot Date: Sat, 16 Nov 2013 22:44:06 +0100 Subject: [PATCH 05/14] Update msg --- de4dot.cui/Program.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/de4dot.cui/Program.cs b/de4dot.cui/Program.cs index 13efbe19..3fe3ea93 100644 --- a/de4dot.cui/Program.cs +++ b/de4dot.cui/Program.cs @@ -96,7 +96,7 @@ namespace de4dot.cui { Logger.Instance.LogErrorDontIgnore("\n\n"); Logger.Instance.LogErrorDontIgnore("Hmmmm... something didn't work. Try the latest version."); } - Logger.Instance.LogErrorDontIgnore("Send a link to the installer or a zip file of all files to de4dot@gmail.com"); + Logger.Instance.LogErrorDontIgnore("Email me all files / installer: de4dot@gmail.com"); exitCode = 1; } From aa01d4eebba625410a010c3568d1b048991c7ee4 Mon Sep 17 00:00:00 2001 From: de4dot Date: Sun, 17 Nov 2013 02:04:11 +0100 Subject: [PATCH 06/14] Add 1.0.7.0 sig --- de4dot.code/deobfuscators/ILProtector/Deobfuscator.cs | 5 +++++ .../deobfuscators/ILProtector/DynamicMethodsDecrypter.cs | 8 ++++---- de4dot.code/deobfuscators/ILProtector/RuntimeFileInfo.cs | 2 ++ .../deobfuscators/ILProtector/StaticMethodsDecrypter.cs | 6 +++--- 4 files changed, 14 insertions(+), 7 deletions(-) diff --git a/de4dot.code/deobfuscators/ILProtector/Deobfuscator.cs b/de4dot.code/deobfuscators/ILProtector/Deobfuscator.cs index 0f3187c8..9ad0a082 100644 --- a/de4dot.code/deobfuscators/ILProtector/Deobfuscator.cs +++ b/de4dot.code/deobfuscators/ILProtector/Deobfuscator.cs @@ -118,11 +118,16 @@ namespace de4dot.code.deobfuscators.ILProtector { else if (dynamicMethodsRestorer != null) { Logger.v("Runtime file versions:"); Logger.Instance.Indent(); + bool emailMe = false; foreach (var info in mainType.RuntimeFileInfos) { var version = info.GetVersion(); + emailMe |= version == null && System.IO.File.Exists(info.PathName); + emailMe |= version != null && version == new Version(1, 0, 7, 0); Logger.v("Version: {0} ({1})", version == null ? "UNKNOWN" : version.ToString(), info.PathName); } Logger.Instance.DeIndent(); + if (emailMe) + Logger.n("**** Email me this program! de4dot@gmail.com"); dynamicMethodsRestorer.Decrypt(); RemoveObfuscatorJunk(dynamicMethodsRestorer); diff --git a/de4dot.code/deobfuscators/ILProtector/DynamicMethodsDecrypter.cs b/de4dot.code/deobfuscators/ILProtector/DynamicMethodsDecrypter.cs index dcae0f9f..8d4f6e05 100644 --- a/de4dot.code/deobfuscators/ILProtector/DynamicMethodsDecrypter.cs +++ b/de4dot.code/deobfuscators/ILProtector/DynamicMethodsDecrypter.cs @@ -159,13 +159,13 @@ namespace de4dot.code.deobfuscators.ILProtector { public abstract byte[] Decrypt(int methodId, uint rid); } - // 1.0.7.1 - 1.0.8.0 - class DecrypterV1_0_7_1 : DecrypterBase { + // 1.0.7.0 - 1.0.8.0 + class DecrypterV1_0_7_0 : DecrypterBase { DecryptMethod decryptMethod; unsafe delegate bool DecryptMethod(int appDomainId, int asmHashCode, int methodId, out byte* pMethodCode, out int methodSize); - public DecrypterV1_0_7_1(DynamicMethodsDecrypter dmd, FieldDef delegateField) + public DecrypterV1_0_7_0(DynamicMethodsDecrypter dmd, FieldDef delegateField) : base(dmd) { IntPtr addr = GetDelegateAddress(delegateField); decryptMethod = (DecryptMethod)Marshal.GetDelegateForFunctionPointer(addr, typeof(DecryptMethod)); @@ -348,7 +348,7 @@ namespace de4dot.code.deobfuscators.ILProtector { if (delegateField == null) return null; - return new DecrypterV1_0_7_1(this, delegateField); + return new DecrypterV1_0_7_0(this, delegateField); } IDecrypter CreateDecrypterV2_0_0_0() { diff --git a/de4dot.code/deobfuscators/ILProtector/RuntimeFileInfo.cs b/de4dot.code/deobfuscators/ILProtector/RuntimeFileInfo.cs index e4da4f20..b6333553 100644 --- a/de4dot.code/deobfuscators/ILProtector/RuntimeFileInfo.cs +++ b/de4dot.code/deobfuscators/ILProtector/RuntimeFileInfo.cs @@ -45,6 +45,7 @@ namespace de4dot.code.deobfuscators.ILProtector { } static readonly VersionInfo[] versionInfo32 = new VersionInfo[] { + new VersionInfo(new Version(1, 0, 7, 0), new byte[] { 0x94, 0x79, 0x6E, 0xC1, 0x1F, 0x6D, 0xE9, 0xE3, 0x2F, 0x5E, 0xA3, 0x57, 0x71, 0x02, 0x22, 0x6A }), 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 }), @@ -65,6 +66,7 @@ namespace de4dot.code.deobfuscators.ILProtector { }; static readonly VersionInfo[] versionInfo64 = new VersionInfo[] { + // No sig for 1.0.7.0 x64 since I don't have it yet. 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 }), diff --git a/de4dot.code/deobfuscators/ILProtector/StaticMethodsDecrypter.cs b/de4dot.code/deobfuscators/ILProtector/StaticMethodsDecrypter.cs index c614017b..7bd1e9ee 100644 --- a/de4dot.code/deobfuscators/ILProtector/StaticMethodsDecrypter.cs +++ b/de4dot.code/deobfuscators/ILProtector/StaticMethodsDecrypter.cs @@ -146,13 +146,13 @@ namespace de4dot.code.deobfuscators.ILProtector { } } - // 1.0.6 - 1.0.7.0 + // 1.0.6.x class DecrypterV106 : DecrypterBase { byte[] decryptionKey6; byte[] decryptionKey7; DecrypterV106(byte[] key0, byte[] key6, byte[] key7, int startOffset) { - this.ilpVersion = "1.0.6 - 1.0.7.0"; + this.ilpVersion = "1.0.6.x"; this.startOffset = startOffset; this.decryptionKey = key0; this.decryptionKey6 = key6; @@ -178,7 +178,7 @@ namespace de4dot.code.deobfuscators.ILProtector { var key0 = DeobUtils.Sha1Sum(sha1Data); // 1.0.6.0 var key6 = GetKey(reader, key0, keyXorOffs6); // 1.0.6.6 - var key7 = GetKey(reader, key0, keyXorOffs7); // 1.0.6.7 - 1.0.7.0 + var key7 = GetKey(reader, key0, keyXorOffs7); // 1.0.6.7 return new DecrypterV106(key0, key6, key7, encryptedOffs); } catch (IOException) { From 8c07ba5db519ed0e8b9b12d0948351f0d6c56882 Mon Sep 17 00:00:00 2001 From: de4dot Date: Sun, 17 Nov 2013 18:11:14 +0100 Subject: [PATCH 07/14] Don't print an extra 's' if it's just one error/warning --- de4dot.cui/Program.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/de4dot.cui/Program.cs b/de4dot.cui/Program.cs index 3fe3ea93..654e9029 100644 --- a/de4dot.cui/Program.cs +++ b/de4dot.cui/Program.cs @@ -101,7 +101,10 @@ namespace de4dot.cui { } if (Logger.Instance.NumIgnoredMessages > 0) { - Logger.n("Ignored {0} warnings/errors", Logger.Instance.NumIgnoredMessages); + if (Logger.Instance.NumIgnoredMessages == 1) + Logger.n("Ignored {0} warning/error", Logger.Instance.NumIgnoredMessages); + else + Logger.n("Ignored {0} warnings/errors", Logger.Instance.NumIgnoredMessages); Logger.n("Use -v/-vv option or set environment variable {0}=1 to see all messages", showAllMessagesEnvName); } From e8879cd51fb359f3a2891af21a5d9cf3db61c811 Mon Sep 17 00:00:00 2001 From: de4dot Date: Sun, 17 Nov 2013 18:12:21 +0100 Subject: [PATCH 08/14] Print old -> new tokens if -v option is used --- de4dot.code/ObfuscatedFile.cs | 2 +- de4dot.code/PrintNewTokens.cs | 107 +++++++++++++++++++++++++++++++++ de4dot.code/de4dot.code.csproj | 1 + 3 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 de4dot.code/PrintNewTokens.cs diff --git a/de4dot.code/ObfuscatedFile.cs b/de4dot.code/ObfuscatedFile.cs index 35cc83e0..e613f31b 100644 --- a/de4dot.code/ObfuscatedFile.cs +++ b/de4dot.code/ObfuscatedFile.cs @@ -347,7 +347,7 @@ namespace de4dot.code { var mdFlags = GetMetaDataFlags(); if (!options.ControlFlowDeobfuscation) mdFlags |= MetaDataFlags.KeepOldMaxStack; - assemblyModule.Save(options.NewFilename, mdFlags, deob as IModuleWriterListener); + assemblyModule.Save(options.NewFilename, mdFlags, new PrintNewTokens(module, deob as IModuleWriterListener)); } IList GetAllMethods() { diff --git a/de4dot.code/PrintNewTokens.cs b/de4dot.code/PrintNewTokens.cs new file mode 100644 index 00000000..0ff4308a --- /dev/null +++ b/de4dot.code/PrintNewTokens.cs @@ -0,0 +1,107 @@ +/* + 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 dnlib.DotNet; +using dnlib.DotNet.MD; +using dnlib.DotNet.Writer; + +namespace de4dot.code { + class PrintNewTokens : IModuleWriterListener { + readonly ModuleDef module; + readonly IModuleWriterListener otherListener; + + public PrintNewTokens(ModuleDef module, IModuleWriterListener otherListener) { + this.module = module; + this.otherListener = otherListener; + } + + public void OnWriterEvent(ModuleWriterBase writer, ModuleWriterEvent evt) { + if (otherListener != null) + otherListener.OnWriterEvent(writer, evt); + if (evt == ModuleWriterEvent.End) + PrintTokens(writer); + } + + void PrintTokens(ModuleWriterBase writer) { + if (Logger.Instance.IgnoresEvent(LoggerEvent.Verbose)) + return; + + var md = writer.MetaData; + + Logger.v("Old -> new tokens: Assembly: {0} (module: {1})", module.Assembly, module.Location); + Logger.Instance.Indent(); + foreach (var type in module.GetTypes()) { + uint newRid; + + newRid = md.GetRid(type); + if (newRid == 0) + continue; + Logger.v("{0:X8} -> {1:X8} Type: {2}", + type.MDToken.ToUInt32(), + new MDToken(Table.TypeDef, newRid).ToUInt32(), + Utils.RemoveNewlines(type)); + + Logger.Instance.Indent(); + + foreach (var method in type.Methods) { + newRid = md.GetRid(method); + if (newRid == 0) + continue; + Logger.v("{0:X8} -> {1:X8} Method: {2}", + method.MDToken.ToUInt32(), + new MDToken(Table.Method, newRid).ToUInt32(), + Utils.RemoveNewlines(method)); + } + + foreach (var field in type.Fields) { + newRid = md.GetRid(field); + if (newRid == 0) + continue; + Logger.v("{0:X8} -> {1:X8} Field: {2}", + field.MDToken.ToUInt32(), + new MDToken(Table.Field, newRid).ToUInt32(), + Utils.RemoveNewlines(field)); + } + + foreach (var prop in type.Properties) { + newRid = md.GetRid(prop); + if (newRid == 0) + continue; + Logger.v("{0:X8} -> {1:X8} Property: {2}", + prop.MDToken.ToUInt32(), + new MDToken(Table.Property, newRid).ToUInt32(), + Utils.RemoveNewlines(prop)); + } + + foreach (var evt in type.Events) { + newRid = md.GetRid(evt); + if (newRid == 0) + continue; + Logger.v("{0:X8} -> {1:X8} Event: {2}", + evt.MDToken.ToUInt32(), + new MDToken(Table.Event, newRid).ToUInt32(), + Utils.RemoveNewlines(evt)); + } + + Logger.Instance.DeIndent(); + } + Logger.Instance.DeIndent(); + } + } +} diff --git a/de4dot.code/de4dot.code.csproj b/de4dot.code/de4dot.code.csproj index 17114906..9f48a2e0 100644 --- a/de4dot.code/de4dot.code.csproj +++ b/de4dot.code/de4dot.code.csproj @@ -311,6 +311,7 @@ + From 8b4b48cb083a2cd8f16e4adb0aa7b511ed8e5ca3 Mon Sep 17 00:00:00 2001 From: de4dot Date: Sun, 17 Nov 2013 20:21:44 +0100 Subject: [PATCH 09/14] Add ToString() overrides --- de4dot.code/resources/BuiltInResourceData.cs | 34 ++++++++++++++++++++ de4dot.code/resources/ResourceElement.cs | 4 +++ de4dot.code/resources/UserResourceData.cs | 16 +++++++++ 3 files changed, 54 insertions(+) diff --git a/de4dot.code/resources/BuiltInResourceData.cs b/de4dot.code/resources/BuiltInResourceData.cs index dcd63eba..9c2b1d05 100644 --- a/de4dot.code/resources/BuiltInResourceData.cs +++ b/de4dot.code/resources/BuiltInResourceData.cs @@ -118,5 +118,39 @@ namespace de4dot.code.resources { throw new ApplicationException("Unknown resource type code"); } } + + public override string ToString() { + switch (code) { + case ResourceTypeCode.Null: + return "NULL"; + + case ResourceTypeCode.String: + case ResourceTypeCode.Boolean: + case ResourceTypeCode.Char: + case ResourceTypeCode.Byte: + case ResourceTypeCode.SByte: + case ResourceTypeCode.Int16: + case ResourceTypeCode.UInt16: + case ResourceTypeCode.Int32: + case ResourceTypeCode.UInt32: + case ResourceTypeCode.Int64: + case ResourceTypeCode.UInt64: + case ResourceTypeCode.Single: + case ResourceTypeCode.Double: + case ResourceTypeCode.Decimal: + case ResourceTypeCode.DateTime: + case ResourceTypeCode.TimeSpan: + return string.Format("{0}: '{1}'", code, data); + + case ResourceTypeCode.ByteArray: + var ary = data as byte[]; + if (ary != null) + return string.Format("{0}: Length: {1}", code, ary.Length); + return string.Format("{0}: '{1}'", code, data); + + default: + return string.Format("{0}: '{1}'", code, data); + } + } } } diff --git a/de4dot.code/resources/ResourceElement.cs b/de4dot.code/resources/ResourceElement.cs index e57ea5fc..7a81b5f3 100644 --- a/de4dot.code/resources/ResourceElement.cs +++ b/de4dot.code/resources/ResourceElement.cs @@ -21,5 +21,9 @@ namespace de4dot.code.resources { class ResourceElement { public string Name { get; set; } public IResourceData ResourceData { get; set; } + + public override string ToString() { + return string.Format("N: {0}, V: {1}", Name, ResourceData); + } } } diff --git a/de4dot.code/resources/UserResourceData.cs b/de4dot.code/resources/UserResourceData.cs index 4baadf52..3dda09cd 100644 --- a/de4dot.code/resources/UserResourceData.cs +++ b/de4dot.code/resources/UserResourceData.cs @@ -52,6 +52,10 @@ namespace de4dot.code.resources { public override void WriteData(BinaryWriter writer, IFormatter formatter) { formatter.Serialize(writer.BaseStream, data); } + + public override string ToString() { + return string.Format("char[]: Length: {0}", data.Length); + } } class IconResourceData : UserResourceData { @@ -66,6 +70,10 @@ namespace de4dot.code.resources { public override void WriteData(BinaryWriter writer, IFormatter formatter) { formatter.Serialize(writer.BaseStream, icon); } + + public override string ToString() { + return string.Format("Icon: {0}", icon); + } } class ImageResourceData : UserResourceData { @@ -80,6 +88,10 @@ namespace de4dot.code.resources { public override void WriteData(BinaryWriter writer, IFormatter formatter) { formatter.Serialize(writer.BaseStream, bitmap); } + + public override string ToString() { + return "Bitmap"; + } } class BinaryResourceData : UserResourceData { @@ -93,5 +105,9 @@ namespace de4dot.code.resources { public override void WriteData(BinaryWriter writer, IFormatter formatter) { writer.Write(data); } + + public override string ToString() { + return string.Format("Binary: Length: {0}", data.Length); + } } } From 8521f7baac49588ffe3398a5cda766e069817fe8 Mon Sep 17 00:00:00 2001 From: de4dot Date: Sun, 17 Nov 2013 20:23:32 +0100 Subject: [PATCH 10/14] ResourceReader.Read() now takes a ModuleDef instead of a ModuleDefMD as 1st param --- de4dot.code/resources/ResourceDataCreator.cs | 18 +++++++++++------- de4dot.code/resources/ResourceReader.cs | 6 ++---- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/de4dot.code/resources/ResourceDataCreator.cs b/de4dot.code/resources/ResourceDataCreator.cs index 0c20f00a..c1026a26 100644 --- a/de4dot.code/resources/ResourceDataCreator.cs +++ b/de4dot.code/resources/ResourceDataCreator.cs @@ -26,12 +26,14 @@ using dnlib.DotNet; namespace de4dot.code.resources { class ResourceDataCreator { - ModuleDefMD module; - Dictionary dict = new Dictionary(StringComparer.Ordinal); - Dictionary asmNameToAsmFullName = new Dictionary(StringComparer.Ordinal); + readonly ModuleDef module; + readonly ModuleDefMD moduleMD; + readonly Dictionary dict = new Dictionary(StringComparer.Ordinal); + readonly Dictionary asmNameToAsmFullName = new Dictionary(StringComparer.Ordinal); - public ResourceDataCreator(ModuleDefMD module) { + public ResourceDataCreator(ModuleDef module) { this.module = module; + this.moduleMD = module as ModuleDefMD; } public int Count { @@ -210,9 +212,11 @@ namespace de4dot.code.resources { string TryGetRealAssemblyName(string assemblyName) { var simpleName = Utils.GetAssemblySimpleName(assemblyName); - var asmRef = module.GetAssemblyRef(simpleName); - if (asmRef != null) - return asmRef.FullName; + if (moduleMD != null) { + var asmRef = moduleMD.GetAssemblyRef(simpleName); + if (asmRef != null) + return asmRef.FullName; + } var asm = TheAssemblyResolver.Instance.Resolve(new AssemblyNameInfo(simpleName), module); return asm == null ? null : asm.FullName; diff --git a/de4dot.code/resources/ResourceReader.cs b/de4dot.code/resources/ResourceReader.cs index 2982ee35..5e7fb105 100644 --- a/de4dot.code/resources/ResourceReader.cs +++ b/de4dot.code/resources/ResourceReader.cs @@ -34,17 +34,15 @@ namespace de4dot.code.resources { } struct ResourceReader { - ModuleDefMD module; IBinaryReader reader; ResourceDataCreator resourceDataCreator; - ResourceReader(ModuleDefMD module, IBinaryReader reader) { - this.module = module; + ResourceReader(ModuleDef module, IBinaryReader reader) { this.reader = reader; this.resourceDataCreator = new ResourceDataCreator(module); } - public static ResourceElementSet Read(ModuleDefMD module, IBinaryReader reader) { + public static ResourceElementSet Read(ModuleDef module, IBinaryReader reader) { return new ResourceReader(module, reader).Read(); } From b7eb39203aea8915f5f4ad0583a7773cc6e1b4f7 Mon Sep 17 00:00:00 2001 From: de4dot Date: Sun, 17 Nov 2013 20:24:55 +0100 Subject: [PATCH 11/14] Add Stream resource type --- de4dot.code/resources/BuiltInResourceData.cs | 2 ++ de4dot.code/resources/ResourceDataCreator.cs | 4 ++++ de4dot.code/resources/ResourceReader.cs | 1 + de4dot.code/resources/ResourceTypeCode.cs | 1 + 4 files changed, 8 insertions(+) diff --git a/de4dot.code/resources/BuiltInResourceData.cs b/de4dot.code/resources/BuiltInResourceData.cs index 9c2b1d05..66164eff 100644 --- a/de4dot.code/resources/BuiltInResourceData.cs +++ b/de4dot.code/resources/BuiltInResourceData.cs @@ -109,6 +109,7 @@ namespace de4dot.code.resources { break; case ResourceTypeCode.ByteArray: + case ResourceTypeCode.Stream: var ary = (byte[])data; writer.Write(ary.Length); writer.Write(ary); @@ -143,6 +144,7 @@ namespace de4dot.code.resources { return string.Format("{0}: '{1}'", code, data); case ResourceTypeCode.ByteArray: + case ResourceTypeCode.Stream: var ary = data as byte[]; if (ary != null) return string.Format("{0}: Length: {1}", code, ary.Length); diff --git a/de4dot.code/resources/ResourceDataCreator.cs b/de4dot.code/resources/ResourceDataCreator.cs index c1026a26..cad6723d 100644 --- a/de4dot.code/resources/ResourceDataCreator.cs +++ b/de4dot.code/resources/ResourceDataCreator.cs @@ -112,6 +112,10 @@ namespace de4dot.code.resources { return new BuiltInResourceData(ResourceTypeCode.ByteArray, value); } + public BuiltInResourceData CreateStream(byte[] value) { + return new BuiltInResourceData(ResourceTypeCode.Stream, value); + } + public CharArrayResourceData Create(char[] value) { return new CharArrayResourceData(CreateUserResourceType(CharArrayResourceData.typeName), value); } diff --git a/de4dot.code/resources/ResourceReader.cs b/de4dot.code/resources/ResourceReader.cs index 5e7fb105..6c515123 100644 --- a/de4dot.code/resources/ResourceReader.cs +++ b/de4dot.code/resources/ResourceReader.cs @@ -139,6 +139,7 @@ namespace de4dot.code.resources { case ResourceTypeCode.DateTime: return resourceDataCreator.Create(new DateTime(reader.ReadInt64())); case ResourceTypeCode.TimeSpan: return resourceDataCreator.Create(new TimeSpan(reader.ReadInt64())); case ResourceTypeCode.ByteArray: return resourceDataCreator.Create(reader.ReadBytes(reader.ReadInt32())); + case ResourceTypeCode.Stream: return resourceDataCreator.CreateStream(reader.ReadBytes(reader.ReadInt32())); default: int userTypeIndex = (int)(code - (uint)ResourceTypeCode.UserTypes); if (userTypeIndex < 0 || userTypeIndex >= userTypes.Count) diff --git a/de4dot.code/resources/ResourceTypeCode.cs b/de4dot.code/resources/ResourceTypeCode.cs index 7e34e199..cb20ab80 100644 --- a/de4dot.code/resources/ResourceTypeCode.cs +++ b/de4dot.code/resources/ResourceTypeCode.cs @@ -37,6 +37,7 @@ namespace de4dot.code.resources { DateTime, TimeSpan, ByteArray = 0x20, + Stream = 0x21, UserTypes = 0x40, } } From 781ade32cd77c4c67b669e6a8a283a1f24d627ac Mon Sep 17 00:00:00 2001 From: de4dot Date: Sun, 17 Nov 2013 20:25:40 +0100 Subject: [PATCH 12/14] Warn user if trying to rename an asm using XAML --- de4dot.code/renamer/Renamer.cs | 36 ++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/de4dot.code/renamer/Renamer.cs b/de4dot.code/renamer/Renamer.cs index 915efe59..fe3b0133 100644 --- a/de4dot.code/renamer/Renamer.cs +++ b/de4dot.code/renamer/Renamer.cs @@ -23,6 +23,7 @@ using System.Text.RegularExpressions; using dnlib.DotNet; using dnlib.DotNet.Emit; using de4dot.code.renamer.asmmodules; +using de4dot.code.resources; using de4dot.blocks; namespace de4dot.code.renamer { @@ -187,6 +188,8 @@ namespace de4dot.code.renamer { public Renamer(IDeobfuscatorContext deobfuscatorContext, IEnumerable files, RenamerFlags flags) { RenamerFlags = flags; + WarnIfXaml(files); + modules = new Modules(deobfuscatorContext); isDelegateClass = new DerivedFrom(delegateClasses); mergeStateHelper = new MergeStateHelper(memberInfos); @@ -195,6 +198,39 @@ namespace de4dot.code.renamer { modules.Add(new Module(file)); } + static void WarnIfXaml(IEnumerable files) { + foreach (var file in files) { + foreach (var tmp in file.ModuleDefMD.Resources) { + var rsrc = tmp as EmbeddedResource; + if (rsrc == null) + continue; + if (UTF8String.IsNullOrEmpty(rsrc.Name)) + continue; + if (!rsrc.Name.String.EndsWith(".g.resources")) + continue; + if (!HasXamlFiles(file.ModuleDefMD, rsrc)) + continue; + + Logger.w("File '{0}' contains XAML which isn't supported. Use --dont-rename.", file.Filename); + return; + } + } + } + + static bool HasXamlFiles(ModuleDef module, EmbeddedResource rsrc) { + try { + rsrc.Data.Position = 0; + var rsrcSet = ResourceReader.Read(module, rsrc.Data); + foreach (var elem in rsrcSet.ResourceElements) { + if (elem.Name.EndsWith(".baml")) + return true; + } + } + catch { + } + return false; + } + public void Rename() { if (modules.Empty) return; From e70ba4f1f09e4b9e98e137a71edd1c154b40d5f7 Mon Sep 17 00:00:00 2001 From: de4dot Date: Sun, 17 Nov 2013 20:43:22 +0100 Subject: [PATCH 13/14] Rename typeName -> ReflectionTypeName --- de4dot.code/resources/ResourceDataCreator.cs | 6 +++--- de4dot.code/resources/UserResourceData.cs | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/de4dot.code/resources/ResourceDataCreator.cs b/de4dot.code/resources/ResourceDataCreator.cs index cad6723d..12b88e44 100644 --- a/de4dot.code/resources/ResourceDataCreator.cs +++ b/de4dot.code/resources/ResourceDataCreator.cs @@ -117,15 +117,15 @@ namespace de4dot.code.resources { } public CharArrayResourceData Create(char[] value) { - return new CharArrayResourceData(CreateUserResourceType(CharArrayResourceData.typeName), value); + return new CharArrayResourceData(CreateUserResourceType(CharArrayResourceData.ReflectionTypeName), value); } public IconResourceData CreateIcon(byte[] value) { - return new IconResourceData(CreateUserResourceType(IconResourceData.typeName), value); + return new IconResourceData(CreateUserResourceType(IconResourceData.ReflectionTypeName), value); } public ImageResourceData CreateImage(byte[] value) { - return new ImageResourceData(CreateUserResourceType(ImageResourceData.typeName), value); + return new ImageResourceData(CreateUserResourceType(ImageResourceData.ReflectionTypeName), value); } public BinaryResourceData CreateSerialized(byte[] value) { diff --git a/de4dot.code/resources/UserResourceData.cs b/de4dot.code/resources/UserResourceData.cs index 3dda09cd..04add74b 100644 --- a/de4dot.code/resources/UserResourceData.cs +++ b/de4dot.code/resources/UserResourceData.cs @@ -41,7 +41,7 @@ namespace de4dot.code.resources { } class CharArrayResourceData : UserResourceData { - public static readonly string typeName = "System.Char[],mscorlib"; + public static readonly string ReflectionTypeName = "System.Char[],mscorlib"; char[] data; public CharArrayResourceData(UserResourceType type, char[] data) @@ -59,7 +59,7 @@ namespace de4dot.code.resources { } class IconResourceData : UserResourceData { - public static readonly string typeName = "System.Drawing.Icon,System.Drawing"; + public static readonly string ReflectionTypeName = "System.Drawing.Icon,System.Drawing"; Icon icon; public IconResourceData(UserResourceType type, byte[] data) @@ -77,7 +77,7 @@ namespace de4dot.code.resources { } class ImageResourceData : UserResourceData { - public static readonly string typeName = "System.Drawing.Bitmap,System.Drawing"; + public static readonly string ReflectionTypeName = "System.Drawing.Bitmap,System.Drawing"; Bitmap bitmap; public ImageResourceData(UserResourceType type, byte[] data) From 9c924d2aa3024d34e04f8c42683c621e636ee4d4 Mon Sep 17 00:00:00 2001 From: de4dot Date: Sun, 17 Nov 2013 21:13:08 +0100 Subject: [PATCH 14/14] Use absolute path of new output filename --- de4dot.cui/CommandLineParser.cs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/de4dot.cui/CommandLineParser.cs b/de4dot.cui/CommandLineParser.cs index 695694e1..d7c2efb7 100644 --- a/de4dot.cui/CommandLineParser.cs +++ b/de4dot.cui/CommandLineParser.cs @@ -247,9 +247,10 @@ namespace de4dot.cui { fileOptions.Add(new OneArgOption("o", null, "Name of output file", "file", (val) => { if (newFileOptions == null) ExitError("Missing input file"); - if (string.Equals(Utils.GetFullPath(newFileOptions.Filename), Utils.GetFullPath(val), StringComparison.OrdinalIgnoreCase)) - ExitError(string.Format("Output file can't be same as input file ({0})", val)); - newFileOptions.NewFilename = val; + var newFilename = Utils.GetFullPath(val); + if (string.Equals(Utils.GetFullPath(newFileOptions.Filename), newFilename, StringComparison.OrdinalIgnoreCase)) + ExitError(string.Format("Output file can't be same as input file ({0})", newFilename)); + newFileOptions.NewFilename = newFilename; })); fileOptions.Add(new OneArgOption("p", null, "Obfuscator type (see below)", "type", (val) => { if (newFileOptions == null)