C++ Hooking kernel32.dll OpenProcess 走弯路

标签 c++ windows winapi dll hook

我正在尝试从 Kernel32.dll Hook OpenProcess 以防止所谓的“injector”程序注入(inject)其他 dll进入我的过程:

// -------------------------------------------------------------------
HANDLE WINAPI myOpenProcess(DWORD dwDesiredAccess, BOOL  bInheritHandle, DWORD dwProcessId)
{
    //

    if (dwDesiredAccess == PROCESS_ALL_ACCESS || dwDesiredAccess == PROCESS_VM_OPERATION ||
        dwDesiredAccess == PROCESS_VM_READ || dwDesiredAccess == PROCESS_VM_WRITE)
    {
        printf("Blcoked Process ID : %d , DesiredAccess : %d ", dwProcessId, dwDesiredAccess);

        return false;
    }

    //

    return dOpenProcess(dwDesiredAccess, bInheritHandle, dwProcessId);
}

如果有人打开“注入(inject)”进程,我需要添加什么才能“检测”? 我不想“预防”,我希望“检测”注入(inject)并决定做什么。

最佳答案

Pic from http://resources.infosecinstitute.com/

该图描述了注入(inject)器通常执行的将 dll 注入(inject)另一个进程的步骤。你的程序应该进行行为分析来决定它是否正在注入(inject)。您需要挂接其他 API,例如 VirtualAlloc\WriteProcessMemoryCreateRemoteThread

Below shows the approach to follow to analyse the injector flow and block the execution when needed. Injector uses many techniques to inject a dll, the below won't be sufficient to all methods.

//
//HookOpenProcess keep track of opened process handle
//
HANDLE process = OpenProcess(PROCESS_ALL_ACCESS, FALSE, procID);

/*
HookVirtualAlloc  Check whether the first param is openprocess handle :: Make the suspicion level 3
*/
LPVOID arg = (LPVOID)VirtualAllocEx(process, NULL, ...);

/*
HookWriteProcessMemory  Check whether the first param is openprocess handle :: Make the suspicion level 2
*/
int n = WriteProcessMemory(process, .....);

/*
HookCreateRemoteThread Check whether the first param is openprocess handle :: Make the suspicion level 1 and block it from execution
*/
HANDLE threadID = CreateRemoteThread(process, .........);

关于C++ Hooking kernel32.dll OpenProcess 走弯路,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33561251/

相关文章:

linux - 将 db 的空副本从 Windows 移动到 Linux

Windows Batch 认为凌晨 2 点是下午 5 点之后

c - LVM_GETITEM 困惑

c# - Windows Forms 和 Aero 不能很好地结合——如何使它们看起来像原生的?

c++ - X11 具有 undefined reference 的功能,但似乎没问题。 IDE 不会提示那些库

windows - LOC 和 %LOC 在 Windows 上的子例程中不起作用

c++ - 包含字符串和整数的文件输入

windows - 如何在 Pascal Script/Inno Setup 中使用 WinAPI 中的 PathCombine()?

c++ - 用memcpy复制一个class对象是对的吗?

c++ - Qt Creator 错误 : LNK1123: failure during conversion to COFF: file invalid or corrupt