Reading Event Logs Smart (Part 1)

by Jun 13, 2018

When you query an event log with PowerShell, by default you get back a text message with the logged information. For example, if you’d like to know who logged on to your machine, you could use code like this (Administrator privileges required):

Get-EventLog -LogName Security -InstanceId 4624 | Select-Object -Property TimeGenerated, Message 

The result could look like this:

 ... 25.04.2018 07:48:41 An account was successfully logged on.... 25.04.2018 07:48:40 An account was successfully logged on.... 24.04.2018 18:18:17 An account was successfully logged on.... ... 

That’s not very meaningful because PowerShell shortens the output. In situations like this, you might want to pipe the result to Format-List:

Get-EventLog -LogName Security -InstanceId 4624 | Select-Object -Property TimeGenerated, Message | Format-List 

It now produces verbose data like this:

 PS> Get-EventLog -LogName Security -InstanceId 4624 | Select-Object -Property TimeGenerated, Message -first 1 | Format-List TimeGenerated : 25.05.2018 11:39:29 Message : An account was successfully logged on. Subject: Security ID: S-1-5-18 Account Name: DESKTOP-7AAMJLF$ Account Domain: WORKGROUP Logon ID: 0x3e7 Logon Information: Logon Type: 5 Restricted Admin Mode: - Virtual Account: %%1843 Elevated Token: %%1842 Impersonation Level: %%1833 New Logon: Security ID: S-1-5-18 Account Name: SYSTEM Account Domain: NT-AUTORITÄT Logon ID: 0x3e7 Linked Logon ID: 0x0 Network Account Name: - Network Account Domain: - Logon GUID: {00000000-0000-0000-0000-000000000000} Process Information: Process ID: 0x328 Process Name: C:\Windows\System32\services.exe Network Information: Workstation Name: - Source Network Address: - Source Port: - Detailed Authentication Information: Logon Process: Advapi Authentication Package: Negotiate Transited Services: - Package Name (NTLM only): - Key Length: 0 This event is generated when a logon session is created. It is generated on the computer that was accessed. The subject fields indicate the account on the local system which requested the logon. This is most commonly a service such as the Server service, or a local process such as Winlogon.exe or Services.exe. The logon type field indicates the kind of logon that occurred. The most common types are 2 (interactive) and 3 (network). The New Logon fields indicate the account for whom the new logon was created, i.e. the account that was logged on. The network fields indicate where a remote logon request originated. Workstation name is not always available and may be left blank in some cases. The impersonation level field indicates the extent to which a process in the logon session can impersonate. The authentication information fields provide detailed information about this specific logon request. - Logon GUID is a unique identifier that can be used to correlate this event with a KDC event. - Transited services indicate which intermediate services have participated in this logon request. - Package name indicates which sub-protocol was used among the NTLM protocols. - Key length indicates the length of the generated session key. This will be 0 if no session key was requested. 

That’s hard to handle. If you wanted to automate something based on this text, or create a report, you’d have to parse the text.

There is a much easier way: the message you see is just a text template, and Windows inserts the relevant information as “Replacement Strings”. They are part of the event data you receive from Get-EventLog. The data is stored as an array which always contains the same pieces of information per event ID.

When you figure out which information is stored in which array element, it is very easy to extract the information you are after:

Get-EventLog -LogName Security -InstanceId 4624 | ForEach-Object { # translate the raw data into a new object  [PSCustomObject]@{ Time = $_.TimeGenerated User = "{0}\{1}" -f $_.ReplacementStrings[5], $_.ReplacementStrings[6] Type = $_.ReplacementStrings[10] Path = $_.ReplacementStrings[17] } } 

When you run this simple code, it returns beautiful authentication info with just the pieces of information you required:

 12.05.2018 17:38:58 SYSTEM\NT-AUTORITÄT Negotiate C:\Windows\System32\services.exe 12.05.2018 17:38:58 tobweltner@zumsel.local\InternalAccount Negotiate C:\Windows\System32\svchost.exe 12.05.2018 17:38:58 SYSTEM\NT-AUTORITÄT Negotiate C:\Windows\System32\services.exe 12.05.2018 17:38:58 SYSTEM\NT-AUTORITÄT Negotiate C:\Windows\System32\services.exe 12.05.2018 17:38:53 SYSTEM\NT-AUTORITÄT Negotiate C:\Windows\System32\services.exe 

Twitter This Tip! ReTweet this Tip!