Filters¶
The filters module implements Sigma filters, which add conditions to existing rules without changing them. See Using Sigma Filters for a guide on writing and applying filters.
SigmaFilter¶
- class sigma.filters.SigmaFilter[source]¶
Bases:
SigmaRuleBaseA Sigma filter: a separate document that adds a condition to existing rules to tune them for an environment without changing the rules themselves, e.g. to exclude known-benign activity.
A filter applies to a rule if the log source of the filter contains the log source of the rule and the rule is referenced in the
rulesfield of the filter section (orrulesisany). Correlation rules aren’t modified, but the rules they refer to are filtered like any other rule. Seeapply_on_rule()for how a filter is merged into a rule.- Parameters:
logsource – Log source the filter applies to. It may be less specific than the log sources of the rules it applies to.
filter – Parsed
filtersection with the filter detections, condition and rule references.
- classmethod from_dict(sigma_filter, collect_errors=False, source=None, policy=None)[source]¶
Converts from a dictionary object to a SigmaFilter object.
- Parameters:
sigma_filter (dict[str, Any]) – Parsed YAML content of the filter.
collect_errors (bool) – Collect errors in the
errorsattribute instead of raising the first one.source (SigmaRuleLocation | None) – Location of the filter, used in error messages.
policy (SigmaPolicy | None) – Policy used to parse the filter.
- Raises:
SigmaLogsourceError – if the log source is missing or invalid.
SigmaFilterError – if the
filtersection is missing or isn’t a map.
- Returns:
Parsed filter.
- Return type:
Self
- to_dict()[source]¶
Convert filter object into dict.
- Parameters:
self (Self)
- Return type:
dict[str, Any]
- apply_on_rule(rule)[source]¶
Apply the filter on a rule if it applies to it.
The filter detections are copied into the rule under names with a random prefix, so they can’t collide with the detections of the rule, and each condition of the rule is replaced with
(<rule condition>) and (<filter condition>). The filter condition is rewritten to the prefixed names. It can therefore only refer to detections of the filter, andthemrefers to all filter detections.- Parameters:
rule (SigmaRule | SigmaCorrelationRule) – Rule the filter is applied to. It is modified in place.
self (Self)
- Returns:
The given rule, with the filter applied if it applies to the rule.
- Return type:
- classmethod from_yaml(rule, collect_errors=False, policy=None)[source]¶
Convert YAML input string with single document into SigmaFilter object.
- Parameters:
rule (str)
collect_errors (bool)
policy (SigmaPolicy | None)
- Return type:
Self
- __init__(title='', id=None, name=None, taxonomy='sigma', related=None, status=None, description=None, license=None, references=<factory>, tags=<factory>, author=None, date=None, modified=None, fields=<factory>, falsepositives=<factory>, level=None, scope=None, errors=<factory>, source=None, custom_attributes=<factory>, policy=None, logsource=<factory>, filter=<factory>, _CONDITION_KEYWORDS=frozenset({'1', 'all', 'and', 'any', 'not', 'of', 'or'}))¶
- Parameters:
title (str)
id (UUID | None)
name (str | None)
taxonomy (str)
related (SigmaRelated | None)
status (SigmaStatus | None)
description (str | None)
license (str | None)
references (list[str])
tags (list[SigmaRuleTag])
author (str | None)
date (dt.date | None)
modified (dt.date | None)
fields (list[str])
falsepositives (list[str])
level (SigmaLevel | None)
scope (list[str] | None)
errors (list[sigma_exceptions.SigmaError])
source (SigmaRuleLocation | None)
custom_attributes (dict[str, Any])
policy (SigmaPolicy | None)
logsource (SigmaLogSource)
filter (SigmaGlobalFilter)
_CONDITION_KEYWORDS (frozenset[str])
- Return type:
None
SigmaGlobalFilter¶
- class sigma.filters.SigmaGlobalFilter[source]¶
Bases:
SigmaDetectionsThe
filtersection of a Sigma filter: the detections and the condition of the filter and the references to the rules it applies to.- Parameters:
rules – Rules the filter applies to. Either the string
"any"to apply the filter to all rules matching the log source of the filter, or a list of references to rule IDs or names.
- classmethod from_dict(detections, source=None, policy=None)[source]¶
Parse the
filtersection of a Sigma filter.The
rulesfield is required. It can beany(case-insensitive), a single rule ID or name or a list of rule IDs or names. An empty list is treated likeany. All other keys exceptconditionare parsed as detection definitions.- Parameters:
detections (dict[str, Any]) – Content of the
filtersection.source (SigmaRuleLocation | None) – Location of the filter, used in error messages.
policy (SigmaPolicy | None) – Policy used to parse the detections.
- Raises:
SigmaFilterConditionError – if the condition is missing or isn’t a single string.
SigmaFilterRuleReferenceError – if the
rulesfield is missing or invalid.
- Returns:
Parsed filter section.
- Return type:
Self
- to_dict()[source]¶
Convert the filter section into a dict, with rule references as plain strings.
- Parameters:
self (Self)
- Return type:
dict[str, Any]
- __init__(detections, condition, source=None, policy=None, rules=<factory>)¶
- Parameters:
detections (dict[str, SigmaDetection])
condition (list[str])
source (SigmaRuleLocation | None)
policy (SigmaPolicy | None)
rules (list[SigmaRuleReference] | str)
- Return type:
None