DLL注入绕行与MinHook

问题描述

我成功使用以下代码钩住了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(),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(),16); // BattleZoneRedux 0x49B430

        DetourTransactionBegin();
        DetourUpdateThread(GetCurrentThread());

        // this will hook the sound function
        DetourAttach(&(LPVOID&)AddressOfHookSoundFunction,"Hooking not initilizing.");
           return false;
       }

使用MinHook吗?

谢谢!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)