jmp api+5 *2


Author: 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
	}
}

#endif

So, 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)

 
Leave Comment:
Name:
Email: (not shown)
Message: (Required)
Math Question: 4 + 70 = ? followed by the letter: A 



Twitter
RSS
About Me
More Blogs
Main Site
Posts: (All)
2020 ( 6 )
2019 ( 12 )
2017 ( 5 )
2016 ( 4 )
2015 ( 6 )
2014 ( 5 )
2013 ( 9 )
2012 (13)
     flash patching
     x64 Hooks
     micro hook
     jmp api+5 *2
     SysAnalyzer Updates
     InjDll runtime config
     C# Asm/Dsm Library
     Shellcode Hook Detection
     Updates II
     findDll
     Java Hacking
     Windows 8
     Win7 x64
2011 ( 19 )
2010 ( 11 )
2009 ( 1 )