0

I have hooked a sound function that work within the game menu after, when the battle start, the game will crash.

The function at the first parameter get correctly the sound file name, before the battle begin.

If the function 'HookSum' is empty the game work without any crash.

The line:

//
std::cout << "your program has been hacked! " << std::endl;
//

don't cause the crash, but If I insert any other line of code for example:

//
std::string StrFileName;
StrFileName = "test";
//

the game crash. So at this point I can't hook nothing, becouse I can't insert any code inside the hooked function.

Someone can please explain why the game crash ?

Follow the full code.

#include <Windows.h>
#include <iostream>

#include "detours.h"
#include "sigscan.h"



DWORD AddressOfSum = 0;


typedef void(*sum) (char *a1, int a2, int a3, int a4);

void HookSum(char *a1, int a2, int a3, int a4)
{

    std::cout << "your program has been hacked! " << std::endl;

    std::string StrFileName;
    StrFileName = "test";

    sum originalSum = (sum)AddressOfSum;
    return originalSum(a1,a2,a3,a4);

    // MessageBoxA(0, a1, "MsgTitle", MB_OK | MB_ICONQUESTION);

    // return;
}













BOOL WINAPI DllMain(HMODULE hModule, DWORD dwReason, LPVOID lpReserved)
{
    // store the address of sum() in testprogram.exe here.

    if (dwReason == DLL_PROCESS_ATTACH)
    {
        // We will use signature scanning to find the function that we want to hook
        // we will find the function in IDA pro and create a signature from it:

        SigScan Scanner;

        AddressOfSum = Scanner.FindPattern("bzone.exe", "\x55\x8B\xEC\x51\x83\x3D\x94\x55\x91", "xxxxxxxxx"); // BattleZoneRedux 43AA30


        DetourTransactionBegin();
        DetourUpdateThread(GetCurrentThread());

        // this will hook the function
        DetourAttach(&(LPVOID&)AddressOfSum, &HookSum);

        DetourTransactionCommit();
    }
    else if (dwReason == DLL_PROCESS_DETACH)
    {
        // unhook
        DetourTransactionBegin();
        DetourUpdateThread(GetCurrentThread());

        // this will hook the function
        DetourDetach(&(LPVOID&)AddressOfSum, &HookSum);

        DetourTransactionCommit();
    }
    return TRUE;
} 
4
  • 1
    It is impossible to tell why it crashes with information you provided. Perhaps the address of function is wrong, the call convention is wrong, the number of parameters is incorrect etc.
    – morsisko
    Aug 22 '20 at 12:17
  • Thank you morsisko, I have solved with a easy workaround. I have added try and catch and now work perfectly !! thanks. Ps: can you please explain better about call convention is wrong and incorect paramenters ? Aug 22 '20 at 16:58
  • You pass the parameters like (a1, a2, a3, a4), maybe there is for example also a5 parameter? To the call convention thing - there are multiple ways how you can pass the parameters to function. Like pushing it on stack from right to left, left to right, using registers etc. You should take a look at en.wikipedia.org/wiki/X86_calling_conventions
    – morsisko
    Aug 22 '20 at 20:21
  • Not sure, but the std::cout operation dont generate any stack allocation, and the instructions std::string StrFileName, so probably something on the stack potentially got the issue.
    – camp0
    Aug 22 '20 at 21:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.