Skip to content
SlateGitOrgPublic

About

An event-time sequence detector for EDR telemetry that returns the same alerts in any arrival order within a stated lateness bound and reports every event later than it.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

Repository files navigation

seqwatch

CI

seqwatch

seqwatch is an event-time sequence detector for endpoint (EDR) telemetry. It evaluates multi-step detections such as "an Office app starts a script host, which then connects out" on the time the events happened, not the time they arrived. Within a stated lateness bound it returns exactly the alerts a sorted, offline run would return, whatever order the events arrive in, and every event that arrives later than the bound is reported on a late output with its host and how late it was. All data in this repository is synthetic.

The problem

Endpoint telemetry reaches a SIEM out of order. The agent ships process events at once but batches network and file events, laptops buffer while offline, and a server's network pipeline can stall for hours. A detection that is evaluated in arrival order fires or not depending on network luck: the network step of a chain often arrives after a later file step, and an event that arrives after its correlation window has been evaluated simply vanishes.

seqwatch makes the alert set a function of event time alone. It buffers each host's events until that host's watermark passes them, feeds them to per-rule state machines in event-time order, and puts anything that still arrives too late on an explicit late output instead of dropping it.

How it works

flowchart LR
    A["Events in arrival order<br/>(event_time, arrival_time)"] --> W["Per-host watermark<br/>max event time - lateness<br/>idle flush"]
    W -->|"key at or after<br/>last released"| B["Per-host reorder buffer<br/>heap by (event_time, event_id)"]
    W -->|"key before<br/>last released"| L["Late output<br/>host, late by, reason"]
    B -->|"released when the<br/>watermark passes"| N["Per-rule state machines<br/>latest partial per join value<br/>expiry at maxspan"]
    N --> R["Alerts<br/>sorted, deterministic ids"]
    O["Batch oracle<br/>sort all events, same machines"] -. "same alerts<br/>when nothing is late" .-> R
Loading

Events. Each event has an event_id, an event_time (when it happened), an arrival_time (when the SIEM received it) and flat ECS-style fields such as process.name. event_id is unique and breaks ties between equal event times. Times are integer seconds. An event whose arrival_time is earlier than its event_time (clock skew) is rejected when it is read.

Rules are YAML. A rule has a by key that must include host.id, ordered steps with a where predicate (operators eq, in, startswith, endswith, contains, regex; strings compare case-insensitively), an optional per-step join tying a field of this step to a field of an earlier step, a maxspan, and an optional until predicate. Validation errors name the rule and the step. The packaged pack (src/seqwatch/data/edr-pack.yml) has six rules:

Rule Sequence maxspan
office-script-egress Office app starts, it starts a script host (parent pid = Office pid), the script host connects out (same pid); a script host exit cancels 10m
debug-privilege-lsass token privilege adjusted to SeDebugPrivilege, same process opens LSASS 5m
temp-service-install-start service installed from a temp path, service started 10m
archive-large-upload archive file created, large upload from the host 15m
scheduled-task-create-run scheduled task registered, the same task runs 30m
browser-credentials-egress non-browser process reads a browser credential store, same process connects out 10m

State machine (seqwatch.nfa). Events are processed in (event_time, event_id) order.

  • A partial match is the events matched so far. Per (rule, by-key, join value, stage) only the latest partial match is kept, so state does not grow with the number of candidates.
  • An event is tried against steps from last to first, so one event never advances a match twice.
  • An event that completes the last step emits one alert, is consumed, and discards every partial match that shares any of the alert's events.
  • A partial match expires when its first event is more than maxspan older than the current event; a gap of exactly maxspan still matches.
  • An event matching until clears every partial match of its by-key and is otherwise ignored.

Watermarks (seqwatch.watermark, seqwatch.engine). The processing clock is the arrival time of the event being ingested, injected rather than read from the wall clock. A host's watermark is the maximum event time seen on that host minus the lateness. Events are released to the state machines, in key order, once their event time is strictly below the host's watermark. When the clock passes a host's last arrival plus the idle timeout, the host is flushed: its buffer is released and its watermark jumps to the maximum event time it has sent. The idle timeout must be at least the lateness (checked at configuration). An event whose (event_time, event_id) sorts before an event already released for its host is late and goes to the late output with the host, how late it was (the released event time minus its own) and the reason (behind_watermark or after_idle_flush).

