rsigma-eval 0.28.0

Evaluator for Sigma detection and correlation rules: match rules against events
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
//! Rule-field extraction shared between `rsigma rule fields` and the daemon's
//! field-observability endpoints.
//!
//! [`RuleFieldSet::collect`] walks a [`SigmaCollection`] (after optional
//! pipeline transformations are applied) and records every field name
//! referenced by detection items, correlation `group-by` / threshold / alias
//! fields, filter detections, and rule-level `fields:` metadata. The result
//! tracks per-field provenance (rule titles + source kinds) so callers can
//! decide whether to surface a finding as a gap signal, a broken-coverage
//! signal, or a coverage summary.
//!
//! The CLI command `rsigma rule fields` and the daemon's
//! `GET /api/v1/fields/*` endpoints share this implementation so the
//! field set the operator inspects offline matches exactly what the engine
//! references at runtime.

use std::collections::{BTreeMap, BTreeSet};

use rsigma_parser::{
    CorrelationCondition, CorrelationRule, Detection, DetectionItem, Detections, FilterRule,
    SigmaCollection, SigmaRule,
};
use serde::Serialize;

use crate::pipeline::{Pipeline, apply_pipelines};

/// Where in a rule a field reference came from.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum FieldSource {
    /// Field used in a detection condition (`selection`, etc.).
    Detection,
    /// Field used by a correlation rule (group-by, threshold field, alias mapping).
    Correlation,
    /// Field used in a filter rule's detection block.
    Filter,
    /// Field listed in rule-level `fields:` metadata.
    Metadata,
}

impl FieldSource {
    /// Stable string identifier used in JSON serialization and human output.
    pub fn as_str(self) -> &'static str {
        match self {
            FieldSource::Detection => "detection",
            FieldSource::Correlation => "correlation",
            FieldSource::Filter => "filter",
            FieldSource::Metadata => "metadata",
        }
    }
}

/// Provenance for a single field name across the loaded rule set.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct FieldOrigin {
    /// Rule titles that reference this field.
    pub rule_titles: BTreeSet<String>,
    /// Source kinds (detection, correlation, filter, metadata) where this
    /// field was seen.
    pub sources: BTreeSet<FieldSource>,
}

/// Set of field names referenced by a loaded `SigmaCollection`, optionally
/// after applying processing pipelines.
///
/// Built via [`RuleFieldSet::collect`] and queried via [`contains`](Self::contains),
/// [`iter`](Self::iter), and [`len`](Self::len). Cheap to clone for sharing
/// across threads behind an `Arc`.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct RuleFieldSet {
    fields: BTreeMap<String, FieldOrigin>,
}

impl RuleFieldSet {
    /// Walk a rule collection (and any pipelines) and return the resulting
    /// field set. When `pipelines` is non-empty, each rule is cloned and
    /// transformed before its fields are collected so the recorded names
    /// match what the engine evaluates against. Rules whose pipeline
    /// application fails fall back to the untransformed names so the set
    /// stays observable even when a pipeline misfires on one rule.
    ///
    /// `include_filters` controls whether filter-rule detection blocks
    /// contribute to the set; mirrors the existing `--no-filters` flag on
    /// `rsigma rule fields`.
    pub fn collect(
        collection: &SigmaCollection,
        pipelines: &[Pipeline],
        include_filters: bool,
    ) -> Self {
        let mut collector = Collector::default();

        if pipelines.is_empty() {
            for rule in &collection.rules {
                collector.collect_rule(rule);
            }
        } else {
            for rule in &collection.rules {
                let mut transformed = rule.clone();
                if apply_pipelines(pipelines, &mut transformed).is_err() {
                    collector.collect_rule(rule);
                    continue;
                }
                collector.collect_rule(&transformed);
            }
        }

        for corr in &collection.correlations {
            collector.collect_correlation(corr);
        }

        if include_filters {
            for filter in &collection.filters {
                collector.collect_filter(filter);
            }
        }

        Self {
            fields: collector.fields,
        }
    }

    /// True if any rule references this field name.
    pub fn contains(&self, field: &str) -> bool {
        self.fields.contains_key(field)
    }

    /// Look up provenance for a single field name.
    pub fn origin(&self, field: &str) -> Option<&FieldOrigin> {
        self.fields.get(field)
    }

    /// Iterate field names and their provenance in sorted order.
    pub fn iter(&self) -> impl Iterator<Item = (&str, &FieldOrigin)> {
        self.fields.iter().map(|(k, v)| (k.as_str(), v))
    }

    /// Iterate just the field names in sorted order.
    pub fn names(&self) -> impl Iterator<Item = &str> {
        self.fields.keys().map(String::as_str)
    }

    /// Number of distinct fields in the set.
    pub fn len(&self) -> usize {
        self.fields.len()
    }

    /// True when no fields were collected.
    pub fn is_empty(&self) -> bool {
        self.fields.is_empty()
    }

    /// Merge another field set into this one, unioning provenance.
    pub fn extend(&mut self, other: RuleFieldSet) {
        for (field, origin) in other.fields {
            let entry = self.fields.entry(field).or_default();
            entry.rule_titles.extend(origin.rule_titles);
            entry.sources.extend(origin.sources);
        }
    }
}

#[derive(Default)]
struct Collector {
    fields: BTreeMap<String, FieldOrigin>,
}

impl Collector {
    fn add(&mut self, field: &str, rule_title: &str, source: FieldSource) {
        let entry = self.fields.entry(field.to_string()).or_default();
        entry.rule_titles.insert(rule_title.to_string());
        entry.sources.insert(source);
    }

