Api hooking -- x64

hii everyone, hope your good.
so i am trying to hook messageboxA() function:

/*
** Simple MessageBoxA hook using the classic 5 byte relative jump technique without a         
trampoline.
** Instead of bypassing the hook in the proxy function when passing execution to 
MessageBoxA, we
** will simply re-write the original bytes, unhooking the function.
*/

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


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

char saved_buffer[5]; // buffer to save the original bytes
FARPROC hooked_address = NULL;
typedef int(__cdecl* MYPROC)(LPWSTR);
// The proxy function we will jump to after the hook has been installed
int __stdcall proxy_function(HWND hWnd, LPCSTR lpText, LPCSTR lpCaption, UINT 
uType)
{
std::cout << "Hello from MessageBox!\n";
std::cout << "Text: " << (LPCSTR)lpText << "\nCaption: " << (LPCSTR)lpCaption << 
std::endl;

// unhook the function (re-write the saved buffer) to prevent infinite recursion
WriteProcessMemory(GetCurrentProcess(), (LPVOID)hooked_address, saved_buffer, 5, 
NULL);

// return to the original function, which is now unhooked, and modify the intended     
parameters
return MessageBoxA(NULL, "yeet", "yeet", uType);
}

void install_hook()
{
HINSTANCE hinstLib;
VOID* proxy_address;
DWORD* relative_offset;
DWORD src;
DWORD dst;
CHAR patch[5] = { 0 };
 
// 1. get memory address of the MessageBoxA function from user32.dll 
hinstLib = LoadLibrary(TEXT("user32.dll"));
hooked_address = GetProcAddress(hinstLib, "MessageBoxA");

// 2. save the first 5 bytes into saved_buffer
ReadProcessMemory(GetCurrentProcess(), hooked_address, saved_buffer, 5, NULL);

// 3. overwrite the first 5 bytes with a jump to proxy_function
proxy_address = &proxy_function;
src = (DWORD)hooked_address + 5; // will jump from the next instruction (after our 5 byte         
jmp instruction)
dst = (DWORD)proxy_address;
relative_offset = (DWORD*)(dst - src);

memcpy(patch, "\xE9", 1);
memcpy(patch + 1, &relative_offset, 4);

WriteProcessMemory(GetCurrentProcess(), (LPVOID)hooked_address, patch, 5, NULL);
}
 


int main()
{ 

// call without hook
MessageBoxA(NULL, "hello calling from the program", "hello", MB_OK);

install_hook();

// call with hook (arguments will be altered through the proxy function)
MessageBoxA(NULL, "we are the hook ", "hello", MB_OK);

return 0;
}

and it didnt work, the main reason why i am doing this is because i want to hook creatprocessA / W
so if anyone can help in a library for that or some source code, or a guide or anything ill be so thankful, or at least what did i do wrong in the above code.
i found a lot of libraries on github but it is so big, and i don’t want my executable to be that big, specially since i am trying to hook 2 or 3 functions, and most of them are for x86 and not for x64
and does the same code work if i changed it to hook createprocessw/a or createprocess

This post was flagged by the community and is temporarily hidden.

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.