首页 > 解决方案 > detours vs MinHook 用于 DLL 注入

问题描述

我已经使用以下代码成功连接了一个 gog 游戏(无 DRM):

bool WINAPI DllMain(HMODULE hModule, DWORD dwReason, LPVOID lpReserved)
{
    static HMODULE libogg_ori = nullptr;

    int FFBInitDelayInMillliseconds;

    char* DLLFileName;

    std::string DLLFile = "libogg_ori.dll";

    switch (dwReason)
    {
    case DLL_PROCESS_ATTACH:
        // Load dll

         // Initialize config File
        Local_InitConfigComplete = Init_Config();

        // Delay for timer that initialize the Force Feedback
        FFBInitDelayInMillliseconds = Config.FFBDelayBeforeInit * 1000;

        DLLFileName = R"(libogg_ori.dll)";

        if (FS::File_Exist(DLLFileName))
        {
            LF::Log_Update("File DLL exit");
            // Load the System DLL
            d3d9dll = LoadLibraryA(DLLFileName);

            if (d3d9dll == NULL)
                LF::Log_Update("Load DLL error");
        }
        else
        {
            LF::Log_Update("File DLL not exist");
        }


        // Initilize Detour
        if (Local_InitConfigComplete)
            Local_InitConfigComplete = Init_Detour();


        break;

    case DLL_PROCESS_DETACH:

        // Close the DLL

        LF::Log_Update("Closing session ...");
        LF::Log_Update("Release hook in progress ...");

        // unhook
        DetourTransactionBegin();
        DetourUpdateThread(GetCurrentThread());

        // this will hook the sound function
        DetourDetach(&(LPVOID&)AddressOfHookSoundFunction, &HookSoundFileSub);

        // this will hook the damage function (if present in configuration)
        if (AddressOfHookDamageFunction>0)
            DetourDetach(&(LPVOID&)AddressOfHookDamageFunction, &HookDamageSub);

        if (DetourTransactionCommit() == NO_ERROR)
            LF::Log_Update(LOG_FLAG_DONE);
        else
            LF::Log_Update(LOG_FLAG_ERROR, "Warning Hooking not relased.");

        LF::Log_Update("Session closed.");

        break;
    }

    return true;
    }
    
    
    
    
    bool Init_Detour()
{
    // Initialize Detours
    // we will find the function/s to hook with IDA pro.

    try
    {
    
        LF::Log_Update("Initialize Hooking ...");
                                                        
        AddressOfHookSoundFunction = std::strtoul(Config.GameFileSoundOffsetPos.c_str() ,NULL,16); // BattleZoneRedux 0x43AA30

         if (Config.GameFileDamageOffsetPos.size() > 1)
             AddressOfHookDamageFunction = std::strtoul(Config.GameFileDamageOffsetPos.c_str(), NULL, 16); // BattleZoneRedux 0x49B430

        DetourTransactionBegin();
        DetourUpdateThread(GetCurrentThread());
    
        // this will hook the sound function
        DetourAttach(&(LPVOID&)AddressOfHookSoundFunction, &HookSoundFileSub);

        // If is present a offset for hook the damage
        if (AddressOfHookDamageFunction!=0)
            DetourAttach(&(LPVOID&)AddressOfHookDamageFunction, &HookDamageSub);


       if (DetourTransactionCommit() == NO_ERROR)
       {
           LF::Log_Update(LOG_FLAG_DONE);
           return true;
       }
       else
       {
           LF::Log_Update(LOG_FLAG_ERROR, "Hooking not initilizing.");
           return false;
       }

        
    }
    catch (const std::exception&)
    {
        LF::Log_Update(LOG_FLAG_ERROR, "Error while initialize Detour.");
        return false;
    }

    
}

为此,我找到了 libogg.dll 和 d3d9.dll(DLL 注入)的自定义版本。两者都完美地工作。

但是当我将一些代码用于另一个没有 DRM 的 GOG 游戏时,用 libogg.dll 注入它时,游戏总是在这一行崩溃:

DetourDetach(&(LPVOID&)AddressOfHookSoundFunction, &HookSoundFileSub);

在我对游戏的所有测试中,即使 AddressOfHookSoundFunction 与不存在的地址相关,也永远不会在该行中崩溃。

简而言之,AddressOfHookSoundFunction 和 HookSoundFileSub 独立于第二场比赛总是崩溃。

两个游戏的区别在这里:

  1. 第一个是 2017 年制作的,第二个是某个软件公司在 2018 年制作的。
  2. 如果我用 IDA 解散第一场比赛,我总是看到 sub_xxxxxx ,但在第二场比赛中,有时我会看到函数的真实名称而不是地址。所以我想它是由更新的 Visual c++ 版本完成的,因此它可能无法正常工作。

在许多论坛上,人们建议使用 MinHook,因为它比走弯路要好,但我不知道为什么以及是否适用于所有情况。

有人可以在这种情况下有一些经验吗?

如果解决方案是使用 minhook,有人可以帮我重写这段代码:

AddressOfHookSoundFunction = std::strtoul(Config.GameFileSoundOffsetPos.c_str() ,NULL,16); // BattleZoneRedux 0x43AA30

         if (Config.GameFileDamageOffsetPos.size() > 1)
             AddressOfHookDamageFunction = std::strtoul(Config.GameFileDamageOffsetPos.c_str(), NULL, 16); // BattleZoneRedux 0x49B430

        DetourTransactionBegin();
        DetourUpdateThread(GetCurrentThread());

        // this will hook the sound function
        DetourAttach(&(LPVOID&)AddressOfHookSoundFunction, &HookSoundFileSub);

        // If is present a offset for hook the damage
        if (AddressOfHookDamageFunction!=0)
            DetourAttach(&(LPVOID&)AddressOfHookDamageFunction, &HookDamageSub);


       if (DetourTransactionCommit() == NO_ERROR)
       {
           LF::Log_Update(LOG_FLAG_DONE);
           return true;
       }
       else
       {
           LF::Log_Update(LOG_FLAG_ERROR, "Hooking not initilizing.");
           return false;
       }

与MinHook ?

谢谢 !

标签: c++hookreverse-engineeringdll-injection

解决方案


推荐阅读