From e7ceb50382ad185b7d8e53bf1ffffc3c157abf29 Mon Sep 17 00:00:00 2001 From: de4dot Date: Tue, 1 Nov 2011 14:19:53 +0100 Subject: [PATCH] Add CanInlineMethods to IDeobfuscator --- de4dot.code/ObfuscatedFile.cs | 11 ++++++----- de4dot.code/deobfuscators/DeobfuscatorBase.cs | 4 ++++ de4dot.code/deobfuscators/IDeobfuscator.cs | 3 +++ 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/de4dot.code/ObfuscatedFile.cs b/de4dot.code/ObfuscatedFile.cs index d5c39396..2cbcfd4c 100644 --- a/de4dot.code/ObfuscatedFile.cs +++ b/de4dot.code/ObfuscatedFile.cs @@ -210,8 +210,8 @@ namespace de4dot { foreach (var deob in deobfuscators) { if (string.Equals(options.ForcedObfuscatorType, deob.Type, StringComparison.OrdinalIgnoreCase)) { deob.earlyDetect(); - deob.detect(); this.deob = deob; + deob.detect(); return; } } @@ -244,6 +244,7 @@ namespace de4dot { IDeobfuscator detected = null; int detectVal = 0; foreach (var deob in deobfuscators) { + this.deob = deob; // So we can call deob.CanInlineMethods in deobfuscate() int val = deob.detect(); Log.v("{0,3}: {1}", val, deob.Type); if (val > detectVal) { @@ -251,6 +252,7 @@ namespace de4dot { detected = deob; } } + this.deob = null; return detected; } @@ -470,7 +472,7 @@ namespace de4dot { Log.v("Deobfuscating methods"); var methodPrinter = new MethodPrinter(); - var cflowDeobfuscator = new BlocksCflowDeobfuscator(); + var cflowDeobfuscator = new BlocksCflowDeobfuscator { InlineMethods = deob.CanInlineMethods }; foreach (var method in allMethods) { Log.v("Deobfuscating {0} ({1:X8})", method, method.MetadataToken.ToUInt32()); Log.indent(); @@ -486,10 +488,8 @@ namespace de4dot { cflowDeobfuscator.deobfuscate(); } - if (deob.deobfuscateOther(blocks) && options.ControlFlowDeobfuscation) { - cflowDeobfuscator.init(blocks); + if (deob.deobfuscateOther(blocks) && options.ControlFlowDeobfuscation) cflowDeobfuscator.deobfuscate(); - } if (options.ControlFlowDeobfuscation) { numRemovedLocals = blocks.optimizeLocals(); @@ -777,6 +777,7 @@ namespace de4dot { deobfuscate(method, "Deobfuscating control flow", (blocks) => { var cflowDeobfuscator = new BlocksCflowDeobfuscator(); + cflowDeobfuscator.InlineMethods = deob.CanInlineMethods; cflowDeobfuscator.init(blocks); cflowDeobfuscator.deobfuscate(); }); diff --git a/de4dot.code/deobfuscators/DeobfuscatorBase.cs b/de4dot.code/deobfuscators/DeobfuscatorBase.cs index a1f526af..99a252f7 100644 --- a/de4dot.code/deobfuscators/DeobfuscatorBase.cs +++ b/de4dot.code/deobfuscators/DeobfuscatorBase.cs @@ -71,6 +71,10 @@ namespace de4dot.deobfuscators { public abstract string Type { get; } public abstract string Name { get; } + public virtual bool CanInlineMethods { + get { return false; } + } + public Func IsValidName { get { return (name) => optionsBase.ValidNameRegex.isMatch(name); } } diff --git a/de4dot.code/deobfuscators/IDeobfuscator.cs b/de4dot.code/deobfuscators/IDeobfuscator.cs index a4273486..9ae356f6 100644 --- a/de4dot.code/deobfuscators/IDeobfuscator.cs +++ b/de4dot.code/deobfuscators/IDeobfuscator.cs @@ -56,6 +56,9 @@ namespace de4dot.deobfuscators { // This is non-null only in detect() and deobfuscateBegin(). IDeobfuscatedFile DeobfuscatedFile { get; set; } + // Return true if methods can be inlined + bool CanInlineMethods { get; } + void init(ModuleDefinition module); // Same as detect() but may be used by deobfuscators to detect obfuscator that decrypt