-
Notifications
You must be signed in to change notification settings - Fork 35
/
NtUtils.Processes.Create.Remote.pas
311 lines (266 loc) · 9.66 KB
/
NtUtils.Processes.Create.Remote.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
unit NtUtils.Processes.Create.Remote;
{
The module provides support for process creation via injecting shellcode
that calls CreateProcess in the context of the parent process.
}
interface
uses
Ntapi.ntpsapi, NtUtils, NtUtils.Processes.Create, NtUtils.Shellcode;
const
PROCESS_CREATE_PROCESS_REMOTE_MIN = NtUtils.Shellcode.PROCESS_REMOTE_EXECUTE;
PROCESS_CREATE_PROCESS_REMOTE = PROCESS_DUP_HANDLE or
PROCESS_CREATE_PROCESS_REMOTE_MIN;
// Call CreateProcess in a context of another process
[SupportedOption(spoCurrentDirectory)]
[SupportedOption(spoSuspended)]
[SupportedOption(spoInheritHandles)]
[SupportedOption(spoBreakawayFromJob)]
[SupportedOption(spoInheritConsole)]
[SupportedOption(spoDesktop)]
[SupportedOption(spoParentProcess, omRequired)]
[SupportedOption(spoTimeout)]
function AdvxCreateProcessRemote(
const Options: TCreateProcessOptions;
out Info: TProcessInfo
): TNtxStatus;
implementation
uses
Ntapi.WinNt, Ntapi.ntdef, Ntapi.ntobapi, Ntapi.ntstatus, Ntapi.WinBase,
Ntapi.ProcessThreadsApi, NtUtils.Processes.Info, NtUtils.Objects,
DelphiUtils.AutoObjects;
{$BOOLEVAL OFF}
{$IFOPT R+}{$DEFINE R+}{$ENDIF}
{$IFOPT Q+}{$DEFINE Q+}{$ENDIF}
type
TProcessInformation64 = record
hProcess: THandle;
{$IFDEF Win32}WoW64Padding1: Cardinal;{$ENDIF}
hThread: THandle;
{$IFDEF Win32}WoW64Padding2: Cardinal;{$ENDIF}
ProcessId: TProcessId32;
ThreadId: TThreadId32;
end;
// The context for the function we are going to inject
TCreateProcessContext = record
CreateProcessW: function (
ApplicationName: PWideChar;
CommandLine: PWideChar;
ProcessAttributes: PSecurityAttributes;
ThreadAttributes: PSecurityAttributes;
InheritHandles: LongBool;
CreationFlags: TProcessCreateFlags;
Environment: PEnvironment;
CurrentDirectory: PWideChar;
const StartupInfo: TStartupInfoW;
out ProcessInformation: TProcessInformation
): LongBool; stdcall;
{$IFDEF Win32}WoW64Padding1: Cardinal;{$ENDIF}
RtlGetLastWin32Error: function: TWin32Error; stdcall;
{$IFDEF Win32}WoW64Padding2: Cardinal;{$ENDIF}
memset: function (
dest: Pointer;
c: Cardinal;
count: NativeUInt
): Pointer; cdecl;
{$IFDEF Win32}WoW64Padding3: Cardinal;{$ENDIF}
CreationFlags: TProcessCreateFlags;
InheritHandles: LongBool;
Info: TProcessInformation64;
CommandLine: PWideChar;
{$IFDEF Win32}WoW64Padding4: Cardinal;{$ENDIF}
Desktop: PWideChar;
{$IFDEF Win32}WoW64Padding5: Cardinal;{$ENDIF}
CurrentDirectory: PWideChar;
{$IFDEF Win32}WoW64Padding6: Cardinal;{$ENDIF}
end;
PCreateProcessContext = ^TCreateProcessContext;
// The function we are going to inject; keep in sync with assembly below
function ProcessCreator(Context: PCreateProcessContext): NTSTATUS; stdcall;
var
SI: TStartupInfoW;
PI: TProcessInformation;
begin
Context.memset(@SI, 0, SizeOf(SI));
SI.cb := SizeOf(SI);
SI.Desktop := Context.Desktop;
if Context.CreateProcessW(nil, Context.CommandLine, nil, nil,
Context.InheritHandles, Context.CreationFlags, nil,
Context.CurrentDirectory, SI, PI) then
begin
Context.Info.hProcess := PI.hProcess;
Context.Info.hThread := PI.hThread;
Context.Info.ProcessId := PI.ProcessId;
Context.Info.ThreadId := PI.ThreadId;
Result := STATUS_SUCCESS;
end
else
Result := NTSTATUS_FROM_WIN32(Context.RtlGetLastWin32Error);
end;
const
// The raw assembly for injection; keep in sync with the code above
{$IFDEF Win64}
ProcessCreatorAsm64: array[0..175] of Byte = (
$55, $53, $48, $81, $EC, $D8, $00, $00, $00, $48, $8B, $EC, $48, $89, $CB,
$48, $8D, $4D, $68, $33, $D2, $41, $B8, $68, $00, $00, $00, $FF, $53, $10,
$C7, $45, $68, $68, $00, $00, $00, $48, $8B, $43, $40, $48, $89, $45, $78,
$33, $C9, $48, $8B, $53, $38, $4D, $33, $C0, $4D, $33, $C9, $8B, $43, $1C,
$89, $44, $24, $20, $8B, $43, $18, $89, $44, $24, $28, $48, $C7, $44, $24,
$30, $00, $00, $00, $00, $48, $8B, $43, $48, $48, $89, $44, $24, $38, $48,
$8D, $45, $68, $48, $89, $44, $24, $40, $48, $8D, $45, $50, $48, $89, $44,
$24, $48, $FF, $13, $85, $C0, $74, $20, $48, $8B, $45, $50, $48, $89, $43,
$20, $48, $8B, $45, $58, $48, $89, $43, $28, $8B, $45, $60, $89, $43, $30,
$8B, $45, $64, $89, $43, $34, $33, $C0, $EB, $0F, $FF, $53, $08, $81, $E0,
$FF, $FF, $00, $00, $81, $C8, $00, $00, $07, $C0, $48, $8D, $A5, $D8, $00,
$00, $00, $5B, $5D, $C3, $CC, $CC, $CC, $CC, $CC, $CC
);
{$ENDIF}
ProcessCreatorAsm32: array[0 .. 127] of Byte = (
$55, $8B, $EC, $83, $C4, $AC, $53, $8B, $5D, $08, $6A, $44, $6A, $00, $8D,
$45, $BC, $50, $FF, $53, $10, $83, $C4, $0C, $C7, $45, $BC, $44, $00, $00,
$00, $8B, $43, $40, $89, $45, $C4, $8D, $45, $AC, $50, $8D, $45, $BC, $50,
$8B, $43, $48, $50, $6A, $00, $8B, $43, $18, $50, $8B, $43, $1C, $50, $6A,
$00, $6A, $00, $8B, $43, $38, $50, $6A, $00, $FF, $13, $85, $C0, $74, $1C,
$8B, $45, $AC, $89, $43, $20, $8B, $45, $B0, $89, $43, $28, $8B, $45, $B4,
$89, $43, $30, $8B, $45, $B8, $89, $43, $34, $33, $C0, $EB, $0D, $FF, $53,
$08, $25, $FF, $FF, $00, $00, $0D, $00, $00, $07, $C0, $5B, $8B, $E5, $5D,
$C2, $04, $00, $CC, $CC, $CC, $CC, $CC
);
function GetCreationFlags(
const Options: TCreateProcessOptions
): TProcessCreateFlags;
begin
Result := 0;
// Suspended state
if poSuspended in Options.Flags then
Result := Result or CREATE_SUSPENDED;
// Job escaping
if poBreakawayFromJob in Options.Flags then
Result := Result or CREATE_BREAKAWAY_FROM_JOB;
// Console
if not (poInheritConsole in Options.Flags) then
Result := Result or CREATE_NEW_CONSOLE;
end;
function MarshalStringRemote(
Source: String;
var Target: Pointer;
var RemoteTarget: Pointer
): Pointer;
begin
if Length(Source) > 0 then
begin
Result := RemoteTarget;
MarshalString(Source, Target);
Inc(PByte(Target), StringSizeZero(Source));
Inc(PByte(RemoteTarget), StringSizeZero(Source));
end
else
Result := nil;
end;
function AdvxCreateProcessRemote;
var
TargetIsWoW64: Boolean;
CommandLine: String;
ntdllFunctions: TArray<Pointer>;
LocalMapping: IMemory<PCreateProcessContext>;
RemoteMapping: IMemory;
CodeRef: TMemory;
DynamicPartLocal, DynamicPartRemote: Pointer;
Timeout: Int64;
begin
Info := Default(TProcessInfo);
// We need a target for injection
if not Assigned(Options.hxParentProcess) then
begin
Result.Location := 'AdvxCreateProcessRemote';
Result.Status := STATUS_INVALID_PARAMETER;
Exit;
end;
// Prevent WoW64 -> Native
Result := RtlxAssertWoW64Compatible(Options.hxParentProcess, TargetIsWoW64);
if not Result.IsSuccess then
Exit;
// Select suitable shellcode
{$IFDEF Win64}
if not TargetIsWoW64 then
CodeRef := TMemory.Reference(ProcessCreatorAsm64)
else
{$ENDIF}
CodeRef := TMemory.Reference(ProcessCreatorAsm32);
CommandLine := Options.CommandLine;
// Map a shared region of memory
Result := RtlxMapSharedMemory(
Options.hxParentProcess,
SizeOf(TCreateProcessContext) + CodeRef.Size + StringSizeZero(CommandLine) +
StringSizeZero(Options.Desktop) + StringSizeZero(Options.CurrentDirectory),
IMemory(LocalMapping),
RemoteMapping,
[mmAllowWrite, mmAllowExecute]
);
if not Result.IsSuccess then
Exit;
// Resolve kernel32 dependency
Result := RtlxFindKnownDllExport(kernel32, TargetIsWoW64, 'CreateProcessW',
@LocalMapping.Data.CreateProcessW);
if not Result.IsSuccess then
Exit;
// Resolve ntdll dependencies
Result := RtlxFindKnownDllExports(ntdll, TargetIsWoW64,
['RtlGetLastWin32Error', 'memset'], ntdllFunctions);
if not Result.IsSuccess then
Exit;
// Start filling up the parameters and code
LocalMapping.Data.RtlGetLastWin32Error := ntdllFunctions[0];
LocalMapping.Data.memset := ntdllFunctions[1];
LocalMapping.Data.CreationFlags := GetCreationFlags(Options);
LocalMapping.Data.InheritHandles := poInheritHandles in Options.Flags;
Move(CodeRef.Address^, LocalMapping.Offset(SizeOf(TCreateProcessContext))^,
CodeRef.Size);
DynamicPartLocal := LocalMapping.Offset(SizeOf(TCreateProcessContext) +
CodeRef.Size);
DynamicPartRemote := RemoteMapping.Offset(SizeOf(TCreateProcessContext) +
CodeRef.Size);
LocalMapping.Data.CommandLine := MarshalStringRemote(CommandLine,
DynamicPartLocal, DynamicPartRemote);
LocalMapping.Data.Desktop := MarshalStringRemote(Options.Desktop,
DynamicPartLocal, DynamicPartRemote);
LocalMapping.Data.CurrentDirectory := MarshalStringRemote(
Options.CurrentDirectory, DynamicPartLocal, DynamicPartRemote);
if Options.Timeout <> 0 then
Timeout := Options.Timeout
else
Timeout := DEFAULT_REMOTE_TIMEOUT;
// Create a thread to execute the code and sync with it
Result := RtlxRemoteExecute(
Options.hxParentProcess,
'Remote::CreateProcessW',
RemoteMapping.Offset(SizeOf(TCreateProcessContext)),
CodeRef.Size,
RemoteMapping.Data,
0,
Timeout,
[RemoteMapping]
);
if not Result.IsSuccess then
Exit;
// Copy the process information
Info.ValidFields := [piProcessID, piThreadID];
Info.ClientId.UniqueProcess := LocalMapping.Data.Info.ProcessId;
Info.ClientId.UniqueThread := LocalMapping.Data.Info.ThreadId;
// Move the process handle
if NtxDuplicateHandleFrom(
Options.hxParentProcess,
LocalMapping.Data.Info.hProcess,
Info.hxProcess,
DUPLICATE_SAME_ACCESS or DUPLICATE_CLOSE_SOURCE
).IsSuccess then
Include(Info.ValidFields, piProcessHandle);
// Move the thread handle
if NtxDuplicateHandleFrom(
Options.hxParentProcess,
LocalMapping.Data.Info.hThread,
Info.hxThread,
DUPLICATE_SAME_ACCESS or DUPLICATE_CLOSE_SOURCE
).IsSuccess then
Include(Info.ValidFields, piThreadHandle);
end;
end.