c++ - Detours 3.0 Hook 崩溃 MessageBoxA

标签 c++ winapi hook detours

我正在尝试将 MessageBoxA 函数与 MS Detours 3.0 Hook ,但是当我尝试它时,我的程序崩溃了。我不确定是什么导致程序崩溃。当我运行测试程序并按下 shift 时,会出现消息框,但是当我注入(inject) dll 并按下 shift 时,我的程序崩溃了。

测试程序

#include <Windows.h>

int main()
{
    for(;;)
    {
        if(GetAsyncKeyState(VK_SHIFT))
        {
            MessageBoxA(0,"NOT HOOKED","HOOK STATUS",0);
        }
    }
}

Hook 动态链接库

#include <Windows.h>
#include <detours.h>
#pragma comment(lib,"detours.lib")

BOOL (WINAPI* oMessageBoxA)(HWND,LPCTSTR,LPCTSTR,UINT);

BOOL WINAPI hMessageBoxA( HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption,UINT uType) 
{ 
        return oMessageBoxA(hWnd,"HOOKED",lpCaption,uType);
} 

void patch()
{
    HMODULE user32 = GetModuleHandle("user32.dll");
    if(user32 != NULL)
    {
        DWORD MessageBoxAddress = (DWORD)GetProcAddress(user32,"MessageBoxA");
        oMessageBoxA = (BOOL (WINAPI*)(HWND, LPCTSTR, LPCTSTR, UINT))MessageBoxAddress;
        DetourTransactionBegin();
        DetourUpdateThread(GetCurrentThread());
        DetourAttach(&(PVOID&)oMessageBoxA, hMessageBoxA);
        DetourTransactionCommit();
    }
}

BOOL WINAPI DllMain(HINSTANCE hinstDLL,DWORD fdwReason,LPVOID lpvReserved)
{
    if(fdwReason==DLL_PROCESS_ATTACH)
    {
        patch();
    }
}

最佳答案

您错误地声明了 MessageBoxA() 的签名,并且您对 DWORD MessageBoxAddress 的使用在 64 位 DLL 中不起作用。

试试这个 DLL 代码:

#include <Windows.h>
#include <detours.h>
#pragma comment(lib,"detours.lib")

typedef int (WINAPI* LPFN_MBA)(HWND, LPCSTR, LPCSTR, UINT);
LPFN_MBA oMessageBoxA = NULL;

int WINAPI hMessageBoxA( HWND hWnd, LPCSTR lpText, LPCSTR lpCaption,UINT uType) 
{ 
    return oMessageBoxA(hWnd,"HOOKED",lpCaption,uType);
} 

void patch()
{
    HMODULE user32 = GetModuleHandle(TEXT("user32.dll"));
    if (user32 != NULL)
    {
        oMessageBoxA = (LPFN_MBA) GetProcAddress(user32, "MessageBoxA");
        if (oMessageBoxA != NULL)
        { 
            DetourTransactionBegin();
            DetourUpdateThread(GetCurrentThread());
            DetourAttach((PVOID*)&oMessageBoxA, hMessageBoxA);
            DetourTransactionCommit();
        } 
    }
}

void unpatch()
{
    if (oMessageBoxA != NULL)
    {
        DetourTransactionBegin();
        DetourUpdateThread(GetCurrentThread());
        DetourDetach((PVOID*)&oMessageBoxA, hMessageBoxA);
        DetourTransactionCommit();
    }
}


BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
    if (fdwReason == DLL_PROCESS_ATTACH)
    {
        DisableThreadLibraryCalls(hinstDLL);
        patch();
    }
    else if (fdwReason == DLL_PROCESS_DETACH)
    {
        unpatch();
    }
}

阅读以下内容了解更多详情:

API Hooking with MS Detours

关于c++ - Detours 3.0 Hook 崩溃 MessageBoxA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21128363/

相关文章:

c++ - Eigen 中的逐元素最大值和正数部分

c++ - 使用 Windows 内置的 MP3 解码器播放音频?

c++ - 共享内存: MapViewOfFile returns error 5

c++ - 钩子(Hook)函数后调用原函数

.net - 低级鼠标钩子(Hook)和 DirectX

c++ - 在 Windows GUI 应用程序中使用 `main()` 而不是 `WinMain()` 时究竟会产生什么不良后果?

c++ - 使用带有状态的 Lambda 作为函数指针参数

c++ - 寻找集成 Qt4 和 v4l2 库的示例代码

winapi - 支持 ARM 上的 Windows 10 桌面应用程序 - MFC 和 COM 以及 OPOS 工作吗?

php - 在新选项卡中打开外部 WooCommerce 产品 - 添加属性目标 ="_blank"