Types¶
The types module defines the value type system used in Sigma detection items.
Enumerations¶
- class sigma.types.SpecialChars[source]¶
Bases:
EnumEnumeration of supported special characters
- WILDCARD_MULTI = 1¶
- WILDCARD_SINGLE = 2¶
Base Type¶
Simple Types¶
- class sigma.types.SigmaNull[source]¶
Bases:
SigmaTypeEmpty/none/null value
- null: ClassVar[None] = None¶
- class sigma.types.SigmaExists[source]¶
Bases:
SigmaTypeField existence check.
- exists: bool¶
- __init__(exists)¶
- Parameters:
exists (bool)
- Return type:
None
- class sigma.types.SigmaNumber[source]¶
Bases:
SigmaTypeNumeric 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:
SigmaTypeBoolean 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)
number (int)
- timestamp_part: TimestampPart¶
String Types¶
- class sigma.types.SigmaString[source]¶
Bases:
SigmaTypeStrings 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]¶
- 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:
- 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:
- 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_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:
- 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:
- class sigma.types.SigmaCasedString[source]¶
Bases:
SigmaStringCase-sensitive string matching.
- classmethod from_sigma_string(s)[source]¶
- Parameters:
s (SigmaString)
- Return type:
Complex Types¶
- class sigma.types.SigmaRegularExpression[source]¶
Bases:
SigmaTypeRegular 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
- 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:
- 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:
regexp_init (InitVar[SigmaString | str])
flags (set[SigmaRegularExpressionFlag])
policy (SigmaPolicy | None)
- Return type:
None
- class sigma.types.SigmaCIDRExpression[source]¶
Bases:
NoPlainConversionMixin,SigmaTypeCIDR 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:
cidr (str)
source (SigmaRuleLocation | None)
- Return type:
None
- class sigma.types.SigmaCompareExpression[source]¶
Bases:
NoPlainConversionMixin,SigmaTypeType for numeric comparison.
- number: SigmaNumber¶
- source: SigmaRuleLocation | None = None¶
- __init__(number, op, source=None)¶
- Parameters:
number (SigmaNumber)
op (CompareOperators)
source (SigmaRuleLocation | None)
- Return type:
None
- class sigma.types.SigmaFieldReference[source]¶
Bases:
NoPlainConversionMixin,SigmaTypeType 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,SigmaTypeSpecial 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¶
- __init__(expr, id)¶
- Parameters:
expr (str)
id (str)
- Return type:
None
- class sigma.types.SigmaExpansion[source]¶
Bases:
NoPlainConversionMixin,SigmaTypeSpecial 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:
the whole expansion is handled as group which is enclosed in parentheses.
the values contained in the expansion are linked with OR, independent from the linking of the context that encloses the expansion.