2012-07-27 14:38:18 +08:00
|
|
|
|
/*
|
2013-01-13 00:15:14 +08:00
|
|
|
|
Copyright (C) 2011-2013 de4dot@gmail.com
|
2012-07-27 14:38:18 +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-08-12 04:34:43 +08:00
|
|
|
|
using System;
|
2012-12-23 04:08:29 +08:00
|
|
|
|
using dnlib.DotNet;
|
|
|
|
|
using dnlib.DotNet.Emit;
|
2012-07-27 14:38:18 +08:00
|
|
|
|
using de4dot.blocks;
|
|
|
|
|
|
|
|
|
|
namespace de4dot.code.deobfuscators.Confuser {
|
2012-08-09 17:47:18 +08:00
|
|
|
|
class AntiDebugger : IVersionProvider {
|
2012-11-19 06:42:43 +08:00
|
|
|
|
ModuleDefMD module;
|
|
|
|
|
MethodDef initMethod;
|
2012-08-12 04:34:43 +08:00
|
|
|
|
ConfuserVersion version = ConfuserVersion.Unknown;
|
|
|
|
|
|
|
|
|
|
enum ConfuserVersion {
|
|
|
|
|
Unknown,
|
|
|
|
|
v14_r57588_normal,
|
|
|
|
|
v14_r57588_safe,
|
|
|
|
|
v14_r60785_normal,
|
|
|
|
|
v16_r61954_normal,
|
|
|
|
|
v16_r61954_safe,
|
|
|
|
|
v17_r73822_normal,
|
|
|
|
|
v17_r73822_safe,
|
|
|
|
|
v17_r74021_normal,
|
|
|
|
|
v17_r74021_safe,
|
|
|
|
|
v19_r76119_safe,
|
|
|
|
|
}
|
2012-07-27 14:38:18 +08:00
|
|
|
|
|
2012-11-19 06:42:43 +08:00
|
|
|
|
public MethodDef InitMethod {
|
2012-07-27 14:38:18 +08:00
|
|
|
|
get { return initMethod; }
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-19 06:42:43 +08:00
|
|
|
|
public TypeDef Type {
|
2012-07-27 14:38:18 +08:00
|
|
|
|
get { return initMethod != null ? initMethod.DeclaringType : null; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Detected {
|
|
|
|
|
get { return initMethod != null; }
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-19 06:42:43 +08:00
|
|
|
|
public AntiDebugger(ModuleDefMD module) {
|
2012-07-27 14:38:18 +08:00
|
|
|
|
this.module = module;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
public void Find() {
|
|
|
|
|
if (CheckMethod(DotNetUtils.GetModuleTypeCctor(module)))
|
2012-07-27 14:38:18 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
bool CheckMethod(MethodDef method) {
|
2012-07-27 14:38:18 +08:00
|
|
|
|
if (method == null || method.Body == null)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
foreach (var instr in method.Body.Instructions) {
|
|
|
|
|
if (instr.OpCode.Code != Code.Call)
|
|
|
|
|
continue;
|
2012-11-19 06:42:43 +08:00
|
|
|
|
var calledMethod = instr.Operand as MethodDef;
|
2012-08-12 04:34:43 +08:00
|
|
|
|
if (calledMethod == null || !calledMethod.IsStatic)
|
2012-07-27 14:38:18 +08:00
|
|
|
|
continue;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
if (!DotNetUtils.IsMethod(calledMethod, "System.Void", "()"))
|
2012-07-30 15:13:17 +08:00
|
|
|
|
continue;
|
2012-08-12 04:34:43 +08:00
|
|
|
|
var type = calledMethod.DeclaringType;
|
|
|
|
|
if (type == null)
|
|
|
|
|
continue;
|
2012-07-27 14:38:18 +08:00
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
if (CheckMethod_normal(type, calledMethod) || CheckMethod_safe(type, calledMethod)) {
|
2012-07-27 14:38:18 +08:00
|
|
|
|
initMethod = calledMethod;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-08-12 04:34:43 +08:00
|
|
|
|
|
2012-07-27 14:38:18 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
static bool CheckProfilerStrings1(MethodDef method) {
|
|
|
|
|
if (!DotNetUtils.HasString(method, "COR_ENABLE_PROFILING"))
|
2012-07-27 14:38:18 +08:00
|
|
|
|
return false;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
if (!DotNetUtils.HasString(method, "COR_PROFILER"))
|
2012-07-27 14:38:18 +08:00
|
|
|
|
return false;
|
2012-08-12 04:34:43 +08:00
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
static bool CheckProfilerStrings2(MethodDef method) {
|
|
|
|
|
if (!DotNetUtils.HasString(method, "COR_"))
|
2012-08-12 04:34:43 +08:00
|
|
|
|
return false;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
if (!DotNetUtils.HasString(method, "ENABLE_PROFILING"))
|
2012-08-12 04:34:43 +08:00
|
|
|
|
return false;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
if (!DotNetUtils.HasString(method, "PROFILER"))
|
2012-08-12 04:34:43 +08:00
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
static MethodDef GetAntiDebugMethod(TypeDef type, MethodDef initMethod) {
|
2012-08-12 04:34:43 +08:00
|
|
|
|
foreach (var method in type.Methods) {
|
|
|
|
|
if (method.Body == null || method == initMethod)
|
|
|
|
|
continue;
|
|
|
|
|
if (!method.IsStatic || method.Name == ".cctor")
|
|
|
|
|
continue;
|
|
|
|
|
if (!method.IsPrivate)
|
|
|
|
|
continue;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
if (!DotNetUtils.IsMethod(method, "System.Void", "()") && !DotNetUtils.IsMethod(method, "System.Void", "(System.Object)"))
|
2012-08-12 04:34:43 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
return method;
|
2012-08-09 03:32:15 +08:00
|
|
|
|
}
|
2012-08-12 04:34:43 +08:00
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
bool CheckMethod_normal(TypeDef type, MethodDef initMethod) {
|
|
|
|
|
var ntQueryInformationProcess = DotNetUtils.GetPInvokeMethod(type, "ntdll", "NtQueryInformationProcess");
|
2012-08-12 04:34:43 +08:00
|
|
|
|
if (ntQueryInformationProcess == null)
|
|
|
|
|
return false;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
if (DotNetUtils.GetPInvokeMethod(type, "ntdll", "NtSetInformationProcess") == null)
|
2012-07-27 14:38:18 +08:00
|
|
|
|
return false;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
if (DotNetUtils.GetPInvokeMethod(type, "kernel32", "CloseHandle") == null)
|
2012-07-27 14:38:18 +08:00
|
|
|
|
return false;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
var antiDebugMethod = GetAntiDebugMethod(type, initMethod);
|
2012-08-12 04:34:43 +08:00
|
|
|
|
if (antiDebugMethod == null)
|
|
|
|
|
return false;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
if (!DotNetUtils.HasString(antiDebugMethod, "Debugger detected (Managed)"))
|
2012-08-12 04:34:43 +08:00
|
|
|
|
return false;
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
if (DotNetUtils.CallsMethod(initMethod, "System.Void System.Threading.Thread::.ctor(System.Threading.ParameterizedThreadStart)")) {
|
|
|
|
|
int failFastCalls = ConfuserUtils.CountCalls(antiDebugMethod, "System.Void System.Environment::FailFast(System.String)");
|
2012-08-12 04:34:43 +08:00
|
|
|
|
if (failFastCalls != 6 && failFastCalls != 8)
|
|
|
|
|
return false;
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
if (!CheckProfilerStrings1(initMethod))
|
2012-07-31 16:46:49 +08:00
|
|
|
|
return false;
|
2012-08-12 04:34:43 +08:00
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
if (!DotNetUtils.CallsMethod(antiDebugMethod, "System.Void System.Threading.Thread::.ctor(System.Threading.ParameterizedThreadStart)")) {
|
|
|
|
|
if (ConfuserUtils.CountCalls(antiDebugMethod, ntQueryInformationProcess) != 2)
|
2012-08-12 04:34:43 +08:00
|
|
|
|
return false;
|
|
|
|
|
version = ConfuserVersion.v16_r61954_normal;
|
|
|
|
|
}
|
|
|
|
|
else if (failFastCalls == 8) {
|
2013-01-19 20:09:49 +08:00
|
|
|
|
if (ConfuserUtils.CountCalls(antiDebugMethod, ntQueryInformationProcess) != 2)
|
2012-08-12 04:34:43 +08:00
|
|
|
|
return false;
|
|
|
|
|
version = ConfuserVersion.v17_r73822_normal;
|
|
|
|
|
}
|
|
|
|
|
else if (failFastCalls == 6) {
|
2013-01-19 20:09:49 +08:00
|
|
|
|
if (DotNetUtils.GetPInvokeMethod(type, "IsDebuggerPresent") == null)
|
2012-08-12 04:34:43 +08:00
|
|
|
|
return false;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
if (ConfuserUtils.CountCalls(antiDebugMethod, ntQueryInformationProcess) != 0)
|
2012-08-12 04:34:43 +08:00
|
|
|
|
return false;
|
|
|
|
|
version = ConfuserVersion.v17_r74021_normal;
|
|
|
|
|
}
|
|
|
|
|
else
|
2012-07-31 16:46:49 +08:00
|
|
|
|
return false;
|
2012-08-12 04:34:43 +08:00
|
|
|
|
}
|
2013-01-19 20:09:49 +08:00
|
|
|
|
else if (!DotNetUtils.CallsMethod(initMethod, "System.Void System.Threading.ThreadStart::.ctor(System.Object,System.IntPtr)")) {
|
|
|
|
|
if (!DotNetUtils.CallsMethod(initMethod, "System.Void System.Diagnostics.Process::EnterDebugMode()"))
|
2012-08-12 04:34:43 +08:00
|
|
|
|
return false;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
if (!CheckProfilerStrings1(antiDebugMethod))
|
2012-07-31 16:46:49 +08:00
|
|
|
|
return false;
|
2012-08-12 04:34:43 +08:00
|
|
|
|
version = ConfuserVersion.v14_r57588_normal;
|
|
|
|
|
}
|
|
|
|
|
else {
|
2013-01-19 20:09:49 +08:00
|
|
|
|
if (!DotNetUtils.CallsMethod(initMethod, "System.Void System.Diagnostics.Process::EnterDebugMode()"))
|
2012-08-12 04:34:43 +08:00
|
|
|
|
return false;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
if (!CheckProfilerStrings1(antiDebugMethod))
|
2012-08-12 04:34:43 +08:00
|
|
|
|
return false;
|
|
|
|
|
version = ConfuserVersion.v14_r60785_normal;
|
2012-07-31 16:46:49 +08:00
|
|
|
|
}
|
2012-07-27 14:38:18 +08:00
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2012-07-31 16:46:49 +08:00
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
bool CheckMethod_safe(TypeDef type, MethodDef initMethod) {
|
|
|
|
|
if (type == DotNetUtils.GetModuleType(module)) {
|
|
|
|
|
if (!DotNetUtils.HasString(initMethod, "Debugger detected (Managed)"))
|
2012-08-12 04:34:43 +08:00
|
|
|
|
return false;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
if (!CheckProfilerStrings1(initMethod))
|
2012-08-12 04:34:43 +08:00
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
version = ConfuserVersion.v14_r57588_safe;
|
2012-07-31 16:46:49 +08:00
|
|
|
|
}
|
2012-08-12 04:34:43 +08:00
|
|
|
|
else {
|
2013-01-19 20:09:49 +08:00
|
|
|
|
var ntQueryInformationProcess = DotNetUtils.GetPInvokeMethod(type, "ntdll", "NtQueryInformationProcess");
|
2012-08-12 04:34:43 +08:00
|
|
|
|
if (ntQueryInformationProcess == null)
|
|
|
|
|
return false;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
if (DotNetUtils.GetPInvokeMethod(type, "ntdll", "NtSetInformationProcess") == null)
|
2012-08-12 04:34:43 +08:00
|
|
|
|
return false;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
if (DotNetUtils.GetPInvokeMethod(type, "kernel32", "CloseHandle") == null)
|
2012-08-12 04:34:43 +08:00
|
|
|
|
return false;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
var antiDebugMethod = GetAntiDebugMethod(type, initMethod);
|
2012-08-12 04:34:43 +08:00
|
|
|
|
if (antiDebugMethod == null)
|
|
|
|
|
return false;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
if (!DotNetUtils.HasString(antiDebugMethod, "Debugger detected (Managed)") &&
|
|
|
|
|
!DotNetUtils.HasString(antiDebugMethod, "Debugger is detected (Managed)"))
|
2012-08-12 04:34:43 +08:00
|
|
|
|
return false;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
if (!DotNetUtils.CallsMethod(initMethod, "System.Void System.Threading.Thread::.ctor(System.Threading.ParameterizedThreadStart)"))
|
2012-08-12 04:34:43 +08:00
|
|
|
|
return false;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
if (ConfuserUtils.CountCalls(antiDebugMethod, ntQueryInformationProcess) != 0)
|
2012-08-12 04:34:43 +08:00
|
|
|
|
return false;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
if (!CheckProfilerStrings1(initMethod) && !CheckProfilerStrings2(initMethod))
|
2012-08-12 04:34:43 +08:00
|
|
|
|
return false;
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
int failFastCalls = ConfuserUtils.CountCalls(antiDebugMethod, "System.Void System.Environment::FailFast(System.String)");
|
2012-08-12 04:34:43 +08:00
|
|
|
|
if (failFastCalls != 2)
|
|
|
|
|
return false;
|
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
if (!DotNetUtils.CallsMethod(antiDebugMethod, "System.Void System.Threading.Thread::.ctor(System.Threading.ParameterizedThreadStart)"))
|
2012-08-12 04:34:43 +08:00
|
|
|
|
version = ConfuserVersion.v16_r61954_safe;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
else if (DotNetUtils.GetPInvokeMethod(type, "IsDebuggerPresent") == null)
|
2012-08-12 04:34:43 +08:00
|
|
|
|
version = ConfuserVersion.v17_r73822_safe;
|
2013-01-19 20:09:49 +08:00
|
|
|
|
else if (CheckProfilerStrings1(initMethod))
|
2012-08-12 04:34:43 +08:00
|
|
|
|
version = ConfuserVersion.v17_r74021_safe;
|
|
|
|
|
else
|
|
|
|
|
version = ConfuserVersion.v19_r76119_safe;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
2012-07-31 16:46:49 +08:00
|
|
|
|
}
|
2012-08-09 17:47:18 +08:00
|
|
|
|
|
2013-01-19 20:09:49 +08:00
|
|
|
|
public bool GetRevisionRange(out int minRev, out int maxRev) {
|
2012-08-12 04:34:43 +08:00
|
|
|
|
switch (version) {
|
|
|
|
|
case ConfuserVersion.Unknown:
|
|
|
|
|
minRev = maxRev = 0;
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
case ConfuserVersion.v14_r57588_safe:
|
|
|
|
|
minRev = 57588;
|
|
|
|
|
maxRev = 60787;
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
case ConfuserVersion.v16_r61954_safe:
|
|
|
|
|
minRev = 61954;
|
|
|
|
|
maxRev = 73791;
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
case ConfuserVersion.v17_r73822_safe:
|
|
|
|
|
minRev = 73822;
|
|
|
|
|
maxRev = 73822;
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
case ConfuserVersion.v17_r74021_safe:
|
|
|
|
|
minRev = 74021;
|
|
|
|
|
maxRev = 76101;
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
case ConfuserVersion.v19_r76119_safe:
|
|
|
|
|
minRev = 76119;
|
|
|
|
|
maxRev = int.MaxValue;
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
case ConfuserVersion.v14_r57588_normal:
|
|
|
|
|
minRev = 57588;
|
|
|
|
|
maxRev = 60408;
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
case ConfuserVersion.v14_r60785_normal:
|
|
|
|
|
minRev = 60785;
|
|
|
|
|
maxRev = 60787;
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
case ConfuserVersion.v16_r61954_normal:
|
|
|
|
|
minRev = 61954;
|
|
|
|
|
maxRev = 73791;
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
case ConfuserVersion.v17_r73822_normal:
|
|
|
|
|
minRev = 73822;
|
|
|
|
|
maxRev = 73822;
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
case ConfuserVersion.v17_r74021_normal:
|
|
|
|
|
minRev = 74021;
|
|
|
|
|
maxRev = int.MaxValue;
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
default: throw new ApplicationException("Invalid version");
|
|
|
|
|
}
|
2012-08-09 17:47:18 +08:00
|
|
|
|
}
|
2012-07-27 14:38:18 +08:00
|
|
|
|
}
|
|
|
|
|
}
|