-
Notifications
You must be signed in to change notification settings - Fork 35
/
NtUtils.Profiles.pas
374 lines (299 loc) · 9.22 KB
/
NtUtils.Profiles.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
unit NtUtils.Profiles;
{
The module provides support for working with normal (user) and AppContainer
profiles.
}
interface
uses
Ntapi.UserEnv, Ntapi.ntseapi, NtUtils, Ntapi.Versions, DelphiApi.Reflection;
type
TProfileInfo = record
[Hex] Flags: Cardinal;
FullProfile: LongBool;
IsLoaded: LongBool;
ProfilePath: String;
User: ISid;
end;
{ User profiles }
// Load a profile using a token
[RequiredPrivilege(SE_BACKUP_PRIVILEGE, rpAlways)]
[RequiredPrivilege(SE_RESTORE_PRIVILEGE, rpAlways)]
function UnvxLoadProfile(
out hxKey: IHandle;
[Access(TOKEN_LOAD_PROFILE)] hxToken: IHandle
): TNtxStatus;
// Unload a profile using a token
[RequiredPrivilege(SE_BACKUP_PRIVILEGE, rpAlways)]
[RequiredPrivilege(SE_RESTORE_PRIVILEGE, rpAlways)]
function UnvxUnloadProfile(
[Access(0)] const hxProfileKey: IHandle;
[Access(TOKEN_LOAD_PROFILE)] hxToken: IHandle
): TNtxStatus;
// Enumerate existing profiles on the system
function UnvxEnumerateProfiles(
out Profiles: TArray<ISid>
): TNtxStatus;
// Enumerate loaded profiles on the system
function UnvxEnumerateLoadedProfiles(
out Profiles: TArray<ISid>
): TNtxStatus;
// Query profile information
function UnvxQueryProfile(
const Sid: ISid;
out Info: TProfileInfo
): TNtxStatus;
{ AppContainer profiles }
// Create an AppContainer profile.
// NOTE: when called within an AppContainer context, the function returns a
// child AppContainer SIDs
[MinOSVersion(OsWin8)]
function UnvxCreateAppContainer(
out Sid: ISid;
const AppContainerName: String;
[opt] DisplayName: String = '';
[opt] Description: String = '';
[opt] const Capabilities: TArray<TGroup> = nil
): TNtxStatus;
// Construct a SID of an AppContainer profile.
// NOTE: when called within an AppContainer context, the function returns a
// child AppContainer SIDs
[MinOSVersion(OsWin8)]
function UnvxDeriveAppContainer(
out Sid: ISid;
const AppContainerName: String
): TNtxStatus;
// Create an AppContainer profile or open an existing one.
// NOTE: when called within an AppContainer context, the function returns a
// child AppContainer SIDs
[MinOSVersion(OsWin8)]
function UnvxCreateDeriveAppContainer(
out Sid: ISid;
const AppContainerName: String;
[opt] const DisplayName: String = '';
[opt] const Description: String = '';
[opt] const Capabilities: TArray<TGroup> = nil
): TNtxStatus;
// Delete an AppContainer profile.
// NOTE: when called within an AppContainer context, the function deletes a
// child AppContainer.
[MinOSVersion(OsWin8)]
function UnvxDeleteAppContainer(
const AppContainerName: String
): TNtxStatus;
// Query AppContainer folder location
[MinOSVersion(OsWin8)]
function UnvxQueryFolderAppContainer(
const AppContainerSid: ISid;
out Path: String
): TNtxStatus;
implementation
uses
Ntapi.WinNt, Ntapi.ntregapi, Ntapi.ntdef, Ntapi.WinError, Ntapi.ObjBase,
Ntapi.ntstatus, NtUtils.Registry, NtUtils.Errors, NtUtils.Ldr, NtUtils.Tokens,
DelphiUtils.Arrays, NtUtils.Security.Sid, NtUtils.Security.AppContainer,
NtUtils.Objects, NtUtils.Tokens.Info, NtUtils.Lsa.Sid;
{$BOOLEVAL OFF}
{$IFOPT R+}{$DEFINE R+}{$ENDIF}
{$IFOPT Q+}{$DEFINE Q+}{$ENDIF}
const
PROFILE_PATH = REG_PATH_MACHINE + '\SOFTWARE\Microsoft\Windows NT\' +
'CurrentVersion\ProfileList';
{ User profiles }
function UnvxLoadProfile;
var
Sid: ISid;
UserName: String;
Profile: TProfileInfoW;
begin
// Expand pseudo-handles
Result := NtxExpandToken(hxToken, TOKEN_LOAD_PROFILE);
if not Result.IsSuccess then
Exit;
// Determine the SID
Result := NtxQuerySidToken(hxToken, TokenUser, Sid);
if not Result.IsSuccess then
Exit;
UserName := LsaxSidToString(Sid);
Profile := Default(TProfileInfoW);
Profile.Size := SizeOf(Profile);
Profile.UserName := PWideChar(UserName);
Result.Location := 'LoadUserProfileW';
Result.LastCall.ExpectedPrivilege := SE_RESTORE_PRIVILEGE;
Result.LastCall.Expects<TTokenAccessMask>(TOKEN_LOAD_PROFILE);
Result.Win32Result := LoadUserProfileW(hxToken.Handle, Profile);
if Result.IsSuccess then
hxKey := Auto.CaptureHandle(Profile.hProfile);
end;
function UnvxUnloadProfile;
begin
// Expand pseudo-handles
Result := NtxExpandToken(hxToken, TOKEN_LOAD_PROFILE);
if not Result.IsSuccess then
Exit;
Result.Location := 'UnloadUserProfile';
Result.LastCall.ExpectedPrivilege := SE_RESTORE_PRIVILEGE;
Result.LastCall.Expects<TTokenAccessMask>(TOKEN_LOAD_PROFILE);
Result.Win32Result := UnloadUserProfile(hxToken.Handle,
HandleOrDefault(hxProfileKey));
end;
function UnvxEnumerateProfiles;
var
hxKey: IHandle;
ProfileKeys: TArray<TNtxRegKey>;
begin
// Lookup the profile list in the registry
Result := NtxOpenKey(hxKey, PROFILE_PATH, KEY_ENUMERATE_SUB_KEYS);
if not Result.IsSuccess then
Exit;
// Each sub-key is a profile SID
Result := NtxEnumerateKeys(hxKey, ProfileKeys);
if not Result.IsSuccess then
Exit;
// Convert strings to SIDs ignoring irrelevant entries
Profiles := TArray.Convert<TNtxRegKey, ISid>(ProfileKeys,
function (
const Key: TNtxRegKey;
out Sid: ISid
): Boolean
begin
Result := RtlxStringToSidConverter(Key.Name, Sid);
end
);
end;
function UnvxEnumerateLoadedProfiles;
var
hxKey: IHandle;
ProfileKeys: TArray<TNtxRegKey>;
begin
// Each loaded profile is a sub-key in HKU
Result := NtxOpenKey(hxKey, REG_PATH_USER, KEY_ENUMERATE_SUB_KEYS);
if not Result.IsSuccess then
Exit;
Result := NtxEnumerateKeys(hxKey, ProfileKeys);
if not Result.IsSuccess then
Exit;
// Convert strings to SIDs ignoring irrelevant entries
Profiles := TArray.Convert<TNtxRegKey, ISid>(ProfileKeys,
function (
const Key: TNtxRegKey;
out Sid: ISid
): Boolean
begin
Result := RtlxStringToSidConverter(Key.Name, Sid);
end
);
end;
function UnvxQueryProfile;
var
SddlSuffix: String;
hxKey: IHandle;
begin
Info := Default(TProfileInfo);
Info.User := Sid;
SddlSuffix := '\' + RtlxSidToString(Sid);
// Test if the hive is loaded
Result := NtxOpenKey(hxKey, REG_PATH_USER + SddlSuffix, 0);
Info.IsLoaded := Result.IsSuccess or (Result.Status = STATUS_ACCESS_DENIED);
// Retrieve profile information from the registry
Result := NtxOpenKey(hxKey, PROFILE_PATH + SddlSuffix, KEY_QUERY_VALUE);
if not Result.IsSuccess then
Exit;
// The only necessary value
Result := NtxQueryValueKeyString(hxKey, 'ProfileImagePath',
Info.ProfilePath);
if Result.IsSuccess then
begin
NtxQueryValueKeyUInt32(hxKey, 'Flags', Info.Flags);
NtxQueryValueKeyUInt32(hxKey, 'FullProfile', Cardinal(Info.FullProfile));
end;
end;
{ AppContainer profiles }
function UnvxCreateAppContainer;
var
CapArray: TArray<TSidAndAttributes>;
i: Integer;
Buffer: PSid;
BufferDeallocator: IAutoReleasable;
begin
Result := LdrxCheckDelayedImport(delayed_CreateAppContainerProfile);
if not Result.IsSuccess then
Exit;
SetLength(CapArray, Length(Capabilities));
for i := 0 to High(CapArray) do
begin
CapArray[i].Sid := Capabilities[i].Sid.Data;
CapArray[i].Attributes := Capabilities[i].Attributes;
end;
// The function does not like empty strings
if DisplayName = '' then
DisplayName := AppContainerName;
if Description = '' then
Description := DisplayName;
Result.Location := 'CreateAppContainerProfile';
Result.HResult := CreateAppContainerProfile(PWideChar(AppContainerName),
PWideChar(DisplayName), PWideChar(Description), CapArray, Length(CapArray),
Buffer);
if not Result.IsSuccess then
Exit;
BufferDeallocator := RtlxDelayFreeSid(Buffer);
Result := RtlxCopySid(Buffer, Sid);
end;
function UnvxDeriveAppContainer;
var
ParentSid: ISid;
begin
// Determine if we need a parent or a child AppContainer
Result := NtxQuerySidToken(NtxCurrentEffectiveToken, TokenAppContainerSid,
ParentSid);
if not Result.IsSuccess then
Exit;
// Construct one
if Assigned(ParentSid) then
Result := RtlxDeriveChildAppContainerSid(ParentSid, AppContainerName, Sid)
else
Result := RtlxDeriveParentAppContainerSid(AppContainerName, Sid)
end;
function UnvxCreateDeriveAppContainer;
begin
Result := UnvxCreateAppContainer(Sid, AppContainerName, DisplayName,
Description, Capabilities);
if Result.Matches(TWin32Error(ERROR_ALREADY_EXISTS).ToNtStatus,
'CreateAppContainerProfile') then
Result := UnvxDeriveAppContainer(Sid, AppContainerName);
end;
function UnvxDeleteAppContainer;
begin
Result := LdrxCheckDelayedImport(delayed_DeleteAppContainerProfile);
if not Result.IsSuccess then
Exit;
Result.Location := 'DeleteAppContainerProfile';
Result.HResult := DeleteAppContainerProfile(PWideChar(AppContainerName));
end;
function UnvxDelayCoTaskMemFree(
[in] Buffer: Pointer
): IAutoReleasable;
begin
Result := Auto.Delay(
procedure
begin
CoTaskMemFree(Buffer);
end
);
end;
function UnvxQueryFolderAppContainer;
var
Buffer: PWideChar;
BufferDeallocator: IAutoReleasable;
begin
Result := LdrxCheckDelayedImport(delayed_GetAppContainerFolderPath);
if not Result.IsSuccess then
Exit;
Result.Location := 'GetAppContainerFolderPath';
Result.HResult := GetAppContainerFolderPath(PWideChar(RtlxSidToString(
AppContainerSid)), Buffer);
if not Result.IsSuccess then
Exit;
BufferDeallocator := UnvxDelayCoTaskMemFree(Buffer);
Path := String(Buffer);
end;
end.