国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

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

2019-11-17 05:38:24
字體:
供稿:網(wǎng)友

  這么多高手在這里,哎,小弟愿意向各位高手學(xué)習(xí)。
Api攔截并不是一個新的技術(shù),很多商業(yè)軟件都采用這種技術(shù)。對windows的Api函數(shù)的攔截,不外乎兩種方法,第一種是Mr. Jeffrey Richter 的修改exe文件的模塊輸入節(jié),種方法,很安全,但很復(fù)雜,而且有些exe文件,沒有Dll的輸入符號的列表,有可能出現(xiàn)攔截不到的情況。第二種方法就是常用的JMP XXX的方法,雖然很古老,卻很簡單實(shí)用。
本文一介紹第二種方法在Win2k下的使用。第二種方法,Win98/me 下因?yàn)檫M(jìn)入Ring0級的方法很多,有LDT,IDT,Vxd等方法,很輕易在內(nèi)存中動態(tài)修改代碼,但在Win2k下,這些方法都不能用,寫WDM太過復(fù)雜,表面上看來很難實(shí)現(xiàn),
其實(shí)不然。Win2k為我們提供了一個強(qiáng)大的內(nèi)存Api操作函數(shù)---VirtualPRotectEx,WriteProcessMemeory,ReadProcessMemeory,有了它們我們就能在內(nèi)存中動態(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ù)請參看MSDN幫助。在Win2k下因?yàn)镈ll和所屬進(jìn)程在同一地址空間,這點(diǎn)又和Win9x/me存在所有進(jìn)程存在共享的地址空間不同,
因此,必須通過鉤子函數(shù)和遠(yuǎn)程注入進(jìn)程的方法,現(xiàn)以一個簡單采用鉤子函數(shù)對MessageBoxA進(jìn)行攔截例子來說明:
其中Dll文件為:
//---------------------------------------------------------------------------

#include <windows.h>

//---------------------------------------------------------------------------
//   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);
}
//---------------------------------------------------------------------------
// 輸出,Uninstall鉤子函數(shù)
extern "C" __declspec(dllexport) __stdcall
BOOL UninstallHook()
{
    return(UnhookWindowsHookEx(g_hHook));
}
//---------------------------------------------------------------------------
// 初始化得到MessageBoxA的地址,并生成Jmp XXX(MyMessageBoxA)的跳轉(zhuǎn)指令
BOOL Init()
{
    hModule = LoadLibrary("user32.dll");
    fpMessageBoxA = GetProcAddress(hModule, "GetSystemDirectoryA");
    if(fpMessageBoxA == NULL)
        return false;
    _asm
    {
        pushad
        lea edi, OldMessageBoxACode
        mov esi, fpMessageBoxA
        cld
        movsd
        movsb
        popad
    }
    NewMessageBoxACode[0] = 0xe9; // jmp MyMessageBoxA的相對地址的指令
    _asm
    {
        lea eax, MyGetSystemDirectory
   &nbs

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 昌乐县| 洪泽县| 三河市| 孟州市| 侯马市| 色达县| 木兰县| 石泉县| 临泉县| 温泉县| 呼图壁县| 靖远县| 贺兰县| 育儿| 武邑县| 稻城县| 丹阳市| 汽车| 鹿邑县| 涞水县| 南川市| 固安县| 长丰县| 宁城县| 阳山县| 霍林郭勒市| 上犹县| 博罗县| 吴旗县| 定结县| 会宁县| 佛山市| 岳池县| 红原县| 老河口市| 民丰县| 茂名市| 遵化市| 鄢陵县| 江华| 东阳市|