2011-09-22 10:55:30 +08:00
|
|
|
|
/*
|
2012-01-10 06:02:47 +08:00
|
|
|
|
Copyright (C) 2011-2012 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/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
using dot10.DotNet;
|
2011-09-24 16:26:29 +08:00
|
|
|
|
using de4dot.blocks;
|
2011-09-22 10:55:30 +08:00
|
|
|
|
|
2011-12-09 16:02:06 +08:00
|
|
|
|
namespace de4dot.code.deobfuscators.SmartAssembly {
|
2011-09-22 10:55:30 +08:00
|
|
|
|
class MemoryManagerInfo {
|
|
|
|
|
ModuleDefinition module;
|
2012-11-02 22:57:11 +08:00
|
|
|
|
TypeDef memoryManagerType;
|
|
|
|
|
MethodDef attachAppMethod;
|
2011-09-22 10:55:30 +08:00
|
|
|
|
|
|
|
|
|
public bool Detected {
|
|
|
|
|
get { return memoryManagerType != null; }
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
public TypeDef Type {
|
2011-09-22 10:55:30 +08:00
|
|
|
|
get { return memoryManagerType; }
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
public MethodDef CctorInitMethod {
|
2011-09-22 10:55:30 +08:00
|
|
|
|
get { return attachAppMethod; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public MemoryManagerInfo(ModuleDefinition module) {
|
|
|
|
|
this.module = module;
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-17 09:54:48 +08:00
|
|
|
|
public bool find() {
|
2012-01-22 18:15:14 +08:00
|
|
|
|
if (checkCalledMethods(DotNetUtils.getModuleTypeCctor(module)))
|
2011-11-05 17:10:36 +08:00
|
|
|
|
return true;
|
|
|
|
|
if (checkCalledMethods(module.EntryPoint))
|
|
|
|
|
return true;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2011-09-22 10:55:30 +08:00
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
bool checkCalledMethods(MethodDef checkMethod) {
|
2011-11-05 17:10:36 +08:00
|
|
|
|
if (checkMethod == null)
|
|
|
|
|
return false;
|
2012-03-18 03:36:41 +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;
|
|
|
|
|
if (!method.IsStatic || !DotNetUtils.isMethod(method, "System.Void", "()"))
|
|
|
|
|
continue;
|
2012-03-18 03:36:41 +08:00
|
|
|
|
if (checkMemoryManagerType(method.DeclaringType, method)) {
|
|
|
|
|
memoryManagerType = method.DeclaringType;
|
2011-09-22 10:55:30 +08:00
|
|
|
|
attachAppMethod = method;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +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) {
|
|
|
|
|
if (MemberReferenceHelper.compareTypes(field.FieldType, type) ||
|
|
|
|
|
field.FieldType.FullName == "System.Int64") {
|
|
|
|
|
fields++;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2011-11-04 07:39:48 +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;
|
|
|
|
|
|
|
|
|
|
if (DotNetUtils.getPInvokeMethod(type, "kernel32", "SetProcessWorkingSetSize") == null)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|