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)
 



Twitter
RSS

About Me
More Blogs
Main Site
Posts:
IDA python over IPC
dns wildcard blocking
64bit IDA Plugins
Twitter Feed
anterior lines
misc news/updates
KANAL Mod
Decoders again
CDO.Message Breakpoints
SysAnalyzer Updates
SysAnalyzer and Site Updates
crazy decoder
ida js w/dbg
flash patching #2
JS Graphing
packet reassembly
Delphi IDA Plugin
scdbg IDA integration
API Hash Database
Winmerge plugin
IDACompare Updates
Guest Post @ hexblog
TCP Stream Reassembly
SysAnalyzer Updates
Apilogger Video
Shellcode2Exe trainer
scdbg updates
IDA Javascript w/IDE
Rop Analysis II
scdbg vrs ROP
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
Graphing ideas
.Net Hacking
Old iDefense Releases
BootLoaders
hll shellcode
ActionScript Tips
-patch fu
scdbg ordinal lookup
scdbg -api mode
Peb Module Lists
scdbg vrs Process Injection
GetProcAddress Scanner
scdbg fopen mode
scdbg findsc mode
scdbg MemMonitor
demo shellcodes
scdbg download
api hashs redux
Api hash gen
Retro XSS Chat Codes
Exe as DLL
Olly Plugins
Debugging Explorer
Attach to hidden process
JS Refactoring
Asm and Shellcode in CSharp
Fancy Return Address
PDF Stream Dumper
Malcode Call API by Hash
WinDbg Cheat Sheet
GPG Automation