Detect classes created by the obfuscator

This commit is contained in:
de4dot 2012-02-03 03:03:19 +01:00
parent 3ce28aebb0
commit 814ca402bf
3 changed files with 75 additions and 13 deletions

View File

@ -64,6 +64,7 @@ namespace de4dot.code.deobfuscators.Spices_Net {
bool startedDeobfuscating = false;
StringDecrypter stringDecrypter;
SpicesMethodCallInliner methodCallInliner;
internal class Options : OptionsBase {
public bool InlineMethods { get; set; }
@ -89,7 +90,7 @@ namespace de4dot.code.deobfuscators.Spices_Net {
public override IMethodCallInliner MethodCallInliner {
get {
if (CanInlineMethods)
return new SpicesMethodCallInliner();
return methodCallInliner;
return new NoMethodInliner();
}
}
@ -111,6 +112,7 @@ namespace de4dot.code.deobfuscators.Spices_Net {
}
protected override void scanForObfuscator() {
methodCallInliner = new SpicesMethodCallInliner(module);
stringDecrypter = new StringDecrypter(module);
stringDecrypter.find();
findSpicesAttributes();
@ -129,6 +131,8 @@ namespace de4dot.code.deobfuscators.Spices_Net {
public override void deobfuscateBegin() {
base.deobfuscateBegin();
methodCallInliner.initialize();
stringDecrypter.initialize();
foreach (var info in stringDecrypter.DecrypterInfos) {
staticStringInliner.add(info.method, (method2, args) => {
@ -154,7 +158,7 @@ namespace de4dot.code.deobfuscators.Spices_Net {
void removeInlinedMethods() {
if (!options.InlineMethods || !options.RemoveInlinedMethods)
return;
removeInlinedMethods(SpicesInlinedMethodsFinder.find(module));
removeInlinedMethods(new SpicesInlinedMethodsFinder(module, methodCallInliner).find());
}
public override IEnumerable<string> getStringDecrypterMethods() {

View File

@ -22,14 +22,22 @@ using Mono.Cecil;
namespace de4dot.code.deobfuscators.Spices_Net {
class SpicesInlinedMethodsFinder {
public static List<MethodDefinition> find(ModuleDefinition module) {
ModuleDefinition module;
SpicesMethodCallInliner methodCallInliner;
public SpicesInlinedMethodsFinder(ModuleDefinition module, SpicesMethodCallInliner methodCallInliner) {
this.module = module;
this.methodCallInliner = methodCallInliner;
}
public List<MethodDefinition> find() {
var inlinedMethods = new List<MethodDefinition>();
foreach (var type in module.GetTypes()) {
if (!type.IsNested)
continue;
foreach (var method in type.Methods) {
if (SpicesMethodCallInliner.checkCanInline(method))
if (methodCallInliner.checkCanInline(method))
inlinedMethods.Add(method);
}
}

View File

@ -18,28 +18,78 @@
*/
using Mono.Cecil;
using Mono.Cecil.Metadata;
using de4dot.blocks;
using de4dot.blocks.cflow;
namespace de4dot.code.deobfuscators.Spices_Net {
class SpicesMethodCallInliner : MethodCallInliner {
public SpicesMethodCallInliner()
ModuleDefinition module;
TypeDefinitionDict<bool> validTypes = new TypeDefinitionDict<bool>();
public SpicesMethodCallInliner(ModuleDefinition module)
: base(false) {
this.module = module;
}
public static bool checkCanInline(MethodDefinition method) {
if (method.Attributes != (MethodAttributes.Assembly | MethodAttributes.Static | MethodAttributes.HideBySig))
return false;
if (!method.DeclaringType.IsNested)
return false;
return true;
public bool checkCanInline(MethodDefinition method) {
return validTypes.find(method.DeclaringType);
}
protected override bool canInline(MethodDefinition method) {
if (!checkCanInline(method))
return checkCanInline(method);
}
public void initialize() {
foreach (var type in module.GetTypes()) {
if (checkValidType(type))
validTypes.add(type, true);
}
}
static bool checkValidType(TypeDefinition type) {
if ((type.Attributes & ~TypeAttributes.BeforeFieldInit) != TypeAttributes.NestedAssembly)
return false;
if (type.HasProperties || type.HasEvents || type.HasFields || type.HasNestedTypes)
return false;
if (type.GenericParameters.Count > 0)
return false;
if (type.IsValueType || type.IsInterface)
return false;
if (type.BaseType == null || type.BaseType.EType != ElementType.Object)
return false;
if (type.Interfaces.Count > 0)
return false;
if (!checkValidTypeMethods(type))
return false;
//TODO: Should only allow certain nested classes here, not all.
return true;
}
static bool checkValidTypeMethods(TypeDefinition type) {
bool foundCtor = false;
int numMethods = 0;
foreach (var method in type.Methods) {
if (method.Name == ".cctor")
return false;
if (method.Name == ".ctor") {
if (method.Parameters.Count != 0)
return false;
foundCtor = true;
continue;
}
if (method.Attributes != (MethodAttributes.Assembly | MethodAttributes.Static | MethodAttributes.HideBySig))
return false;
if (method.HasPInvokeInfo || method.PInvokeInfo != null)
return false;
if (method.GenericParameters.Count > 0)
return false;
numMethods++;
}
return numMethods > 0 && foundCtor;
}
}
}