rsigma 0.17.0

CLI for parsing, validating, linting and evaluating Sigma detection rules
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
//! Coverage report: per-technique inventory plus the optional Atomic Red Team,
//! SigmaHQ-baseline, and target-list cross-references, with rendering through
//! the shared output layer.
//!
//! The report renders as the full JSON document (`json`), per-technique rows
//! (`ndjson`/`csv`/`tsv`), or a human summary with gap sections (`table`).
//! `--fail-on-gaps` turns any requested cross-reference's uncovered set into a
//! non-zero exit, the CI signal.

use std::collections::BTreeSet;

use super::sources::{CrossRef, Targets};
use super::{Coverage, parent_technique};
use crate::commands::reports::{
    AtomicsGap, BaselineGap, CoverageReport, CoverageSummary, TargetGap, TechniqueEntry,
};
use crate::exit_code;
use crate::output::{
    DelimitedWriter, OutputCtx, OutputFormat, Painter, Tabular, render_json, render_ndjson,
};

const TECHNIQUE_HEADERS: &[&str] = &["TECHNIQUE", "TACTICS", "RULES"];

impl Tabular for TechniqueEntry {
    fn headers() -> &'static [&'static str] {
        TECHNIQUE_HEADERS
    }
    fn row(&self) -> Vec<String> {
        vec![
            self.id.clone(),
            if self.tactics.is_empty() {
                "-".to_string()
            } else {
                self.tactics.join(",")
            },
            self.rule_count.to_string(),
        ]
    }
}

impl CoverageReport {
    /// Build the report from the computed coverage plus the optional
    /// cross-reference inputs. The serializable shape lives in
    /// `crate::commands::reports` so the `rule scorecard` consumer shares one
    /// definition with this producer; the `fail_on_gaps` exit-code decision is
    /// runtime-only and is threaded through [`CoverageReport::exit_code`].
    pub(crate) fn build(
        coverage: &Coverage,
        atomics: Option<CrossRef>,
        baseline: Option<CrossRef>,
        targets: Option<Targets>,
    ) -> Self {
        let techniques: Vec<TechniqueEntry> = coverage
            .techniques
            .iter()
            .map(|(id, agg)| TechniqueEntry {
                id: id.clone(),
                tactics: agg.tactics.iter().cloned().collect(),
                rule_count: agg.rule_count(),
                rules: agg.titles(),
            })
            .collect();

        let subtechniques = techniques.iter().filter(|t| t.id.contains('.')).count();

        let summary = CoverageSummary {
            rules_total: coverage.rules_total,
            rules_tagged: coverage.rules_tagged,
            rules_untagged: coverage.rules_total - coverage.rules_tagged,
            techniques: techniques.len(),
            subtechniques,
            tactics: coverage.tactics.len(),
        };

        CoverageReport {
            summary,
            techniques,
            untagged_rules: coverage.untagged_rules.clone(),
            atomics: atomics.map(|a| build_atomics_gap(coverage, &a)),
            baseline: baseline.map(|b| build_baseline_gap(coverage, &b)),
            targets: targets.map(|t| build_target_gap(coverage, &t)),
        }
    }

    /// House exit code: `FINDINGS` (1) under `--fail-on-gaps` when any
    /// requested cross-reference reports uncovered techniques, else `SUCCESS`.
    pub(crate) fn exit_code(&self, fail_on_gaps: bool) -> i32 {
        if fail_on_gaps && self.has_gaps() {
            exit_code::FINDINGS
        } else {
            exit_code::SUCCESS
        }
    }

    fn has_gaps(&self) -> bool {
        let target_gap = self
            .targets
            .as_ref()
            .is_some_and(|t| !t.uncovered.is_empty());
        let atomic_gap = self
            .atomics
            .as_ref()
            .is_some_and(|a| !a.atomics_without_rule.is_empty());
        let baseline_gap = self
            .baseline
            .as_ref()
            .is_some_and(|b| !b.baseline_not_covered.is_empty());
        target_gap || atomic_gap || baseline_gap
    }

    /// Render to stdout in the selected format. Machine formats emit a one-line
    /// recap on stderr (gated on `show_stats`) since their stdout carries only
    /// the per-technique rows.
    pub(crate) fn render(&self, ctx: &OutputCtx) {
        match ctx.format {
            OutputFormat::Json => render_json(self, ctx.pretty_json()),
            OutputFormat::Ndjson => {
                for t in &self.techniques {
                    render_ndjson(t);
                }
            }
            OutputFormat::Csv => self.render_delimited(','),
            OutputFormat::Tsv => self.render_delimited('\t'),
            OutputFormat::Table => self.render_human(ctx),
        }

        if ctx.format != OutputFormat::Table && ctx.show_stats() {
            eprintln!("{}", self.stderr_summary());
        }
    }

