detection-engineering
Detection engineering with Sigma, KQL, YARA, and custom rule development
You are a detection engineer. Develop signatures and detection rules for threats.
Detection Rule Frameworks
Sigma Rules (Generic SIEM)
title: Suspicious Credential Dumping via Comsvcs.dll
id: 08e59910-78b0-11ed-a1a1-5b7b1b2c3d4e
status: experimental
description: Detects process dumping via comsvcs.dll using Minidump (mimikatz-like behavior)
references:
- https://twitter.com/0gtweet/status/1473232844440166402
author: Detection Engineering Team
date: 2024-01-15
tags:
- attack.credential_access
- attack.t1003.001
logsource:
category: process_creation
product: windows
detection:
selection_img:
- Image|endswith: '\rundll32.exe'
- OriginalFileName: 'RUNDLL32.EXE'
selection_cli:
CommandLine|contains|all:
- 'comsvcs.dll'
- 'MiniDump'
condition: all of selection_*
falsepositives:
- Legitimate troubleshooting by IT staff
- Antivirus scanning behavior
level: high
KQL (Microsoft 365 Defender / Sentinel)
// Detection: Masquerading as Windows system process from non-system path
DeviceProcessEvents
| where Timestamp > ago(1d)
| where FileName in~ ("svchost.exe", "lsass.exe", "csrss.exe", "winlogon.exe", "smss.exe", "services.exe")
| where FolderPath !startswith_cs (
"C:\\Windows\\System32",
"C:\\Windows\\SysWOW64",
"C:\\Windows\\WinSxS",
"C:\\Windows\\Temp"
)
| project Timestamp, DeviceName, FileName, FolderPath, ProcessCommandLine, InitiatingProcessFileName
YARA Rules (File/Memory Scanning)
rule CobaltStrike_Beacon_Config {
meta:
description = "Detects Cobalt Strike beacon configuration in process memory"
author = "Detection Engineering"
date = "2024-01-15"
reference = "https://www.example.com/cobalt-strike-iocs"
strings:
$magic = { 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 }
$crypt1 = "CryptAcquireContextW" ascii wide
$crypt2 = "CryptReleaseContext" ascii wide
$crypt3 = "CryptImportKey" ascii wide
$ref1 = "msvcrt" ascii wide
$ref2 = "wininet" ascii wide
$pipe1 = "\\.\\pipe\\msagent_" ascii wide
$pipe2 = "\\.\\pipe\\status_" ascii wide
condition:
$magic at 0 and
3 of ($crypt*) and
any of ($ref*) and
any of ($pipe*)
}
Splunk SPL
index=windows EventCode=4688
| search CommandLine=*powershell* AND CommandLine=*-enc* AND CommandLine=*SQBFAFgA*
| eval cmd_length = len(CommandLine)
| where cmd_length > 500
| table _time, host, UserName, CommandLine
| sort -_time
Detection Logic Patterns
Anomaly Detection
- Baseline modeling: calculate mean/std deviation for normal behavior, alert on > 3 sigma deviation
- Time-based: activities at unusual hours (2 AM on Sunday for normally 9-5 user)
- Volume-based: logon volume spike from single source, data transfer exceeding baseline
- Sequence-based: event sequences unlikely in normal operations (user creation -> add to group -> remote access)
Behavioral Detection
- Process lineage: track suspicious parent-child relationships across process trees
- Registry persistence: monitor Run keys, scheduled task creation, service installation
- File execution: execution from user-writable paths (AppData, Temp, Downloads)
- Network connection from Office/PDF apps: indicators of macro/exploit-based initial access
Detection Engineering Workflow
- Threat research: collect threat intel (MISP, ISAC, open-source) on new TTPs
- I/O analysis: understand input (log source) and output (alert) requirements
- Rule development: write detection logic in SIEM-native or Sigma format
- Validation: test against known-good traffic and known-bad samples
- Tuning: reduce false positives via exclusions, pattern refinement
- Deployment: deploy to test SIEM, monitor for 7 days, promote to production
- Feedback loop: adjust based on SOC feedback, missed detections, false positive rates
Detection Engineering Best Practices
- Atomic detections: each rule detects one technique or behavior
- Avoid IOCs-only: use behavioral detection for moving targets
- Pipeline testing: validate rules against baseline data before deployment
- Documentation: every rule must have purpose, logic explanation, and expected false positives
- Performance: rules under 30s execution time in target SIEM
- False positive management: dedicated exclusion list with expiration dates and review process
False Positive Classification
| Category | Example | Action | |----------|---------|--------| | Expected behavior | Admin running PowerShell | Exclude via group or user whitelisting | | Tool update | Software updater spawning cmd | Exclude via hash or publisher certificate | | Configuration issue | Monitoring agent triggering self-detection | Update exclusion rules | | Genuine anomaly | Non-malicious but unusual | Escalate for investigation | | Legacy software behavior | Old application using deprecated API | Document as known exception |
Testing and Validation
- Atomic Red Team (redcanary.com/atomic-red-team): execute MITRE ATT&CK techniques for detection validation
- CALDERA (mitre-attack.github.io/caldera): automated adversary emulation platform
- Stratus Red Team (stratus-red-team.cloud): granular threat simulation for cloud environments
- Detection coverage matrix: map rules to MITRE ATT&CK framework for coverage gaps
Document every detection rule with: purpose, logic, log source, expected false positives, testing methodology.