Collection¶
The collection module manages groups of Sigma rules with loading, merging, and reference resolution capabilities.
SigmaCollection¶
- class sigma.collection.SigmaCollection[source]¶
Bases:
objectCollection of Sigma rules
- init_rules: dataclasses.InitVar[list[sigma.rule.rule.SigmaRule | sigma.correlations.SigmaCorrelationRule | sigma.filters.SigmaFilter]]¶
- errors: list[SigmaError]¶
- collect_filters: dataclasses.InitVar[bool] = False¶
- resolve_references: dataclasses.InitVar[bool] = True¶
- rules: list[SigmaRule | SigmaCorrelationRule]¶
- filters: list[SigmaFilter]¶
- ids_to_rules: dict[UUID, SigmaRule | SigmaCorrelationRule]¶
- names_to_rules: dict[str, SigmaRule | SigmaCorrelationRule]¶
- apply_filters(filters)[source]¶
Apply filters on each rule and replace the rule with the filtered rule
- Parameters:
self (Self)
filters (list[SigmaFilter])
- Return type:
None
- resolve_rule_references()[source]¶
Resolve rule references in correlation rules to the actual rule objects and sort the rules by reference order (rules that are referenced by other rules come first).
This must be called before referencing rules are converted into queries to make references available.
- Parameters:
self (Self)
- Return type:
None
- classmethod from_dicts(rules, collect_errors=False, source=None, collect_filters=False, resolve_references=True, policy=None)[source]¶
Generate a rule collection from list of dicts containing parsed YAML content.
If the collect_errors parameters is set, exceptions are not raised while parsing but collected in the errors property individually for each Sigma rule and the whole SigmaCollection.
If collect_filters is set, filters are only collected in the collection but not yet applied to the rules.
- Parameters:
rules (list[NestedDict])
collect_errors (bool)
source (SigmaRuleLocation | None)
collect_filters (bool)
resolve_references (bool)
policy (SigmaPolicy | None)
- Return type:
Self
- classmethod from_yaml(yaml_str, collect_errors=False, source=None, collect_filters=False, resolve_references=True, policy=None)[source]¶
Generate a rule collection from a string containing one or multiple YAML documents.
If the collect_errors parameters is set, exceptions are not raised while parsing but collected in the errors property individually for each Sigma rule and the whole SigmaCollection.
If collect_filters is set, filters are only collected in the collection but not yet applied to the rules.
- Parameters:
yaml_str (bytes | str | IO[Any])
collect_errors (bool)
source (SigmaRuleLocation | None)
collect_filters (bool)
resolve_references (bool)
policy (SigmaPolicy | None)
- Return type:
Self
- classmethod resolve_paths(inputs, recursion_pattern='**/*.yml')[source]¶
Resolve list of paths inputs that can contain files as well as directories into a flat list of files matching resursion_pattern.
- Parameters:
inputs (list[str | Path])
recursion_pattern (str)
- Return type:
Iterable[Path]
- classmethod load_ruleset(inputs, collect_errors=False, on_beforeload=None, on_load=None, recursion_pattern='**/*.yml', resolve_references=True)[source]¶
Load a ruleset from a list of files or directories and construct a
SigmaCollectionobject.- Parameters:
inputs (list[str | Path]) – List of strings and
pathlib.Pathobjects that reference files orcollect_errors (bool)
on_beforeload (Callable[[Path], Path | None] | None)
on_load (Callable[[Path, SigmaCollection], SigmaCollection | None] | None)
recursion_pattern (str)
resolve_references (bool)
- Return type:
Self
directories that should be loaded. :param collect_errors: parse or verification errors are collected in
SigmaRuleBaseobjects instead of raising them immediately. Defaults toFalse. :param on_beforeload: Optional function that is called for each path to a Sigma rule before the parsing and construction of theSigmaCollectionobject is done. The path returned by this function is used as input. A rule path is skipped ifNoneis returned. :param on_load: Optional function that is called after theSigmaCollectionwas constructed from the path. The path and the SigmaCollection object are passed to this function and it is expected to return aSigmaCollectionobject that is merged in the collection of the ruleset orNoneif the generated collection should be skipped. :param recursion_pattern: Pattern used to recurse into directories, defaults to**/*.yml.- Returns:
SigmaCollectionof all sigma rules contained in given paths.- Parameters:
inputs (list[str | Path])
collect_errors (bool)
on_beforeload (Callable[[Path], Path | None] | None)
on_load (Callable[[Path, SigmaCollection], SigmaCollection | None] | None)
recursion_pattern (str)
resolve_references (bool)
- Return type:
Self
- classmethod merge(collections, resolve_references=True)[source]¶
Merge multiple SigmaCollection objects into one and return it.
- Parameters:
collections (Iterable[SigmaCollection])
resolve_references (bool)
- Return type:
Self
- get_output_rules()[source]¶
Returns an iterator across all rules where the output property is set to true
- Parameters:
self (Self)
- Return type:
Iterable[SigmaRuleBase]
- get_unreferenced_rules()[source]¶
Returns an iterator across all rules that are not referenced by any other rule
- Parameters:
self (Self)
- Return type:
Iterable[SigmaRuleBase]
- __init__(init_rules, errors=<factory>, collect_filters=False, resolve_references=True, rules=<factory>, filters=<factory>)¶
- Parameters:
init_rules (dataclasses.InitVar[list[sigma.rule.rule.SigmaRule | sigma.correlations.SigmaCorrelationRule | sigma.filters.SigmaFilter]])
errors (list[SigmaError])
collect_filters (dataclasses.InitVar[bool])
resolve_references (dataclasses.InitVar[bool])
rules (list[SigmaRule | SigmaCorrelationRule])
filters (list[SigmaFilter])
- Return type:
None