登录  | 立即注册

游客您好!登录后享受更多精彩

查看: 106|回复: 0

关于不用汇编获取IDT的方式

[复制链接]

90

主题

9

回帖

407

积分

管理员

积分
407
发表于 2024-12-24 19:56:59 | 显示全部楼层 |阅读模式
  1. #include <WinDef.h>
  2. #include<ntddk.h>

  3.    typedef union _KIDTENTRY64 {
  4.     struct {
  5.      USHORT OffsetLow;
  6.      USHORT Selector;
  7.      USHORT IstIndex : 3;
  8.      USHORT Reserved0 : 5;
  9.      USHORT Type : 5;
  10.      USHORT Dpl : 2;
  11.      USHORT Present : 1;
  12.      USHORT OffsetMiddle;
  13.      ULONG OffsetHigh;
  14.      ULONG Reserved1;
  15.     };
  16.     ULONG64 Alignment;
  17.    } KIDTENTRY64, *PKIDTENTRY64;
  18.    typedef struct _AMD64_DESCRIPTOR {
  19.     USHORT  Pad[3];
  20.     USHORT  Limit;
  21.     ULONG64 Base;
  22.    } AMD64_DESCRIPTOR, *PAMD64_DESCRIPTOR;
  23.    typedef NTSTATUS(NTAPI *_KeSetAffinityThread)(
  24.     IN PKTHREAD Thread,
  25.     IN KAFFINITY Affinity);
  26.    PKIDTENTRY64 *g_ppIdtEntry;


  27. VOID GetIDT()
  28. {
  29. PKIDTENTRY64 Idt;
  30. KAFFINITY Processor = KeQueryActiveProcessors();
  31. UNICODE_STRING ustrKeSetAffinityThread;
  32. _KeSetAffinityThread KeSetAffinityThread;
  33. RtlInitUnicodeString(&ustrKeSetAffinityThread, L"KeSetAffinityThread");
  34. KeSetAffinityThread = (_KeSetAffinityThread)MmGetSystemRoutineAddress(&ustrKeSetAffinityThread);
  35. LONG Count = 0;
  36. for (LONG i = 0; i < Processor; i++)
  37. {
  38.    LONG a = Processor & (1 << i);
  39.    if (a != 0)
  40.    {
  41.     KeSetAffinityThread(KeGetCurrentThread(), (KAFFINITY)a);
  42.     Idt = KeGetPcr()->IdtBase;//主要是巨硬封装好的函数
  43.     g_ppIdtEntry[Count] = Idt;
  44.     DbgPrint("%d  %p \r\n", Count, g_ppIdtEntry[Count]);
  45.     Count++;
  46.     continue;
  47.    }
  48.    break;
  49. }
  50. }
  51. VOID IDTUnload(IN PDRIVER_OBJECT DriverObject)
  52. {
  53. DbgPrint("卸载成功");
  54. ExFreePoolWithTag(g_ppIdtEntry, 'gidt');
  55. }
  56. NTSTATUS DriverEntry(IN PDRIVER_OBJECT DriverObject, IN PUNICODE_STRING  RegistryPath)
  57. {
  58. NTSTATUS Status = STATUS_SUCCESS;

  59. g_ppIdtEntry = (PKIDTENTRY64 *)ExAllocatePoolWithTag(NonPagedPool, sizeof(PKIDTENTRY64) * KeNumberProcessors,'gidt');
  60. if (!MmIsAddressValid(g_ppIdtEntry))
  61. {
  62.    DbgPrint("g_ppIdtEntry Error ");
  63.    return Status;
  64. }
  65. GetIDT();
  66. for (size_t i = 0; i < KeNumberProcessors; i++)
  67. {
  68.    DbgPrint("%0.8X %0.8X %0.8X %p\r\n",
  69.     g_ppIdtEntry[0].OffsetHigh,
  70.     g_ppIdtEntry[0].OffsetMiddle,
  71.     g_ppIdtEntry[0].OffsetLow,
  72.     (((ULONGLONG)g_ppIdtEntry[0].OffsetHigh << 32) |               //(直接+也可以)
  73.     ((ULONGLONG)g_ppIdtEntry[0].OffsetMiddle << 16) |
  74.     (ULONG)g_ppIdtEntry[0].OffsetLow));
  75. }

  76. DriverObject->DriverUnload = IDTUnload;
  77. return Status;
  78. }
复制代码


您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|断点社区 |网站地图

GMT+8, 2025-1-18 18:45 , Processed in 0.070731 second(s), 26 queries .

Powered by XiunoBBS

Copyright © 2001-2025, 断点社区.

快速回复 返回顶部 返回列表