de4dot-cex/de4dot.code/deobfuscators/dotNET_Reactor/v4/EmptyClass.cs

103 lines
2.7 KiB
C#
Raw Permalink Normal View History

2011-11-24 17:44:01 +08:00
/*
2015-10-30 05:45:26 +08:00
Copyright (C) 2011-2015 de4dot@gmail.com
2011-11-24 17:44:01 +08:00
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 <http://www.gnu.org/licenses/>.
*/
using dnlib.DotNet;
2011-11-24 17:44:01 +08:00
using de4dot.blocks;
namespace de4dot.code.deobfuscators.dotNET_Reactor.v4 {
2011-11-24 17:44:01 +08:00
// Detect some empty class that is called from most .ctor's
class EmptyClass {
2012-11-18 01:57:36 +08:00
ModuleDefMD module;
MethodDef emptyMethod;
2011-11-24 17:44:01 +08:00
public MethodDef Method {
2011-11-24 17:44:01 +08:00
get { return emptyMethod; }
}
public TypeDef Type {
2011-11-24 17:44:01 +08:00
get { return emptyMethod != null ? emptyMethod.DeclaringType : null; }
}
2012-11-18 01:57:36 +08:00
public EmptyClass(ModuleDefMD module) {
2011-11-24 17:44:01 +08:00
this.module = module;
2013-01-19 20:03:57 +08:00
Initialize();
2011-11-24 17:44:01 +08:00
}
2013-01-19 20:03:57 +08:00
void Initialize() {
2011-11-24 17:44:01 +08:00
var callCounter = new CallCounter();
int count = 0;
foreach (var type in module.GetTypes()) {
if (count >= 40)
break;
foreach (var method in type.Methods) {
2011-11-26 19:34:17 +08:00
if (method.Name != ".ctor" && method.Name != ".cctor" && module.EntryPoint != method)
2011-11-24 17:44:01 +08:00
continue;
2013-01-19 20:03:57 +08:00
foreach (var calledMethod in DotNetUtils.GetCalledMethods(module, method)) {
2011-11-24 17:44:01 +08:00
if (!calledMethod.IsStatic || calledMethod.Body == null)
continue;
2013-01-19 20:03:57 +08:00
if (!DotNetUtils.IsMethod(calledMethod, "System.Void", "()"))
2011-11-24 17:44:01 +08:00
continue;
2013-01-19 20:03:57 +08:00
if (IsEmptyClass(calledMethod)) {
callCounter.Add(calledMethod);
2011-11-24 17:44:01 +08:00
count++;
}
}
}
}
2011-11-26 19:34:17 +08:00
int numCalls;
2013-01-19 20:03:57 +08:00
var theMethod = (MethodDef)callCounter.Most(out numCalls);
2011-11-26 19:34:17 +08:00
if (numCalls >= 10)
emptyMethod = theMethod;
2011-11-24 17:44:01 +08:00
}
2013-01-19 20:03:57 +08:00
bool IsEmptyClass(MethodDef emptyMethod) {
if (!DotNetUtils.IsEmptyObfuscated(emptyMethod))
2011-11-24 17:44:01 +08:00
return false;
var type = emptyMethod.DeclaringType;
if (type.HasEvents || type.HasProperties)
return false;
if (type.Fields.Count != 1)
return false;
if (type.Fields[0].FieldType.FullName != "System.Boolean")
return false;
2011-11-26 19:34:17 +08:00
if (type.IsPublic)
return false;
2011-11-24 17:44:01 +08:00
2011-11-26 19:34:17 +08:00
int otherMethods = 0;
2011-11-24 17:44:01 +08:00
foreach (var method in type.Methods) {
if (method.Name == ".ctor" || method.Name == ".cctor")
continue;
if (method == emptyMethod)
continue;
2011-11-26 19:34:17 +08:00
otherMethods++;
if (method.Body == null)
return false;
if (method.Body.Instructions.Count > 20)
return false;
2011-11-24 17:44:01 +08:00
}
2011-11-26 19:34:17 +08:00
if (otherMethods > 8)
return false;
2011-11-24 17:44:01 +08:00
return true;
}
}
}