【问题标题】:DetourAttach success but no functions hooked :(DetourAttach 成功,但没有挂钩功能 :(
【发布时间】:2015-02-06 12:46:39
【问题描述】:

早上好!

我最近阅读了关于挂钩函数的非常有趣的文章,我学习了一两个教程,但它似乎从来没有用过,我正在使用 Detoured,这里是完整的代码,在我看来完全正常:(

#include <stdio.h>
#include <windows.h>

#include "stdafx.h"
#include "detours.h"

#pragma comment(lib, "detours.lib")

int(__stdcall* realFunc)(int) = (int(__stdcall*)(int))(0x004157B0);

void hookedFunc(int num)
{
    printf("Test : %d\n", num + 100);
}

BOOL APIENTRY DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
    switch (ul_reason_for_call)
    {
    case DLL_PROCESS_ATTACH:
        DetourAttach((PVOID*)(&realFunc), (PVOID)hookedFunc);
        break;
    case DLL_THREAD_ATTACH:
        DetourTransactionBegin();
        DetourUpdateThread(GetCurrentThread());
        DetourAttach((PVOID*)(&realFunc), (PVOID)hookedFunc);
        DetourTransactionCommit();
        hookedFunc(100);
        break;
    case DLL_THREAD_DETACH:
        break;
    case DLL_PROCESS_DETACH:
        DetourDetach((PVOID*)0x004157B0, hookedFunc);
        break;
    }
    return TRUE;
}

当使用 RemoteDLL 和一个简单的控制台应用程序作为 dummy 来挂钩函数时,所有步骤都成功完成(以管理员身份运行),我要挂钩的函数的内存地址匹配,但是代码行“printf(”测试:%d\n", num + 100);"没有执行,结果不会出现在屏幕上...

如果有人知道发生了什么,我会很高兴听到它!

提前致谢!

【问题讨论】:

    标签: dll hook inject detours


    【解决方案1】:

    首先,hookedFunc 必须具有相同的签名:int __stdcall hookedFunc(int x)。

    我想你的代码的效果如下:每次有人调用地址 0x004157B0 处的函数时都会调用 hookedFunc。是你所期望的吗?

    为了测试,您调用此地址。让我稍微修改一下代码以澄清一下:

    extern int __stdcall FunctionIWantToHook(int);
    int(__stdcall* realFunc)(int) = FunctionIWantToHook;
    
    ...
    DetourAttach((PVOID*)(&realFunc), (PVOID)hookedFunc);
    FunctionIWantToHook(100); // hookedFunc will be called here
    

    【讨论】:

    • 感谢您的回复!我想要的只是在地址 0x004157B0 处挂接函数,该函数只显示一条消息,我想通过显示一条新消息来尝试更改它,但我可能误解了这个概念,或者忘记了一些东西,加上当我使用这些下一行要分离:DetourTransactionBegin(); DetourUpdateThread(GetCurrentThread()); DetourDetach(&(PVOID&)realFunc, hookedFunc); DetourTransactionCommit();注入不再起作用,并且在 RemoteDLL 中的第 4 步失败:(
    猜你喜欢
    • 2022-09-26
    • 2021-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-09
    • 2015-10-20
    相关资源
    最近更新 更多