From 991a5281ab6d548a66549daa367f3923bf6fa1ca Mon Sep 17 00:00:00 2001 From: de4dot Date: Sun, 22 Jan 2012 19:58:31 +0100 Subject: [PATCH] Add DS obfuscator support --- de4dot.code/de4dot.code.csproj | 7 + .../deobfuscators/DeepSea/AssemblyResolver.cs | 93 ++++++ .../deobfuscators/DeepSea/Deobfuscator.cs | 202 ++++++++++++++ .../DeepSea/DsInlinedMethodsFinder.cs | 38 +++ .../DeepSea/MethodCallInliner.cs | 185 ++++++++++++ .../deobfuscators/DeepSea/ResolverBase.cs | 123 ++++++++ .../deobfuscators/DeepSea/ResourceResolver.cs | 66 +++++ .../deobfuscators/DeepSea/StringDecrypter.cs | 264 ++++++++++++++++++ de4dot.cui/Program.cs | 1 + 9 files changed, 979 insertions(+) create mode 100644 de4dot.code/deobfuscators/DeepSea/AssemblyResolver.cs create mode 100644 de4dot.code/deobfuscators/DeepSea/Deobfuscator.cs create mode 100644 de4dot.code/deobfuscators/DeepSea/DsInlinedMethodsFinder.cs create mode 100644 de4dot.code/deobfuscators/DeepSea/MethodCallInliner.cs create mode 100644 de4dot.code/deobfuscators/DeepSea/ResolverBase.cs create mode 100644 de4dot.code/deobfuscators/DeepSea/ResourceResolver.cs create mode 100644 de4dot.code/deobfuscators/DeepSea/StringDecrypter.cs diff --git a/de4dot.code/de4dot.code.csproj b/de4dot.code/de4dot.code.csproj index 9889f84c..d124243d 100644 --- a/de4dot.code/de4dot.code.csproj +++ b/de4dot.code/de4dot.code.csproj @@ -90,6 +90,13 @@ + + + + + + + diff --git a/de4dot.code/deobfuscators/DeepSea/AssemblyResolver.cs b/de4dot.code/deobfuscators/DeepSea/AssemblyResolver.cs new file mode 100644 index 00000000..55607e4d --- /dev/null +++ b/de4dot.code/deobfuscators/DeepSea/AssemblyResolver.cs @@ -0,0 +1,93 @@ +/* + Copyright (C) 2011-2012 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.Collections.Generic; +using System.IO; +using System.Text.RegularExpressions; +using Mono.Cecil; + +namespace de4dot.code.deobfuscators.DeepSea { + class AssemblyResolver : ResolverBase { + public class AssemblyInfo { + public byte[] data; + public string fullName; + public string simpleName; + public string extension; + public EmbeddedResource resource; + + public AssemblyInfo(byte[] data, string fullName, string simpleName, string extension, EmbeddedResource resource) { + this.data = data; + this.fullName = fullName; + this.simpleName = simpleName; + this.extension = extension; + this.resource = resource; + } + } + + public AssemblyResolver(ModuleDefinition module) + : base(module) { + } + + static string[] handlerLocalTypes = new string[] { + "System.Byte[]", + "System.Security.Cryptography.SHA1CryptoServiceProvider", + "System.IO.Compression.DeflateStream", + "System.IO.MemoryStream", + "System.IO.Stream", + "System.Reflection.Assembly", + "System.String", + }; + protected override bool checkHandlerMethodInternal(MethodDefinition handler) { + return new LocalTypes(handler).all(handlerLocalTypes); + } + + public IEnumerable getAssemblyInfos() { + var infos = new List(); + + foreach (var tmp in module.Resources) { + var resource = tmp as EmbeddedResource; + if (resource == null) + continue; + if (!Regex.IsMatch(resource.Name, @"^[0-9A-F]{40}$")) + continue; + var info = getAssemblyInfos(resource); + if (info == null) + continue; + infos.Add(info); + } + + return infos; + } + + AssemblyInfo getAssemblyInfos(EmbeddedResource resource) { + try { + var decrypted = decryptResource(resource); + var asm = AssemblyDefinition.ReadAssembly(new MemoryStream(decrypted)); + var fullName = asm.Name.FullName; + var simpleName = asm.Name.Name; + var extension = DeobUtils.getExtension(asm.Modules[0].Kind); + return new AssemblyInfo(decrypted, fullName, simpleName, extension, resource); + } + catch (Exception) { + return null; + } + } + } +} diff --git a/de4dot.code/deobfuscators/DeepSea/Deobfuscator.cs b/de4dot.code/deobfuscators/DeepSea/Deobfuscator.cs new file mode 100644 index 00000000..08d56f1c --- /dev/null +++ b/de4dot.code/deobfuscators/DeepSea/Deobfuscator.cs @@ -0,0 +1,202 @@ +/* + Copyright (C) 2011-2012 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.Collections.Generic; +using Mono.Cecil; +using de4dot.blocks.cflow; + +namespace de4dot.code.deobfuscators.DeepSea { + public class DeobfuscatorInfo : DeobfuscatorInfoBase { + public const string THE_NAME = "DeepSea"; + public const string THE_TYPE = "ds"; + BoolOption inlineMethods; + BoolOption removeInlinedMethods; + BoolOption decryptResources; + BoolOption dumpEmbeddedAssemblies; + + public DeobfuscatorInfo() + : base() { + inlineMethods = new BoolOption(null, makeArgName("inline"), "Inline short methods", true); + removeInlinedMethods = new BoolOption(null, makeArgName("remove-inlined"), "Remove inlined methods", true); + decryptResources = new BoolOption(null, makeArgName("rsrc"), "Decrypt resources", true); + dumpEmbeddedAssemblies = new BoolOption(null, makeArgName("embedded"), "Dump embedded assemblies", true); + } + + public override string Name { + get { return THE_NAME; } + } + + public override string Type { + get { return THE_TYPE; } + } + + public override IDeobfuscator createDeobfuscator() { + return new Deobfuscator(new Deobfuscator.Options { + ValidNameRegex = validNameRegex.get(), + InlineMethods = inlineMethods.get(), + RemoveInlinedMethods = removeInlinedMethods.get(), + DecryptResources = decryptResources.get(), + DumpEmbeddedAssemblies = dumpEmbeddedAssemblies.get(), + }); + } + + protected override IEnumerable