欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來(lái)到入門(mén)教程網(wǎng)!

C語(yǔ)言

當(dāng)前位置:主頁(yè) > 軟件編程 > C語(yǔ)言 >

C++實(shí)現(xiàn)inline hook的原理及應(yīng)用實(shí)例

來(lái)源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:C語(yǔ)言|點(diǎn)擊: 次

本文實(shí)例簡(jiǎn)述了C++實(shí)現(xiàn)inline hook的原理及應(yīng)用,對(duì)于大家更好的理解inline hook原理及其應(yīng)用有很大的幫助。具體內(nèi)容如下:

一、Inline Hook簡(jiǎn)介:

1.INLINE HOOK原理:

Inline Hook通過(guò)硬編碼的方式向內(nèi)核API的內(nèi)存空間(通常是開(kāi)始的一段字節(jié),且一般在第一個(gè)call之前,這么做是為了防止堆?;靵y)寫(xiě)入跳轉(zhuǎn)語(yǔ)句,這樣,該API只要被調(diào)用,程序就會(huì)跳轉(zhuǎn)到我們的函數(shù)中來(lái),我們?cè)谧约簩?xiě)的函數(shù)里需要完成3個(gè)任務(wù):

1)重新調(diào)整當(dāng)前堆棧。程序流程在剛剛跳轉(zhuǎn)的時(shí)候,內(nèi)核API并沒(méi)有執(zhí)行完,而我們的函數(shù)需要根據(jù)其結(jié)果來(lái)進(jìn)行信息過(guò)濾,所以我們需要保證內(nèi)核API能在順利執(zhí)行完畢后返回到我們的函數(shù)中來(lái),這就要求對(duì)當(dāng)前堆棧做一個(gè)調(diào)整。

2)執(zhí)行遺失的指令。我們向內(nèi)核API地址空間些如跳轉(zhuǎn)指令(jmp xxxxxxxx)時(shí),勢(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)信息過(guò)濾。這個(gè)就不用多說(shuō)了,內(nèi)核API順利執(zhí)行并返回到我們的函數(shù)中,我們自然要根據(jù)其結(jié)果做一些信息過(guò)濾,這部分內(nèi)容因被hook的API以及Hook目的的不同而不同。

2.Inline hook的工作流程:

1)驗(yàn)證內(nèi)核API的版本(特征碼匹配)。

2)撰寫(xiě)自己的函數(shù),要完成以上三項(xiàng)任務(wù)。

3)獲取自己函數(shù)的地址,覆蓋內(nèi)核API內(nèi)存,供跳轉(zhuǎn)。

簡(jiǎn)而言之,inlinehook的原理就是,修改函數(shù),使其跳轉(zhuǎn)到我們指定的地方。

常見(jiàn)的有改函數(shù)入口,也有改函數(shù)尾,函數(shù)中間的
比如,通常函數(shù)開(kāi)頭的匯編代碼都是這樣:mov edi,edi;push esp;mov ebp,esp,而我們便可以通過(guò)修改這里進(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;
}
//
// 開(kāi)始inline hook -- KiInsertQueueApc
//
VOID HookKiInsertQueueApc ()
{
  KIRQL oldIrql;
  if (g_KiInsertQueueApc == 0) {
    DbgPrint("KiInsertQueueApc == NULL\n");
    return;
  }
  //DbgPrint("開(kāi)始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)寫(xiě)保護(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ù)寫(xiě)保護(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(zhǎng)轉(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;
}

網(wǎng)頁(yè)制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語(yǔ)言數(shù)據(jù)庫(kù)服務(wù)器

如果侵犯了您的權(quán)利,請(qǐng)與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有