    fn collect_detection_items(
        &mut self,
        detection: &Detection,
        rule_title: &str,
        source: FieldSource,
    ) {
        match detection {
            Detection::AllOf(items) => {
                for item in items {
                    self.collect_item(item, rule_title, source);
                }
            }
            Detection::AnyOf(subs) => {
                for sub in subs {
                    self.collect_detection_items(sub, rule_title, source);
                }
            }
            Detection::ArrayMatch { field, body, .. } => {
                // Report the array container field, then the member fields
                // referenced inside the block.
                self.add(field, rule_title, source);
                self.collect_detection_items(body, rule_title, source);
            }
            Detection::And(subs) => {
                for sub in subs {
                    self.collect_detection_items(sub, rule_title, source);
                }
            }
            Detection::Conditional { named, .. } => {
                for sub in named.values() {
                    self.collect_detection_items(sub, rule_title, source);
                }
            }
            Detection::Keywords(_) => {}
        }
    }

    fn collect_item(&mut self, item: &DetectionItem, rule_title: &str, source: FieldSource) {
        if let Some(ref name) = item.field.name {
            self.add(name, rule_title, source);
        }
    }

    fn collect_detections(
        &mut self,
        detections: &Detections,
        rule_title: &str,
        source: FieldSource,
    ) {
        for det in detections.named.values() {
            self.collect_detection_items(det, rule_title, source);
        }
    }

    fn collect_rule(&mut self, rule: &SigmaRule) {
        self.collect_detections(&rule.detection, &rule.title, FieldSource::Detection);
        for f in &rule.fields {
            self.add(f, &rule.title, FieldSource::Metadata);
        }
    }

    fn collect_correlation(&mut self, corr: &CorrelationRule) {
        for f in &corr.group_by {
            self.add(f, &corr.title, FieldSource::Correlation);
        }
        if let CorrelationCondition::Threshold {
            field: Some(ref fields),
            ..
        } = corr.condition
        {
            for f in fields {
                self.add(f, &corr.title, FieldSource::Correlation);
            }
        }
        for alias in &corr.aliases {
            for mapped_field in alias.mapping.values() {
                self.add(mapped_field, &corr.title, FieldSource::Correlation);
            }
        }
        for f in &corr.fields {
            self.add(f, &corr.title, FieldSource::Metadata);
        }
    }

    fn collect_filter(&mut self, filter: &FilterRule) {
        self.collect_detections(&filter.detection, &filter.title, FieldSource::Filter);
        for f in &filter.fields {
            self.add(f, &filter.title, FieldSource::Metadata);
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use rsigma_parser::parse_sigma_yaml;

    fn build(yaml: &str) -> SigmaCollection {
        parse_sigma_yaml(yaml).expect("parse")
    }

    #[test]
    fn collects_detection_fields() {
        let collection = build(
            r#"
title: Test
status: test
logsource:
    category: test
detection:
    selection:
        CommandLine|contains: whoami
        EventID: 1
    condition: selection
"#,
        );
        let set = RuleFieldSet::collect(&collection, &[], true);
        assert!(set.contains("CommandLine"));
        assert!(set.contains("EventID"));
        assert!(
            set.origin("CommandLine")
                .unwrap()
                .sources
                .contains(&FieldSource::Detection)
        );
    }

    #[test]
    fn collects_correlation_group_by() {
        let collection = build(
            r#"
title: Login
id: login-rule
logsource:
    category: auth
detection:
    selection:
        EventType: login
    condition: selection
---
title: Many Logins
correlation:
    type: event_count
    rules:
        - login-rule
    group-by:
        - User
    timespan: 60s
    condition:
        gte: 3
"#,
        );
        let set = RuleFieldSet::collect(&collection, &[], true);
        assert!(set.contains("EventType"));
        assert!(set.contains("User"));
        let user_origin = set.origin("User").unwrap();
        assert!(user_origin.sources.contains(&FieldSource::Correlation));
    }

    #[test]
    fn include_filters_toggle() {
        let collection = build(
            r#"
title: Detection
status: test
logsource:
    category: test
detection:
    selection:
        DetField: x
    condition: selection
---
title: Filter
logsource:
    category: test
filter:
    rules:
        - non-existent
    selection:
        FilterField: y
    condition: selection
"#,
        );
        let with_filters = RuleFieldSet::collect(&collection, &[], true);
        let without_filters = RuleFieldSet::collect(&collection, &[], false);
        assert!(with_filters.contains("FilterField"));
        assert!(!without_filters.contains("FilterField"));
        assert!(with_filters.contains("DetField"));
        assert!(without_filters.contains("DetField"));
    }

    #[test]
    fn empty_collection_is_empty_set() {
        let collection = SigmaCollection::default();
        let set = RuleFieldSet::collect(&collection, &[], true);
        assert!(set.is_empty());
        assert_eq!(set.len(), 0);
    }

    #[test]
    fn extend_unions_fields_and_provenance() {
        let first = RuleFieldSet::collect(
            &build(
                "title: One\nid: one\nlogsource:\n    category: test\ndetection:\n    selection:\n        Shared: a\n        First: b\n    condition: selection\n",
            ),
            &[],
            true,
        );
        let second = RuleFieldSet::collect(
            &build(
                "title: Two\nid: two\nlogsource:\n    category: test\ndetection:\n    selection:\n        Second: d\n    condition: selection\nfields:\n    - Shared\n",
            ),
            &[],
            true,
        );
        let mut merged = first;
        merged.extend(second);
        assert!(merged.contains("First"));
        assert!(merged.contains("Second"));
        let shared = merged.origin("Shared").unwrap();
        assert_eq!(
            shared.rule_titles,
            BTreeSet::from(["One".to_string(), "Two".to_string()])
        );
        assert_eq!(
            shared.sources,
            BTreeSet::from([FieldSource::Detection, FieldSource::Metadata])
        );
    }
}