|
Per enumerare le sessioni sul computer locale e restituire le informazioni
principali di ognuna, useremo principalmente le seguenti 2 funzioni esportate
dalla dll secur32.dll:
1) LsaEnumerateLogonSessions: si occupa dell'enumerazione
2) LsaGetLogonSessionData: restituisce informazioni sulla singola
sessione
L'implementazione coinvolge un certo numero di strutture ed ulteriori api
win32 per cui opterei per il classico approccio "Meglio un esempio oggi che
tanta teoria fino a dopodomani"
1. Unit con la dichiarazione delle funzioni e la
definizione dei tipi
unit uTypes;
interface
const
secur32 = 'secur32.dll';
type
PULONG = ^Cardinal;
PLargeInteger = ^LARGE_INTEGER;
LARGE_INTEGER = record
case Integer of
0: (
LowPart: Cardinal;
HighPart: Integer);
1: (
QuadPart: Int64);
end;
PLuid = ^LUID;
LUID = record
LowPart: Cardinal;
HighPart: integer;
end;
PLSA_UNICODE_STRING = ^LSA_UNICODE_STRING;
LSA_UNICODE_STRING = record
Length: Word;
MaximumLength: Word;
Buffer: PWideChar;
end;
PSID_IDENTIFIER_AUTHORITY = ^SID_IDENTIFIER_AUTHORITY;
SID_IDENTIFIER_AUTHORITY = record
Value: array [0..5] of Byte;
end;
PSid = ^SID;
SID = record
Revision: Byte;
SubAuthorityCount: Byte;
IdentifierAuthority: SID_IDENTIFIER_AUTHORITY;
SubAuthority: array [0..0] of Cardinal; //array di qualsiasi dimensione
end;
PSECURITY_LOGON_TYPE = ^SECURITY_LOGON_TYPE;
SECURITY_LOGON_TYPE = (
seltFiller0, seltFiller1,
Interactive, // Interactively logged on (locally or remotely)
Network, // Accessing system via network
Batch, // Started via a batch queue
Service, // Service started by service controller
Proxy, // Proxy logon
Unlock, // Unlock workstation
NetworkCleartext, // Network logon with cleartext credentials
NewCredentials, // Clone caller, new default credentials
RemoteInteractive, // Remote, yet interactive. Terminal server
CachedInteractive, // Try cached credentials without hitting the net.
CachedRemoteInteractive, // Same as RemoteInteractive,
//this is used internally for auditing purpose
CachedUnlock); // Cached Unlock workstation
PSECURITY_LOGON_SESSION_DATA = ^SECURITY_LOGON_SESSION_DATA;
SECURITY_LOGON_SESSION_DATA = record
Size: Cardinal;
LogonId: LUID;
UserName: LSA_UNICODE_STRING;
LogonDomain: LSA_UNICODE_STRING;
AuthenticationPackage: LSA_UNICODE_STRING;
LogonType: SECURITY_LOGON_TYPE;
Session: Cardinal;
Sid: PSID;
LogonTime: LARGE_INTEGER;
LogonServer: LSA_UNICODE_STRING;
DnsDomainName: LSA_UNICODE_STRING;
Upn: LSA_UNICODE_STRING;
end;
PLSA_OBJECT_ATTRIBUTES = ^LSA_OBJECT_ATTRIBUTES;
LSA_OBJECT_ATTRIBUTES = record
Length: Cardinal;
RootDirectory: Cardinal;
ObjectName: PLSA_UNICODE_STRING;
Attributes: Cardinal;
//puntatore a SECURITY_DESCRIPTOR
SecurityDescriptor: Pointer;
//puntatore a SECURITY_QUALITY_OF_SERVICE
SecurityQualityOfService: Pointer;
end;
function LsaEnumerateLogonSessions(LogonSessionCount: PULONG;
var LogonSessionList: PLUID): integer; stdcall;
function LsaFreeReturnBuffer(Buffer: pointer): Integer; stdcall;
function LsaGetLogonSessionData(LogonId: PLUID;
var ppLogonSessionData: PSECURITY_LOGON_SESSION_DATA): Integer; stdcall;
implementation
function LsaEnumerateLogonSessions; external secur32 name 'LsaEnumerateLogonSessions';
function LsaFreeReturnBuffer; external secur32 name 'LsaFreeReturnBuffer';
function LsaGetLogonSessionData; external secur32 name 'LsaGetLogonSessionData';
end.
2. Procedura per elencare le sessioni ed esporre le caratteristiche di ognuna
function FileTimeToDateTime(const FileTime: Int64): TDateTime;
const
FileTimeBase = -109205.0;
FileTimeStep: Extended = 24.0 * 60.0 * 60.0 * 1000.0 * 1000.0 * 10.0; // 100 nSek per Day
begin
Result := Int64(FileTime) / FileTimeStep;
Result := Result + FileTimeBase;
end;
function TipoLogon(LogonType: SECURITY_LOGON_TYPE): string;
begin
case LogonType of
Interactive : Result := 'interactive';
Network : Result := 'network';
Batch : Result := 'batch';
Service : Result := 'service';
Proxy : Result := 'proxy';
Unlock : Result := 'unlock';
NetworkCleartext : Result := 'networkcleartext';
NewCredentials : Result := 'newcredentials';
RemoteInteractive : Result := 'remoteinteractive';
CachedInteractive : Result := 'cached interactive';
CachedRemoteInteractive : Result := 'cachedremoteinteractive';
CachedUnlock : Result := 'cachedunlock';
end;
end;
procedure InformazioniSessione(session: PLUID);
var
sessionData: PSECURITY_LOGON_SESSION_DATA;
retval: Longint;
buffer: pWideChar;
usBuffer: pWideChar;
usLength: Longint;
begin
LsaGetLogonSessionData (session, sessionData);
Form1.Memo1.Lines.Add('');
Form1.Memo1.Lines.Add('Tipo logon: ' +
TipoLogon(sessionData.LogonType));
Form1.Memo1.Lines.Add('Nome Account: ' +
sessionData.UserName.Buffer);
Form1.Memo1.Lines.Add('Logon Domain: ' +
sessionData.LogonDomain.Buffer);
Form1.Memo1.Lines.Add('Pacchetto di autenticazione: ' +
sessionData.AuthenticationPackage.Buffer);
Form1.Memo1.Lines.Add('Sessione: ' +
IntToStr(sessionData.Session));
Form1.Memo1.Lines.Add('Tempo di sessione: ' +
DateTimeToStr(FileTimeToDateTime(sessionData.LogonTime.QuadPart)));
//per Windows XP /////
Form1.Memo1.Lines.Add('Logon server: ' +
sessionData.LogonServer.Buffer);
Form1.Memo1.Lines.Add('Nome dns: ' +
sessionData.DnsDomainName.Buffer);
Form1.Memo1.Lines.Add('Upn: ' +
sessionData.Upn.Buffer);
//////////////////////
LsaFreeReturnBuffer(sessionData);
end;
procedure ElencoSessioni;
var
NumeroSessioniLogon: Cardinal;
LogonSessionList: PLUID;
i: integer;
begin
//
LsaEnumerateLogonSessions(@NumeroSessioniLogon, LogonSessionList);
for i := 0 to NumeroSessioniLogon - 1 do
begin
InformazioniSessione(LogonSessionList);
Inc(LogonSessionList);
end;
LsaFreeReturnBuffer(LogonSessionList);
end;
3. Esempio di output
Tipo logon: interactive
Nome Account: __vmware_user__
Logon Domain: NOME-0277EF807C
Pacchetto di autenticazione: NTLM
Sessione: 0
Tempo di sessione: 19/01/2007 13.30.47
Logon server: NOME-0277EF807C
Nome dns:
Upn:
Tipo logon: network
Nome Account: ACCESSO ANONIMO
Logon Domain: NT AUTHORITY
Pacchetto di autenticazione: NTLM
Sessione: 0
Tempo di sessione: 19/01/2007 13.30.40
Logon server:
Nome dns:
Upn:
Tipo logon: interactive
Nome Account: a
Logon Domain: NOME-0277EF807C
Pacchetto di autenticazione: NTLM
Sessione: 0
Tempo di sessione: 19/01/2007 13.30.34
Logon server: NOME-0277EF807C
Nome dns:
Upn:
Tipo logon: service
Nome Account: SERVIZIO LOCALE
Logon Domain: NT AUTHORITY
Pacchetto di autenticazione: Negotiate
Sessione: 0
Tempo di sessione: 19/01/2007 13.30.23
Logon server:
Nome dns:
Upn:
Tipo logon: service
Nome Account: SERVIZIO DI RETE
Logon Domain: NT AUTHORITY
Pacchetto di autenticazione: Negotiate
Sessione: 0
Tempo di sessione: 19/01/2007 13.30.22
Logon server:
Nome dns:
Upn:
Tipo logon:
Nome Account:
Logon Domain:
Pacchetto di autenticazione: NTLM
Sessione: 0
Tempo di sessione: 19/01/2007 13.30.21
Logon server:
Nome dns:
Upn:
Tipo logon:
Nome Account: NOME-0277EF807C$
Logon Domain: MSHOME
Pacchetto di autenticazione: NTLM
Sessione: 0
Tempo di sessione: 19/01/2007 13.30.21
Logon server:
Nome dns:
Upn:
sessioni_windows
|