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

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   --...

Installtion of SQLMutant tool

This tool is perfectly works on ubuntu 24 system. And I found it is not working properly in kali linux 24 version.   https://github.com/blackhatethicalhacking/SQLMutant/tree/main This tool need to use along with sqlmap tool. Showing this cheat sheet for kali or debian based system.  This tool actually analyze everything and give you the vulnerable url where sql injection is possible. You just need to use then sqlmap to exploit that.   Prerequisite: apt install pipx -y (for ubuntu) pip3 install uro or pipx install uro pipx ensurepath pipx completions  (not needed)  source ~/.bashrc   or restart system If go tool is not installed then run the below two commands first ( golang-go ) or follow this link to install go (https://mahimfiroj.blogspot.com/2024/12/installing-nuclei-in-kali.html) otherwise skip this step.   dpkg -l | grep packagename (Using this command you can check package is installed or not) apt install gccgo-go -y or apt install gol...

Installing nuclei and go tool in kali

 First you need to install go: https://go.dev/doc/install You need to download this go tool go1.23.4.linux-amd64.tar.gz by clicking the Download button.  Say you are root and download the tool in your Downloads directory. Now run the below command: tar -C /usr/local -xzf go1.23.4.linux-amd64.tar.gz (if this cmd fails then you need to move this tool to /usr/local folder then run this cmd tar -xzf  go1.23.4.linux-amd64.tar.gz) Now add /usr/local/go/bin to the PATH environment variable. You can do this by adding the following line to your $HOME/.profile or /etc/profile (for a system-wide installation): export PATH=$PATH:/usr/local/go/bin Now use the following command for immediate effect. Preventing you from log off then log back in: source $HOME/.profile go version (to check it is installed successfully) Install nuclei: go install -v github.com/projectdiscovery/nuclei/v3/cmd/nuclei@latest or apt install nuclei nuclei -update-templates nuclei -u https://www.domain....