jmp api+5 *2Author: David Zimmer Date: 09.09.12 - 8:26pm I have been working to make an x64 build of sclog, which means redoing the hooking engine i built for it. I think I am going to go with the distorm/ntcoreHookingEngine. While I am playing with it, I am also adding several choices of hooks to embed. One in particular i wanted to see if I could add was a jmp api+5 safe hook that would allow the functions to still run. After some experimentation i finally came up with a clean solution to this. Its a 10 byte Api hook that looks like the following: WinExec+0 e9 xxxxxxxx jmp My_HookProc WinExec+5 e8 xxxxxxxx call UnwindProlog #ifdef _M_IX86 void __stdcall output(ULONG_PTR ra){ HOOK_INFO *hi = GetHookInfoFromFunction(ra); if(hi){ printf("jmp+5 api caught %s\n", hi->ApiName ); }else{ printf("jmp+5 api caught %x\n", ra ); } } void __declspec(naked) UnwindProlog(void){ _asm{ mov eax, [ebp-4] //return address sub eax, 10 //function entry point push eax //arg to output call output pop eax //we got here through a call from our api hook stub so this is ret sub eax, 10 //back to api start address mov esp, ebp //now unwind the prolog the shellcode did on its own.. pop ebp //saved ebp jmp eax //and jmp back to the api public entry point to hit main hook jmp } } #endifSo, if the api is called normally, the jmp hook works normally. If the shellcode does its own prolog and tries to get cute and do a jmp+5, the call hook comes into play. The reason i choose a call hook for the second transfer of execution is so that the return address is automatically saved. Since my hook is 10 bytes, we can calculate the function start address, and then look it up to see which api it was to display to the user. Once we do a user notification, and unwind the stack mods, we then transfer execution back to the apis start address, and let the jmp hook do its job as normal. Simple, yet complicated all at once! On another note...not all API can support a 10 byte hook, For simplicity I think I will make it do an automatic fall back to a standard jmp hook if there isnt enough room. Also for x64 Win7 hooks, I will have to do conditional compilations for certain api like GetProcAddress, which just forward to exports of kernelbase.dll now. Comments: (0) |
About Me More Blogs Main Site |