-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathEXPORTS.cpp
More file actions
497 lines (389 loc) · 12.5 KB
/
Copy pathEXPORTS.cpp
File metadata and controls
497 lines (389 loc) · 12.5 KB
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
#include "EXPORTS.h"
namespace EXPORTS {
PVOID mm_get_system_routine_address(
PUNICODE_STRING SystemRoutineName
) {
auto function_address = NTOS::find_export<addr_t>(E("MmGetSystemRoutineAddress"));
if (!function_address) {
return {};
}
using function_t = PVOID(
PUNICODE_STRING SystemRoutineName
);
return reinterpret_cast<function_t*>(function_address)(SystemRoutineName);
}
inline addr_t c_ex_get_previous_mode = NULL;
KPROCESSOR_MODE ex_get_previous_mode() {
if (c_ex_get_previous_mode == NULL) {
c_ex_get_previous_mode = NTOS::find_export<addr_t>(E("ExGetPreviousMode"));
if (!c_ex_get_previous_mode) {
return NULL;
}
}
using function_t = KPROCESSOR_MODE(*)();
KPROCESSOR_MODE mode = reinterpret_cast<function_t>(c_ex_get_previous_mode)();
return mode;
}
NTSTATUS zw_query_system_information(
SYSTEM_INFORMATION_CLASS SystemInformationClass,
PVOID SystemInformation,
ULONG SystemInformationLength,
PULONG ReturnLength
) {
auto function_address = NTOS::find_export<addr_t>(E("ZwQuerySystemInformation"));
if (!function_address) {
return STATUS_UNSUCCESSFUL;
}
using function_t = NTSTATUS(
SYSTEM_INFORMATION_CLASS,
PVOID,
ULONG,
PULONG
);
return reinterpret_cast<function_t*>(function_address)(
SystemInformationClass,
SystemInformation,
SystemInformationLength,
ReturnLength
);
}
void mm_unmap_locked_pages(PVOID map, PMDL mdl) {
auto function_address = NTOS::find_export<addr_t>(E("MmUnmapLockedPages"));
if (!function_address) {
return;
}
using function_t = void(*)(PVOID, PMDL);
reinterpret_cast<function_t>(function_address)(map, mdl);
}
void mm_unlock_pages(PMDL mdl) {
auto function_address = NTOS::find_export<addr_t>(E("MmUnlockPages"));
if (!function_address) {
return;
}
using function_t = void(*)(PMDL);
reinterpret_cast<function_t>(function_address)(mdl);
}
void io_free_mdl(PMDL mdl) {
auto function_address = NTOS::find_export<addr_t>(E("IoFreeMdl"));
if (!function_address) {
return;
}
using function_t = void(*)(PMDL);
reinterpret_cast<function_t>(function_address)(mdl);
}
void mm_protect_mdl_system_address(PMDL mdl, ULONG protection) {
auto function_address = NTOS::find_export<addr_t>(E("MmProtectMdlSystemAddress"));
if (!function_address) {
return;
}
using function_t = void(*)(PMDL, ULONG);
reinterpret_cast<function_t>(function_address)(mdl, protection);
}
void mm_map_locked_pages_specify_cache(PMDL mdl, KPROCESSOR_MODE mode, ULONG cache_type, ULONG frame_size, ULONG priority) {
auto function_address = NTOS::find_export<addr_t>(E("MmMapLockedPagesSpecifyCache"));
if (!function_address) {
return;
}
using function_t = PVOID(*)(PMDL, KPROCESSOR_MODE, ULONG, ULONG, ULONG);
reinterpret_cast<function_t>(function_address)(mdl, mode, cache_type, frame_size, priority);
}
void mm_probe_and_lock_pages(PMDL mdl, KPROCESSOR_MODE mode, LOCK_OPERATION operation) {
auto function_address = NTOS::find_export<addr_t>(E("MmProbeAndLockPages"));
if (!function_address) {
return;
}
using function_t = NTSTATUS(*)(PMDL, KPROCESSOR_MODE, LOCK_OPERATION);
reinterpret_cast<function_t>(function_address)(mdl, mode, operation);
}
PMDL io_allocate_mdl(PVOID address, SIZE_T size, BOOLEAN secondary_buffer, BOOLEAN charge_quota, PIRP irp) {
auto function_address = NTOS::find_export<addr_t>(E("IoAllocateMdl"));
if (!function_address) {
return nullptr;
}
using function_t = PMDL(*)(PVOID, SIZE_T, BOOLEAN, BOOLEAN, PIRP);
return reinterpret_cast<function_t>(function_address)(address, size, secondary_buffer, charge_quota, irp);
}
void ke_stack_attach_process(PEPROCESS process, KAPC_STATE* apc_state) {
auto function_address = NTOS::find_export<addr_t>(E("KeStackAttachProcess"));
if (!function_address) {
return;
}
using function_t = void(*)(PEPROCESS, KAPC_STATE*);
reinterpret_cast<function_t>(function_address)(process, apc_state);
}
void ke_unstack_detach_process(KAPC_STATE* apc_state) {
auto function_address = NTOS::find_export<addr_t>(E("KeUnstackDetachProcess"));
if (!function_address) {
return;
}
using function_t = void(*)(KAPC_STATE*);
reinterpret_cast<function_t>(function_address)(apc_state);
}
void ob_dereference_object(PVOID object) {
auto function_address = NTOS::find_export<addr_t>(E("ObDereferenceObject"));
if (!function_address) {
return;
}
using function_t = void(*)(PVOID);
reinterpret_cast<function_t>(function_address)(object);
}
NTSTATUS rtl_compare_unicode_string(PUNICODE_STRING string1, PUNICODE_STRING string2, BOOLEAN case_insensitive) {
auto function_address = NTOS::find_export<addr_t>(E("RtlCompareUnicodeString"));
if (!function_address) {
return 0;
}
using function_t = NTSTATUS(*)(PUNICODE_STRING, PUNICODE_STRING, BOOLEAN);
return reinterpret_cast<function_t>(function_address)(string1, string2, case_insensitive);
}
PVOID ex_allocate_pool_with_tag(POOL_TYPE pool_type, SIZE_T size, ULONG tag) {
auto function_address = NTOS::find_export<addr_t>(E("ExAllocatePoolWithTag"));
if (!function_address) {
return nullptr;
}
using function_t = PVOID(*)(POOL_TYPE, SIZE_T, ULONG);
return reinterpret_cast<function_t>(function_address)(pool_type, size, tag);
}
ULONG get_current_processor_index() {
auto function_address = NTOS::find_export<addr_t>(E("KeGetCurrentProcessorIndex"));
if (!function_address) {
return 0;
}
using function_t = ULONG(*)();
return reinterpret_cast<function_t>(function_address)();
}
inline addr_t c_get_virtual_for_physical = NULL;
PVOID get_virtual_for_physical(PHYSICAL_ADDRESS physical_address) {
if (c_get_virtual_for_physical == NULL) {
c_get_virtual_for_physical = NTOS::find_export<addr_t>(E("MmGetVirtualForPhysical"));
if (!c_get_virtual_for_physical) {
return nullptr;
}
}
using function_t = PVOID(*)(PHYSICAL_ADDRESS);
return reinterpret_cast<function_t>(c_get_virtual_for_physical)(physical_address);
}
void* mm_allocate_contiguous_memory(
SIZE_T NumberOfBytes,
PHYSICAL_ADDRESS HighestAcceptableAddress
) {
auto function_address = NTOS::find_export<addr_t>(E("MmAllocateContiguousMemory"));
if (!function_address) {
return nullptr;
}
using function_t = void* (
SIZE_T NumberOfBytes,
PHYSICAL_ADDRESS HighestAcceptableAddress
);
return reinterpret_cast<function_t*>(function_address)(
NumberOfBytes,
HighestAcceptableAddress
);
};
inline addr_t c_ps_lookup_process_by_process_id = NULL;
NTSTATUS ps_lookup_process_by_process_id(HANDLE ProcessID, PEPROCESS* Process) {
if (c_ps_lookup_process_by_process_id == NULL) {
c_ps_lookup_process_by_process_id = NTOS::find_export<addr_t>(E("PsLookupProcessByProcessId"));
if (!c_ps_lookup_process_by_process_id) {
return STATUS_NOT_FOUND;
}
}
using function_t = NTSTATUS(
HANDLE ProcessID,
PEPROCESS* Process
);
return reinterpret_cast<function_t*>(c_ps_lookup_process_by_process_id)(ProcessID, Process);
}
void ex_release_resource_lite(PERESOURCE Resource) {
auto function_address = NTOS::find_export<addr_t>(E("ExReleaseResourceLite"));
if (!function_address) {
return;
}
using function_t = void(
PERESOURCE Resource
);
return reinterpret_cast<function_t*>(function_address)(Resource);
}
BOOLEAN rtl_delete_element_generic_table_avl(PRTL_AVL_TABLE Table, PVOID Buffer) {
auto function_address = NTOS::find_export<addr_t>(E("RtlDeleteElementGenericTableAvl"));
if (!function_address) {
return false;
}
using function_t = BOOLEAN(
PRTL_AVL_TABLE Table, PVOID Buffer
);
return reinterpret_cast<function_t*>(function_address)(Table, Buffer);
}
PVOID rtl_lookup_element_generic_table_avl(PRTL_AVL_TABLE Table, PVOID Buffer) {
auto function_address = NTOS::find_export<addr_t>(E("RtlLookupElementGenericTableAvl"));
if (!function_address) {
return nullptr;
}
using function_t = PVOID(
PRTL_AVL_TABLE Table, PVOID Buffer
);
return reinterpret_cast<function_t*>(function_address)(Table, Buffer);
}
void ke_query_system_time_precise(PLARGE_INTEGER CurrentTime) {
auto function_address = NTOS::find_export<addr_t>(E("KeQuerySystemTimePrecise"));
if (!function_address) {
return;
}
using function_t = void(
PLARGE_INTEGER CurrentTime
);
return reinterpret_cast<function_t*>(function_address)(
CurrentTime
);
}
void ex_free_pool_with_tag(
void* base_address,
ULONG tag
) {
auto function_address = NTOS::find_export<addr_t>(E("ExFreePoolWithTag"));
if (!function_address) {
return;
}
using function_t = void(
void* base_address,
ULONG tag
);
reinterpret_cast<function_t*>(function_address) (
base_address,
tag);
}
void* ex_allocate_pool(
POOL_TYPE pool_type,
SIZE_T number_of_bytes
) {
auto function_address = NTOS::find_export<addr_t>(E("ExAllocatePool"));
if (!function_address) {
return nullptr;
}
using function_t = void* (
POOL_TYPE pool_type,
SIZE_T number_of_bytes
);
return reinterpret_cast<function_t*>(function_address)(
pool_type,
number_of_bytes);
}
BOOLEAN ex_acquire_exclusive_lite(PERESOURCE PiDDBLock, BOOL Wait) {
auto function_address = NTOS::find_export<addr_t>(E("ExAcquireResourceExclusiveLite"));
if (!function_address) {
return false;
}
using function_t = BOOLEAN(
PERESOURCE PiDDBLock, BOOL Wait
);
return reinterpret_cast<function_t*>(function_address)(PiDDBLock, Wait);
}
[[ nodiscard ]]
void rtl_init_unicode_string(
PUNICODE_STRING destination_string,
PCWSTR source_string
) {
auto function_address = NTOS::find_export<addr_t>(E("RtlInitUnicodeString"));
if (!function_address) {
return;
}
using function_t = void* (
PUNICODE_STRING destination_string,
PCWSTR source_string
);
reinterpret_cast<function_t*>(function_address) (
destination_string,
source_string);
}
NTSTATUS ZwQuerySystemInformation_impl(
SYSTEM_INFORMATION_CLASS SystemInformationClass,
PVOID SystemInformation,
ULONG SystemInformationLength,
PULONG ReturnLength
) {
auto function_address = NTOS::find_export<addr_t>(E("ZwQuerySystemInformation"));
if (!function_address) {
return STATUS_UNSUCCESSFUL;
}
using function_t = NTSTATUS(
SYSTEM_INFORMATION_CLASS,
PVOID,
ULONG,
PULONG
);
return reinterpret_cast<function_t*>(function_address)(
SystemInformationClass,
SystemInformation,
SystemInformationLength,
ReturnLength
);
}
uintptr_t ps_initial_system_process() {
auto function_address = NTOS::find_export<addr_t>(E("PsInitialSystemProcess"));
if (!function_address) {
return {};
}
return *reinterpret_cast<uintptr_t*>(function_address);
}
bool ps_get_process_exit_status(
uintptr_t Process
) {
auto function_address = NTOS::find_export<addr_t>(E("PsGetProcessExitStatus"));
if (!function_address) {
return false;
}
using function_t = NTSTATUS(
uintptr_t Process
);
return reinterpret_cast<function_t*>(function_address)(Process) == 0x103;
}
unicode_string_t* ps_query_full_process_image_name(
std::uintptr_t process
) {
auto function_address = NTOS::find_export<addr_t>(E("SeLocateProcessImageName"));
if (!function_address) return { };
while (function_address[0x0] != 0xec
|| function_address[0x1] != 0x28
|| function_address[0x2] != 0xe8)
function_address++;
auto ps_rva{ &function_address[0x7] + *reinterpret_cast<std::int32_t*>(&function_address[0x3]) };
if (!ps_rva)
return {};
while (ps_rva[0x0] != 0x0f
|| ps_rva[0x1] != 0x85
|| ps_rva[0x6] != 0x48)
ps_rva++;
return *reinterpret_cast<unicode_string_t**>
(process + *reinterpret_cast<std::int32_t*>(&ps_rva[0x9]));
}
void* ps_get_process_section_base_address(
PEPROCESS Process
) {
auto function_address = NTOS::find_export<addr_t>(E("PsGetProcessSectionBaseAddress"));
if (!function_address) {
return nullptr;
}
using function_t = void* (
PEPROCESS Process
);
return reinterpret_cast<function_t*>(function_address)(Process);
}
PPEB ps_get_process_peb(PEPROCESS Process) {
auto function_address = NTOS::find_export<addr_t>(E("PsGetProcessPeb"));
if (!function_address) {
return nullptr;
}
using function_t = PPEB(
PEPROCESS Process
);
return reinterpret_cast<function_t*>(function_address)(Process);
}
PKTHREAD ke_get_current_thread() {
auto function_address = NTOS::find_export<addr_t>(E("KeGetCurrentThread"));
if (!function_address) {
return nullptr;
}
using function_t = PKTHREAD(
);
return reinterpret_cast<function_t*>(function_address)();
}
}