C++實現(xiàn)inline hook的原理及應用實例
本文實例簡述了C++實現(xiàn)inline hook的原理及應用,對于大家更好的理解inline hook原理及其應用有很大的幫助。具體內(nèi)容如下:
一、Inline Hook簡介:
1.INLINE HOOK原理:
Inline Hook通過硬編碼的方式向內(nèi)核API的內(nèi)存空間(通常是開始的一段字節(jié),且一般在第一個call之前,這么做是為了防止堆?;靵y)寫入跳轉(zhuǎn)語句,這樣,該API只要被調(diào)用,程序就會跳轉(zhuǎn)到我們的函數(shù)中來,我們在自己寫的函數(shù)里需要完成3個任務:
1)重新調(diào)整當前堆棧。程序流程在剛剛跳轉(zhuǎn)的時候,內(nèi)核API并沒有執(zhí)行完,而我們的函數(shù)需要根據(jù)其結(jié)果來進行信息過濾,所以我們需要保證內(nèi)核API能在順利執(zhí)行完畢后返回到我們的函數(shù)中來,這就要求對當前堆棧做一個調(diào)整。
2)執(zhí)行遺失的指令。我們向內(nèi)核API地址空間些如跳轉(zhuǎn)指令(jmp xxxxxxxx)時,勢必要覆蓋原先的一些匯編指令,所以我們一定要保證這些被覆蓋的指令能夠順利執(zhí)行(否則,你的及其就要BSOD了,呵呵,Blue Screen Of Death)。關(guān)于這部分指令的執(zhí)行,一般是將其放在我們的函數(shù)中,讓我們的函數(shù)“幫助”內(nèi)核API執(zhí)行完被覆蓋的指令,然后再跳回內(nèi)核API中被覆蓋內(nèi)后后的地址繼續(xù)執(zhí)行剩余內(nèi)容。跳回去的時候,一定要算好是跳回到什么地址,是內(nèi)核API起始地址后的第幾個字節(jié)。
3)信息過濾。這個就不用多說了,內(nèi)核API順利執(zhí)行并返回到我們的函數(shù)中,我們自然要根據(jù)其結(jié)果做一些信息過濾,這部分內(nèi)容因被hook的API以及Hook目的的不同而不同。
2.Inline hook的工作流程:
1)驗證內(nèi)核API的版本(特征碼匹配)。
2)撰寫自己的函數(shù),要完成以上三項任務。
3)獲取自己函數(shù)的地址,覆蓋內(nèi)核API內(nèi)存,供跳轉(zhuǎn)。
簡而言之,inlinehook的原理就是,修改函數(shù),使其跳轉(zhuǎn)到我們指定的地方。
常見的有改函數(shù)入口,也有改函數(shù)尾,函數(shù)中間的
比如,通常函數(shù)開頭的匯編代碼都是這樣:mov edi,edi;push esp;mov ebp,esp,而我們便可以通過修改這里進行HOOK。
二、示例代碼(該示例摘自看雪)
#include <ntifs.h> #include <windef.h> ULONG g_KiInsertQueueApc; ULONG g_uCr0; BYTE g_HookCode[5] = { 0xe9, 0, 0, 0, 0 }; //JMP NEAR BYTE g_OrigCode[5] = { 0 }; // 原函數(shù)的前字節(jié)內(nèi)容 BYTE jmp_orig_code[7] = { 0xEA, 0, 0, 0, 0, 0x08, 0x00 }; //JMP FAR BOOL g_bHooked = FALSE; VOID fake_KiInsertQueueApc ( PKAPC Apc, KPRIORITY Increment ); VOID Proxy_KiInsertQueueApc ( PKAPC Apc, KPRIORITY Increment ); void WPOFF() { ULONG uAttr; _asm { push eax; mov eax, cr0; mov uAttr, eax; and eax, 0FFFEFFFFh; // CR0 16 BIT = 0 mov cr0, eax; pop eax; cli }; g_uCr0 = uAttr; //保存原有的 CRO 屬性 } VOID WPON() { _asm { sti push eax; mov eax, g_uCr0; //恢復原有 CR0 屬性 mov cr0, eax; pop eax; }; } // // 停止inline hook // VOID UnHookKiInsertQueueApc () { KIRQL oldIrql; WPOFF(); oldIrql = KeRaiseIrqlToDpcLevel(); RtlCopyMemory ( (BYTE*)g_KiInsertQueueApc, g_OrigCode, 5 ); KeLowerIrql(oldIrql); WPON(); g_bHooked = FALSE; } // // 開始inline hook -- KiInsertQueueApc // VOID HookKiInsertQueueApc () { KIRQL oldIrql; if (g_KiInsertQueueApc == 0) { DbgPrint("KiInsertQueueApc == NULL\n"); return; } //DbgPrint("開始inline hook -- KiInsertQueueApc\n"); DbgPrint( "KiInsertQueueApc的地址t0x%08x\n", (ULONG)g_KiInsertQueueApc ); DbgPrint( "fake_KiInsertQueueApc的地址t0x%08x\n", (ULONG)fake_KiInsertQueueApc ); // 保存原函數(shù)的前字節(jié)內(nèi)容 RtlCopyMemory (g_OrigCode, (BYTE*)g_KiInsertQueueApc, 5); //jmp指令,此處為短跳,計算相對偏移,同時,jmp xxxxxx這條指令占了5個字節(jié) *( (ULONG*)(g_HookCode + 1) ) = (ULONG)fake_KiInsertQueueApc - (ULONG)g_KiInsertQueueApc - 5; // 禁止系統(tǒng)寫保護,提升IRQL到DPC WPOFF(); oldIrql = KeRaiseIrqlToDpcLevel(); RtlCopyMemory ( (BYTE*)g_KiInsertQueueApc, g_HookCode, 5 ); *( (ULONG*)(jmp_orig_code + 1) ) = (ULONG) ( (BYTE*)g_KiInsertQueueApc + 5 ); RtlCopyMemory ( (BYTE*)Proxy_KiInsertQueueApc, g_OrigCode, 5); RtlCopyMemory ( (BYTE*)Proxy_KiInsertQueueApc + 5, jmp_orig_code, 7); // 恢復寫保護,降低IRQL KeLowerIrql(oldIrql); WPON(); g_bHooked = TRUE; } // // 跳轉(zhuǎn)到我們的函數(shù)里面進行預處理,裸函數(shù),有調(diào)用者進行堆棧的平衡 // __declspec (naked) VOID fake_KiInsertQueueApc ( PKAPC Apc, KPRIORITY Increment ) { // 去掉DbgPrint,不然這個hook會產(chǎn)生遞歸 //DbgPrint("inline hook -- KiInsertQueueApc 成功\n"); __asm { jmp Proxy_KiInsertQueueApc } } // // 代理函數(shù),負責跳轉(zhuǎn)到原函數(shù)中繼續(xù)執(zhí)行 // __declspec (naked) VOID Proxy_KiInsertQueueApc ( PKAPC Apc, KPRIORITY Increment ) { __asm { // 共字節(jié) _emit 0x90 _emit 0x90 _emit 0x90 _emit 0x90 _emit 0x90 // 前字節(jié)實現(xiàn)原函數(shù)的頭字節(jié)功能 _emit 0x90 // 這個填充jmp _emit 0x90 _emit 0x90 _emit 0x90 _emit 0x90 // 這字節(jié)保存原函數(shù)+5處的地址 _emit 0x90 _emit 0x90 // 因為是長轉(zhuǎn)移,所以必須是0x0080 } } ULONG GetFunctionAddr( IN PCWSTR FunctionName) { UNICODE_STRING UniCodeFunctionName; RtlInitUnicodeString( &UniCodeFunctionName, FunctionName ); return (ULONG)MmGetSystemRoutineAddress( &UniCodeFunctionName ); } //根據(jù)特征值,從KeInsertQueueApc搜索中搜索KiInsertQueueApc ULONG FindKiInsertQueueApcAddress() { char * Addr_KeInsertQueueApc = 0; int i = 0; char Findcode[] = { 0xE8, 0xcc, 0x29, 0x00, 0x00 }; ULONG Addr_KiInsertQueueApc = 0; Addr_KeInsertQueueApc = (char *) GetFunctionAddr(L"KeInsertQueueApc"); for(i = 0; i < 100; i ++) { if( Addr_KeInsertQueueApc[i] == Findcode[0] && Addr_KeInsertQueueApc[i + 1] == Findcode[1] && Addr_KeInsertQueueApc[i + 2] == Findcode[2] && Addr_KeInsertQueueApc[i + 3] == Findcode[3] && Addr_KeInsertQueueApc[i + 4] == Findcode[4] ) { Addr_KiInsertQueueApc = (ULONG)&Addr_KeInsertQueueApc[i] + 0x29cc + 5; break; } } return Addr_KiInsertQueueApc; } VOID OnUnload( IN PDRIVER_OBJECT DriverObject ) { DbgPrint("My Driver Unloaded!"); UnHookKiInsertQueueApc(); } NTSTATUS DriverEntry( IN PDRIVER_OBJECT theDriverObject, IN PUNICODE_STRING theRegistryPath ) { DbgPrint("My Driver Loaded!"); theDriverObject->DriverUnload = OnUnload; g_KiInsertQueueApc = FindKiInsertQueueApcAddress(); HookKiInsertQueueApc(); return STATUS_SUCCESS; }
- c++中的內(nèi)聯(lián)函數(shù)inline用法實例
- 詳解C++中的inline用法
- C/C++: Inline function, calloc 對比 malloc
- C++中inline函數(shù)詳解
- Inline Hook(ring3)的簡單C++實現(xiàn)方法
- c++內(nèi)聯(lián)函數(shù)(inline)使用詳解
- C++ 關(guān)鍵字 inline詳細介紹
- C/C++中static,const,inline三種關(guān)鍵字詳細總結(jié)
- c++中inline的用法分析
- c++ 盡量不要使用#define 而是用const、enum、inline替換。
- 如何區(qū)分C++中的inline和#define宏
相關(guān)文章
va_list(),va_start(),va_arg(),va_end() 詳細解析
這些宏定義在stdarg.h中,所以用到可變參數(shù)的程序應該包含這個頭文件.下面我們寫一個簡單的可變參數(shù)的函數(shù),該函數(shù)至少有一個整數(shù)參數(shù),第二個參數(shù)也是整數(shù),是可選的.函數(shù)只是打印這兩個參數(shù)的值2013-09-09C語言中sizeof()與strlen()函數(shù)的使用入門及對比
這篇文章主要介紹了C語言中sizeof()與strlen()函數(shù)的使用入門及對比,同時二者在C++中的使用情況也基本上同理,是需要的朋友可以參考下2015-12-12C++產(chǎn)生隨機數(shù)的實現(xiàn)代碼
本篇文章是對C++中產(chǎn)生隨機數(shù)的實現(xiàn)代碼進行了詳細的分析介紹,需要的朋友參考下2013-05-05Qt 使用Poppler實現(xiàn)pdf閱讀器的示例代碼
下面小編就為大家分享一篇Qt 使用Poppler實現(xiàn)pdf閱讀器的示例代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01