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

对内核代码任意位置hook编写 (内联HOOK inline hook)

  1. #include "ntddk.h"

  2. #define PAGEDCODE code_seg("PAGE")
  3. #define LOCKEDCODE code_seg()
  4. #define INITCODE code_seg("INIT")

  5. #define PAGEDDATA data_seg("PAGE")
  6. #define LOCKEDDATA data_seg()
  7. #define INITDATA data_seg("INIT")

  8. #pragma pack(1)
  9. typedef struct ServiceDescriptorEntry {
  10.         unsigned int *ServiceTableBase;
  11.         unsigned int *ServiceCounterTableBase; //仅适用于checked build版本
  12.         unsigned int NumberOfServices;
  13.         unsigned char *ParamTableBase;
  14. } ServiceDescriptorTableEntry_t, *PServiceDescriptorTableEntry_t;
  15. #pragma pack()

  16. __declspec (dllimport) ServiceDescriptorTableEntry_t KeServiceDescriptorTable;

  17. void PageProtectOn()
  18. {
  19.         //恢复内存保护
  20.         __asm
  21.         {
  22.                 mov eax, cr0
  23.                 or eax, 10000h
  24.                 mov cr0, eax
  25.                 sti
  26.         }
  27. }

  28. void PageProtectOff()
  29. {
  30.         //去掉内存保护
  31.         __asm
  32.         {
  33.                 cli
  34.                 mov eax, cr0
  35.                 and eax, not 10000h
  36.                 mov cr0, eax
  37.         }
  38. }

  39. ULONG g_ntOpenKey;
  40. ULONG g_jmp_origNtOpenKey;
  41. UCHAR g_orig_funcode[5];

  42. void FilterNtOpenKey()
  43. {
  44.         KdPrint(("%s\n", (char*)PsGetCurrentProcess() + 0x174));
  45. }

  46. __declspec(naked) void NewOpenKey()
  47. {
  48.         __asm
  49.         {
  50.                 pushad
  51.                 call FilterNtOpenKey
  52.                 popad
  53.                 pop eax
  54.                 push 0x94
  55.                 jmp g_jmp_origNtOpenKey
  56.         }
  57. }



  58. #pragma PAGEDCODE
  59. void HookNtOpenKey()
  60. {
  61.         ULONG u_jmp_temp;
  62.         UCHAR jmp_code[5];

  63.         g_ntOpenKey = KeServiceDescriptorTable.ServiceTableBase[119];
  64.         g_jmp_origNtOpenKey = g_ntOpenKey + 5;

  65.         u_jmp_temp = (ULONG)NewOpenKey - g_ntOpenKey - 5;

  66.         jmp_code[0] = 0xe8;
  67.         *(ULONG *)&jmp_code[1] = u_jmp_temp;

  68.         PageProtectOff();

  69.         RtlCopyMemory(g_orig_funcode, (PVOID)g_ntOpenKey, 5);
  70.         RtlCopyMemory((PVOID)g_ntOpenKey, jmp_code, 5);

  71.         PageProtectOn();
  72. }

  73. #pragma PAGEDCODE
  74. void UnHookOpenKey()
  75. {
  76.         PageProtectOff();

  77.         RtlCopyMemory((PVOID)g_ntOpenKey, g_orig_funcode, 5);

  78.         PageProtectOn();
  79. }

  80. #pragma PAGEDCODE
  81. VOID MyDriverUnload(IN PDRIVER_OBJECT pDriverObject)
  82. {

  83.         UnHookOpenKey();
  84.         KdPrint(("DriverEntry unLoading...\n"));

  85. }


  86. #pragma INITCODE
  87. NTSTATUS DriverEntry(IN PDRIVER_OBJECT pDriverObject, IN PUNICODE_STRING RegistryPath)
  88. {
  89.         NTSTATUS status = STATUS_SUCCESS;

  90.         HookNtOpenKey();
  91.         pDriverObject->DriverUnload = MyDriverUnload;
  92.         return status;
  93. }
复制代码

返回列表