C++實現(xiàn)inline hook的原理及應(yīng)用實例
本文實例簡述了C++實現(xiàn)inline hook的原理及應(yīng)用,對于大家更好的理解inline hook原理及其應(yīng)用有很大的幫助。具體內(nèi)容如下:
一、Inline Hook簡介:
1.INLINE HOOK原理:
Inline Hook通過硬編碼的方式向內(nèi)核API的內(nèi)存空間(通常是開始的一段字節(jié),且一般在第一個call之前,這么做是為了防止堆棧混亂)寫入跳轉(zhuǎn)語句,這樣,該API只要被調(diào)用,程序就會跳轉(zhuǎn)到我們的函數(shù)中來,我們在自己寫的函數(shù)里需要完成3個任務(wù):
1)重新調(diào)整當(dāng)前堆棧。程序流程在剛剛跳轉(zhuǎn)的時候,內(nèi)核API并沒有執(zhí)行完,而我們的函數(shù)需要根據(jù)其結(jié)果來進行信息過濾,所以我們需要保證內(nèi)核API能在順利執(zhí)行完畢后返回到我們的函數(shù)中來,這就要求對當(dāng)前堆棧做一個調(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ù),要完成以上三項任務(wù)。
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; //恢復(fù)原有 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);
// 恢復(fù)寫保護,降低IRQL
KeLowerIrql(oldIrql);
WPON();
g_bHooked = TRUE;
}
//
// 跳轉(zhuǎn)到我們的函數(shù)里面進行預(yù)處理,裸函數(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ù),負責(zé)跳轉(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)文章
C++ virtual destructor虛擬析構(gòu)函數(shù)
C++中基類采用virtual虛析構(gòu)函數(shù)是為了防止內(nèi)存泄漏。具體地說,如果派生類中申請了內(nèi)存空間,并在其析構(gòu)函數(shù)中對這些內(nèi)存空間進行釋放,今天通過本文給大家介紹C++ virtual destructor虛擬析構(gòu)函數(shù)的相關(guān)知識,感興趣的朋友一起看看吧2021-05-05
C++中based for循環(huán)的實現(xiàn)
C++中的范圍for循環(huán)是一種簡潔的遍歷容器的方法,本文主要介紹了C++中based for循環(huán)的實現(xiàn),具有一定的參考價值,感興趣的可以了解一下2025-02-02
C語言中指針 int *p=0;和int *p;*p=0;和”&“的關(guān)系和區(qū)別詳解
這篇文章主要介紹了C語言中指針 int *p=0;和int *p;*p=0;和”&“有什么關(guān)系和區(qū)別,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2020-02-02

