-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathHookInt.cpp
77 lines (62 loc) · 1.9 KB
/
HookInt.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include "HookInt.h"
// Only work when compiled on WinXP
//extern "C" KAFFINITY KeSetAffinityThread(IN PKTHREAD Thread, IN KAFFINITY Affinity);
// Workaround for Win7/Vista
typedef NTSTATUS (NTAPI *_KeSetAffinityThread)(
IN PKTHREAD Thread,
IN KAFFINITY Affinity
);
_KeSetAffinityThread KeSetAffinityThread;
#define RESTORE_INTS
#define DISABLE_INTS
#define MAKELONG(a, b) ((ULONG)(((USHORT)(((ULONG)(a)) & 0xffff)) | ((ULONG)((USHORT)(((ULONG)(b)) & 0xffff))) << 16))
// layout of data struct retrieved from sidt instruction
typedef struct
{
USHORT limit;
USHORT lowBase;
USHORT highBase;
} IDT_INFO, *PIDT_INFO;
// simplified layout of an IDT entry
// (all the flag details have been omitted, we don't change them anyway)
typedef struct
{
USHORT lowOffset;
USHORT segSelector;
USHORT flags;
USHORT highOffset;
} IDT_ENTRY, *PIDT_ENTRY;
IDT_INFO getIDTInfo()
{
IDT_INFO retVal;
__asm
{
sidt retVal
//mov eax, fs:[0]KPCR.IDT
//mov retVal, eax
}
return retVal;
}
VOID hookInterrupt(PVOID newHandler, ULONG number, PUINT_PTR oldHandler)
{
IDT_INFO info;
__asm sidt info;
PIDT_ENTRY idt = (PIDT_ENTRY)MAKELONG(info.lowBase, info.highBase);
// save EFLAGS, then disable interrupts
__asm pushfd
__asm cli
UINT_PTR origHandler = (ULONG)(idt[number].highOffset) << 16 | idt[number].lowOffset;
idt[number].lowOffset = (USHORT)newHandler;
idt[number].highOffset = (USHORT)((ULONG)newHandler >> 16);
if (oldHandler) *oldHandler = origHandler;
// CLI just clears the IF in EFLAGS so we don't need to execute STI here
// by popping the previously pushed EFLAGS we revert to the original state
__asm popfd
}
VOID switchToCPU(CCHAR cpu)
{
UNICODE_STRING ustrKeSetAffinityThread;
RtlInitUnicodeString(&ustrKeSetAffinityThread, L"KeSetAffinityThread");
KeSetAffinityThread = (_KeSetAffinityThread)MmGetSystemRoutineAddress(&ustrKeSetAffinityThread);
KeSetAffinityThread(KeGetCurrentThread(), 1 << cpu);
}