Tools · Threat Hunting

Active Directory Security Event Forensics: The Incident Response Query Playbook

A comprehensive tactical playbook for querying Windows Security event logs at lightning speed using native XPath XML filters, decoding authentication failure codes, and detecting Active Directory attacks.

By K Shankar R Karanth Tools Hands-On Homelab-tested — Windows Server 2025, DFL 2025
Interactive Companion Tool: This playbook directly documents the detection signatures behind the Active Directory & Windows Security Event Query Builder. Launch the tool to dynamically construct optimized XPath queries, generate copy-ready PowerShell scripts, and decode authentication error hex codes in real time.
FilterXml Engine

Querying the Windows Event Log using pre-compiled XPath strings pushes evaluation directly to the kernel event provider, cutting query time by 98%.

Lockout Origin (4740)

Pinpointing the exact CallerComputerName stops helpdesks from blindly unlocking accounts while rogue services continue hammering bad passwords.

Attack Signatures

DCSync (4662) and Kerberoasting (4769 RC4) leave indelible event fingerprints that allow rapid containment before domain compromise.

The Event Log Bottleneck

During an active security incident or an executive account lockout, speed is everything. Yet, many systems administrators and Tier 2 analysts still approach Windows Event Logs with legacy command patterns:

# The Anti-Pattern: Slow, memory-heavy, and unindexed
Get-EventLog -LogName Security | Where-Object { $_.EventID -eq 4740 }

On a busy enterprise Domain Controller processing 5,000 authentications per minute, the Security event log frequently spans 20 GB to 40 GB across multiple wrapped EVTX archives. Piping every raw event object across the PowerShell pipeline forces the engine to deserialize millions of XML documents into memory. The query takes 15 minutes, pegs the DC’s CPU at 100%, and often crashes with an out-of-memory exception.

Think of querying Windows Event Logs like querying an enterprise SQL database: piping to Where-Object is the equivalent of running a table scan on an unindexed billion-row database. In contrast, XPath filtering via Get-WinEvent -FilterXml passes the query directly to the native Windows Event Log subsystem (wevtutil/kernel), evaluating conditions before bytes ever cross into user space.

High-Speed XPath XML Queries

XPath XML queries filter events at the provider level using the <QueryList> specification. A properly constructed XPath query can scan 500,000 security events on a Domain Controller in under two seconds.

The core anatomy of an optimized event query consists of two primary clauses: the System header (EventID, Channel, and TimeCreated) and the structured EventData payload:

<QueryList>
  <Query Id="0" Path="Security">
    <Select Path="Security">
      *[System[(EventID=4740) and TimeCreated[timediff(@SystemTime) <= 86400000]]]
    </Select>
  </Query>
</QueryList>

The timediff(@SystemTime) <= 86400000 directive instructs the kernel to ignore any event older than 24 hours (86,400,000 milliseconds), drastically reducing disk I/O.

Hunting Account Lockouts (4740)

When an Active Directory user account locks out repeatedly, simply running Unlock-ADAccount is a temporary band-aid. Within seconds, whatever background process, stale network mapping, mobile ActiveSync profile, or scheduled task holds the bad password will trigger another failure and lock the account again.

Event ID 4740 is generated on the Domain Controller holding the PDC Emulator role (or the DC that processed the lockout threshold). The key to permanent resolution lies in extracting the CallerComputerName attribute:

Property Index Property Name Operational Value
$_.Properties[0] TargetUserName The sAMAccountName of the locked user account.
$_.Properties[1] CallerComputerName The NetBIOS or host name of the client machine originating the bad password.
$_.MachineName DomainController The DC that logged the event and processed the lockout.

Here is the production PowerShell script generated by the Forensic Query Builder to isolate the caller immediately:

# Fast Account Lockout Caller Discovery via FilterXml
$FilterXml = @'
<QueryList>
  <Query Id="0" Path="Security">
    <Select Path="Security">
      *[System[(EventID=4740) and TimeCreated[timediff(@SystemTime) <= 86400000]]]
    </Select>
  </Query>
