Rules

The rule module contains classes for parsing and representing Sigma rules.

SigmaRule

class sigma.rule.SigmaRule[source]

Bases: SigmaRuleBase, ProcessingItemTrackingMixin

A single Sigma rule.

logsource: SigmaLogSource
detection: SigmaDetections
classmethod from_dict(rule, collect_errors=False, source=None, policy=None)[source]

Convert Sigma rule parsed in dict structure into SigmaRule object.

if collect_errors is set to False exceptions are collected in the errors property of the resulting SigmaRule object. Else the first recognized error is raised as exception.

Parameters:
Return type:

Self

classmethod from_yaml(rule, collect_errors=False, policy=None)[source]

Convert YAML input string with single document into SigmaRule object.

Parameters:
  • rule (str)

  • collect_errors (bool)

  • policy (SigmaPolicy | None)

Return type:

Self

to_dict()[source]

Convert rule object into dict.

Return type:

dict[str, Any]

__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>, detection=<factory>)
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)

  • detection (SigmaDetections)

Return type:

None

SigmaRuleBase

class sigma.rule.base.SigmaRuleBase[source]

Bases: object

SigmaRuleBase(title: ‘str’ = ‘’, id: ‘UUID | None’ = None, name: ‘str | None’ = None, taxonomy: ‘str’ = ‘sigma’, related: ‘SigmaRelated | None’ = None, status: ‘SigmaStatus | None’ = None, description: ‘str | None’ = None, license: ‘str | None’ = None, references: ‘list[str]’ = <factory>, tags: ‘list[SigmaRuleTag]’ = <factory>, author: ‘str | None’ = None, date: ‘dt.date | None’ = None, modified: ‘dt.date | None’ = None, fields: ‘list[str]’ = <factory>, falsepositives: ‘list[str]’ = <factory>, level: ‘SigmaLevel | None’ = None, scope: ‘list[str] | None’ = None, errors: ‘list[sigma_exceptions.SigmaError]’ = <factory>, source: ‘SigmaRuleLocation | None’ = None, custom_attributes: ‘dict[str, Any]’ = <factory>, policy: “‘SigmaPolicy | None’” = None)

title: str = ''
id: UUID | None = None
name: str | None = None
taxonomy: str = 'sigma'
related: SigmaRelated | None = None
status: SigmaStatus | None = None
description: str | None = None
license: str | None = None
references: list[str]
tags: list[SigmaRuleTag]
author: str | None = None
date: dt.date | None = None
modified: dt.date | None = None
fields: list[str]
falsepositives: list[str]
level: SigmaLevel | None = None
scope: list[str] | None = None
errors: list[sigma_exceptions.SigmaError]
source: SigmaRuleLocation | None = None
custom_attributes: dict[str, Any]
policy: SigmaPolicy | None = None
classmethod from_dict_common_params(rule, collect_errors=False, source=None, policy=None)[source]

Convert Sigma rule base parsed in dict structure into kwargs dict that can be passed to the class instantiation of an object derived from the SigmaRuleBase class and the errors list. This is intended to be called only by to_dict() methods for processing the general parameters defined in the base class.

if collect_errors is set to False exceptions are collected in the errors property of the resulting SigmaRule object. Else the first recognized error is raised as exception.

Parameters:
Return type:

tuple[dict[str, Any], list[SigmaError]]

abstractmethod classmethod from_dict(rule, collect_errors=False, policy=None)[source]

Convert dict input into SigmaRule object.

Parameters:
  • rule (dict[str, Any])

  • collect_errors (bool)

  • policy (SigmaPolicy | None)

Return type:

Self

classmethod from_yaml(rule, collect_errors=False, policy=None)[source]

Convert YAML input string with single document into SigmaRule object.

Parameters:
  • rule (str)

  • collect_errors (bool)

  • policy (SigmaPolicy | None)

Return type:

Self

to_dict()[source]

Convert rule object into dict.

Parameters:

self (Self)

Return type:

dict[str, Any]

add_backreference(rule)[source]

Add backreference to another rule.

Parameters:
Return type:

