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/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
using Mono.Cecil;
|
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;
|
|
|
|
|
TypeDefinition memoryManagerType;
|
|
|
|
|
MethodDefinition attachAppMethod;
|
|
|
|
|
|
|
|
|
|
public bool Detected {
|
|
|
|
|
get { return memoryManagerType != null; }
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-06 22:24:30 +08:00
|
|
|
|
public TypeDefinition Type {
|
2011-09-22 10:55:30 +08:00
|
|
|
|
get { return memoryManagerType; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public MethodDefinition CctorInitMethod {
|
|
|
|
|
get { return attachAppMethod; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public MemoryManagerInfo(ModuleDefinition module) {
|
|
|
|
|
this.module = module;
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-17 09:54:48 +08:00
|
|
|
|
public bool find() {
|
2011-11-05 17:10:36 +08:00
|
|
|
|
if (checkCalledMethods(DotNetUtils.getMethod(DotNetUtils.getModuleType(module), ".cctor")))
|
|
|
|
|
return true;
|
|
|
|
|
if (checkCalledMethods(module.EntryPoint))
|
|
|
|
|
return true;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2011-09-22 10:55:30 +08:00
|
|
|
|
|
2011-11-05 17:10:36 +08:00
|
|
|
|
bool checkCalledMethods(MethodDefinition checkMethod) {
|
|
|
|
|
if (checkMethod == null)
|
|
|
|
|
return false;
|
|
|
|
|
foreach (var tuple in DotNetUtils.getCalledMethods(module, checkMethod)) {
|
2011-09-22 10:55:30 +08:00
|
|
|
|
var method = tuple.Item2;
|
|
|
|
|
if (method.Name == ".cctor" || method.Name == ".ctor")
|
|
|
|
|
continue;
|
|
|
|
|
if (!method.IsStatic || !DotNetUtils.isMethod(method, "System.Void", "()"))
|
|
|
|
|
continue;
|
|
|
|
|
if (checkMemoryManagerType(tuple.Item1, method)) {
|
|
|
|
|
memoryManagerType = tuple.Item1;
|
|
|
|
|
attachAppMethod = method;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool checkMemoryManagerType(TypeDefinition type, MethodDefinition method) {
|
|
|
|
|
// 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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|