forked from strivexjun/DriverInjectDll
-
Notifications
You must be signed in to change notification settings - Fork 0
/
undocumented.cpp
420 lines (376 loc) · 12.6 KB
/
undocumented.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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
#include "undocumented.h"
#include "ssdt.h"
typedef NTSTATUS(NTAPI* ZWQUERYINFORMATIONPROCESS)(
IN HANDLE ProcessHandle,
IN PROCESSINFOCLASS ProcessInformationClass,
OUT PVOID ProcessInformation,
IN ULONG ProcessInformationLength,
OUT PULONG ReturnLength OPTIONAL
);
typedef NTSTATUS(NTAPI* ZWQUERYINFORMATIONTHREAD)(
IN HANDLE ThreadHandle,
IN THREADINFOCLASS ThreadInformationClass,
IN OUT PVOID ThreadInformation,
IN ULONG ThreadInformationLength,
OUT PULONG ReturnLength OPTIONAL
);
typedef NTSTATUS(NTAPI* NTQUERYOBJECT)(
IN HANDLE Handle OPTIONAL,
IN OBJECT_INFORMATION_CLASS ObjectInformationClass,
OUT PVOID ObjectInformation OPTIONAL,
IN ULONG ObjectInformationLength,
OUT PULONG ReturnLength OPTIONAL
);
typedef NTSTATUS(NTAPI* ZWQUERYSYSTEMINFORMATION)(
IN SYSTEM_INFORMATION_CLASS SystemInformationClass,
OUT PVOID SystemInformation,
IN ULONG SystemInformationLength,
OUT PULONG ReturnLength OPTIONAL
);
typedef NTSTATUS(NTAPI* NTQUERYSYSTEMINFORMATION)(
IN SYSTEM_INFORMATION_CLASS SystemInformationClass,
OUT PVOID SystemInformation,
IN ULONG SystemInformationLength,
OUT PULONG ReturnLength OPTIONAL
);
typedef NTSTATUS(NTAPI* NTCLOSE)(
IN HANDLE Handle
);
typedef NTSTATUS(NTAPI* NTSETCONTEXTTHREAD)(
IN HANDLE ThreadHandle,
IN PCONTEXT Context
);
typedef NTSTATUS(NTAPI* NTCONTINUE)(
IN PCONTEXT Context,
BOOLEAN RaiseAlert
);
typedef NTSTATUS(NTAPI* NTDUPLICATEOBJECT)(
IN HANDLE SourceProcessHandle,
IN HANDLE SourceHandle,
IN HANDLE TargetProcessHandle,
OUT PHANDLE TargetHandle,
IN ACCESS_MASK DesiredAccess OPTIONAL,
IN ULONG HandleAttributes,
IN ULONG Options
);
typedef NTSTATUS(NTAPI* KERAISEUSEREXCEPTION)(
IN NTSTATUS ExceptionCode
);
typedef NTSTATUS(NTAPI* NTSETINFORMATIONTHREAD)(
IN HANDLE ThreadHandle,
IN THREADINFOCLASS ThreadInformationClass,
IN PVOID ThreadInformation,
IN ULONG ThreadInformationLength
);
typedef NTSTATUS(NTAPI* NTSETINFORMATIONPROCESS)(
IN HANDLE ProcessHandle,
IN PROCESSINFOCLASS ProcessInformationClass,
IN PVOID ProcessInformation,
IN ULONG ProcessInformationLength
);
typedef NTSTATUS(NTAPI* NTQUERYINFORMATIONPROCESS)(
IN HANDLE ProcessHandle,
IN PROCESSINFOCLASS ProcessInformationClass,
OUT PVOID ProcessInformation,
IN ULONG ProcessInformationLength,
OUT PULONG ReturnLength OPTIONAL
);
typedef NTSTATUS(NTAPI* NTSYSTEMDEBUGCONTROL)(
IN SYSDBG_COMMAND Command,
IN PVOID InputBuffer OPTIONAL,
IN ULONG InputBufferLength,
OUT PVOID OutputBuffer OPTIONAL,
IN ULONG OutputBufferLength,
OUT PULONG ReturnLength OPTIONAL
);
static ZWQUERYINFORMATIONPROCESS ZwQIP = 0;
static ZWQUERYINFORMATIONTHREAD ZwQIT = 0;
static NTQUERYOBJECT NtQO = 0;
static ZWQUERYSYSTEMINFORMATION ZwQSI = 0;
static NTQUERYSYSTEMINFORMATION NtQSI = 0;
static NTCLOSE NtClo = 0;
static NTSETCONTEXTTHREAD NtSCT = 0;
static NTCONTINUE NtCon = 0;
static NTDUPLICATEOBJECT NtDO = 0;
static KERAISEUSEREXCEPTION KeRUE = 0;
static NTSETINFORMATIONTHREAD NtSIT = 0;
static NTSETINFORMATIONPROCESS NtSIP = 0;
static NTQUERYINFORMATIONPROCESS NtQIP = 0;
static NTSYSTEMDEBUGCONTROL NtSDBC = 0;
NTSTATUS NTAPI Undocumented::ZwQueryInformationProcess(
IN HANDLE ProcessHandle,
IN PROCESSINFOCLASS ProcessInformationClass,
OUT PVOID ProcessInformation,
IN ULONG ProcessInformationLength,
OUT PULONG ReturnLength OPTIONAL)
{
return ZwQIP(ProcessHandle, ProcessInformationClass, ProcessInformation, ProcessInformationLength, ReturnLength);
}
NTSTATUS NTAPI Undocumented::ZwQueryInformationThread(
IN HANDLE ThreadHandle,
IN THREADINFOCLASS ThreadInformationClass,
IN OUT PVOID ThreadInformation,
IN ULONG ThreadInformationLength,
OUT PULONG ReturnLength OPTIONAL)
{
return ZwQIT(ThreadHandle, ThreadInformationClass, ThreadInformation, ThreadInformationLength, ReturnLength);
}
NTSTATUS NTAPI Undocumented::NtQueryObject(
IN HANDLE Handle OPTIONAL,
IN OBJECT_INFORMATION_CLASS ObjectInformationClass,
OUT PVOID ObjectInformation OPTIONAL,
IN ULONG ObjectInformationLength,
OUT PULONG ReturnLength OPTIONAL)
{
return NtQO(Handle, ObjectInformationClass, ObjectInformation, ObjectInformationLength, ReturnLength);
}
NTSTATUS NTAPI Undocumented::ZwQuerySystemInformation(
IN SYSTEM_INFORMATION_CLASS SystemInformationClass,
OUT PVOID SystemInformation,
IN ULONG SystemInformationLength,
OUT PULONG ReturnLength OPTIONAL)
{
return ZwQSI(SystemInformationClass, SystemInformation, SystemInformationLength, ReturnLength);
}
NTSTATUS NTAPI Undocumented::NtQuerySystemInformation(
IN SYSTEM_INFORMATION_CLASS SystemInformationClass,
OUT PVOID SystemInformation,
IN ULONG SystemInformationLength,
OUT PULONG ReturnLength OPTIONAL)
{
return NtQSI(SystemInformationClass, SystemInformation, SystemInformationLength, ReturnLength);
}
NTSTATUS NTAPI Undocumented::NtClose(
IN HANDLE Handle)
{
return NtClo(Handle);
}
NTSTATUS NTAPI Undocumented::NtSetContextThread(
IN HANDLE ThreadHandle,
IN PCONTEXT Context)
{
return NtSCT(ThreadHandle, Context);
}
NTSTATUS NTAPI Undocumented::NtContinue(
IN PCONTEXT Context,
BOOLEAN RaiseAlert)
{
return NtCon(Context, RaiseAlert);
}
NTSTATUS NTAPI Undocumented::NtDuplicateObject(
IN HANDLE SourceProcessHandle,
IN HANDLE SourceHandle,
IN HANDLE TargetProcessHandle,
OUT PHANDLE TargetHandle,
IN ACCESS_MASK DesiredAccess OPTIONAL,
IN ULONG HandleAttributes,
IN ULONG Options)
{
return NtDO(SourceProcessHandle, SourceHandle, TargetProcessHandle, TargetHandle, DesiredAccess, HandleAttributes, Options);
}
NTSTATUS NTAPI Undocumented::KeRaiseUserException(
IN NTSTATUS ExceptionCode)
{
return KeRUE(ExceptionCode);
}
NTSTATUS NTAPI Undocumented::NtSetInformationThread(
IN HANDLE ThreadHandle,
IN THREADINFOCLASS ThreadInformationClass,
IN PVOID ThreadInformation,
IN ULONG ThreadInformationLength)
{
return NtSIT(ThreadHandle, ThreadInformationClass, ThreadInformation, ThreadInformationLength);
}
NTSTATUS NTAPI Undocumented::NtSetInformationProcess(
IN HANDLE ProcessHandle,
IN PROCESSINFOCLASS ProcessInformationClass,
IN PVOID ProcessInformation,
IN ULONG ProcessInformationLength)
{
return NtSIP(ProcessHandle, ProcessInformationClass, ProcessInformation, ProcessInformationLength);
}
NTSTATUS NTAPI Undocumented::NtQueryInformationProcess(
IN HANDLE ProcessHandle,
IN PROCESSINFOCLASS ProcessInformationClass,
OUT PVOID ProcessInformation,
IN ULONG ProcessInformationLength,
OUT PULONG ReturnLength OPTIONAL)
{
return NtQIP(ProcessHandle, ProcessInformationClass, ProcessInformation, ProcessInformationLength, ReturnLength);
}
NTSTATUS NTAPI Undocumented::NtSystemDebugControl(
IN SYSDBG_COMMAND Command,
IN PVOID InputBuffer,
IN ULONG InputBufferLength,
OUT PVOID OutputBuffer,
IN ULONG OutputBufferLength,
OUT PULONG ReturnLength)
{
return NtSDBC(Command, InputBuffer, InputBufferLength, OutputBuffer, OutputBufferLength, ReturnLength);
}
bool Undocumented::UndocumentedInit()
{
//Exported kernel functions after this
if(!ZwQIP)
{
UNICODE_STRING routineName;
RtlInitUnicodeString(&routineName, L"ZwQueryInformationProcess");
ZwQIP = (ZWQUERYINFORMATIONPROCESS)MmGetSystemRoutineAddress(&routineName);
if(!ZwQIP)
return false;
}
if(!ZwQIT)
{
UNICODE_STRING routineName;
RtlInitUnicodeString(&routineName, L"ZwQueryInformationThread");
ZwQIT = (ZWQUERYINFORMATIONTHREAD)MmGetSystemRoutineAddress(&routineName);
if(!ZwQIT)
return false;
}
if(!ZwQSI)
{
UNICODE_STRING routineName;
RtlInitUnicodeString(&routineName, L"ZwQuerySystemInformation");
ZwQSI = (ZWQUERYSYSTEMINFORMATION)MmGetSystemRoutineAddress(&routineName);
if(!ZwQSI)
return false;
}
if(!NtQSI)
{
UNICODE_STRING routineName;
RtlInitUnicodeString(&routineName, L"NtQuerySystemInformation");
NtQSI = (NTQUERYSYSTEMINFORMATION)MmGetSystemRoutineAddress(&routineName);
if(!NtQSI)
return false;
}
if(!NtClo)
{
UNICODE_STRING routineName;
RtlInitUnicodeString(&routineName, L"NtClose");
NtClo = (NTCLOSE)MmGetSystemRoutineAddress(&routineName);
if(!NtClo)
return false;
}
if(!NtDO)
{
UNICODE_STRING routineName;
RtlInitUnicodeString(&routineName, L"NtDuplicateObject");
NtDO = (NTDUPLICATEOBJECT)MmGetSystemRoutineAddress(&routineName);
if(!NtDO)
return false;
}
if(!KeRUE)
{
UNICODE_STRING routineName;
RtlInitUnicodeString(&routineName, L"KeRaiseUserException");
KeRUE = (KERAISEUSEREXCEPTION)MmGetSystemRoutineAddress(&routineName);
if(!KeRUE)
return false;
}
if(!NtSIT)
{
UNICODE_STRING routineName;
RtlInitUnicodeString(&routineName, L"NtSetInformationThread");
NtSIT = (NTSETINFORMATIONTHREAD)MmGetSystemRoutineAddress(&routineName);
if(!NtSIT)
return false;
}
if(!NtSIP)
{
UNICODE_STRING routineName;
RtlInitUnicodeString(&routineName, L"NtSetInformationProcess");
NtSIP = (NTSETINFORMATIONPROCESS)MmGetSystemRoutineAddress(&routineName);
if(!NtSIP)
return false;
}
if(!NtQIP)
{
UNICODE_STRING routineName;
RtlInitUnicodeString(&routineName, L"NtQueryInformationProcess");
NtQIP = (NTQUERYINFORMATIONPROCESS)MmGetSystemRoutineAddress(&routineName);
if(!NtQIP)
return false;
}
//SSDT-only functions after this
if(!NtQO)
{
NtQO = (NTQUERYOBJECT)SSDT::GetFunctionAddress("NtQueryObject");
if(!NtQO)
return false;
}
if(!NtSCT)
{
NtSCT = (NTSETCONTEXTTHREAD)SSDT::GetFunctionAddress("NtSetContextThread");
if(!NtSCT)
return false;
}
if(!NtCon)
{
NtCon = (NTCONTINUE)SSDT::GetFunctionAddress("NtContinue");
if(!NtCon)
return false;
}
if(!NtSDBC)
{
NtSDBC = (NTSYSTEMDEBUGCONTROL)SSDT::GetFunctionAddress("NtSystemDebugControl");
if(!NtSDBC)
return false;
}
return true;
}
//Based on: http://alter.org.ua/docs/nt_kernel/procaddr
PVOID Undocumented::GetKernelBase(PULONG pImageSize)
{
typedef struct _SYSTEM_MODULE_ENTRY
{
HANDLE Section;
PVOID MappedBase;
PVOID ImageBase;
ULONG ImageSize;
ULONG Flags;
USHORT LoadOrderIndex;
USHORT InitOrderIndex;
USHORT LoadCount;
USHORT OffsetToFileName;
UCHAR FullPathName[256];
} SYSTEM_MODULE_ENTRY, *PSYSTEM_MODULE_ENTRY;
#pragma warning(disable:4200)
typedef struct _SYSTEM_MODULE_INFORMATION
{
ULONG Count;
SYSTEM_MODULE_ENTRY Module[0];
} SYSTEM_MODULE_INFORMATION, *PSYSTEM_MODULE_INFORMATION;
PVOID pModuleBase = NULL;
PSYSTEM_MODULE_INFORMATION pSystemInfoBuffer = NULL;
ULONG SystemInfoBufferSize = 0;
NTSTATUS status = Undocumented::ZwQuerySystemInformation(SystemModuleInformation,
&SystemInfoBufferSize,
0,
&SystemInfoBufferSize);
if(!SystemInfoBufferSize)
{
DPRINT("[DeugMessage] ZwQuerySystemInformation (1) failed...\r\n");
return NULL;
}
pSystemInfoBuffer = (PSYSTEM_MODULE_INFORMATION)ExAllocatePool(NonPagedPool, SystemInfoBufferSize * 2);
if(!pSystemInfoBuffer)
{
DPRINT("[DeugMessage] ExAllocatePool failed...\r\n");
return NULL;
}
memset(pSystemInfoBuffer, 0, SystemInfoBufferSize * 2);
status = Undocumented::ZwQuerySystemInformation(SystemModuleInformation,
pSystemInfoBuffer,
SystemInfoBufferSize * 2,
&SystemInfoBufferSize);
if(NT_SUCCESS(status))
{
pModuleBase = pSystemInfoBuffer->Module[0].ImageBase;
if(pImageSize)
*pImageSize = pSystemInfoBuffer->Module[0].ImageSize;
}
else
DPRINT("[DeugMessage] ZwQuerySystemInformation (2) failed...\r\n");
ExFreePool(pSystemInfoBuffer);
return pModuleBase;
}