pySigma
pySigma is a Python library for processing and converting Sigma rules, a generic and open signature format that allows security analysts to describe relevant log events in a structured way. It serves as the core engine for Sigma rule management and transformation into various SIEM or EDR query languages. The current version is 1.3.2, with minor releases and bug fixes occurring frequently.
Common errors
-
ModuleNotFoundError: No module named 'pysigma.collection'
cause Attempting to import `SigmaCollection` from the old `pysigma.collection` path, which was changed in v1.0.0.fixThe main collection class is now `PySigmaCollection` and should be imported directly from the top-level `pysigma` package: `from pysigma import PySigmaCollection`. -
AttributeError: 'SigmaRule' object has no attribute 'to_s'
cause Using a deprecated method `to_s` to get a string representation of a rule, which was renamed in v1.0.0.fixThe method `to_s` has been renamed to `to_plain_text` as part of the v1.0.0 API changes. Use `rule.to_plain_text()` instead. -
pysigma.exceptions.SigmaError: Failed to parse Sigma rule: unexpected indent
cause The YAML content of a Sigma rule file is syntactically incorrect, often due to improper indentation or other YAML parsing issues.fixCarefully review the Sigma rule file for YAML syntax errors. Use a YAML linter or the `sigmac validate` command (from `pysigma-cli`) to identify and fix issues. Ensure fields like `logsource` and `detection` are correctly structured. -
ValueError: Template variable file not found: /path/to/missing_vars.yml
cause A processing pipeline or backend configuration specifies a template variables file that does not exist at the given path.fixVerify that the template variables file (e.g., `vars.yml`) exists at the specified absolute path or a path relative to the pipeline configuration file. Correct the path or ensure the file is present.
Warnings
- breaking pySigma v1.0.0 introduced significant breaking changes, including a redesigned API, new package structure, and changes to pipeline configuration. Key changes include `SigmaCollection` being replaced by `PySigmaCollection` for loading, movement of `SigmaRule` class, and a new structure for `Rule` objects.
- breaking A security vulnerability was identified in v1.3.0 related to custom template variables. Untrusted processing pipelines utilizing the template vars feature could lead to unintended arbitrary code execution. Users should be aware that pipelines can imply execution of arbitrary code.
- gotcha Prior to v1.3.2, MITRE data loading in tag validators was not deferred. This could cause timeouts or errors when pySigma was used in offline environments or without proper internet access for MITRE ATT&CK data validation.
- gotcha As of v1.0.1, pySigma uses PyPI dependency information for plugin compatibility checks. Custom or locally developed plugins might require explicit `pysigma_compatibility` entries in their `setup.py` or equivalent to ensure they are recognized as compatible.
Install
-
pip install pysigma -
pip install pysigma[splunk,elasticsearch]
Imports
- PySigmaCollection
from pysigma.collection import SigmaCollection
from pysigma import PySigmaCollection
- SigmaDetectionsBackend
from pysigma.backends.splunk import SplunkBackend # if plugin not installed
from pysigma.backends.sigma import SigmaDetectionsBackend
- SigmaRule
from sigma.rule import SigmaRule
from pysigma.parser.rule import SigmaRule
Quickstart
import os
from pysigma import PySigmaCollection
from pysigma.backends.sigma import SigmaDetectionsBackend
# Create a dummy Sigma rule file for demonstration
rule_content = """
title: Detect PowerShell Encoded Command
id: 03f57279-7928-4e89-a5e2-6320573e6a4b
status: stable
description: Detects PowerShell usage with encoded commands, often used in malicious activity.
logsource:
category: process_creation
product: windows
detection:
selection:
Image|endswith:
- '\\powershell.exe'
- '\\pwsh.exe'
CommandLine|contains:
- '-EncodedCommand'
- '-eNcoDedCOmmaNd'
condition: selection
fields:
- CommandLine
- ParentCommandLine
- Image
tags:
- attack.execution
- attack.t1059.001
"""
# Save the rule to a temporary directory
if not os.path.exists('sigma_rules'):
os.makedirs('sigma_rules')
with open('sigma_rules/powershell_encoded_command.yml', 'w') as f:
f.write(rule_content)
# 1. Load Sigma rules from a directory
collection = PySigmaCollection.from_directory('sigma_rules')
print(f"Loaded {len(collection.rules)} Sigma rule(s).")
# 2. Instantiate a backend (e.g., generic Sigma detection backend)
# For Splunk, use: from pysigma.backends.splunk import SplunkBackend; backend = SplunkBackend()
backend = SigmaDetectionsBackend()
# 3. Process the collection using the backend
# This generates a list of Backend_Rule objects
detection_rules = backend.convert(collection)
# 4. Print the converted query for each rule
for rule in detection_rules:
print(f"\nRule ID: {rule.id}")
print(f"Query: {rule.text}")
# Clean up the dummy rule file and directory
os.remove('sigma_rules/powershell_encoded_command.yml')
os.rmdir('sigma_rules')