Writing SIEM Detection Rules and Alerts
Build detection rules that balance sensitivity (catching real threats) against specificity (minimizing alert fatigue) for common attack techniques.
Writing SIEM Detection Rules and Alerts is a free Cloud & IT Cert Prep lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Cloud & IT Cert Prep learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Detection Rules: What and Why
Detection rules are the logic encoded in a SIEM that defines what conditions constitute a security alert. Without good detection rules, a SIEM is just an expensive log storage system. Well-crafted rules identify specific attacker behaviors — credential stuffing, lateral movement, data exfiltration — while avoiding triggering on normal operations. Detection engineering is the discipline of writing, testing, and maintaining these rules continuously.
Anatomy of a Detection Rule
Every detection rule has key components. A data source specifies which logs are queried. A filter condition specifies what events match. A threshold or pattern defines how many events or what sequence triggers the alert. Metadata includes severity, MITRE ATT&CK mapping, description, and recommended response. Well-documented rules help analysts quickly understand what an alert means and how to respond when it fires.
# Detection rule anatomy example:
# Name: 'Suspicious PowerShell Encoded Command'
# Severity: HIGH
# ATT&CK: T1059.001 - Command and Scripting Interpreter: PowerShell
# Source: Windows Security Event Logs (EventID 4688)
# Condition: CommandLine contains '-EncodedCommand' OR '-enc '
# AND ParentImage NOT IN ('sccm.exe','wsus.exe')
# Threshold: Any single occurrence
# Response: Isolate host, collect memory dump, notify SOCSensitivity vs Specificity Trade-off
Every detection rule balances sensitivity (catching all true positives) against specificity (avoiding false positives). A highly sensitive rule catches every attack variant but generates enormous alert noise. A highly specific rule fires rarely but may miss novel attack variations. Good detection engineering starts with high specificity to build analyst trust, then progressively broadens scope as tuning reduces false positives and confidence in the rule grows.
Threshold-Based Detection
Threshold-based rules trigger when an event count exceeds a limit within a time window. They are ideal for detecting volume-based attacks like brute force, port scanning, and DDoS. Key parameters: count threshold (how many events), time window (within how many minutes), and grouping field (per source IP, per user, per host). Incorrect thresholds cause alert noise or missed detections — tune carefully using historical baseline data.
# Threshold rule: RDP brute force detection
# Source: Windows Event ID 4625 (failed logon)
# Filter: LogonType = 10 (RemoteInteractive/RDP)
# Threshold: count >= 10
# Window: 5 minutes
# Group by: TargetComputerName, IpAddress
# Alert: 'RDP Brute Force Attempt'
# Include: src_ip, target_host, account_list, failure_countSequence-Based Detection
Sequence-based rules look for a specific ordered chain of events, which is ideal for detecting multi-stage attack patterns. For example: phishing email received THEN malicious attachment opened THEN PowerShell spawned by Office application. Each event alone may be benign, but the sequence indicates compromise. Most modern SIEMs (Splunk, Sentinel, Elastic) support sequence matching with time correlation across event chains.
# Sequence rule: Office macro spawning shell (conceptual)
# Step 1: process_create where ParentImage ENDS_WITH 'WINWORD.EXE'
# AND Image IN ('cmd.exe','powershell.exe','wscript.exe')
# THEN within 30 seconds:
# Step 2: network_connect from same PID
# AND destination NOT IN allowlist
# --> Alert: 'Macro-spawned Shell with Outbound Connection'
# --> Severity: CRITICALSigma Rules: Portable Detection Logic
Sigma is an open, vendor-agnostic format for writing detection rules that can be converted to SIEM-specific query languages (SPL for Splunk, KQL for Sentinel, Lucene for Elastic). The security community shares thousands of Sigma rules on GitHub, covering common ATT&CK techniques. Using Sigma allows organizations to adopt community detections without manually rewriting them for their specific SIEM platform, accelerating detection coverage significantly.
# Sigma rule example (YAML format):
# title: Suspicious PowerShell Encoded Command
# status: stable
# logsource:
# category: process_creation
# product: windows
# detection:
# selection:
# Image|endswith: '\\powershell.exe'
# CommandLine|contains:
# - '-EncodedCommand'
# - '-enc '
# condition: selection
# falsepositives:
# - SCCM software deployment
# level: highTesting Detection Rules
Detection rules must be tested before production deployment. Best practices include: unit testing with synthetic events that represent both true positive and false positive scenarios, replay testing using recorded benign traffic to measure false positive rate, and red team exercises where the rule is expected to fire on controlled attack simulations. Tools like Atomic Red Team provide small test scripts that simulate specific ATT&CK techniques safely.
# Atomic Red Team test: simulate PowerShell encoded command
# T1059.001 - Atomic Test #1: PowerShell Encoded Command
# Command simulated:
# powershell.exe -EncodedCommand JABj...(base64)
# (decodes to: $cmd = 'whoami'; Invoke-Expression $cmd)
# After running: verify SIEM fired alert within 60 seconds
# If not: check log ingestion, parser, rule condition
# Then clean up: no persistence, process exits cleanlyTuning Rules to Reduce False Positives
After a rule is deployed, continuous tuning is required. Common tuning techniques: exclusion lists for known-good processes or accounts that legitimately trigger the rule, allow-listing specific source IPs (scanners, monitoring tools), adjusting thresholds based on observed baseline rates, and adding context conditions (only alert if the host is also externally reachable). Document every exclusion with justification so future analysts understand why they exist.
Alert Severity Tiers
Detection rules should carry severity ratings that guide analyst prioritization. Common tiers: Critical — active exploitation, ransomware, domain controller compromise. High — lateral movement, credential dumping, C2 communication. Medium — suspicious reconnaissance, policy violations. Low/Informational — unusual but not immediately dangerous events worth tracking. Severity should align with business impact, not just technical severity.
Detection Rule Lifecycle Management
Detection rules have a lifecycle that must be actively managed. Rules become stale when the environment changes (new software deployed, IP ranges change) and generate false positives. Rules miss new attack techniques as adversaries evolve. Best practice: maintain rules in version control (git), review and update quarterly, map every rule to at least one ATT&CK technique, and measure rule effectiveness (fires-per-week, true positive rate) to retire or improve underperforming rules.
Building a Detection Coverage Map
A detection coverage map overlays existing detection rules against the MITRE ATT&CK matrix to visualize coverage gaps. Each technique covered by at least one rule is marked green; uncovered techniques are red. This visual reveals which attack phases (e.g., Persistence, Exfiltration) lack detection coverage, helping teams prioritize new rule development. Regular coverage reviews ensure the detection program keeps pace with evolving adversary techniques.
Quick Check
Test your understanding of CompTIA Security+ (SY0-701) concepts from this lesson.
Lesson Recap
In this lesson you learned: detection rules encode specific attacker behaviors as alert conditions in the SIEM, threshold and sequence-based rules address different attack pattern types, and Sigma provides a portable format for sharing detections across the security community. Next up we explore UEBA and behavioral analytics for detecting insider threats and compromised accounts.
Frequently asked questions
Is the “Writing SIEM Detection Rules and Alerts” lesson free?
Yes — the full text of “Writing SIEM Detection Rules and Alerts” is free to read here on the web, and the Cloud & IT Cert Prep course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Cloud & IT Cert Prep course, upgrade to CoddyKit PRO.
What will I learn in “Writing SIEM Detection Rules and Alerts”?
Build detection rules that balance sensitivity (catching real threats) against specificity (minimizing alert fatigue) for common attack techniques. You practise Cloud & IT Cert Prep with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Cloud & IT Cert Prep?
No prior experience is required. Cloud & IT Cert Prep on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Writing SIEM Detection Rules and Alerts” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Cloud & IT Cert Prep lesson?
Yes. Every Cloud & IT Cert Prep lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.