-
Notifications
You must be signed in to change notification settings - Fork 35
/
NtUtils.Processes.Create.Native.pas
903 lines (747 loc) · 26.4 KB
/
NtUtils.Processes.Create.Native.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
unit NtUtils.Processes.Create.Native;
{
The module provides support for process creation via Native API.
}
interface
uses
Ntapi.ntrtl, Ntapi.ntseapi, Ntapi.Versions, NtUtils, NtUtils.Processes.Create,
DelphiUtils.AutoObjects;
type
IRtlUserProcessParameters = IMemory<PRtlUserProcessParameters>;
// Allocate user process parameters
function RtlxCreateProcessParameters(
const Options: TCreateProcessOptions;
out xMemory: IRtlUserProcessParameters
): TNtxStatus;
// Create a new process via RtlCreateUserProcess
[SupportedOption(spoCurrentDirectory)]
[SupportedOption(spoSuspended)]
[SupportedOption(spoInheritHandles)]
[SupportedOption(spoEnvironment)]
[SupportedOption(spoSecurity)]
[SupportedOption(spoWindowMode)]
[SupportedOption(spoWindowTitle)]
[SupportedOption(spoStdHandles)]
[SupportedOption(spoDesktop)]
[SupportedOption(spoToken)]
[SupportedOption(spoParentProcess)]
[SupportedOption(spoDebugPort)]
[SupportedOption(spoDetectManifest)]
[RequiredPrivilege(SE_ASSIGN_PRIMARY_TOKEN_PRIVILEGE, rpSometimes)]
function RtlxCreateUserProcess(
const Options: TCreateProcessOptions;
out Info: TProcessInfo
): TNtxStatus;
// Create a new process via RtlCreateUserProcessEx
[MinOSVersion(OsWin10RS2)]
[SupportedOption(spoCurrentDirectory)]
[SupportedOption(spoSuspended)]
[SupportedOption(spoInheritHandles)]
[SupportedOption(spoEnvironment)]
[SupportedOption(spoSecurity)]
[SupportedOption(spoWindowMode)]
[SupportedOption(spoWindowTitle)]
[SupportedOption(spoStdHandles)]
[SupportedOption(spoDesktop)]
[SupportedOption(spoToken)]
[SupportedOption(spoParentProcess)]
[SupportedOption(spoJob)]
[SupportedOption(spoDebugPort)]
[SupportedOption(spoDetectManifest)]
[RequiredPrivilege(SE_ASSIGN_PRIMARY_TOKEN_PRIVILEGE, rpSometimes)]
function RtlxCreateUserProcessEx(
const Options: TCreateProcessOptions;
out Info: TProcessInfo
): TNtxStatus;
// Create a new process via NtCreateUserProcess
[SupportedOption(spoCurrentDirectory)]
[SupportedOption(spoSuspended)]
[SupportedOption(spoInheritHandles)]
[SupportedOption(spoBreakawayFromJob)]
[SupportedOption(spoForceBreakaway)]
[SupportedOption(spoInheritConsole)]
[SupportedOption(spoEnvironment)]
[SupportedOption(spoObjectInherit)]
[SupportedOption(spoDesiredAccess)]
[SupportedOption(spoSecurity)]
[SupportedOption(spoWindowMode)]
[SupportedOption(spoWindowTitle)]
[SupportedOption(spoStdHandles)]
[SupportedOption(spoDesktop)]
[SupportedOption(spoToken)]
[SupportedOption(spoParentProcess)]
[SupportedOption(spoJob)]
[SupportedOption(spoDebugPort)]
[SupportedOption(spoHandleList)]
[SupportedOption(spoMemoryReserve)]
[SupportedOption(spoPriorityClass)]
[SupportedOption(spoChildPolicy)]
[SupportedOption(spoLPAC)]
[SupportedOption(spoPackageBreakaway)]
[SupportedOption(spoProtection)]
[SupportedOption(spoSafeOpenPromptOriginClaim)]
[SupportedOption(spoAdditionalFileAccess)]
[SupportedOption(spoDetectManifest)]
[RequiredPrivilege(SE_ASSIGN_PRIMARY_TOKEN_PRIVILEGE, rpSometimes)]
[RequiredPrivilege(SE_TCB_PRIVILEGE, rpSometimes)]
function NtxCreateUserProcess(
const Options: TCreateProcessOptions;
out Info: TProcessInfo
): TNtxStatus;
implementation
uses
Ntapi.WinNt, Ntapi.ntdef, Ntapi.ntpsapi, Ntapi.ntstatus, Ntapi.ntioapi,
Ntapi.ntpebteb, Ntapi.ProcessThreadsApi, Ntapi.ConsoleApi, NtUtils.Threads,
NtUtils.Files, NtUtils.Objects, NtUtils.Ldr, NtUtils.Tokens,
NtUtils.Processes.Info, NtUtils.Files.Open, NtUtils.Manifests;
{$BOOLEVAL OFF}
{$IFOPT R+}{$DEFINE R+}{$ENDIF}
{$IFOPT Q+}{$DEFINE Q+}{$ENDIF}
{ Process Parameters & Attributes }
type
TAutoUserProcessParams = class (TCustomAutoMemory, IMemory, IAutoPointer, IAutoReleasable)
procedure Release; override;
end;
procedure TAutoUserProcessParams.Release;
begin
if Assigned(FData) then
RtlDestroyProcessParameters(FData);
FData := nil;
inherited;
end;
function RtlxCreateProcessParameters;
var
Buffer: PRtlUserProcessParameters;
ApplicationWin32: String;
ApplicationWin32Str, CommandLineStr, CurrentDirStr, DesktopStr,
WindowTitleStr: TNtUnicodeString;
WindowTitleStrRef: PNtUnicodeString;
begin
// Keep the string from the Options.ApplicationWin32() call alive
ApplicationWin32 := Options.ApplicationWin32;
Result := RtlxInitUnicodeString(ApplicationWin32Str, ApplicationWin32);
if not Result.IsSuccess then
Exit;
Result := RtlxInitUnicodeString(CommandLineStr, Options.CommandLine);
if not Result.IsSuccess then
Exit;
Result := RtlxInitUnicodeString(CurrentDirStr, Options.CurrentDirectory);
if not Result.IsSuccess then
Exit;
Result := RtlxInitUnicodeString(DesktopStr, Options.Desktop);
if not Result.IsSuccess then
Exit;
if (poForceWindowTitle in Options.Flags) or (Options.WindowTitle <> '') then
begin
Result := RtlxInitUnicodeString(WindowTitleStr, Options.WindowTitle);
if not Result.IsSuccess then
Exit;
WindowTitleStrRef := @WindowTitleStr
end
else
WindowTitleStrRef := nil;
Result.Location := 'RtlCreateProcessParametersEx';
Result.Status := RtlCreateProcessParametersEx(
Buffer,
ApplicationWin32Str,
nil, // DllPath
CurrentDirStr.RefOrNil,
@CommandLineStr,
Auto.RefOrNil<PEnvironment>(Options.Environment),
WindowTitleStrRef,
DesktopStr.RefOrNil,
nil, // ShellInfo
nil, // RuntimeData
RTL_USER_PROC_PARAMS_NORMALIZED
);
if not Result.IsSuccess then
Exit;
// Make sure zero-length strings use null pointers
Buffer.DLLPath := Default(TNtUnicodeString);
Buffer.ShellInfo := Default(TNtUnicodeString);
Buffer.RuntimeData := Default(TNtUnicodeString);
if Buffer.WindowTitle.Length = 0 then
Buffer.WindowTitle := Default(TNtUnicodeString);
IMemory(xMemory) := TAutoUserProcessParams.Capture(Buffer,
Buffer.MaximumLength + Buffer.EnvironmentSize);
// Adjust window mode flags
if poUseWindowMode in Options.Flags then
begin
xMemory.Data.WindowFlags := xMemory.Data.WindowFlags or STARTF_USESHOWWINDOW;
xMemory.Data.ShowWindowFlags := Options.WindowMode;
end;
// Standard I/O handles
if poUseStdHandles in Options.Flags then
begin
xMemory.Data.WindowFlags := xMemory.Data.WindowFlags or STARTF_USESTDHANDLES;
xMemory.Data.StandardInput := HandleOrDefault(Options.hxStdInput);
xMemory.Data.StandardOutput := HandleOrDefault(Options.hxStdOutput);
xMemory.Data.StandardError := HandleOrDefault(Options.hxStdError);
end;
end;
type
TPsAttributesRecord = record
private
Source: TCreateProcessOptions;
FImageName: String;
FClientId: TClientId;
FTebAddress: PTeb;
FHandleList: TArray<THandle>;
FStdHandleInfo: TPsStdHandleInfo;
hxExpandedToken: IHandle;
hJob: THandle;
PackagePolicy: TProcessAllPackagesFlags;
PsProtection: TPsProtection;
SeSafePromptClaim: TSeSafeOpenPromptResults;
Buffer: IMemory<PPsAttributeList>;
function GetData: PPsAttributeList;
public
function Create(const Options: TCreateProcessOptions): TNtxStatus;
property Data: PPsAttributeList read GetData;
property ClientId: TClientId read FClientId;
property ImageName: String read FImageName;
property TebAddress: PTeb read FTebAddress;
end;
{ TPsAttributesRecord }
function RtlxWin32ToNativeProtection(
Win32Protection: TProtectionLevel;
out NativeProtection: TPsProtection
): TNtxStatus;
const
PROTECTION_TYPE: array [TProtectionLevel] of TPsProtectionType = (
PsProtectedTypeProtectedLight, PsProtectedTypeProtected,
PsProtectedTypeProtectedLight, PsProtectedTypeProtectedLight,
PsProtectedTypeProtectedLight, PsProtectedTypeProtected,
PsProtectedTypeProtectedLight, PsProtectedTypeProtected,
PsProtectedTypeProtectedLight
);
PROTECTION_SIGNER: array [TProtectionLevel] of TPsProtectionSigner = (
PsProtectedSignerWinTcb, PsProtectedSignerWindows, PsProtectedSignerWindows,
PsProtectedSignerAntimalware, PsProtectedSignerLsa, PsProtectedSignerWinTcb,
PsProtectedSignerCodeGen, PsProtectedSignerAuthenticode,
PsProtectedSignerApp
);
begin
if (Win32Protection >= Low(TProtectionLevel)) and
(Win32Protection <= High(TProtectionLevel)) then
begin
Result := NtxSuccess;
NativeProtection := Byte(PROTECTION_TYPE[Win32Protection]) or
(Byte(PROTECTION_SIGNER[Win32Protection]) shl PS_PROTECTED_SIGNER_SHIFT);
end
else if Win32Protection = PROTECTION_LEVEL_SAME then
Result := NtxProcess.Query(NtxCurrentProcess, ProcessProtectionInformation,
NativeProtection)
else
begin
Result.Location := 'RtlxWin32ToNativeProtection';
Result.Status := STATUS_INVALID_PARAMETER;
end;
end;
function TPsAttributesRecord.Create;
var
Count, j: Integer;
Attribute: PPsAttribute;
begin
// Always use Image Name, Client ID, and TEB address
Count := 3;
if Assigned(Options.hxToken) then
Inc(Count);
if Assigned(Options.hxParentProcess) then
Inc(Count);
if Assigned(Options.hxDebugPort) then
Inc(Count);
if Length(Options.HandleList) > 0 then
Inc(Count);
if Assigned(Options.hxJob) then
Inc(Count);
if Length(Options.MemoryReserve) > 0 then
Inc(Count);
if Options.PriorityClass <> PROCESS_PRIORITY_CLASS_UNKNOWN then
Inc(Count);
if HasAny(Options.ChildPolicy) then
Inc(Count);
if poLPAC in Options.Flags then
Inc(Count);
if HasAny(Options.PackageBreakaway) then
Inc(Count);
if poUseProtection in Options.Flags then
Inc(Count);
if poInheritConsole in Options.Flags then
Inc(Count);
if poUseSafeOpenPromptOriginClaim in Options.Flags then
Inc(Count);
Source := Options;
IMemory(Buffer) := Auto.AllocateDynamic(TPsAttributeList.SizeOfCount(Count));
Data.TotalLength := Buffer.Size;
Attribute := @Data.Attributes[0];
// Image name
FImageName := Options.ApplicationNative;
Attribute.Attribute := PS_ATTRIBUTE_IMAGE_NAME;
Attribute.Size := StringSizeNoZero(FImageName);
Pointer(Attribute.Value) := PWideChar(FImageName);
Inc(Attribute);
// Client ID
Attribute.Attribute := PS_ATTRIBUTE_CLIENT_ID;
Attribute.Size := SizeOf(TClientId);
Pointer(Attribute.Value) := @FClientId;
Inc(Attribute);
// TEB address
Attribute.Attribute := PS_ATTRIBUTE_TEB_ADDRESS;
Attribute.Size := SizeOf(PTeb);
Pointer(Attribute.Value) := @FTebAddress;
Inc(Attribute);
// Token
if Assigned(Source.hxToken) then
begin
// Allow use of pseudo-handles
hxExpandedToken := Options.hxToken;
Result := NtxExpandToken(hxExpandedToken, TOKEN_ASSIGN_PRIMARY);
if not Result.IsSuccess then
Exit;
Attribute.Attribute := PS_ATTRIBUTE_TOKEN;
Attribute.Size := SizeOf(THandle);
Attribute.Value := hxExpandedToken.Handle;
Inc(Attribute);
end;
// Parent process
if Assigned(Source.hxParentProcess) then
begin
Attribute.Attribute := PS_ATTRIBUTE_PARENT_PROCESS;
Attribute.Size := SizeOf(THandle);
Attribute.Value := Source.hxParentProcess.Handle;
Inc(Attribute);
end;
// Debug port
if Assigned(Source.hxDebugPort) then
begin
Attribute.Attribute := PS_ATTRIBUTE_DEBUG_PORT;
Attribute.Size := SizeOf(THandle);
Attribute.Value := Source.hxDebugPort.Handle;
Inc(Attribute);
end;
// Handle list
if Length(Source.HandleList) > 0 then
begin
SetLength(FHandleList, Length(Source.HandleList));
for j := 0 to High(FHandleList) do
FHandleList[j] := Source.HandleList[j].Handle;
Attribute.Attribute := PS_ATTRIBUTE_HANDLE_LIST;
Attribute.Size := SizeOf(THandle) * Length(FHandleList);
Pointer(Attribute.Value) := Pointer(FHandleList);
Inc(Attribute);
end;
// Job object
if Assigned(Source.hxJob) then
begin
hJob := Source.hxJob.Handle;
Attribute.Attribute := PS_ATTRIBUTE_JOB_LIST;
Attribute.Size := SizeOf(THandle);
Pointer(Attribute.Value) := @hJob;
Inc(Attribute);
end;
// Memory reserve
if Length(Options.MemoryReserve) > 0 then
begin
Attribute.Attribute := PS_ATTRIBUTE_MEMORY_RESERVE;
Attribute.Size := SizeOf(TPsMemoryReserve) * Length(Source.MemoryReserve);
Pointer(Attribute.Value) := @Source.MemoryReserve[0];
Inc(Attribute);
end;
// Priority
if Options.PriorityClass <> PROCESS_PRIORITY_CLASS_UNKNOWN then
begin
Attribute.Attribute := PS_ATTRIBUTE_PRIORITY_CLASS;
Attribute.Size := SizeOf(Byte);
Pointer(Attribute.Value) := @Source.PriorityClass;
Inc(Attribute);
end;
// Child process policy
if HasAny(Source.ChildPolicy) then
begin
Attribute.Attribute := PS_ATTRIBUTE_CHILD_PROCESS_POLICY;
Attribute.Size := SizeOf(TProcessChildFlags);
Pointer(Attribute.Value) := @Source.ChildPolicy;
Inc(Attribute);
end;
// Low-privileged AppContainer
if poLPAC in Options.Flags then
begin
PackagePolicy := PROCESS_CREATION_ALL_APPLICATION_PACKAGES_OPT_OUT;
Attribute.Attribute := PS_ATTRIBUTE_ALL_APPLICATION_PACKAGES_POLICY;
Attribute.Size := SizeOf(TProcessAllPackagesFlags);
Pointer(Attribute.Value) := @PackagePolicy;
Inc(Attribute);
end;
// Package breakaway (aka Desktop App Policy
if HasAny(Options.PackageBreakaway) then
begin
Attribute.Attribute := PS_ATTRIBUTE_DESKTOP_APP_POLICY;
Attribute.Size := SizeOf(TProcessDesktopAppFlags);
Pointer(Attribute.Value) := @Options.PackageBreakaway;
Inc(Attribute);
end;
// Process protection
if poUseProtection in Options.Flags then
begin
Result := RtlxWin32ToNativeProtection(Options.Protection, PsProtection);
if not Result.IsSuccess then
Exit;
Attribute.Attribute := PS_ATTRIBUTE_PROTECTION_LEVEL;
Attribute.Size := SizeOf(TPsProtection);
Attribute.Value := PsProtection;
Inc(Attribute);
end;
// Std handle info
if poInheritConsole in Options.Flags then
begin
if poUseStdHandles in Options.Flags then
FStdHandleInfo.Flags := PS_STD_STATE_NEVER_DUPLICATE
else
FStdHandleInfo.Flags := PS_STD_STATE_REQUEST_DUPLICATE;
FStdHandleInfo.StdHandleSubsystemType := IMAGE_SUBSYSTEM_WINDOWS_CUI;
Attribute.Attribute := PS_ATTRIBUTE_STD_HANDLE_INFO;
Attribute.Size := SizeOf(TPsStdHandleInfo);
Pointer(Attribute.Value) := @FStdHandleInfo;
Inc(Attribute);
end;
// Safe open prompt origin claim
if poUseSafeOpenPromptOriginClaim in Options.Flags then
begin
SeSafePromptClaim := Default(TSeSafeOpenPromptResults);
SeSafePromptClaim.Results := Options.SafeOpenPromptOriginClaimResult;
SeSafePromptClaim.SetPath(Options.SafeOpenPromptOriginClaimPath);
Attribute.Attribute := PS_ATTRIBUTE_SAFE_OPEN_PROMPT_ORIGIN_CLAIM;
Attribute.Size := SizeOf(TSeSafeOpenPromptResults);
Pointer(Attribute.Value) := @SeSafePromptClaim;
end;
Result := NtxSuccess;
end;
function TPsAttributesRecord.GetData;
begin
Result := Buffer.Data;
end;
function RtlxDetectManifestAndSaveAddresses(
const Options: TCreateProcessOptions;
var Info: TProcessInfo
): TNtxStatus;
var
Addresses: TProcessAddresses;
hxSection: IHandle;
ManifestRva: TMemory;
begin
Result := NtxQueryAddressesProcess(Info.hxProcess, Addresses);
if not Result.IsSuccess then
Exit;
// Save PEB
if Assigned(Addresses.PebAddressNative) then
begin
Include(Info.ValidFields, piPebAddress);
Info.PebAddressNative := Addresses.PebAddressNative;
end;
// Save WoW64 PEB
if Assigned(Addresses.PebAddressWoW64) then
begin
Include(Info.ValidFields, piPebAddressWoW64);
Info.PebAddressWoW64 := Addresses.PebAddressWoW64;
end;
// Save Image Base
Include(Info.ValidFields, piImageBase);
Info.ImageBaseAddress := Addresses.ImageBase;
hxSection := nil;
// Parse the file trying to locate the embedded manifest
Result := RtlxFindManifestInFile(FileParameters.UseFileName(
Options.ApplicationNative), ManifestRva);
if Result.IsSuccess then
begin
// Convert RVA to VA and save the result
Inc(PByte(ManifestRva.Address), UIntPtr(Info.ImageBaseAddress));
Include(Info.ValidFields, piManifest);
Info.Manifest := ManifestRva;
end;
end;
{ Process Creation }
function ReferenceSecurityDescriptor(
const ObjectAttributes: IObjectAttributes
): PSecurityDescriptor;
begin
if Assigned(ObjectAttributes) and Assigned(ObjectAttributes.Security) then
Result := ObjectAttributes.Security.Data
else
Result := nil;
end;
function RtlxCreateUserProcess;
var
ProcessParams: IRtlUserProcessParameters;
ProcessInfo: TRtlUserProcessInformation;
ApplicationNative: String;
ApplicationNativeStr: TNtUnicodeString;
hxExpandedToken: IHandle;
begin
Result := RtlxCreateProcessParameters(Options, ProcessParams);
if not Result.IsSuccess then
Exit;
// Keep the string from the Options.ApplicationNative() call alive
ApplicationNative := Options.ApplicationNative;
Result := RtlxInitUnicodeString(ApplicationNativeStr, ApplicationNative);
if not Result.IsSuccess then
Exit;
// Allow use of pseudo-tokens
hxExpandedToken := Options.hxToken;
Result := NtxExpandToken(hxExpandedToken, TOKEN_ASSIGN_PRIMARY);
if not Result.IsSuccess then
Exit;
Result.Location := 'RtlCreateUserProcess';
if Assigned(Options.hxParentProcess) then
Result.LastCall.Expects<TProcessAccessMask>(PROCESS_CREATE_PROCESS);
if Assigned(Options.hxToken) then
begin
Result.LastCall.ExpectedPrivilege := SE_ASSIGN_PRIMARY_TOKEN_PRIVILEGE;
Result.LastCall.Expects<TTokenAccessMask>(TOKEN_ASSIGN_PRIMARY);
end;
Result.Status := RtlCreateUserProcess(
ApplicationNativeStr,
0, // Deprecated attributes
ProcessParams.Data,
ReferenceSecurityDescriptor(Options.ProcessAttributes),
ReferenceSecurityDescriptor(Options.ThreadAttributes),
HandleOrDefault(Options.hxParentProcess),
poInheritHandles in Options.Flags,
HandleOrDefault(Options.hxDebugPort),
HandleOrDefault(hxExpandedToken),
ProcessInfo
);
if not Result.IsSuccess then
Exit;
// Capture the information about the new process
Info.ValidFields := [piProcessID, piThreadID, piProcessHandle, piThreadHandle,
piImageInformation];
Info.ClientId := ProcessInfo.ClientId;
Info.hxProcess := Auto.CaptureHandle(ProcessInfo.Process);
Info.hxThread := Auto.CaptureHandle(ProcessInfo.Thread);
Info.ImageInformation := ProcessInfo.ImageInformation;
if (poDetectManifest in Options.Flags) then
RtlxDetectManifestAndSaveAddresses(Options, Info);
// Resume the process if necessary
if not (poSuspended in Options.Flags) then
NtxResumeThread(Info.hxThread);
end;
function RtlxCreateUserProcessEx;
var
ProcessParams: IRtlUserProcessParameters;
ProcessInfo: TRtlUserProcessInformation;
ParamsEx: TRtlUserProcessExtendedParameters;
ApplicationNative: String;
ApplicationNativeStr: TNtUnicodeString;
hxExpandedToken: IHandle;
begin
Result := LdrxCheckDelayedImport(delayed_RtlCreateUserProcessEx);
if not Result.IsSuccess then
Exit;
Result := RtlxCreateProcessParameters(Options, ProcessParams);
if not Result.IsSuccess then
Exit;
// Keep the string from the Options.ApplicationNative() call alive
ApplicationNative := Options.ApplicationNative;
Result := RtlxInitUnicodeString(ApplicationNativeStr, ApplicationNative);
if not Result.IsSuccess then
Exit;
// Allow use of pseudo-tokens
hxExpandedToken := Options.hxToken;
Result := NtxExpandToken(hxExpandedToken, TOKEN_ASSIGN_PRIMARY);
if not Result.IsSuccess then
Exit;
ParamsEx := Default(TRtlUserProcessExtendedParameters);
ParamsEx.Version := RTL_USER_PROCESS_EXTENDED_PARAMETERS_VERSION;
ParamsEx.ProcessSecurityDescriptor :=
ReferenceSecurityDescriptor(Options.ProcessAttributes);
ParamsEx.ThreadSecurityDescriptor :=
ReferenceSecurityDescriptor(Options.ThreadAttributes);
ParamsEx.ParentProcess := HandleOrDefault(Options.hxParentProcess);
ParamsEx.TokenHandle := HandleOrDefault(Options.hxToken);
ParamsEx.JobHandle := HandleOrDefault(Options.hxJob);
ParamsEx.DebugPort := HandleOrDefault(Options.hxDebugPort);
Result.Location := 'RtlCreateUserProcessEx';
if Assigned(Options.hxParentProcess) then
Result.LastCall.Expects<TProcessAccessMask>(PROCESS_CREATE_PROCESS);
if Assigned(Options.hxToken) then
begin
Result.LastCall.ExpectedPrivilege := SE_ASSIGN_PRIMARY_TOKEN_PRIVILEGE;
Result.LastCall.Expects<TTokenAccessMask>(TOKEN_ASSIGN_PRIMARY);
end;
if Assigned(Options.hxJob) then
Result.LastCall.Expects<TJobObjectAccessMask>(JOB_OBJECT_ASSIGN_PROCESS);
Result.Status := RtlCreateUserProcessEx(
ApplicationNativeStr,
ProcessParams.Data,
poInheritHandles in Options.Flags,
@ParamsEx,
ProcessInfo
);
if not Result.IsSuccess then
Exit;
// Capture the information about the new process
Info.ValidFields := [piProcessID, piThreadID, piProcessHandle, piThreadHandle,
piImageInformation];
Info.ClientId := ProcessInfo.ClientId;
Info.hxProcess := Auto.CaptureHandle(ProcessInfo.Process);
Info.hxThread := Auto.CaptureHandle(ProcessInfo.Thread);
Info.ImageInformation := ProcessInfo.ImageInformation;
if (poDetectManifest in Options.Flags) then
RtlxDetectManifestAndSaveAddresses(Options, Info);
// Resume the process if necessary
if not (poSuspended in Options.Flags) then
NtxResumeThread(Info.hxThread);
end;
function NtxCreateUserProcess;
var
hProcess, hThread: THandle;
ProcessFlags: TProcessCreateFlags;
ThreadFlags: TThreadCreateFlags;
ProcessParams: IRtlUserProcessParameters;
CreateInfo: TPsCreateInfo;
Attributes: TPsAttributesRecord;
ProcessObjAttr, ThreadObjAttr: PObjectAttributes;
begin
Info := Default(TProcessInfo);
// Prepare Rtl parameters
Result := RtlxCreateProcessParameters(Options, ProcessParams);
if not Result.IsSuccess then
Exit;
// Prepare process object attributes
Result := AttributesRefOrNil(ProcessObjAttr, Options.ProcessAttributes);
if not Result.IsSuccess then
Exit;
// Prepare thread object attributes
Result := AttributesRefOrNil(ThreadObjAttr, Options.ThreadAttributes);
if not Result.IsSuccess then
Exit;
// Prepare PS attributes
Result := Attributes.Create(Options);
if not Result.IsSuccess then
Exit;
// Prepare flags
ProcessFlags := 0;
if poBreakawayFromJob in Options.Flags then
ProcessFlags := ProcessFlags or PROCESS_CREATE_FLAGS_BREAKAWAY;
if poForceBreakaway in Options.Flags then
ProcessFlags := ProcessFlags or PROCESS_CREATE_FLAGS_FORCE_BREAKAWAY;
if poInheritHandles in Options.Flags then
ProcessFlags := ProcessFlags or PROCESS_CREATE_FLAGS_INHERIT_HANDLES;
if poUseProtection in Options.Flags then
ProcessFlags := ProcessFlags or PROCESS_CREATE_FLAGS_PROTECTED_PROCESS;
ThreadFlags := 0;
if poSuspended in Options.Flags then
ThreadFlags := ThreadFlags or THREAD_CREATE_FLAGS_CREATE_SUSPENDED;
// Console inheritance
if poInheritConsole in Options.Flags then
begin
if LdrxCheckDelayedImport(delayed_BaseGetConsoleReference).IsSuccess then
ProcessParams.Data.ConsoleHandle := BaseGetConsoleReference
else
ProcessParams.Data.ConsoleHandle :=
RtlGetCurrentPeb.ProcessParameters.ConsoleHandle;
ProcessParams.Data.ProcessGroupID :=
RtlGetCurrentPeb.ProcessParameters.ProcessGroupID;
end;
// Ask for us as much info as possible
CreateInfo := Default(TPsCreateInfo);
CreateInfo.Size := SizeOf(TPsCreateInfo);
CreateInfo.State := PsCreateInitialState;
CreateInfo.AdditionalFileAccess := Options.AdditionalFileAccess;
CreateInfo.InitFlags :=
PS_CREATE_INITIAL_STATE_WRITE_OUTPUT_ON_EXIT or
PS_CREATE_INITIAL_STATE_IFEO_SKIP_DEBUGGER;
if poDetectManifest in Options.Flags then
CreateInfo.InitFlags := CreateInfo.InitFlags or
PS_CREATE_INITIAL_STATE_DETECT_MANIFEST;
Result.Location := 'NtCreateUserProcess';
if Assigned(Options.hxParentProcess) then
Result.LastCall.Expects<TProcessAccessMask>(PROCESS_CREATE_PROCESS);
if Assigned(Options.hxToken) then
begin
Result.LastCall.ExpectedPrivilege := SE_ASSIGN_PRIMARY_TOKEN_PRIVILEGE;
Result.LastCall.Expects<TTokenAccessMask>(TOKEN_ASSIGN_PRIMARY);
end;
if Assigned(Options.hxJob) then
Result.LastCall.Expects<TJobObjectAccessMask>(JOB_OBJECT_ASSIGN_PROCESS);
if poForceBreakaway in Options.Flags then
Result.LastCall.ExpectedPrivilege := SE_TCB_PRIVILEGE;
Result.Status := NtCreateUserProcess(
hProcess,
hThread,
AccessMaskOverride(MAXIMUM_ALLOWED, Options.ProcessAttributes),
AccessMaskOverride(MAXIMUM_ALLOWED, Options.ThreadAttributes),
ProcessObjAttr,
ThreadObjAttr,
ProcessFlags,
ThreadFlags,
ProcessParams.Data,
CreateInfo,
Attributes.Data
);
// Attach the stage that failed as an info class
Result.LastCall.UsesInfoClass(CreateInfo.State, icPerform);
if Result.IsSuccess then
begin
// Capture info about the process
Info.ValidFields := Info.ValidFields + [piProcessID, piThreadID,
piTebAddress];
Info.ClientId := Attributes.ClientId;
Info.TebAddress := Attributes.TebAddress;
if hProcess <> 0 then
begin
Include(Info.ValidFields, piProcessHandle);
Info.hxProcess := Auto.CaptureHandle(hProcess);
end;
if hThread <> 0 then
begin
Include(Info.ValidFields, piThreadHandle);
Info.hxThread := Auto.CaptureHandle(hThread);
end;
end;
// Make sure to either close or capture all handles
case CreateInfo.State of
PsCreateFailOnSectionCreate:
if CreateInfo.FileHandleFail <> 0 then
begin
Include(Info.ValidFields, piFileHandle);
Info.hxFile := Auto.CaptureHandle(CreateInfo.FileHandleFail);
end;
PsCreateFailExeName:
if CreateInfo.IFEOKey <> 0 then
NtxClose(CreateInfo.IFEOKey);
PsCreateSuccess:
begin
// Capture more info about the process
Info.ValidFields := Info.ValidFields + [piPebAddress,
piUserProcessParameters, piUserProcessParametersFlags];
Info.PebAddressNative := CreateInfo.PebAddressNative;
Info.UserProcessParameters := CreateInfo.UserProcessParametersNative;
Info.UserProcessParametersFlags := CreateInfo.CurrentParameterFlags;
if CreateInfo.PebAddressWow64.Value <> 0 then
begin
Include(Info.ValidFields, piPebAddressWoW64);
Info.PebAddressWoW64 := CreateInfo.PebAddressWow64;
end;
if BitTest(CreateInfo.OutputFlags and
PS_CREATE_SUCCESS_MANIFEST_DETECTED) then
begin
Include(Info.ValidFields, piManifest);
Info.Manifest.Address := CreateInfo.ManifestAddress;
Info.Manifest.Size := CreateInfo.ManifestSize;
end;
if CreateInfo.FileHandleSuccess <> 0 then
begin
Include(Info.ValidFields, piFileHandle);
Info.hxFile := Auto.CaptureHandle(CreateInfo.FileHandleSuccess);
end;
if CreateInfo.SectionHandle <> 0 then
begin
Include(Info.ValidFields, piSectionHandle);
Info.hxSection := Auto.CaptureHandle(CreateInfo.SectionHandle);
end;
end;
end;
end;
end.