None

referenced_by(rule)[source]

Check if rule is referenced by another rule.

Parameters:
Return type:

bool

set_conversion_result(result)[source]

Set conversion result.

Parameters:
  • self (Self)

  • result (list[Any])

Return type:

None

get_conversion_result()[source]

Get conversion result.

Parameters:

self (Self)

Return type:

list[Any]

set_conversion_states(state)[source]

Set conversion state.

Parameters:
Return type:

None

get_conversion_states()[source]

Get conversion state.

Parameters:

self (Self)

Return type:

list[ConversionState]

disable_output()[source]

Disable output of rule.

Parameters:

self (Self)

Return type:

None

__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)
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)

Return type:

None

SigmaYAMLLoader

class sigma.rule.base.SigmaYAMLLoader[source]

Bases: CSafeLoader

Custom YAML loader implementing additional functionality for Sigma.

Detection Classes

SigmaDetections

class sigma.rule.detection.SigmaDetections[source]

Bases: object

Sigma detection section including named detections and condition.

detections: dict[str, SigmaDetection]
condition: list[str]
source: SigmaRuleLocation | None = None
policy: SigmaPolicy | None = None
classmethod from_dict(detections, source=None, policy=None)[source]
Parameters:
Return type:

Self

to_dict()[source]
Parameters:

self (Self)

Return type:

dict[str, Any]

__init__(detections, condition, source=None, policy=None)
Parameters:
Return type:

None

SigmaDetection

class sigma.rule.detection.SigmaDetection[source]

Bases: ParentChainMixin

A detection is a set of atomic event definitions represented by SigmaDetectionItem instances. SigmaDetectionItems of a SigmaDetection are OR-linked.

A detection can be defined by:

  1. a mapping between field/value pairs that all should appear in matched events.

  2. a plain value

  3. a list of plain values or mappings defined and matched as in 1 where at least one of the items should appear in matched events.

detection_items: list[SigmaDetectionItem | SigmaDetection]
source: SigmaRuleLocation | None = None
item_linking: type[ConditionAND | ConditionOR] | None = None
classmethod from_definition(definition, source=None, policy=None)[source]

Instantiate an appropriate SigmaDetection object from a parsed Sigma detection definition.

Parameters:
  • definition (Mapping[str, Any] | list[int | float | str | bool | None] | float | str | bool | None)

  • source (SigmaRuleLocation | None)

  • policy (SigmaPolicy | None)

Return type:

Self

to_plain()[source]

Returns a dictionary or list representation of the detection.

AND-linked detection items become a dictionary, OR-linked ones a list. Detections created by processing may contain structures without a representation in a Sigma rule, for example several AND-linked sub-detections. For these a SigmaValueError is raised instead of returning a representation with a different meaning.

Parameters:

self (Self)

Return type:

dict[str, Any] | list[Any] | str | int | float | bool | None

postprocess(detections, parent=None, source=None)[source]

Convert detection item into condition tree element

Parameters:
Return type:

ConditionItem | ConditionFieldEqualsValueExpression | ConditionValueExpression | None

add_applied_processing_item(processing_item)[source]

Propagate processing item to all contained detection items.

Parameters:

processing_item (ProcessingItemBase | None)

Return type:

None

__init__(detection_items, source=None, item_linking=None)
Parameters:
Return type:

None

SigmaDetectionItem

class sigma.rule.detection.SigmaDetectionItem[source]

Bases: ProcessingItemTrackingMixin, ParentChainMixin

Single Sigma detection definition

A detection consists of: * an optional field name * a list of value modifiers that can also be empty * the mandatory value or a list of values (internally it’s always a list of values)

By default all values are OR-linked but the ‘all’ modifier can be used to override this behavior.

If the auto_modifiers parameter is set to False, modifiers are not automatically applied to the values. This shouldn’t normally be used, but only in test scenarios.

field: str | None
modifiers: list[type[SigmaModifier[Any, Any]]]
value: list[SigmaType]
value_linking

alias of ConditionOR

