I have a hook function that executes inline assembly:
__declspec(naked) int hookConnect()
{
__asm
{
pushad;
pushfd;
push [esp + 0x2C];
pop sockAddrBackup;
push[esp + 0x2C];
push[esp + 0x2C];
call mConnect;
popfd;
popad;
call connectTramp;
ret;
}
}
It saves the registers and flags. Then saves one of the args to a variable and finally pushes 2 args and calls my custom function which just logs args.
After that, it restores registers, flags. At this time I want to call the trampoline:
Trampoline:
5 original bytes
jmp (original func + 5 bytes)
Which executes first 5 bytes, jumps to original func and executes it, then returns to my hook function.
At this point, I want to return to the caller of the original func, but since the leave is called when function finishes, it breaks the stack?
How could I preserve the stack through the execution? Save return address before calling?
callbut your description hasjmp. It seems likejmpwould be correct. Also note that you have to copy complete instructions rather than stopping after 5 bytes. There are libraries like MS Detours that take care of all of this for you. - Ben Voigt