《Windows核心编程》---CAPIHook类

封装CAPIHook类:(某本书的笔记)

1HOOK所有模块

HOOK一个进程对某个API的调用时,不仅要修改主模块的导入表,还必须遍历此进程的所有模块,替换掉每个对目标API的调用:

void CAPIHook::ReplaceIATEntryInOneMod(LPSTR pszExportMod, PROC pfnCurrent,

PROC pfnNew, HMODULE hModCaller)

{

//取得模块的导入表(import descriptor)的首地址

//ImageDirectoryEntryToData函数可以返回导入表地址

ULONG ulSize;

PIMAGE_IMPORT_DESCRIPTOR pImportDesc =

(PIMAGE_IMPORT_DESCRIPTOR)::ImageDirectoryEntryToData(hModCaller, TRUE,

IMAGE_DIRECTORY_ENTRY_IMPORT, &ulSize);

if(pImportDesc == NULL) //这个模块没有导入表项

return;

//查找包含pszExportMod模块中函数导入信息的导入表项

while(pImportDesc->Name != 0)

{

LPSTR pszMod = (LPSTR)((DWORD)hModCaller + pImportDesc->Name);

if (lstrcmpiA(pszMod, pszExportMod) == 0) //找到

{

break;

}

pImportDesc++;

}

if (pImportDesc->Name == 0) //hModCaller模块没有从pszExportMod模块导入任何函数

{

return;

}

//取得调用者的导入地址表(import address table, IAT)

PIMAGE_THUNK_DATA pThunk = (PIMAGE_THUNK_DATA)(pImportDesc->FirstThunk +

(DWORD)hModCaller);

//查找我们要HOOK的函数,将它的地址用新函数的地址替换掉

while (pThunk->u1.Function)

{

//lpAddr指向的内存保存了函数的地址

PDWORD lpAddr = (PDWORD)&(pThunk->u1.Function);

if (*lpAddr == (DWORD)pfnCurrent)

{

//修改页的保护属性

DWORD dwOldProtect;

MEMORY_BASIC_INFORMATION mbi;

::VirtualQuery(lpAddr, &mbi, sizeof(mbi));

::VirtualProtect(lpAddr, sizeof(DWORD), PAGE_READWRITE, &dwOldProtect);

//修改内存地址,相当于"lpAddr = (DWORD)pfnNew;"

::WriteProcessMemory(::GetCurrentProcess(), lpAddr,

&pfnNew, sizeof(DWORD), NULL);

::VirtualProtect(lpAddr, sizeof(DWORD), dwOldProtect, 0);

break;

}

pThunk++;

}

}

void CAPIHook::ReplaceIATEntryInAllMods(LPSTR pszExportMod, PROC pfnCurrent,

PROC pfnNew, BOOL bExcludeAPIHookMod)

{

//取得当前模块的句柄

HMODULE hModThis = NULL;

if(bExcludeAPIHookMod)

{

MEMORY_BASIC_INFORMATION mbi;

if(::VirtualQuery(ReplaceIATEntryInAllMods, &mbi, sizeof(mbi)) != 0)

hModThis = (HMODULE)mbi.AllocationBase;

}

//取得本进程的模块列表

HANDLE hSnap = ::CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, ::GetCurrentProcessId());

//遍历所有模块,分别对它们调用ReplaceIATEntryInOnMod函数,修改导入地址表

MODULEENTRY32 me = {sizeof(MODULEENTRY32)};

BOOL bOK = ::Module32First(hSnap, &me);

while (bOK)

{

//注意,我们不HOOK当前模块的函数

if (me.hModule != hModThis)

{

ReplaceIATEntryInOneMod(pszExportMod, pfnCurrent, pfnNew, me.hModule);

}

bOK = ::Module32Next(hSnap, &me);

}

::CloseHandle(hSnap);

}

ReplaceIATEntryInOneMod函数修改hModCaller模块的IAT,将所有对pfnCurrent函数的调用改为对pfnNew函数的调用,参数pszExportMod是目标API所在的模块。例如,为了将自定义的函数AceMessageBoxA替换当前模块中的MessageBoxA函数,可以调用如下:

CAPIHOOK::ReplaceIATEntryInOneMod("User32.dll",

(PROC)AceMessageBoxA, (PROC)MessageBoxA, ::GetModuleHandle(NULL));

ReplaceIATEntryInAllMods函数修改进程内所有模块的IAT,挂钩用户指定的API函数,最后一个参数bExcludeAPIHookMod指定是否将负责HOOK API的模块排除在外。

由于CAPIHook类工作在一个单独的DLL中,为了在此模块中方便地调用原来的API函数,一般选择不HOOK当前模块的函数,即将bExcludeAPIHookMod设为TRUE

