Types

The types module defines the value type system used in Sigma detection items.

Enumerations

class sigma.types.SpecialChars[source]

Bases: Enum

Enumeration of supported special characters

WILDCARD_MULTI = 1
WILDCARD_SINGLE = 2
class sigma.types.TimestampPart[source]

Bases: Enum

Enumeration of supported datetime parts for a SigmaTimestamp object

MINUTE = 1
HOUR = 2
DAY = 3
WEEK = 4
MONTH = 5
YEAR = 6
class sigma.types.CompareOperators[source]

Bases: Enum

LT = 1
LTE = 2
GT = 3
GTE = 4
NEQ = 5
class sigma.types.SigmaRegularExpressionFlag[source]

Bases: Enum

IGNORECASE = 1
MULTILINE = 2
DOTALL = 3

Base Type

class sigma.types.SigmaType[source]

Bases: ABC

Base class for Sigma value types

__init__(dummy)[source]
Parameters:

dummy (Any)

Return type:

None

to_plain()[source]

Return plain Python value (str, int etc.) from SigmaType instance for usage in conversion of Sigma rules back to dicts. Uses the first annotated member as return value.

Return type:

Any

Simple Types

class sigma.types.SigmaNull[source]

Bases: SigmaType

Empty/none/null value

null: ClassVar[None] = None
__init__(dummy=None)[source]
Parameters:

dummy (Any | None)

class sigma.types.SigmaExists[source]

Bases: SigmaType

Field existence check.

exists: bool
__init__(exists)
Parameters:

exists (bool)

Return type:

None

class sigma.types.SigmaNumber[source]

Bases: SigmaType

Numeric value type

number: int | float
init_number: dataclasses.InitVar[Any]
__init__(init_number)
Parameters:

init_number (dataclasses.InitVar[Any])

Return type:

None

class sigma.types.SigmaBool[source]

Bases: SigmaType

Boolean value type

boolean: bool
__init__(boolean)
Parameters:

boolean (bool)

Return type:

None

class sigma.types.SigmaTimestampPart[source]

Bases: SigmaNumber

__init__(timestamp_part, number)[source]
Parameters:
timestamp_part: TimestampPart

String Types

class sigma.types.SigmaString[source]

Bases: SigmaType

Strings in Sigma detection values containing wildcards.

max_placeholder_expansions: ClassVar[int] = 200
__init__(s=None, escape=True)[source]

Initializes SigmaString instance from raw string by parsing it:

  • characters from char_mapping are interpreted as special characters and interrupt the plain string in the resulting sequence

  • escape_char disables special character mapping in the next character

  • if escaping character is followed by a character without special meaning the escaping character is used as plain character

Parameters:
  • s (str) – string to be parsed

  • escape (bool) – whether to enable escaping of special characters

original: str
s: list[str | SpecialChars | Placeholder]
classmethod from_str(s)[source]
Parameters:

s (str)

Return type:

SigmaString

insert_placeholders()[source]

Replace %something% placeholders with Placeholder stub objects that can be later handled by the processing pipeline. This implements the expand modifier.

Iterates over the parts in self.s. For plain string parts, scans for %name% patterns and replaces them with Placeholder objects. Backslashes are treated as ordinary literal characters — a backslash immediately before %name% is emitted as a literal backslash followed by the placeholder. The placeholder name must be non-empty and must not contain backslashes or percent signs; otherwise the opening % is treated as a literal character.

Non-string parts (special characters, existing placeholders) are preserved unchanged.

Return type:

SigmaString

replace_with_placeholder(regex, placeholder_name)[source]

Replace all occurrences of string part matching regular expression with placeholder.

Parameters:
  • regex (Pattern) – regular expression that should be matched.

  • placeholder_name (str) – name of placeholder that should be inserted.

Returns:

Returns a string with the replacement placeholders.

Return type:

SigmaString

to_plain(regex=False, escape_backslash=False)[source]

Generate string representation of SigmaString with or without regex escaping.

If escape_backslash is set, backslashes that would be interpreted as escaping character when the result is parsed again (a backslash followed by a wildcard or another backslash) are escaped, so that parsing the result yields the same SigmaString.

Parameters:
  • regex (bool)

  • escape_backslash (bool)

Return type:

str

to_plain_regex()[source]

Return plain string representation of SigmaString with reduced escaping.

Return type:

str

startswith(val)[source]

Check if string starts with a given string or special character.

Parameters:

val (str | SpecialChars | Placeholder)

Return type:

