rsigma-parser 0.28.0

Parser for Sigma detection rules, correlations, and filters
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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
//! Dependency closure, filter retention, and uniqueness checks.

use std::collections::HashMap;

use super::error::RulesetError;
use super::select::{Decision, RefIndex, decide};
use super::source::{Candidate, DocumentKind};
use super::spec::{DependencyPolicy, RulesetSpec};

/// The result of closure: one decision per candidate plus warnings.
#[derive(Debug)]
pub(crate) struct Closure {
    pub decisions: Vec<Decision>,
    pub warnings: Vec<String>,
}

/// True when a decision keeps the candidate in the resolved collection.
pub(crate) fn is_kept(decision: &Decision) -> bool {
    matches!(
        decision,
        Decision::Included { .. } | Decision::PulledIn { .. } | Decision::FilterKept
    )
}

/// Resolve dependency references under the configured policy, retain filters
/// whose targets survived, and reject duplicate ids and names.
pub(crate) fn close(
    name: &str,
    spec: &RulesetSpec,
    candidates: &[Candidate],
) -> Result<Closure, RulesetError> {
    let policy = match &spec.dependencies {
        Some(value) => DependencyPolicy::parse(value)
            .map_err(|e| RulesetError::InvalidSpec(vec![format!("ruleset '{name}': {e}")]))?,
        None => DependencyPolicy::Error,
    };
    let mut decisions = decide(candidates, spec).map_err(|e| e.named(name))?;
    let refs = RefIndex::build(candidates);
    let mut warnings = Vec::new();
    let mut errors: Vec<String> = Vec::new();

    let mut iterations = 0;
    loop {
        iterations += 1;
        if iterations > candidates.len() + 1 {
            break;
        }
        let mut changed = false;

        for (index, candidate) in candidates.iter().enumerate() {
            if candidate.kind() != DocumentKind::Correlation || !is_kept(&decisions[index]) {
                continue;
            }
            let references = candidate.referenced_rules().to_vec();
            for reference in references {
                let resolved: Vec<usize> = refs
                    .resolve(&reference)
                    .into_iter()
                    .filter(|target| candidates[*target].kind() != DocumentKind::Filter)
                    .collect();
                if resolved.iter().any(|target| is_kept(&decisions[*target])) {
                    continue;
                }
                let reason = unresolved_reason(&reference, &resolved, &decisions);
                let subject = correlation_subject(candidate);

                match policy {
                    DependencyPolicy::Error => {
                        push_unique(&mut errors, format!("ruleset '{name}': {subject} {reason}"));
                    }
                    DependencyPolicy::Include => {
                        let pullable: Vec<usize> = resolved
                            .into_iter()
                            .filter(|target| decisions[*target] == Decision::NotSelected)
                            .collect();
                        if pullable.is_empty() {
                            push_unique(
                                &mut errors,
                                format!("ruleset '{name}': {subject} {reason}"),
                            );
                        }
                        for target in pullable {
                            decisions[target] = Decision::PulledIn {
                                by: correlation_label(candidate),
                            };
                            changed = true;
                        }
                    }
                    DependencyPolicy::Drop => {
                        warnings.push(format!(
                            "ruleset '{name}': dropped {subject} because it {reason}"
                        ));
                        decisions[index] = Decision::Dropped { reason };
                        changed = true;
                        break;
                    }
                }
            }
        }

        if !changed {
            break;
        }
    }

    if !errors.is_empty() {
        return Err(RulesetError::Dependency(errors));
    }

    // Filters stay when they target nothing specific or at least one kept
    // detection rule.
    for (index, candidate) in candidates.iter().enumerate() {
        if candidate.kind() != DocumentKind::Filter
            || matches!(decisions[index], Decision::Excluded { .. })
        {
            continue;
        }
        let kept = match candidate.filter_targets() {
            None => true,
            Some(targets) => targets.iter().any(|target| {
                refs.resolve_filter_target(target)
                    .into_iter()
                    .any(|target| {
                        candidates[target].kind() == DocumentKind::Detection
                            && is_kept(&decisions[target])
                    })
            }),
        };
        decisions[index] = if kept {
            Decision::FilterKept
        } else {
            Decision::FilterOrphaned
        };
    }

    check_uniqueness(name, candidates, &decisions)?;

    Ok(Closure {
        decisions,
        warnings,
    })
}

