de4dot-cex/de4dot.code/deobfuscators/SmartAssembly/MemoryManagerInfo.cs

95 lines
2.5 KiB
C#
Raw Normal View History

2011-09-22 10:55:30 +08:00
/*
2014-03-12 05:15:43 +08:00
Copyright (C) 2011-2014 de4dot@gmail.com
2011-09-22 10:55:30 +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-09-24 16:26:29 +08:00
using de4dot.blocks;
2011-09-22 10:55:30 +08:00
namespace de4dot.code.deobfuscators.SmartAssembly {
2011-09-22 10:55:30 +08:00
class MemoryManagerInfo {
2012-11-19 00:07:02 +08:00
ModuleDefMD module;
TypeDef memoryManagerType;
MethodDef attachAppMethod;
2011-09-22 10:55:30 +08:00
public bool Detected {
get { return memoryManagerType != null; }
}
public TypeDef Type {
2011-09-22 10:55:30 +08:00
get { return memoryManagerType; }
}
public MethodDef CctorInitMethod {
2011-09-22 10:55:30 +08:00
get { return attachAppMethod; }
}
2012-11-19 00:07:02 +08:00
public MemoryManagerInfo(ModuleDefMD module) {
2011-09-22 10:55:30 +08:00
this.module = module;
}
2013-01-19 20:03:57 +08:00
public bool Find() {
if (CheckCalledMethods(DotNetUtils.GetModuleTypeCctor(module)))
2011-11-05 17:10:36 +08:00
return true;
2013-01-19 20:03:57 +08:00
if (CheckCalledMethods(module.EntryPoint))
2011-11-05 17:10:36 +08:00
return true;
return false;
}
2011-09-22 10:55:30 +08:00
2013-01-19 20:03:57 +08:00
bool CheckCalledMethods(MethodDef checkMethod) {
2011-11-05 17:10:36 +08:00
if (checkMethod == null)
return false;
2013-01-19 20:03:57 +08:00
foreach (var method in DotNetUtils.GetCalledMethods(module, checkMethod)) {
2011-09-22 10:55:30 +08:00
if (method.Name == ".cctor" || method.Name == ".ctor")
continue;
2013-01-19 20:03:57 +08:00
if (!method.IsStatic || !DotNetUtils.IsMethod(method, "System.Void", "()"))
2011-09-22 10:55:30 +08:00
continue;
2013-01-19 20:03:57 +08:00
if (CheckMemoryManagerType(method.DeclaringType, method)) {
2012-03-18 03:36:41 +08:00
memoryManagerType = method.DeclaringType;
2011-09-22 10:55:30 +08:00
attachAppMethod = method;
return true;
}
}
return false;
}
2013-01-19 20:03:57 +08:00
bool CheckMemoryManagerType(TypeDef type, MethodDef method) {
2011-09-22 10:55:30 +08:00
// Only two fields: itself and a long
int fields = 0;
foreach (var field in type.Fields) {
2012-11-19 00:07:02 +08:00
if (new SigComparer().Equals(field.FieldType, type) ||
2011-09-22 10:55:30 +08:00
field.FieldType.FullName == "System.Int64") {
fields++;
continue;
}
2013-01-19 20:03:57 +08:00
if (DotNetUtils.DerivesFromDelegate(DotNetUtils.GetType(module, field.FieldType)))
2011-09-22 10:55:30 +08:00
continue;
return false;
}
if (fields != 2)
return false;
2013-01-19 20:03:57 +08:00
if (DotNetUtils.GetPInvokeMethod(type, "kernel32", "SetProcessWorkingSetSize") == null)
2011-09-22 10:55:30 +08:00
return false;
return true;
}
}
}