bool

endswith(val)[source]

Check if string ends with a given string or special character.

Parameters:

val (str | SpecialChars | Placeholder)

Return type:

bool

contains_special()[source]

Check if string contains special characters.

Return type:

bool

contains_placeholder(include=None, exclude=None)[source]

Check if string contains placeholders and if any placeholder name is

  • contained on the include list (if there is one)

  • not contained on the include list (if there is one)

It is sufficient that one placeholder matches these conditions. The purpose of this method is to determine if there are placeholders for further processing.

Parameters:
  • include (list[str] | None)

  • exclude (list[str] | None)

Return type:

bool

replace_placeholders(callback)[source]

Iterate over all placeholders and call the callback for each one. The callback is called with the placeholder instance as argument and yields replacement values (plain strings or SpecialChars instances). Each yielded replacement value is concatenated to the SigmaString prefix before the placeholder and the method is called recursively with the suffix after the placeholder. All placeholder replacements are combined with all returned SigmaString suffixes. Therefore, the callback could be called multiple times with the same placeholder instance and should return the same results to ensure a consistent result.

The callback can return a plain string, a SpecialChars instance (for insertion of wildcards) or a Placeholder (e.g. to keep the placeholder for later processing pipeline items).

The total number of expanded strings is limited to max_placeholder_expansions.

Parameters:

callback (Callable[[Placeholder], Iterator[str | SpecialChars | Placeholder | SigmaString]])

Return type:

list[SigmaString]

iter_parts()[source]
Return type:

Iterable[str | SpecialChars | Placeholder]

map_parts(func, filter_func=<function SigmaString.<lambda>>, interpret_special=False)[source]
Parameters:
  • func (Callable[[str | SpecialChars | Placeholder], str | SpecialChars | Placeholder | None])

  • filter_func (Callable[[str | SpecialChars | Placeholder], bool])

  • interpret_special (bool)

Return type:

SigmaString

map_str_parts(func)[source]
Parameters:

func (Callable[[str], str | None])

Return type:

SigmaString

convert(escape_char='\\', wildcard_multi='*', wildcard_single='?', add_escaped='', filter_chars='')[source]

Convert SigmaString into a query string or pattern. The following parameters allow to change the behavior:

  • escape_char: the character used to escape special characters. By default these are only the wildcard characters.

  • wildcard_multi and wildcard_single: strings that should be output as wildcards for multiple and single characters.

  • add_escaped: characters which are escaped in addition to the wildcards

  • filter_chars: characters that are filtered out.

Setting one of the wildcard or multiple parameters to None indicates that this feature is not supported. Appearance of these characters in a string will raise a SigmaValueError.

Parameters:
  • escape_char (str | None)

  • wildcard_multi (str | None)

  • wildcard_single (str | None)

  • add_escaped (str)

  • filter_chars (str)

Return type:

str

to_regex(custom_escaped='')[source]

Convert SigmaString into a regular expression.

Parameters:

custom_escaped (str)

Return type:

SigmaRegularExpression

upper()[source]
Return type:

SigmaString

lower()[source]
Return type:

SigmaString

snake_case()[source]
Return type:

SigmaString

class sigma.types.SigmaCasedString[source]

Bases: SigmaString

Case-sensitive string matching.

classmethod from_sigma_string(s)[source]
Parameters:

s (SigmaString)

Return type:

SigmaCasedString

Complex Types

class sigma.types.SigmaRegularExpression[source]

Bases: SigmaType

Regular expression type

regexp: SigmaString
regexp_init: InitVar[SigmaString | str]
flags: set[SigmaRegularExpressionFlag]
policy: SigmaPolicy | None = None
sigma_to_python_flags: ClassVar[dict[SigmaRegularExpressionFlag, re.RegexFlag]] = {SigmaRegularExpressionFlag.DOTALL: re.DOTALL, SigmaRegularExpressionFlag.IGNORECASE: re.IGNORECASE, SigmaRegularExpressionFlag.MULTILINE: re.MULTILINE}
sigma_to_re_flag: ClassVar[dict[SigmaRegularExpressionFlag, str]] = {SigmaRegularExpressionFlag.DOTALL: 's', SigmaRegularExpressionFlag.IGNORECASE: 'i', SigmaRegularExpressionFlag.MULTILINE: 'm'}
add_flag(flag)[source]
Parameters:

flag (SigmaRegularExpressionFlag)

Return type:

None

compile()[source]

Verify if regular expression is valid by compiling it