</QueryList>
'@

$Events = Get-WinEvent -FilterXml $FilterXml -ErrorAction SilentlyContinue

$Events | Select-Object TimeCreated,
  @{N='LockedUser';E={$_.Properties[0].Value}},
  @{N='CallerDevice';E={$_.Properties[1].Value}},
  @{N='DomainController';E={$_.MachineName}} | Format-Table -AutoSize

The Event 4625 SubStatus Decoder

Event ID 4625 is logged whenever an authentication attempt fails. However, looking only at the event message “An account failed to log on” does not explain why. The diagnostic truth is embedded inside the SubStatus hex code.

Our forensic query builder incorporates the complete NTSTATUS failure decoder table:

SubStatus Code NTSTATUS Constant Root Cause Diagnostic
0xC000006A STATUS_WRONG_PASSWORD The username is valid, but the password provided was incorrect. Indicates typo, stale cached credential, or brute-force attack.
0xC0000064 STATUS_NO_SUCH_USER The username does not exist in the domain. High volumes indicate username enumeration or password spraying against non-existent accounts.
0xC0000234 STATUS_ACCOUNT_LOCKED_OUT Authentication was rejected because the account was already in a locked state prior to the logon attempt.
0xC0000072 STATUS_ACCOUNT_DISABLED The user account exists in Active Directory but has been administrative disabled.
0xC000006F STATUS_INVALID_LOGON_HOURS The logon attempt occurred outside the user’s permitted logon hours restriction.
0xC0000193 STATUS_ACCOUNT_EXPIRED The Active Directory account expiration date attribute has passed.

DCSync (4662) & Kerberoasting (4769)

Beyond operational lockouts, high-speed event queries are critical for detecting advanced threat actors moving laterally inside an Active Directory domain:

DCSync Detection (4662)

Mimikatz DCSync impersonates a Domain Controller to pull password hashes using Directory Replication Service (DRSUAPI). An alert must fire whenever Event 4662 contains the replication GUID 1131f6ad-9c07-11d1-f79f-00c04fc2dcd2 (Get-Changes-All) and the caller account does NOT end with $.

Kerberoasting (4769)

Attackers request service tickets for SPN service accounts to crack passwords offline. Legitimate environments request AES encryption. An alert should flag Event 4769 where TicketEncryptionType is 0x17 (legacy RC4-HMAC) and the service name is not krbtgt.

Log Tampering (1102)

Fires immediately when the Windows Security audit log is manually wiped using wevtutil or Clear-EventLog. This is an emergency indicator of adversary defense evasion.

Importing XML into Event Viewer

If you prefer working inside the standard Windows Event Viewer GUI (eventvwr.msc) rather than PowerShell, the Forensic Query Builder allows you to create permanent custom views in 30 seconds:

  1. Open Event Viewer (eventvwr.msc) on any Domain Controller or management workstation.
  2. In the left tree view, right-click Custom Views and click Create Custom View…
  3. In the Create Custom View dialog, click the XML tab at the top right.
  4. Check the box Edit query manually and click Yes when prompted.
  5. Copy the XML query from the Forensic Query Builder and paste it into the editor.
  6. Click OK and name your view (e.g. AD Lockout Origin Hunter).

Final Thoughts & SOC Integration

High-performance event log forensics bridges the gap between raw telemetry and actionable incident containment. By leveraging pre-compiled XPath filters and automated PowerShell objects, you eliminate the overhead of manual log reviews and pinpoint root causes in seconds.

To generate custom queries for your environment, launch the live Active Directory & Windows Security Event Query Builder. To audit and harden the network perimeter protecting these Domain Controllers, explore our companion Active Directory & Infrastructure Firewall Matrix Guide or visit the central Tools Directory.


Discover more from Karanth.ovh

Subscribe to get the latest posts sent to your email.