Skip to content
Caleb SmithCyberSecurityWithSmitti
Browse PowerShell example contentPowerShell example

Failed-logon burst review

Group local 4625 events into fixed UTC buckets by account and source address.

How to use it

.\Find-SocFailedLogonBursts.ps1 -Hours 24 -WindowMinutes 15 -Threshold 5

Platform and permissions

Windows Security log read access; relevant audit policy and retained events.

What changes / what is read

Reads a capped event set; does not block accounts or sources.

Scope and interpretation

Fixed buckets are not sliding detection windows. Source addresses can be absent or shared. Reaching the event cap means the full time window may not be covered.

Practice task

Using synthetic event fixtures, compare two failures on either side of a bucket boundary.

Reference

Microsoft documentation. Review the documentation for your installed version and use only an authorised learning or investigation environment.

This is personal educational material. Review the complete code and your signing policy before use. PowerShell code is displayed and downloaded as text; WordPress does not run it. Collection caps limit rows/files, not every provider’s wall-clock execution time.

PowerShell · PowerShell 5.1+ syntax target; Windows collectors require the stated Windows modules. Full Windows acceptance testing remains required.Download .ps1
#requires -Version 5.1
[CmdletBinding()]
param([ValidateRange(1,168)][int]$Hours=24,[ValidateRange(1,60)][int]$WindowMinutes=15,[ValidateRange(2,10000)][int]$Threshold=5,[ValidateRange(100,20000)][int]$MaxEvents=5000)
if($env:OS -ne 'Windows_NT'){throw 'Windows Security log is required.'}
$filter=@{LogName='Security';Id=4625;StartTime=(Get-Date).AddHours(-$Hours)}
try { $events=@(Get-WinEvent -FilterHashtable $filter -MaxEvents $MaxEvents -ErrorAction Stop) }
catch {
    if($_.FullyQualifiedErrorId -like 'NoMatchingEventsFound*'){$events=@()}
    else {throw ('Security log query failed; do not interpret this as zero failures: '+$_.Exception.Message)}
}
$rows=@(foreach($event in $events){
    $xml=[xml]$event.ToXml();$data=@{}
    foreach($node in $xml.Event.EventData.Data){$data[$node.GetAttribute('Name')]=$node.InnerText}
    $utc=$event.TimeCreated.ToUniversalTime();$ticks=[timespan]::FromMinutes($WindowMinutes).Ticks
    $start=[datetime]::new(($utc.Ticks-($utc.Ticks % $ticks)),[DateTimeKind]::Utc)
    [pscustomobject]@{BucketUtc=$start;SourceIP=$data['IpAddress'];User=$data['TargetUserName'];Domain=$data['TargetDomainName'];LogonType=$data['LogonType'];Status=$data['Status'];RecordId=$event.RecordId}
})
$bursts=@($rows | Group-Object BucketUtc,SourceIP,Domain,User | Where-Object Count -ge $Threshold | ForEach-Object {
    $first=$_.Group[0]
    [pscustomobject]@{BucketUtc=$first.BucketUtc;WindowMinutes=$WindowMinutes;SourceIP=$first.SourceIP;Domain=$first.Domain;User=$first.User;Count=$_.Count;LogonTypes=($_.Group.LogonType | Sort-Object -Unique)-join ',';RecordIds=@($_.Group.RecordId)}
} | Sort-Object Count -Descending)
[pscustomobject]@{CollectedUtc=[datetime]::UtcNow;EventsRead=$events.Count;ReachedEventCap=($events.Count -ge $MaxEvents);Bursts=$bursts;Note='Fixed UTC buckets, not sliding windows. Adjacent-bucket bursts can be missed. Results depend on audit policy, retention, local collection and rights; no attribution or malicious verdict is established.'}

Example code is displayed as text. Review it and test in your own lab before use.