/// The fixed-point loop revisits every kept correlation on each pass, so the
/// same unresolvable reference would otherwise be reported once per pass.
fn push_unique(errors: &mut Vec<String>, reason: String) {
    if !errors.contains(&reason) {
        errors.push(reason);
    }
}

fn correlation_label(candidate: &Candidate) -> String {
    candidate
        .id()
        .or_else(|| candidate.name())
        .unwrap_or_else(|| candidate.title())
        .to_string()
}

fn origin_label(origin: &super::source::RuleOrigin) -> String {
    format!("{}#{}", origin.file.display(), origin.ordinal)
}

fn correlation_subject(candidate: &Candidate) -> String {
    format!(
        "correlation '{}' from {}",
        candidate.title(),
        origin_label(&candidate.origin)
    )
}

/// Why `reference` resolves to no kept document, preferring the copy that
/// could still be selected when a forked id has several.
fn unresolved_reason(reference: &str, resolved: &[usize], decisions: &[Decision]) -> String {
    let because = if resolved.is_empty() {
        "exists in no source".to_string()
    } else if resolved
        .iter()
        .any(|index| decisions[*index] == Decision::NotSelected)
    {
        "is not matched by include".to_string()
    } else if let Some(selector) = resolved.iter().find_map(|index| match decisions[*index] {
        Decision::Excluded { selector } => Some(selector),
        _ => None,
    }) {
        format!("is excluded by exclude selector {selector}")
    } else {
        "was dropped".to_string()
    };
    format!("references '{reference}', which {because}")
}