Return type:

None

to_plain()[source]

Return plain Python value (str, int etc.) from SigmaType instance for usage in conversion of Sigma rules back to dicts. Uses the first annotated member as return value.

Return type:

str

escape(escaped=(), escape_char='\\', escape_escape_char=True, flag_prefix=True)[source]

Escape strings from escaped tuple as well as escape_char itself (can be disabled with escape_escape_char) with escape_char. Prepends a (?…) expression with set flags (i, m and s) if flag_prefix is set.

Parameters:
  • escaped (list[str])

  • escape_char (str)

  • escape_escape_char (bool)

  • flag_prefix (bool)

Return type:

str

contains_placeholder(include=None, exclude=None)[source]
Parameters:
  • include (list[str] | None)

  • exclude (list[str] | None)

Return type:

bool

insert_placeholders()[source]

Replace %something% placeholders with Placeholder stub objects that can be later handled by the processing pipeline. This implements the expand modifier.

Return type:

SigmaRegularExpression

replace_placeholders(callback)[source]

Replace all occurrences of string part matching regular expression with placeholder.

Parameters:

callback (Callable[[Placeholder], Iterator[str | SpecialChars | Placeholder | SigmaString]])

Return type:

list[SigmaRegularExpression]

__init__(regexp_init, flags=<factory>, policy=None)
Parameters:
Return type:

None

class sigma.types.SigmaCIDRExpression[source]

Bases: NoPlainConversionMixin, SigmaType

CIDR IP address range expression type

cidr: str
source: SigmaRuleLocation | None = None
network: IPv4Network | IPv6Network
expand(wildcard='*')[source]

Convert CIDR range into a list of wildcard patterns or plain CIDR notation. The following parameters allow to change the behavior:

  • wildcard: string that should be output as wildcard. Usually not required because this is passed to SigmaString that generates a wildcard pecial character from ‘*’ that is converted into possible individual wildcard characters.

Setting wildcard to None indicates that this feature is not need and the query language handles CIDR notation properly.

Parameters:

wildcard (str)

Return type:

list[str]

__init__(cidr, source=None)
Parameters:
Return type:

None

class sigma.types.SigmaCompareExpression[source]

Bases: NoPlainConversionMixin, SigmaType

Type for numeric comparison.

number: SigmaNumber
op: CompareOperators
source: SigmaRuleLocation | None = None
class CompareOperators

Bases: Enum

LT = 1
LTE = 2
GT = 3
GTE = 4
NEQ = 5
__init__(number, op, source=None)
Parameters:
Return type:

None

class sigma.types.SigmaFieldReference[source]

Bases: NoPlainConversionMixin, SigmaType

Type for referencing to other fields for comparison between them.

field: str
starts_with: bool = False
ends_with: bool = False
__init__(field, starts_with=False, ends_with=False)
Parameters:
  • field (str)

  • starts_with (bool)

  • ends_with (bool)

Return type:

None

class sigma.types.SigmaQueryExpression[source]

Bases: NoPlainConversionMixin, SigmaType

Special purpose type for passing a query part (e.g. list lookups in placeholders) directly into the generated query. The query string may contain a {field} placeholder, which is replaced with the field name contained in the detection item containing the query expression. This is done by the finalize method.

Because this is very specific to the target language, it has to be used in late stages of the conversion process by backend-specific processing pipelines or the backend itself.

expr: str
id: str
has_field_placeholder()[source]
Return type:

bool

finalize(field=None)[source]
Parameters:

field (str | None)

Return type:

str

__init__(expr, id)
Parameters:
  • expr (str)

  • id (str)

Return type:

None

class sigma.types.SigmaExpansion[source]

Bases: NoPlainConversionMixin, SigmaType

Special purpose type for correct logic linking of values expanded by modifiers. In the usual cases the writer of a Sigma rule expects the values expanded by modifiers like base64offset or windash are OR-linked, even if the value list containing the original values is linked with AND by modifying it with ‘all’. A SigmaExpansion is emitted by such modifiers, contains the expanded values and is converted as follows:

  1. the whole expansion is handled as group which is enclosed in parentheses.

  2. the values contained in the expansion are linked with OR, independent from the linking of the context that encloses the expansion.

__init__(values)
Parameters:

values (list[SigmaType])

Return type:

None

values: list[SigmaType]

Utility Functions

sigma.types.sigma_type(v)[source]

Return Sigma type from Python value

Parameters:

v (int | float | str | bool | None)

Return type:

SigmaType