rsigma-convert 0.24.0

Sigma rule conversion engine — convert rules to backend-native query strings
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
use std::collections::HashMap;

use rsigma_eval::pipeline::{Pipeline, apply_pipelines_to_correlation, apply_pipelines_with_state};
use rsigma_parser::{CorrelationRule, SigmaCollection};

use crate::backend::Backend;
use crate::error::{ConvertError, Result};
use crate::output::{ConversionOutput, ConversionResult};

/// Convert a collection of Sigma rules using the given backend and pipelines.
///
/// Merges the collection's filters into the rules they target (see
/// [`rsigma_eval::apply_filters`]), applies each pipeline to every rule, then
/// delegates to the backend for conversion. Errors from individual rules are
/// collected rather than aborting the entire batch.
///
/// For backends that support correlation, a rule-to-table mapping is built from
/// each detection rule's pipeline state and `postgres.table` custom attribute.
/// This mapping is injected into the correlation pipeline state under
/// `_rule_tables` so that temporal correlations can generate multi-table
/// `UNION ALL` queries when referenced rules target different tables.
pub fn convert_collection(
    backend: &dyn Backend,
    collection: &SigmaCollection,
    pipelines: &[Pipeline],
    output_format: &str,
) -> Result<ConversionOutput> {
    if backend.requires_pipeline() && pipelines.is_empty() {
        return Err(ConvertError::PipelineRequired);
    }

    let mut output = ConversionOutput::new();
    let mut rule_table_map: HashMap<String, String> = HashMap::new();
    let mut rule_schema_map: HashMap<String, String> = HashMap::new();
    let mut rule_query_map: HashMap<String, String> = HashMap::new();

    for mut rule in rsigma_eval::apply_filters(collection) {
        let emit_standalone = !backend.supports_correlation()
            || should_emit_standalone(
                rule.id.as_deref(),
                rule.name.as_deref(),
                &collection.correlations,
            );
        let pipeline_state = if !pipelines.is_empty() {
            apply_pipelines_with_state(pipelines, &mut rule)?
        } else {
            Default::default()
        };

        // Record rule → table/schema for multi-table correlation support.
        // custom_attributes["postgres.*"] takes precedence over pipeline state.
        let resolved_table = rule
            .custom_attributes
            .get("postgres.table")
            .and_then(|v| v.as_str())
            .or_else(|| pipeline_state.state.get("table").and_then(|v| v.as_str()));

        if let Some(table) = resolved_table {
            if let Some(id) = &rule.id {
                rule_table_map.insert(id.clone(), table.to_string());
            }
            if let Some(name) = &rule.name {
                rule_table_map.insert(name.clone(), table.to_string());
            }
            rule_table_map.insert(rule.title.clone(), table.to_string());
        }

        let resolved_schema = rule
            .custom_attributes
            .get("postgres.schema")
            .and_then(|v| v.as_str())
            .or_else(|| pipeline_state.state.get("schema").and_then(|v| v.as_str()));

        if let Some(schema) = resolved_schema {
            if let Some(id) = &rule.id {
                rule_schema_map.insert(id.clone(), schema.to_string());
            }
            if let Some(name) = &rule.name {
                rule_schema_map.insert(name.clone(), schema.to_string());
            }
            rule_schema_map.insert(rule.title.clone(), schema.to_string());
        }

        match backend.convert_rule(&rule, output_format, &pipeline_state) {
            Ok(queries) => {
                if let Some(q) = queries.first() {
                    if let Some(id) = &rule.id {
                        rule_query_map.insert(id.clone(), q.clone());
                    }
                    if let Some(name) = &rule.name {
                        rule_query_map.insert(name.clone(), q.clone());
                    }
                    rule_query_map.insert(rule.title.clone(), q.clone());
                }
                if emit_standalone {
                    output.queries.push(ConversionResult {
                        rule_title: rule.title.clone(),
                        rule_id: rule.id.clone(),
                        queries,
                        warnings: Vec::new(),
                    });
                }
            }
            Err(e) => {
                output.errors.push((rule.title.clone(), e));
            }
        }
    }

    if backend.supports_correlation() {
        for corr in &collection.correlations {
            let emit_standalone = should_emit_standalone(
                corr.id.as_deref(),
                corr.name.as_deref(),
                &collection.correlations,
            );
            let mut corr = corr.clone();
            let mut pipeline_state = if !pipelines.is_empty() {
                apply_pipelines_to_correlation(pipelines, &mut corr)?
            } else {
                Default::default()
            };

            if !rule_table_map.is_empty() {
                let map_value = serde_json::to_value(&rule_table_map)
                    .unwrap_or(serde_json::Value::Object(Default::default()));
                pipeline_state.set_state("_rule_tables".to_string(), map_value);
            }
            if !rule_schema_map.is_empty() {
                let map_value = serde_json::to_value(&rule_schema_map)
                    .unwrap_or(serde_json::Value::Object(Default::default()));
                pipeline_state.set_state("_rule_schemas".to_string(), map_value);
            }
            let map_value = serde_json::to_value(&rule_query_map)
                .unwrap_or(serde_json::Value::Object(Default::default()));
            pipeline_state.set_state("_rule_queries".to_string(), map_value);

            let mut warnings = Vec::new();
            match backend.convert_correlation_rule_with_warnings(
                &corr,
                output_format,
                &pipeline_state,
                &mut warnings,
            ) {
                Ok(queries) => {
                    if emit_standalone {
                        output.queries.push(ConversionResult {
                            rule_title: corr.title.clone(),
                            rule_id: corr.id.clone(),
                            queries,
                            warnings,
                        });
                    }
                }
                Err(e) => {
                    output.errors.push((corr.title.clone(), e));
                }
            }
        }
    } else {
        for corr in &collection.correlations {
            output.errors.push((
                corr.title.clone(),
                ConvertError::UnsupportedCorrelation(corr.correlation_type.as_str().into()),
            ));
        }
    }

    Ok(output)
}

