2011-10-23 00:13:13 +08:00
|
|
|
|
/*
|
2013-01-02 00:03:16 +08:00
|
|
|
|
Copyright (C) 2011-2013 de4dot@gmail.com
|
2011-10-23 00:13:13 +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-12-20 09:06:09 +08:00
|
|
|
|
using dnlib.DotNet;
|
2011-10-23 00:13:13 +08:00
|
|
|
|
using de4dot.blocks;
|
|
|
|
|
|
2011-12-09 16:02:06 +08:00
|
|
|
|
namespace de4dot.code.deobfuscators.CryptoObfuscator {
|
2011-10-23 00:13:13 +08:00
|
|
|
|
class AntiDebugger {
|
2012-11-09 05:24:13 +08:00
|
|
|
|
ModuleDefMD module;
|
2011-10-23 00:13:13 +08:00
|
|
|
|
ISimpleDeobfuscator simpleDeobfuscator;
|
|
|
|
|
IDeobfuscator deob;
|
2012-11-02 22:57:11 +08:00
|
|
|
|
TypeDef antiDebuggerType;
|
|
|
|
|
MethodDef antiDebuggerMethod;
|
2011-10-23 00:13:13 +08:00
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
public TypeDef Type {
|
2011-10-23 15:03:00 +08:00
|
|
|
|
get { return antiDebuggerType; }
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-02 22:57:11 +08:00
|
|
|
|
public MethodDef Method {
|
2011-10-23 15:03:00 +08:00
|
|
|
|
get { return antiDebuggerMethod; }
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-09 05:24:13 +08:00
|
|
|
|
public AntiDebugger(ModuleDefMD module, ISimpleDeobfuscator simpleDeobfuscator, IDeobfuscator deob) {
|
2011-10-23 00:13:13 +08:00
|
|
|
|
this.module = module;
|
|
|
|
|
this.simpleDeobfuscator = simpleDeobfuscator;
|
|
|
|
|
this.deob = deob;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
public void Find() {
|
|
|
|
|
if (Find(module.EntryPoint))
|
2011-10-23 00:13:13 +08:00
|
|
|
|
return;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (Find(DotNetUtils.GetModuleTypeCctor(module)))
|
2011-12-15 23:16:21 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
2011-10-23 00:13:13 +08:00
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
bool Find(MethodDef methodToCheck) {
|
2011-12-15 23:16:21 +08:00
|
|
|
|
if (methodToCheck == null)
|
|
|
|
|
return false;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
foreach (var method in DotNetUtils.GetCalledMethods(module, methodToCheck)) {
|
2012-03-18 03:36:41 +08:00
|
|
|
|
var type = method.DeclaringType;
|
2011-10-23 00:13:13 +08:00
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (!method.IsStatic || !DotNetUtils.IsMethod(method, "System.Void", "()"))
|
2011-10-23 00:13:13 +08:00
|
|
|
|
continue;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (DotNetUtils.GetPInvokeMethod(type, "kernel32", "LoadLibrary") == null)
|
2011-10-23 00:13:13 +08:00
|
|
|
|
continue;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
if (DotNetUtils.GetPInvokeMethod(type, "kernel32", "GetProcAddress") == null)
|
2011-10-23 00:13:13 +08:00
|
|
|
|
continue;
|
2013-01-19 20:03:57 +08:00
|
|
|
|
Deobfuscate(method);
|
|
|
|
|
if (!ContainsString(method, "debugger is activ") &&
|
|
|
|
|
!ContainsString(method, "debugger is running") &&
|
|
|
|
|
!ContainsString(method, "Debugger detected") &&
|
|
|
|
|
!ContainsString(method, "Debugger was detected") &&
|
|
|
|
|
!ContainsString(method, "{0} was detected") &&
|
|
|
|
|
!ContainsString(method, "run under") &&
|
|
|
|
|
!ContainsString(method, "run with"))
|
2011-10-23 00:13:13 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
antiDebuggerType = type;
|
|
|
|
|
antiDebuggerMethod = method;
|
2011-12-15 23:16:21 +08:00
|
|
|
|
return true;
|
2011-10-23 00:13:13 +08:00
|
|
|
|
}
|
2011-12-15 23:16:21 +08:00
|
|
|
|
|
|
|
|
|
return false;
|
2011-10-23 00:13:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
void Deobfuscate(MethodDef method) {
|
|
|
|
|
simpleDeobfuscator.Deobfuscate(method);
|
|
|
|
|
simpleDeobfuscator.DecryptStrings(method, deob);
|
2011-10-23 00:13:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:03:57 +08:00
|
|
|
|
bool ContainsString(MethodDef method, string part) {
|
|
|
|
|
foreach (var s in DotNetUtils.GetCodeStrings(method)) {
|
2011-10-23 00:13:13 +08:00
|
|
|
|
if (s.Contains(part))
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|