-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathNT Funcs.h
487 lines (424 loc) · 11.1 KB
/
NT Funcs.h
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
#pragma once
#include "Win7.h"
#include "Win8.h"
#include "Win81.h"
#include "Win10.h"
#include "Win11.h"
#define DEF_STRUCT_DEFAULT(name, suffix) \
using name = name##suffix; \
using P##name = P##name##suffix; \
using _##name = _##name##suffix;
#define DEF_STRUCT_DEFAULT_32(name, suffix) \
using name##_32 = name##suffix##_32; \
using P##name##_32 = P##name##suffix##_32; \
using _##name##_32 = _##name##suffix##_32;
#ifndef _WIN32_WINNT
#error Not supported
#else
#if(_WIN32_WINNT == _WIN32_WINNT_WIN7)
DEF_STRUCT_DEFAULT(LDR_DATA_TABLE_ENTRY, _WIN7)
DEF_STRUCT_DEFAULT(LDR_DDAG_NODE, _WIN7)
#ifdef _WIN64
DEF_STRUCT_DEFAULT_32(LDR_DATA_TABLE_ENTRY, _WIN7)
DEF_STRUCT_DEFAULT_32(LDR_DDAG_NODE, _WIN7)
#endif
#elif (_WIN32_WINNT == _WIN32_WINNT_WIN8)
DEF_STRUCT_DEFAULT(LDR_DATA_TABLE_ENTRY, _WIN8)
DEF_STRUCT_DEFAULT(LDR_DDAG_NODE, _WIN8)
#ifdef _WIN64
DEF_STRUCT_DEFAULT_32(LDR_DATA_TABLE_ENTRY, _WIN8)
DEF_STRUCT_DEFAULT_32(LDR_DDAG_NODE, _WIN8)
#endif
#elif (_WIN32_WINNT == _WIN32_WINNT_WINBLUE)
DEF_STRUCT_DEFAULT(LDR_DATA_TABLE_ENTRY, _WIN81)
DEF_STRUCT_DEFAULT(LDR_DDAG_NODE, _WIN81)
#ifdef _WIN64
DEF_STRUCT_DEFAULT_32(LDR_DATA_TABLE_ENTRY, _WIN81)
DEF_STRUCT_DEFAULT_32(LDR_DDAG_NODE, _WIN81)
#endif
#elif (_WIN32_WINNT == _WIN32_WINNT_WIN10) //includes Win11
#if (WDK_NTDDI_VERSION == NTDDI_WIN10_CO) //Win11 SDK is called NTDDI_WIN10_CO
DEF_STRUCT_DEFAULT(LDR_DATA_TABLE_ENTRY, _WIN11)
DEF_STRUCT_DEFAULT(LDR_DDAG_NODE, _WIN11)
#ifdef _WIN64
DEF_STRUCT_DEFAULT_32(LDR_DATA_TABLE_ENTRY, _WIN11)
DEF_STRUCT_DEFAULT_32(LDR_DDAG_NODE, _WIN11)
#endif
#else
DEF_STRUCT_DEFAULT(LDR_DATA_TABLE_ENTRY, _WIN10)
DEF_STRUCT_DEFAULT(LDR_DDAG_NODE, _WIN10)
#ifdef _WIN64
DEF_STRUCT_DEFAULT_32(LDR_DATA_TABLE_ENTRY, _WIN10)
DEF_STRUCT_DEFAULT_32(LDR_DDAG_NODE, _WIN10)
#endif
#endif
#else
#error Not supported
#endif
#endif
#pragma region function prototypes
using f_NtCreateThreadEx = NTSTATUS (__stdcall *)
(
HANDLE * pHandle,
ACCESS_MASK DesiredAccess,
void * pAttr,
HANDLE hTargetProc,
void * pFunc,
void * pArg,
ULONG Flags,
SIZE_T ZeroBits,
SIZE_T StackSize,
SIZE_T MaxStackSize,
void * pAttrListOut
);
using f_LdrLoadDll = NTSTATUS (__stdcall *)
(
LDR_SEARCH_PATH ldrSearchPath,
ULONG * pFlags,
UNICODE_STRING * pModuleFileName,
HANDLE * pOut
);
using f_LdrLoadDll_WIN8 = NTSTATUS (__stdcall *)
(
BOOLEAN Unknown1, //set to TRUE
ULONG * LoadFlags,
UNICODE_STRING * pModuleFileName,
HANDLE * pOut
);
using f_LdrUnloadDll = NTSTATUS (__stdcall *)
(
HANDLE DllHandle
);
using f_LdrpLoadDll_WIN7 = NTSTATUS (__stdcall *)
(
UNICODE_STRING * dll_path,
UNICODE_STRING * search_path,
LDRP_LOAD_CONTEXT_FLAGS Flags,
BOOLEAN Unknown1, //set to TRUE
PVOID Unknown2, //can be nullptr
LDR_DATA_TABLE_ENTRY_WIN7 ** ldr_out
);
using f_LdrpLoadDll_WIN8 = NTSTATUS (__stdcall *)
(
UNICODE_STRING * dll_path,
LDRP_PATH_SEARCH_CONTEXT_WIN8 * search_ctx,
LDRP_LOAD_CONTEXT_FLAGS Flags,
BOOLEAN Unknown, //set to TRUE
LDR_DATA_TABLE_ENTRY_WIN8 ** entry_out,
LDR_DDAG_NODE_WIN8 ** ddag_out
);
using f_LdrpLoadDll_WIN81 = NTSTATUS (__fastcall *)
(
UNICODE_STRING * dll_path,
LDRP_PATH_SEARCH_CONTEXT_WIN81 * search_ctx,
LDRP_LOAD_CONTEXT_FLAGS Flags,
BOOLEAN Unknown, //set to TRUE
LDR_DATA_TABLE_ENTRY_WIN81 ** entry_out,
LDR_DDAG_NODE_WIN81 ** ddag_out
);
//1507-1803
using f_LdrpLoadDll_1507 = NTSTATUS (__fastcall *)
(
UNICODE_STRING * dll_path,
LDRP_PATH_SEARCH_CONTEXT * search_path,
LDRP_LOAD_CONTEXT_FLAGS Flags,
BOOLEAN bUnknown, //set to TRUE
LDR_DATA_TABLE_ENTRY_WIN10 ** ldr_out
);
//1809+
using f_LdrpLoadDll = NTSTATUS (__fastcall *)
(
UNICODE_STRING * dll_path,
LDRP_PATH_SEARCH_CONTEXT * search_path,
LDRP_LOAD_CONTEXT_FLAGS Flags,
LDR_DATA_TABLE_ENTRY ** ldr_out
);
using f_LdrpLoadDllInternal = NTSTATUS (__fastcall *)
(
UNICODE_STRING * dll_path,
LDRP_PATH_SEARCH_CONTEXT * search_path,
LDRP_LOAD_CONTEXT_FLAGS Flags,
ULONG32 Unknown0, //set to 4
LDR_DATA_TABLE_ENTRY_WIN10 * Unknown1, //set to nullptr
LDR_DATA_TABLE_ENTRY_WIN10 * Unknown2, //set to nullptr
LDR_DATA_TABLE_ENTRY_WIN10 ** ldr_out,
ULONG_PTR * Unknown3 //set to pointer to nullptr
);
using f_LdrpLoadDllInternal_WIN11 = NTSTATUS (__fastcall *)
(
UNICODE_STRING * dll_path,
LDRP_PATH_SEARCH_CONTEXT * search_path,
LDRP_LOAD_CONTEXT_FLAGS Flags,
ULONG32 Unknown0, //set to 4
LDR_DATA_TABLE_ENTRY_WIN11 * Unknown1, //set to nullptr
LDR_DATA_TABLE_ENTRY_WIN11 * Unknown2, //set to nullptr
LDR_DATA_TABLE_ENTRY_WIN11 ** ldr_out,
ULONG_PTR * Unknown3, //set to pointer to nullptr
ULONG Unknown4 //set to 0
);
using f_LdrGetDllHandleEx = NTSTATUS (__stdcall *)
(
ULONG Flags,
PWSTR OptDllPath,
PULONG OptDllCharacteristics,
UNICODE_STRING * DllName,
PVOID * DllHandle
);
using f_LdrGetProcedureAddress = NTSTATUS (__stdcall *)
(
PVOID BaseAddress,
ANSI_STRING * Name,
ULONG Ordinal,
PVOID * ProcedureAddress
);
using f_NtQueryInformationProcess = NTSTATUS (__stdcall *)
(
HANDLE hTargetProc,
PROCESSINFOCLASS PIC,
void * pBuffer,
ULONG BufferSize,
ULONG * SizeOut
);
using f_NtQuerySystemInformation = NTSTATUS (__stdcall *)
(
SYSTEM_INFORMATION_CLASS SIC,
void * pBuffer,
ULONG BufferSize,
ULONG * SizeOut
);
using f_NtQueryInformationThread = NTSTATUS (__stdcall *)
(
HANDLE hThread,
THREADINFOCLASS TIC,
void * pBuffer,
ULONG BufferSize,
ULONG * SizeOut
);
using f_RtlQueueApcWow64Thread = NTSTATUS (__stdcall *)
(
HANDLE hThread,
void * pRoutine,
void * pArg1,
void * pArg2,
void * pArg3
);
using f_LdrpPreprocessDllName = NTSTATUS (__fastcall *)
(
UNICODE_STRING * DllName,
LDRP_UNICODE_STRING_BUNDLE * OutputDllName,
LDR_DATA_TABLE_ENTRY * pOptParentEntry,
LDRP_LOAD_CONTEXT_FLAGS * LoadContextFlags
);
using f_RtlInsertInvertedFunctionTable_WIN7 = NTSTATUS (__stdcall *)
(
RTL_INVERTED_FUNCTION_TABLE_WIN7 * pTable,
void * ImageBase,
DWORD SizeOfImage
);
using f_RtlInsertInvertedFunctionTable_WIN8 = NTSTATUS (__stdcall *)
(
void * ImageBase,
DWORD SizeOfImage
);
using f_RtlInsertInvertedFunctionTable = BOOL (__fastcall *)
(
void * ImageBase,
DWORD SizeOfImage
);
#ifdef _WIN64
using f_RtlAddFunctionTable = BOOL (__stdcall *)
(
RUNTIME_FUNCTION * FunctionTable,
DWORD EntryCount,
DWORD64 BaseAddress
);
#endif
using f_LdrpHandleTlsData_WIN8 = NTSTATUS (__stdcall *)
(
LDR_DATA_TABLE_ENTRY_WIN8 * pEntry
);
using f_LdrpHandleTlsData = NTSTATUS (__fastcall *)
(
LDR_DATA_TABLE_ENTRY * pEntry
);
using f_LdrLockLoaderLock = NTSTATUS (__stdcall *)
(
ULONG Flags,
ULONG * State,
ULONG_PTR * Cookie
);
using f_LdrUnlockLoaderLock = NTSTATUS (__stdcall *)
(
ULONG Flags,
ULONG_PTR Cookie
);
using f_memmove = VOID (__cdecl *)
(
PVOID UNALIGNED Destination,
LPCVOID UNALIGNED Source,
SIZE_T Length
);
using f_RtlZeroMemory = VOID (__stdcall *)
(
PVOID UNALIGNED Destination,
SIZE_T Length
);
using f_RtlAllocateHeap = PVOID (__stdcall *)
(
PVOID HeapHandle,
ULONG Flags,
SIZE_T Size
);
using f_RtlFreeHeap = BOOLEAN (__stdcall *)
(
PVOID HeapHandle,
ULONG Flags,
PVOID BaseAddress
);
using f_RtlAnsiStringToUnicodeString = NTSTATUS (__stdcall *)
(
UNICODE_STRING * DestinationString,
ANSI_STRING * SourceString,
BOOLEAN AllocateDestinationString
);
using f_RtlRbInsertNodeEx = VOID (__stdcall *)
(
RTL_RB_TREE * Tree,
RTL_BALANCED_NODE * Parent,
BOOLEAN Right,
RTL_BALANCED_NODE * Node
);
using f_RtlRbRemoveNode = VOID (__stdcall *)
(
RTL_RB_TREE * pTree,
RTL_BALANCED_NODE * pNode
);
using f_NtOpenFile = NTSTATUS (__stdcall *)
(
HANDLE * hFileOut,
ACCESS_MASK DesiredAccess,
OBJECT_ATTRIBUTES * pAtrributes,
IO_STATUS_BLOCK * pIoStatusBlock,
ULONG ShareAccess,
ULONG OpenOptions
);
using f_NtReadFile = NTSTATUS (__stdcall *)
(
HANDLE FileHandle,
HANDLE hOptEvent,
PVOID pOptApc,
PVOID pOptApcContext,
IO_STATUS_BLOCK * IoStatusBlock,
PVOID Buffer,
ULONG Length,
LARGE_INTEGER * pOptByteOffset,
ULONG * pOptKey
);
using f_NtSetInformationFile = NTSTATUS (__stdcall *)
(
HANDLE FileHandle,
IO_STATUS_BLOCK * IoStatusBlock,
PVOID FileInformation,
ULONG Length,
FILE_INFORMATION_CLASS FileInformationClass
);
using f_NtQueryInformationFile = NTSTATUS (__stdcall *)
(
HANDLE FileHandle,
IO_STATUS_BLOCK * pIoStatusBlock,
PVOID FileInformation,
ULONG Length,
FILE_INFORMATION_CLASS FileInformationClass
);
using f_NtClose = NTSTATUS (__stdcall *)
(
HANDLE Handle
);
using f_NtAllocateVirtualMemory = NTSTATUS (__stdcall *)
(
HANDLE ProcessHandle,
PVOID * BaseAddress,
ULONG_PTR ZeroBits,
SIZE_T * RegionSize,
ULONG AllocationType,
ULONG Protect
);
using f_NtFreeVirtualMemory = NTSTATUS (__stdcall *)
(
HANDLE ProcessHandle,
PVOID * BaseAddress,
SIZE_T * RegionSize,
ULONG FreeType
);
using f_NtProtectVirtualMemory = NTSTATUS (__stdcall *)
(
HANDLE ProcessHandle,
PVOID * BaseAddress,
SIZE_T * Size,
ULONG NewAccess,
ULONG * OldAccess
);
using f_NtCreateSection = NTSTATUS (__stdcall *)
(
HANDLE * SectionHandle,
ACCESS_MASK DesiredAccess,
OBJECT_ATTRIBUTES * ObjectAttributes,
LARGE_INTEGER * MaximumSize,
ULONG SectionPageProtection,
ULONG AllocationAttributes,
HANDLE FileHandle
);
using f_NtMapViewOfSection = NTSTATUS (__stdcall *)
(
HANDLE SectionHandle,
HANDLE ProcessHandle,
PVOID * BaseAddress,
ULONG_PTR ZeroBits,
SIZE_T CommitSize,
LARGE_INTEGER * SectionOffset,
SIZE_T * ViewSize,
SECTION_INHERIT InheritDisposition,
ULONG AllocationType,
ULONG Win32Protect
);
using f_LdrProtectMrdata = VOID (__stdcall *)
(
BOOL bProtected
);
using f_RtlAddVectoredExceptionHandler = PVOID (__stdcall *)
(
ULONG FirstHandler,
PVECTORED_EXCEPTION_HANDLER VectoredHandler
);
using f_RtlRemoveVectoredExceptionHandler = ULONG (__stdcall *)
(
PVOID Handle
);
using f_NtDelayExecution = NTSTATUS (__stdcall *)
(
BOOLEAN Alertable,
LARGE_INTEGER * DelayInterval
);
using f_NtQueryObject = NTSTATUS (__stdcall *)
(
HANDLE Handle,
OBJECT_INFORMATION_CLASS ObjectInformationClass,
PVOID ObjectInformation,
ULONG ObjectInformationLength,
PULONG ReturnLength
);
using f_memset = decltype(memset)*;
using f_LdrpModuleBaseAddressIndex = RTL_RB_TREE *;
using f_LdrpMappingInfoIndex = RTL_RB_TREE *;
using f_LdrpHeap = PVOID *;
using f_LdrpInvertedFunctionTable = RTL_INVERTED_FUNCTION_TABLE *;
using f_LdrpDefaultPath = UNICODE_STRING *;
using f_LdrpVectorHandlerList = RTL_VECTORED_HANDLER_LIST *;
using f_LdrpTlsList = LIST_ENTRY *;
//ntdll.dll:
using f_RtlpUnhandledExceptionFilter = ULONG_PTR *; //encrypted with RtlEncodePointer, points to kernel32.UnhandledExceptionFilter
//kernel32.dll:
using f_UnhandledExceptionFilter = ULONG_PTR *; //PTOP_LEVEL_EXCEPTION_FILTER
using f_SingleHandler = ULONG_PTR *; //encrypted with RtlEncodePointer, points to kernel32.DefaultHandler
using f_DefaultHandler = ULONG_PTR *; //PTOP_LEVEL_EXCEPTION_FILTER
#pragma endregion