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

hook IDT

  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. #define WORD USHORT
  9. #define DWORD ULONG
  10. #define MAKELONG(a, b) ((LONG)(((WORD)(((DWORD_PTR)(a)) & 0xffff)) | ((DWORD)((WORD)(((DWORD_PTR)(b)) & 0xffff))) << 16))

  11. #pragma pack(push)
  12. #pragma pack(1) // 1字节对齐
  13. typedef struct _IDTR //IDT基址
  14. {
  15.         USHORT IDT_limit; //范围占位
  16.         USHORT IDT_LOWbase;//基地址占位_IDT_ENTRY类型指针
  17.         USHORT IDT_HIGbase;
  18. }IDTR,*PIDTR;

  19. typedef struct _IDT_ENTRY
  20. {
  21.         USHORT LowOffset; //中断处理函数地址低位
  22.         USHORT selector;
  23.         UCHAR  reserved;
  24.         UCHAR  type:4;         //4位
  25.         UCHAR  always0:1;        //1位
  26.         UCHAR  dpl:2;                //2位
  27.         UCHAR  present:1;        //1位
  28.         USHORT HigOffset;//中断处理函数地址高位
  29. }IDTENTRY,*PIDTENTRY;//获取基址实际上是这个类型
  30. #pragma pack(pop)


  31. typedef KAFFINITY (*KESETAFFINITYTHREAD)(
  32.     __inout PKTHREAD Thread,
  33.     __in KAFFINITY Affinity
  34.     );

  35. //Global
  36. ULONG g_InterruptFunc3;


  37. void PageProtectOn()
  38. {
  39.         //恢复内存保护
  40.         __asm
  41.         {
  42.                 mov eax, cr0
  43.                         or eax, 10000h
  44.                         mov cr0, eax
  45.                         sti
  46.         }
  47. }

  48. void PageProtectOff()
  49. {
  50.         //去掉内存保护
  51.         __asm
  52.         {
  53.                 cli
  54.                         mov eax, cr0
  55.                         and eax, not 10000h
  56.                         mov cr0, eax
  57.         }
  58. }


  59. #pragma PAGEDCODE
  60. VOID GetIdt()
  61. {
  62.         IDTR idtr;

  63.         ULONG uIndex;
  64.         PIDTENTRY pIdt_Entry;

  65.         __asm
  66.         {
  67.                 SIDT idtr;
  68.         }

  69.         KdPrint(("%d\n", idtr.IDT_limit));

  70.         pIdt_Entry = (PIDTENTRY)MAKELONG(idtr.IDT_LOWbase, idtr.IDT_HIGbase);
  71.         KdPrint(("%X\n", pIdt_Entry));

  72.         for (uIndex = 0; uIndex <= idtr.IDT_limit / sizeof(IDTENTRY); uIndex++)
  73.         {
  74.                 KdPrint(("pIdt_Entry[%d]:%X", uIndex, MAKELONG(pIdt_Entry[uIndex].LowOffset, pIdt_Entry[uIndex].HigOffset)));
  75.         }
  76. }

  77. #pragma PAGEDCODE
  78. ULONG GetInterruptFuncAddress(ULONG InterruptIndex)
  79. {
  80.         IDTR idtr;
  81.         PIDTENTRY pIdtEntry;

  82.         __asm SIDT idtr;

  83.         pIdtEntry = (PIDTENTRY)MAKELONG(idtr.IDT_LOWbase, idtr.IDT_HIGbase);

  84.         return MAKELONG(pIdtEntry[InterruptIndex].LowOffset, pIdtEntry[InterruptIndex].HigOffset);
  85. }

  86. VOID __stdcall FilterInterruptFunc3()
  87. {
  88.         KdPrint(("CurrentProcess:%s\n", (char*)PsGetCurrentProcess() + 0x174));
  89. }

  90. __declspec(naked) void NewInterruptFun3()
  91. {
  92.         __asm
  93.         {
  94.                 pushad
  95.                 pushfd
  96.                 push fs

  97.                 push 0x30
  98.                 pop fs
  99.                 mov ax, 0x23
  100.                 mov ds, ax
  101.                 mov es, ax
  102.                 call FilterInterruptFunc3

  103.                 pop fs
  104.                 popfd
  105.                 popad

  106.                 jmp g_InterruptFunc3
  107.         }
  108. }

  109. #pragma PAGEDCODE
  110. void HookInterrupt(ULONG InterruptIndex, ULONG NewInterruptFunc)
  111. {
  112.         ULONG Index, Affinity, CurrentAffinity;
  113.         ULONG fnKeSetAffinityThread;
  114.         ULONG * pKiProcessorBlock;
  115.         UNICODE_STRING usFuncName;
  116.         ULONG kPrcb;

  117.         PIDTENTRY pIdtEntry;


  118.         RtlInitUnicodeString(&usFuncName,L"KeSetAffinityThread");

  119.         fnKeSetAffinityThread = (ULONG)MmGetSystemRoutineAddress(&usFuncName);

  120.         if (!MmIsAddressValid((PVOID)fnKeSetAffinityThread))
  121.         {
  122.                 return;
  123.         }


  124.         Affinity = KeQueryActiveProcessors();

  125.         CurrentAffinity = 1;
  126.         Index = 0;
  127.         while(Affinity)
  128.         {
  129.                 Affinity &= ~CurrentAffinity;
  130.                 ((KESETAFFINITYTHREAD)fnKeSetAffinityThread)((PKTHREAD)PsGetCurrentThread(),(KAFFINITY)CurrentAffinity);
  131.                 CurrentAffinity <<= 1;

  132.                 __asm
  133.                 {
  134.                         push eax
  135.                         mov         eax,fs:[0x20]
  136.                         mov         kPrcb,eax
  137.                         pop         eax
  138.                 }

  139.                 pKiProcessorBlock = &kPrcb;
  140.                 pIdtEntry = *(PIDTENTRY*)(pKiProcessorBlock[Index] - 0xe8);
  141.                 KdPrint(("pIdtEntry:%X\n", pIdtEntry));

  142.                 PageProtectOff();
  143.        
  144.                 pIdtEntry[InterruptIndex].LowOffset =(USHORT)((ULONG)NewInterruptFunc & 0xffff);
  145.                 pIdtEntry[InterruptIndex].HigOffset = (USHORT)((ULONG)NewInterruptFunc >> 16);

  146.                 PageProtectOn();

  147.                 Index++;
  148.         }

  149.         KdPrint(("Index:%d\n", Index));
  150.         return;
  151. }



  152. #pragma PAGEDCODE
  153. VOID MyDriverUnload(IN PDRIVER_OBJECT pDriverObject)
  154. {
  155.         HookInterrupt(3, g_InterruptFunc3);

  156.         KdPrint(("DriverEntry unLoading...\n"));

  157. }



  158. #pragma INITCODE
  159. NTSTATUS DriverEntry(IN PDRIVER_OBJECT pDriverObject, IN PUNICODE_STRING RegistryPath)
  160. {
  161.         NTSTATUS status = STATUS_SUCCESS;
  162.        
  163.         g_InterruptFunc3 = GetInterruptFuncAddress(3);

  164.         HookInterrupt(3, (ULONG)NewInterruptFun3);
  165.        
  166.         pDriverObject->DriverUnload = MyDriverUnload;
  167.         return status;
  168. }
复制代码

返回列表