# =============================================================================
# Detection spec — template
#
# Companion to:
#   https://entropywatch.dev/articles/write-the-detection-spec-before-the-rule/
# License: CC BY 4.0 — ENTROPY//WATCH
# Template version: 2026-10-24
# Structure inspired by Palantir's Alerting and Detection Strategy framework:
#   https://github.com/palantir/alerting-detection-strategy-framework
#
# HOW TO USE
#   One file per detection, next to the rule in version control, e.g.
#   detections/windows/new-service-on-dc/spec.yml + rule.xml / rule.kql.
#   Write `goal`, `data` and `blind_spots` BEFORE the rule. Fill `validation`
#   before the rule goes to production. Append to `tuning_log`, never rewrite it.
#
# BEFORE YOU USE
#   - ADAPT `status` values and `priority` to your own triage model.
#   - ADAPT `data.sources` to the ids in your log source inventory.
#   - The example below is complete but generic: replace every value.
# =============================================================================

id: DET-0001                          # stable, never reused
title: "New service installed on a domain controller"
status: draft                         # draft | testing | production | retired
owner: ""                             # person or team who answers for this rule
created: YYYY-MM-DD
updated: YYYY-MM-DD

# What behavior should this detect, in one or two sentences, and why it matters.
goal: >
  Detect a new Windows service on a domain controller. Services run as SYSTEM, start
  automatically and are a common way to run code persistently or move laterally to a DC.
  New services on DCs are rare and planned.

# ATT&CK only where the rule actually detects the technique.
attack:
  - technique: T1543.003
    name: "Create or Modify System Process: Windows Service"

# Which data the rule reads, and what must be true for that data to exist.
data:
  sources:
    - inventory_id: "Windows domain controllers — Security log"   # from log-source-inventory.yml
      events: ["4697"]
      requires: "Advanced Audit Policy: System > Security System Extension: Success"
    - inventory_id: "Windows domain controllers — System log"
      events: ["7045"]
      requires: "Nothing: written by the Service Control Manager by default"
  fields:
    - "ServiceName, ServiceFileName (4697) / Service Name, Image Path (7045)"
    - "SubjectUserName (4697): who installed it"

# How the rule works, in plain language. The rule itself lives next to this file.
strategy: >
  Alert on any 4697 or 7045 on a domain controller. Join both on host and service name
  within 5 min to avoid duplicate alerts. Exclude services on the allowlist below.

# What this rule will NOT see, and what it assumes. The most important section.
blind_spots:
  - "Services created by changing an existing service's ImagePath: no 4697/7045. Covered by a separate rule on registry changes."
  - "Security System Extension auditing disabled on a DC: only 7045 remains."
  - "Attacker clears the System log after installing: 104 is written; covered by the log-clear rule."
assumptions:
  - "All DCs are in the DC host group in the SIEM."
  - "Planned service installations on DCs go through change management."

# Expected legitimate matches and how they are handled.
false_positives:
  - source: "Monthly patching: Windows Defender and agent updates reinstall services"
    handling: "Allowlist by ServiceName + ImagePath, reviewed every quarter"
  - source: "Backup or monitoring agent upgrades"
    handling: "Allowlist by ImagePath under the vendor's program folder"
allowlist:
  - service_name: ""                  # exact name
    image_path: ""                    # full path, not a wildcard on the file name
    reason: ""
    added: YYYY-MM-DD
    review_by: YYYY-MM-DD

# How to prove the rule works. Run before production and after every change.
validation:
  environment: "Test DC or lab domain controller"
  steps:
    - "sc.exe create ew-det-test binPath= C:\\Windows\\System32\\notepad.exe"
    - "sc.exe delete ew-det-test"
  expected:
    - "4697 and 7045 for ew-det-test on the test DC"
    - "One alert in the SIEM within the expected latency"
  last_run: YYYY-MM-DD
  result: ""                          # pass | fail + note

priority: high                        # what triage does with it, not how scary it sounds
response: >
  Identify who installed the service (SubjectUserName on 4697) and whether a change
  ticket exists. If not: isolate per the DC compromise playbook; do not delete the service
  before collecting the binary at ImagePath.

# Append-only record of changes and why.
tuning_log:
  - date: YYYY-MM-DD
    change: "Initial version"
    why: ""
    alerts_per_week_before: null
    alerts_per_week_after: null

references:
  - https://attack.mitre.org/techniques/T1543/003/
