ENGINEERING NOTES / DETECTION ENGINEERING

Stateful Detection.

How detection logic uses accumulated event context over time to identify behaviour that cannot be understood from a single event in isolation.

DETECTION ENGINEERING EVENT CORRELATION PYTHON MINISOC

Detection requires context

A stateless detection rule evaluates an event independently. It may identify a single failed login, an unusual process, or a connection to a particular destination.

Stateful detection keeps relevant information from earlier events and evaluates new activity against that accumulated context. This makes it possible to identify patterns that emerge over time—such as repeated authentication failures from one source within a defined period.

Core idea
One event provides an observation. A sequence of events can provide behavioural evidence.

Two different ways to evaluate events

STATELESS

Single-event evaluation

Evaluates an event using only the fields available in that event. No history is required.

STATEFUL

Context over time

Maintains relevant event history or counters and uses that context when evaluating incoming events.

TRADE-OFF

More context, more responsibility

State introduces lifecycle, expiry, memory management, event ordering and reset considerations.

Repeated authentication failures

A single failed authentication attempt may be routine. A sequence of failures associated with the same source within a short time window may warrant investigation.

A basic detection model tracks the relevant source, the timestamps of failed attempts, and a configurable threshold. When the number of failures inside the active window reaches the threshold, the detector emits an alert.

Illustrative rule
Trigger an alert when a source records at least N failed authentication events within T minutes. The threshold and time window should be configurable and tuned to the environment.

Conceptual event sequence

Event 1 ── Failed login ──┐
Event 2 ── Failed login ──┤
Event 3 ── Failed login ──┤── Context updated
Event 4 ── Failed login ──┤
                          └── Threshold reached
                               │
                               ▼
                         Alert generated

Maintain, evaluate, expire

01 / INGEST

Receive event

Accept a normalized authentication event and determine whether it is relevant to the rule.

02 / UPDATE

Track context

Record the event in the state associated with its correlation key, such as a source address.

03 / WINDOW

Expire old data

Remove observations that fall outside the configured time window.

04 / EVALUATE

Check threshold

Evaluate the remaining observations against the rule's threshold.

05 / ALERT

Produce evidence

When the condition is met, create an alert containing the relevant event context.

06 / CLEANUP

Manage state

Remove inactive keys and prevent tracking data from growing indefinitely.

What should the detector remember?

State should contain only the information required to evaluate the rule. For a simple failed-login detector, the state may be keyed by source address and contain timestamps for recent failures.

state = {
    "source_key": [
        timestamp_1,
        timestamp_2,
        timestamp_3
    ]
}

This is a conceptual representation, not a prescribed implementation. Depending on the rule, state may instead contain counters, sets of affected accounts, last-seen timestamps, or other compact context.

Design considerations

  • Correlation key: decide which entity groups events together, such as source IP, username, host, or a combination.
  • Time source: use event timestamps consistently and consider clock skew or out-of-order records.
  • Expiry: define when observations are no longer relevant.
  • Memory: bound state size and clean up inactive keys.
  • Alert behaviour: decide whether a threshold produces one alert, repeated alerts, or an alert with updated evidence.

Moving from concept to code

In MiniSOC, stateful detection is separated from parsing. Parsers convert source records into normalized events; detection components consume those events and maintain only the context required by their rules.

A dedicated state-management component can help keep detection rules focused on evaluating conditions rather than duplicating storage and expiry logic.

Implementation discipline
Keep state handling explicit, testable and bounded. Prefer a small, well-defined interface over a detector that mixes parsing, event storage, threshold logic and reporting.

Questions to answer during implementation

  • What is the correlation key and why?
  • Are events processed in timestamp order or arrival order?
  • How are events exactly on the time-window boundary handled?
  • What happens when state is empty or a key is first observed?
  • How is expired state removed?
  • How do tests verify threshold and expiry behaviour?

Verify the behaviour, not just the alert

A stateful detector should be tested across event sequences. Testing only a single event is insufficient to validate accumulation, expiry and threshold behaviour.

  • Events below the threshold do not trigger an alert.
  • Reaching the threshold within the active window triggers the expected alert.
  • Events outside the configured window are excluded.
  • Events associated with different correlation keys remain isolated.
  • State is cleaned up when it becomes inactive.
  • Repeated evaluation follows the intended alert policy.
  • Boundary timestamps and out-of-order events behave consistently.
Record the actual test cases and results in the project documentation after implementing them. Do not claim behaviour that has not been verified.

State introduces complexity

Stateful rules can provide stronger behavioural context, but they also introduce operational and design challenges.

  • Thresholds may produce false positives or fail to detect activity that stays below them.
  • Event ordering and timestamp quality can affect evaluation.
  • State must be bounded and cleaned up to avoid resource exhaustion.
  • Restarting a process may discard in-memory state unless persistence is implemented.
  • Correlation keys may be incomplete or shared by multiple users or systems.

A detection rule should be evaluated in the context of its environment. A threshold is a starting point for investigation—not proof of malicious activity.

Further reading