-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathNtUtils.Threads.pas
975 lines (808 loc) · 26 KB
/
NtUtils.Threads.pas
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
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
unit NtUtils.Threads;
{
This module provides functions for working with threads via Native API.
}
interface
uses
Ntapi.WinNt, Ntapi.ntdef, Ntapi.ntpsapi, Ntapi.ntrtl, Ntapi.ntseapi,
Ntapi.ntpebteb, Ntapi.Versions, NtUtils, DelphiUtils.AutoEvents;
const
THREAD_READ_TEB = THREAD_GET_CONTEXT or THREAD_SET_CONTEXT;
// For suspend/resume via state change
THREAD_CHANGE_STATE = THREAD_SET_INFORMATION or THREAD_SUSPEND_RESUME;
type
TThreadInfo = record
ClientID: TClientId;
TebAddress: PTeb;
end;
PThreadInfo = ^TThreadInfo;
TThreadApcOptions = set of (
apcForceSignal, // Use special user APCs when possible (Win 10 RS5+)
apcWoW64 // Queue a WoW64 APC
);
{ Opening }
// Open a thread (always succeeds for the current PID)
[RequiredPrivilege(SE_DEBUG_PRIVILEGE, rpForBypassingChecks)]
function NtxOpenThread(
out hxThread: IHandle;
TID: TThreadId;
DesiredAccess: TThreadAccessMask;
HandleAttributes: TObjectAttributesFlags = 0;
[opt] PID: TProcessId = 0
): TNtxStatus;
// Reopen a handle to the current thread with the specific access
function NtxOpenCurrentThread(
out hxThread: IHandle;
DesiredAccess: TThreadAccessMask = MAXIMUM_ALLOWED;
HandleAttributes: TObjectAttributesFlags = 0
): TNtxStatus;
// Open the next accessible thread in the process
[RequiredPrivilege(SE_DEBUG_PRIVILEGE, rpForBypassingChecks)]
function NtxGetNextThread(
[Access(PROCESS_QUERY_INFORMATION)] const hxProcess: IHandle;
[opt] var hxThread: IHandle; // use nil to start
DesiredAccess: TThreadAccessMask;
HandleAttributes: TObjectAttributesFlags = 0
): TNtxStatus;
// Make a for-in iterator for enumerating process thread via NtGetNextThread.
// Note: when the Status parameter is not set, the function might raise
// exceptions during enumeration.
function NtxIterateGetNextThread(
[out, opt] Status: PNtxStatus;
[Access(PROCESS_QUERY_INFORMATION)] const hxProcess: IHandle;
DesiredAccess: TThreadAccessMask;
HandleAttributes: TObjectAttributesFlags = 0
): IEnumerable<IHandle>;
// Open a process containing a thread
[RequiredPrivilege(SE_DEBUG_PRIVILEGE, rpForBypassingChecks)]
function NtxOpenProcessByThreadId(
out hxProcess: IHandle;
TID: TThreadId;
DesiredAccess: TProcessAccessMask;
HandleAttributes: TObjectAttributesFlags = 0
): TNtxStatus;
{ Querying/Setting }
// Query variable-size information
function NtxQueryThread(
[Access(THREAD_QUERY_INFORMATION)] const hxThread: IHandle;
InfoClass: TThreadInfoClass;
out xMemory: IMemory;
InitialBuffer: Cardinal = 0;
[opt] GrowthMethod: TBufferGrowthMethod = nil
): TNtxStatus;
// Set variable-size information
[RequiredPrivilege(SE_DEBUG_PRIVILEGE, rpSometimes)]
[RequiredPrivilege(SE_INCREASE_BASE_PRIORITY_PRIVILEGE, rpSometimes)]
function NtxSetThread(
[Access(THREAD_SET_INFORMATION)] const hxThread: IHandle;
InfoClass: TThreadInfoClass;
[in] Buffer: Pointer;
BufferSize: Cardinal
): TNtxStatus;
type
NtxThread = class abstract
// Query fixed-size information
class function Query<T>(
[Access(THREAD_QUERY_INFORMATION)] const hxThread: IHandle;
InfoClass: TThreadInfoClass;
out Buffer: T
): TNtxStatus; static;
// Set fixed-size information
[RequiredPrivilege(SE_DEBUG_PRIVILEGE, rpSometimes)]
[RequiredPrivilege(SE_INCREASE_BASE_PRIORITY_PRIVILEGE, rpSometimes)]
class function &Set<T>(
[Access(THREAD_SET_INFORMATION)] const hxThread: IHandle;
InfoClass: TThreadInfoClass;
const Buffer: T
): TNtxStatus; static;
// Read a portion of thread's TEB
class function ReadTeb<T>(
[Access(THREAD_READ_TEB)] const hxThread: IHandle;
out Buffer: T;
Offset: Cardinal = 0
): TNtxStatus; static;
end;
// Assign a thread a name
function NtxQueryNameThread(
[Access(THREAD_QUERY_LIMITED_INFORMATION)] const hxThread: IHandle;
out Name: String
): TNtxStatus;
// Assign a thread a name
function NtxSetNameThread(
[Access(THREAD_SET_LIMITED_INFORMATION)] const hxThread: IHandle;
const Name: String
): TNtxStatus;
// Read content of thread's TEB
function NtxReadTebThread(
[Access(THREAD_READ_TEB)] const hxThread: IHandle;
Offset: Cardinal;
Size: Cardinal;
out Memory: IMemory
): TNtxStatus;
// Query last syscall issued by a thread
function NtxQueryLastSyscallThread(
[Access(THREAD_GET_CONTEXT)] const hxThread: IHandle;
out LastSyscall: TThreadLastSyscall
): TNtxStatus;
// Query exit status of a thread
function NtxQueryExitStatusThread(
[Access(THREAD_QUERY_LIMITED_INFORMATION)] const hxThread: IHandle;
out ExitStatus: NTSTATUS
): TNtxStatus;
{ Manipulation }
// Queue user APC to a thread
function NtxQueueApcThreadEx(
[Access(THREAD_SET_CONTEXT)] const hxThread: IHandle;
Routine: TPsApcRoutine;
[in, opt] Argument1: Pointer = nil;
[in, opt] Argument2: Pointer = nil;
[in, opt] Argument3: Pointer = nil;
Options: TThreadApcOptions = []
): TNtxStatus;
// Get thread context
function NtxGetContextThread(
[Access(THREAD_GET_CONTEXT)] const hxThread: IHandle;
FlagsToQuery: TContextFlags;
out Context: IContext
): TNtxStatus;
// Set thread context
function NtxSetContextThread(
[Access(THREAD_SET_CONTEXT)] const hxThread: IHandle;
[in] Context: PContext
): TNtxStatus;
// Suspend a thread
function NtxSuspendThread(
[Access(THREAD_SUSPEND_RESUME)] const hxThread: IHandle;
[out, opt] PreviousSuspendCount: PCardinal = nil
): TNtxStatus;
// Resume a thread
function NtxResumeThread(
[Access(THREAD_SUSPEND_RESUME)] const hxThread: IHandle;
[out, opt] PreviousSuspendCount: PCardinal = nil
): TNtxStatus;
// Make an alertable thread alerted
function NtxAlertThread(
[Access(THREAD_ALERT)] const hxThread: IHandle
): TNtxStatus;
// Resume a thread into an alerted state
function NtxAlertResumeThread(
[Access(THREAD_SUSPEND_RESUME)] const hxThread: IHandle;
[out, opt] PreviousSuspendCount: PCardinal = nil
): TNtxStatus;
// Terminate a thread
function NtxTerminateThread(
[Access(THREAD_TERMINATE)] const hxThread: IHandle;
ExitStatus: NTSTATUS
): TNtxStatus;
// Resume a thread when the object goes out of scope
function NtxDelayedResumeThread(
[Access(THREAD_SUSPEND_RESUME)] const hxThread: IHandle
): IAutoReleasable;
// Resume a thread into an alerted state when the object goes out of scope
function NtxDelayedAlertResumeThread(
[Access(THREAD_SUSPEND_RESUME)] const hxThread: IHandle
): IAutoReleasable;
// Terminate a thread when the object goes out of scope
function NtxDelayedTerminateThread(
[Access(THREAD_TERMINATE)] const hxThread: IHandle;
ExitStatus: NTSTATUS
): IAutoReleasable;
// Create a thread state change object
[MinOSVersion(OsWin11)]
function NtxCreateThreadState(
out hxThreadState: IHandle;
[Access(THREAD_CHANGE_STATE)] const hxThread: IHandle;
[opt] const ObjectAttributes: IObjectAttributes = nil
): TNtxStatus;
// Suspend or resume a thread via state change
[MinOSVersion(OsWin11)]
function NtxChangeStateThread(
[Access(THREAD_STATE_CHANGE_STATE)] const hxThreadState: IHandle;
[Access(THREAD_CHANGE_STATE)] const hxThread: IHandle;
Action: TThreadStateChangeType
): TNtxStatus;
// Suspend a thread using the best method and resume it automatically later
function NtxSuspendThreadAuto(
[Access(THREAD_CHANGE_STATE)] const hxThread: IHandle;
out Reverter: IAutoReleasable
): TNtxStatus;
// Temporarily suspend all threads in the current process except for the caller
function RtlxSuspendAllThreadsAuto: IAutoReleasable;
{ Creation }
// Create a thread in a process via a legacy syscall
function NtxCreateThread(
out hxThread: IHandle;
[Access(PROCESS_CREATE_THREAD)] const hxProcess: IHandle;
[in] const Context: TContext;
[in] const InitialTeb: TInitialTeb;
CreateSuspended: Boolean;
[opt] const ObjectAttributes: IObjectAttributes = nil;
[out, opt] ThreadInfo: PThreadInfo = nil
): TNtxStatus;
// Create a thread in a process
function NtxCreateThreadEx(
out hxThread: IHandle;
[Access(PROCESS_CREATE_THREAD)] const hxProcess: IHandle;
StartRoutine: TUserThreadStartRoutine;
[in, opt] Argument: Pointer;
CreateFlags: TThreadCreateFlags = 0;
ZeroBits: NativeUInt = 0;
StackSize: NativeUInt = 0;
MaxStackSize: NativeUInt = 0;
[opt] const ObjectAttributes: IObjectAttributes = nil;
[out, opt] ThreadInfo: PThreadInfo = nil
): TNtxStatus;
// Create a thread in a process
function RtlxCreateThread(
out hxThread: IHandle;
[Access(PROCESS_CREATE_THREAD)] const hxProcess: IHandle;
StartRoutine: TUserThreadStartRoutine;
[in, opt] Parameter: Pointer;
CreateSuspended: Boolean = False
): TNtxStatus;
// Subscribe to thread creation/termination notifications
[ThreadSafe]
function RtlxSubscribeThreadNotification(
Callback: TEventCallback<TDllReason>;
out Registration: IAutoReleasable
): TNtxStatus;
implementation
uses
Ntapi.ntstatus, Ntapi.ntobapi, Ntapi.ntmmapi, Ntapi.ntldr, NtUtils.Objects,
NtUtils.Ldr, NtUtils.Processes, DelphiUtils.AutoObjects,
NtUtils.Synchronization;
{$BOOLEVAL OFF}
{$IFOPT R+}{$DEFINE R+}{$ENDIF}
{$IFOPT Q+}{$DEFINE Q+}{$ENDIF}
function NtxOpenThread;
var
hThread: THandle;
ClientId: TClientId;
ObjAttr: TObjectAttributes;
begin
if (TID = NtCurrentThreadId) and
not BitTest(DesiredAccess and ACCESS_SYSTEM_SECURITY) then
begin
// Always succeed on the current thread
hxThread := NtxCurrentThread;
Exit(NtxSuccess);
end;
InitializeObjectAttributes(ObjAttr, nil, HandleAttributes);
ClientId.UniqueProcess := PID;
ClientId.UniqueThread := TID;
Result.Location := 'NtOpenThread';
Result.LastCall.OpensForAccess(DesiredAccess);
Result.Status := NtOpenThread(hThread, DesiredAccess, ObjAttr, ClientId);
if Result.IsSuccess then
hxThread := Auto.CaptureHandle(hThread);
end;
function NtxOpenCurrentThread;
var
hThread: THandle;
Flags: TDuplicateOptions;
begin
// Duplicating the pseudo-handle is more reliable then opening thread by TID
if BitTest(DesiredAccess and MAXIMUM_ALLOWED) and
not BitTest(DesiredAccess and ACCESS_SYSTEM_SECURITY) then
begin
Flags := DUPLICATE_SAME_ACCESS;
DesiredAccess := 0;
end
else
Flags := 0;
Result.Location := 'NtDuplicateObject';
Result.LastCall.OpensForAccess(DesiredAccess);
Result.Status := NtDuplicateObject(NtCurrentProcess, NtCurrentThread,
NtCurrentProcess, hThread, DesiredAccess, HandleAttributes, Flags);
if Result.IsSuccess then
hxThread := Auto.CaptureHandle(hThread);
end;
function NtxGetNextThread;
var
hThread, hNewThread: THandle;
begin
if Assigned(hxThread) then
hThread := hxThread.Handle
else
hThread := 0;
Result.Location := 'NtGetNextThread';
Result.LastCall.Expects<TProcessAccessMask>(PROCESS_QUERY_INFORMATION);
Result.LastCall.OpensForAccess(DesiredAccess);
Result.Status := NtGetNextThread(HandleOrDefault(hxProcess), hThread,
DesiredAccess, HandleAttributes, 0, hNewThread);
if Result.IsSuccess then
hxThread := Auto.CaptureHandle(hNewThread);
end;
function NtxIterateGetNextThread;
var
hxThread: IHandle;
begin
hxThread := nil;
Result := NtxAuto.Iterate<IHandle>(Status,
function (out Current: IHandle): TNtxStatus
begin
// Advance to the next thread handle
Result := NtxGetNextThread(hxProcess, hxThread, DesiredAccess,
HandleAttributes);
if not Result.IsSuccess then
Exit;
Current := hxThread;
end
);
end;
function NtxOpenProcessByThreadId;
var
hxThread: IHandle;
Info: TThreadBasicInformation;
begin
Result := NtxOpenThread(hxThread, TID, THREAD_QUERY_LIMITED_INFORMATION);
if not Result.IsSuccess then
Exit;
Result := NtxThread.Query(hxThread, ThreadBasicInformation, Info);
if not Result.IsSuccess then
Exit;
Result := NtxOpenProcess(hxProcess, Info.ClientId.UniqueProcess,
DesiredAccess, HandleAttributes);
end;
function NtxQueryThread;
var
Required: Cardinal;
begin
Result.Location := 'NtQueryInformationThread';
Result.LastCall.UsesInfoClass(InfoClass, icQuery);
Result.LastCall.Expects(ExpectedThreadQueryAccess(InfoClass));
xMemory := Auto.AllocateDynamic(InitialBuffer);
repeat
Required := 0;
Result.Status := NtQueryInformationThread(HandleOrDefault(hxThread),
InfoClass, xMemory.Data, xMemory.Size, @Required);
until not NtxExpandBufferEx(Result, xMemory, Required, GrowthMethod);
end;
function NtxSetThread;
begin
Result.Location := 'NtSetInformationThread';
Result.LastCall.UsesInfoClass(InfoClass, icSet);
Result.LastCall.Expects(ExpectedThreadSetAccess(InfoClass));
Result.LastCall.ExpectedPrivilege := ExpectedThreadSetPrivilege(InfoClass);
// Additional expected access
case InfoClass of
ThreadImpersonationToken:
Result.LastCall.Expects<TTokenAccessMask>(TOKEN_IMPERSONATE);
ThreadCpuAccountingInformation:
Result.LastCall.Expects<TSessionAccessMask>(SESSION_MODIFY_ACCESS);
ThreadAttachContainer:
Result.LastCall.Expects<TJobObjectAccessMask>(JOB_OBJECT_IMPERSONATE);
end;
Result.Status := NtSetInformationThread(HandleOrDefault(hxThread), InfoClass,
Buffer, BufferSize);
end;
class function NtxThread.Query<T>;
begin
Result.Location := 'NtQueryInformationThread';
Result.LastCall.UsesInfoClass(InfoClass, icQuery);
Result.LastCall.Expects(ExpectedThreadQueryAccess(InfoClass));
Result.Status := NtQueryInformationThread(HandleOrDefault(hxThread),
InfoClass, @Buffer, SizeOf(Buffer), nil);
end;
class function NtxThread.ReadTeb<T>;
var
TebInfo: TThreadTebInformation;
begin
TebInfo.TebInformation := @Buffer;
TebInfo.TebOffset := Offset;
TebInfo.BytesToRead := SizeOf(Buffer);
Result := NtxThread.Query(hxThread, ThreadTebInformation, TebInfo);
end;
class function NtxThread.&Set<T>;
begin
Result := NtxSetThread(hxThread, InfoClass, @Buffer, SizeOf(Buffer));
end;
function NtxQueryNameThread;
var
Buffer: IMemory<PNtUnicodeString>;
begin
Result := NtxQueryThread(hxThread, ThreadNameInformation, IMemory(Buffer));
if Result.IsSuccess then
Name := Buffer.Data.ToString;
end;
function NtxSetNameThread;
var
NameStr: TNtUnicodeString;
begin
Result := RtlxInitUnicodeString(NameStr, Name);
if not Result.IsSuccess then
Exit;
Result := NtxThread.Set(hxThread, ThreadNameInformation, NameStr);
end;
function NtxReadTebThread;
var
TebInfo: TThreadTebInformation;
begin
Memory := Auto.AllocateDynamic(Size);
// Describe the read request
TebInfo.TebInformation := Memory.Data;
TebInfo.TebOffset := Offset;
TebInfo.BytesToRead := Size;
// Query TEB content
Result := NtxThread.Query(hxThread, ThreadTebInformation, TebInfo);
if not Result.IsSuccess then
Memory := nil;
end;
function NtxQueryLastSyscallThread;
var
LastSyscallWin7: TThreadLastSyscallWin7;
begin
if RtlOsVersionAtLeast(OsWin8) then
begin
LastSyscall := Default(TThreadLastSyscall);
Result := NtxThread.Query(hxThread, ThreadLastSystemCall, LastSyscall);
end
else
begin
LastSyscallWin7 := Default(TThreadLastSyscallWin7);
Result := NtxThread.Query(hxThread, ThreadLastSystemCall, LastSyscallWin7);
if Result.IsSuccess then
begin
LastSyscall.FirstArgument := LastSyscallWin7.FirstArgument;
LastSyscall.SystemCallNumber := LastSyscallWin7.SystemCallNumber;
LastSyscall.WaitTime := 0;
end;
end;
end;
function NtxQueryExitStatusThread;
var
Info: TThreadBasicInformation;
begin
Result := NtxThread.Query(hxThread, ThreadBasicInformation, Info);
if Result.IsSuccess then
ExitStatus := Info.ExitStatus;
end;
function NtxQueueApcThreadEx;
var
Flags: THandle;
begin
if (apcForceSignal in Options) and RtlOsVersionAtLeast(OsWin10RS5) then
Flags := APC_FORCE_THREAD_SIGNAL
else
Flags := 0;
{$IFDEF Win64}
// Encode the pointer the same way RtlQueueApcWow64Thread does
if apcWoW64 in Options then
UIntPtr(@Routine) := UIntPtr(-IntPtr(@Routine)) shl 2;
{$ENDIF}
Result.Location := 'NtQueueApcThreadEx';
Result.LastCall.Expects<TThreadAccessMask>(THREAD_SET_CONTEXT);
Result.Status := NtQueueApcThreadEx(HandleOrDefault(hxThread), Flags, Routine,
Argument1, Argument2, Argument3);
end;
function NtxGetContextThread;
begin
IMemory(Context) := Auto.AllocateDynamic(SizeOf(TContext));
Context.Data.ContextFlags := FlagsToQuery;
Result.Location := 'NtGetContextThread';
Result.LastCall.Expects<TThreadAccessMask>(THREAD_GET_CONTEXT);
Result.Status := NtGetContextThread(HandleOrDefault(hxThread), Context.Data);
end;
function NtxSetContextThread;
begin
Result.Location := 'NtSetContextThread';
Result.LastCall.Expects<TThreadAccessMask>(THREAD_SET_CONTEXT);
Result.Status := NtSetContextThread(HandleOrDefault(hxThread), Context);
end;
function NtxSuspendThread;
begin
Result.Location := 'NtSuspendThread';
Result.LastCall.Expects<TThreadAccessMask>(THREAD_SUSPEND_RESUME);
Result.Status := NtSuspendThread(HandleOrDefault(hxThread),
PreviousSuspendCount);
end;
function NtxResumeThread;
begin
Result.Location := 'NtResumeThread';
Result.LastCall.Expects<TThreadAccessMask>(THREAD_SUSPEND_RESUME);
Result.Status := NtResumeThread(HandleOrDefault(hxThread), PreviousSuspendCount);
end;
function NtxAlertThread;
begin
Result.Location := 'NtAlertThread';
Result.LastCall.Expects<TThreadAccessMask>(THREAD_ALERT);
Result.Status := NtAlertThread(HandleOrDefault(hxThread));
end;
function NtxAlertResumeThread;
begin
Result.Location := 'NtAlertResumeThread';
Result.LastCall.Expects<TThreadAccessMask>(THREAD_SUSPEND_RESUME);
Result.Status := NtAlertResumeThread(HandleOrDefault(hxThread),
PreviousSuspendCount);
end;
function NtxTerminateThread;
begin
Result.Location := 'NtTerminateThread';
Result.LastCall.Expects<TThreadAccessMask>(THREAD_TERMINATE);
Result.Status := NtTerminateThread(HandleOrDefault(hxThread), ExitStatus);
end;
function NtxDelayedResumeThread;
begin
Result := Auto.Delay(
procedure
begin
NtxResumeThread(hxThread);
end
);
end;
function NtxDelayedAlertResumeThread;
begin
Result := Auto.Delay(
procedure
begin
NtxAlertResumeThread(hxThread);
end
);
end;
function NtxDelayedTerminateThread;
begin
Result := Auto.Delay(
procedure
begin
NtxTerminateThread(hxThread, ExitStatus);
end
);
end;
function NtxCreateThreadState;
var
ObjAttr: PObjectAttributes;
hThreadState: THandle;
begin
Result := LdrxCheckDelayedImport(delayed_NtCreateThreadStateChange);
if not Result.IsSuccess then
Exit;
Result := AttributesRefOrNil(ObjAttr, ObjectAttributes);
if not Result.IsSuccess then
Exit;
Result.Location := 'NtCreateThreadStateChange';
Result.LastCall.Expects<TThreadAccessMask>(THREAD_SET_INFORMATION);
Result.Status := NtCreateThreadStateChange(
hThreadState,
AccessMaskOverride(THREAD_STATE_CHANGE_STATE, ObjectAttributes),
ObjAttr,
HandleOrDefault(hxThread),
0
);
if Result.IsSuccess then
hxThreadState := Auto.CaptureHandle(hThreadState);
end;
function NtxChangeStateThread;
begin
Result := LdrxCheckDelayedImport(delayed_NtChangeThreadState);
if not Result.IsSuccess then
Exit;
Result.Location := 'NtChangeThreadState';
Result.LastCall.UsesInfoClass(Action, icPerform);
Result.LastCall.Expects<TThreadStateAccessMask>(THREAD_STATE_CHANGE_STATE);
Result.LastCall.Expects<TThreadAccessMask>(THREAD_SUSPEND_RESUME);
Result.Status := NtChangeThreadState(HandleOrDefault(hxThreadState),
HandleOrDefault(hxThread), Action, nil, 0, 0);
end;
function NtxSuspendThreadAuto;
var
hxThreadState: IHandle;
begin
// Try state change-based suspension first
Result := NtxCreateThreadState(hxThreadState, hxThread);
if Result.IsSuccess then
begin
Result := NtxChangeStateThread(hxThreadState, hxThread,
ThreadStateChangeSuspend);
if Result.IsSuccess then
begin
// Releasing the state change handle will resume the thread
Reverter := hxThreadState;
Exit;
end;
end;
// Fall back to classic suspension
Result := NtxSuspendThread(hxThread);
if Result.IsSuccess then
Reverter := NtxDelayedResumeThread(hxThread);
end;
function RtlxSuspendAllThreadsAuto;
var
Reverter: IAutoReleasable;
Reverters: TArray<IAutoReleasable>;
BasicInfo: TThreadBasicInformation;
hxThread: IHandle;
Status: TNtxStatus;
begin
hxThread := nil;
Reverters := nil;
while NtxGetNextThread(NtxCurrentProcess, hxThread,
THREAD_SUSPEND_RESUME or THREAD_QUERY_LIMITED_INFORMATION).IsSuccess do
begin
// Determine thread ID
Status := NtxThread.Query(hxThread, ThreadBasicInformation, BasicInfo);
if not Status.IsSuccess then
Continue;
// Skip the current thread
if BasicInfo.ClientId.UniqueThread = NtCurrentThreadId then
Continue;
// Suspend and save the reverter
Status := NtxSuspendThreadAuto(hxThread, Reverter);
if Status.IsSuccess then
begin
SetLength(Reverters, Length(Reverters) + 1);
Reverters[High(Reverters)] := Reverter;
end;
end;
Result := Auto.Copy(Reverters);
end;
function NtxCreateThread;
var
ObjAttr: PObjectAttributes;
hThread: THandle;
ClientId: TClientId;
BasicInfo: TThreadBasicInformation;
begin
Result := AttributesRefOrNil(ObjAttr, ObjectAttributes);
if not Result.IsSuccess then
Exit;
Result.Location := 'NtCreateThread';
Result.LastCall.Expects<TProcessAccessMask>(PROCESS_CREATE_THREAD);
Result.Status := NtCreateThread(
hThread,
AccessMaskOverride(THREAD_ALL_ACCESS, ObjectAttributes),
ObjAttr,
HandleOrDefault(hxProcess),
ClientId,
Context,
InitialTeb,
CreateSuspended
);
if not Result.IsSuccess then
Exit;
hxThread := Auto.CaptureHandle(hThread);
if Assigned(ThreadInfo) then
begin
ThreadInfo.ClientID := ClientId;
// Determine the TEB address when possible
if NtxThread.Query(hxThread, ThreadBasicInformation,
BasicInfo).IsSuccess then
ThreadInfo.TebAddress := BasicInfo.TebBaseAddress
else
ThreadInfo.TebAddress := nil;
end;
end;
function NtxCreateThreadEx;
var
ObjAttr: PObjectAttributes;
hThread: THandle;
PsAttributes: IMemory<PPsAttributeList>;
PsAttribute: PPsAttribute;
begin
Result := AttributesRefOrNil(ObjAttr, ObjectAttributes);
if not Result.IsSuccess then
Exit;
if Assigned(ThreadInfo) then
begin
IMemory(PsAttributes) := Auto.AllocateDynamic(
TPsAttributeList.SizeOfCount(2));
PsAttributes.Data.TotalLength := PsAttributes.Size;
PsAttribute := @PsAttributes.Data.Attributes[0];
// Retrieve the client ID
PsAttribute.Attribute := PS_ATTRIBUTE_CLIENT_ID;
PsAttribute.Size := SizeOf(TClientId);
Pointer(PsAttribute.Value) := @ThreadInfo.ClientId;
Inc(PsAttribute);
// Retrieve the TEB address
PsAttribute.Attribute := PS_ATTRIBUTE_TEB_ADDRESS;
PsAttribute.Size := SizeOf(PTeb);
Pointer(PsAttribute.Value) := @ThreadInfo.TebAddress;
end
else
PsAttributes := nil;
Result.Location := 'NtCreateThreadEx';
Result.LastCall.Expects<TProcessAccessMask>(PROCESS_CREATE_THREAD);
Result.Status := NtCreateThreadEx(
hThread,
AccessMaskOverride(THREAD_ALL_ACCESS, ObjectAttributes),
ObjAttr,
HandleOrDefault(hxProcess),
StartRoutine,
Argument,
CreateFlags,
ZeroBits,
StackSize,
MaxStackSize,
Auto.RefOrNil<PPsAttributeList>(PsAttributes)
);
if Result.IsSuccess then
hxThread := Auto.CaptureHandle(hThread);
end;
function RtlxCreateThread;
var
hThread: THandle;
begin
Result.Location := 'RtlCreateUserThread';
Result.LastCall.Expects<TProcessAccessMask>(PROCESS_CREATE_THREAD);
Result.Status := RtlCreateUserThread(HandleOrDefault(hxProcess), nil,
CreateSuspended, 0, 0, 0, StartRoutine, Parameter, hThread, nil);
if Result.IsSuccess then
hxThread := Auto.CaptureHandle(hThread);
end;
var
// A list of registered thread creation/termination callbacks
RtlxpThreadCallbacks: TAutoEvent<TDllReason>;
RtlxpThreadCallbackDispatcherInit: TRtlRunOnce;
RtlxpThreadCallbackDispatcherAttachLdrEntry: PLdrDataTableEntry;
RtlxpThreadCallbackDispatcherUnload: IAutoReleasable;
// A dispatcher callback that is binary compatible with DllMain routines
function RtlxpThreadCallbackDispatcher(
[in] DllHandle: Pointer;
Reason: TDllReason;
[in, opt] Context: PContext
): Boolean; stdcall;
begin
RtlxpThreadCallbacks.Invoke(Reason);
Result := True;
end;
procedure RtlxpRemoveThreadCallbackDispatcher;
var
LdrEntry: PLdrDataTableEntry;
Status: TNtxStatus;
begin
// Verify that the LDR entry we attached to is still available
Status := LdrxFindModuleEntry(LdrEntry,
function (Entry: PLdrDataTableEntry): Boolean
begin
Result := (Entry = RtlxpThreadCallbackDispatcherAttachLdrEntry);
end
);
if Status.IsSuccess then
begin
// Detach the distaptcher
LdrEntry.Flags := LdrEntry.Flags and not LDRP_PROCESS_ATTACH_CALLED;
LdrEntry.EntryPoint := nil;
end;
end;
function RtlxSubscribeThreadNotification;
var
Init: IAcquiredRunOnce;
Lock: IAutoReleasable;
LdrEntry: PLdrDataTableEntry;
begin
if RtlxRunOnceBegin(@RtlxpThreadCallbackDispatcherInit, Init) then
begin
// The module loader invokes DLL entrypoints for thread attaching/detaching.
// Unfortunately, there doesn't seem to be alternative mechanisms available
// for non-DLL code. As a solution, adjust the PEB Ldr data and pretend
// to be ntdll's entrypoint (which is unused) to receive notifications.
Result := LdrxCheckDelayedModule(delayed_ntdll);
if not Result.IsSuccess then
Exit;
// Locate ntdll LDR entry
Result := LdrxFindModuleEntry(LdrEntry, LdrxEntryStartsAt(
delayed_ntdll.DllAddress));
if not Result.IsSuccess then
Exit;
if Assigned(LdrEntry.EntryPoint) then
begin
// Already in use
Result.Location := 'RtlxSubscribeThreadNotification';
Result.Status := STATUS_UNSUCCESSFUL;
Exit;
end;
// Install the dispatcher
LdrxAcquireLoaderLock(Lock);
LdrEntry.EntryPoint := @RtlxpThreadCallbackDispatcher;
LdrEntry.Flags := LdrEntry.Flags or LDRP_PROCESS_ATTACH_CALLED;
Lock := nil;
// Clear the dispatcher on module unload
RtlxpThreadCallbackDispatcherAttachLdrEntry := LdrEntry;
RtlxpThreadCallbackDispatcherUnload := Auto.Delay(
RtlxpRemoveThreadCallbackDispatcher);
Init.Complete;
end;
// Register the callback in the auto-event list
Registration := RtlxpThreadCallbacks.Subscribe(Callback);
Result := NtxSuccess;
end;
end.