免費論壇 繁體 | 簡體
Sclub交友聊天~加入聊天室當版主
分享
返回列表 发帖

通过hook KiFastCallEntry来过滤SSDT 或者 Shadow SSDT

Driver.h
  1. #ifdef __cplusplus
  2. extern "C"
  3. {
  4. #endif
  5. #include <ntddk.h>
  6. NTSTATUS DriverEntry(IN PDRIVER_OBJECT DriverObject, IN PUNICODE_STRING RegistryPath);

  7. #ifdef __cplusplus
  8. }
  9. #endif

  10. #define PAGEDCODE code_seg("PAGE")
  11. #define LOCKEDCODE code_seg()
  12. #define INITCODE code_seg("INIT")

  13. #define PAGEDDATA data_seg("PAGE")
  14. #define LOCKEDDATA data_seg()
  15. #define INITDATA data_seg("INIT")

  16. #define arraysize(p) (sizeof(p)/sizeof((p)[0]))

  17. VOID DriverUnload(IN PDRIVER_OBJECT DriverObject);
复制代码
Driver.cpp
  1. #include "Driver.h"

  2. ULONG g_KiFastCallEntry_hookpointer;
  3. ULONG g_goto_origfunc;
  4. UCHAR g_szBackupKiFastCallEntry[5];


  5. void PageProtectOn()
  6. {
  7.         //恢复内存保护
  8.         __asm
  9.         {
  10.                 mov eax, cr0
  11.                         or eax, 10000h
  12.                         mov cr0, eax
  13.                         sti
  14.         }
  15. }

  16. void PageProtectOff()
  17. {
  18.         //去掉内存保护
  19.         __asm
  20.         {
  21.                 cli
  22.                         mov eax, cr0
  23.                         and eax, not 10000h
  24.                         mov cr0, eax
  25.         }
  26. }

  27. ULONG GetKiFastCallEntryAddr()
  28. {
  29.         ULONG uKiFastCallEntryAddr = 0;

  30.         __asm
  31.         {
  32.                 mov ecx, 0x176
  33.                 rdmsr
  34.                 mov uKiFastCallEntryAddr, eax
  35.         }

  36.         if (uKiFastCallEntryAddr == 0)
  37.         {
  38.                 return 0;
  39.         }

  40.         return uKiFastCallEntryAddr;

  41. }


  42. ULONG SearchHookPointer(ULONG StartAddress)
  43. {

  44.         PUCHAR p = (PUCHAR)StartAddress;
  45.         ULONG i = 0;
  46.         BOOLEAN bFind = FALSE;

  47.         while(i < 0x200)
  48.         {
  49.                 if (*p == 0x2b
  50.                         && *(p + 1) == 0xe1
  51.                         && *(p + 2) == 0xc1
  52.                         && *(p + 3) == 0xe9
  53.                         && *(p + 4) == 0x02
  54.                         && *(p - 1) == 0x87
  55.                         && *(p - 2) == 0x1c)
  56.                 {
  57.                         bFind = TRUE;
  58.                         break;
  59.                 }
  60.                 i++;
  61.                 p++;
  62.         }

  63.         if (!bFind)
  64.         {
  65.                 KdPrint(("Find SearchHookPointer faile!\n"));
  66.                 return 0;
  67.         }


  68.         ULONG uFindAddr = (ULONG)p;

  69.         return uFindAddr;

  70. }

  71. ULONG FilterKiFastCallEntry(ULONG uServiceTableBaseAddr, ULONG uIndex, ULONG uOrigFuncAddr)
  72. {
  73.        

  74.         return uOrigFuncAddr;
  75. }

  76. __declspec(naked) void NewKiFastCallEntry()
  77. {
  78.         __asm
  79.         {
  80.                 pushad
  81.                 pushfd

  82.                 push ebx
  83.                 push eax
  84.                 push edi
  85.                 call FilterKiFastCallEntry

  86.                 mov [esp + 0x14], eax

  87.                 popfd
  88.                 popad

  89.                 sub     esp,ecx
  90.                 shr     ecx,2

  91.                 jmp g_goto_origfunc
  92.         }
  93. }

  94. void UnHookKiFastCallEntry()
  95. {
  96.         PageProtectOff();
  97.         RtlCopyMemory((PVOID)g_KiFastCallEntry_hookpointer, g_szBackupKiFastCallEntry, 5);
  98.         PageProtectOn();
  99. }

  100. VOID HookKiFastCallEntry()
  101. {
  102.         ULONG uKiFastCallEntryAddr = GetKiFastCallEntryAddr();

  103.         g_KiFastCallEntry_hookpointer = SearchHookPointer(uKiFastCallEntryAddr);

  104.         g_goto_origfunc = g_KiFastCallEntry_hookpointer + 5;

  105.         ULONG   utemp;
  106.         UCHAR   jmp_code[5];

  107.         jmp_code[0] = 0xE9;

  108.         utemp = (ULONG)NewKiFastCallEntry - g_KiFastCallEntry_hookpointer - 5;

  109.         *(ULONG*)&jmp_code[1] = utemp;

  110.         PageProtectOff();

  111.         RtlCopyMemory(g_szBackupKiFastCallEntry, (PVOID)g_KiFastCallEntry_hookpointer, 5);

  112.         RtlCopyMemory((PVOID)g_KiFastCallEntry_hookpointer, jmp_code, 5);

  113.         PageProtectOn();

  114. }

  115. #pragma INITCODE
  116. NTSTATUS DriverEntry(IN PDRIVER_OBJECT DriverObject, IN PUNICODE_STRING RegistryPath)
  117. {
  118.     NTSTATUS status = STATUS_SUCCESS;

  119.         HookKiFastCallEntry();

  120.     DriverObject->DriverUnload = DriverUnload;
  121.     return status;
  122. }

  123. #pragma PAGEDCODE
  124. VOID DriverUnload(IN PDRIVER_OBJECT DriverObject)
  125. {
  126.   
  127.         UnHookKiFastCallEntry();
  128.     KdPrint(("DriverEntry unLoading...\n"));
  129.   
  130. }
复制代码

返回列表