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

hook KiFastCallEntry实现程序对系统服务调用的拦截

  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. typedef NTSTATUS  (*pNtCreateFile)(
  40.     OUT PHANDLE  FileHandle,
  41.     IN ACCESS_MASK  DesiredAccess,
  42.     IN POBJECT_ATTRIBUTES  ObjectAttributes,
  43.     OUT PIO_STATUS_BLOCK  IoStatusBlock,
  44.     IN PLARGE_INTEGER  AllocationSize,
  45.     IN ULONG  FileAttributes,
  46.     IN ULONG  ShareAccess,
  47.     IN ULONG  CreateDisposition,
  48.     IN ULONG  CreateOptions,
  49.     IN PVOID  EaBuffer,
  50.     IN ULONG  EaLength
  51.     );


  52. //global
  53. ULONG g_ntCreateFile;
  54. ULONG g_kifastcall_hookpoint;
  55. ULONG g_goto_OrigFunc;

  56. #pragma PAGEDCODE
  57. ULONG SearchHookPointer(ULONG StartAddress)
  58. {
  59.         ULONG u_index;
  60.         UCHAR *p = (UCHAR*)StartAddress;

  61.         for (u_index = 0; u_index < 200; u_index++)
  62.         {
  63.                 if (*p == 0x2b &&
  64.                         *(p+1) == 0xe1 &&
  65.                         *(p+2) == 0xc1 &&
  66.                         *(p+3) == 0xe9 &&
  67.                         *(p+4) == 0x02)
  68.                 {
  69.                         return (ULONG)p;
  70.                 }
  71.                 p--;
  72.         }

  73.         return 0;
  74. }

  75. #pragma PAGEDCODE
  76. void __stdcall FilterKiFastCallEntry(ULONG ServiceTableBase, ULONG FuncIndex)
  77. {
  78.         if (ServiceTableBase == (ULONG)KeServiceDescriptorTable.ServiceTableBase)
  79.         {
  80.                 if (FuncIndex == 122)
  81.                 {
  82.                          KdPrint(("%s\n", (char*)PsGetCurrentProcess() + 0x174));
  83.                 }
  84.         }
  85. }

  86. __declspec(naked) void NewKiFastCallEntry()
  87. {
  88.         __asm
  89.         {
  90.                 pushad
  91.                 pushfd
  92.        
  93.                 push eax
  94.                 push edi
  95.                 call FilterKiFastCallEntry

  96.                 popfd
  97.                 popad

  98.                 sub esp,ecx
  99.                 shr ecx,2
  100.                 jmp g_goto_OrigFunc
  101.         }
  102. }

  103. void UnHookKiFastCallEntry()
  104. {
  105.         UCHAR str_origfuncode[5] = {0x2b, 0xe1, 0xc1, 0xe9, 0x02};
  106.         if (g_kifastcall_hookpoint == 0)
  107.         {
  108.                 return;
  109.         }

  110.         PageProtectOff();
  111.         RtlCopyMemory((PVOID)g_kifastcall_hookpoint, str_origfuncode, 5);
  112.         PageProtectOn();
  113. }

  114. void HookKiFastCallEntry(ULONG HookPointer)
  115. {
  116.         ULONG u_temp;
  117.         UCHAR u_jmp_code[5];

  118.         u_jmp_code[0] = 0xe9;
  119.        
  120.         u_temp = (ULONG)NewKiFastCallEntry - HookPointer - 5;
  121.         *(ULONG*)&u_jmp_code[1] = u_temp;

  122.         PageProtectOff();
  123.        
  124.         RtlCopyMemory((PVOID)HookPointer, u_jmp_code, 5);

  125.         PageProtectOn();

  126. }

  127. NTSTATUS  NewNtCreateFile(
  128.     OUT PHANDLE  FileHandle,
  129.     IN ACCESS_MASK  DesiredAccess,
  130.     IN POBJECT_ATTRIBUTES  ObjectAttributes,
  131.     OUT PIO_STATUS_BLOCK  IoStatusBlock,
  132.     IN PLARGE_INTEGER  AllocationSize,
  133.     IN ULONG  FileAttributes,
  134.     IN ULONG  ShareAccess,
  135.     IN ULONG  CreateDisposition,
  136.     IN ULONG  CreateOptions,
  137.     IN PVOID  EaBuffer,
  138.     IN ULONG  EaLength
  139.     )
  140. {
  141.         ULONG u_call_retAddr;
  142.         __asm
  143.         {
  144.                 pushad
  145.                 mov eax, [ebp + 0x4]
  146.                 mov u_call_retAddr, eax
  147.                 popad
  148.         }

  149.         KdPrint(("u_call_retAddr: %x\n", u_call_retAddr));
  150.         g_kifastcall_hookpoint = SearchHookPointer(u_call_retAddr);
  151.        
  152.         if (g_kifastcall_hookpoint == 0)
  153.         {
  154.                 KdPrint(("search failed!\n"));
  155.         }
  156.         else
  157.         {
  158.                 KdPrint(("search success %x\n", g_kifastcall_hookpoint));
  159.         }

  160.         g_goto_OrigFunc = g_kifastcall_hookpoint + 5;
  161.         HookKiFastCallEntry(g_kifastcall_hookpoint);

  162.         PageProtectOff();
  163.        
  164.         KeServiceDescriptorTable.ServiceTableBase[37] = (unsigned int)g_ntCreateFile;
  165.        
  166.         PageProtectOn();

  167.         return ((pNtCreateFile)g_ntCreateFile)(
  168.                 FileHandle,
  169.                 DesiredAccess,
  170.                 ObjectAttributes,
  171.                 IoStatusBlock,
  172.                 AllocationSize,
  173.                 FileAttributes,
  174.                 ShareAccess,
  175.                 CreateDisposition,
  176.                 CreateOptions,
  177.                 EaBuffer,
  178.                 EaLength);
  179. }




  180. #pragma PAGEDCODE
  181. void SearchKiFastCallEntry()
  182. {
  183.        
  184.         PageProtectOff();
  185.         g_ntCreateFile = KeServiceDescriptorTable.ServiceTableBase[37];
  186.         KeServiceDescriptorTable.ServiceTableBase[37] = (unsigned int)NewNtCreateFile;
  187.         PageProtectOn();
  188. }


  189. #pragma PAGEDCODE
  190. VOID MyDriverUnload(IN PDRIVER_OBJECT pDriverObject)
  191. {

  192.         UnHookKiFastCallEntry();
  193.         KdPrint(("DriverEntry unLoading...\n"));

  194. }



  195. #pragma INITCODE
  196. NTSTATUS DriverEntry(IN PDRIVER_OBJECT pDriverObject, IN PUNICODE_STRING RegistryPath)
  197. {
  198.         NTSTATUS status = STATUS_SUCCESS;

  199.         SearchKiFastCallEntry();

  200.         pDriverObject->DriverUnload = MyDriverUnload;
  201.         return status;
  202. }
复制代码

返回列表