先上代码
#pragma pack(1)
typedef struct
{
unsigned short LowOffset;
unsigned short selector;
unsigned char unused_lo;
unsigned char segment_type:4; //0x0E is an interrupt gate
unsigned char system_segment_flag:1;
unsigned char DPL:2; // descriptor privilege level
unsigned char P:1; /* present */
unsigned short HiOffset;
} IDTENTRY;
/* sidt returns idt in this format */
typedef struct
{
unsigned short IDTLimit;
unsigned short LowIDTbase;
unsigned short HiIDTbase;
}IDTINFO;
#pragma pack()
#define MAKELONG(a, b) ((unsigned long) (((unsigned short) (a)) | ((unsigned long) ((unsigned short) (b))) << 16))
typedef VOID (*pfnKeSetAffinityThread)(IN PKTHREAD pKThread, IN KAFFINITY Affinity);
//修改IDT isr
BOOLEAN IDTHook_Modify(ULONG ulIndex, ULONG ulHookFuncAddr)
{
ULONG ulProcessors = KeNumberProcessors;
KIRQL irql;
IDTINFO IdtInfo;
IDTENTRY *pIdtEntry = NULL;
pfnKeSetAffinityThread KeSetAffinityThread = NULL;
UNICODE_STRING ustrKeSetAffinityThread;
RtlInitUnicodeString(&ustrKeSetAffinityThread, L"KeSetAffinityThread");
KeSetAffinityThread = (pfnKeSetAffinityThread)MmGetSystemRoutineAddress(&ustrKeSetAffinityThread);
if (KeSetAffinityThread == NULL)
{
return FALSE;
}
while (ulProcessors)
{
PMDL pMdlForIDT = NULL;
IDTENTRY *pTempIdt = NULL;
KeSetAffinityThread(KeGetCurrentThread(), ulProcessors);
KeRaiseIrql(HIGH_LEVEL, &irql);
//此处修改IDT 项
__asm sidt IdtInfo
pIdtEntry = MAKELONG(IdtInfo.LowIDTbase, IdtInfo.HiIDTbase);
pMdlForIDT = MmCreateMdl(NULL, &pIdtEntry[ulIndex], sizeof(IDTENTRY));
MmBuildMdlForNonPagedPool(pMdlForIDT);
pMdlForIDT->MdlFlags = pMdlForIDT->MdlFlags | MDL_MAPPED_TO_SYSTEM_VA;
pTempIdt = MmGetSystemAddressForMdlSafe(pMdlForIDT, NormalPagePriority);
if (!MmIsAddressValid(pTempIdt))
{
KdPrint(("地址无效\r\n"));
}
__asm cli;
pTempIdt->HiOffset = (USHORT)(ulHookFuncAddr >> 16);
pTempIdt->LowOffset = (USHORT)ulHookFuncAddr;
__asm sti;
MmUnmapLockedPages(pTempIdt, pMdlForIDT);
IoFreeMdl(pMdlForIDT);
KeLowerIrql(&irql);
ulProcessors--;
}
return TRUE;
}
pTempIdt->HiOffset = (USHORT)(ulHookFuncAddr >> 16);
此处bsod,求解释. pTempIdt正确
[注意]传递专业知识、拓宽行业人脉——看雪讲师团队等你加入!