Getting Started

Requirements

pySigma requires Python 3.11 or later.

Installation

Install pySigma using your preferred Python package manager:

pip install pysigma

Or with Poetry:

poetry add pysigma

Or with pipenv:

pipenv install pysigma

Quick Example

Here is a minimal example that converts a Sigma rule into a query:

from sigma.rule import SigmaRule
from sigma.backends.test import TextQueryTestBackend
from sigma.collection import SigmaCollection

# Parse a Sigma rule from YAML
rule = SigmaRule.from_yaml("""
    title: Test Rule
    status: test
    logsource:
        category: process_creation
        product: windows
    detection:
        selection:
            CommandLine|contains: mimikatz
        condition: selection
""")

# Create a collection and convert with a backend
collection = SigmaCollection(rules=[rule])
backend = TextQueryTestBackend()
result = backend.convert(collection)
print(result)

Note

The TextQueryTestBackend is a test backend included in pySigma for testing purposes. For real-world use, install a backend for your target system (e.g., pySigma-backend-splunk).

Using Processing Pipelines

Processing pipelines transform rules before conversion. They are typically used to map generic Sigma field names to the specific field names used in your environment:

from sigma.processing.pipeline import ProcessingPipeline
from sigma.processing.transformations import FieldMappingTransformation

# Create a pipeline that maps field names
pipeline = ProcessingPipeline(
    name="my_pipeline",
    priority=20,
    items=[
        ProcessingItem(
            identifier="field_mapping",
            transformation=FieldMappingTransformation({
                "CommandLine": "process.command_line",
                "Image": "process.executable",
            }),
        )
    ]
)

# Pass pipeline to backend
backend = TextQueryTestBackend(processing_pipeline=pipeline)
result = backend.convert(collection)

Loading Rules from Files

You can load rule collections from directories or individual files:

from sigma.collection import SigmaCollection
from pathlib import Path

# Load from a directory of YAML files
collection = SigmaCollection.load_ruleset([Path("./rules/")])

# Load from a single YAML string containing multiple documents
collection = SigmaCollection.from_yaml(open("rules.yml").read())

Validating Rules

pySigma includes a validation framework to check rules for correctness:

from sigma.validation import SigmaValidator
from sigma.validators.core.metadata import IdentifierExistenceValidator

validator = SigmaValidator(validators=[IdentifierExistenceValidator])
issues = validator.validate_rule(rule)
for issue in issues:
    print(f"{issue.severity.name}: {issue}")

Next Steps