C++實(shí)現(xiàn)inline hook的原理及應(yīng)用實(shí)例
本文實(shí)例簡述了C++實(shí)現(xiàn)inline hook的原理及應(yīng)用,對(duì)于大家更好的理解inline hook原理及其應(yīng)用有很大的幫助。具體內(nèi)容如下:
一、Inline Hook簡介:
1.INLINE HOOK原理:
Inline Hook通過硬編碼的方式向內(nèi)核API的內(nèi)存空間(通常是開始的一段字節(jié),且一般在第一個(gè)call之前,這么做是為了防止堆棧混亂)寫入跳轉(zhuǎn)語句,這樣,該API只要被調(diào)用,程序就會(huì)跳轉(zhuǎn)到我們的函數(shù)中來,我們在自己寫的函數(shù)里需要完成3個(gè)任務(wù):
1)重新調(diào)整當(dāng)前堆棧。程序流程在剛剛跳轉(zhuǎn)的時(shí)候,內(nèi)核API并沒有執(zhí)行完,而我們的函數(shù)需要根據(jù)其結(jié)果來進(jìn)行信息過濾,所以我們需要保證內(nèi)核API能在順利執(zhí)行完畢后返回到我們的函數(shù)中來,這就要求對(duì)當(dāng)前堆棧做一個(gè)調(diào)整。
2)執(zhí)行遺失的指令。我們向內(nèi)核API地址空間些如跳轉(zhuǎn)指令(jmp xxxxxxxx)時(shí),勢必要覆蓋原先的一些匯編指令,所以我們一定要保證這些被覆蓋的指令能夠順利執(zhí)行(否則,你的及其就要BSOD了,呵呵,Blue Screen Of Death)。關(guān)于這部分指令的執(zhí)行,一般是將其放在我們的函數(shù)中,讓我們的函數(shù)“幫助”內(nèi)核API執(zhí)行完被覆蓋的指令,然后再跳回內(nèi)核API中被覆蓋內(nèi)后后的地址繼續(xù)執(zhí)行剩余內(nèi)容。跳回去的時(shí)候,一定要算好是跳回到什么地址,是內(nèi)核API起始地址后的第幾個(gè)字節(jié)。
3)信息過濾。這個(gè)就不用多說了,內(nèi)核API順利執(zhí)行并返回到我們的函數(shù)中,我們自然要根據(jù)其結(jié)果做一些信息過濾,這部分內(nèi)容因被hook的API以及Hook目的的不同而不同。
2.Inline hook的工作流程:
1)驗(yàn)證內(nèi)核API的版本(特征碼匹配)。
2)撰寫自己的函數(shù),要完成以上三項(xiàng)任務(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,而我們便可以通過修改這里進(jìn)行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指令,此處為短跳,計(jì)算相對(duì)偏移,同時(shí),jmp xxxxxx這條指令占了5個(gè)字節(jié)
*( (ULONG*)(g_HookCode + 1) ) = (ULONG)fake_KiInsertQueueApc - (ULONG)g_KiInsertQueueApc - 5;
// 禁止系統(tǒng)寫保護(hù),提升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ù)寫保護(hù),降低IRQL
KeLowerIrql(oldIrql);
WPON();
g_bHooked = TRUE;
}
//
// 跳轉(zhuǎn)到我們的函數(shù)里面進(jìn)行預(yù)處理,裸函數(shù),有調(diào)用者進(jìn)行堆棧的平衡
//
__declspec (naked)
VOID
fake_KiInsertQueueApc (
PKAPC Apc,
KPRIORITY Increment
)
{
// 去掉DbgPrint,不然這個(gè)hook會(huì)產(chǎn)生遞歸
//DbgPrint("inline hook -- KiInsertQueueApc 成功\n");
__asm
{
jmp Proxy_KiInsertQueueApc
}
}
//
// 代理函數(shù),負(fù)責(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é)實(shí)現(xiàn)原函數(shù)的頭字節(jié)功能
_emit 0x90 // 這個(gè)填充jmp
_emit 0x90
_emit 0x90
_emit 0x90
_emit 0x90 // 這字節(jié)保存原函數(shù)+5處的地址
_emit 0x90
_emit 0x90 // 因?yàn)槭情L轉(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;
}
上一篇:Inline Hook(ring3)的簡單C++實(shí)現(xiàn)方法
欄 目:C語言
下一篇:基于VC實(shí)現(xiàn)的網(wǎng)絡(luò)監(jiān)聽功能程序?qū)嵗?/a>
本文標(biāo)題:C++實(shí)現(xiàn)inline hook的原理及應(yīng)用實(shí)例
本文地址:http://www.jygsgssxh.com/a1/Cyuyan/3526.html
您可能感興趣的文章
- 04-02c語言沒有round函數(shù) round c語言
- 01-10數(shù)據(jù)結(jié)構(gòu)課程設(shè)計(jì)-用棧實(shí)現(xiàn)表達(dá)式求值的方法詳解
- 01-10使用OpenGL實(shí)現(xiàn)3D立體顯示的程序代碼
- 01-10深入理解C++中常見的關(guān)鍵字含義
- 01-10求斐波那契(Fibonacci)數(shù)列通項(xiàng)的七種實(shí)現(xiàn)方法
- 01-10C語言 解決不用+、-、&#215;、&#247;數(shù)字運(yùn)算符做加法
- 01-10使用C++實(shí)現(xiàn)全排列算法的方法詳解
- 01-10c++中inline的用法分析
- 01-10用C++實(shí)現(xiàn)DBSCAN聚類算法
- 01-10深入全排列算法及其實(shí)現(xiàn)方法


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對(duì)數(shù)函數(shù)的表達(dá)式 c語言中對(duì)
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機(jī)閱讀
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 01-10C#中split用法實(shí)例總結(jié)
- 01-10delphi制作wav文件的方法
- 08-05織夢dedecms什么時(shí)候用欄目交叉功能?
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 04-02jquery與jsp,用jquery


