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:
objectA 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:
objectLightweight logsource descriptor for events.
- category: str | None = None¶
- product: str | None = None¶
- service: str | None = 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:
objectOrchestrator 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:
collection (SigmaCollection)
pipeline (ProcessingPipeline | None)
max_workers (int | None)
max_events_per_match (int | None)
max_correlation_state (int | None)
policy (SigmaPolicy | None)
- 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]
SigmaMatch¶
- class sigma.matching.SigmaMatch[source]¶
Bases:
objectResult of a successful Sigma rule match against one or more events.
- rule: SigmaRuleBase¶
- 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:
objectEvaluates a single SigmaRule against events using a precompiled matcher.
- __init__(rule, compiler)[source]¶
- Parameters:
rule (SigmaRule)
compiler (MatchCompiler)
- 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:
objectEvaluates a SigmaCorrelationRule by tracking matches from referenced base rules.
- __init__(rule, max_state=None, max_events_per_match=100)[source]¶
- Parameters:
rule (SigmaCorrelationRule)
max_state (int | None)
max_events_per_match (int | None)
- 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:
match (SigmaMatch)
source_rule_name (str)
- 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:
objectCompiles Sigma rule detection logic into optimized Python callables.
All regular expressions (
|revalues and wildcard strings) are compiled with the regex engine of the givenSigmaPolicy. If no policy is passed, the global default policy (sigma.policy.default_policy, by default the DoS-safe RE2-backedSafePolicy) is used.- __init__(policy=None)[source]¶
- Parameters:
policy (SigmaPolicy | None)
- Return type:
None
- class sigma.matching.state.SlidingWindowState[source]¶
Bases:
objectManages 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:
group_key (tuple[Any, ...])
match (SigmaMatch)
- 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]