Win2K下的Api函數(shù)的攔截

字號(hào):

Api攔截并不是一個(gè)新的技術(shù),很多商業(yè)軟件都采用這種技術(shù)。對(duì)windows的Api函數(shù)的攔截,不外乎兩種方法,第一種是Mr. Jeffrey Richter 的修改exe文件的模塊輸入節(jié),種方法,很安全,但很復(fù)雜,而且有些exe文件,沒有Dll的輸入符號(hào)的列表,有可能出現(xiàn)攔截不到的情況。第二種方法就是常用的JMP XXX的方法,雖然很古老,卻很簡(jiǎn)單實(shí)用。
    本文一介紹第二種方法在Win2k下的使用。第二種方法,Win98/me 下因?yàn)檫M(jìn)入Ring0級(jí)的方法很多,有LDT,IDT,Vxd等方法,很容易在內(nèi)存中動(dòng)態(tài)修改代碼,但在Win2k下,這些方法都不能用,寫WDM太過復(fù)雜,表面上看來很難實(shí)現(xiàn),其實(shí)不然。Win2k為我們提供了一個(gè)強(qiáng)大的內(nèi)存Api操作函數(shù)---VirtualProtectEx,WriteProcessMemeory,ReadProcessMemeory,有了它們我們就能在內(nèi)存中動(dòng)態(tài)修改代碼了,其原型為:
    BOOL VirtualProtectEx(
     HANDLE hProcess, // 要修改內(nèi)存的進(jìn)程句柄
     LPVOID lpAddress, // 要修改內(nèi)存的起始地址
     DWORD dwSize, // 修改內(nèi)存的字節(jié)
     DWORD flNewProtect, // 修改后的內(nèi)存屬性
     PDWORD lpflOldProtect // 修改前的內(nèi)存屬性的地址
    );
    BOOL WriteProcessMemory(
     HANDLE hProcess, // 要寫進(jìn)程的句柄
     LPVOID lpBaseAddress, // 寫內(nèi)存的起始地址
     LPVOID lpBuffer, // 寫入數(shù)據(jù)的地址
     DWORD nSize, // 要寫的字節(jié)數(shù)
     LPDWORD lpNumberOfBytesWritten // 實(shí)際寫入的子節(jié)數(shù)
    );
    BOOL ReadProcessMemory(
     HANDLE hProcess, // 要讀進(jìn)程的句柄
     LPCVOID lpBaseAddress, // 讀內(nèi)存的起始地址
     LPVOID lpBuffer, // 讀入數(shù)據(jù)的地址
     DWORD nSize, // 要讀入的字節(jié)數(shù)
     LPDWORD lpNumberOfBytesRead // 實(shí)際讀入的子節(jié)數(shù)
    );
    具體的參數(shù)請(qǐng)參看MSDN幫助。在Win2k下因?yàn)镈ll和所屬進(jìn)程在同一地址空間,這點(diǎn)又和Win9x/me存在所有進(jìn)程存在共享的地址空間不同,
    因此,必須通過鉤子函數(shù)和遠(yuǎn)程注入進(jìn)程的方法,現(xiàn)以一個(gè)簡(jiǎn)單采用鉤子函數(shù)對(duì)MessageBoxA進(jìn)行攔截例子來說明:
    其中Dll文件為:
    //---------------------------------------------------------------------------
    #include
    //---------------------------------------------------------------------------
    // Important note about DLL memory management when your DLL uses the
    // static version of the RunTime Library:
    //
    // If your DLL exports any functions that pass String objects (or structs/
    // classes containing nested Strings) as parameter or function results,
    // you will need to add the library MEMMGR.LIB to both the DLL project and
    // any other projects that use the DLL. You will also need to use MEMMGR.LIB
    // if any other projects which use the DLL will be performing new or delete
    // operations on any non-TObject-derived classes which are exported from the
    // DLL. Adding MEMMGR.LIB to your project will change the DLL and its calling
    // EXE's to use the BORLNDMM.DLL as their memory manager. In these cases,
    // the file BORLNDMM.DLL should be deployed along with your DLL.
    //
    // To avoid using BORLNDMM.DLL, pass string information using "char *" or
    // ShortString parameters.
    //
    // If your DLL uses the dynamic version of the RTL, you do not need to
    // explicitly add MEMMGR.LIB as this will be done implicitly for you
    //---------------------------------------------------------------------------
    #pragma argsused
    HHOOK g_hHook;
    HINSTANCE g_hinstDll;
    FARPROC fpMessageBoxA;
    HMODULE hModule ;
    BYTE OldMessageBoxACode[5], NewMessageBoxACode[5];
    DWORD dwIdOld, dwIdNew;
    BOOL bHook = false;
    void HookOn();
    void HookOff();
    BOOL Init();
    int WINAPI MyMessageBoxA(HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, UINT uType);
    //---------------------------------------------------------------------------
    // 空的鉤子函數(shù)
    LRESULT WINAPI Hook(int nCode, WPARAM wParam, LPARAM lParam)
    {
     return(CallNextHookEx(g_hHook, nCode, wParam, lParam));
    }
    //---------------------------------------------------------------------------
    // 輸出,安裝空的鉤子函數(shù)
    extern "C" __declspec(dllexport) __stdcall
    BOOL InstallHook()
    {
     g_hinstDll = LoadLibrary("project1.dll"); // 這里的文件名為Dll本身的文件名
     g_hHook = SetWindowsHookEx(WH_GETMESSAGE, (HOOKPROC)Hook, g_hinstDll, 0);
     if (!g_hHook)
     {
     MessageBoxA(NULL, "SET ERROR", "ERROR", MB_OK);
     return(false);
     }
     return(true);
    }
    //---------------------------------------------------------------------------