/// Whether a detection or correlation rule with this `id` and `name` gets
/// its own query: it is referenced by no correlation, or by at least one
/// correlation with `generate: true`.
fn should_emit_standalone(
    id: Option<&str>,
    name: Option<&str>,
    correlations: &[CorrelationRule],
) -> bool {
    let mut referenced = false;
    for correlation in correlations {
        let matches = correlation
            .rules
            .iter()
            .any(|rule_ref| id == Some(rule_ref.as_str()) || name == Some(rule_ref.as_str()));
        if matches {
            referenced = true;
            if correlation.generate {
                return true;
            }
        }
    }
    !referenced
}

/// True if any dot-segment of a field path is a positional array index
/// (`name[N]`, including a negative `name[-N]`). The quantifier selectors never
/// reach field names (the parser desugars them into `Detection::ArrayMatch`),
/// so a bracketed integer is the positional-index signal.
pub(crate) fn field_has_positional_index(field: &str) -> bool {
    field.split('.').any(|seg| {
        // Only an unescaped trailing `[...]` is a selector; `\[` / `\]` are a
        // literal bracket in the field name, not a positional index.
        let Some(open) = rsigma_parser::fieldpath::first_unescaped(seg, b'[') else {
            return false;
        };
        if !rsigma_parser::fieldpath::ends_with_unescaped(seg, b']') {
            return false;
        }
        let inner = &seg[open + 1..seg.len() - 1];
        let digits = inner.strip_prefix('-').unwrap_or(inner);
        !digits.is_empty() && digits.bytes().all(|b| b.is_ascii_digit())
    })
}

#[cfg(test)]
mod tests {
    use super::{convert_collection, field_has_positional_index, should_emit_standalone};
    use crate::backends::postgres::PostgresBackend;
    use crate::backends::test::TextQueryTestBackend;
    use rsigma_eval::pipeline::parse_pipeline;
    use rsigma_parser::parse_sigma_yaml;

    const FILTERED: &str = r#"
title: Whoami
name: whoami
logsource:
    category: process_creation
    product: windows
detection:
    selection:
        Image|endswith: '\whoami.exe'
    condition: selection
---
title: Ping
logsource:
    category: process_creation
    product: windows
detection:
    selection:
        Image|endswith: '\ping.exe'
    condition: selection
---
title: Exclude admins
logsource:
    category: process_creation
    product: windows
filter:
    rules: [whoami]
    selection:
        User: admin
    condition: not selection
"#;

    fn test_queries(yaml: &str, pipelines: &[rsigma_eval::Pipeline]) -> Vec<String> {
        let collection = parse_sigma_yaml(yaml).unwrap();
        let output = convert_collection(
            &TextQueryTestBackend::new(),
            &collection,
            pipelines,
            "default",
        )
        .unwrap();
        assert!(output.errors.is_empty(), "{:?}", output.errors);
        output
            .queries
            .into_iter()
            .flat_map(|result| result.queries)
            .collect()
    }

    #[test]
    fn filters_apply_to_the_rules_they_reference() {
        assert_eq!(
            test_queries(FILTERED, &[]),
            [
                r#"Image endswith "\whoami.exe" and not User="admin""#,
                r#"Image endswith "\ping.exe""#,
            ]
        );
    }

    #[test]
    fn pipelines_transform_filter_detections() {
        let pipeline = parse_pipeline(
            r#"
name: map user
transformations:
    - type: field_name_mapping
      mapping:
          User: user_name
"#,
        )
        .unwrap();
        assert_eq!(
            test_queries(FILTERED, &[pipeline])[0],
            r#"Image endswith "\whoami.exe" and not user_name="admin""#
        );
    }

    #[test]
    fn correlations_fail_on_a_backend_without_correlation_support() {
        let yaml = r#"
title: Base
name: base
logsource:
    category: test
detection:
    selection:
        EventID: 1
    condition: selection
---
title: Count
correlation:
    type: event_count
    rules: [base]
    group-by: [User]
    timespan: 1m
    condition:
        gte: 2
"#;
        let collection = parse_sigma_yaml(yaml).unwrap();
        let output =
            convert_collection(&TextQueryTestBackend::new(), &collection, &[], "default").unwrap();
        assert_eq!(output.queries.len(), 1);
        assert_eq!(output.errors.len(), 1);
        let (title, err) = &output.errors[0];
        assert_eq!(title, "Count");
        assert!(
            matches!(err, crate::ConvertError::UnsupportedCorrelation(t) if t == "event_count"),
            "{err}"
        );
    }

    #[test]
    fn filter_logsource_must_be_contained_in_the_rule() {
        let yaml = FILTERED
            .replace("    rules: [whoami]", "    rules: any")
            .replace(
                "title: Exclude admins\n",
                "title: Exclude admins\nlogsource:\n    product: linux\n",
            );
        assert_eq!(
            test_queries(&yaml, &[]),
            [
                r#"Image endswith "\whoami.exe""#,
                r#"Image endswith "\ping.exe""#,
            ]
        );
    }

    #[test]
    fn positional_index_detection_respects_escaping() {
        assert!(field_has_positional_index("args[0]"));
        assert!(field_has_positional_index("args[-1]"));
        assert!(field_has_positional_index("connections[0].ip"));
        // Escaped brackets are a literal field name, not a positional index.
        assert!(!field_has_positional_index("args\\[0\\]"));
        assert!(!field_has_positional_index("weird\\[x\\]"));
        // Quantifier selectors never reach field names, and plain fields have
        // no index.
        assert!(!field_has_positional_index("process.args"));
    }

    #[test]
    fn referenced_rules_are_standalone_only_when_generate_is_true() {
        let yaml = r#"
title: Base
name: base
logsource:
    category: test
detection:
    selection:
        EventID: 1
    condition: selection
---
title: Count
correlation:
    type: event_count
    rules: [base]
    group-by: [User]
    timespan: 1m
    condition:
        gte: 2
"#;
        let collection = parse_sigma_yaml(yaml).unwrap();
        assert!(!should_emit_standalone(
            None,
            Some("base"),
            &collection.correlations
        ));

        let mut generated = collection.correlations.clone();
        generated[0].generate = true;
        assert!(should_emit_standalone(None, Some("base"), &generated));
    }

    #[test]
    fn collection_conversion_omits_referenced_rule_by_default() {
        let yaml = r#"
title: Base
name: base
logsource:
    category: test
detection:
    selection:
        EventID: 1
    condition: selection
---
title: Count
correlation:
    type: event_count
    rules: [base]
    group-by: [User]
    timespan: 1m
    condition:
        gte: 2
"#;
        let collection = parse_sigma_yaml(yaml).unwrap();
        let output =
            convert_collection(&PostgresBackend::new(), &collection, &[], "default").unwrap();
        let titles: Vec<_> = output
            .queries
            .iter()
            .map(|result| result.rule_title.as_str())
            .collect();
        assert_eq!(titles, ["Count"]);

        let mut generated = collection;
        generated.correlations[0].generate = true;
        let output =
            convert_collection(&PostgresBackend::new(), &generated, &[], "default").unwrap();
        let titles: Vec<_> = output
            .queries
            .iter()
            .map(|result| result.rule_title.as_str())
            .collect();
        assert_eq!(titles, ["Base", "Count"]);
    }

    #[test]
    fn aggregate_correlation_over_a_correlation_is_an_error() {
        let yaml = r#"
title: Base
name: base
logsource:
    category: test
detection:
    selection:
        EventID: 1
    condition: selection
---
title: Count
name: count
correlation:
    type: event_count
    rules: [base]
    group-by: [User]
    timespan: 1m
    condition:
        gte: 2
---
title: Bursts
correlation:
    type: event_count
    rules: [count]
    group-by: [User]
    timespan: 1h
    condition:
        gte: 3
"#;
        let collection = parse_sigma_yaml(yaml).unwrap();
        let output =
            convert_collection(&PostgresBackend::new(), &collection, &[], "default").unwrap();
        assert!(output.queries.is_empty(), "{:?}", output.queries);
        let [(title, error)] = output.errors.as_slice() else {
            panic!("expected one error: {:?}", output.errors);
        };
        assert_eq!(title, "Bursts");
        assert!(
            error.to_string().contains("rule reference 'count'"),
            "{error}"
        );
    }
}