C++實現inline hook的原理及應用實例
本文實例簡述了C++實現inline hook的原理及應用,對于大家更好的理解inline hook原理及其應用有很大的幫助。具體內容如下:
一、Inline Hook簡介:
1.INLINE HOOK原理:
Inline Hook通過硬編碼的方式向內核API的內存空間(通常是開始的一段字節(jié),且一般在第一個call之前,這么做是為了防止堆?;靵y)寫入跳轉語句,這樣,該API只要被調用,程序就會跳轉到我們的函數中來,我們在自己寫的函數里需要完成3個任務:
1)重新調整當前堆棧。程序流程在剛剛跳轉的時候,內核API并沒有執(zhí)行完,而我們的函數需要根據其結果來進行信息過濾,所以我們需要保證內核API能在順利執(zhí)行完畢后返回到我們的函數中來,這就要求對當前堆棧做一個調整。
2)執(zhí)行遺失的指令。我們向內核API地址空間些如跳轉指令(jmp xxxxxxxx)時,勢必要覆蓋原先的一些匯編指令,所以我們一定要保證這些被覆蓋的指令能夠順利執(zhí)行(否則,你的及其就要BSOD了,呵呵,Blue Screen Of Death)。關于這部分指令的執(zhí)行,一般是將其放在我們的函數中,讓我們的函數“幫助”內核API執(zhí)行完被覆蓋的指令,然后再跳回內核API中被覆蓋內后后的地址繼續(xù)執(zhí)行剩余內容。跳回去的時候,一定要算好是跳回到什么地址,是內核API起始地址后的第幾個字節(jié)。
3)信息過濾。這個就不用多說了,內核API順利執(zhí)行并返回到我們的函數中,我們自然要根據其結果做一些信息過濾,這部分內容因被hook的API以及Hook目的的不同而不同。
2.Inline hook的工作流程:
1)驗證內核API的版本(特征碼匹配)。
2)撰寫自己的函數,要完成以上三項任務。
3)獲取自己函數的地址,覆蓋內核API內存,供跳轉。
簡而言之,inlinehook的原理就是,修改函數,使其跳轉到我們指定的地方。
常見的有改函數入口,也有改函數尾,函數中間的
比如,通常函數開頭的匯編代碼都是這樣: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 }; // 原函數的前字節(jié)內容
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 );
// 保存原函數的前字節(jié)內容
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;
}
//
// 跳轉到我們的函數里面進行預處理,裸函數,有調用者進行堆棧的平衡
//
__declspec (naked)
VOID
fake_KiInsertQueueApc (
PKAPC Apc,
KPRIORITY Increment
)
{
// 去掉DbgPrint,不然這個hook會產生遞歸
//DbgPrint("inline hook -- KiInsertQueueApc 成功\n");
__asm
{
jmp Proxy_KiInsertQueueApc
}
}
//
// 代理函數,負責跳轉到原函數中繼續(xù)執(zhí)行
//
__declspec (naked)
VOID
Proxy_KiInsertQueueApc (
PKAPC Apc,
KPRIORITY Increment
)
{
__asm { // 共字節(jié)
_emit 0x90
_emit 0x90
_emit 0x90
_emit 0x90
_emit 0x90 // 前字節(jié)實現原函數的頭字節(jié)功能
_emit 0x90 // 這個填充jmp
_emit 0x90
_emit 0x90
_emit 0x90
_emit 0x90 // 這字節(jié)保存原函數+5處的地址
_emit 0x90
_emit 0x90 // 因為是長轉移,所以必須是0x0080
}
}
ULONG GetFunctionAddr( IN PCWSTR FunctionName)
{
UNICODE_STRING UniCodeFunctionName;
RtlInitUnicodeString( &UniCodeFunctionName, FunctionName );
return (ULONG)MmGetSystemRoutineAddress( &UniCodeFunctionName );
}
//根據特征值,從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;
}
相關文章
va_list(),va_start(),va_arg(),va_end() 詳細解析
這些宏定義在stdarg.h中,所以用到可變參數的程序應該包含這個頭文件.下面我們寫一個簡單的可變參數的函數,該函數至少有一個整數參數,第二個參數也是整數,是可選的.函數只是打印這兩個參數的值2013-09-09
C語言中sizeof()與strlen()函數的使用入門及對比
這篇文章主要介紹了C語言中sizeof()與strlen()函數的使用入門及對比,同時二者在C++中的使用情況也基本上同理,是需要的朋友可以參考下2015-12-12