2)防止程序在运行期间动态加载模块

HOOK完目标进程当前所有模块之后,目标进程还可以调用LoadLibrary函数加载新的模块。为了将今后目标进程动态加载的模块也HOOK掉,我们可以默认挂钩LoadLibrary之类的函数。方法就是在代理函数中首先调用原来的LoadLibrary函数,然后再对新加载的模块调用ReplaceIATEntryInOneMod函数。

我们设替换LoadLibrary的自定义函数是HookNewlyLoadedModule。一个CAPIHook对象仅能够挂钩一个API函数,为了挂钩多个API,用户很可能申请了多个CAPIHook对象,所以,在自定义函数HookNewlyLoadedModule中,必须为每个CAPIHook对象调用ReplaceIATEntryInOneMod函数才能确保新模块中相应的IAT项被修改。因此,我们需要记录用户申请的所有CAPIHook对象的指针。

比较简单的方法是将所有CAPIHook对象连成一个链表,用一个静态变量记录下表头地址,在每个CAPIHook对象中再记录表中下一个CAPIHook对象的地址:

class CAPIHook

{

//这两个指针用来将当前模块中所有的CAPIHook对象连在一起

static CAPIHook *sm_pHeader;

CAPIHook *m_pNext;

}

静态函数HookNewlyLoadedModule的实现代码如下:

void WINAPI CAPIHook::HookNewlyLoadedModule(HMODULE hModule, DWORD dwFlags)

{

//如果一个新的模块被加载,挂钩各CAPIHook对象要求的API函数

if((hModule != NULL) && ((dwFlags&LOAD_LIBRARY_AS_DATAFILE) == 0 ))

{

CAPIHook *p = sm_pHeader;

while(p != NULL)

{

ReplaceIATEntryInOneMod(p->m_pszModName, p->m_pfnOrig,

p->m_pfnHook, hModule);

p = p->m_pNext;

}

}

}

其中,dwFlagsLoadLibraryEx函数的一个附加参数,它指定了加载时采取的行动,只要它的值中不包含LOAD_LIBRARY_AS_DATAFILE标记,就说明文件要以镜像方式映射到内存中。

3)防止程序在运行期间动态调用API函数

并不是只有经过导入表才能调用API函数,应用程序可以在运行期间调用GetProcAddress函数取得API函数的地址再调用它,因此,我们也需要默认挂钩GetProcAddress函数。CAPIHook类的静态成员函数GetProcAddress将替换这个API

FARPROC WINAPI CAPIHook::GetProcAddress(HMODULE hModule, PCSTR pszProcName)

{

//得到这个函数的真实地址

FARPROC pfn = ::GetProcAddress(hModule, pszProcName);

//看它是不是我们要hook的函数

CAPIHook *p = sm_pHeader;

while(p != NULL)

{

if(p->m_pfnOrig == pfn)

{

pfn = p->m_pfnHook;

break;

}

p = p->m_pNext;

}

return pfn;

}

下面是完整的CAPIHook类的实现:

=====================APIHook.h============================

#ifndef __APIHOOK_H__

#define __APIHOOK_H__

#include <windows.h>

class CAPIHook

{

public:

CAPIHook(LPSTR pszModName, LPSTR pszFuncName,

PROC pfnHook, BOOL bExcludeAPIHookMod=TRUE);

virtual ~CAPIHook();

operator PROC() {return m_pfnOrig;}

private:

LPSTR m_pszModName; //导出要HOOK函数的模块的名字

LPSTR m_pszFuncName; //HOOK的函数的名字

PROC m_pfnOrig; //API函数地址

PROC m_pfnHook; //HOOK后函数的地址

BOOL m_bExcludeAPIHookMod; //是否将HOOK API的模块本身排除在外

private:

static void ReplaceIATEntryInAllMods(LPSTR pszExportMod, PROC pfnCurrent,

PROC pfnNew, BOOL bExcludeAPIHookMod);

static void ReplaceIATEntryInOneMod(LPSTR pszExportMod, PROC pfnCurrent,

PROC pfnNew, HMODULE hModCaller);

//下面的代码用来解决其他模块动态加载DLL的问题

private:

//这两个指针用来将所有的CAPIHook对象连在一起

static CAPIHook *sm_pHeader;

CAPIHook *m_pNext;

private:

//当一个新的DLL被加载时,调用此函数

static void WINAPI HookNewlyLoadedModule(HMODULE hModule, DWORD dwFlags);

//用来跟踪当前进程加载新的DLL

static HMODULE WINAPI LoadLibraryA(PCSTR pszModulePath);

static HMODULE WINAPI LoadLibraryW(PCWSTR pszModulePath);

static HMODULE WINAPI LoadLibraryExA(PCSTR pszModulePath, HANDLE hFile, DWORD dwFlags);

static HMODULE WINAPI LoadLibraryExW(PCWSTR pszModulePath, HANDLE hFile, DWORD dwFlags);

//如果请求已HOOKAPI函数,则返回用户自定义函数的地址

static FARPROC WINAPI GetProcAddress(HMODULE hModule, PCSTR pszProcName);

private:

//自动对这些函数进行挂钩

static CAPIHook sm_LoadLibraryA;

static CAPIHook sm_LoadLibraryW;

static CAPIHook sm_LoadLibraryExA;

static CAPIHook sm_LoadLibraryExW;

static CAPIHook sm_GetProcAddress;

};

