The Windows Event Logs are not text files that can be viewed using a text editor. However, the raw data can be translated into XML using the Windows API. The events in these log files are stored in a proprietary binary format with a .evt or .evtx extension. The log files with the .evtx file extension typically reside in C:\Windows\System32\winevt\Logs
Event logs classified into types:
There are three main ways of accessing these event logs within a Windows system:
PS> C:\Users\Administrator> wevtutil.exe /? Elenco dei comandi/opzioniEsempi utili:
Per utilizzare wevtutil.exe e ottenere il numero dei nomi dei log, puoi eseguire il seguente comando in PowerShell:
wevtutil el | Measure-Object Questo comando elenca tutti i log disponibili e poi conta il numero di log utilizzando Measure-Object. Il risultato sarà un conteggio dei log presenti nel sistema.Per utilizzare il comando wevtutil con query-events, puoi usare la seguente sintassi:
wevtutil qe /q:"" /f:text wevtutil qe Application /q:"*/System[EventID=100]" /f:text (esempio per ottenere gli eventi dal log 'Application' per EventID 100 in formato testo)wevtutil qe /? help per wevtutil con opzione query/{lf | logfile}:[true|false] If true, wevtutil qe Application /c:3 /rd:true /f:text
This is a PowerShell cmdlet called Get-WinEvent. Per Microsoft, the Get-WinEvent cmdlet "gets events from event logs and event tracing log files on local and remote computers." It provides information on event logs and event log providers. Additionally, you can combine numerous events from multiple sources into a single command and filter using XPath queries, structured XML queries, and hash table queries.
Guida ufficiale su Get-WinEventGet-WinEvent -ListLog * Get all logs from a computer Get-WinEvent -ListProvider * Get event log providers and log namesGet-WinEvent -LogName Application | Where-Object { $_.ProviderName -Match 'WLMS' } Log filteringWhen working with large event logs, per Microsoft, it's inefficient to send objects down the pipeline to a Where-Object command. The use of the Get-WinEvent cmdlet's FilterHashtable parameter is recommended to filter event logs.
Get-WinEvent -FilterHashtable @{
LogName='Application'
ProviderName='WLMS'
}
FilterHashtable per Livello: Livelli evento (syslog / Windows)
⚠️ In Windows Event Log, di solito: Level = 4 → Information
Esempio:
Get-WinEvent -FilterHashtable @{
LogName = 'Application'
Level = 4
StartTime = (Get-Date).AddDays(-1)
}
The valid Get-WinEvent key/value pairs are as follows:
When building a query with a hash table, Microsoft recommends making the hash table one key-value pair at a time. Event Viewer can provide quick information on what you need to build your hash table.
Get-WinEvent - PS 7.5Get-WinEvent -ListLog *OpenSSH* elenca i Log con il nome contenuto nella stringa tra gli *(Get-WinEvent -ListProvider Microsoft-Windows-GroupPolicy).Events |
Format-Table Id, Description
This command lists the Event Ids that the Microsoft-Windows-GroupPolicy event provider generates along with the event description.$Date = (Get-Date).AddDays(-2)
Get-WinEvent -FilterHashtable @{ LogName='Application'; StartTime=$Date; Id='1003' } The Get-WinEvent cmdlet gets log information. The FilterHashtable parameter is used to filter the output. The LogName key specifies the value as the Application log. The StartTime key uses the value stored in the $Date variable. The Id key uses an Event Id value, 1003.
Esempio: Filter Event log results
# Using the Where-Object cmdlet:
$Yesterday = (Get-Date) - (New-TimeSpan -Day 1)
Get-WinEvent -LogName 'Windows PowerShell' | Where-Object { $_.TimeCreated -ge $Yesterday }
# Using the FilterHashtable parameter:
$Yesterday = (Get-Date) - (New-TimeSpan -Day 1)
Get-WinEvent -FilterHashtable @{ LogName='Windows PowerShell'; Level=3; StartTime=$Yesterday }
# Using the FilterXML parameter: (<> equivale a < , perchè mi dava errore di visualizzazione)
$xmlQuery = @'
<>QueryList>
<>Query Id="0" Path="Windows PowerShell">
<>Select Path="System">*[System[(Level=3) and
TimeCreated[timediff(@SystemTime) <= 86400000]]]
<>/Query>
<>/QueryList>
'@
Get-WinEvent -FilterXML $xmlQuery
# Using the FilterXPath parameter:
$XPath = '*[System[Level=3 and TimeCreated[timediff(@SystemTime) <= 86400000]]]'
Get-WinEvent -LogName 'Windows PowerShell' -FilterXPath $XPath
The W3C created XPath, or XML Path Language in full, to provide a standard syntax and semantics for addressing parts of an XML document and manipulating strings, numbers, and booleans . The Windows Event Log supports a subset of XPath 1.0
Get-WinEvent -LogName Application -FilterXPath '*'Get-WinEvent -LogName Application -FilterXPath '*/System/'Get-WinEvent -LogName Application -FilterXPath '*/System/EventID=100'Get-WinEvent -LogName Application -FilterXPath '*/System/Provider[@Name="WLMS"]'Get-WinEvent -LogName Application -FilterXPath '*/System/EventID=101 and */System/Provider[@Name="WLMS"]'Get-WinEvent -LogName Security -FilterXPath '*/EventData/Data[@Name="TargetUserName"]="System"'Nota Bene: Prendendo i dettagli dal file XML, è facile scomporre la struttura e creare qualsiasi query ( ho scritto in sequenza le query per velocizzare la comprensione! )
Esempi:
Get-WinEvent -LogName System |
Group-Object Id |
Sort-Object Count -Descending
Elenca tutti gli Eventi ID presenti nel lognameGet-WinEvent -ListLog * |
Select-Object -First 10 LogName
I primi 10 "Logname"Get-WinEvent -LogName Security |
Group-Object Id |
Sort-Object Count -Descending |
Select-Object -First 10 Name, Count
(dopo Group-Object non esiste più Id, ma Name (che rappresenta l’Event ID).)Get-WinEvent -ListLog * |
Select-Object LogName |
Sort-Object LogName
Elenca tutti i Logname disponibiliGet-WinEvent -LogName Application -FilterXPath '*/System/Provider[@Name="WLMS"] and */System/
TimeCreated[@SystemTime="2020-12-15T01:09:08.940277500Z"]' Elenca log con quel Provider name e dataGet-WinEvent -LogName Security -FilterXPath ‘*/EventData/Data[@Name=”TargetUserName”]=”Sam” and */System/EventID=4720’ Combo di 2 query con event ID e UserNameGet-WinEvent -Path "C:\Users\Administrator\Desktop\merged.evtx" | Where-Object { $_.Id -eq 1102 } Ricerca Event ID = 1102 da file log esterno Get-WinEvent -Path "C:\Users\Administrator\Desktop\merged.evtx" | Where-Object {
$_.ProviderName -eq "EventLog"
} Ricerca ProviderName = EventLog da file log esterno Get-WinEvent -Path "C:\Users\Administrator\Desktop\merged.evtx" | Where-Object { $_.Id -gt 9999 } Ricerca Event ID > 9999 da file log esterno Get-WinEvent -Path "C:\Logs\Application.evtx" |
Where-Object {
$_.ProviderName -eq "EventLog" -and
$_.Id -gt 9999
} Combo Ricerca Event ID > 9999 + EventLog da file esternoGet-WinEvent -Path "C:\Logs\Application.evtx" |
Where-Object { $_.Id -gt 9999 } |
Select-Object TimeCreated, Id, ProviderName, LevelDisplayName
Ricerca event id > 9999 da file esterno, con colonne selezionateGet-WinEvent -LogName Security -MaxEvents 5 |
ForEach-Object { $_.ToXml() } Legge i primi 5 elementi del log e ci fa vedere XMLGet-WinEvent -Path "C:\Users\Administrator\Desktop\merged.evtx" | Where-Object { $_.Id -eq 104 -and $_.TimeCreated -eq "3/19/2019 4:34:25 PM"} | ForEach-Object {$_.ToXml()} da log esterno, event id = 104, tempo definito e risultato XMLGet-WinEvent -Path "C:\Users\Administrator\Desktop\merged.evtx" -FilterXPath '*/EventData/Data[@Name="CallerProcessName"]="C:\Windows\System32\net1.exe"'
- ricerca da file esterno dato nome del processoRisorse Utilissime:
Note: Some events will not be generated by default, and certain features will need to be enabled/configured on the endpoint, such as PowerShell logging. This feature can be enabled via Group Policy or the Registry.
Local Computer Policy > Computer Configuration > Administrative Templates > Windows Components > Windows PowerShell
Another feature to enable/configure is Audit Process Creation, which will generate event ID 4688. This will allow command-line process auditing. This setting is NOT enabled in the virtual machine but feel free to enable it and observe the events generated after executing some commands.
Local Computer Policy > Computer Configuration > Administrative Templates > System > Audit Process Creation
è una tecnica in cui un attaccante sfrutta le versioni precedenti di PowerShell per eludere le misure di sicurezza
Ricerca su MITRE: Impair Defenses: Downgrade Attack : ID: T1562.010 , Sub-technique of: T1562 , Tactic: Defense Evasion
Detecting Downgrade Attacks : DET0350
WinEventLog:Sysmon EventCode=1WinEventLog:Security EventCode=4657Abuse of PowerShell for Arbitrary Execution Technique Detected: PowerShell | T1059.001 , ID: DET0455
WinEventLog:Sysmon EventCode=1WinEventLog:PowerShell EventCode=4103, 4104, 4105, 4106WinEventLog:PowerShell EventCode=400, 403WinEventLog:Sysmon EventCode=7Process Metadata: Contextual data about a running process, which may include information such as environment variables, image name, user/owner, etc.
WinEventLog:PowerShell EventCode=400, 403