opseclint 1.3.0

Detection-coverage analyzer for Linux/auditd, Windows/Sysmon, and macOS/Endpoint Security: resolve shell/command actions to ATT&CK techniques, the telemetry they emit, and the detections that would fire.
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
//! Coverage diff: compare the current analysis against a previously saved
//! `--json` report and render what changed — findings added, removed, or whose
//! detectability / Sigma verdict shifted. It answers "did this change make me
//! louder or quieter?" across a ruleset or playbook revision.

use std::collections::BTreeMap;
use std::fmt::Write as _;

use serde::Serialize;

use crate::theme::{self, Painter};
use opseclint_core::model::{Detection, Report, Severity};

const WIDTH: usize = 60;

/// One rule's collapsed coverage state, across every line it matched.
#[derive(Debug, Clone, Serialize)]
pub struct DiffEntry {
    pub rule_id: String,
    pub description: String,
    pub techniques: Vec<String>,
    pub noise: u8,
    pub severity: Severity,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub verdict: Option<String>,
}

/// A rule present in both reports whose detectability or verdict moved.
#[derive(Debug, Clone, Serialize)]
pub struct Changed {
    pub before: DiffEntry,
    pub after: DiffEntry,
}

/// The full delta between a baseline and the current report.
#[derive(Debug, Clone, Serialize)]
pub struct Delta {
    pub platform: String,
    /// Set to the baseline's platform when it differs from the current one.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub platform_mismatch: Option<String>,
    pub added: Vec<DiffEntry>,
    pub removed: Vec<DiffEntry>,
    pub changed: Vec<Changed>,
    pub baseline_findings: usize,
    pub current_findings: usize,
    pub baseline_max_noise: u8,
    pub current_max_noise: u8,
}

impl Delta {
    /// True when the current run's peak detectability rose above the baseline —
    /// consistent with the tool's headline "loudest action" metric and the
    /// standard `--ci` gate. Drives both the trend word and the diff CI gate.
    pub fn is_louder(&self) -> bool {
        self.current_max_noise > self.baseline_max_noise
    }

    /// True when peak detectability fell below the baseline.
    pub fn is_quieter(&self) -> bool {
        self.current_max_noise < self.baseline_max_noise
    }

    pub fn is_empty(&self) -> bool {
        self.added.is_empty() && self.removed.is_empty() && self.changed.is_empty()
    }
}

fn verdict_rank(v: &str) -> u8 {
    match v.split_whitespace().next().unwrap_or("") {
        "fires" => 3,
        "indeterminate" => 2,
        "no-fire" => 1,
        _ => 0,
    }
}

/// The strongest verdict among a finding's detections (fires > indeterminate >
/// no-fire), normalized to its leading word. `None` when no rule was evaluated.
fn summarize_verdict(dets: &[Detection]) -> Option<String> {
    dets.iter()
        .filter_map(|d| d.verdict.as_deref())
        .max_by_key(|v| verdict_rank(v))
        .map(|v| v.split_whitespace().next().unwrap_or(v).to_string())
}

/// Collapse per-line findings into one entry per rule, keeping the loudest.
fn collapse(report: &Report) -> BTreeMap<String, DiffEntry> {
    let mut map: BTreeMap<String, DiffEntry> = BTreeMap::new();
    for f in &report.findings {
        let entry = DiffEntry {
            rule_id: f.rule_id.clone(),
            description: f.description.clone(),
            techniques: f.techniques.iter().map(|t| t.id.clone()).collect(),
            noise: f.noise,
            severity: f.severity,
            verdict: summarize_verdict(&f.detections),
        };
        map.entry(f.rule_id.clone())
            .and_modify(|e| {
                if entry.noise > e.noise {
                    *e = entry.clone();
                }
            })
            .or_insert(entry);
    }
    map
}

