Proactive Threat Detection · Topic 5 of 5
Threat Hunting with PowerShell, YARA and Tools
Learn how hunters use PowerShell cmdlets, YARA rules, Sysmon events and tools such as EDR, osquery and Velociraptor to find hidden threats for CSA 312-39.
- 5 min
- PowerShell cmdlets such as Get-WinEvent, Get-CimInstance, Get-NetTCPConnection and Get-ScheduledTask answer hunting questions on hosts.
- Script Block Logging writes event 4104 and exposes obfuscated PowerShell.
- A YARA rule has meta, strings and a condition; the condition decides the match.
What it is
A hunting hypothesis needs tools to test it. Three groups matter for the exam:
- PowerShell, built into Windows, to query events, processes, connections, services and persistence locations on one or many hosts.
- YARA, a pattern matching tool that identifies malware and suspicious files with rules based on strings and conditions.
- Hunting tools and telemetry, such as Sysmon, EDR, osquery, Velociraptor, Zeek and the SIEM, that collect and search data at scale.
How it works
Hunting with PowerShell
Short cmdlets answer common hunting questions. They read data; they do not change the system.
| Question | Command |
|---|---|
| Failed logons in the Security log | Get-WinEvent -FilterHashtable @{LogName='Security'; Id=4625} |
| Running processes, including parent process ID and command line | Get-CimInstance Win32_Process |
| Active network connections | Get-NetTCPConnection -State Established |
| Scheduled tasks | Get-ScheduledTask |
| Run key persistence | Get-ItemProperty 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Run' |
| Services, including the path to each binary | Get-CimInstance Win32_Service |
| Members of local Administrators | Get-LocalGroupMember -Group 'Administrators' |
| Hash of a suspicious file | Get-FileHash -Algorithm SHA256 C:\Temp\update.exe |
Piping the output to Select-Object keeps only the fields of interest, such as CommandLine or PathName. Invoke-Command -ComputerName runs the same query on many remote hosts through PowerShell remoting, which lets one hunter compare a whole server group.
Attackers also abuse PowerShell, so hunters look for it in logs. Script Block Logging writes event 4104 to the Microsoft-Windows-PowerShell/Operational log, recording the code that ran even when it was obfuscated. Signs worth hunting include encoded commands (the -EncodedCommand parameter), download cradles that fetch and run remote scripts, and PowerShell started by Office applications.
Hunting with YARA
A YARA rule has a name, optional meta information, strings to look for, and a condition that decides a match. Strings can be text, hexadecimal bytes or regular expressions.
rule Suspicious_Credential_Tool
{
meta:
description = "Flags Windows executables that contain credential dumping tool strings"
strings:
$mz = { 4D 5A }
$s1 = "sekurlsa::logonpasswords" nocase
$s2 = "mimikatz" nocase
condition:
$mz at 0 and any of ($s1, $s2)
}
The condition requires the MZ header at offset 0 (a Windows executable) and at least one of the two strings. A scan of a folder and its subfolders looks like yara -r rules.yar C:\Users. YARA can also scan a running process's memory by process ID.
Hunters write YARA rules from malware analysis or intelligence reports, then sweep endpoints, file shares and email attachments for matches. Many EDR and sandbox products accept YARA rules directly.
Sysmon and other hunting tools
Sysmon (System Monitor, part of Microsoft Sysinternals) adds detailed telemetry to the Windows event log.
| Sysmon event ID | Records |
|---|---|
| 1 | Process creation, with command line, hashes and parent process |
| 3 | Network connection |
| 7 | Image (DLL) loaded |
| 8 | CreateRemoteThread, a sign of injection |
| 10 | Process access, such as a tool opening LSASS |
| 11 | File created |
| 12, 13, 14 | Registry object created or deleted, value set, key or value renamed |
| 22 | DNS query |
| Tool | What it does for a hunter |
|---|---|
| EDR / XDR | Collects endpoint telemetry fleet wide and allows searches and isolation |
| osquery | Exposes the operating system as tables queried with SQL |
| Velociraptor | Open source endpoint collection and hunting across many hosts |
| Zeek | Turns network traffic into connection, DNS, HTTP and other logs |
| SIEM | Searches all collected logs over long periods |
| Sigma | Shares hunting and detection logic in a vendor neutral format |
A worked example
The hypothesis is credential dumping on file servers. The hunter searches Sysmon event 10 for processes opening lsass.exe and finds an unsigned executable in a temporary folder on one server. A Get-FileHash of the file returns a hash unknown to the TIP, and Get-CimInstance Win32_Process shows it was started by a scheduled task.
A YARA sweep with the rule above matches the file and one copy on a second server. The hunter hands both hosts to incident response and adds a detection for unusual LSASS access.
How the exam asks about this
- "Which tool identifies malware families using rules made of strings and conditions?" → YARA.
- "Which Sysmon event ID records process creation with the command line?" → Event ID 1.
- "Which PowerShell event ID records the content of executed script blocks?" → Event ID 4104.
What to remember for the exam
- PowerShell cmdlets such as Get-WinEvent, Get-CimInstance, Get-NetTCPConnection and Get-ScheduledTask answer hunting questions on hosts.
- Script Block Logging writes event 4104 and exposes obfuscated PowerShell.
- A YARA rule has meta, strings and a condition; the condition decides the match.
- Sysmon 1 is process creation, 3 network connection, 10 process access, 22 DNS query.
- EDR, osquery, Velociraptor and Zeek scale a hunt across the fleet and network.
A YARA rule built on one generic string matches many clean files. Combine several specific strings with a file type check in the condition.
Common mistakes
Confusing Sysmon IDs with Security log IDs. Sysmon event 1 is process creation; in the Security log, process creation is 4688.
Hunting only with indicators. Hashes change easily. Hunt for behaviors such as LSASS access or encoded PowerShell as well.
Changing evidence while hunting. Hunting queries should read data. Deleting or quarantining files belongs to incident response once a finding is confirmed.
Check yourself
What are the main parts of a YARA rule?
The rule name, optional meta information, strings to search for, and a condition that decides when the rule matches.
Why is Sysmon useful for hunting?
It records detailed Windows telemetry such as process creation with command lines, network connections, process access and DNS queries.
How can a hunter run the same PowerShell query on many hosts?
With Invoke-Command and PowerShell remoting against a list of computer names.
Topic 5 of 5 in Proactive Threat Detection
Test yourself on Proactive Threat Detection
The full EC-Council 312-39 question bank has 700 questions, with timed practice exams and an explanation for every answer.
This study guide is independent and is not affiliated with, endorsed by, or sponsored by EC-Council. Vendor names, certification names and exam codes are trademarks of their respective owners and are used for identification only.