Why the alerts do not depend on arrival order. Suppose every event arrives no more than the lateness bound L after its event time, and the idle timeout is at least L. Take an event e for host h and an event r already released for h. Either r was released by the watermark, so r.time < f.time - L for some earlier-ingested event f, or by an idle flush, so r.time is at most the maximum event time h had sent when the clock passed its last arrival plus the idle timeout.

  • In the first case f.arrival >= f.time > r.time + L. If e sorted before r, then e.time <= r.time and e.arrival <= e.time + L < f.arrival, but e is ingested after f, so e.arrival >= f.arrival. Contradiction.
  • In the second case e arrives after that flush, so e.arrival is strictly greater than the host's last arrival plus the idle timeout, and e.time >= e.arrival - L is strictly greater than that last arrival, which is at least every event time the host had sent. So e sorts after r.

So nothing is late, and each machine sees exactly the host's events in sorted order. Every rule is partitioned by host, so per-host order is all a machine needs, and one offline host never holds back another. The batch oracle (seqwatch.batch) sorts all events and runs the same machines; tests/test_property.py checks the equality with Hypothesis over generated arrival orders, and also that for arbitrary delays the streaming alerts equal the oracle on the events not reported late.

State is bounded. Partial matches are keyed by (by-key, stage, join value) and a partial match at stage k holds a step-k event no more than maxspan old, so per rule and host there are at most as many as the step-matching events in one maxspan window. A host's reorder buffer holds at most the events that fall inside one lateness-wide window of event time, plus the event being admitted. After flush, buffered events and partial matches are both 0. The window bound holds for hosts that keep sending: a silent host keeps its stale partial matches (still bounded as above) until its next event or the final flush. The tests assert each of these against the streaming engine's own per-host peaks, not against a total.

Quick start

python -m venv .venv && . .venv/bin/activate
pip install -e ".[dev]"

seqwatch gen --out events.jsonl --truth truth.json      # seed 23: 50 hosts, 6 h, ~60,000 events
seqwatch run events.jsonl --lateness 30m                # streaming, per-host watermarks
seqwatch run events.jsonl --lateness 3h
seqwatch batch events.jsonl                             # the oracle: all events sorted
seqwatch stats events.jsonl --lateness 30m              # state size and watermark lag
pytest -q

run and stats read the file in order and treat file order as arrival order; gen writes it that way. --idle-timeout defaults to the lateness. --rules takes your own rule file and --late-out writes the late events as JSON Lines.

Example output

The data is synthetic (seed 23). Nine attack chains are planted on seven hosts: two on laptops that are offline for 2 h and flush their backlog when they reconnect, and one on srv-09, whose network pipeline stalls for 2 h. Three decoys must not alert: a chain cancelled by until, a chain that spans more than maxspan, and a chain whose join does not match.

seqwatch run events.jsonl --lateness 30m:

seqwatch run: lateness 30m, idle timeout 30m, 60029 events, 6 rules
alerts: 8
COMPLETED (UTC)      RULE                        HOST    SPAN  EVENT IDS
2026-03-01 00:35:05  office-script-egress        ws-003  105s  5678,5734,5978
2026-03-01 01:08:10  temp-service-install-start  srv-02  90s   11167,11408
2026-03-01 01:25:05  office-script-egress        lt-01   105s  13968,14039,14284
2026-03-01 02:23:20  scheduled-task-create-run   ws-017  10m   22205,23883
2026-03-01 02:30:50  browser-credentials-egress  lt-02   50s   24960,25089
2026-03-01 03:20:40  debug-privilege-lsass       ws-003  40s   33406,33532
2026-03-01 04:17:00  archive-large-upload        srv-02  7m    41835,42969
2026-03-01 04:44:10  browser-credentials-egress  ws-031  50s   47310,47430

late events: 159 on 1 host
HOST    LATE  MAX LATE BY  REASON
srv-09  159   5359s        behind_watermark

The srv-09 chain is missed because its network step was delayed 2 h, far past the 30 minute bound; the stalled network events are reported on the late output. Both offline-laptop chains alert: their backlog is late only against the wall clock, not against their own host's watermark.

seqwatch run events.jsonl --lateness 3h:

seqwatch run: lateness 3h, idle timeout 3h, 60029 events, 6 rules
alerts: 9
COMPLETED (UTC)      RULE                        HOST    SPAN  EVENT IDS
2026-03-01 00:35:05  office-script-egress        ws-003  105s  5678,5734,5978
2026-03-01 01:08:10  temp-service-install-start  srv-02  90s   11167,11408
2026-03-01 01:25:05  office-script-egress        lt-01   105s  13968,14039,14284
2026-03-01 01:57:00  archive-large-upload        srv-09  7m    18389,19515
2026-03-01 02:23:20  scheduled-task-create-run   ws-017  10m   22205,23883
2026-03-01 02:30:50  browser-credentials-egress  lt-02   50s   24960,25089
2026-03-01 03:20:40  debug-privilege-lsass       ws-003  40s   33406,33532
2026-03-01 04:17:00  archive-large-upload        srv-02  7m    41835,42969
2026-03-01 04:44:10  browser-credentials-egress  ws-031  50s   47310,47430

late events: 0

seqwatch stats events.jsonl --lateness 30m:

events processed     60029
alerts               8
late events          159
max buffered events  4979
max partial matches  83
watermark lag p50    1805s
watermark lag p99    2206s

seqwatch stats events.jsonl --lateness 3h:

events processed     60029
alerts               9
late events          0
max buffered events  30086
max partial matches  78
watermark lag p50    10805s
watermark lag p99    11206s

Watermark lag is the processing clock minus the host's watermark, measured at every ingested event; the percentiles are nearest-rank.

Design decisions

  • Watermarks per host, not global. Rules are partitioned by host, so a host's events only have to be ordered against that host's other events. A global watermark would let one offline laptop hold every alert back for 2 h; per-host watermarks do not. The cost is that a rule without host.id in by is rejected, so cross-host correlation (lateral movement) is outside this tool. This is a documented limitation.
  • Late means "sorts before something already released", not "older than the watermark". Comparing the full (event_time, event_id) key against the last released event keeps the released sequence strictly ordered, including ties on event time, which is what the proof above needs. It also means an event can be behind the watermark and still be accepted if nothing newer has been released yet.
  • Latest partial match per (stage, join value). This bounds memory by construction and makes the semantics easy to state, but it is lossy: with no join, two A events before one B give one alert for the later A. An exhaustive matcher would emit more alerts and need a bound on the number of candidates.
  • until clears the whole by-key. It is a coarse cancel: any matching event on the host ends every pending match of the rule on that host, not just the one it relates to. A finer cancel would need its own join.
  • Idle flush trades latency for completeness. A silent host's events would otherwise wait for a newer event that may never come. The idle timeout must be at least the lateness, otherwise a host could be flushed while events inside the bound are still in flight.
  • Partial matches expire on the host's released event time, which never runs ahead of the watermark except after an idle flush, where nothing within the bound can still arrive. A silent host keeps its partial matches until its next event or the final flush; they are still bounded.

Testing

GITHUB_ACTIONS=true CI=true COLUMNS=80 pytest -q

85 tests pass. They cover:

  • eight hand-written micro-fixtures (join mismatch, maxspan exceeded, until cancel, overlapping partial matches, an event shared by two candidate sequences, equal timestamps ordered by event id, the same rule on two hosts, expiry at exactly maxspan), each checked against both the oracle and the streaming engine;
  • a Hypothesis property: for streams where every event arrives within the lateness bound, in any arrival order, nothing is late and the alerts equal the oracle's; and for arbitrary delays, the alerts equal the oracle's over the events not reported late;
  • the seeded data set: 8 of 9 chains alert at 30 minutes (including both offline laptops), the srv-09 chain does not and its 159 stalled network events are on the late output, all 9 alert at 3 hours with an empty late output, none of the decoys alert, every (rule, host) partial-match peak and every host's buffer peak stay within the documented bounds, state is empty after flush, and alert ids and order are deterministic;
  • idle flush, rejection of an idle timeout shorter than the lateness, rule validation messages that name the rule and step, and the CLI.

Project layout

src/seqwatch/
  events.py      event model and JSON Lines I/O
  duration.py    30m / 3h style durations
  rules.py       YAML rules, validation, predicates
  nfa.py         per-rule state machine (RuleMachine, Alert)
  watermark.py   per-host watermarks, idle flush, config check
  engine.py      reorder buffers, late output, state counters
  batch.py       reference evaluator (the oracle)
  gen.py         seeded EDR telemetry, attack chains, decoys
  report.py      text tables for alerts, late events, stats
  cli.py         gen, run, batch, stats
  data/edr-pack.yml   the six-rule pack
tests/           fixtures, property tests, seeded data set, CLI

License

MIT. See LICENSE.

About

An event-time sequence detector for EDR telemetry that returns the same alerts in any arrival order within a stated lateness bound and reports every event later than it.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages