forked from cheat-engine/cheat-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnoexceptions.c
200 lines (145 loc) · 4.2 KB
/
noexceptions.c
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include "noexceptions.h"
int MaxCPUCount;
PCPUSTATE cpustate = NULL;
#ifdef AMD64
extern void NoException14(void); //declared in debuggera.asm
extern int ExceptionlessCopy_Internal(PVOID destination, PVOID source, int size);
#else
extern void __cdecl NoException14(void); //declared in debuggera.asm
extern int __cdecl ExceptionlessCopy_Internal(PVOID destination, PVOID source, int size);
#endif
BOOL NoExceptions_Enter()
{
KIRQL old;
int i;
int cpunr;
//DbgPrint("NoExceptions_Enter");
__try
{
if (cpustate == NULL)
{
//initialize the list
MaxCPUCount = KeQueryActiveProcessorCount(NULL);
cpustate = ExAllocatePoolWithTag(NonPagedPool, MaxCPUCount*sizeof(CPUSTATE), 'cece');
if (cpustate)
{
RtlZeroMemory(cpustate, MaxCPUCount*sizeof(CPUSTATE));
for (i = 0; i < MaxCPUCount; i++)
{
cpustate[i].NoExceptionVectorList = ExAllocatePool(NonPagedPool, 256 * sizeof(INT_VECTOR));
if (cpustate[i].NoExceptionVectorList)
{
RtlZeroMemory(cpustate[i].NoExceptionVectorList, 256 * sizeof(INT_VECTOR));
}
else
{
//alloc failed, cleanup and quit
int j;
for (j = i - 1; i >= 0; i--)
ExFreePool(cpustate[i].NoExceptionVectorList);
cpustate = NULL;
return FALSE;
}
}
}
else
return FALSE;
}
//DbgPrint("cpustate setup here");
KeRaiseIrql(HIGH_LEVEL, &old);
cpunr = KeGetCurrentProcessorNumber();
cpustate[cpunr].entryIRQL = old;
if (cpustate[cpunr].OriginalIDT.wLimit == 0)
{
//initialize this
UINT_PTR newAddress;
__sidt(&cpustate[cpunr].OriginalIDT);
RtlCopyMemory(cpustate[cpunr].NoExceptionVectorList, cpustate[cpunr].OriginalIDT.vector, cpustate[cpunr].OriginalIDT.wLimit + 1);
//DbgPrint("idt. Limit=%d Vector=%p", (int)cpustate[cpunr].OriginalIDT.wLimit, cpustate[cpunr].OriginalIDT.vector);
//hook cpustate[cpunr].NoExceptionVectorList[0-15]
//DbgPrint("")
newAddress = (UINT_PTR)NoException14;
cpustate[cpunr].NoExceptionVectorList[14].wHighOffset = (WORD)((DWORD)(newAddress >> 16));
cpustate[cpunr].NoExceptionVectorList[14].wLowOffset = (WORD)newAddress;
#ifdef AMD64
cpustate[cpunr].NoExceptionVectorList[14].TopOffset = (newAddress >> 32);
cpustate[cpunr].NoExceptionVectorList[14].Reserved = 0;
#endif
/*
newVector.wHighOffset=(WORD)((DWORD)(newEIP >> 16));
newVector.wLowOffset=(WORD)newEIP;
newVector.wSelector=(WORD)newCS;
newVector.bUnused=0;
newVector.bAccessFlags=idt.vector[intnr].bAccessFlags; //don't touch accessflag, the default settings are good (e.g: int3,4 and 8 have dpl=3)
#ifdef AMD64
newVector.TopOffset=(newEIP >> 32);
newVector.Reserved=0;
#endif
*/
cpustate[cpunr].ModdedIDT = cpustate[cpunr].OriginalIDT;
cpustate[cpunr].ModdedIDT.vector = cpustate[cpunr].NoExceptionVectorList;
};
#ifdef AMD64
_disable();
#else
__asm{
cli
}
#endif
__lidt(&cpustate[cpunr].ModdedIDT);
return TRUE;
}
__except (1)
{
DbgPrint("Exception during NoExceptions_Enter. Figures");
}
//KeGetCurrentProcessorNumber()
//hadError = FALSE;
return FALSE;
}
int NoExceptions_CopyMemory(PVOID Destination, PVOID Source, int size)
{
BOOL EnteredNoExceptions = FALSE;
EFLAGS e = getEflags();
int r = 0;
if (KeGetCurrentIrql() != HIGH_LEVEL)
{
EnteredNoExceptions = NoExceptions_Enter();
if (EnteredNoExceptions == FALSE)
return 0;
}
r=ExceptionlessCopy_Internal(Destination, Source, size);
if (EnteredNoExceptions)
NoExceptions_Leave();
return r;
}
void NoExceptions_Leave()
{
int cpunr = KeGetCurrentProcessorNumber();
//restore the IDT
__lidt(&cpustate[cpunr].OriginalIDT);
#ifdef AMD64
_enable();
#else
__asm{
sti
}
#endif
KeLowerIrql(cpustate[cpunr].entryIRQL);
}
void NoExceptions_Cleanup()
{
if (cpustate)
{
int i;
for (i = 0; i < MaxCPUCount; i++)
{
if (cpustate[i].NoExceptionVectorList)
{
ExFreePool(cpustate[i].NoExceptionVectorList);
cpustate[i].NoExceptionVectorList = NULL;
}
}
ExFreePool(cpustate);
}
}