Skip to content

About

pySigma SQlite backend

Resources

Security policy

Stars

9 stars

Watchers

1 watching

Forks

 
 

Repository files navigation

Tests Upstream coverage

pySigma SQLite Backend

Converts Sigma rules into SQLite queries. The package sigma.backends.sqlite provides sqliteBackend, registered as the sigma-cli target sqlite. It is the converter behind Zircolite, and its output runs on any SQLite table of flattened events.

Maintained by wagga.

Install

pip install pysigma-backend-sqlite

Requires Python 3.11+ and pySigma 2.1.0 or newer within the 2.x series. Correlation output needs SQLite 3.38 or newer. Use sigma-cli 4.0 or newer: sigma-cli 3.x requires pySigma 1.x, so alongside it pip installs backend 2.0.0, which has no keyword search.

Quick start

from sigma.collection import SigmaCollection
from sigma.backends.sqlite import sqliteBackend

rules = SigmaCollection.from_yaml(r"""
title: Whoami execution
status: test
logsource:
    category: process_creation
    product: windows
detection:
    selection:
        Image|endswith: '\whoami.exe'
    condition: selection
""")

print(sqliteBackend().convert(rules)[0])
# SELECT * FROM logs WHERE Image LIKE '%\\whoami.exe' ESCAPE '\'

Processing pipelines map a logsource to concrete fields. With sqliteBackend(sysmon_pipeline() + windows_logsource_pipeline()) the same rule becomes:

