-
Notifications
You must be signed in to change notification settings - Fork 35
/
NtUtils.Lsa.Logon.pas
150 lines (116 loc) · 3.27 KB
/
NtUtils.Lsa.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
unit NtUtils.Lsa.Logon;
{
This module enumerating and retrieving information about active logon sessions
}
interface
uses
Ntapi.WinNt, Ntapi.NtSecApi, NtUtils, DelphiUtils.AutoObjects;
type
ILogonSession = IMemory<PSecurityLogonSessionData>;
// Enumerate logon sessions
function LsaxEnumerateLogonSessions(
out Luids: TArray<TLogonId>
): TNtxStatus;
// Query logon session information
function LsaxQueryLogonSession(
const LogonId: TLogonId;
out Data: ILogonSession
): TNtxStatus;
// Construct a SID for one of well-known logon sessions. May return nil.
[Result: opt]
function LsaxLookupKnownLogonSessionSid(
const LogonId: TLogonId
): ISid;
implementation
uses
NtUtils.Security.Sid, NtUtils.Processes.Info;
{$BOOLEVAL OFF}
{$IFOPT R+}{$DEFINE R+}{$ENDIF}
{$IFOPT Q+}{$DEFINE Q+}{$ENDIF}
type
TLsaAutoMemory = class (TCustomAutoMemory, IMemory, IAutoPointer, IAutoReleasable)
procedure Release; override;
end;
{ TLogonAutoMemory }
procedure TLsaAutoMemory.Release;
begin
if Assigned(FData) then
LsaFreeReturnBuffer(FData);
FData := nil;
inherited;
end;
function LsaxDelayFreeReturnBuffer(
[in] Buffer: Pointer
): IAutoReleasable;
begin
Result := Auto.Delay(
procedure
begin
LsaFreeReturnBuffer(Buffer);
end
);
end;
{ Functions }
function LsaxEnumerateLogonSessions;
var
Count, i: Integer;
Buffer: PLuidArray;
BufferDeallocator: IAutoReleasable;
HasAnonymousLogon: Boolean;
begin
Result.Location := 'LsaEnumerateLogonSessions';
Result.Status := LsaEnumerateLogonSessions(Count, Buffer);
if not Result.IsSuccess then
Exit;
BufferDeallocator := LsaxDelayFreeReturnBuffer(Buffer);
SetLength(Luids, Count);
// Invert the order so that later logons appear later in the list
for i := 0 to High(Luids) do
Luids[i] := Buffer{$R-}[Count - 1 - i]{$IFDEF R+}{$R+}{$ENDIF};
// Make sure anonymous logon is in the list (most likely it is not)
HasAnonymousLogon := False;
for i := 0 to High(Luids) do
if Luids[i] = ANONYMOUS_LOGON_LUID then
begin
HasAnonymousLogon := True;
Break;
end;
if not HasAnonymousLogon then
Insert(ANONYMOUS_LOGON_LUID, Luids, 0);
end;
function LsaxQueryLogonSession;
var
Buffer: PSecurityLogonSessionData;
begin
{$IFDEF Win32}
// LsaGetLogonSessionData returns an invalid pointer under WoW64
if RtlxAssertNotWoW64(Result) then
Exit;
{$ENDIF}
Result.Location := 'LsaGetLogonSessionData';
Result.Status := LsaGetLogonSessionData(LogonId, Buffer);
if not Result.IsSuccess then
Exit;
// Fix missing logon ID
if Buffer.LogonId = 0 then
Buffer.LogonId := LogonId;
IMemory(Data) := TLsaAutoMemory.Capture(Buffer, Buffer.Size);
end;
function LsaxLookupKnownLogonSessionSid;
begin
case LogonId of
SYSTEM_LUID:
Result := RtlxMakeSid(SECURITY_NT_AUTHORITY, [SECURITY_LOCAL_SYSTEM_RID]);
ANONYMOUS_LOGON_LUID:
Result := RtlxMakeSid(SECURITY_NT_AUTHORITY, [SECURITY_ANONYMOUS_LOGON_RID]);
LOCALSERVICE_LUID:
Result := RtlxMakeSid(SECURITY_NT_AUTHORITY, [SECURITY_LOCAL_SERVICE_RID]);
NETWORKSERVICE_LUID:
Result := RtlxMakeSid(SECURITY_NT_AUTHORITY, [SECURITY_NETWORK_SERVICE_RID]);
IUSER_LUID:
Result := RtlxMakeSid(SECURITY_NT_AUTHORITY, [SECURITY_IUSER_RID]);
else
Result := nil;
end;
end;
end.