#endif

========================APIHook.cpp========================

#include "APIHook.h"

#include "Tlhelp32.h"

#include <ImageHlp.h> //为了调用ImageDirectoryEntryToData函数

#pragma comment(lib, "ImageHlp")

//CAPIHook对象链表的头指针

CAPIHook *CAPIHook::sm_pHeader = NULL;

//////////////////////////////////////////////////////////////////////

// Construction/Destruction

//////////////////////////////////////////////////////////////////////

CAPIHook::CAPIHook(LPSTR pszModName, LPSTR pszFuncName,

PROC pfnHook, BOOL bExcludeAPIHookMod/* =TRUE */)

{

//保存这个Hook函数的信息

m_bExcludeAPIHookMod = bExcludeAPIHookMod;

m_pszModName = pszModName;

m_pszFuncName = pszFuncName;

m_pfnHook = pfnHook;

m_pfnOrig = ::GetProcAddress(::GetModuleHandle(pszModName), pszFuncName);

//将此对象添加到链表中

m_pNext = sm_pHeader;

sm_pHeader = this;

//在所有当前已加载的模块中HOOK这个函数

ReplaceIATEntryInAllMods(m_pszModName, m_pfnOrig, m_pfnHook,

bExcludeAPIHookMod);

}

CAPIHook::~CAPIHook()

{

//取消对所有模块中函数的HOOK

ReplaceIATEntryInAllMods(m_pszModName, m_pfnHook, m_pfnOrig,

m_bExcludeAPIHookMod);

CAPIHook *p = sm_pHeader;

//从链表中移除此对象

if(p == this)

{

sm_pHeader = p->m_pNext;

}

else

{

while(p != NULL)

{

if(p->m_pNext == this)

{

p->m_pNext = this->m_pNext;

break;

}

p = p->m_pNext;

}

}

}

void CAPIHook::ReplaceIATEntryInOneMod(LPSTR pszExportMod, PROC pfnCurrent,

PROC pfnNew, HMODULE hModCaller)

{

//取得模块的导入表(import descriptor)的首地址

//ImageDirectoryEntryToData函数可以返回导入表地址

ULONG ulSize;

PIMAGE_IMPORT_DESCRIPTOR pImportDesc =

(PIMAGE_IMPORT_DESCRIPTOR)::ImageDirectoryEntryToData(hModCaller, TRUE,

IMAGE_DIRECTORY_ENTRY_IMPORT, &ulSize);

if(pImportDesc == NULL) //这个模块没有导入表项

return;

//查找包含pszExportMod模块中函数导入信息的导入表项

while(pImportDesc->Name != 0)

{

LPSTR pszMod = (LPSTR)((DWORD)hModCaller + pImportDesc->Name);

if (lstrcmpi(pszMod, pszExportMod) == 0) //找到

{

break;

}

pImportDesc++;

}

if (pImportDesc->Name == 0) //hModCaller模块没有从pszExportMod模块导入任何函数

{

return;

}

//取得调用者的导入地址表(import address table, IAT)

PIMAGE_THUNK_DATA pThunk = (PIMAGE_THUNK_DATA)(pImportDesc->FirstThunk +

(DWORD)hModCaller);

//查找我们要HOOK的函数,将它的地址用新函数的地址替换掉

while (pThunk->u1.Function)

{

//lpAddr指向的内存保存了函数的地址

PDWORD lpAddr = (PDWORD)&(pThunk->u1.Function);

if (*lpAddr == (DWORD)pfnCurrent)

{

//修改页的保护属性

DWORD dwOldProtect;

MEMORY_BASIC_INFORMATION mbi;

::VirtualQuery(lpAddr, &mbi, sizeof(mbi));

::VirtualProtect(lpAddr, sizeof(DWORD), PAGE_READWRITE, &dwOldProtect);

//修改内存地址,相当于"lpAddr = (DWORD)pfnNew;"

::WriteProcessMemory(::GetCurrentProcess(), lpAddr,

&pfnNew, sizeof(DWORD), NULL);

::VirtualProtect(lpAddr, sizeof(DWORD), dwOldProtect, 0);

break;

}

pThunk++;

}

}

