-
Notifications
You must be signed in to change notification settings - Fork 33
/
NtUtils.Processes.Create.Shell.pas
301 lines (250 loc) · 7.62 KB
/
NtUtils.Processes.Create.Shell.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
unit NtUtils.Processes.Create.Shell;
{
The module provides support for process creation via Shell API
}
interface
uses
Ntapi.ObjIdl, NtUtils, NtUtils.Processes.Create;
type
TShlxCommandLineInfo = record
Application: String;
CommandLine: String;
Parameters: String;
end;
// Create a new process via ShellExecCmdLine
[SupportedOption(spoCurrentDirectory)]
[SupportedOption(spoRequireElevation)]
[SupportedOption(spoRunAsInvoker)]
[SupportedOption(spoWindowMode)]
function ShlxExecuteCmd(
const Options: TCreateProcessOptions;
out Info: TProcessInfo
): TNtxStatus;
// Create a new process via ShellExecuteExW
[SupportedOption(spoCurrentDirectory)]
[SupportedOption(spoSuspended)]
[SupportedOption(spoBreakawayFromJob)]
[SupportedOption(spoInheritConsole)]
[SupportedOption(spoRequireElevation)]
[SupportedOption(spoRunAsInvoker)]
[SupportedOption(spoWindowMode)]
function ShlxExecute(
const Options: TCreateProcessOptions;
out Info: TProcessInfo
): TNtxStatus;
{ Other functions }
// Create a service provider for ICreatingProcess
function ShlxMakeCreatingProcessProvider(
const Flags: TNewProcessFlags;
[opt] InnerProvider: IServiceProvider = nil
): IServiceProvider;
// Parse a command line string into a program path and parameters
// Note that this function does not respect the current directory and requires
// the target file to exist.
function ShlxEvaluateSystemCommandTemplate(
const CommandLine: String;
out Info: TShlxCommandLineInfo
): TNtxStatus;
implementation
uses
Ntapi.WinError, Ntapi.ShellApi, Ntapi.WinUser, Ntapi.ObjBase,
Ntapi.ProcessThreadsApi, NtUtils.Objects;
{$BOOLEVAL OFF}
{$IFOPT R+}{$DEFINE R+}{$ENDIF}
{$IFOPT Q+}{$DEFINE Q+}{$ENDIF}
function ShlxExecuteCmd;
var
ShowMode: TShowMode32;
SeclFlags: TSeclFlags;
RunAsInvoker: IAutoReleasable;
begin
Info := Default(TProcessInfo);
// Allow running as invoker
Result := RtlxApplyCompatLayer(
poRunAsInvokerOn in Options.Flags,
poRunAsInvokerOff in Options.Flags,
RunAsInvoker
);
if not Result.IsSuccess then
Exit;
// Always set window mode to something
if poUseWindowMode in Options.Flags then
ShowMode := Options.WindowMode
else
ShowMode := TShowMode32.SW_SHOW_DEFAULT;
SeclFlags := SECL_NO_UI or SECL_ALLOW_NONEXE;
// Request elevation
if poRequireElevation in Options.Flags then
SeclFlags := SeclFlags or SECL_RUNAS;
Result.Location := 'ShellExecCmdLine';
Result.HResult := ShellExecCmdLine(
0,
PWideChar(Options.CommandLine),
PWideChar(Options.CurrentDirectory),
ShowMode,
nil,
SeclFlags
);
// No information about the new process is available
end;
type
TCreatingProcessCallback = reference to function (
[in] const cpi: ICreateProcessInputs
): HResult;
TCreatingProcess = class (TInterfacedObject, ICreatingProcess)
private
FCallback: TCreatingProcessCallback;
public
function OnCreating(
[in] const cpi: ICreateProcessInputs
): HResult; stdcall;
constructor Create(
const Callback: TCreatingProcessCallback
);
end;
TCreatingProcessProvider = class (TInterfacedObject, IServiceProvider)
private
FCallback: TCreatingProcessCallback;
FInnerProvider: IServiceProvider;
public
function QueryService(
[in] const guidService: TGuid;
[in] const riid: TIid;
[out] out vObject
): HResult; stdcall;
constructor Create(
const Callback: TCreatingProcessCallback;
[opt] const InnerProvider: IServiceProvider = nil
);
end;
constructor TCreatingProcess.Create;
begin
inherited Create;
FCallback := Callback;
end;
function TCreatingProcess.OnCreating;
begin
if Assigned(cpi) and Assigned(FCallback) then
Result := FCallback(cpi)
else
Result := E_INVALIDARG;
end;
constructor TCreatingProcessProvider.Create;
begin
inherited Create;
FCallback := Callback;
FInnerProvider := InnerProvider;
end;
function TCreatingProcessProvider.QueryService;
begin
if (guidService = SID_ExecuteCreatingProcess) and
(riid = ICreatingProcess) then
begin
// Notify ShellExecuteEx that we want to adjust process creation flags
ICreatingProcess(vObject) := TCreatingProcess.Create(FCallback);
Result := S_OK;
end
else if Assigned(FInnerProvider) then
// Forward the request further
Result := FInnerProvider.QueryService(guidService, riid, vObject)
else
Result := E_NOINTERFACE;
end;
function ShlxMakeCreatingProcessProvider;
begin
Result := TCreatingProcessProvider.Create(
function (const cpi: ICreateProcessInputs): HResult
var
FlagsToAdd: TProcessCreateFlags;
begin
FlagsToAdd := 0;
if poSuspended in Flags then
FlagsToAdd := FlagsToAdd or CREATE_SUSPENDED;
if poBreakawayFromJob in Flags then
FlagsToAdd := FlagsToAdd or CREATE_BREAKAWAY_FROM_JOB;
Result := cpi.AddCreateFlags(FlagsToAdd);
end,
InnerProvider
);
end;
function ShlxExecute;
var
ExecInfo: TShellExecuteInfoW;
RunAsInvoker: IAutoReleasable;
CustomProvider: IServiceProvider;
begin
Info := Default(TProcessInfo);
ExecInfo := Default(TShellExecuteInfoW);
ExecInfo.cbSize := SizeOf(TShellExecuteInfoW);
ExecInfo.Mask := SEE_MASK_NOASYNC or SEE_MASK_UNICODE or
SEE_MASK_NOCLOSEPROCESS or SEE_MASK_FLAG_NO_UI;
ExecInfo.FileName := PWideChar(Options.ApplicationWin32);
ExecInfo.Parameters := PWideChar(Options.Parameters);
ExecInfo.Directory := PWideChar(Options.CurrentDirectory);
// Always set window mode to something
if poUseWindowMode in Options.Flags then
ExecInfo.Show := Options.WindowMode
else
ExecInfo.Show := TShowMode32.SW_SHOW_DEFAULT;
// SEE_MASK_NO_CONSOLE is opposite to CREATE_NEW_CONSOLE
if poInheritConsole in Options.Flags then
ExecInfo.Mask := ExecInfo.Mask or SEE_MASK_NO_CONSOLE;
if [poSuspended, poBreakawayFromJob] * Options.Flags <> [] then
begin
CustomProvider := ShlxMakeCreatingProcessProvider(Options.Flags);
ExecInfo.Mask := ExecInfo.Mask or SEE_MASK_FLAG_HINST_IS_SITE;
ExecInfo.hInstApp := UIntPtr(CustomProvider);
end;
// Request elevation
if poRequireElevation in Options.Flags then
ExecInfo.Verb := 'runas';
// Allow running as invoker
Result := RtlxApplyCompatLayer(
poRunAsInvokerOn in Options.Flags,
poRunAsInvokerOff in Options.Flags,
RunAsInvoker
);
if not Result.IsSuccess then
Exit;
Result.Location := 'ShellExecuteExW';
Result.Win32Result := ShellExecuteExW(ExecInfo);
if not Result.IsSuccess then
Exit;
// We only conditionally get a handle to the process.
if ExecInfo.hProcess <> 0 then
begin
Include(Info.ValidFields, piProcessHandle);
Info.hxProcess := Auto.CaptureHandle(ExecInfo.hProcess);
end;
end;
function DelayCoTaskMemFree(
[in] Buffer: Pointer
): IAutoReleasable;
begin
Result := Auto.Delay(
procedure
begin
CoTaskMemFree(Buffer);
end
);
end;
function ShlxEvaluateSystemCommandTemplate;
var
ApplicationBuffer, CommandLineBuffer, ParametersBuffer: PWideChar;
ApplicationDeallocator: IAutoReleasable;
CommandLineDeallocator: IAutoReleasable;
ParametersDeallocator: IAutoReleasable;
begin
Result.Location := 'SHEvaluateSystemCommandTemplate';
Result.HResult := SHEvaluateSystemCommandTemplate(PWideChar(CommandLine),
ApplicationBuffer, CommandLineBuffer, ParametersBuffer);
if not Result.IsSuccess then
Exit;
ApplicationDeallocator := DelayCoTaskMemFree(ApplicationBuffer);
CommandLineDeallocator := DelayCoTaskMemFree(CommandLineBuffer);
ParametersDeallocator := DelayCoTaskMemFree(ParametersBuffer);
Info.Application := String(ApplicationBuffer);
Info.CommandLine := String(CommandLineBuffer);
Info.Parameters := String(ParametersBuffer);
end;
end.