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

通过Hook SwapContext 获得目标线程的 IDT GDT KPCR (fs寄存器)

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);

  18. EXTERN_C NTKERNELAPI VOID KiDispatchInterrupt();

  19. EXTERN_C NTKERNELAPI UCHAR * PsGetProcessImageFileName(__in PEPROCESS Process);
复制代码
Driver.cpp
  1. #include "Driver.h"

  2. //global
  3. ULONG g_SwapContext_hookpointer;
  4. ULONG   g_goto_origfunc;
  5. UCHAR g_szBackupSwapContext[5];


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

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

  28. ULONG GetSwapContextAddr()
  29. {
  30.         ULONG uAddr = (ULONG)KiDispatchInterrupt;

  31.         PUCHAR p = (PUCHAR)uAddr;
  32.         ULONG i = 0;
  33.         BOOLEAN bFind = FALSE;

  34.         while(i < 0x200)
  35.         {
  36.                 if (*p == 0xe8
  37.                         && *(p + 5) == 0x8b
  38.                         && *(p + 6) == 0x2c
  39.                         && *(p - 2) == 0xb1)
  40.                 {
  41.                         bFind = TRUE;
  42.                         break;
  43.                 }
  44.                 i++;
  45.                 p++;
  46.         }

  47.         if (!bFind)
  48.         {
  49.                 KdPrint(("Find SwapContextAddr faile!\n"));
  50.                 return 0;
  51.         }

  52.         ULONG uSwapContextAddr =  (ULONG)(*(PULONG)(p + 1) + p + 5);

  53.         return uSwapContextAddr;
  54. }


  55. ULONG SearchHookPointer(ULONG StartAddress)
  56. {

  57.         PUCHAR p = (PUCHAR)StartAddress;
  58.         ULONG i = 0;
  59.         BOOLEAN bFind = FALSE;

  60.         while(i < 0x200)
  61.         {
  62.                 if (*p == 0x89
  63.                         && *(p + 1) == 0x43
  64.                         && *(p + 2) == 0x18
  65.                         && *(p + 3) == 0xfb
  66.                         && *(p + 4) == 0x8b
  67.                         && *(p - 1) == 0x20
  68.                         && *(p - 2) == 0x46)
  69.                 {
  70.                         bFind = TRUE;
  71.                         break;
  72.                 }
  73.                 i++;
  74.                 p++;
  75.         }

  76.         if (!bFind)
  77.         {
  78.                 KdPrint(("Find SearchHookPointer faile!\n"));
  79.                 return 0;
  80.         }


  81.         ULONG uFindAddr = (ULONG)p;

  82.         return uFindAddr;
  83.        
  84. }

  85. void FilterSwapContext(PETHREAD pTargetThread, PETHREAD pCurrentThread, PKPCR pkpcr)
  86. {
  87.         PEPROCESS pTargetProcess = (PEPROCESS)*(PULONG)((ULONG)pTargetThread + 0x44);
  88.         PEPROCESS pCurrentProcess = (PEPROCESS)*(PULONG)((ULONG)pCurrentThread + 0x44);
  89.         PUCHAR pszTarget =  PsGetProcessImageFileName(pTargetProcess);
  90.         PUCHAR pszCurrent =  PsGetProcessImageFileName(pCurrentProcess);

  91.         if (_stricmp((const char*)pszTarget, "calc.exe") == 0)
  92.         {
  93.                 KdPrint(("pIdtEntry:%X-----pGdt:%X\n", pkpcr->IDT, pkpcr->GDT));

  94.                 KdPrint(("pszTarget:%s--pszCurrent:%s\n",pszTarget, pszCurrent));
  95.         }

  96.        

  97. }

  98. __declspec(naked) void NewSwapContext()
  99. {
  100.         __asm
  101.         {
  102.                 pushad
  103.                 pushfd

  104.                 push ebx
  105.                 push edi
  106.                 push esi
  107.                 call FilterSwapContext

  108.                 popfd
  109.                 popad

  110.                 mov [ebx + 18h], eax
  111.                 sti
  112.                 mov eax, [edi + 44h]

  113.                 jmp g_goto_origfunc
  114.         }
  115. }

  116. void UnHookSwapContext()
  117. {
  118.         KIRQL OldIrql = KeRaiseIrqlToDpcLevel();
  119.         PageProtectOff();
  120.         RtlCopyMemory((PVOID)g_SwapContext_hookpointer, g_szBackupSwapContext, 5);
  121.         PageProtectOn();
  122.         KeLowerIrql(OldIrql);
  123. }

  124. VOID HookSwapContext()
  125. {
  126.         ULONG uSwapContextAddr = GetSwapContextAddr();

  127.         g_SwapContext_hookpointer = SearchHookPointer(uSwapContextAddr);
  128.         g_goto_origfunc = g_SwapContext_hookpointer + 7;

  129.         ULONG   utemp;
  130.         UCHAR   jmp_code[5];

  131.         jmp_code[0] = 0xE9;

  132.         utemp = (ULONG)NewSwapContext - g_SwapContext_hookpointer - 5;

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

  134.         PageProtectOff();
  135.        
  136.         RtlCopyMemory(g_szBackupSwapContext, (PVOID)g_SwapContext_hookpointer, 5);

  137.         RtlCopyMemory((PVOID)g_SwapContext_hookpointer, jmp_code, 5);
  138.        
  139.         PageProtectOn();

  140. }

  141. #pragma INITCODE
  142. NTSTATUS DriverEntry(IN PDRIVER_OBJECT DriverObject, IN PUNICODE_STRING RegistryPath)
  143. {
  144.     NTSTATUS status = STATUS_SUCCESS;

  145.         HookSwapContext();

  146.     DriverObject->DriverUnload = DriverUnload;
  147.     return status;
  148. }

  149. #pragma PAGEDCODE
  150. VOID DriverUnload(IN PDRIVER_OBJECT DriverObject)
  151. {
  152.   
  153.         UnHookSwapContext();
  154.     KdPrint(("DriverEntry unLoading...\n"));
  155.   
  156. }
复制代码

返回列表