void CAPIHook::ReplaceIATEntryInAllMods(LPSTR pszExportMod, PROC pfnCurrent,

PROC pfnNew, BOOL bExcludeAPIHookMod)

{

//取得当前模块的句柄

HMODULE hModThis = NULL;

if(bExcludeAPIHookMod)

{

MEMORY_BASIC_INFORMATION mbi;

if(::VirtualQuery(ReplaceIATEntryInAllMods, &mbi, sizeof(mbi)) != 0)

hModThis = (HMODULE)mbi.AllocationBase;

}

//取得本进程的模块列表

HANDLE hSnap = ::CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, ::GetCurrentProcessId());

//遍历所有模块,分别对它们调用ReplaceIATEntryInOnMod函数,修改导入地址表

MODULEENTRY32 me = {sizeof(MODULEENTRY32)};

BOOL bOK = ::Module32First(hSnap, &me);

while (bOK)

{

//注意,我们不HOOK当前模块的函数

if (me.hModule != hModThis)

{

ReplaceIATEntryInOneMod(pszExportMod, pfnCurrent, pfnNew, me.hModule);

}

bOK = ::Module32Next(hSnap, &me);

}

::CloseHandle(hSnap);

}

//挂钩LoadLibraryGetProcAddress函数,以便在这些函数被调用以后,挂钩的函数也能够被正确的处理

CAPIHook CAPIHook::sm_LoadLibraryA("Kernel32.dll", "LoadLibraryA",

(PROC)CAPIHook::LoadLibraryA, TRUE);

CAPIHook CAPIHook::sm_LoadLibraryW("Kernel32.dll", "LoadLibraryW",

(PROC)CAPIHook::LoadLibraryW, TRUE);

CAPIHook CAPIHook::sm_LoadLibraryExA("Kernel32.dll", "LoadLibraryExA",

(PROC)CAPIHook::LoadLibraryExA, TRUE);

CAPIHook CAPIHook::sm_LoadLibraryExW("Kernel32.dll", "LoadLibraryExW",

(PROC)CAPIHook::LoadLibraryExW, TRUE);

CAPIHook CAPIHook::sm_GetProcAddress("Kernel32.dll", "GetProcAddress",

(PROC)CAPIHook::GetProcAddress, TRUE);

void WINAPI CAPIHook::HookNewlyLoadedModule(HMODULE hModule, DWORD dwFlags)

{

//如果一个新的模块被加载,挂钩各CAPIHook对象要求的API函数

if((hModule != NULL) && ((dwFlags&LOAD_LIBRARY_AS_DATAFILE) == 0))

{

CAPIHook *p = sm_pHeader;

while (p != NULL)

{

ReplaceIATEntryInOneMod(p->m_pszModName, p->m_pfnOrig,

p->m_pfnHook, hModule);

p = p->m_pNext;

}

}

}

HMODULE WINAPI CAPIHook::LoadLibraryA(PCSTR pszModulePath)

{

HMODULE hModule = ::LoadLibraryA(pszModulePath);

HookNewlyLoadedModule(hModule, 0);

return hModule;

}

HMODULE WINAPI CAPIHook::LoadLibraryW(PCWSTR pszModulePath)

{

HMODULE hModule = ::LoadLibraryW(pszModulePath);

HookNewlyLoadedModule(hModule, 0);

return hModule;

}

HMODULE WINAPI CAPIHook::LoadLibraryExA(PCSTR pszModulePath, HANDLE hFile, DWORD dwFlags)

{

HMODULE hModule = ::LoadLibraryExA(pszModulePath, hFile, dwFlags);

HookNewlyLoadedModule(hModule, dwFlags);

return hModule;

}

HMODULE WINAPI CAPIHook::LoadLibraryExW(PCWSTR pszModulePath, HANDLE hFile, DWORD dwFlags)

{

HMODULE hModule = ::LoadLibraryExW(pszModulePath, hFile, dwFlags);

HookNewlyLoadedModule(hModule, dwFlags);

return hModule;

}

FARPROC WINAPI CAPIHook::GetProcAddress(HMODULE hModule, PCSTR pszProcName)

{

//得到函数的真实地址

FARPROC pfn = ::GetProcAddress(hModule, pszProcName);

//看它是否我们要HOOK的函数

CAPIHook *p = sm_pHeader;

while(p != NULL)

{

if (p->m_pfnOrig == pfn)

{

pfn = p->m_pfnHook;

break;

}

p = p->m_pNext;

}

return pfn;

}

你可能感兴趣的:(windows)