c++ - 如何正确使用 C++ 中的 Detour 库来实现具有已知内存地址的函数的简单 Hook ?

标签 c++ detours

我无法使用绕行来获得我的第一个钩子(Hook)。我正在使用 Detour 3.0。

我的代码编译得很好,我可以使用 Winject 注入(inject) DLL,但是,我应该 Hook 的函数似乎没有被 Hook 。我正在尝试在 记事本 中挂接函数 InsertDateTime。
http://www.9injector.com/winject-injector/

我使用 IDA Pro Free 以十六进制表示法找到了 InsertDateTime 的地址。

下面的代码中是否存在任何根本性错误,或者进程中的内存是否在每次调用时都不是同时出现?

我注入(inject)的 DLL 代码如下所示:

 // dllmain.cpp : Defines the entry point for the DLL application.
#include "stdafx.h"

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

int(__stdcall* InsertDateTime)(int) = (int(__stdcall*)(int))(0x0100978A);
int MyInsertDateTime(int x) //Our function
{
//Messagebox
MessageBox(NULL, TEXT("InsertDateTime Just Got Called"), TEXT("InsertDateTime"), MB_OK);
return InsertDateTime(x); //Return the origional function
}

BOOL APIENTRY DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
switch (ul_reason_for_call) //Decide what to do
{
case DLL_PROCESS_ATTACH: //On dll attach
    //InsertDateTime = (int (__stdcall*)(int))DetourAttach((PVOID*)0x0100978A, MyInsertDateTime);
    //MessageBox(NULL, TEXT("InsertDateTime Just Got Called"), TEXT("InsertDateTime"), MB_OK);
    DetourAttach((PVOID*)(&InsertDateTime), (PVOID)MyInsertDateTime);
    //if(!errorCode) {
    //Detour successful

break;
case DLL_THREAD_ATTACH: //On thread attach
        DetourAttach((PVOID*)(&InsertDateTime), (PVOID)MyInsertDateTime);
break;
case DLL_THREAD_DETACH: //On thread detach
break;
case DLL_PROCESS_DETACH: //on process detach
    DetourDetach((PVOID*)0x0100978A, InsertDateTime);
break;
}
return TRUE;
}

此外,代码主要取自使用 Detour 1.5 的旧教程。 引用:http://www.moddb.com/groups/ibepex/tutorials/function-hooking

最佳答案

Detours 使用的是类似于数据库的事务系统。在您可以调用 Attach 或 Detach 之前,您必须启动一个事务,并且更改仅在您提交事务时应用。

DetourTransactionBegin();
DetourAttach(...);
DetourAttach(...);
DetourTransactionCommit();

我认为这是在 2.0 中引入的,这可以解释为什么 1.5 的教程代码不包含它。

关于c++ - 如何正确使用 C++ 中的 Detour 库来实现具有已知内存地址的函数的简单 Hook ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16989429/

相关文章:

c++ - 虚拟类有什么好处?

c++ - 将 sleep() 注入(inject)到外部进程的函数中

c++ - 如何获取 ConnectEx() 指针

c++ - JSONCPP 到 Visual Studio

c++ - 虚拟继承中的构造函数顺序

c++ - MinGW 链接单个 EXE

C++ 构造函数错误。无法初始化字符串数组

c++ - 从另一个应用程序访问 lua_State

c++ - 使用 Microsoft Detours - 一堆未定义的

c++ - Detours 3.0 钩子(Hook) GetProcAddresss()