-
Notifications
You must be signed in to change notification settings - Fork 33
/
NtUtils.Processes.Create.Package.pas
341 lines (292 loc) · 10 KB
/
NtUtils.Processes.Create.Package.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
unit NtUtils.Processes.Create.Package;
{
The module provides support for creating processes with package identity.
}
interface
uses
Ntapi.WinNt, Ntapi.ObjBase, Ntapi.appmodel, Ntapi.Versions, NtUtils,
NtUtils.Processes.Create;
// Create a new process in a package context via IDesktopAppXActivator
[RequiresCOM]
[MinOSVersion(OsWin10RS1)]
[SupportedOption(spoCurrentDirectory)]
[SupportedOption(spoSuspended)]
[SupportedOption(spoRequireElevation)]
[SupportedOption(spoToken)]
[SupportedOption(spoParentProcess, OsWin10RS2)]
[SupportedOption(spoWindowMode)]
[SupportedOption(spoAppUserModeId, omRequired)]
[SupportedOption(spoPackageBreakaway)]
function PkgxCreateProcessInPackage(
const Options: TCreateProcessOptions;
out Info: TProcessInfo
): TNtxStatus;
// Activate a Windows Store application
[RequiresCOM]
[MinOSVersion(OsWin8)]
function PkgxActivateApplication(
const AppUserModelId: String;
[opt] const Arguments: String = '';
Options: TActivateOptions = 0;
[out, opt] pProcessId: PProcessId32 = nil
): TNtxStatus;
implementation
uses
Ntapi.ntdef, Ntapi.ntpsapi, Ntapi.ntobapi, Ntapi.ShellApi, Ntapi.ObjIdl,
Ntapi.WinUser, Ntapi.ProcessThreadsApi, Ntapi.ntstatus, NtUtils.Errors,
NtUtils.Com, NtUtils.Tokens.Impersonate, NtUtils.Threads, NtUtils.Objects,
NtUtils.Processes.Create.Shell, NtUtils.Processes.Info, NtUtils.AntiHooking,
DelphiUtils.AutoObjects;
{$BOOLEVAL OFF}
{$IFOPT R+}{$DEFINE R+}{$ENDIF}
{$IFOPT Q+}{$DEFINE Q+}{$ENDIF}
type
TAppxActivatorHookContext = record
CurrentThreadId: TThreadId;
TargetProcessId: TProcessId;
Options: TCreateProcessOptions;
ShellExecHookReverter: IAutoReleasable;
OpenProcessHookReverter: IAutoReleasable;
end;
PAppxActivatorHookContext = ^TAppxActivatorHookContext;
IAppxActivatorHookContext = IMemory<PAppxActivatorHookContext>;
var
// A variable to pass info to the hooks we install
AppxActivatorHookContext: Weak<IAppxActivatorHookContext>;
function HookedNtOpenProcess(
out ProcessHandle: THandle;
DesiredAccess: TProcessAccessMask;
const ObjectAttributes: TObjectAttributes;
const ClientId: TClientId
): NTSTATUS; stdcall;
var
ShouldHandle: Boolean;
Context: IAppxActivatorHookContext;
begin
// Verify that we are on the right thread processing the right request
ShouldHandle := AppxActivatorHookContext.Upgrade(Context) and
(Context.Data.CurrentThreadId = NtCurrentThreadId) and
(ClientId.UniqueProcess = Context.Data.TargetProcessId) and
Assigned(Context.Data.Options.hxParentProcess);
if ShouldHandle then
// Reuse the existing handle
Result := NtDuplicateObject(NtCurrentProcess,
Context.Data.Options.hxParentProcess.Handle, NtCurrentProcess,
ProcessHandle, 0, 0, DUPLICATE_SAME_ACCESS)
else
// Forward the request further
Result := NtOpenProcess(ProcessHandle, DesiredAccess, ObjectAttributes,
ClientId);
end;
function HookedShellExecuteExW(
var ExecInfo: TShellExecuteInfoW
): LongBool; stdcall;
var
ShouldHandle: Boolean;
Context: IAppxActivatorHookContext;
PassedProvider, HookedProvider: IServiceProvider;
PassedDirectory: PWideChar;
begin
PassedDirectory := nil;
// Verify that we are on the right thread processing the right request
ShouldHandle := AppxActivatorHookContext.Upgrade(Context) and
(Context.Data.CurrentThreadId = NtCurrentThreadId) and
BitTest(ExecInfo.Mask and SEE_MASK_FLAG_HINST_IS_SITE) and
IUnknown(ExecInfo.hInstApp).QueryInterface(IServiceProvider,
PassedProvider).IsSuccess;
if ShouldHandle then
begin
PassedDirectory := ExecInfo.Directory;
ExecInfo.Directory := PWideChar(Context.Data.Options.CurrentDirectory);
if poUseWindowMode in Context.Data.Options.Flags then
ExecInfo.Show := Context.Data.Options.WindowMode;
if poSuspended in Context.Data.Options.Flags then
begin
// Create our extended service provider
HookedProvider := ShlxMakeCreatingProcessProvider(
Context.Data.Options.Flags, PassedProvider);
ExecInfo.hInstApp := UIntPtr(HookedProvider);
end;
end;
// Invoke the unhooked API
Result := ShellExecuteExW(ExecInfo);
if ShouldHandle then
begin
// Restore pointers in case the caller depends on them
ExecInfo.Directory := PassedDirectory;
ExecInfo.hInstApp := UIntPtr(PassedProvider);
end;
end;
function RtlxAppxActivatorHost: String;
begin
if RtlOsVersionAtLeast(OsWin11) then
Result := 'twinui.appcore.dll'
else
Result := 'twinui.dll';
end;
function PkgxCreateProcessInPackage;
var
Activator: IUnknown;
ActivatorV1: IDesktopAppXActivatorV1;
ActivatorV2: IDesktopAppXActivatorV2;
ActivatorV3: IDesktopAppXActivatorV3;
Flags: TDesktopAppxActivateOptions;
WindowMode: TShowMode32;
ImpersonationReverter: IAutoReleasable;
ParentInfo: TProcessBasicInformation;
HookContext: IAppxActivatorHookContext;
hProcess: THandle;
begin
Info := Default(TProcessInfo);
// Create the activator without asking for any specific interface
Result := ComxCreateInstanceWithFallback(RtlxAppxActivatorHost,
CLSID_DesktopAppXActivator, IUnknown, Activator,
'CLSID_DesktopAppXActivator');
if not Result.IsSuccess then
Exit;
// Prepare parameters
Flags := DAXAO_NONPACKAGED_EXE or DAXAO_NO_ERROR_UI;
if poRequireElevation in Options.Flags then
Flags := Flags or DAXAO_ELEVATE;
if BitTest(Options.PackageBreakaway and
PROCESS_CREATION_DESKTOP_APP_BREAKAWAY_DISABLE_PROCESS_TREE) then
Flags := Flags or DAXAO_NONPACKAGED_EXE_PROCESS_TREE;
// Determine the PID of the parent
if Assigned(Options.hxParentProcess) then
begin
Result := NtxProcess.Query(Options.hxParentProcess, ProcessBasicInformation,
ParentInfo);
if not Result.IsSuccess then
Exit;
end
else
ParentInfo.UniqueProcessID := 0;
// Extend the functionality via hooking
if Assigned(Options.hxParentProcess) or (Options.CurrentDirectory <> '') or
([poUseWindowMode, poSuspended] * Options.Flags <> []) then
begin
// Allocate the hook context
IMemory(HookContext) := Auto.Allocate<TAppxActivatorHookContext>;
HookContext.Data.CurrentThreadId := NtCurrentThreadId;
HookContext.Data.Options := Options;
// Make a global weak reference
AppxActivatorHookContext := HookContext;
if (Options.CurrentDirectory <> '') or
([poUseWindowMode, poSuspended] * Options.Flags <> []) then
begin
// Install the hook on ShellExecuteExW to add parameters
Result := RtlxInstallIATHook(HookContext.Data.ShellExecHookReverter,
RtlxAppxActivatorHost, shell32, 'ShellExecuteExW',
@HookedShellExecuteExW);
if not Result.IsSuccess then
Exit;
end;
if Assigned(Options.hxParentProcess) then
begin
HookContext.Data.TargetProcessId := ParentInfo.UniqueProcessID;
// Install the hook on NtOpenProcess to provide the parent handle
Result := RtlxInstallIATHook(HookContext.Data.OpenProcessHookReverter,
kernelbase, ntdll, 'NtOpenProcess', @HookedNtOpenProcess);
if not Result.IsSuccess then
Exit;
end;
end;
// Pass the token via impersonation
if Assigned(Options.hxToken) then
begin
// Revert to the original one when we are done.
ImpersonationReverter := NtxBackupThreadToken(NtxCurrentThread);
Result := NtxImpersonateAnyToken(Options.hxToken);
if not Result.IsSuccess then
begin
// No need to revert impersonation if we did not change it.
ImpersonationReverter.AutoRelease := False;
Exit;
end;
end;
if Activator.QueryInterface(IDesktopAppXActivatorV3,
ActivatorV3).IsSuccess then
begin
if poUseWindowMode in Options.Flags then
WindowMode := Options.WindowMode
else
WindowMode := TShowMode32.SW_SHOW_NORMAL;
// Use Win 11+ version
Result.Location := 'IDesktopAppXActivator::ActivateWithOptionsArgsWorkingDirectoryShowWindow';
Result.HResult := ActivatorV3.ActivateWithOptionsArgsWorkingDirectoryShowWindow(
PWideChar(Options.AppUserModeId),
PWideChar(Options.ApplicationWin32),
PWideChar(Options.Parameters),
Flags,
ParentInfo.UniqueProcessID,
nil,
NtUtils.RefStrOrNil(Options.CurrentDirectory),
WindowMode,
hProcess
);
end
else if Activator.QueryInterface(IDesktopAppXActivatorV2,
ActivatorV2).IsSuccess then
begin
// Use RS2+ version
Result.Location := 'IDesktopAppXActivator::ActivateWithOptions';
Result.HResult := ActivatorV2.ActivateWithOptions(
PWideChar(Options.AppUserModeId),
PWideChar(Options.ApplicationWin32),
PWideChar(Options.Parameters),
Flags,
ParentInfo.UniqueProcessID,
hProcess
);
end
else if Activator.QueryInterface(IDesktopAppXActivatorV1,
ActivatorV1).IsSuccess then
begin
// Use RS1 version
Result.Location := 'IDesktopAppXActivator::ActivateWithOptions';
Result.HResult := ActivatorV1.ActivateWithOptions(
PWideChar(Options.AppUserModeId),
PWideChar(Options.ApplicationWin32),
PWideChar(Options.Parameters),
Flags,
hProcess
);
end
else
begin
// Unknown version
Result.Location := 'AppxCreateProcess';
Result.Status := STATUS_UNKNOWN_REVISION;
Exit;
end;
if not Result.IsSuccess then
Exit;
if hProcess <> 0 then
begin
// We get a process handle in response
Include(Info.ValidFields, piProcessHandle);
Info.hxProcess := Auto.CaptureHandle(hProcess);
end;
end;
function PkgxActivateApplication;
const
DLL_NAME: array [Boolean] of String = ('twinui.dll', 'twinui.appcore.dll');
var
ActivationManager: IApplicationActivationManager;
ProcessId: TProcessId32;
begin
Result := ComxCreateInstanceWithFallback(
DLL_NAME[RtlOsVersionAtLeast(OsWin81)],
CLSID_ApplicationActivationManager,
IApplicationActivationManager,
ActivationManager
);
if not Result.IsSuccess then
Exit;
Result.Location := 'IApplicationActivationManager.ActivateApplication';
Result.HResult := ActivationManager.ActivateApplication(
PWideChar(AppUserModelId), RefStrOrNil(Arguments), Options, ProcessId);
if Result.IsSuccess and Assigned(pProcessId) then
pProcessId^ := ProcessId;
end;
end.