From e67ecfdff4eac6c366abb1ea327b59f8a57581a8 Mon Sep 17 00:00:00 2001 From: de4dot Date: Fri, 3 Feb 2012 06:33:54 +0100 Subject: [PATCH] Remove the methods types --- de4dot.code/de4dot.code.csproj | 1 - de4dot.code/deobfuscators/DeobfuscatorBase.cs | 2 +- .../deobfuscators/Spices_Net/Deobfuscator.cs | 10 +++- .../Spices_Net/SpicesInlinedMethodsFinder.cs | 48 ------------------- .../Spices_Net/SpicesMethodCallInliner.cs | 31 ++++++++++++ 5 files changed, 41 insertions(+), 51 deletions(-) delete mode 100644 de4dot.code/deobfuscators/Spices_Net/SpicesInlinedMethodsFinder.cs diff --git a/de4dot.code/de4dot.code.csproj b/de4dot.code/de4dot.code.csproj index 9f068fbe..2676548b 100644 --- a/de4dot.code/de4dot.code.csproj +++ b/de4dot.code/de4dot.code.csproj @@ -148,7 +148,6 @@ - diff --git a/de4dot.code/deobfuscators/DeobfuscatorBase.cs b/de4dot.code/deobfuscators/DeobfuscatorBase.cs index 00038d5b..43863f3a 100644 --- a/de4dot.code/deobfuscators/DeobfuscatorBase.cs +++ b/de4dot.code/deobfuscators/DeobfuscatorBase.cs @@ -648,7 +648,7 @@ namespace de4dot.code.deobfuscators { addMethodsToBeRemoved(new UnusedMethodsFinder(module, inlinedMethods, getRemovedMethods()).find(), "Inlined method"); } - MethodCollection getRemovedMethods() { + protected MethodCollection getRemovedMethods() { var removedMethods = new MethodCollection(); removedMethods.add(getMethodsToRemove()); removedMethods.addAndNested(getTypesToRemove()); diff --git a/de4dot.code/deobfuscators/Spices_Net/Deobfuscator.cs b/de4dot.code/deobfuscators/Spices_Net/Deobfuscator.cs index c7be8d11..1d29fb4c 100644 --- a/de4dot.code/deobfuscators/Spices_Net/Deobfuscator.cs +++ b/de4dot.code/deobfuscators/Spices_Net/Deobfuscator.cs @@ -167,7 +167,15 @@ namespace de4dot.code.deobfuscators.Spices_Net { void removeInlinedMethods() { if (!options.InlineMethods || !options.RemoveInlinedMethods) return; - removeInlinedMethods(new SpicesInlinedMethodsFinder(module, methodCallInliner).find()); + + var unusedMethods = new UnusedMethodsFinder(module, methodCallInliner.getInlinedMethods(), getRemovedMethods()).find(); + var removedTypes = methodCallInliner.getInlinedTypes(unusedMethods); + + addTypesToBeRemoved(removedTypes.getKeys(), "Obfuscator methods type"); + foreach (var method in unusedMethods) { + if (!removedTypes.find(method.DeclaringType)) + addMethodToBeRemoved(method, "Inlined method"); + } } public override IEnumerable getStringDecrypterMethods() { diff --git a/de4dot.code/deobfuscators/Spices_Net/SpicesInlinedMethodsFinder.cs b/de4dot.code/deobfuscators/Spices_Net/SpicesInlinedMethodsFinder.cs deleted file mode 100644 index 9e83ba8e..00000000 --- a/de4dot.code/deobfuscators/Spices_Net/SpicesInlinedMethodsFinder.cs +++ /dev/null @@ -1,48 +0,0 @@ -/* - 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; - -namespace de4dot.code.deobfuscators.Spices_Net { - class SpicesInlinedMethodsFinder { - ModuleDefinition module; - SpicesMethodCallInliner methodCallInliner; - - public SpicesInlinedMethodsFinder(ModuleDefinition module, SpicesMethodCallInliner methodCallInliner) { - this.module = module; - this.methodCallInliner = methodCallInliner; - } - - public List find() { - var inlinedMethods = new List(); - - foreach (var type in module.GetTypes()) { - if (!type.IsNested) - continue; - foreach (var method in type.Methods) { - if (methodCallInliner.checkCanInline(method)) - inlinedMethods.Add(method); - } - } - - return inlinedMethods; - } - } -} diff --git a/de4dot.code/deobfuscators/Spices_Net/SpicesMethodCallInliner.cs b/de4dot.code/deobfuscators/Spices_Net/SpicesMethodCallInliner.cs index 99fe4af0..4e4b4665 100644 --- a/de4dot.code/deobfuscators/Spices_Net/SpicesMethodCallInliner.cs +++ b/de4dot.code/deobfuscators/Spices_Net/SpicesMethodCallInliner.cs @@ -17,6 +17,7 @@ along with de4dot. If not, see . */ +using System.Collections.Generic; using Mono.Cecil; using Mono.Cecil.Cil; using Mono.Cecil.Metadata; @@ -140,5 +141,35 @@ namespace de4dot.code.deobfuscators.Spices_Net { return true; } + + public List getInlinedMethods() { + var list = new List(); + + foreach (var type in validTypes.getKeys()) + list.AddRange(type.Methods); + + return list; + } + + public TypeDefinitionDict getInlinedTypes(IEnumerable unusedMethods) { + var unused = new MethodDefinitionAndDeclaringTypeDict(); + foreach (var method in unusedMethods) + unused.add(method, true); + + var types = new TypeDefinitionDict(); + foreach (var type in validTypes.getKeys()) { + if (checkAllMethodsUnused(unused, type)) + types.add(type, true); + } + return types; + } + + static bool checkAllMethodsUnused(MethodDefinitionAndDeclaringTypeDict unused, TypeDefinition type) { + foreach (var method in type.Methods) { + if (!unused.find(method)) + return false; + } + return true; + } } }