-
Notifications
You must be signed in to change notification settings - Fork 35
/
NtUtils.Tokens.Logon.pas
373 lines (317 loc) · 10.8 KB
/
NtUtils.Tokens.Logon.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
unit NtUtils.Tokens.Logon;
{
The module introduces functions for logging users into the system via a
normal and Services-for-Users logon.
}
interface
uses
Ntapi.WinNt, Ntapi.WinBase, Ntapi.NtSecApi, Ntapi.ntseapi, NtUtils,
NtUtils.Objects, DelphiApi.Reflection;
type
TLogonInfoFields = set of (
liToken, // All
liLogonId, // All
liQuotas, // All
liLogonCount, // Interactive
liBadPasswordCount, // Interactive
liLogonTime, // Interactive
liLogoffTime, // Interactive, Lm20
liKickOffTime, // Interactive, Lm20
liPasswordLastSet, // Interactive
liPasswordCanChange, // Interactive
liPasswordMustChange, // Interactive
liLogonScript, // Interactive
liHomeDirectory, // Interactive
liHomeDirectoryDrive, // Interactive
liFullName, // Interactive
liProfilePath, // Interactive
liLogonServer, // Interactive, Lm20
liUserFlags, // Interactive, Lm20
liUserSessionKey, // Lm20
liLogonDomainName, // Lm20
liLanmanSessionKey, // Lm20
liUserParameters // Lm20
);
TLogonInfo = record
ValidFields: TLogonInfoFields;
hxToken: IHandle;
LogonId: TLogonId;
Quotas: TQuotaLimits;
LogonCount: Word;
BadPasswordCount: Word;
LogonTime: TLargeInteger;
LogoffTime: TLargeInteger;
KickOffTime: TLargeInteger;
PasswordLastSet: TLargeInteger;
PasswordCanChange: TLargeInteger;
PasswordMustChange: TLargeInteger;
LogonScript: String;
HomeDirectory: String;
HomeDirectoryDrive: String;
FullName: String;
ProfilePath: String;
LogonServer: String;
UserFlags: TLogonFlags;
UserSessionKey: TGuid;
LogonDomainName: String;
[Hex] LanmanSessionKey: UInt64;
UserParameters: String;
end;
TLogonCredentials = record
Domain: String;
Username: String;
Password: String;
S4UFlags: TS4ULogonFlags;
end;
// A low-level wrapper for LsaLogonUser
[RequiredPrivilege(SE_TCB_PRIVILEGE, rpSometimes)]
function LsaxLogonUserInternal(
out Info: TLogonInfo;
const Buffer: IMemory;
const LogonType: TSecurityLogonType;
const TokenSource: TTokenSource;
[opt] const AdditionalGroups: TArray<TGroup> = nil;
const PackageName: AnsiString = NEGOSSP_NAME_A;
const OriginName: AnsiString = 'S4U'
): TNtxStatus;
// Logon a user
[RequiredPrivilege(SE_TCB_PRIVILEGE, rpSometimes)]
function LsaxLogonUser(
out Info: TLogonInfo;
MessageType: TLogonSubmitType;
LogonType: TSecurityLogonType;
const Credentials: TLogonCredentials;
const TokenSource: TTokenSource;
[opt] const AdditionalGroups: TArray<TGroup> = nil;
const PackageName: AnsiString = NEGOSSP_NAME_A
): TNtxStatus;
implementation
uses
Ntapi.ntdef, Ntapi.ntstatus, NtUtils.Processes.Info, NtUtils.Tokens.Misc,
DelphiUtils.AutoObjects, NtUtils.Lsa, NtUtils.Security.Sid, NtUtils.Errors;
{$BOOLEVAL OFF}
{$IFOPT R+}{$DEFINE R+}{$ENDIF}
{$IFOPT Q+}{$DEFINE Q+}{$ENDIF}
function LsaxDelayFreeReturnBuffer(
[in] Buffer: Pointer
): IAutoReleasable;
begin
Result := Auto.Delay(
procedure
begin
LsaFreeReturnBuffer(Buffer);
end
);
end;
function LsaxCaptureLogonProfile(
var Info: TLogonInfo;
[in, ReadsFrom] Profile: Pointer;
[in, NumberOfBytes] ProfileSize: Cardinal
): TNtxStatus;
var
ProfileInteractive: PInteractiveLogonProfile absolute Profile;
ProfileLm20: PLm20LogonProfile absolute Profile;
begin
Result.Location := 'LsaxCaptureLogonProfile';
if ProfileSize < SizeOf(TLogonProfileBufferType) then
begin
Result.Status := STATUS_INVALID_BUFFER_SIZE;
Exit;
end;
case TLogonProfileBufferType(Profile^) of
TLogonProfileBufferType.InteractiveProfile:
begin
if ProfileSize < SizeOf(TInteractiveLogonProfile) then
begin
Result.Status := STATUS_INVALID_BUFFER_SIZE;
Exit;
end;
Info.ValidFields := Info.ValidFields + [liLogonCount..liUserFlags];
Info.LogonCount := ProfileInteractive.LogonCount;
Info.BadPasswordCount := ProfileInteractive.BadPasswordCount;
Info.LogonTime := ProfileInteractive.LogonTime;
Info.LogoffTime := ProfileInteractive.LogoffTime;
Info.KickOffTime := ProfileInteractive.KickOffTime;
Info.PasswordLastSet := ProfileInteractive.PasswordLastSet;
Info.PasswordCanChange := ProfileInteractive.PasswordCanChange;
Info.PasswordMustChange := ProfileInteractive.PasswordMustChange;
Info.LogonScript := ProfileInteractive.LogonScript.ToString;
Info.HomeDirectory := ProfileInteractive.HomeDirectory.ToString;
Info.HomeDirectoryDrive := ProfileInteractive.HomeDirectoryDrive.ToString;
Info.FullName := ProfileInteractive.FullName.ToString;
Info.ProfilePath := ProfileInteractive.ProfilePath.ToString;
Info.LogonServer := ProfileInteractive.LogonServer.ToString;
Info.UserFlags := ProfileInteractive.UserFlags;
end;
TLogonProfileBufferType.Lm20LogonProfile:
begin
if ProfileSize < SizeOf(TLm20LogonProfile) then
begin
Result.Status := STATUS_INVALID_BUFFER_SIZE;
Exit;
end;
Info.ValidFields := Info.ValidFields + [liLogoffTime, liKickOffTime,
liLogonServer, liUserFlags, liUserSessionKey..liUserParameters];
Info.LogoffTime := ProfileLm20.LogoffTime;
Info.KickOffTime := ProfileLm20.KickOffTime;
Info.LogonServer := ProfileLm20.LogonServer.ToString;
Info.UserFlags := ProfileLm20.UserFlags;
Info.UserSessionKey := ProfileLm20.UserSessionKey;
Info.LogonDomainName := ProfileLm20.LogonDomainName.ToString;
Info.LanmanSessionKey := ProfileLm20.LanmanSessionKey;
Info.UserParameters := ProfileLm20.UserParameters.ToString;
end
else
Result.Status := STATUS_NOT_SUPPORTED;
Exit;
end;
Result.Status := STATUS_SUCCESS;
end;
function LsaxLogonUserInternal;
var
hToken: THandle;
LsaHandle: ILsaHandle;
AuthPkg: Cardinal;
OriginNameStr: TLsaAnsiString;
GroupArray: IMemory<PTokenGroups>;
GroupArrayData: Pointer;
ProfileBuffer: Pointer;
ProfileBufferLength: Cardinal;
ProfileBufferDeallocator: IAutoReleasable;
SubStatus: NTSTATUS;
begin
Info := Default(TLogonInfo);
GroupArrayData := nil;
// Connect to LSA
Result := LsaxConnectUntrusted(LsaHandle);
if not Result.IsSuccess then
Exit;
// Lookup the Negotiate package
Result := LsaxLookupAuthPackage(AuthPkg, PackageName, LsaHandle);
if not Result.IsSuccess then
Exit;
Result := RtlxInitAnsiString(OriginNameStr, OriginName);
if not Result.IsSuccess then
Exit;
Result.Location := 'LsaLogonUser';
Result.LastCall.UsesInfoClass(TLogonSubmitType(Buffer.Data^), icPerform);
if (Length(AdditionalGroups) > 0) or
(TLogonSubmitType(Buffer.Data^) = TLogonSubmitType.VirtualLogon) then
begin
// Note: The function requires SeTcbPrivilege when adding groups but
// returns ERROR_ACCESS_DENIED in place of ERROR_PRIVILEGE_NOT_HELD,
// which is confusing.
Result.LastCall.ExpectedPrivilege := SE_TCB_PRIVILEGE;
GroupArray := NtxpAllocGroups2(AdditionalGroups);
GroupArrayData := GroupArray.Data;
end;
SubStatus := STATUS_SUCCESS;
// Perform the logon
Result.Status := LsaLogonUser(LsaHandle.Handle, OriginNameStr, LogonType,
AuthPkg, Buffer.Data, Buffer.Size, GroupArrayData, TokenSource,
ProfileBuffer, ProfileBufferLength, Info.LogonId, hToken, Info.Quotas,
SubStatus);
// Prefer more detailed errors
if not Result.IsSuccess and not SubStatus.IsSuccess then
Result.Status := SubStatus;
if not Result.IsSuccess then
begin
// HACK: LsaLogonUser might return an HRESULT error instead of an NTSTATUS.
// As a rule of thumb, treat everything with warning severity and
// non-default facility as an HRESULT instead.
if (NT_SEVERITY(Result.Status) = SEVERITY_WARNING) and
(NT_FACILITY(Result.Status) <> FACILITY_NONE) then
Result.HResult := HResult(Result.Status);
Exit;
end;
Info.ValidFields := [liToken, liLogonId, liQuotas];
Info.hxToken := Auto.CaptureHandle(hToken);
if Assigned(ProfileBuffer) then
begin
ProfileBufferDeallocator := LsaxDelayFreeReturnBuffer(ProfileBuffer);
LsaxCaptureLogonProfile(Info, ProfileBuffer, ProfileBufferLength);
end;
end;
function LsaxMakeInteractiveBuffer(
out Buffer: IMemory<PInteractiveLogon>;
MessageType: TLogonSubmitType;
const Credentials: TLogonCredentials
): TNtxStatus;
var
Cursor: Pointer;
begin
IMemory(Buffer) := Auto.AllocateDynamic(
SizeOf(TInteractiveLogon) +
StringSizeZero(Credentials.Username) +
StringSizeZero(Credentials.Domain) +
StringSizeZero(Credentials.Password)
);
Buffer.Data.MessageType := MessageType;
Cursor := Buffer.Offset(SizeOf(TInteractiveLogon));
// Write the domain name
Result := RtlxMarshalUnicodeString(Credentials.Domain,
Buffer.Data.LogonDomainName, Cursor);
if not Result.IsSuccess then
Exit;
// Advace past the domain string
Inc(PByte(Cursor), Buffer.Data.LogonDomainName.MaximumLength);
// Write the user name
Result := RtlxMarshalUnicodeString(Credentials.Username, Buffer.Data.UserName,
Cursor);
if not Result.IsSuccess then
Exit;
// Advance past the user name
Inc(PByte(Cursor), Buffer.Data.UserName.MaximumLength);
// Write the password
Result := RtlxMarshalUnicodeString(Credentials.Password, Buffer.Data.Password,
Cursor);
end;
function LsaxMakeS4UBuffer(
out Buffer: IMemory<PS4ULogon>;
MessageType: TLogonSubmitType;
const Credentials: TLogonCredentials
): TNtxStatus;
var
Cursor: Pointer;
begin
IMemory(Buffer) := Auto.AllocateDynamic(
SizeOf(TS4ULogon) +
StringSizeZero(Credentials.Username) +
StringSizeZero(Credentials.Domain)
);
Buffer.Data.MessageType := MessageType;
Buffer.Data.Flags := Credentials.S4UFlags;
Cursor := Buffer.Offset(SizeOf(TS4ULogon));
// Write the user name
Result := RtlxMarshalUnicodeString(Credentials.Username,
Buffer.Data.UserPrincipalName, Cursor);
if not Result.IsSuccess then
Exit;
// Advance past the user name
Inc(PByte(Cursor), Buffer.Data.UserPrincipalName.MaximumLength);
// Write the domain
Result := RtlxMarshalUnicodeString(Credentials.Domain, Buffer.Data.DomainName,
Cursor);
end;
function LsaxLogonUser;
var
Buffer: IMemory;
begin
case MessageType of
TLogonSubmitType.InteractiveLogon, TLogonSubmitType.VirtualLogon,
TLogonSubmitType.NoElevationLogon:
Result := LsaxMakeInteractiveBuffer(IMemory<PInteractiveLogon>(Buffer),
MessageType, Credentials);
TLogonSubmitType.S4ULogon:
Result := LsaxMakeS4UBuffer(IMemory<PS4ULogon>(Buffer), MessageType,
Credentials);
else
Result.Location := 'LsaxLogonUser';
Result.Status := STATUS_NOT_IMPLEMENTED;
end;
if not Result.IsSuccess then
Exit;
Result := LsaxLogonUserInternal(Info, Buffer, LogonType, TokenSource,
AdditionalGroups, PackageName);
end;
end.