-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathNtUtils.Packages.ExecAlias.pas
539 lines (425 loc) · 15.3 KB
/
NtUtils.Packages.ExecAlias.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
unit NtUtils.Packages.ExecAlias;
{
This module provides functions for working with app execution aliases.
}
interface
{$BOOLEVAL OFF}
{$IFOPT R+}{$DEFINE R+}{$ENDIF}
{$IFOPT Q+}{$DEFINE Q+}{$ENDIF}
uses
Ntapi.WinNt, Ntapi.appmodel.ExecAlias, Ntapi.ntseapi, Ntapi.ntioapi, Ntapi.ObjBase,
Ntapi.Versions, NtUtils, DelphiUtils.AutoObjects;
type
IExecAliasInfo = IAutoPointer<PAppExecutionAliasInfo>;
IExecAliasData = IAutoPointer<PAppExecAliasData>;
// Ask AppInfo to parse an execution alias and derive a token for it
[MinOSVersion(OsWin10RS3)]
function PkgxLoadExecutionAliasInfo(
out Info: IExecAliasInfo;
const ApplicationPath: String;
[opt, Access(TOKEN_LOAD_ALIAS)] const hxIncomingToken: IHandle = nil
): TNtxStatus;
// Generate a token for an existing execution alias
[MinOSVersion(OsWin10RS3)]
function PkgxLoadExecutionAliasToken(
out hxToken: IHandle;
const ApplicationPath: String;
[opt, Access(TOKEN_LOAD_ALIAS)] const hxIncomingToken: IHandle = nil
): TNtxStatus;
// Generate a token for an AUMID by creating a temporary execution alias
[MinOSVersion(OsWin10RS4)]
function PkgxGenerateExecAliasTokenForAumid(
out hxToken: IHandle;
const ApplicationUserModelId: String;
AliasType: TAppExecutionAliasType = AppExecAliasDesktop;
[opt] const hxIncomingToken: IHandle = nil
): TNtxStatus;
// Open and parse an execution alias
[MinOSVersion(OsWin10RS3)]
function PkgxOpenExecutionAlias(
out hxExecAlias: IExecAliasData;
const Path: String;
[opt, Access(TOKEN_QUERY)] const hxToken: IHandle = nil
): TNtxStatus;
// Prepare a new execution alias
[MinOSVersion(OsWin10RS4)]
function PkgxCreateExecutionAlias(
out hxExecAlias: IExecAliasData;
const ApplicationUserModelId: String;
const PackageRelativeExecutable: String;
AliasType: TAppExecutionAliasType = AppExecAliasDesktop;
[opt] PackageFamilyName: String = ''
): TNtxStatus;
// Save an execution alias to an existing file by name
[MinOSVersion(OsWin10RS3)]
function PkgxPersistExecutionAliasByName(
const hxExecAlias: IExecAliasData;
const Path: String
): TNtxStatus;
// Save an execution alias to a file by handle
[MinOSVersion(OsWin1019H2)]
function PkgxPersistExecutionAliasByHandle(
const hxExecAlias: IExecAliasData;
[Access(FILE_WRITE_DATA or FILE_WRITE_ATTRIBUTES)] const hxFile: IHandle
): TNtxStatus;
// Save an execution alias to a new/existing file
[MinOSVersion(OsWin10RS3)]
function PkgxPersistExecutionAlias(
const hxExecAlias: IExecAliasData;
const Path: String
): TNtxStatus;
// Query the target executable for an execution alias
[MinOSVersion(OsWin10RS3)]
function PkgxQueryExecutionAliasExecutable(
const hxExecAlias: IExecAliasData;
out Executable: String
): TNtxStatus;
// Query the target app user model ID for an execution alias
[MinOSVersion(OsWin10RS3)]
function PkgxQueryExecutionAliasAumid(
const hxExecAlias: IExecAliasData;
out Aumid: String
): TNtxStatus;
// Query the target full package name for an execution alias
[RequiresCOM]
[MinOSVersion(OsWin10RS3)]
function PkgxQueryExecutionAliasFullPackageName(
const hxExecAlias: IExecAliasData;
out PackageFullName: String
): TNtxStatus;
// Query the target package family name for an execution alias
[MinOSVersion(OsWin10RS3)]
function PkgxQueryExecutionAliasFamilyPackageName(
const hxExecAlias: IExecAliasData;
out PackageFamilyName: String
): TNtxStatus;
// Query the application type for an execution alias
[MinOSVersion(OsWin10RS4)]
function PkgxQueryExecutionAliasType(
const hxExecAlias: IExecAliasData;
out AliasType: TAppExecutionAliasType
): TNtxStatus;
implementation
uses
Ntapi.ntstatus, Ntapi.ntobapi, NtUtils.Ldr, NtUtils.Objects, NtUtils.Packages,
NtUtils.Packages.SRCache, NtUtils.Files.Open, NtUtils.Files.Operations,
NtUtils.SysUtils, NtUtils.Environment;
{ Execution aliases }
type
TAppExecAliasAutoInfo = class (TCustomAutoPointer, IAutoPointer,
IAutoReleasable)
procedure Release; override;
end;
TAppExecAliasAutoData = class (TCustomAutoPointer, IAutoPointer,
IAutoReleasable)
procedure Release; override;
end;
procedure TAppExecAliasAutoInfo.Release;
begin
if Assigned(FData) and LdrxCheckDelayedImport(
delayed_FreeAppExecutionAliasInfoEx).IsSuccess then
FreeAppExecutionAliasInfoEx(FData);
FData := nil;
inherited;
end;
procedure TAppExecAliasAutoData.Release;
begin
if Assigned(FData) and LdrxCheckDelayedImport(
delayed_CloseAppExecutionAliasEx).IsSuccess then
CloseAppExecutionAliasEx(FData);
FData := nil;
inherited;
end;
function PkgxLoadExecutionAliasInfo;
var
hInfo: PAppExecutionAliasInfo;
begin
Result := LdrxCheckDelayedImport(delayed_LoadAppExecutionAliasInfoEx);
if not Result.IsSuccess then
Exit;
Result.Location := 'LoadAppExecutionAliasInfoEx';
if Assigned(hxIncomingToken) then
Result.LastCall.Expects<TTokenAccessMask>(TOKEN_LOAD_ALIAS);
Result.Status := LoadAppExecutionAliasInfoEx(PWideChar(ApplicationPath),
HandleOrDefault(hxIncomingToken), hInfo);
if Result.IsSuccess then
IAutoPointer(Info) := TAppExecAliasAutoInfo.Capture(hInfo);
end;
function PkgxLoadExecutionAliasToken;
var
Info: IExecAliasInfo;
begin
Result := PkgxLoadExecutionAliasInfo(Info, ApplicationPath, hxIncomingToken);
if not Result.IsSuccess then
Exit;
if Info.Data.ActivationToken = 0 then
begin
Result.Location := 'AdvxLoadExecutionAliasToken';
Result.Status := STATUS_NO_TOKEN;
Exit;
end;
// Reopen the handle since the returned one will be closed with the info
Result := NtxDuplicateHandleLocal(Info.Data.ActivationToken, hxToken, 0, 0,
DUPLICATE_SAME_ACCESS or DUPLICATE_SAME_ATTRIBUTES);
end;
function PkgxGenerateExecAliasTokenForAumid;
var
ApplicationId: TSRCacheApplicationId;
PackageId: TSRCachePackageId;
AliasPath, InstallLocation: String;
hxAliasData: IExecAliasData;
hxApplicationKey, hxPackageKey, hxAliasFile: IHandle;
FileDeleter: IAutoReleasable;
begin
// Locate the TEMP directory to write the execution alias to
Result := RtlxQueryVariableEnvironment(RtlxCurrentEnvironment, 'TEMP',
AliasPath);
if not Result.IsSuccess then
Exit;
// Find the application ID in the state repository cache
Result := PkgxSRCacheFindApplicationId(ApplicationId, ApplicationUserModelId);
if not Result.IsSuccess then
Exit;
// Open the state repository cache data key for the AUMID
Result := PkgxSRCacheOpenApplication(ApplicationId, hxApplicationKey);
if not Result.IsSuccess then
Exit;
// Determine the corresponding package ID
Result := PkgxSRCacheQueryApplicationPackageID(hxApplicationKey, PackageId);
if not Result.IsSuccess then
Exit;
hxApplicationKey := nil;
// Open the state repository cache data key for the package
Result := PkgxSRCacheOpenPackage(PackageId, hxPackageKey);
if not Result.IsSuccess then
Exit;
// Determine package installation location
Result := PkgxSRCacheQueryPackageLocation(hxPackageKey, InstallLocation);
if not Result.IsSuccess then
Exit;
hxPackageKey := nil;
// Prepare reparse data for the execution alias. AppInfo might require the
// target file to belong to the package, so we specify the manifest, as it
// always exists in packages that contain applications.
Result := PkgxCreateExecutionAlias(hxAliasData, ApplicationUserModelId,
RtlxCombinePaths(InstallLocation, 'AppxManifest.xml'), AliasType);
if not Result.IsSuccess then
Exit;
// Generate a random file name
AliasPath := RtlxCombinePaths(AliasPath, RtlxGuidToString(RtlxRandomGuid));
// Create a temporary file for the reparse point
Result := NtxCreateFile(hxAliasFile, FileParameters
.UseFileName(AliasPath, fnWin32)
.UseAccess(FILE_WRITE_ATTRIBUTES or _DELETE)
.UseFileAttributes(FILE_ATTRIBUTE_TEMPORARY)
.UseOptions(FILE_OPEN_REPARSE_POINT or FILE_OPEN_FOR_BACKUP_INTENT)
.UseShareMode(FILE_SHARE_READ)
.UseDisposition(FILE_CREATE)
);
if not Result.IsSuccess then
Exit;
// Undo file creation on exit
FileDeleter := Auto.Delay(
procedure
begin
NtxFile.Set<Boolean>(hxAliasFile, FileDispositionInformation, True);
end
);
// Apply the execution alias reparse point
if LdrxCheckDelayedImport(delayed_PersistAppExecutionAliasToFileHandleEx)
.IsSuccess then
Result := PkgxPersistExecutionAliasByHandle(hxAliasData, hxAliasFile)
else
Result := PkgxPersistExecutionAliasByName(hxAliasData, AliasPath);
if not Result.IsSuccess then
Exit;
// Ask AppInfo to generate a token
Result := PkgxLoadExecutionAliasToken(hxToken, AliasPath, hxIncomingToken);
end;
function PkgxOpenExecutionAlias;
var
hExecAlias: PAppExecAliasData;
begin
Result := LdrxCheckDelayedImport(delayed_OpenAppExecutionAliasForUserEx);
if not Result.IsSuccess then
Exit;
Result.Location := 'OpenAppExecutionAliasForUserEx';
if Assigned(hxToken) then
Result.LastCall.Expects<TTokenAccessMask>(TOKEN_QUERY);
Result.HResult := OpenAppExecutionAliasForUserEx(PWideChar(Path),
HandleOrDefault(hxToken), hExecAlias);
if Result.IsSuccess then
IAutoPointer(hxExecAlias) := TAppExecAliasAutoData.Capture(hExecAlias);
end;
function PkgxCreateExecutionAlias;
var
hExecAlias: PAppExecAliasData;
RelativeAppId: String;
begin
Result := LdrxCheckDelayedImport(delayed_CreateAppExecutionAliasEx2);
if not Result.IsSuccess then
Exit;
if PackageFamilyName = '' then
begin
// Derive the family name from Aumid
Result := PkgxDeriveFamilyNameFromAppUserModelId(ApplicationUserModelId,
PackageFamilyName, RelativeAppId);
if not Result.IsSuccess then
Exit;
end;
Result.Location := 'CreateAppExecutionAliasEx2';
Result.HResult := CreateAppExecutionAliasEx2(PWideChar(PackageFamilyName),
PWideChar(ApplicationUserModelId), PWideChar(PackageRelativeExecutable),
AliasType, hExecAlias);
if Result.IsSuccess then
IAutoPointer(hxExecAlias) := TAppExecAliasAutoData.Capture(hExecAlias);
end;
function PkgxPersistExecutionAliasByName;
begin
Result := LdrxCheckDelayedImport(delayed_PersistAppExecutionAliasToFileEx);
if not Result.IsSuccess then
Exit;
Result.Location := 'PersistAppExecutionAliasToFileEx';
Result.HResult := PersistAppExecutionAliasToFileEx(
Auto.RefOrNil<PAppExecAliasData>(hxExecAlias), PWideChar(Path));
end;
function PkgxPersistExecutionAliasByHandle;
begin
Result := LdrxCheckDelayedImport(delayed_PersistAppExecutionAliasToFileHandleEx);
if not Result.IsSuccess then
Exit;
Result.Location := 'PersistAppExecutionAliasToFileHandleEx';
Result.LastCall.Expects<TIoFileAccessMask>(FILE_WRITE_DATA or
FILE_WRITE_ATTRIBUTES);
Result.HResult := PersistAppExecutionAliasToFileHandleEx(
Auto.RefOrNil<PAppExecAliasData>(hxExecAlias), HandleOrDefault(hxFile));
end;
function PkgxPersistExecutionAlias;
var
hxFile: IHandle;
ActionTaken: TFileIoStatusResult;
begin
// Open or create the target file's reparse point
Result := NtxCreateFile(hxFile, FileParameters
.UseFileName(Path, fnWin32)
.UseAccess(FILE_WRITE_ATTRIBUTES or _DELETE)
.UseDisposition(FILE_OPEN_IF)
.UseOptions(FILE_OPEN_REPARSE_POINT or FILE_OPEN_FOR_BACKUP_INTENT),
@ActionTaken
);
if not Result.IsSuccess then
Exit;
// When available, use the newer (by handle) method; othewise, fall back to
// the API that reopens the file
if LdrxCheckDelayedImport(delayed_PersistAppExecutionAliasToFileHandleEx)
.IsSuccess then
Result := PkgxPersistExecutionAliasByHandle(hxExecAlias, hxFile)
else
Result := PkgxPersistExecutionAliasByName(hxExecAlias, Path);
// Undo file creation on failure
if not Result.IsSuccess and (ActionTaken = FILE_CREATED) then
NtxFile.Set<Boolean>(hxFile, FileDispositionInformation, True);
end;
function PkgxQueryExecutionAliasExecutable;
var
Buffer: IMemory<PWideChar>;
BufferLength: Cardinal;
begin
Result := LdrxCheckDelayedImport(delayed_GetAppExecutionAliasExecutableEx);
if not Result.IsSuccess then
Exit;
IMemory(Buffer) := Auto.AllocateDynamic(MAX_PATH * SizeOf(WideChar));
repeat
BufferLength := Buffer.Size div SizeOf(WideChar);
Result.Location := 'GetAppExecutionAliasExecutableEx';
Result.HResult := GetAppExecutionAliasExecutableEx(
Auto.RefOrNil<PAppExecAliasData>(hxExecAlias), Buffer.Data, BufferLength);
until not NtxExpandBufferEx(Result, IMemory(Buffer), BufferLength *
SizeOf(WideChar), nil);
if not Result.IsSuccess then
Exit;
// Strip the terminating zero
if BufferLength > 0 then
Dec(BufferLength);
SetString(Executable, Buffer.Data, BufferLength);
end;
function PkgxQueryExecutionAliasAumid;
var
Buffer: IMemory<PWideChar>;
BufferLength: Cardinal;
begin
Result := LdrxCheckDelayedImport(
delayed_GetAppExecutionAliasApplicationUserModelIdEx);
if not Result.IsSuccess then
Exit;
IMemory(Buffer) := Auto.AllocateDynamic(MAX_PATH * SizeOf(WideChar));
repeat
BufferLength := Buffer.Size div SizeOf(WideChar);
Result.Location := 'GetAppExecutionAliasApplicationUserModelIdEx';
Result.HResult := GetAppExecutionAliasApplicationUserModelIdEx(
Auto.RefOrNil<PAppExecAliasData>(hxExecAlias), Buffer.Data, BufferLength);
until not NtxExpandBufferEx(Result, IMemory(Buffer), BufferLength *
SizeOf(WideChar), nil);
if not Result.IsSuccess then
Exit;
// Strip the terminating zero
if BufferLength > 0 then
Dec(BufferLength);
SetString(Aumid, Buffer.Data, BufferLength);
end;
function PkgxQueryExecutionAliasFullPackageName;
var
Buffer: IMemory<PWideChar>;
BufferLength: Cardinal;
begin
Result := LdrxCheckDelayedImport(delayed_GetAppExecutionAliasPackageFullNameEx);
if not Result.IsSuccess then
Exit;
IMemory(Buffer) := Auto.AllocateDynamic(MAX_PATH * SizeOf(WideChar));
repeat
BufferLength := Buffer.Size div SizeOf(WideChar);
Result.Location := 'GetAppExecutionAliasPackageFullNameEx';
Result.HResult := GetAppExecutionAliasPackageFullNameEx(
Auto.RefOrNil<PAppExecAliasData>(hxExecAlias), Buffer.Data, BufferLength);
until not NtxExpandBufferEx(Result, IMemory(Buffer), BufferLength *
SizeOf(WideChar), nil);
if not Result.IsSuccess then
Exit;
// Strip the terminating zero
if BufferLength > 0 then
Dec(BufferLength);
SetString(PackageFullName, Buffer.Data, BufferLength);
end;
function PkgxQueryExecutionAliasFamilyPackageName;
var
Buffer: IMemory<PWideChar>;
BufferLength: Cardinal;
begin
Result := LdrxCheckDelayedImport(delayed_GetAppExecutionAliasPackageFamilyNameEx);
if not Result.IsSuccess then
Exit;
IMemory(Buffer) := Auto.AllocateDynamic(MAX_PATH * SizeOf(WideChar));
repeat
BufferLength := Buffer.Size div SizeOf(WideChar);
Result.Location := 'GetAppExecutionAliasPackageFamilyNameEx';
Result.HResult := GetAppExecutionAliasPackageFamilyNameEx(
Auto.RefOrNil<PAppExecAliasData>(hxExecAlias), Buffer.Data, BufferLength);
until not NtxExpandBufferEx(Result, IMemory(Buffer), BufferLength *
SizeOf(WideChar), nil);
if not Result.IsSuccess then
Exit;
// Strip the terminating zero
if BufferLength > 0 then
Dec(BufferLength);
SetString(PackageFamilyName, Buffer.Data, BufferLength);
end;
function PkgxQueryExecutionAliasType;
begin
Result := LdrxCheckDelayedImport(delayed_GetAppExecutionAliasApplicationType);
if not Result.IsSuccess then
Exit;
Result.Location := 'GetAppExecutionAliasApplicationType';
Result.HResult := GetAppExecutionAliasApplicationType(
Auto.RefOrNil<PAppExecAliasData>(hxExecAlias), AliasType);
end;
end.