fn check_uniqueness(
    name: &str,
    candidates: &[Candidate],
    decisions: &[Decision],
) -> Result<(), RulesetError> {
    let mut ids: HashMap<String, usize> = HashMap::new();
    let mut names: HashMap<String, usize> = HashMap::new();
    for (index, candidate) in candidates.iter().enumerate() {
        if !is_kept(&decisions[index]) {
            continue;
        }
        if let Some(id) = candidate.id() {
            let key = id.to_ascii_lowercase();
            if let Some(previous) = ids.insert(key, index) {
                return Err(RulesetError::Duplicate(format!(
                    "ruleset '{name}': duplicate id '{id}' in {} and {}",
                    origin_label(&candidates[previous].origin),
                    origin_label(&candidate.origin)
                )));
            }
        }
        if let Some(candidate_name) = candidate.name()
            && let Some(previous) = names.insert(candidate_name.to_string(), index)
        {
            return Err(RulesetError::Duplicate(format!(
                "ruleset '{name}': duplicate name '{candidate_name}' in {} and {}",
                origin_label(&candidates[previous].origin),
                origin_label(&candidate.origin)
            )));
        }
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::ruleset::source::{CandidateDoc, RuleOrigin};
    use crate::ruleset::spec::{RuleSelector, RulesetSpec};

    fn candidate(yaml: &str, file: &str, ordinal: usize) -> Candidate {
        let collection = crate::parse_sigma_yaml(yaml).unwrap();
        let doc = if let Some(rule) = collection.rules.into_iter().next() {
            CandidateDoc::Detection(rule)
        } else if let Some(rule) = collection.correlations.into_iter().next() {
            CandidateDoc::Correlation(rule)
        } else {
            CandidateDoc::Filter(collection.filters.into_iter().next().unwrap())
        };
        Candidate {
            doc,
            origin: RuleOrigin {
                source: 0,
                root: std::path::PathBuf::from("."),
                file: std::path::PathBuf::from(file),
                ordinal,
            },
        }
    }

    fn detection(id: &str, file: &str) -> Candidate {
        candidate(
            &format!(
                "title: Detection {id}\nid: {id}\nlogsource:\n    product: windows\ndetection:\n    selection:\n        Field: value\n    condition: selection\n"
            ),
            file,
            0,
        )
    }

    fn correlation(id: &str, refs: &[&str], file: &str) -> Candidate {
        let refs = refs
            .iter()
            .map(|r| format!("        - {r}\n"))
            .collect::<String>();
        candidate(
            &format!(
                "title: Correlation {id}\nid: {id}\ncorrelation:\n    type: event_count\n    rules:\n{refs}    timespan: 5m\n    condition:\n        gte: 2\n"
            ),
            file,
            0,
        )
    }

    fn spec(
        dependencies: &str,
        include: Vec<RuleSelector>,
        exclude: Vec<RuleSelector>,
    ) -> RulesetSpec {
        RulesetSpec {
            sources: vec![std::path::PathBuf::from("rules")],
            dependencies: Some(dependencies.into()),
            include,
            exclude,
            ..Default::default()
        }
    }

    fn all_kept(decisions: &[Decision]) -> Vec<bool> {
        decisions.iter().map(is_kept).collect()
    }

    #[test]
    fn error_policy_reports_missing_reference() {
        let candidates = vec![correlation("c", &["missing"], "c.yml")];
        let error = close("test", &spec("error", vec![], vec![]), &candidates).unwrap_err();
        let message = error.to_string();
        assert!(message.contains("exists in no source"), "{message}");
    }

    #[test]
    fn error_policy_reports_excluded_reference() {
        let candidates = vec![detection("d", "d.yml"), correlation("c", &["d"], "c.yml")];
        let exclude = vec![RuleSelector {
            id: vec!["d".into()],
            ..Default::default()
        }];
        let error = close("test", &spec("error", vec![], exclude), &candidates).unwrap_err();
        assert!(
            error.to_string().contains("excluded by exclude selector 0"),
            "{error}"
        );
    }

    #[test]
    fn include_pulls_in_a_chain() {
        let candidates = vec![
            detection("base", "base.yml"),
            correlation("middle", &["base"], "middle.yml"),
            correlation("top", &["middle"], "top.yml"),
        ];
        let include = vec![RuleSelector {
            id: vec!["top".into()],
            ..Default::default()
        }];
        let closure = close("test", &spec("include", include, vec![]), &candidates).unwrap();
        assert_eq!(all_kept(&closure.decisions), [true, true, true]);
        assert!(matches!(closure.decisions[0], Decision::PulledIn { .. }));
    }

    #[test]
    fn include_refuses_an_excluded_rule() {
        let candidates = vec![
            detection("base", "base.yml"),
            correlation("top", &["base"], "top.yml"),
        ];
        let include = vec![RuleSelector {
            id: vec!["top".into()],
            ..Default::default()
        }];
        let exclude = vec![RuleSelector {
            id: vec!["base".into()],
            ..Default::default()
        }];
        let error = close("test", &spec("include", include, exclude), &candidates).unwrap_err();
        assert_eq!(
            error.to_string(),
            "ruleset 'test': correlation 'Correlation top' from top.yml#0 references 'base', which is excluded by exclude selector 0"
        );
    }

    /// A local fork shares its id with the vendored rule, and `path` excludes
    /// one copy. The other copy must still be pullable.
    fn forked(excluded_file: &str) -> (Vec<Candidate>, Vec<RuleSelector>, Vec<RuleSelector>) {
        let candidates = vec![
            detection("w1", "vendor.yml"),
            detection("w1", "fork.yml"),
            correlation("c", &["w1"], "c.yml"),
        ];
        let include = vec![RuleSelector {
            id: vec!["c".into()],
            ..Default::default()
        }];
        let exclude = vec![RuleSelector {
            path: vec![excluded_file.into()],
            ..Default::default()
        }];
        (candidates, include, exclude)
    }

    #[test]
    fn include_pulls_the_copy_of_a_fork_that_is_not_excluded() {
        for (excluded, pulled) in [("fork.yml", 0), ("vendor.yml", 1)] {
            let (candidates, include, exclude) = forked(excluded);
            let closure = close("test", &spec("include", include, exclude), &candidates).unwrap();
            assert!(
                matches!(closure.decisions[pulled], Decision::PulledIn { .. }),
                "{excluded}: {:?}",
                closure.decisions
            );
            assert!(matches!(
                closure.decisions[1 - pulled],
                Decision::Excluded { .. }
            ));
        }
    }

    #[test]
    fn error_policy_names_the_selectable_copy_of_a_fork() {
        let (candidates, include, exclude) = forked("fork.yml");
        let error = close("test", &spec("error", include, exclude), &candidates).unwrap_err();
        assert!(
            error
                .to_string()
                .ends_with("references 'w1', which is not matched by include"),
            "{error}"
        );
    }

    #[test]
    fn include_reports_each_unresolvable_reference_once() {
        // `pulls` changes a decision on the first pass, which forces a second
        // pass over `broken`.
        let candidates = vec![
            detection("base", "base.yml"),
            correlation("pulls", &["base"], "pulls.yml"),
            correlation("broken", &["missing"], "broken.yml"),
        ];
        let include = vec![RuleSelector {
            id: vec!["pulls".into(), "broken".into()],
            ..Default::default()
        }];
        let error = close("test", &spec("include", include, vec![]), &candidates).unwrap_err();
        let RulesetError::Dependency(reasons) = error else {
            panic!("expected a dependency error, got {error}");
        };
        assert_eq!(reasons.len(), 1, "{reasons:?}");
        assert!(reasons[0].contains("'missing'"), "{reasons:?}");
    }

    #[test]
    fn include_and_drop_handle_a_reference_in_no_source() {
        let candidates = vec![correlation("c", &["missing"], "c.yml")];
        let error = close("test", &spec("include", vec![], vec![]), &candidates).unwrap_err();
        assert!(
            error
                .to_string()
                .ends_with("references 'missing', which exists in no source"),
            "{error}"
        );

        let closure = close("test", &spec("drop", vec![], vec![]), &candidates).unwrap();
        assert_eq!(
            closure.decisions[0],
            Decision::Dropped {
                reason: "references 'missing', which exists in no source".into()
            }
        );
    }

    #[test]
    fn drop_cascades_to_a_parent() {
        let candidates = vec![
            detection("base", "base.yml"),
            correlation("middle", &["base"], "middle.yml"),
            correlation("top", &["middle"], "top.yml"),
        ];
        let exclude = vec![RuleSelector {
            id: vec!["base".into()],
            ..Default::default()
        }];
        let closure = close("test", &spec("drop", vec![], exclude), &candidates).unwrap();
        assert!(!is_kept(&closure.decisions[1]));
        assert!(!is_kept(&closure.decisions[2]));
        assert_eq!(
            closure.warnings,
            [
                "ruleset 'test': dropped correlation 'Correlation middle' from middle.yml#0 because it references 'base', which is excluded by exclude selector 0",
                "ruleset 'test': dropped correlation 'Correlation top' from top.yml#0 because it references 'middle', which was dropped",
            ]
        );
        assert_eq!(
            closure.decisions[2],
            Decision::Dropped {
                reason: "references 'middle', which was dropped".into()
            }
        );
    }

    #[test]
    fn references_resolve_by_name() {
        let mut base = detection("d", "d.yml");
        if let CandidateDoc::Detection(rule) = &mut base.doc {
            rule.name = Some("named_rule".into());
        }
        let candidates = vec![base, correlation("top", &["named_rule"], "top.yml")];
        let include = vec![RuleSelector {
            id: vec!["top".into()],
            ..Default::default()
        }];
        let closure = close("test", &spec("include", include, vec![]), &candidates).unwrap();
        assert!(is_kept(&closure.decisions[0]));
    }

    #[test]
    fn filter_any_is_kept_and_targeted_filters_follow_targets() {
        let candidates = vec![
            detection("d", "d.yml"),
            candidate(
                "title: Filter any\nlogsource:\n    product: windows\nfilter:\n    rules: any\n    selection:\n        Field: value\n    condition: selection\n",
                "any.yml",
                0,
            ),
            candidate(
                "title: Filter d\nlogsource:\n    product: windows\nfilter:\n    rules:\n        - d\n    selection:\n        Field: value\n    condition: selection\n",
                "d.yml",
                0,
            ),
            candidate(
                "title: Filter title\nlogsource:\n    product: windows\nfilter:\n    rules:\n        - Detection d\n    selection:\n        Field: value\n    condition: selection\n",
                "title.yml",
                0,
            ),
        ];
        let closure = close("test", &spec("error", vec![], vec![]), &candidates).unwrap();
        assert_eq!(closure.decisions[1], Decision::FilterKept);
        assert_eq!(closure.decisions[2], Decision::FilterKept);
        assert_eq!(closure.decisions[3], Decision::FilterKept);
    }

    #[test]
    fn filter_with_all_targets_excluded_is_orphaned() {
        let candidates = vec![
            detection("d", "d.yml"),
            candidate(
                "title: Filter d\nlogsource:\n    product: windows\nfilter:\n    rules:\n        - d\n    selection:\n        Field: value\n    condition: selection\n",
                "f.yml",
                0,
            ),
        ];
        let exclude = vec![RuleSelector {
            id: vec!["d".into()],
            ..Default::default()
        }];
        let closure = close("test", &spec("error", vec![], exclude), &candidates).unwrap();
        assert_eq!(closure.decisions[1], Decision::FilterOrphaned);
    }

    #[test]
    fn excluded_filter_is_excluded() {
        let candidates = vec![candidate(
            "title: Filter\nid: f\nlogsource:\n    product: windows\nfilter:\n    rules: any\n    selection:\n        Field: value\n    condition: selection\n",
            "f.yml",
            0,
        )];
        let exclude = vec![RuleSelector {
            id: vec!["f".into()],
            ..Default::default()
        }];
        let closure = close("test", &spec("error", vec![], exclude), &candidates).unwrap();
        assert!(matches!(closure.decisions[0], Decision::Excluded { .. }));
    }

    #[test]
    fn duplicate_ids_and_names_are_rejected() {
        let candidates = vec![detection("same", "a.yml"), detection("SAME", "b.yml")];
        let error = close("test", &spec("error", vec![], vec![]), &candidates).unwrap_err();
        let message = error.to_string();
        assert!(message.contains("duplicate id"), "{message}");
        assert!(
            message.contains("a.yml#0") && message.contains("b.yml#0"),
            "{message}"
        );

        let mut first = detection("a", "a.yml");
        let mut second = detection("b", "b.yml");
        if let CandidateDoc::Detection(rule) = &mut first.doc {
            rule.name = Some("dup".into());
        }
        if let CandidateDoc::Detection(rule) = &mut second.doc {
            rule.name = Some("dup".into());
        }
        let error = close("test", &spec("error", vec![], vec![]), &[first, second]).unwrap_err();
        assert!(error.to_string().contains("duplicate name"), "{error}");
    }

    #[test]
    fn determinism() {
        let candidates = vec![detection("d", "d.yml"), correlation("c", &["d"], "c.yml")];
        let first = close("test", &spec("error", vec![], vec![]), &candidates).unwrap();
        let second = close("test", &spec("error", vec![], vec![]), &candidates).unwrap();
        assert_eq!(first.decisions, second.decisions);
    }
}