-
Notifications
You must be signed in to change notification settings - Fork 33
/
NtUtils.UserManager.pas
345 lines (268 loc) · 8.2 KB
/
NtUtils.UserManager.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
unit NtUtils.UserManager;
{
This module provides functions for interacting with User Manager service.
}
interface
uses
Ntapi.WinNt, Ntapi.usermgr, Ntapi.ntseapi, Ntapi.Versions, NtUtils,
DelphiUtils.AutoObjects;
type
TSessionUserContext = Ntapi.usermgr.TSessionUserContext;
{ Contexts }
// Enumerate session user contexts
[MinOSVersion(OsWin10TH1)]
[RequiredPrivilege(SE_TCB_PRIVILEGE, rpAlways)]
function UmgrxEnumerateSessionUsers(
out Contexts: TArray<TSessionUserContext>
): TNtxStatus;
// Query user context from a token
[MinOSVersion(OsWin10TH1)]
function UMgrxQueryUserContext(
[Access(TOKEN_QUERY)] hxToken: IHandle;
out ContextToken: TLuid
): TNtxStatus;
// Query user context from a user SID
[MinOSVersion(OsWin10TH1)]
function UMgrxQueryUserContextFromSid(
const Sid: ISid;
out ContextToken: TLuid
): TNtxStatus;
// Query user context from a user name
[MinOSVersion(OsWin10TH1)]
function UMgrxQueryUserContextFromName(
const UserName: String;
out ContextToken: TLuid
): TNtxStatus;
{ Tokens }
// Open the token of the default account
[MinOSVersion(OsWin10TH1)]
[RequiredPrivilege(SE_TCB_PRIVILEGE, rpAlways)]
function UMgrxQueryDefaultAccountToken(
out hxToken: IHandle
): TNtxStatus;
// Open the token of the user session
[MinOSVersion(OsWin10TH1)]
function UMgrxQuerySessionUserToken(
SessionId: TSessionId;
out hxToken: IHandle
): TNtxStatus;
// Open the token of the user by its context
[MinOSVersion(OsWin10TH1)]
[RequiredPrivilege(SE_TCB_PRIVILEGE, rpForExtendedFunctionality)]
function UMgrxQueryUserToken(
Context: TLuid;
out hxToken: IHandle
): TNtxStatus;
// Open the token of the user by its SID
[MinOSVersion(OsWin10TH1)]
[RequiredPrivilege(SE_TCB_PRIVILEGE, rpForExtendedFunctionality)]
function UMgrxQueryUserTokenFromSid(
const Sid: ISid;
out hxToken: IHandle
): TNtxStatus;
// Open the token of the user by its name
[MinOSVersion(OsWin10TH1)]
[RequiredPrivilege(SE_TCB_PRIVILEGE, rpForExtendedFunctionality)]
function UMgrxQueryUserTokenFromName(
const UserName: String;
out hxToken: IHandle
): TNtxStatus;
// Open the impersonation token of the user by its token and context
[MinOSVersion(OsWin10TH2)]
[RequiredPrivilege(SE_IMPERSONATE_PRIVILEGE, rpAlways)]
function UMgrxQueryImpersonationTokenForContext(
[Access(TOKEN_QUERY or TOKEN_IMPERSONATE)] hxInputToken: IHandle;
Context: TLuid;
out hxOutputToken: IHandle
): TNtxStatus;
// Open the token of the active user shell in the session
[MinOSVersion(OsWin10RS1)]
[RequiredPrivilege(SE_TCB_PRIVILEGE, rpForExtendedFunctionality)]
function UMgrxQueryActiveShellUserToken(
SessionId: TSessionId;
out hxToken: IHandle
): TNtxStatus;
implementation
uses
Ntapi.ntstatus, Ntapi.ProcessThreadsApi, NtUtils.Ldr, NtUtils.Security.Sid,
NtUtils.Tokens, NtUtils.Objects;
{$BOOLEVAL OFF}
{$IFOPT R+}{$DEFINE R+}{$ENDIF}
{$IFOPT Q+}{$DEFINE Q+}{$ENDIF}
function UmgrxDelayedFreeSessionUsers(
[in] Buffer: Pointer
): IAutoReleasable;
begin
if not LdrxCheckDelayedImport(delayed_UMgrFreeSessionUsers).IsSuccess then
Exit(nil);
Result := Auto.Delay(
procedure
begin
UMgrFreeSessionUsers(Buffer);
end
);
end;
[RequiredPrivilege(SE_TCB_PRIVILEGE, rpAlways)]
function UmgrxEnumerateSessionUsers(
out Contexts: TArray<TSessionUserContext>
): TNtxStatus;
var
Buffer: PSessionUserContextArray;
BufferDeallocator: IAutoReleasable;
Count: Cardinal;
i: Integer;
begin
Result := LdrxCheckDelayedImport(delayed_UMgrEnumerateSessionUsers);
if not Result.IsSuccess then
Exit;
Buffer := nil;
Result.Location := 'UMgrEnumerateSessionUsers';
Result.LastCall.ExpectedPrivilege := SE_TCB_PRIVILEGE;
Result.HResult := UMgrEnumerateSessionUsers(Count, Buffer);
if not Result.IsSuccess then
Exit;
BufferDeallocator := UmgrxDelayedFreeSessionUsers(Buffer);
SetLength(Contexts, Count);
for i := 0 to High(Contexts) do
Contexts[i] := Buffer{$R-}[i]{$IFDEF R+}{$R+}{$ENDIF};
end;
function UMgrxQueryUserContext;
begin
Result := LdrxCheckDelayedImport(delayed_UMgrQueryUserContext);
if not Result.IsSuccess then
Exit;
Result := NtxExpandToken(hxToken, TOKEN_QUERY);
if not Result.IsSuccess then
Exit;
Result.Location := 'UMgrQueryUserContext';
Result.LastCall.Expects<TTokenAccessMask>(TOKEN_QUERY);
Result.HResult := UMgrQueryUserContext(hxToken.Handle, ContextToken);
end;
function UMgrxQueryUserContextFromSid;
var
SDDL: String;
begin
Result := LdrxCheckDelayedImport(delayed_UMgrQueryUserContextFromSid);
if not Result.IsSuccess then
Exit;
SDDL := RtlxSidToString(Sid);
Result.Location := 'UMgrQueryUserContextFromSid';
Result.HResult := UMgrQueryUserContextFromSid(PWideChar(SDDL), ContextToken);
end;
function UMgrxQueryUserContextFromName;
begin
Result := LdrxCheckDelayedImport(delayed_UMgrQueryUserContextFromName);
if not Result.IsSuccess then
Exit;
Result.Location := 'UMgrQueryUserContextFromName';
Result.HResult := UMgrQueryUserContextFromName(PWideChar(UserName),
ContextToken);
end;
function UMgrxQueryDefaultAccountToken;
var
hToken: THandle;
begin
Result := LdrxCheckDelayedImport(delayed_UMgrQueryDefaultAccountToken);
if not Result.IsSuccess then
Exit;
hToken := 0;
Result.Location := 'UMgrQueryDefaultAccountToken';
Result.LastCall.ExpectedPrivilege := SE_TCB_PRIVILEGE;
Result.HResult := UMgrQueryDefaultAccountToken(hToken);
if not Result.IsSuccess then
Exit;
Result := NtxCaptureHandle(hxToken, hToken);
end;
function UMgrxQuerySessionUserToken;
var
hToken: THandle;
begin
Result := LdrxCheckDelayedImport(delayed_UMgrQuerySessionUserToken);
if not Result.IsSuccess then
Exit;
hToken := 0;
Result.Location := 'UMgrQuerySessionUserToken';
Result.HResult := UMgrQuerySessionUserToken(SessionId, hToken);
if not Result.IsSuccess then
Exit;
Result := NtxCaptureHandle(hxToken, hToken);
end;
function UMgrxQueryUserToken;
var
hToken: THandle;
begin
Result := LdrxCheckDelayedImport(delayed_UMgrQueryUserToken);
if not Result.IsSuccess then
Exit;
hToken := 0;
Result.Location := 'UMgrQueryUserToken';
Result.HResult := UMgrQueryUserToken(Context, hToken);
if not Result.IsSuccess then
Exit;
Result := NtxCaptureHandle(hxToken, hToken);
end;
function UMgrxQueryUserTokenFromSid;
var
hToken: THandle;
SDDL: String;
begin
Result := LdrxCheckDelayedImport(delayed_UMgrQueryUserTokenFromSid);
if not Result.IsSuccess then
Exit;
hToken := 0;
SDDL := RtlxSidToString(Sid);
Result.Location := 'UMgrQueryUserTokenFromSid';
Result.HResult := UMgrQueryUserTokenFromSid(PWideChar(SDDL), hToken);
if not Result.IsSuccess then
Exit;
Result := NtxCaptureHandle(hxToken, hToken);
end;
function UMgrxQueryUserTokenFromName;
var
hToken: THandle;
begin
Result := LdrxCheckDelayedImport(delayed_UMgrQueryUserTokenFromName);
if not Result.IsSuccess then
Exit;
hToken := 0;
Result.Location := 'UMgrQueryUserTokenFromName';
Result.HResult := UMgrQueryUserTokenFromName(PWideChar(UserName), hToken);
if not Result.IsSuccess then
Exit;
Result := NtxCaptureHandle(hxToken, hToken);
end;
function UMgrxQueryImpersonationTokenForContext;
var
hOutputToken: THandle;
begin
Result := LdrxCheckDelayedImport(delayed_UMgrGetImpersonationTokenForContext);
if not Result.IsSuccess then
Exit;
Result := NtxExpandToken(hxInputToken, TOKEN_QUERY or TOKEN_IMPERSONATE);
if not Result.IsSuccess then
Exit;
hOutputToken := 0;
Result.Location := 'UMgrGetImpersonationTokenForContext';
Result.LastCall.Expects<TTokenAccessMask>(TOKEN_QUERY or TOKEN_IMPERSONATE);
Result.LastCall.ExpectedPrivilege := SE_IMPERSONATE_PRIVILEGE;
Result.HResult := UMgrGetImpersonationTokenForContext(hxInputToken.Handle,
Context, hOutputToken);
if not Result.IsSuccess then
Exit;
Result := NtxCaptureHandle(hxOutputToken, hOutputToken);
end;
function UMgrxQueryActiveShellUserToken;
var
hToken: THandle;
begin
Result := LdrxCheckDelayedImport(delayed_UMgrGetSessionActiveShellUserToken);
if not Result.IsSuccess then
Exit;
hToken := 0;
Result.Location := 'UMgrGetSessionActiveShellUserToken';
Result.HResult := UMgrGetSessionActiveShellUserToken(SessionId, hToken);
if not Result.IsSuccess then
Exit;
Result := NtxCaptureHandle(hxToken, hToken);
end;
end.