Skip to main content

pySigma DuckDB backend for local Sigma rule validation against JSON logs

Project description

pySigma DuckDB Backend

Tests Status

This is a pySigma backend that generates DuckDB SQL queries from Sigma rules. It's designed for local validation of Sigma rules against JSON log files, making it ideal for CI/CD pipelines and regression testing.

Features

  • Convert Sigma rules to DuckDB SQL queries
  • Validate rules against local JSON log files
  • Support for JSON arrays, single objects, and NDJSON formats
  • Built-in LogIndex class for efficient log loading and querying
  • ValidationResult with match counts and matched log details

Installation

pip install pySigma-backend-duckdb

Usage

Basic Query Generation

from sigma.rule import SigmaRule
from sigma.collection import SigmaCollection
from sigma.backends.duckdb import DuckDBBackend

rule = SigmaRule.from_yaml("""
    title: Suspicious PowerShell Execution
    logsource:
        category: process_creation
        product: windows
    detection:
        selection:
            CommandLine|contains: powershell
        condition: selection
""")

backend = DuckDBBackend()
queries = backend.convert(SigmaCollection([rule]))
print(queries[0])
# SELECT * FROM logs WHERE CommandLine ILIKE '%powershell%'

Validating Rules Against Local Logs

from sigma.backends.duckdb import DuckDBBackend, LogIndex

# Load logs from JSON files
index = LogIndex()
index.load_json_file("logs.json")
# Or load from a directory
index.load_directory("logs/")

# Validate a rule
backend = DuckDBBackend()
result = backend.validate_rule(rule_yaml, index)

print(f"Rule: {result.rule_title}")
print(f"Matches: {result.match_count}/{result.total_logs}")
print(f"Success: {result.success}")

for log in result.matched_logs:
    print(f"  - {log.get('CommandLine', 'N/A')}")

Directory Validation for CI

from sigma.backends.duckdb import validate_rules_directory

# Validate all rules against all logs
results = validate_rules_directory(
    rules_dir="rules/",
    logs_dir="test_logs/",
)

for result in results:
    status = "PASS" if result.has_matches else "FAIL"
    print(f"{status}: {result.rule_title} ({result.match_count} matches)")

Log Format

The backend expects logs in JSON format. By default, it uses Sysmon field names (e.g., CommandLine, Image, ParentImage). You can use custom pipelines for different schemas.

Splunk Sysmon format (default):

{
    "CommandLine": "powershell.exe -e ...",
    "Image": "C:\\Windows\\System32\\powershell.exe",
    "ParentImage": "C:\\Windows\\System32\\cmd.exe"
}

Elastic ECS format (with elastic_ecs pipeline):

{
    "process": {
        "command_line": "powershell.exe -e ...",
        "executable": "C:\\Windows\\System32\\powershell.exe"
    }
}

Pipelines

The backend includes pipelines for common log formats:

from sigma.backends.duckdb import DuckDBBackend
from sigma.backends.duckdb.pipelines import splunk_sysmon, elastic_ecs

# For Splunk with Sysmon TA (default)
backend = DuckDBBackend(processing_pipeline=splunk_sysmon())

# For Elasticsearch with ECS
backend = DuckDBBackend(processing_pipeline=elastic_ecs())

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pysigma_backend_duckdb-0.1.3.tar.gz (48.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pysigma_backend_duckdb-0.1.3-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

Details for the file pysigma_backend_duckdb-0.1.3.tar.gz.

File metadata

  • Download URL: pysigma_backend_duckdb-0.1.3.tar.gz
  • Upload date:
  • Size: 48.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.9 {"installer":{"name":"uv","version":"0.9.9"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for pysigma_backend_duckdb-0.1.3.tar.gz
Algorithm Hash digest
SHA256 b68d36774ac0d613da8df1cb57fb522905743d81b9c232195fe1c05b4e83b236
MD5 6abe2885277b66ed1158b71a79bee0de
BLAKE2b-256 d873215d212258ae11bd7b49afa08ae417ada054d1a63dfcc5b2820d60a5094b

See more details on using hashes here.

File details

Details for the file pysigma_backend_duckdb-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: pysigma_backend_duckdb-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 5.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.9 {"installer":{"name":"uv","version":"0.9.9"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for pysigma_backend_duckdb-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 40c155c17967b04407d58b7b351be884bb9f9637d16b53c6838fbc3af75f65bf
MD5 9bff0de52a084348ca2294b149250fe1
BLAKE2b-256 d6c2dcb54aa4411cb255b1723e72bc88e92ecb406b013f5d527077ef51f7bb5e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page