SELECT * FROM logs WHERE Channel='Microsoft-Windows-Sysmon/Operational' AND (EventID=1 AND Image LIKE '%\\whoami.exe' ESCAPE '\')

Install the pipelines with pip install pysigma-pipeline-sysmon pysigma-pipeline-windows; releases 3.0 and newer support pySigma 2. With sigma-cli:

sigma convert -t sqlite -f zircolite -p sysmon -p windows-logsources rules/

Output formats

Format Output
default One standalone SQL statement per detection condition or correlation
zircolite A JSON list with one entry per converted condition or correlation, including metadata and execution requirements. See output format

Options

Set options when constructing the backend or with -O name=value in sigma-cli. Changing attributes after construction is unsupported. Table and column names must be nonempty. pySigma also forwards options to the processing pipeline as backend_<name>. A name resembling a misspelled SQLite option produces a warning so you can check which setting took effect.

Option Default Purpose
table logs Table to query. A pipeline setState on table takes precedence
collate_nocase false Emit field='value' COLLATE NOCASE for tables without NOCASE columns
prefilter_bounds true Emit Channel/EventID allow-lists for Zircolite storage. Set to false for custom collations or event ID coercions
text_mode portable Use unicode for Unicode case folding in detections, grouping and distinct counts
presence_field none JSON array column listing the flattened fields present in each source event
sum_mode portable Use exact to preserve integer window totals beyond SQLite's signed 64-bit range
week_mode iso ISO week of year (1–53). Use legacy for SQLite's Monday-based week 0–53
modifier_mode sigma windash expands every dash position and re|startswith/re|endswith stay within the field. Use pysigma for pySigma's own expansion, as in 2.1
max_flat_operands 100 Longer AND/OR chains are nested to stay under SQLite's expression depth limit
cidr_mode expanded Use portable prefix matching or opt into exact network membership with exact
fulltext_field none Column of searchable event text; enables keyword search
timestamp_field timestamp Event time column used by correlations
timestamp_format iso iso, unix, unix_ms or unix_us
timestamp_seconds_expression ISO parser SQL turning {field} into Unix seconds; overrides timestamp_format
event_id_field rowid Non-NULL, per-table unique event identifier for correlation evidence
observation_end latest event Unix seconds up to which absence windows can expire

The optional Unicode, presence and exact-sum contracts require a consumer that understands schema 5. The table below describes the portable settings. Sigma 2.1 feature support and parser compatibility

Generated SQL

Modifier SQL
contains, startswith, endswith LIKE '%value%' ESCAPE '\' and its prefix/suffix forms
all AND of the values
re REGEXP (needs a registered function, see Limitations)
cidr Expanded mode uses = or LIKE. Exact mode uses equality for IPv4 /32 and a registered membership function for other networks
cased GLOB; literal *, ?, [ become [*], [?], [[]
fieldref field1=field2, or LIKE with the second field's % and _ escaped
exists IS NOT NULL / IS NULL
gt, gte, lt, lte Compare finite numbers and numeric text numerically
neq Negated equality
minute, hour, day, week, month, year Calendar part of an ISO-8601 timestamp
base64, base64offset, wide, utf16be, utf16, windash Expanded into text predicates. Follow byte encodings with a Base64 modifier

Generated SQL handles the following cases.

  • Negation. Sigma reads a condition on a missing field as false; SQLite reads it as NULL, and NOT NULL drops the row. Every NOT therefore wraps its operand: EventID=1 AND (NOT COALESCE((CommandLine LIKE '%foo%' ESCAPE '\'), 0)).
  • Case. LIKE ignores case; = does only on NOCASE columns (Zircolite's layout), otherwise set collate_nocase.
  • Booleans. Flag: true matches text or integer storage: (Flag='true' OR Flag=1).
  • Field names. SQLite keywords and names with characters other than letters, digits and _ are quoted with backticks. field: null becomes field IS NULL.
  • Numeric comparisons. gt, gte, lt and lte validate and convert the stored value before comparison. A plain index on that column cannot supply a range lookup for this predicate, even on an INTEGER column. Detections read +11, 11. and 011 as eleven. Numeric correlation statistics accept text in JSON number syntax and skip all three spellings.

Correlations

All Sigma correlation types run in SQLite, including chains, aliases and extended temporal conditions. See correlations for their semantics and the migration guide for upgrading from backend 1.x.

CIDR matching

The default cidr_mode="expanded" uses portable SQL. It checks the spelling of an address and can use SQLite prefix indexes. Exact mode parses the event value and tests membership in the network. For example, SQL generated for these inputs has the following behavior.

CIDR Event value Expanded Exact
10.0.0.0/8 10.1.2.300 match no match
10.0.0.0/8 10.1.2.3 followed by a space match no match
2001:db8::/32 2001:0db8:0:0::1 no match match
10.0.0.0/8 ::ffff:10.0.0.5 (IPv4-mapped IPv6) no match no match
0.0.0.0/0 ::ffff:10.0.0.5 match no match
::/0 10.0.0.5 match no match
Either /0 unknown match no match

Both expanded /0 networks compile to LIKE '%' and match every non-NULL text value. Exact mode parses the address and respects its IP family.

from sigma.backends.sqlite.runtime import register_cidr

register_cidr(connection)
query = sqliteBackend(cidr_mode="exact").convert(rules)[0]
rows = connection.execute(query).fetchall()

For sigma-cli, pass -O cidr_mode=exact and register the function on the connection that executes the SQL. IPv4 ranges with at least one fixed octet can use a prefix or equality index. When an IPv6 network fixes a nonzero first group of 16 bits, SQLite checks that group before parsing the address, allowing any valid letter case or leading zeros. This check can use a compatible index. Other IPv6 networks skip values without a colon before parsing. Exact IPv4 /32 uses a built-in SQLite comparison. Zircolite 4.7.1 can run these entries without registering a function. Entries that use the CIDR function require a schema-4 consumer. Original CIDR report

Keyword search

Keyword (fieldless) rules need fulltext_field, a column holding each event's searchable text. See keyword search.

Limitations

  • REGEXP. SQLite has no built-in REGEXP. Register one that is false for NULL: connection.create_function("regexp", 2, lambda p, v: v is not None and re.search(p, str(v)) is not None). pySigma validates |re with RE2 by default, rejecting lookarounds and backreferences. For trusted rules, pass policy=TrustedPolicy (from sigma.policy.profiles) to SigmaCollection.from_yaml(); the patterns must then suit Python's re.
  • Case folding. Stock BINARY columns compare literal text case-sensitively. SQLite NOCASE columns or collate_nocase=True cover ASCII. Set text_mode="unicode" and register the runtime helpers for Unicode equality, wildcards, field comparisons and keywords.
  • Timestamp parts use strftime() and need ISO-8601 text; a numeric epoch is read as a Julian day. ISO week 53 occurs in some years. Only week_mode="legacy" can produce week 0.
  • Field existence. With presence_field, exists includes an explicitly null field. The default uses SQL NULL as a proxy for absence.
  • Multi-field value_count counts distinct complete tuples in each window. A NULL component excludes that tuple from the count.
  • Unknown columns. A query naming a column the table lacks fails to prepare. Add missing columns as NULL first; see output format.

Development

poetry install
poetry run pytest
SQLITE_MIN_BINARY=/path/to/sqlite-3.38 poetry run pytest tests/test_sqlite_minimum.py

tools/audit_corpus.py converts and prepares a SigmaHQ checkout per profile (see coverage). tools/benchmark_correlations.py times sparse and dense correlation windows. tools/benchmark_cidr.py compares expanded and exact event queries and materialized correlations over indexed and unindexed IPv4 and IPv6 data. It checks event and evidence identities and records SQLite query plans.

poetry run python tools/benchmark_cidr.py --size 100000 --iterations 5 --output cidr.json

About

pySigma SQlite backend

Resources

Security policy

Stars

9 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages