Inline API HOOK方法有很多,常见的一种方法无需内嵌汇编语句,原理简单易懂,针对64位微软操作系统不允许内嵌汇编这种无解的事比较给力(列宁大神提供的消息,本人未亲自尝试)。下面简单说一下这种方法的原理。
首先说一说inline HOOK的原理,具体的资料上说的很多了,我只是想提一下核心的技术实现,主要是修改映射到内存的api函数的执行代码的第一条指令(一般是第一条指令)成jmp指令,使其跳转到我们的函数,执行完毕之后跳转回去继续执行。
首先我们需要知道一个函数比如MessageBox的首条指令的内容是什么,占几个字节。这我们可以使用ollydbg进行反汇编得到,具体方法参考ollydbg使用手册。我们这里作为例子的是CreateProcessInternalW这个函数,这个函数是由kernel32导出的一个内部函数,我们可以使用ShellExecute来调出这个函数,我们随便写一个程序,调用ShellExecute,反汇编跟踪到CreateProcessInternalW函数(这里涉及反汇编入门知识,随便找找资料就有),就不难发现这个函数第一条指令是push指令,如图:
总共5字节,那我们就可以将他修改成jmp指令指向我们的函数,具体代码如下,这种方法就不多做赘述了:
#include <windows.h> #include <stdio.h> #include <iostream.h> #include <tchar.h> //修改API入口为 mov eax, 00400000;jmp eax是程序能跳转到自己的函数 BYTE NewBytes[5] = {0xe9,0x0,0x0,0x0,0x0}; BYTE OldBytes[5] = {0}; FARPROC CreateProcessInternalW_Addr; typedef BOOL (_stdcall *PFNCreateProcessInternalW) ( HANDLE hToken, LPCTSTR lpApplicationName, LPTSTR lpCommandLine, LPSECURITY_ATTRIBUTES lpProcessAttributes, LPSECURITY_ATTRIBUTES lpThreadAttributes, BOOL bInheritHandles, DWORD dwCreationFlags, LPVOID lpEnvironment, LPCTSTR lpCurrentDirectory, LPSTARTUPINFO lpStartupInfo, LPPROCESS_INFORMATION lpProcessInformation , PHANDLE hNewToken ); PFNCreateProcessInternalW RealCreateProcessInternalW; BOOL WINAPI MyCreateProcessInternalW( HANDLE hToken, LPCTSTR lpApplicationName, LPTSTR lpCommandLine, LPSECURITY_ATTRIBUTES lpProcessAttributes, LPSECURITY_ATTRIBUTES lpThreadAttributes, BOOL bInheritHandles, DWORD dwCreationFlags, LPVOID lpEnvironment, LPCTSTR lpCurrentDirectory, LPSTARTUPINFO lpStartupInfo, LPPROCESS_INFORMATION lpProcessInformation , PHANDLE hNewToken ) { MessageBox(NULL,"MyCreateProcessInternalW","",MB_OK); // 恢复API头8个字节 if(!WriteProcessMemory( INVALID_HANDLE_VALUE, (void*)RealCreateProcessInternalW,(void*)OldBytes,5, NULL)) printf("write error!\n"); //调用正确的函数 FlushInstructionCache(GetCurrentProcess(),(void*)RealCreateProcessInternalW,5); BOOL Flag=(BOOL)(PFNCreateProcessInternalW)RealCreateProcessInternalW(hToken,lpApplicationName, lpCommandLine,lpProcessAttributes, lpThreadAttributes,bInheritHandles, dwCreationFlags,lpEnvironment, lpCurrentDirectory,lpStartupInfo, lpProcessInformation,hNewToken); printf("OK!\n"); // 写入跳转语句,继续Hook WriteProcessMemory( INVALID_HANDLE_VALUE, (void *)RealCreateProcessInternalW,(void*)NewBytes,5, NULL); return TRUE; } void main() { //读CreateFile函数的前8个字节 HMODULE hHand= LoadLibrary("Kernel32.dll"); RealCreateProcessInternalW=(PFNCreateProcessInternalW)GetProcAddress(hHand,"CreateProcessInternalW"); if(ReadProcessMemory(INVALID_HANDLE_VALUE,(void *)RealCreateProcessInternalW,OldBytes,5,NULL)==0) { printf("ReadProcessMemory error\n"); return; } printf("OldBytes is %x%x%x%x%x\n",OldBytes[0],OldBytes[1],OldBytes[2], OldBytes[3],OldBytes[4]); //将NewBytes改成My函数地址 printf("%x\n",MyCreateProcessInternalW); printf("%x\n",RealCreateProcessInternalW); *(DWORD*)(NewBytes + 1) = (DWORD)MyCreateProcessInternalW-(DWORD)RealCreateProcessInternalW-5; printf("NewBytes is %x%x%x%x%x\n",NewBytes[0],NewBytes[1],NewBytes[2],NewBytes[3], NewBytes[4]); //写入跳转,开始Hook WriteProcessMemory(INVALID_HANDLE_VALUE,RealCreateProcessInternalW,NewBytes,5,NULL); ShellExecute(NULL,"open","c:\\Help.txt","","",SW_SHOW); }
上面这种方法有个明显的缺陷,不知道各位看出来没有?对,就是当有多个线程执行了我们inline Hook的目标函数的时候,一个线程在使用修改之前的RealCreateProcessInternalW,一个线程在使用修改之后的RealCreateProcessInternalW,就会导致修改之前的RealCreateProcessInternalW函数不能够实现挂钩的效果。当然也有解决方法,为这个函数加锁,只能够一个线程使用!这种方法当然也有弊端,当要使用这个函数的线程较多时就会出现较多的等待信号量的情况,程序效率变低。
下面我们讲解另一种inline API Hook的方法,这种方法支持多线程操作。
我们很自然的想到,修改了RealCreateProcessInternalW的前5个字节为jmp XXXX到我们的自己的MyCreateProcessInternalW后在执行我们的MyCreateProcessInternalW的时候执行我们的目标指令之后再执行RealCreateProcessInternalW的前5个字节的指令在跳回RealCreateProcessInternalW+5的位置进行执行,不就能够实现真实的RealCreateProcessInternalW的效果了吗?
于是我们就这样写我们的MyCreateProcessInternalW:
BOOL WINAPI MyCreateProcessInternalW( HANDLE hToken, LPCTSTR lpApplicationName, LPTSTR lpCommandLine, LPSECURITY_ATTRIBUTES lpProcessAttributes, LPSECURITY_ATTRIBUTES lpThreadAttributes, BOOL bInheritHandles, DWORD dwCreationFlags, LPVOID lpEnvironment, LPCTSTR lpCurrentDirectory, LPSTARTUPINFO lpStartupInfo, LPPROCESS_INFORMATION lpProcessInformation , PHANDLE hNewToken ) { MessageBox(NULL,"MyCreateProcessInternalW","",MB_OK); _asm { push a mov eax,RealCreateProcessInternalW add eax,5 jmp eax } return TRUE; }
运行程序,发现程序崩溃了!为什么呢???此处楼主也是辛苦的找了很久,最后“列宁”同志一语点醒了我,栈平衡!!!
我们先来回顾一下我们的过程:
发现问题了没有?没有发现问题没关系,我们将问题程序进行反汇编,进入MyCreateProcessInternalW,如图:
我们发现原来RealCreateProcessInternalW的栈信息在jmp到MyCreateProcessInternalW的时候栈数据被破坏了,因为MyCreateProcessInternalW也push了好多数据进去。这就是我们程序崩溃的原因!怎么解决?好办在jmp之前pop所有MyCreateProcessInternalW的push来平衡栈信息。问题来了,RealCreateProcessInternalW的第一条指令如何执行?我们来回顾一下RealCreateProcessInternalW的栈信息是如何初始化的,首先push ebp,再从右至左(_stdcall方式)push所有参数在执行第一条指令push XXXX,push XXX被替换成了jmp指令,所以没有执行,那好办了我们在MyCreateProcessInternalW函数中平衡完栈之后直接push XXX就可以了,仔细想想?前提是我们之前要保存push XXXX的XXX值,如上面的程序中的OldByte中。
所以程序就出来了,这里只贴上我们的MyCreateProcessInternalW函数代码,其他的同上:
BOOL WINAPI MyCreateProcessInternalW( HANDLE hToken, LPCTSTR lpApplicationName, LPTSTR lpCommandLine, LPSECURITY_ATTRIBUTES lpProcessAttributes, LPSECURITY_ATTRIBUTES lpThreadAttributes, BOOL bInheritHandles, DWORD dwCreationFlags, LPVOID lpEnvironment, LPCTSTR lpCurrentDirectory, LPSTARTUPINFO lpStartupInfo, LPPROCESS_INFORMATION lpProcessInformation , PHANDLE hNewToken ) { MessageBox(NULL,"MyCreateProcessInternalW","",MB_OK); _asm { mov eax,RealCreateProcessInternalW add eax,5 pop edi pop esi pop ebx pop ebp push a jmp eax } return TRUE; }
正因为菜鸟的思维局限性,局限性不可避免,换一个函数,换一个编译器就不行了,但是如果我们要求方法尽量简单,代码尽量短小精悍的话这不失为一种好方法,另外如果有人关注通用的方法,参看“列宁”同志的博客:
64位 inline Hook
最后菜鸟言论,仅供娱乐。