/// Compute the coverage delta of `current` relative to `baseline`.
pub fn compute(baseline: &Report, current: &Report) -> Delta {
    let base = collapse(baseline);
    let curr = collapse(current);

    let mut added = Vec::new();
    let mut changed = Vec::new();
    for (id, c) in &curr {
        match base.get(id) {
            None => added.push(c.clone()),
            Some(b) if b.noise != c.noise || b.verdict != c.verdict => changed.push(Changed {
                before: b.clone(),
                after: c.clone(),
            }),
            Some(_) => {}
        }
    }
    let mut removed: Vec<DiffEntry> = base
        .iter()
        .filter(|(id, _)| !curr.contains_key(*id))
        .map(|(_, b)| b.clone())
        .collect();

    // Loudest-first for display.
    let by_noise =
        |a: &DiffEntry, b: &DiffEntry| b.noise.cmp(&a.noise).then(a.rule_id.cmp(&b.rule_id));
    added.sort_by(by_noise);
    removed.sort_by(by_noise);
    changed.sort_by(|a, b| by_noise(&a.after, &b.after));

    Delta {
        platform: current.platform.clone(),
        platform_mismatch: (baseline.platform != current.platform)
            .then(|| baseline.platform.clone()),
        added,
        removed,
        changed,
        baseline_findings: baseline.findings.len(),
        current_findings: current.findings.len(),
        baseline_max_noise: baseline.max_noise,
        current_max_noise: current.max_noise,
    }
}

fn techs(p: &Painter, ids: &[String]) -> String {
    if ids.is_empty() {
        return String::new();
    }
    format!("  {}", p.paint(theme::PURPLE, &ids.join(", ")))
}

/// Render the delta for a terminal in the Tokyo Night palette.
pub fn render_human(delta: &Delta, color: bool) -> String {
    let p = Painter::new(color);
    let mut out = String::new();

    let _ = writeln!(
        out,
        "{}{}",
        p.bold(theme::BLUE, "opseclint"),
        p.paint(
            theme::COMMENT,
            &format!(" · coverage diff · {}", delta.platform)
        )
    );
    if let Some(base_plat) = &delta.platform_mismatch {
        let _ = writeln!(
            out,
            "{}",
            p.paint(
                theme::YELLOW,
                &format!(
                    "  ! baseline platform was {base_plat}, current is {} — comparison may be misleading",
                    delta.platform
                )
            )
        );
    }
    let _ = writeln!(
        out,
        "{}",
        p.paint(
            theme::COMMENT,
            &format!(
                "baseline {} finding(s) · current {} finding(s)",
                delta.baseline_findings, delta.current_findings
            )
        )
    );
    let _ = writeln!(out, "{}", p.rule(WIDTH));

    if delta.is_empty() {
        let _ = writeln!(out, " {}", p.paint(theme::FG_DIM, "No coverage change."));
    }

    for e in &delta.added {
        let _ = writeln!(
            out,
            " {}  {}{}",
            p.paint(
                theme::GREEN,
                &format!("+ {:<8} {:>2}", e.severity.label(), e.noise)
            ),
            p.paint(theme::FG, &e.description),
            techs(&p, &e.techniques),
        );
    }
    for e in &delta.removed {
        let _ = writeln!(
            out,
            " {}  {}{}",
            p.paint(
                theme::RED,
                &format!("- {:<8} {:>2}", e.severity.label(), e.noise)
            ),
            p.paint(theme::FG_DIM, &e.description),
            techs(&p, &e.techniques),
        );
    }
    for c in &delta.changed {
        let (b, a) = (&c.before, &c.after);
        let mut delta_str = String::new();
        if b.noise != a.noise {
            let _ = write!(
                delta_str,
                "{} {} {}",
                p.paint(
                    theme::COMMENT,
                    &format!("{} {}", b.severity.label(), b.noise)
                ),
                p.paint(theme::COMMENT, ""),
                p.paint(
                    theme::severity_color(a.severity),
                    &format!("{} {}", a.severity.label(), a.noise)
                ),
            );
        }
        if b.verdict != a.verdict {
            let fmt = |v: &Option<String>| v.clone().unwrap_or_else(|| "".to_string());
            if !delta_str.is_empty() {
                delta_str.push_str(&p.paint(theme::COMMENT, " · "));
            }
            let _ = write!(
                delta_str,
                "{} {} {}",
                p.paint(theme::COMMENT, &fmt(&b.verdict)),
                p.paint(theme::COMMENT, ""),
                p.paint(theme::CYAN, &fmt(&a.verdict)),
            );
        }
        let _ = writeln!(
            out,
            " {}  {}  {}{}",
            p.paint(theme::YELLOW, "~ CHANGED "),
            p.paint(theme::FG, &a.description),
            delta_str,
            techs(&p, &a.techniques),
        );
    }

    let _ = writeln!(out, "{}", p.rule(WIDTH));
    let trend = if delta.is_louder() {
        p.paint(theme::ORANGE, "louder")
    } else if delta.is_quieter() {
        p.paint(theme::GREEN, "quieter")
    } else {
        p.paint(theme::COMMENT, "peak unchanged")
    };
    let _ = writeln!(
        out,
        " {}  {} · {} · {} · max noise {}{} · {}",
        p.bold(theme::FG, "summary"),
        p.paint(theme::GREEN, &format!("+{}", delta.added.len())),
        p.paint(theme::RED, &format!("-{}", delta.removed.len())),
        p.paint(theme::YELLOW, &format!("~{}", delta.changed.len())),
        delta.baseline_max_noise,
        delta.current_max_noise,
        trend,
    );
    out
}