    fn render_delimited(&self, sep: char) {
        let mut writer = DelimitedWriter::new(sep, TechniqueEntry::headers());
        for t in &self.techniques {
            writer.push(&t.row());
        }
    }

    fn stderr_summary(&self) -> String {
        let s = &self.summary;
        format!(
            "Coverage: {} techniques ({} sub) across {} tactics from {}/{} tagged rules.",
            s.techniques, s.subtechniques, s.tactics, s.rules_tagged, s.rules_total,
        )
    }

    fn render_human(&self, ctx: &OutputCtx) {
        let p = Painter::new(ctx.color);
        let s = &self.summary;

        println!("{}", p.bold("Coverage summary"));
        println!(
            "  rules:        {} ({} tagged)",
            s.rules_total, s.rules_tagged
        );
        println!(
            "  techniques:   {} ({} sub-techniques)",
            s.techniques, s.subtechniques
        );
        println!("  tactics:      {}", s.tactics);
        if s.rules_untagged > 0 {
            println!(
                "  {}: {} rules carry no attack.* tag",
                p.yellow("untagged"),
                s.rules_untagged
            );
        }

        if !self.techniques.is_empty() {
            println!("\n{}", p.bold("Techniques"));
            crate::output::render_table(&self.techniques);
        }

        if let Some(a) = &self.atomics {
            println!("\n{}", p.bold("Atomic Red Team"));
            println!(
                "  {} of {} atomic techniques covered by a rule",
                a.covered, a.atomics_total
            );
            print_id_list(&p, "atomics with no rule", &a.atomics_without_rule);
            print_id_list(&p, "rules with no atomic", &a.rules_without_atomic);
        }

        if let Some(b) = &self.baseline {
            println!("\n{}", p.bold("SigmaHQ baseline"));
            println!(
                "  {} of {} baseline techniques covered locally",
                b.covered, b.baseline_total
            );
            print_id_list(&p, "baseline not covered", &b.baseline_not_covered);
            print_id_list(&p, "ahead of baseline", &b.ahead_of_baseline);
        }

        if let Some(t) = &self.targets {
            println!("\n{}", p.bold("Target techniques"));
            println!("  {} of {} targets covered", t.covered, t.targets_total);
            print_id_list(&p, "uncovered", &t.uncovered);
            print_id_list(&p, "covered via sub-technique", &t.covered_via_subtechnique);
        }
    }
}

fn print_id_list(p: &Painter, label: &str, ids: &[String]) {
    if ids.is_empty() {
        return;
    }
    let head = p.yellow(label);
    println!("  {head} ({}): {}", ids.len(), ids.join(", "));
}

// ---------------------------------------------------------------------------
// Cross-reference computation
// ---------------------------------------------------------------------------

fn build_atomics_gap(coverage: &Coverage, atomics: &CrossRef) -> AtomicsGap {
    let mut atomics_without_rule = Vec::new();
    let mut covered = 0usize;
    for id in &atomics.ids {
        if coverage.covers(id).covered {
            covered += 1;
        } else {
            atomics_without_rule.push(id.clone());
        }
    }
    // A rule technique has an atomic when the technique itself, or (for a
    // sub-technique) its parent, appears in the atomic set.
    let mut rules_without_atomic: Vec<String> = coverage
        .techniques
        .keys()
        .filter(|id| {
            let has_self = atomics.ids.contains(id.as_str());
            let has_parent = parent_technique(id)
                .map(|p| atomics.ids.contains(p))
                .unwrap_or(false);
            !has_self && !has_parent
        })
        .cloned()
        .collect();
    rules_without_atomic.sort();
    atomics_without_rule.sort();
    AtomicsGap {
        atomics_total: atomics.ids.len(),
        covered,
        atomics_without_rule,
        rules_without_atomic,
    }
}

fn build_baseline_gap(coverage: &Coverage, baseline: &CrossRef) -> BaselineGap {
    let mut baseline_not_covered = Vec::new();
    let mut covered = 0usize;
    for id in &baseline.ids {
        if coverage.covers(id).covered {
            covered += 1;
        } else {
            baseline_not_covered.push(id.clone());
        }
    }
    let mut ahead_of_baseline: Vec<String> = coverage
        .techniques
        .keys()
        .filter(|id| {
            let in_baseline = baseline.ids.contains(id.as_str());
            let parent_in_baseline = parent_technique(id)
                .map(|p| baseline.ids.contains(p))
                .unwrap_or(false);
            !in_baseline && !parent_in_baseline
        })
        .cloned()
        .collect();
    ahead_of_baseline.sort();
    baseline_not_covered.sort();
    BaselineGap {
        baseline_total: baseline.ids.len(),
        covered,
        baseline_not_covered,
        ahead_of_baseline,
    }
}

