[QUESTION] How to hook this function properly?

05/11/2022 11:20 senpaiex#1
Hi everyone, im trying to get some arguments from a function. But i couldnt hook this function;

Code:
module_addr = 0x123456;
module_instance = reinterpret_cast<DWORD>(module_addr);

typedef bool(__thiscall* tModuleFunc)(void* This, int arg1, int arg2);
static DWORD func_addr = 0x112233;
static tModuleFunc ModuleFunc;

ModuleFunc = (tModuleFunc)(func_addr);

void ShowArgs(int arg1, int arg2){
        std::cout << arg1= << arg1 << std::endl;
        std::cout << arg2= << arg2 << std::endl;
}

static bool CallModuleFunc(int arg1, int arg2)
{
    try
    {
        return ModuleFunc((void*)module_instance, arg1, arg2);
    }
    catch (...)
    {
        return false;
    }
}

static bool HookCallModuleFunc(int arg1, int arg2)
{
    try
    {
	ShowArgs(arg1, arg2);
        return ModuleFunc((void*)module_instance, arg1, arg2);
    }
    catch (...)
    {
        return false;
    }
}
What is the right way to do this ? Thanks all!
05/11/2022 18:36 br4ve-trave1er.asf#2
Quote:
Originally Posted by senpaiex View Post
Hi everyone, im trying to get some arguments from a function. But i couldnt hook this function;

Code:
module_addr = 0x123456;
module_instance = reinterpret_cast<DWORD>(module_addr);

typedef bool(__thiscall* tModuleFunc)(void* This, int arg1, int arg2);
static DWORD func_addr = 0x112233;
static tModuleFunc ModuleFunc;

ModuleFunc = (tModuleFunc)(func_addr);

void ShowArgs(int arg1, int arg2){
        std::cout << arg1= << arg1 << std::endl;
        std::cout << arg2= << arg2 << std::endl;
}

static bool CallModuleFunc(int arg1, int arg2)
{
    try
    {
        return ModuleFunc((void)module_instance, arg1, arg2);
    }
    catch (...)
    {
        return false;
    }
}

static bool HookCallModuleFunc(int arg1, int arg2)
{
    try
    {
	ShowArgs(arg1, arg2);
        return ModuleFunc((void)module_instance, arg1, arg2);
    }
    catch (...)
    {
        return false;
    }
}
What is the right way to do this ? Thanks all!
1. (void) -> (void*)

if it still wont work please show disassembly/pseudocode/c++ of the function and give more informations about what isnt working
05/11/2022 20:56 senpaiex#3
Quote:
Originally Posted by br4ve-trave1er.asf View Post
1. (void) -> (void*)

if it still wont work please show disassembly/pseudocode/c++ of the function and give more informations about what isnt working
Sorry it is already (void*) actually, my bad. Yes i'll share asm part of it as soon as possible.
05/13/2022 03:52 badforce788#4
you may be giving missing arguments
05/13/2022 19:13 senpaiex#5
Quote:
Originally Posted by badforce788 View Post
you may be giving missing arguments
No im sure but it doesnt matter anyway. Im just asking "How to hook a function like this one?". I mean what is the right way?
05/13/2022 23:02 Mi4uric3#6
"i couldnt hook this function" is not a very detailed error description, neither is your code. All I can see is that your `HookCallModuleFunc` is not `__stdcall`, which I assume is the function you hook the game function to.
05/16/2022 01:10 senpaiex#7
Quote:
Originally Posted by Mi4uric3 View Post
"i couldnt hook this function" is not a very detailed error description, neither is your code. All I can see is that your `HookCallModuleFunc` is not `__stdcall`, which I assume is the function you hook the game function to.
Yes its game function. Its not "detailed error description" because its not an error. If you read the title of thread "How to hook this fuction properly?" you can see that im just asking "What is the right way to do this?".

(I think the code is totally fine btw)
05/16/2022 09:57 br4ve-trave1er.asf#8
Quote:
Originally Posted by senpaiex View Post
Yes its game function. Its not "detailed error description" because its not an error. If you read the title of thread "How to hook this fuction properly?" you can see that im just asking "What is the right way to do this?".

(I think the code is totally fine btw)
well yes, but what is the original function, we just see your pseudo code - the already mentioned asm hasn't been added. Even a simple "im trying to hook CPythonNetworkStream::SendOnClickPacket" or "im trying to hook function 0x123456 on gf metin2 turkish servers which is used for xyz *here* is the disassembly which i used to get the address and the calltype from" would help helping you
05/16/2022 16:22 Mi4uric3#9
There are tons of resources online on how to hook (stdcall) functions, there is no need for us to spoonfeed you. You can look them up in your favorite search engine. If you then have any problem applying the methods you learned there you can come back with a specific question, detailing what you tried and what problems you encountered.