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

隐藏 IDT 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. #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. typedef struct _KGDTENTRY
  31. {
  32.         USHORT  LimitLow;
  33.         USHORT  BaseLow;
  34.         union {
  35.                 struct {
  36.                         UCHAR   BaseMid;
  37.                         UCHAR   Flags1;     // Declare as bytes to avoid alignment
  38.                         UCHAR   Flags2;     // Problems.
  39.                         UCHAR   BaseHi;
  40.                 } Bytes;
  41.                 struct {
  42.                         ULONG   BaseMid : 8;
  43.                         ULONG   Type : 5;
  44.                         ULONG   Dpl : 2;
  45.                         ULONG   Pres : 1;
  46.                         ULONG   LimitHi : 4;
  47.                         ULONG   Sys : 1;
  48.                         ULONG   Reserved_0 : 1;
  49.                         ULONG   Default_Big : 1;
  50.                         ULONG   Granularity : 1;
  51.                         ULONG   BaseHi : 8;
  52.                 } Bits;
  53.         } HighWord;
  54. } KGDTENTRY, *PKGDTENTRY;
  55. #pragma pack(pop)


  56. typedef KAFFINITY (*KESETAFFINITYTHREAD)(
  57.         __inout PKTHREAD Thread,
  58.         __in KAFFINITY Affinity
  59.         );

  60. //global
  61. USHORT g_FilterJmp[3];
  62. ULONG g_OrigInterruptFunc3;


  63. void PageProtectOn()
  64. {
  65.         //恢复内存保护
  66.         __asm
  67.         {
  68.                 mov eax, cr0
  69.                         or eax, 10000h
  70.                         mov cr0, eax
  71.                         sti
  72.         }
  73. }

  74. void PageProtectOff()
  75. {
  76.         //去掉内存保护
  77.         __asm
  78.         {
  79.                 cli
  80.                         mov eax, cr0
  81.                         and eax, not 10000h
  82.                         mov cr0, eax
  83.         }
  84. }

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

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

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

  102.                 pop fs
  103.                 popfd
  104.                 popad

  105.                 jmp g_OrigInterruptFunc3
  106.         }
  107. }

  108. __declspec(naked) void NewInterrupt3()
  109. {
  110.         __asm
  111.         {
  112.                 jmp fword ptr [g_FilterJmp]
  113.         }
  114. }


  115. #pragma PAGEDCODE
  116. ULONG GetInterruptFuncAddress(ULONG InterruptIndex)
  117. {
  118.         IDTR idtr;
  119.         PIDTENTRY pIdtEntry;

  120.         __asm SIDT idtr;

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

  122.         return MAKELONG(pIdtEntry[InterruptIndex].LowOffset, pIdtEntry[InterruptIndex].HigOffset);
  123. }


  124. #pragma PAGEDCODE
  125. void SetInterrupt(ULONG InterruptIndex, ULONG uNewBase, BOOLEAN bIsNew)
  126. {
  127.         ULONG Index, Affinity, CurrentAffinity;
  128.         ULONG fnKeSetAffinityThread;
  129.         ULONG *pKiProcessorBlock;
  130.         UNICODE_STRING usFuncName;
  131.         ULONG kPrcb;

  132.         PIDTENTRY pIdtEntry;
  133.         PKGDTENTRY pGdt;


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

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

  136.         if (!MmIsAddressValid((PVOID)fnKeSetAffinityThread))
  137.         {
  138.                 return;
  139.         }


  140.         Affinity = KeQueryActiveProcessors();

  141.         CurrentAffinity = 1;
  142.         Index = 0;
  143.         while(Affinity)
  144.         {
  145.                 Affinity &= ~CurrentAffinity;
  146.                 ((KESETAFFINITYTHREAD)fnKeSetAffinityThread)((PKTHREAD)PsGetCurrentThread(),(KAFFINITY)CurrentAffinity);
  147.                 CurrentAffinity <<= 1;

  148.                 __asm
  149.                 {
  150.                         push eax
  151.                         mov  eax,fs:[0x20]
  152.                         mov  kPrcb,eax
  153.                         pop  eax
  154.                 }

  155.                
  156.                 pKiProcessorBlock = &kPrcb;
  157.                
  158.                
  159.                 //KdPrint(("pKiProcessorBlock[%d]:%X\n", Index, *(ULONG*)(pKiProcessorBlock[Index] - 0xe8)));
  160.                 //KdPrint(("pKiProcessorBlock[%d]:%X\n", Index, *(ULONG*)(pKiProcessorBlock[Index] - 0xe4)));
  161.                
  162.                
  163.                
  164.                 pIdtEntry = *(PIDTENTRY*)(pKiProcessorBlock[Index] - 0xe8);
  165.                 KdPrint(("pIdtEntry:%X\n", pIdtEntry));

  166.                 pGdt = *(PKGDTENTRY*)(pKiProcessorBlock[Index] - 0xe4);
  167.                 KdPrint(("pGdt:%X--%X--%X--%X\n", pGdt, pGdt[1].BaseLow, pGdt[1].HighWord.Bits.BaseMid, pGdt[1].HighWord.Bits.BaseHi));


  168.                 PageProtectOff();

  169.                 if (bIsNew)
  170.                 {
  171.                         pIdtEntry[InterruptIndex].selector = 168;
  172.                         RtlCopyMemory(&pGdt[21], &pGdt[1], sizeof(KGDTENTRY));
  173.                         pGdt[21].BaseLow = (USHORT) (uNewBase & 0xffff);
  174.                         pGdt[21].HighWord.Bytes.BaseMid = (UCHAR)((uNewBase >> 16) & 0xff);
  175.                         pGdt[21].HighWord.Bytes.BaseHi = (UCHAR)(uNewBase >> 24);
  176.                 }
  177.                 else
  178.                 {
  179.                         pIdtEntry[InterruptIndex].selector = 8;
  180.                         memset(&pGdt[21], 0, sizeof(KGDTENTRY));
  181.                 }

  182.                 PageProtectOn();
  183.        

  184.                 Index++;
  185.         }

  186.         KdPrint(("Index:%d\n", Index));
  187.         return;
  188. }

  189. #pragma PAGEDCODE
  190. void HookInterruptFunc(ULONG InterruptIndex, ULONG NewInterruptFunc)
  191. {
  192.         ULONG uNewBase;

  193.         g_OrigInterruptFunc3 = GetInterruptFuncAddress(InterruptIndex);
  194.         uNewBase = NewInterruptFunc - g_OrigInterruptFunc3;

  195.         *(ULONG*)g_FilterJmp = (ULONG)NewInterrupt3OfOrigBase;
  196.         g_FilterJmp[2] = 0x8;

  197.         SetInterrupt(InterruptIndex, uNewBase, TRUE);
  198. }

  199. #pragma PAGEDCODE
  200. void UnHookInterruptFunc(ULONG InterruptIndex)
  201. {
  202.         SetInterrupt(InterruptIndex, 0, FALSE);
  203. }

  204. #pragma PAGEDCODE
  205. VOID MyDriverUnload(IN PDRIVER_OBJECT pDriverObject)
  206. {

  207.         UnHookInterruptFunc(3);
  208.         KdPrint(("DriverEntry unLoading...\n"));

  209. }


  210. #pragma INITCODE
  211. NTSTATUS DriverEntry(IN PDRIVER_OBJECT pDriverObject, IN PUNICODE_STRING RegistryPath)
  212. {
  213.         NTSTATUS status = STATUS_SUCCESS;

  214.         HookInterruptFunc(3, (ULONG)NewInterrupt3);

  215.         pDriverObject->DriverUnload = MyDriverUnload;
  216.         return status;
  217. }
复制代码

返回列表