pySigma-backend-sqlite 2.0.0__tar.gz → 2.1.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,169 @@
1
+ Metadata-Version: 2.4
2
+ Name: pySigma-backend-sqlite
3
+ Version: 2.1.1
4
+ Summary: pySigma sqlite backend
5
+ License-Expression: LGPL-3.0-only
6
+ License-File: LICENSE
7
+ Author: wagga
8
+ Requires-Python: >=3.11,<4.0
9
+ Classifier: Programming Language :: Python :: 3
10
+ Classifier: Programming Language :: Python :: 3.11
11
+ Classifier: Programming Language :: Python :: 3.12
12
+ Classifier: Programming Language :: Python :: 3.13
13
+ Classifier: Programming Language :: Python :: 3.14
14
+ Classifier: Programming Language :: Python :: 3.15
15
+ Requires-Dist: pysigma (>=2.0.1,<3.0)
16
+ Project-URL: Repository, https://github.com/SigmaHQ/pySigma-backend-sqlite
17
+ Description-Content-Type: text/markdown
18
+
19
+ ![Tests](https://github.com/wagga40/pySigma-backend-sqlite/actions/workflows/test.yml/badge.svg)
20
+ ![Coverage Badge](https://img.shields.io/endpoint?url=https://gist.githubusercontent.com/wagga40/2ec45ded898fa11f2c42bcb9d2b163cf/raw/test.json)
21
+
22
+ # pySigma SQLite Backend
23
+
24
+ Converts [Sigma](https://github.com/SigmaHQ/sigma) rules into SQLite queries. The
25
+ package `sigma.backends.sqlite` provides `sqliteBackend`, registered as the
26
+ sigma-cli target `sqlite`. It is the converter behind
27
+ [Zircolite](https://github.com/wagga40/Zircolite), and its output runs on any
28
+ SQLite table of flattened events.
29
+
30
+ Maintained by [wagga](https://github.com/wagga40/).
31
+
32
+ ## Install
33
+
34
+ ```shell
35
+ pip install pysigma-backend-sqlite
36
+ ```
37
+
38
+ Requires Python 3.11+ and [pySigma](https://github.com/SigmaHQ/pySigma) 2.0.1 or
39
+ newer. Correlation output needs SQLite 3.38 or newer. sigma-cli 3.1 still requires
40
+ pySigma 1.x, so alongside it pip installs backend 2.0.0, which has no keyword search.
41
+
42
+ ## Quick start
43
+
44
+ ```python
45
+ from sigma.collection import SigmaCollection
46
+ from sigma.backends.sqlite import sqliteBackend
47
+
48
+ rules = SigmaCollection.from_yaml(r"""
49
+ title: Whoami execution
50
+ status: test
51
+ logsource:
52
+ category: process_creation
53
+ product: windows
54
+ detection:
55
+ selection:
56
+ Image|endswith: '\whoami.exe'
57
+ condition: selection
58
+ """)
59
+
60
+ print(sqliteBackend().convert(rules)[0])
61
+ # SELECT * FROM logs WHERE Image LIKE '%\\whoami.exe' ESCAPE '\'
62
+ ```
63
+
64
+ Processing pipelines map a logsource to concrete fields. With
65
+ `sqliteBackend(sysmon_pipeline() + windows_logsource_pipeline())` the same rule becomes:
66
+
67
+ ```sql
68
+ SELECT * FROM logs WHERE Channel='Microsoft-Windows-Sysmon/Operational' AND (EventID=1 AND Image LIKE '%\\whoami.exe' ESCAPE '\')
69
+ ```
70
+
71
+ The sysmon and windows pipelines still declare `pysigma<2`; install them with
72
+ `pip install --no-deps pysigma-pipeline-sysmon pysigma-pipeline-windows`. With sigma-cli:
73
+
74
+ ```shell
75
+ sigma convert -t sqlite -f zircolite -p sysmon -p windows-logsources rules/
76
+ ```
77
+
78
+ ## Output formats
79
+
80
+ | Format | Output |
81
+ |---|---|
82
+ | `default` | One `SELECT * FROM logs WHERE ...` statement per rule |
83
+ | `zircolite` | A JSON list with one entry per rule: Sigma metadata, the SQL, and what a consumer needs to run it. See [output format](https://github.com/SigmaHQ/pySigma-backend-sqlite/blob/main/docs/output-format.md) |
84
+
85
+ ## Options
86
+
87
+ Pass options as constructor keywords, or from sigma-cli with `-O name=value`. Set
88
+ them at construction; changing attributes afterwards is unsupported.
89
+
90
+ | Option | Default | Purpose |
91
+ |---|---|---|
92
+ | `table` | `logs` | Table to query. A pipeline `setState` on `table` takes precedence |
93
+ | `collate_nocase` | `false` | Emit `field='value' COLLATE NOCASE` for tables without `NOCASE` columns |
94
+ | `max_flat_operands` | `100` | Longer AND/OR chains are nested to stay under SQLite's expression depth limit |
95
+ | `fulltext_field` | none | Column of searchable event text; enables [keyword search](https://github.com/SigmaHQ/pySigma-backend-sqlite/blob/main/docs/keyword-search.md) |
96
+ | `timestamp_field` | `timestamp` | Event time column used by correlations |
97
+ | `timestamp_format` | `iso` | `iso`, `unix`, `unix_ms` or `unix_us` |
98
+ | `timestamp_seconds_expression` | ISO parser | SQL turning `{field}` into Unix seconds; overrides `timestamp_format` |
99
+ | `event_id_field` | `rowid` | Non-NULL, per-table unique event identifier for correlation evidence |
100
+ | `observation_end` | latest event | Unix seconds up to which absence windows can expire |
101
+
102
+ ## Generated SQL
103
+
104
+ | Modifier | SQL |
105
+ |---|---|
106
+ | `contains`, `startswith`, `endswith` | `LIKE '%value%' ESCAPE '\'` and its prefix/suffix forms |
107
+ | `all` | `AND` of the values |
108
+ | `re` | `REGEXP` (needs a registered function, see [Limitations](#limitations)) |
109
+ | `cidr` | `=` or `LIKE` prefixes of the expanded network |
110
+ | `cased` | `GLOB`; literal `*`, `?`, `[` become `[*]`, `[?]`, `[[]` |
111
+ | `fieldref` | `field1=field2`, or `LIKE` with the second field's `%` and `_` escaped |
112
+ | `exists` | `IS NOT NULL` / `IS NULL` |
113
+ | `gt`, `gte`, `lt`, `lte`, `neq` | `>`, `>=`, `<`, `<=`, negated equality |
114
+ | `minute`, `hour`, `day`, `week`, `month`, `year` | `strftime()` part |
115
+ | `base64`, `base64offset`, `wide`, `utf16*`, `windash` | Expanded by pySigma into ordinary matches |
116
+
117
+ The SQL keeps Sigma's meaning without help from the caller:
118
+
119
+ - **Negation.** Sigma reads a condition on a missing field as false; SQLite reads
120
+ it as `NULL`, and `NOT NULL` drops the row. Every `NOT` therefore wraps its
121
+ operand: `EventID=1 AND (NOT COALESCE((CommandLine LIKE '%foo%' ESCAPE '\'), 0))`.
122
+ - **Case.** `LIKE` ignores case; `=` does only on `NOCASE` columns (Zircolite's
123
+ layout), otherwise set `collate_nocase`.
124
+ - **Booleans.** `Flag: true` matches text or integer storage: `(Flag='true' OR Flag=1)`.
125
+ - **Field names.** SQLite keywords and names with characters other than letters,
126
+ digits and `_` are quoted with backticks. `field: null` becomes `field IS NULL`.
127
+
128
+ ## Correlations
129
+
130
+ All Sigma correlation types run in SQLite, including chains, aliases and extended
131
+ temporal conditions. See [correlations](https://github.com/SigmaHQ/pySigma-backend-sqlite/blob/main/docs/correlations.md)
132
+ for their semantics and the [migration guide](https://github.com/SigmaHQ/pySigma-backend-sqlite/blob/main/docs/migration-v2.md)
133
+ for upgrading from backend 1.x.
134
+
135
+ ## Keyword search
136
+
137
+ Keyword (fieldless) rules need `fulltext_field`, a column holding each event's
138
+ searchable text. See [keyword search](https://github.com/SigmaHQ/pySigma-backend-sqlite/blob/main/docs/keyword-search.md).
139
+
140
+ ## Limitations
141
+
142
+ - **REGEXP.** SQLite has no built-in `REGEXP`. Register one that is false for `NULL`:
143
+ `connection.create_function("regexp", 2, lambda p, v: v is not None and re.search(p, str(v)) is not None)`.
144
+ pySigma validates `|re` with RE2 by default, rejecting lookarounds and
145
+ backreferences. For trusted rules, pass `policy=TrustedPolicy` (from
146
+ `sigma.policy.profiles`) to `SigmaCollection.from_yaml()`; the patterns must then
147
+ suit Python's `re`.
148
+ - **CIDR.** SQLite has no network type. Prefix patterns match IPv6 addresses only
149
+ in the spellings pySigma expands to.
150
+ - **Case folding** (`NOCASE`, `LIKE`) is ASCII only.
151
+ - **Timestamp parts** use `strftime()` and need ISO-8601 text; a numeric epoch is
152
+ read as a Julian day. `%W` can return week 0.
153
+ - **Field existence.** SQL `NULL` cannot tell a missing field from an explicit null.
154
+ - **Multi-field `value_count`** (a list in `condition.field`) is rejected.
155
+ - **Unknown columns.** A query naming a column the table lacks fails to prepare.
156
+ Add missing columns as `NULL` first; see [output format](https://github.com/SigmaHQ/pySigma-backend-sqlite/blob/main/docs/output-format.md).
157
+
158
+ ## Development
159
+
160
+ ```shell
161
+ poetry install
162
+ poetry run pytest
163
+ SQLITE_MIN_BINARY=/path/to/sqlite-3.38 poetry run pytest tests/test_sqlite_minimum.py
164
+ ```
165
+
166
+ `tools/audit_corpus.py` converts and prepares a SigmaHQ checkout per profile (see
167
+ [coverage](https://github.com/SigmaHQ/pySigma-backend-sqlite/blob/main/docs/keyword-search.md#coverage)).
168
+ `tools/benchmark_correlations.py` times sparse and dense correlation windows.
169
+
@@ -0,0 +1,150 @@
1
+ ![Tests](https://github.com/wagga40/pySigma-backend-sqlite/actions/workflows/test.yml/badge.svg)
2
+ ![Coverage Badge](https://img.shields.io/endpoint?url=https://gist.githubusercontent.com/wagga40/2ec45ded898fa11f2c42bcb9d2b163cf/raw/test.json)
3
+
4
+ # pySigma SQLite Backend
5
+
6
+ Converts [Sigma](https://github.com/SigmaHQ/sigma) rules into SQLite queries. The
7
+ package `sigma.backends.sqlite` provides `sqliteBackend`, registered as the
8
+ sigma-cli target `sqlite`. It is the converter behind
9
+ [Zircolite](https://github.com/wagga40/Zircolite), and its output runs on any
10
+ SQLite table of flattened events.
11
+
12
+ Maintained by [wagga](https://github.com/wagga40/).
13
+
14
+ ## Install
15
+
16
+ ```shell
17
+ pip install pysigma-backend-sqlite
18
+ ```
19
+
20
+ Requires Python 3.11+ and [pySigma](https://github.com/SigmaHQ/pySigma) 2.0.1 or
21
+ newer. Correlation output needs SQLite 3.38 or newer. sigma-cli 3.1 still requires
22
+ pySigma 1.x, so alongside it pip installs backend 2.0.0, which has no keyword search.
23
+
24
+ ## Quick start
25
+
26
+ ```python
27
+ from sigma.collection import SigmaCollection
28
+ from sigma.backends.sqlite import sqliteBackend
29
+
30
+ rules = SigmaCollection.from_yaml(r"""
31
+ title: Whoami execution
32
+ status: test
33
+ logsource:
34
+ category: process_creation
35
+ product: windows
36
+ detection:
37
+ selection:
38
+ Image|endswith: '\whoami.exe'
39
+ condition: selection
40
+ """)
41
+
42
+ print(sqliteBackend().convert(rules)[0])
43
+ # SELECT * FROM logs WHERE Image LIKE '%\\whoami.exe' ESCAPE '\'
44
+ ```
45
+
46
+ Processing pipelines map a logsource to concrete fields. With
47
+ `sqliteBackend(sysmon_pipeline() + windows_logsource_pipeline())` the same rule becomes:
48
+
49
+ ```sql
50
+ SELECT * FROM logs WHERE Channel='Microsoft-Windows-Sysmon/Operational' AND (EventID=1 AND Image LIKE '%\\whoami.exe' ESCAPE '\')
51
+ ```
52
+
53
+ The sysmon and windows pipelines still declare `pysigma<2`; install them with
54
+ `pip install --no-deps pysigma-pipeline-sysmon pysigma-pipeline-windows`. With sigma-cli:
55
+
56
+ ```shell
57
+ sigma convert -t sqlite -f zircolite -p sysmon -p windows-logsources rules/
58
+ ```
59
+
60
+ ## Output formats
61
+
62
+ | Format | Output |
63
+ |---|---|
64
+ | `default` | One `SELECT * FROM logs WHERE ...` statement per rule |
65
+ | `zircolite` | A JSON list with one entry per rule: Sigma metadata, the SQL, and what a consumer needs to run it. See [output format](https://github.com/SigmaHQ/pySigma-backend-sqlite/blob/main/docs/output-format.md) |
66
+
67
+ ## Options
68
+
69
+ Pass options as constructor keywords, or from sigma-cli with `-O name=value`. Set
70
+ them at construction; changing attributes afterwards is unsupported.
71
+
72
+ | Option | Default | Purpose |
73
+ |---|---|---|
74
+ | `table` | `logs` | Table to query. A pipeline `setState` on `table` takes precedence |
75
+ | `collate_nocase` | `false` | Emit `field='value' COLLATE NOCASE` for tables without `NOCASE` columns |
76
+ | `max_flat_operands` | `100` | Longer AND/OR chains are nested to stay under SQLite's expression depth limit |
77
+ | `fulltext_field` | none | Column of searchable event text; enables [keyword search](https://github.com/SigmaHQ/pySigma-backend-sqlite/blob/main/docs/keyword-search.md) |
78
+ | `timestamp_field` | `timestamp` | Event time column used by correlations |
79
+ | `timestamp_format` | `iso` | `iso`, `unix`, `unix_ms` or `unix_us` |
80
+ | `timestamp_seconds_expression` | ISO parser | SQL turning `{field}` into Unix seconds; overrides `timestamp_format` |
81
+ | `event_id_field` | `rowid` | Non-NULL, per-table unique event identifier for correlation evidence |
82
+ | `observation_end` | latest event | Unix seconds up to which absence windows can expire |
83
+
84
+ ## Generated SQL
85
+
86
+ | Modifier | SQL |
87
+ |---|---|
88
+ | `contains`, `startswith`, `endswith` | `LIKE '%value%' ESCAPE '\'` and its prefix/suffix forms |
89
+ | `all` | `AND` of the values |
90
+ | `re` | `REGEXP` (needs a registered function, see [Limitations](#limitations)) |
91
+ | `cidr` | `=` or `LIKE` prefixes of the expanded network |
92
+ | `cased` | `GLOB`; literal `*`, `?`, `[` become `[*]`, `[?]`, `[[]` |
93
+ | `fieldref` | `field1=field2`, or `LIKE` with the second field's `%` and `_` escaped |
94
+ | `exists` | `IS NOT NULL` / `IS NULL` |
95
+ | `gt`, `gte`, `lt`, `lte`, `neq` | `>`, `>=`, `<`, `<=`, negated equality |
96
+ | `minute`, `hour`, `day`, `week`, `month`, `year` | `strftime()` part |
97
+ | `base64`, `base64offset`, `wide`, `utf16*`, `windash` | Expanded by pySigma into ordinary matches |
98
+
99
+ The SQL keeps Sigma's meaning without help from the caller:
100
+
101
+ - **Negation.** Sigma reads a condition on a missing field as false; SQLite reads
102
+ it as `NULL`, and `NOT NULL` drops the row. Every `NOT` therefore wraps its
103
+ operand: `EventID=1 AND (NOT COALESCE((CommandLine LIKE '%foo%' ESCAPE '\'), 0))`.
104
+ - **Case.** `LIKE` ignores case; `=` does only on `NOCASE` columns (Zircolite's
105
+ layout), otherwise set `collate_nocase`.
106
+ - **Booleans.** `Flag: true` matches text or integer storage: `(Flag='true' OR Flag=1)`.
107
+ - **Field names.** SQLite keywords and names with characters other than letters,
108
+ digits and `_` are quoted with backticks. `field: null` becomes `field IS NULL`.
109
+
110
+ ## Correlations
111
+
112
+ All Sigma correlation types run in SQLite, including chains, aliases and extended
113
+ temporal conditions. See [correlations](https://github.com/SigmaHQ/pySigma-backend-sqlite/blob/main/docs/correlations.md)
114
+ for their semantics and the [migration guide](https://github.com/SigmaHQ/pySigma-backend-sqlite/blob/main/docs/migration-v2.md)
115
+ for upgrading from backend 1.x.
116
+
117
+ ## Keyword search
118
+
119
+ Keyword (fieldless) rules need `fulltext_field`, a column holding each event's
120
+ searchable text. See [keyword search](https://github.com/SigmaHQ/pySigma-backend-sqlite/blob/main/docs/keyword-search.md).
121
+
122
+ ## Limitations
123
+
124
+ - **REGEXP.** SQLite has no built-in `REGEXP`. Register one that is false for `NULL`:
125
+ `connection.create_function("regexp", 2, lambda p, v: v is not None and re.search(p, str(v)) is not None)`.
126
+ pySigma validates `|re` with RE2 by default, rejecting lookarounds and
127
+ backreferences. For trusted rules, pass `policy=TrustedPolicy` (from
128
+ `sigma.policy.profiles`) to `SigmaCollection.from_yaml()`; the patterns must then
129
+ suit Python's `re`.
130
+ - **CIDR.** SQLite has no network type. Prefix patterns match IPv6 addresses only
131
+ in the spellings pySigma expands to.
132
+ - **Case folding** (`NOCASE`, `LIKE`) is ASCII only.
133
+ - **Timestamp parts** use `strftime()` and need ISO-8601 text; a numeric epoch is
134
+ read as a Julian day. `%W` can return week 0.
135
+ - **Field existence.** SQL `NULL` cannot tell a missing field from an explicit null.
136
+ - **Multi-field `value_count`** (a list in `condition.field`) is rejected.
137
+ - **Unknown columns.** A query naming a column the table lacks fails to prepare.
138
+ Add missing columns as `NULL` first; see [output format](https://github.com/SigmaHQ/pySigma-backend-sqlite/blob/main/docs/output-format.md).
139
+
140
+ ## Development
141
+
142
+ ```shell
143
+ poetry install
144
+ poetry run pytest
145
+ SQLITE_MIN_BINARY=/path/to/sqlite-3.38 poetry run pytest tests/test_sqlite_minimum.py
146
+ ```
147
+
148
+ `tools/audit_corpus.py` converts and prepares a SigmaHQ checkout per profile (see
149
+ [coverage](https://github.com/SigmaHQ/pySigma-backend-sqlite/blob/main/docs/keyword-search.md#coverage)).
150
+ `tools/benchmark_correlations.py` times sparse and dense correlation windows.
@@ -1,8 +1,8 @@
1
1
  [project]
2
2
  name = "pySigma-backend-sqlite"
3
- version = "2.0.0"
4
- requires-python = ">=3.10,<4.0"
5
- dependencies = ["pysigma>=1.5.1,<2.0"]
3
+ version = "2.1.1"
4
+ requires-python = ">=3.11,<4.0"
5
+ dependencies = ["pysigma>=2.0.1,<3.0"]
6
6
  description = "pySigma sqlite backend"
7
7
  readme = "README.md"
8
8
  authors = [
@@ -16,25 +16,21 @@ packages = [
16
16
  ]
17
17
 
18
18
  [tool.poetry.group.dev.dependencies]
19
- black = "^24.1"
20
19
  pytest = "^8.0"
21
- pytest-cov = "^4.1"
20
+ pytest-cov = "^7.1"
22
21
  coverage = "^7.4"
22
+ ruff = "^0.16.10"
23
23
 
24
24
 
25
25
  [tool.pytest.ini_options]
26
26
  testpaths = ["tests"]
27
- filterwarnings = [
28
- # Suppress pyparsing deprecation warning from pySigma library
29
- "ignore:'parseString' deprecated:DeprecationWarning",
30
- ]
31
27
 
32
- [tool.pdm]
33
- [tool.pdm.dev-dependencies]
34
- dev = []
28
+ [tool.ruff]
29
+ target-version = "py311"
30
+
31
+ [tool.ruff.lint]
32
+ select = ["E4", "E7", "E9", "F", "I", "UP"]
35
33
 
36
- [tool.pdm.build]
37
- includes = ["sigma"]
38
34
  [build-system]
39
35
  requires = ["poetry-core>=2.0.0"]
40
36
  build-backend = "poetry.core.masonry.api"