Matching

The sigma.matching package provides a pure-Python runtime for evaluating log events directly against Sigma rules, without converting them to a query language.

Performance Note

This engine is not designed for high-throughput production workloads. It prioritises correctness and ease of use over speed. For performance-sensitive use cases, consider the Rust-based Sigma engine rsigma.

See also: Matching Events Against Rules for a step-by-step introduction with examples.

Event

class sigma.matching.Event[source]

Bases: object

A parsed log event to be matched against Sigma rules.

timestamp: datetime
logsource: EventLogSource
data: dict[str, Any]
raw: str | None = None
get_raw()[source]

Return raw log representation, falling back to JSON-serialized data.

Return type:

str

__init__(timestamp, logsource, data, raw=None)
Parameters:
  • timestamp (datetime)

  • logsource (EventLogSource)

  • data (dict[str, Any])

  • raw (str | None)

Return type:

None

class sigma.matching.EventLogSource[source]

Bases: object

Lightweight logsource descriptor for events.

category: str | None = None
product: str | None = None
service: str | None = None
as_tuple()[source]
Return type:

tuple[str | None, str | None, str | None]

__init__(category=None, product=None, service=None)
Parameters:
  • category (str | None)

  • product (str | None)

  • service (str | None)

Return type:

None

SigmaMatcher

class sigma.matching.SigmaMatcher[source]

Bases: object

Orchestrator that matches events against a collection of Sigma rules.

Manages logsource dispatch, threading, and correlation.

__init__(collection, pipeline=None, max_workers=None, max_events_per_match=100, max_correlation_state=None, policy=None)[source]
Parameters:
Return type:

None

match(event)[source]

Match a single event against all relevant rules. Returns list of matches.

Parameters:

event (Event)

Return type:

list[SigmaMatch]

bulk_match(events)[source]

Match multiple events, returning all matches. Processes sequentially for ordering.

Parameters:

events (Iterable[Event])

Return type:

list[SigmaMatch]

close()[source]

Clean up resources.

Return type:

None

SigmaMatch

class sigma.matching.SigmaMatch[source]

Bases: object

Result of a successful Sigma rule match against one or more events.

rule: SigmaRuleBase
events: list[Event]
timestamp: datetime
group_values: dict[str, Any] | None = None
__init__(rule, events, timestamp, group_values=None)
Parameters:
  • rule (SigmaRuleBase)

  • events (list[Event])

  • timestamp (datetime)

  • group_values (dict[str, Any] | None)

Return type:

None

SigmaRuleMatcher

class sigma.matching.SigmaRuleMatcher[source]

Bases: object

Evaluates a single SigmaRule against events using a precompiled matcher.

__init__(rule, compiler)[source]
Parameters:
Return type:

None

matches(event)[source]

Return a SigmaMatch if the event matches this rule, else None.

Parameters:

event (Event)

Return type:

SigmaMatch | None

SigmaCorrelationRuleMatcher

class sigma.matching.SigmaCorrelationRuleMatcher[source]

Bases: object

Evaluates a SigmaCorrelationRule by tracking matches from referenced base rules.

__init__(rule, max_state=None, max_events_per_match=100)[source]
Parameters:
Return type:

None

receive_match(match, source_rule_name)[source]

Process an incoming match from a base rule and return correlation matches if any.

Parameters:
Return type:

list[SigmaMatch]

Internal Components

The following classes are used internally by SigmaMatcher and are not part of the public API, but are documented here for completeness.

class sigma.matching.compiler.MatchCompiler[source]

Bases: object

Compiles Sigma rule detection logic into optimized Python callables.

All regular expressions (|re values and wildcard strings) are compiled with the regex engine of the given SigmaPolicy. If no policy is passed, the global default policy (sigma.policy.default_policy, by default the DoS-safe RE2-backed SafePolicy) is used.

__init__(policy=None)[source]
Parameters:

policy (SigmaPolicy | None)

Return type:

None

compile_rule(rule)[source]

Compile a SigmaRule’s conditions into a single callable matcher.

Parameters:

rule (SigmaRule)

Return type:

Callable[[dict[str, Any], str | None], bool]

class sigma.matching.state.SlidingWindowState[source]

Bases: object

Manages sliding time window state for correlation rules.

__init__(window_seconds, max_entries=None)[source]
Parameters:
  • window_seconds (int)

  • max_entries (int | None)

Return type:

None

add(group_key, match)[source]

Add a match to the state for the given group key.

Parameters:
Return type:

None

get_window(group_key, now)[source]

Get all matches within the time window for the given group key.

Parameters:
  • group_key (tuple[Any, ...])

  • now (datetime)

Return type:

list[SigmaMatch]

evict()[source]

Remove empty groups.

Return type:

None