Implement advanced correlation techniques to identify sophisticated threats that use service installation as part of a larger attack chain.
- Create a comprehensive timeline of system events around service installation:
$TargetTime = (Get-Date "2026-03-18 14:30:00")
$TimeWindow = 1800 # 30 minutes
$StartTime = $TargetTime.AddSeconds(-$TimeWindow)
$EndTime = $TargetTime.AddSeconds($TimeWindow)
# Collect multiple event types
$Events = @()
$Events += Get-WinEvent -FilterHashtable @{LogName='System'; Id=7045; StartTime=$StartTime; EndTime=$EndTime} | Select-Object TimeCreated, Id, LevelDisplayName, @{Name='EventType';Expression={'ServiceInstall'}}, @{Name='Details';Expression={"Service: $($_.Properties[0].Value), Path: $($_.Properties[1].Value)"}}
$Events += Get-WinEvent -FilterHashtable @{LogName='Security'; Id=4688; StartTime=$StartTime; EndTime=$EndTime} | Select-Object TimeCreated, Id, LevelDisplayName, @{Name='EventType';Expression={'ProcessCreation'}}, @{Name='Details';Expression={"Process: $($_.Properties[5].Value)"}}
$Events += Get-WinEvent -FilterHashtable @{LogName='Security'; Id=4624; StartTime=$StartTime; EndTime=$EndTime} | Select-Object TimeCreated, Id, LevelDisplayName, @{Name='EventType';Expression={'Logon'}}, @{Name='Details';Expression={"User: $($_.Properties[5].Value), Type: $($_.Properties[8].Value)"}}
$Events | Sort-Object TimeCreated | Format-Table -AutoSize
- Analyze service installation patterns for anomalies:
# Check for multiple services installed in short timeframe
$RecentServices = Get-WinEvent -FilterHashtable @{LogName='System'; Id=7045; StartTime=(Get-Date).AddHours(-24)}
$ServicesByHour = $RecentServices | Group-Object {$_.TimeCreated.ToString("yyyy-MM-dd HH")} | Where-Object {$_.Count -gt 3}
if ($ServicesByHour) {
Write-Warning "Unusual service installation activity detected:"
$ServicesByHour | ForEach-Object {
Write-Host "$($_.Name): $($_.Count) services installed"
}
}
- Check for services with suspicious characteristics:
$SuspiciousServices = Get-WinEvent -FilterHashtable @{LogName='System'; Id=7045; StartTime=(Get-Date).AddDays(-7)} | Where-Object {
$ServiceName = $_.Properties[0].Value
$ImagePath = $_.Properties[1].Value
# Flag services with suspicious names or paths
($ServiceName -match "[0-9]{8,}" -or
$ServiceName -match "^[a-f0-9]{32}$" -or
$ImagePath -like "*\temp\*" -or
$ImagePath -like "*\appdata\*" -or
$ImageName -like "*.tmp" -or
$ServiceName.Length -lt 3)
}
if ($SuspiciousServices) {
Write-Warning "Potentially malicious services detected:"
$SuspiciousServices | ForEach-Object {
Write-Host "Time: $($_.TimeCreated), Service: $($_.Properties[0].Value), Path: $($_.Properties[1].Value)"
}
}
- Export findings for further analysis:
$Report = @()
Get-WinEvent -FilterHashtable @{LogName='System'; Id=7045; StartTime=(Get-Date).AddDays(-30)} | ForEach-Object {
$Report += [PSCustomObject]@{
Timestamp = $_.TimeCreated
ServiceName = $_.Properties[0].Value
ImagePath = $_.Properties[1].Value
ServiceType = $_.Properties[2].Value
StartType = $_.Properties[3].Value
UserSID = $_.UserId
Suspicious = ($_.Properties[1].Value -like "*\temp\*" -or $_.Properties[1].Value -like "*\appdata\*")
}
}
$Report | Export-Csv -Path "C:\Temp\ServiceInstallationAnalysis.csv" -NoTypeInformation
Warning: Always correlate Event ID 7045 with network connections, file system changes, and registry modifications to build a complete picture of potential threats.