/// Render the delta as pretty JSON.
pub fn render_json(delta: &Delta) -> String {
    serde_json::to_string_pretty(delta).unwrap_or_else(|e| format!("{{\"error\":\"{e}\"}}"))
}

#[cfg(test)]
mod tests {
    use super::*;
    use opseclint_core::model::{Finding, Technique};

    fn finding(rule: &str, noise: u8, verdict: Option<&str>) -> Finding {
        Finding {
            line: 1,
            source: "opseclint".into(),
            rule_id: rule.into(),
            description: format!("desc {rule}"),
            techniques: vec![Technique {
                id: "T1059".into(),
                name: "n".into(),
            }],
            telemetry: vec![],
            detections: verdict
                .map(|v| {
                    vec![Detection {
                        source: "Sigma".into(),
                        rule: "r".into(),
                        confidence: "high".into(),
                        verdict: Some(v.into()),
                    }]
                })
                .unwrap_or_default(),
            edr: vec![],
            noise,
            severity: Severity::from_noise(noise),
            matched_command: None,
            observed_event: None,
            observed_side_effects: Vec::new(),
        }
    }

    fn report(platform: &str, findings: Vec<Finding>) -> Report {
        let max_noise = findings.iter().map(|f| f.noise).max().unwrap_or(0);
        Report {
            platform: platform.into(),
            note: String::new(),
            findings,
            max_noise,
            lines_analyzed: 1,
        }
    }

    #[test]
    fn detects_added_removed_and_changed() {
        let base = report(
            "linux-auditd",
            vec![finding("a", 40, None), finding("b", 60, None)],
        );
        // a: noise bumped 40 -> 55; b: removed; c: added.
        let curr = report(
            "linux-auditd",
            vec![finding("a", 55, None), finding("c", 70, None)],
        );
        let d = compute(&base, &curr);
        assert_eq!(
            d.added
                .iter()
                .map(|e| e.rule_id.as_str())
                .collect::<Vec<_>>(),
            ["c"]
        );
        assert_eq!(
            d.removed
                .iter()
                .map(|e| e.rule_id.as_str())
                .collect::<Vec<_>>(),
            ["b"]
        );
        assert_eq!(d.changed.len(), 1);
        assert_eq!(d.changed[0].after.rule_id, "a");
        assert!(d.is_louder()); // new finding + higher max noise
    }

    #[test]
    fn verdict_flip_is_a_change_even_at_same_noise() {
        let base = report("linux-auditd", vec![finding("a", 50, Some("no-fire"))]);
        let curr = report("linux-auditd", vec![finding("a", 50, Some("fires"))]);
        let d = compute(&base, &curr);
        assert_eq!(d.changed.len(), 1);
        assert_eq!(d.changed[0].before.verdict.as_deref(), Some("no-fire"));
        assert_eq!(d.changed[0].after.verdict.as_deref(), Some("fires"));
    }

    #[test]
    fn removing_the_loudest_action_reads_as_quieter() {
        let base = report(
            "linux-auditd",
            vec![finding("shell", 82, None), finding("recon", 45, None)],
        );
        let curr = report("linux-auditd", vec![finding("recon", 45, None)]);
        let d = compute(&base, &curr);
        assert!(d.is_quieter());
        assert!(!d.is_louder());
        assert_eq!(d.removed.len(), 1);
    }

    #[test]
    fn identical_reports_have_no_delta() {
        let r = report("linux-auditd", vec![finding("a", 50, None)]);
        let d = compute(&r, &r);
        assert!(d.is_empty());
        assert!(!d.is_louder());
    }

    #[test]
    fn platform_mismatch_is_flagged() {
        let base = report("windows-sysmon", vec![]);
        let curr = report("linux-auditd", vec![]);
        let d = compute(&base, &curr);
        assert_eq!(d.platform_mismatch.as_deref(), Some("windows-sysmon"));
    }
}