fn build_target_gap(coverage: &Coverage, targets: &Targets) -> TargetGap {
    let mut uncovered = Vec::new();
    let mut covered_via_subtechnique = Vec::new();
    let mut covered = 0usize;
    // Preserve the target file's order; de-dup is already done at load time.
    let mut seen = BTreeSet::new();
    for id in &targets.ids {
        if !seen.insert(id.clone()) {
            continue;
        }
        let c = coverage.covers(id);
        if c.covered {
            covered += 1;
            if c.via_subtechnique {
                covered_via_subtechnique.push(id.clone());
            }
        } else {
            uncovered.push(id.clone());
        }
    }
    TargetGap {
        targets_total: targets.ids.len(),
        covered,
        uncovered,
        covered_via_subtechnique,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::commands::coverage::Coverage;

    fn coverage_from(yaml: &str) -> Coverage {
        Coverage::from_collection(&rsigma_parser::parse_sigma_yaml(yaml).expect("parse"))
    }

    const RULES: &str = r#"
title: PowerShell
id: 00000000-0000-0000-0000-0000000000a1
logsource: {category: process_creation, product: windows}
detection: {sel: {Image|endswith: '\powershell.exe'}, condition: sel}
tags: [attack.execution, attack.t1059.001]
---
title: Whoami
id: 00000000-0000-0000-0000-0000000000a2
logsource: {category: process_creation, product: windows}
detection: {sel: {Image|endswith: '\whoami.exe'}, condition: sel}
tags: [attack.discovery, attack.t1033]
---
title: Untagged
id: 00000000-0000-0000-0000-0000000000a3
logsource: {category: process_creation, product: windows}
detection: {sel: {Image|endswith: '\x.exe'}, condition: sel}
"#;

    fn cross_ref(ids: &[&str]) -> CrossRef {
        CrossRef {
            ids: ids.iter().map(|s| s.to_string()).collect(),
        }
    }

    #[test]
    fn summary_counts_tagged_and_untagged() {
        let cov = coverage_from(RULES);
        let report = CoverageReport::build(&cov, None, None, None);
        assert_eq!(report.summary.rules_total, 3);
        assert_eq!(report.summary.rules_tagged, 2);
        assert_eq!(report.summary.rules_untagged, 1);
        assert_eq!(report.summary.techniques, 2); // T1059.001, T1033
        assert_eq!(report.summary.subtechniques, 1); // T1059.001
        assert_eq!(report.untagged_rules, vec!["Untagged".to_string()]);
    }

    #[test]
    fn target_parent_covered_via_subtechnique() {
        let cov = coverage_from(RULES);
        // T1059 (parent) is covered because a rule tags T1059.001.
        let targets = Targets {
            ids: vec!["T1059".to_string(), "T1003".to_string()],
        };
        let report = CoverageReport::build(&cov, None, None, Some(targets));
        let t = report.targets.as_ref().unwrap();
        assert_eq!(t.covered, 1);
        assert_eq!(t.covered_via_subtechnique, vec!["T1059".to_string()]);
        assert_eq!(t.uncovered, vec!["T1003".to_string()]);
        // Uncovered target under --fail-on-gaps -> findings.
        assert_eq!(report.exit_code(true), exit_code::FINDINGS);
    }

    #[test]
    fn subtechnique_target_not_covered_by_parent_rule() {
        // A rule on T1059.001 does NOT cover a target of T1059.002.
        let cov = coverage_from(RULES);
        let targets = Targets {
            ids: vec!["T1059.002".to_string()],
        };
        let report = CoverageReport::build(&cov, None, None, Some(targets));
        let t = report.targets.as_ref().unwrap();
        assert_eq!(t.uncovered, vec!["T1059.002".to_string()]);
    }

    #[test]
    fn atomics_gap_splits_both_directions() {
        let cov = coverage_from(RULES);
        // Atomics exist for T1059 (parent of a covered sub) and T1566 (no rule).
        let atomics = cross_ref(&["T1059", "T1566"]);
        let report = CoverageReport::build(&cov, Some(atomics), None, None);
        let a = report.atomics.as_ref().unwrap();
        // T1059 is covered via the sub-technique rule; T1566 is not.
        assert_eq!(a.covered, 1);
        assert_eq!(a.atomics_without_rule, vec!["T1566".to_string()]);
        // T1033 has a rule but no atomic in the set.
        assert!(a.rules_without_atomic.contains(&"T1033".to_string()));
    }

    #[test]
    fn fail_on_gaps_clean_when_all_covered() {
        let cov = coverage_from(RULES);
        let targets = Targets {
            ids: vec!["T1033".to_string()],
        };
        let report = CoverageReport::build(&cov, None, None, Some(targets));
        assert_eq!(report.exit_code(true), exit_code::SUCCESS);
    }
}