Skip to main content

Xpath query | Powershell | event logs

Following xpath expression you need to do while searching for event mentioning event id:

-FilterXPath 'Events/System/EventId=4624'

the following two queries are same because eventid appears from only one single place in the schema. 

-FilterXPath '*/System/EventId=4624'

or

-FilterXPath '*/*/EventId=4624'

 

for example, when you are given a .evtx file to query:

Get-WinEvent -Path C:\Users\ContosoAdmin\Desktop\SecurityLog.evtx -FilterXPath '*/System/EventID=4624' -MaxEvents 1

C:\Windows\system32>wevtutil.exe qe C:\Users\ContosoAdmin\Desktop\SecurityLog.evtx /lf:true /q:*/System/EventID=4624 /c:1 /rd:true /f:text

 

Query events based on time:

# Query on a specific timestamp (must go to milliseconds!)

Get-WinEvent -LogName System -FilterXPath '*/*/TimeCreated[@SystemTime="2020-03-08T16:24:27.042Z"]'

 

# Query events before a date

Get-WinEvent -LogName System -FilterXPath '*/System/TimeCreated[(@SystemTime<"2020-03-01T00:00:00Z")]'

 

# Query events in a given hour of a day

Get-WinEvent -LogName System -FilterXPath '*/System/TimeCreated[(@SystemTime>"2020-02-11T11:00:00Z") and (@SystemTime<"2020-02-11T12:00:00Z")]'

 

# Query events in the last 24 hours (86400000 ms)

Get-WinEvent -LogName System -FilterXPath '*/*/TimeCreated[timediff(@SystemTime) <= 86400000]'

Get-WinEvent -Path C:\Users\ContosoAdmin\Desktop\SystemLog.evtx -FilterXPath '*/System/TimeCreated[@SystemTime="2019-12-13T08:24:27.5440626"]'

 

Query by username:

Get-WinEvent -Path C:\Users\ContosoAdmin\Desktop\SecurityLog.evtx -MaxEvents 1 -FilterXPath '*/EventData/Data[@Name="TargetUserName"]="eleanor"'

or 

Get-WinEvent -Path C:\Users\ContosoAdmin\Desktop\SecurityLog.evtx -MaxEvents 1 -FilterXPath '*/*/Data[@Name="TargetUserName"]="eleanor"'

 

combining queries:

Get-WinEvent -Path C:\Users\ContosoAdmin\Desktop\SecurityLog.evtx -FilterXPath '*/System/EventID=4624 and */EventData/Data[@Name="TargetUserName"]="eleanor"' -MaxEvents 1

 

Get-WinEvent -Path C:\Users\ContosoAdmin\Desktop\SecurityLog.evtx -FilterXPath '*/System/EventID=4624 and */System/TimeCreated[@SystemTime>"2020-02-26T00:00:00"] and */EventData/Data[@Name="TargetUserName"]="eleanor"' -Oldest -MaxEvents 1

 

 

Reverse sort order:

-Oldest

Get-WinEvent -path C:\Users\ContosoAdmin\Desktop\SecurityLog.evtx -FilterXPath '*/System/EventID=4624 and */EventData/Data[@Name="TargetUserName"]="eleanor"' -Oldest -MaxEvents 1

 

Application query:

package installation event id is 1033. what is the product name of msi that was installed on 02/11/2020 around 11 am?

Get-WinEvent -Path C:\Users\ContosoAdmin\Desktop\ApplicationLog.evtx -FilterXPath '*/*/EventID=1033 and */*/TimeCreated[@SystemTime<"2020-02-11T12:00:00"]'

 

 shutdown events query:

what was the first shutdown time on 02/03/2020? shutdown event id is 13. 

Get-WinEvent -Path C:\Users\ContosoAdmin\Desktop\SystemLog.evtx -FilterXPath '*/*/EventID=13 and */*/TimeCreated[@SystemTime>"2020-02-03T00:00:00"]' -Oldest -MaxEvents 1

 

 

Get-Winevent -Path C:\Users\ContosoAdmin\Desktop\SecurityLog.evtx -FilterXPath '*/*/EventID=4616 and */*/Data[@Name="SubjectDomainName"]="NT AUTHORITY"'

 