negated: bool = False
source: SigmaRuleLocation | None = None
original_value: list[SigmaType] | None
auto_modifiers: bool = True
policy: SigmaPolicy | None = None
apply_modifiers()[source]

Applies modifiers to detection and values

Parameters:

self (Self)

Return type:

None

classmethod from_mapping(key, val, source=None, policy=None)[source]

Constructs SigmaDetectionItem object from a mapping between field name containing modifiers and a value. This also supports keys containing only value modifiers which results in a keyword detection.

The value accepts plain values as well as lists of values and resolves them into the value list always contained in a SigmaDetectionItem instance.

Parameters:
  • key (str | None)

  • val (list[float | str | bool | None] | float | str | bool | None)

  • source (SigmaRuleLocation | None)

  • policy (SigmaPolicy | None)

Return type:

Self

classmethod from_value(val, source=None, policy=None)[source]

Convenience method for from_mapping(None, value).

Parameters:
Return type:

Self

disable_conversion_to_plain()[source]

Mark detection item as not convertible to plain data type. This is required in cases where the value and original value get out of sync, e.g. because transformation are applied and conversion with to_plain() would yield an outdated state.

Parameters:

self (Self)

Return type:

None

to_plain()[source]

Convert detection item into plain Python type, that can be:

  • a plain value if it is a single plain keyword value.

  • a list of values if it is a list of keyword values

  • a dict in all other cases (detection item bound to field or keyword with modifiers)

Parameters:

self (Self)

Return type:

dict[str, Any] | list[Any] | str | int | float | bool | None

postprocess(detections, parent=None, source=None)[source]

Minimal default postprocessing implementation for classes which don’t bring their own postprocess method. Just sets the parent and source property.

Parameters:
Return type:

ConditionItem | ConditionAND | ConditionOR | ConditionFieldEqualsValueExpression | ConditionValueExpression | None

is_keyword()[source]

Returns True if detection item is a keyword detection without field reference.

Parameters:

self (Self)

Return type:

bool

__init__(field, modifiers, value, value_linking=<class 'sigma.conditions.ConditionOR'>, negated=False, source=None, auto_modifiers=True, policy=None)
Parameters:
Return type:

None

Log Source

SigmaLogSource

class sigma.rule.logsource.SigmaLogSource[source]

Bases: object

SigmaLogSource(category: str | None = None, product: str | None = None, service: str | None = None, definition: str | None = None, source: sigma.exceptions.SigmaRuleLocation | None = None, custom_attributes: dict[str, Any] | None = None)

category: str | None = None
product: str | None = None
service: str | None = None
definition: str | None = None
source: SigmaRuleLocation | None = None
custom_attributes: dict[str, Any] | None = None
classmethod from_dict(logsource, source=None)[source]

Returns SigmaLogSource object from dict with fields.

Parameters:
Return type:

SigmaLogSource

to_dict()[source]
Return type:

dict[str, Any]

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

  • product (str | None)

  • service (str | None)

  • definition (str | None)

  • source (SigmaRuleLocation | None)

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

Return type:

None

Rule Attributes

SigmaStatus

class sigma.rule.attributes.SigmaStatus[source]

Bases: EnumLowercaseStringMixin, Enum

UNSUPPORTED = 1
DEPRECATED = 2
EXPERIMENTAL = 3
TEST = 4
STABLE = 5

SigmaLevel

class sigma.rule.attributes.SigmaLevel[source]

Bases: EnumLowercaseStringMixin, Enum

INFORMATIONAL = 1
LOW = 2
MEDIUM = 3
HIGH = 4
CRITICAL = 5

SigmaRuleTag

class sigma.rule.attributes.SigmaRuleTag[source]

Bases: object

SigmaRuleTag(namespace: ‘str’, name: ‘str’, source: ‘SigmaRuleLocation | None’ = None)

namespace: str
name: str
source: SigmaRuleLocation | None = None
classmethod from_str(tag, source=None)[source]

Build SigmaRuleTag class from plain text tag string.

Parameters:
Return type:

SigmaRuleTag

__init__(namespace, name, source=None)
Parameters:
Return type:

None