Writing Processing Pipelines¶
Processing pipelines are the primary mechanism for transforming Sigma rules before they are converted into target queries. They handle tasks like field name mapping, log source adjustments, value transformations, and environment-specific adaptations.
This guide covers both YAML-based and Python-based pipeline definitions, when to use each, and the full range of available transformations and conditions.
Overview¶
A processing pipeline consists of three stages applied in order:
Transformations (rule pre-processing): Modify the Sigma rule structure before conversion. These operate on detection items, field names, and values.
Post-processing: Transform the generated query strings after backend conversion.
Finalizers: Operate on the complete set of queries for final output formatting.
When to Use YAML vs Python¶
Use YAML pipelines when:
Defining field mappings and environment-specific configurations.
Creating reusable pipeline configurations that non-developers can modify.
Distributing pipelines as configuration files.
Combining existing transformations without custom logic.
Use Python pipelines when:
Implementing custom transformation logic.
Creating reusable pipeline packages (plugins).
Needing dynamic or conditional behavior beyond what YAML supports.
Building complex pipelines with programmatic construction.
Both approaches use the same underlying ProcessingPipeline class and can be combined.
Security-sensitive runtime options such as template vars execution, allowed vars paths, external source access, and regex engine selection are carried by SigmaPolicy, not by YAML keys inside a pipeline definition.
Pipeline Priorities¶
When multiple pipelines are combined using the ProcessingPipelineResolver, they are applied
in order of their priority value (lowest first). The following priority conventions are
recommended:
Priority |
Category |
Purpose |
|---|---|---|
10 |
Log source |
Generic log source transformations |
20 |
Backend pre |
Transformations before the backend pipeline |
50 |
Backend |
Backend-specific transformations (built-in) |
60 |
Output format |
Output format-specific transformations |
YAML Pipeline Definition¶
A YAML pipeline has the following top-level structure:
name: my_pipeline
priority: 20
allowed_backends:
- my_backend
vars:
custom_var: value
transformations:
- id: transformation_1
type: transformation_type
# ... transformation parameters ...
rule_conditions:
- type: condition_type
# ... condition parameters ...
rule_cond_op: and # or "or"
rule_cond_not: false
postprocessing:
- id: postproc_1
type: postprocessing_type
# ... parameters ...
finalizers:
- type: finalizer_type
# ... parameters ...
Top-Level Keys¶
name: Optional name identifier for the pipeline.priority: Integer priority for ordering when combining pipelines.allowed_backends: Optional list of backend identifiers this pipeline is intended for.vars: Optional dictionary of variables accessible to transformations.transformations: List of rule pre-processing transformation items.postprocessing: List of query post-processing items.finalizers: List of output finalizer items.
Example: Field Mapping Pipeline¶
name: windows_field_mapping
priority: 20
transformations:
- id: windows_process_creation_fieldmapping
type: field_name_mapping
mapping:
CommandLine: process.command_line
Image: process.executable
ParentImage: process.parent.executable
User: user.name
LogonId: user.id
IntegrityLevel: process.integrity_level
rule_conditions:
- type: logsource
category: process_creation
product: windows
Example: Log Source Transformation¶
name: sysmon_logsource_mapping
priority: 10
transformations:
- id: map_to_sysmon
type: change_logsource
service: sysmon
rule_conditions:
- type: logsource
category: process_creation
product: windows
This is a typical use case for log source transformations: mapping generic Sigma rules to backend-specific log sources. In this example, any generic Windows process creation rule is mapped to Sysmon events, ensuring the rule is queried against the appropriate data source in your backend system. Backends often require rules to target specific event sources like Sysmon, WinEventLog, or other data providers to generate correct queries.
Example: Value Replacement¶
name: value_adjustments
priority: 30
transformations:
- id: replace_backslash_path
type: replace_string
regex: "\\\\\\\\+"
replacement: "/"
field_name_conditions:
- type: include_fields
fields:
- Image
- ParentImage
Python Pipeline Definition¶
In Python, pipelines are constructed using the ProcessingPipeline, ProcessingItem,
QueryPostprocessingItem, and transformation classes directly:
from sigma.processing.pipeline import ProcessingPipeline, ProcessingItem, QueryPostprocessingItem
from sigma.processing.transformations import (
FieldMappingTransformation,
ChangeLogsourceTransformation,
ReplaceStringTransformation,
)
from sigma.processing.conditions import LogsourceCondition, IncludeFieldCondition
pipeline = ProcessingPipeline(
name="my_pipeline",
priority=20,
items=[
ProcessingItem(
identifier="field_mapping",
transformation=FieldMappingTransformation({
"CommandLine": "process.command_line",
"Image": "process.executable",
}),
rule_conditions=[
LogsourceCondition(
category="process_creation",
product="windows",
)
],
),
ProcessingItem(
identifier="logsource_change",
transformation=ChangeLogsourceTransformation(
category="endpoint",
),
rule_conditions=[
LogsourceCondition(
category="process_creation",
product="windows",
)
],
),
],
)
Loading Pipelines from YAML¶
from sigma.processing.pipeline import ProcessingPipeline
# From a YAML string
pipeline = ProcessingPipeline.from_yaml(yaml_string)
# From a file
with open("pipeline.yml") as f:
pipeline = ProcessingPipeline.from_yaml(f.read(), source_path="pipeline.yml")
To opt into security-sensitive behavior, pass a ``SigmaPolicy`` explicitly:
.. code-block:: python
from sigma.policy import SigmaPolicy
from sigma.policy.regex_engine import RE2RegexEngine
from sigma.processing.pipeline import ProcessingPipeline
policy = SigmaPolicy(
regex_engine=RE2RegexEngine(),
allow_template_vars=True,
)
with open("pipeline.yml") as f:
pipeline = ProcessingPipeline.from_yaml(
f.read(),
source_path="pipeline.yml",
policy=policy,
)
.. note::
``policy``, ``allow_template_vars``, ``vars_allowed_paths``,
``allow_external_sources``, and ``restrict_template_path`` are not part of the
trusted YAML schema for pipeline items. pySigma sanitizes these keys when nested
transformation or finalizer definitions are loaded from dictionaries or YAML.
See :doc:`sigma_policy` for the effective policy model.
Combining Pipelines¶
There are two ways to combine pipelines, but only one respects priority ordering:
Using ProcessingPipelineResolver (Recommended)
The resolver automatically sorts pipelines by priority before merging them:
from sigma.processing.resolver import ProcessingPipelineResolver
resolver = ProcessingPipelineResolver()
resolver.add_pipeline_class(pipeline1) # e.g., priority=10
resolver.add_pipeline_class(pipeline2) # e.g., priority=20
# Resolves and merges in priority order (pipeline1, then pipeline2)
combined = resolver.resolve([pipeline1.name, pipeline2.name])
Simple Addition (Use with Caution)
Direct addition with the + operator does not respect pipeline priorities:
# WARNING: This combines pipelines in the order specified, ignoring priorities
combined = pipeline1 + pipeline2 # Always in this order, regardless of priority values
Important
Use ProcessingPipelineResolver.resolve() when you have multiple pipelines with
different priority values. Simple addition (+) concatenates pipelines in the order
specified without considering priorities. If pipeline priorities matter for your use case,
use the resolver.
Conditions¶
Conditions control when a transformation is applied. There are three levels of conditions:
Rule conditions: Evaluated against the whole rule (e.g., log source matching).
Detection item conditions: Evaluated against individual detection items.
Field name conditions: Evaluated against field names.
Rule Conditions¶
Applied at the processing item level. If all conditions match (by default), the transformation is applied.
YAML Type |
Python Class |
Description |
|---|---|---|
|
|
Match by log source category, product, and/or service. |
|
|
Rule contains a detection item with specified field and value. |
|
|
Rule contains a field with the specified field name. |
|
|
The rule is a regular Sigma rule (not a correlation rule). |
|
|
The rule is a correlation rule. |
|
|
Match by rule attribute (title, id, status, level, etc.). |
|
|
Match by rule tag. |
|
|
A specific processing item was already applied to this rule. |
|
|
Match by pipeline state variable. |
Condition linking: By default, multiple conditions are combined with AND (all must match).
Use rule_cond_op: or to use OR logic. Use rule_cond_not: true to negate the result.
Condition expressions: For complex logic, use rule_cond_expr instead of rule_cond_op:
transformations:
- id: complex_condition
type: field_name_mapping
mapping:
FieldA: field_a
rule_conditions:
cond1:
type: logsource
category: process_creation
cond2:
type: logsource
product: windows
rule_cond_expr: cond1 and not cond2
Detection Item Conditions¶
Applied per detection item within a rule.
YAML Type |
Python Class |
Description |
|---|---|---|
|
|
Detection item value matches a string pattern. |
|
|
Detection item value matches a specific value. |
|
|
Detection item value contains wildcard characters. |
|
|
Detection item value is null. |
|
|
A specific processing item was applied to this detection item. |
|
|
Match by processing state. |
Field Name Conditions¶
Control which field names a transformation applies to.
YAML Type |
Python Class |
Description |
|---|---|---|
|
|
Only apply to listed field names (supports wildcards). |
|
|
Exclude listed field names from transformation. |
|
|
A specific processing item was applied. |
|
|
Match by processing state. |
Transformations Reference¶
Rule Pre-Processing Transformations¶
These are the type values for the transformations section.
Field Transformations:
YAML Type |
Python Class |
Description |
|---|---|---|
|
|
Map field names using a dictionary. Supports one-to-many mappings. |
|
|
Map field name prefixes (e.g., |
|
|
Apply a function to field names (e.g., |
|
|
Add a suffix to all matching field names. |
|
|
Add a prefix to all matching field names. |
|
|
Add a new field to the rule’s fields list. |
|
|
Remove a field from the rule’s fields list. |
|
|
Set a field in detection items to a specific name. |
Value Transformations:
YAML Type |
Python Class |
Description |
|---|---|---|
|
|
Replace strings in values using regex patterns. |
|
|
Map string values using a dictionary. |
|
|
Transform values using regex with capture groups. |
|
|
Set detection item values to a fixed value. |
|
|
Convert value types (e.g., string to regex). |
|
|
Change case of string values (lower, upper, title). |
|
|
Split detection items with hash values into separate items per algorithm. |
|
|
Extract and expand fields from detection items into separate fields. |
Detection Item Transformations:
YAML Type |
Python Class |
Description |
|---|---|---|
|
|
Remove a detection item from the detection. |
Condition Transformations:
YAML Type |
Python Class |
Description |
|---|---|---|
|
|
Add a new condition expression to the detection. |
Rule Transformations:
YAML Type |
Python Class |
Description |
|---|---|---|
|
|
Change the log source category, product, or service. |
|
|
Set a custom attribute on the rule. |
State Transformations:
YAML Type |
Python Class |
Description |
|---|---|---|
|
|
Set a variable in the pipeline state. |
Placeholder Transformations:
YAML Type |
Python Class |
Description |
|---|---|---|
|
|
Replace placeholders with lists of values. |
|
|
Replace placeholders with wildcard patterns. |
|
|
Replace placeholders with query-language expressions. |
|
|
Replace placeholders with values from external files. |
|
|
Replace placeholders with values from HTTP endpoints. |
|
|
Replace placeholders with values from external command outputs. |
Failure Transformations:
YAML Type |
Python Class |
Description |
|---|---|---|
|
|
Raise an error for rules matching the condition. |
|
|
Raise an error for detection items matching the condition. |
|
|
Raise an error for rules with unmapped field names. |
Meta Transformations:
YAML Type |
Python Class |
Description |
|---|---|---|
|
|
Apply a nested pipeline to matching rules. |
Query Post-Processing Transformations¶
These operate on the query strings generated by the backend.
YAML Type |
Python Class |
Description |
|---|---|---|
|
|
Embed query in a prefix/suffix string. |
|
|
Apply a simple template to the query string. |
|
|
Apply a Jinja2 template with access to rule and pipeline state. |
|
|
Embed query into a JSON structure. |
|
|
Replace patterns in the query string using regex. |
|
|
Apply a nested set of post-processing transformations. |
Output Finalizers¶
Finalizers operate on the complete list of generated queries.
YAML Type |
Python Class |
Description |
|---|---|---|
|
|
Concatenate all queries with a separator. |
|
|
Output all queries as a JSON array. |
|
|
Output all queries as a YAML document. |
|
|
Apply a Jinja2 template to the full query list. |
|
|
Apply a nested set of finalizers. |
Complete YAML Example¶
Here is a comprehensive pipeline example:
name: comprehensive_example
priority: 20
allowed_backends:
- splunk
vars:
index_name: main
source_type: WinEventLog
transformations:
# Map field names for Windows process creation events
- id: proc_creation_fields
type: field_name_mapping
mapping:
CommandLine: process
Image: process_path
ParentImage: parent_process_path
User: user
rule_conditions:
- type: logsource
category: process_creation
product: windows
# Add index condition
- id: add_index
type: add_condition
conditions:
index: "{vars.index_name}"
rule_conditions:
- type: logsource
product: windows
# Change log source
- id: change_logsource
type: change_logsource
category: endpoint
rule_conditions:
- type: logsource
category: process_creation
product: windows
# Set pipeline state for query structure
- id: set_index_state
type: set_state
key: index
value: main
postprocessing:
# Prefix all queries with the data source
- id: add_source
type: embed
prefix: "index={pipeline.state[index]} "
suffix: ""
finalizers:
# Concatenate all queries
- type: concat
separator: "\\n\\n"
Writing Custom Transformations¶
To create a custom transformation, subclass the appropriate base class:
from dataclasses import dataclass
from sigma.processing.transformations.base import (
DetectionItemTransformation,
StringValueTransformation,
PreprocessingTransformation,
)
from sigma.rule import SigmaRule, SigmaDetectionItem
from sigma.types import SigmaString
@dataclass
class MyDetectionItemTransformation(DetectionItemTransformation):
"""My custom transformation that operates on detection items."""
my_param: str = "default"
def apply_detection_item(self, detection_item: SigmaDetectionItem) -> None:
# Modify the detection item in place
# Mark as applied for condition tracking:
self.processing_item_applied(detection_item)
@dataclass
class MyValueTransformation(StringValueTransformation):
"""Transform string values."""
def apply_string_value(self, field: str, val: SigmaString) -> SigmaString | None:
# Return transformed value, or None to keep original
return val + "_modified"
Pipeline Variables¶
Variables defined in the vars section can be referenced in transformations and
post-processing using the {vars.variable_name} syntax in template-enabled parameters.
The pipeline state (set by set_state transformations) is accessible in post-processing
and finalization via {pipeline.state[key]}.
Additionally, the backend automatically sets these pipeline variables:
backend: Name of the current backend.output_format: Current output format.backend_<option>: Backend options passed by the user.