Get-Winevent -Path C:\Users\ContosoAdmin\Desktop\SecurityLog.evtx -FilterXPath '*/*/EventID=4624 and */*/Data[@Name="TargetUserName"]="NETWORK SERVICE" and */*/TimeCreated[@SystemTime>"2020-02-26T00:00:00"]' | Format-List -Property Message

 

From user system, say user system is amy.contoso.azure query to domain controller to check users access log:

Get-WinEvent -LogName 'Security' -FilterXPath '*/*/Data="bob"' -MaxEvents 20 -ComputerName dc.contoso.azure
 

Get-WinEvent -LogName 'Security' -FilterXPath '*/*/EventID="4624" and */*/Data="nadia"' -MaxEvents 20 -ComputerName dc.contoso.azure


Detection pass the hash log on to dc using logon type 9:

say from your system you use mimikatz to pass the hash login to the dc. how can we detect those? by doing Pass-the-hash using Mimikatz on your desktop, you left a trace in the security log — a cached interactive logon type 9

Get-WinEvent -LogName 'Security' -FilterXPath '*/*/EventID="4624" and */*/Data="nadia"' -MaxEvents 20  | Format-List | findstr /R /C:'Logon Type:'
 

Get-WinEvent -LogName 'Security' -FilterXPath '*/*/EventID="4624" and */*/Data[@Name="LogonType"]="9"' | Format-List | findstr /R \<Network.Account.Name
 

 

 

 

Avi

 

 

 

 

 

 


 

 

 

 

 

 

Comments

Popular posts from this blog

API hacking lab setup

 Follow the commands to install and configure API hacking lab: 1. Install kali linux and update all the packages.  apt update -y apt upgrade -y or apt dist-upgrade -y or apt full-upgrade -y If you face any problem regarding update, install cloud flare warp in the host machine, then again start updating packages in your kali vm.  2. Install and configure burpsuite professional.  After that open burpsuite and go to Extensions tab. Click on BAppStore. Search for Autorize extension, It will help us to automate authorization testing. Click on Download Jython from the right side. From Jython website click on Jython standalone JAR and save it. Go to Extensios > Extensions settings >  under Core extension settings find out Python environment on the right pane. Select the jython jar file that you just downloaded. Now again go to BAppStore and re-search for Autorize extension. You will see Install option this time after selecting Autorize extension. Install it. You ...

Install Nessus from docker

Docker installation. Give the below commands one by one. apt install docker-cli or apt install docker.io After the installation is complete, if you are inside wsl then give this command to start docker, because inside wsl systemd (systemctl) does not work: service docker start WSL troubleshooting : If the above command " service docker start " does not work then use below command: dockerd (It may not work if any previous docker process is running. It will show you pid of that process. Use this command to kill that process " kill -9 pid " and run dockerd command again) If " docker ps -a " giving error like " Cannot connect to the Docker daemon at unix:///run/podman/podman.sock. Is the docker daemon running? " This is because you may installed podman-docker package. If you remove the package still you will get this error but you should remove the package. Then issue this command: env | grep -i docker DOCKER_HOST=unix:///run/podman/podman.sock   --...

Installing kansa incident response tool

 Kansa is an IR framework. https://github.com/davehull/Kansa For enterprise data collection, you need to do this first from the admin system: Set-NetConnectionProfile -NetworkCategory Private (In private network) Enable-PSRemoting  from powershell on the system where you want to run this tool. This will enable winrm service with port 5985 and 5986. Check: netstat -naob | findstr "5985"   Also allow tcp port 5985 and 5986 for winrm through the network. You can use  GPO. Though winrm is communicating over http and https but authentication will be happened using kerberos in domain environment.  After downloading it  from the github and unzip it, you need to unlock it using powershell. Need powershell v3 or later. ls -r *.ps1 | Unblock-File Powershell policy bypass: Set-ExecutionPolicy AllSigned | RemoteSigned | Unrestricted From FOR508 course: .\kansa.ps1 -OutputPath .\Output\ -TargetList .\hostlist -TargetCount 250 -Verbose -Pushbin -Pushbin is requir...