Page 1 of 1

Is it possible to hook un-exported function

PostPosted: Sat Dec 14, 2019 6:18 pm
by wineggdrop
#include <windows.h>
#include <stdio.h>

bool TestFunction()
{
..............
return true;
}

int main(int argc,char *argv[])
{
TestFunction();
return 0;
}

Is it possible to hook TestFunction() in the above program with madcodehook?if so,how?

Re: Is it possible to hook un-exported function

PostPosted: Sun Dec 15, 2019 8:11 pm
by iconic
Hello,

Unexported functions can be tricky to hook only because they can be tricky to "find". Most common approach, but not the best in this case, is to use binary signature pattern scanning.

If you break down the below function:

BOOL TestFunction()
{
...............
return TRUE;
}

You would only have these bytes to search for, unless ... is indicative of more code you can scan for, of course.
Code: Select all
0xb8, 0x01, 0x00, 0x00, 0x00, 0xc3

with the assembly code looking like this
Code: Select all
mov eax, 0x00000001
ret


You can scan the specific module containing your target function by searching through its PE sections that contain executable code, usually .text and/or .code sections are the most common.
Once your signature function locates the code pattern you can return the (hModule + code section base address + code section index you matched at) for the function pointer and then
pass this into HookCode(). It's actually rather simple to do but creating unique code signature patterns can prove difficult and depends on what the binary code structure looks like.

--Iconic

Re: Is it possible to hook un-exported function

PostPosted: Wed Dec 18, 2019 10:05 am
by madshi
Yep, as iconic said, finding the address of the function you want to hook is the tricky part. The actual hooking itself is pretty simply, just call HookCode(). Of course if you already happen to know the address of the function you want to hook, everything's easy as cake.