8.7 父进程检测反调试

首先这是一种比较奇特的反调试思路,通过检测自身父进程来判定是否被调试,原理非常简单,我们的系统在运行程序的时候,绝大多数应用程序都是由Explorer.exe这个父进程派生而来的子进程,也就是说如果没有被调试其得到的父进程就是Explorer.exe的进程PID,而如果被调试则该进程的父进程PID就会变成调试器的PID值,通过对父进程的检测即可实现检测是否被调试的功能。

#include <Windows.h>
#include <stdio.h>
#include <tlhelp32.h>

int IsDebug()
{
DWORD ExplorerId = 0;
PROCESSENTRY32 pe32 = { 0 };
DWORD ProcessId = GetCurrentProcessId();

GetWindowThreadProcessId(FindWindow("Progman", NULL), &ExplorerId);

HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
if (hProcessSnap != INVALID_HANDLE_VALUE)
{
pe32.dwSize = sizeof(PROCESSENTRY32);
Process32First(hProcessSnap, &pe32);
do
{
// 先判断是不是我们自己进程的PID
if (ProcessId == pe32.th32ProcessID)
{
// 判断父进程是否是 Explorer.exe
if (pe32.th32ParentProcessID != ExplorerId)
{
return TRUE;
}
}
} while (Process32Next(hProcessSnap, &pe32));
}
return FALSE;
}

int main(int argc, char * argv[])
{
if (IsDebug())
{
printf("[-] 进程正在被调试 \n");
}

system("pause");
return 0;
}