-
Notifications
You must be signed in to change notification settings - Fork 33
/
NtUtils.Processes.Create.Manual.pas
400 lines (322 loc) · 11.6 KB
/
NtUtils.Processes.Create.Manual.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
unit NtUtils.Processes.Create.Manual;
{
The module provides support for completely manual process creation via
NtCreateProcessEx.
}
interface
uses
Ntapi.ntpsapi, Ntapi.ntseapi, Ntapi.ntmmapi, NtUtils,
NtUtils.Processes.Create;
// Create a process object with no threads
[RequiredPrivilege(SE_ASSIGN_PRIMARY_TOKEN_PRIVILEGE, rpSometimes)]
[RequiredPrivilege(SE_TCB_PRIVILEGE, rpSometimes)]
function NtxCreateProcessObject(
out hxProcess: IHandle;
Flags: TProcessCreateFlags;
[opt, Access(SECTION_MAP_EXECUTE)] const hxSection: IHandle;
[opt, Access(PROCESS_CREATE_PROCESS)] const hxParent: IHandle = nil;
[opt, Access(TOKEN_ASSIGN_PRIMARY)] const hxToken: IHandle = nil;
[opt] const ObjectAttributes: IObjectAttributes = nil;
[opt, Access(DEBUG_PROCESS_ASSIGN)] const hxDebugObject: IHandle = nil
): TNtxStatus;
// Prepare and write process parameters into a process
function RtlxSetProcessParameters(
const Options: TCreateProcessOptions;
[Access(PROCESS_VM_OPERATION or PROCESS_VM_WRITE or
PROCESS_QUERY_LIMITED_INFORMATION)] var Info: TProcessInfo
): TNtxStatus;
// Create the first thread in a process
function RtlxCreateInitialThread(
const Options: TCreateProcessOptions;
[Access(SECTION_MAP_EXECUTE or SECTION_MAP_READ),
Access(PROCESS_CREATE_THREAD)] var Info: TProcessInfo
): TNtxStatus;
// Start a new process via NtCreateProcessEx
[SupportedOption(spoCurrentDirectory)]
[SupportedOption(spoSuspended)]
[SupportedOption(spoInheritHandles)]
[SupportedOption(spoBreakawayFromJob)]
[SupportedOption(spoForceBreakaway)]
[SupportedOption(spoEnvironment)]
[SupportedOption(spoObjectInherit)]
[SupportedOption(spoDesiredAccess)]
[SupportedOption(spoSecurity)]
[SupportedOption(spoWindowMode)]
[SupportedOption(spoWindowTitle)]
[SupportedOption(spoStdHandles)]
[SupportedOption(spoDesktop)]
[SupportedOption(spoToken)]
[SupportedOption(spoParentProcess)]
[SupportedOption(spoSection)]
[SupportedOption(spoDebugPort)]
[SupportedOption(spoAdditionalFileAccess)]
[SupportedOption(spoDetectManifest)]
[RequiredPrivilege(SE_ASSIGN_PRIMARY_TOKEN_PRIVILEGE, rpSometimes)]
[RequiredPrivilege(SE_TCB_PRIVILEGE, rpSometimes)]
function NtxCreateProcessEx(
const Options: TCreateProcessOptions;
out Info: TProcessInfo
): TNtxStatus;
implementation
uses
Ntapi.WinNt, Ntapi.ntstatus, Ntapi.ntioapi, Ntapi.ntrtl, Ntapi.ImageHlp,
Ntapi.ntdbg, Ntapi.ntdef, NtUtils.Processes, NtUtils.Objects, NtUtils.Memory,
NtUtils.ImageHlp, NtUtils.Sections, NtUtils.Files.Open, NtUtils.Threads,
NtUtils.Processes.Info, NtUtils.Processes.Create.Native, NtUtils.Manifests,
DelphiUtils.RangeChecks;
{$BOOLEVAL OFF}
{$IFOPT R+}{$DEFINE R+}{$ENDIF}
{$IFOPT Q+}{$DEFINE Q+}{$ENDIF}
function NtxCreateProcessObject;
var
ObjAttr: PObjectAttributes;
hProcess: THandle;
begin
Result := AttributesRefOrNil(ObjAttr, ObjectAttributes);
if not Result.IsSuccess then
Exit;
Result.Location := 'NtCreateProcessEx';
Result.LastCall.Expects<TSectionAccessMask>(SECTION_MAP_EXECUTE);
if Assigned(hxParent) and (hxParent.Handle <> NtCurrentProcess) then
Result.LastCall.Expects<TProcessAccessMask>(PROCESS_CREATE_PROCESS);
if Assigned(hxToken) then
begin
Result.LastCall.ExpectedPrivilege := SE_ASSIGN_PRIMARY_TOKEN_PRIVILEGE;
Result.LastCall.Expects<TTokenAccessMask>(TOKEN_ASSIGN_PRIMARY);
end;
if Assigned(hxDebugObject) then
Result.LastCall.Expects<TDebugObjectAccessMask>(DEBUG_PROCESS_ASSIGN);
if BitTest(Flags and PROCESS_CREATE_FLAGS_FORCE_BREAKAWAY) then
Result.LastCall.ExpectedPrivilege := SE_TCB_PRIVILEGE;
Result.Status := NtCreateProcessEx(
hProcess,
AccessMaskOverride(PROCESS_ALL_ACCESS, ObjectAttributes),
ObjAttr,
HandleOrDefault(hxParent, NtCurrentProcess),
Flags,
HandleOrDefault(hxSection),
HandleOrDefault(hxDebugObject),
HandleOrDefault(hxToken),
0
);
if Result.IsSuccess then
hxProcess := Auto.CaptureHandle(hProcess);
end;
function RtlxSetProcessParameters;
var
Params: IRtlUserProcessParameters;
RemoteParameters: IMemory;
BasicInfo: TProcessBasicInformation;
begin
// Prepare process parameters locally
Result := RtlxCreateProcessParameters(Options, Params);
if not Result.IsSuccess then
Exit;
// Since we are copying parameters to a different address,
// switch to offsets instead of absolute pointers.
RtlDeNormalizeProcessParams(Params.Data);
Include(Info.ValidFields, piUserProcessParametersFlags);
Info.UserProcessParametersFlags := Params.Data.Flags;
// Allocate an area within the remote process. Note that it does not need to
// be on the heap (there is no heap yet!); the initialization code in ntdll
// will copy it into the heap for us.
Result := NtxAllocateMemory(Info.hxProcess, Params.Size, RemoteParameters,
PAGE_READWRITE);
if not Result.IsSuccess then
Exit;
// Unfortunately, denormalization doesn't make the environment pointer
// relative; Fix it by manually changing it to the remote address.
{$Q-}{$R-}
UIntPtr(Params.Data.Environment) := UIntPtr(Params.Data.Environment) -
UIntPtr(Params.Data) + UIntPtr(RemoteParameters.Data);
{$IFDEF R+}{$R+}{$ENDIF}{$IFDEF Q+}{$Q+}{$ENDIF}
// Write the parameters to the target
Result := NtxWriteMemory(Info.hxProcess, RemoteParameters.Data,
Params.Region);
if not Result.IsSuccess then
Exit;
// Determine the PEB address
Result := NtxProcess.Query(Info.hxProcess, ProcessBasicInformation,
BasicInfo);
if not Result.IsSuccess then
Exit;
// Adjust PEB's pointer to process parameters
Result := NtxMemory.Write(Info.hxProcess,
@BasicInfo.PebBaseAddress.ProcessParameters, RemoteParameters.Data);
if not Result.IsSuccess then
Exit;
Include(Info.ValidFields, piUserProcessParameters);
Info.UserProcessParameters := RemoteParameters.Data;
// Transfer the ownership of the memory region to the target
RemoteParameters.AutoRelease := False;
end;
function RtlxGetInitialThreadParameters(
const Image: TMemory;
out EntryPointRva: Cardinal;
out StackCommit: Cardinal;
out StackReserve: Cardinal
): TNtxStatus;
var
Header: PImageNtHeaders;
Bitness: TImageBitness;
begin
try
Result := RtlxGetImageNtHeader(Header, Image);
if not Result.IsSuccess then
Exit;
Result := RtlxGetImageBitness(Bitness, Image, Header);
if not Result.IsSuccess then
Exit;
Result.Location := 'RtlxGetInitialThreadParameters';
Result.Status := STATUS_INVALID_IMAGE_FORMAT;
case Bitness of
ib32Bit:
if not CheckStruct(Image, Header, UIntPtr(@PImageNtHeaders(nil)
.OptionalHeader32.SizeOfStackCommit) + SizeOf(Cardinal)) then
Exit;
ib64Bit:
if not CheckStruct(Image, Header, UIntPtr(@PImageNtHeaders(nil)
.OptionalHeader64.SizeOfStackCommit) + SizeOf(UInt64)) then
Exit;
end;
EntryPointRva := Header.OptionalHeader.AddressOfEntryPoint;
StackCommit := Header.OptionalHeader.SizeOfStackCommit;
StackReserve := Header.OptionalHeader.SizeOfStackReserve;
Result.Status := STATUS_SUCCESS;
except
Result.Location := 'RtlxGetInitialThreadParameters';
Result.Status := STATUS_UNHANDLED_EXCEPTION;
end;
end;
function RtlxCreateInitialThread;
var
LocalMapping: IMemory;
ThreadFlags: TThreadCreateFlags;
RemoteImageBase: Pointer;
BasicInfo: TProcessBasicInformation;
ThreadInfo: TThreadInfo;
ManifestRva: TMemory;
EntryPointRva: Cardinal;
StackCommit: Cardinal;
StackReserve: Cardinal;
begin
ThreadFlags := 0;
if poSuspended in Options.Flags then
ThreadFlags := ThreadFlags or THREAD_CREATE_FLAGS_CREATE_SUSPENDED;
// Map the image locally do determine various thread parameters.
// Use PAGE_EXECUTE to pass an access check only on SECTION_MAP_EXECUTE,
// despite mapping as a readable image. This is the bare minimum since
// NtCreateProcessEx requires it anyway.
Result := NtxMapViewOfSection(Info.hxSection, NtxCurrentProcess, LocalMapping,
MappingParameters.UseProtection(PAGE_EXECUTE));
if not Result.IsSuccess then
Exit;
Result := RtlxGetInitialThreadParameters(LocalMapping.Region, EntryPointRva,
StackCommit, StackReserve);
if not Result.IsSuccess then
Exit;
// Determine PEB address
Result := NtxProcess.Query(Info.hxProcess, ProcessBasicInformation,
BasicInfo);
if not Result.IsSuccess then
Exit;
Include(Info.ValidFields, piPebAddress);
Info.PebAddressNative := BasicInfo.PebBaseAddress;
// Determine the remote image base
Result := NtxMemory.Read(Info.hxProcess,
@BasicInfo.PebBaseAddress.ImageBaseAddress, RemoteImageBase);
if not Result.IsSuccess then
Exit;
Include(Info.ValidFields, piImageBase);
Info.ImageBaseAddress := RemoteImageBase;
// Find embedded manifest if required
if poDetectManifest in Options.Flags then
if RtlxFindManifestInSection(Info.hxSection, ManifestRva).IsSuccess then
begin
Inc(PByte(ManifestRva.Address), UIntPtr(RemoteImageBase));
Include(Info.ValidFields, piManifest);
Info.Manifest := ManifestRva;
end;
// Create the initial thread
Result := NtxCreateThreadEx(
Info.hxThread,
Info.hxProcess,
Pointer(UIntPtr(RemoteImageBase) + EntryPointRva),
BasicInfo.PebBaseAddress,
ThreadFlags,
0,
StackCommit,
StackReserve,
Options.ThreadAttributes,
@ThreadInfo
);
if not Result.IsSuccess then
Exit;
Info.ValidFields := Info.ValidFields + [piProcessID, piThreadID,
piThreadHandle, piTebAddress];
Info.ClientId := ThreadInfo.ClientID;
Info.TebAddress := ThreadInfo.TebAddress;
end;
function NtxCreateProcessEx;
var
ProcessFlags: TProcessCreateFlags;
TerminateOnFailure: IAutoReleasable;
begin
Info := Default(TProcessInfo);
if Assigned(Options.hxSection) then
Info.hxSection := Options.hxSection
else
begin
// Open the executable file. Note that as long as we don't specify execute
// protection for the section, we don't even need FILE_EXECUTE.
Result := NtxOpenFile(Info.hxFile, FileParameters
.UseOptions(FILE_NON_DIRECTORY_FILE)
.UseAccess(FILE_READ_DATA or Options.AdditionalFileAccess)
.UseFileName(Options.ApplicationNative)
);
if not Result.IsSuccess then
Exit;
Include(Info.ValidFields, piFileHandle);
// Create an image section from the file. Note that the call uses
// PAGE_READONLY only for access checks on the file, not the page protection
Result := NtxCreateFileSection(Info.hxSection, Info.hxFile, PAGE_READONLY,
SEC_IMAGE);
if not Result.IsSuccess then
Exit;
end;
Include(Info.ValidFields, piSectionHandle);
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;
// Create a process object with no threads
Result := NtxCreateProcessObject(
Info.hxProcess,
ProcessFlags,
Info.hxSection,
Options.hxParentProcess,
Options.hxToken,
Options.ProcessAttributes,
Options.hxDebugPort
);
if not Result.IsSuccess then
Exit;
Include(Info.ValidFields, piProcessHandle);
// Make sure to clean up if we fail on a later stage
TerminateOnFailure := NtxDelayedTerminateProcess(Info.hxProcess,
STATUS_CANCELLED);
// Prepare and write process parameters
Result := RtlxSetProcessParameters(Options, Info);
if not Result.IsSuccess then
Exit;
// Create the initial thread
Result := RtlxCreateInitialThread(Options, Info);
if not Result.IsSuccess then
Exit;
// Created successfully, cancel automatic clean-up
TerminateOnFailure.AutoRelease := False;
end;
end.