seer-core 0.54.0

Core library for Seer domain name utilities
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
//! Human (colored) renderers for the security/intelligence report types:
//! drift, email posture, standalone CAA, confusables, and classified
//! subdomains.

use super::{sanitize_line, HumanFormatter};
use crate::caa::CaaPolicy;
use crate::confusables::ConfusableReport;
use crate::drift::DriftReport;
use crate::headers::HeaderReport;
use crate::output::{subdomain_status_label, takeover_label};
use crate::posture::{EmailPosture, PostureVerdict};
use crate::subdomains::{SubdomainBaselineDiff, SubdomainClassification, SubdomainStatus};
use crate::takeover::{TakeoverReport, TakeoverVerdict};

impl HumanFormatter {
    /// Colors a posture/header verdict token by strength, so both security
    /// reports read identically.
    fn verdict(&self, verdict: PostureVerdict) -> String {
        let label = verdict.as_str();
        match verdict {
            PostureVerdict::Strict => self.success(label),
            PostureVerdict::Moderate | PostureVerdict::Weak | PostureVerdict::Unknown => {
                self.warning(label)
            }
            PostureVerdict::Present => self.value(label),
            PostureVerdict::Absent => self.error(label),
        }
    }

    pub(super) fn format_drift(&self, report: &DriftReport) -> String {
        let mut out = vec![self.header(&format!("Drift: {}", sanitize_line(&report.domain)))];
        if let Some(reason) = &report.inconclusive {
            out.push(self.warning(&format!("Not compared: {}", sanitize_line(reason))));
        } else if report.changes.is_empty() {
            out.push(self.success("No changes since the previous snapshot"));
        } else {
            for c in &report.changes {
                let old = c.old.as_deref().unwrap_or("(none)");
                let new = c.new.as_deref().unwrap_or("(none)");
                out.push(format!(
                    "{}: {} {} {}",
                    self.label(&sanitize_line(&c.field)),
                    self.dim(&sanitize_line(old)),
                    self.warning("→"),
                    self.value(&sanitize_line(new)),
                ));
            }
        }
        out.join("\n")
    }

    pub(super) fn format_subdomain_baseline_diff(&self, report: &SubdomainBaselineDiff) -> String {
        let mut out = vec![self.header(&format!(
            "Subdomain diff: {}",
            sanitize_line(&report.domain)
        ))];

        if report.baseline_missing {
            // Neutral phrasing: the CLI/REPL note carries the actionable
            // "--record" hint (it knows whether a baseline was just recorded).
            out.push(self.warning("No stored baseline to compare against (first run)"));
            return out.join("\n");
        }

        if let Some(at) = report.baseline_recorded_at {
            let at = self.value(&at.format("%Y-%m-%d %H:%M UTC").to_string());
            self.rows(&mut out, "").kv("Baseline recorded", at);
        }
        if report.baseline_truncated {
            out.push(self.warning(
                "Baseline came from truncated enumerations — added names may be long-standing \
                 (not counted as new until a complete run is recorded)",
            ));
        }
        out.push(format!(
            "{} added, {} removed, {} unchanged",
            self.value(&report.added.len().to_string()),
            self.value(&report.removed.len().to_string()),
            self.value(&report.unchanged_count.to_string()),
        ));

        if report.added.is_empty() && report.removed.is_empty() {
            out.push(self.success("No changes since the baseline"));
            return out.join("\n");
        }

        if !report.added.is_empty() {
            out.push(String::new());
            out.push(self.label("Added:"));
            for name in &report.added {
                out.push(format!(
                    "  {} {}",
                    self.warning("+"),
                    self.value(&sanitize_line(name))
                ));
            }
        }
        if !report.removed.is_empty() {
            out.push(String::new());
            // Removals are informational: CT logs are append-mostly, so a
            // vanished name usually means source flakiness (see baseline.rs).
            out.push(self.label("Removed (informational — often CT source flakiness):"));
            for name in &report.removed {
                out.push(format!(
                    "  {} {}",
                    self.dim("-"),
                    self.dim(&sanitize_line(name))
                ));
            }
        }
        out.join("\n")
    }

    pub(super) fn format_posture(&self, posture: &EmailPosture) -> String {
        let mut out = vec![self.header(&format!(
            "Email posture: {}",
            sanitize_line(&posture.domain)
        ))];

        let line = |name: &str, verdict: PostureVerdict, detail: Option<&str>| {
            let base = format!("{}: {}", self.label(name), self.verdict(verdict));
            let detail = match verdict {
                PostureVerdict::Unknown => Some("(lookup failed)"),
                _ => detail,
            };
            match detail {
                Some(d) if !d.is_empty() => format!("{base} {}", self.dim(&sanitize_line(d))),
                _ => base,
            }
        };

        out.push(line(
            "SPF",
            posture.spf.verdict,
            posture
                .spf
                .all_qualifier
                .as_ref()
                .map(|q| format!("{q}all"))
                .as_deref(),
        ));
        out.push(line(
            "DMARC",
            posture.dmarc.verdict,
            posture
                .dmarc
                .policy
                .as_deref()
                .map(|p| format!("p={p}"))
                .as_deref(),
        ));
        out.push(line("MTA-STS", posture.mta_sts.verdict, None));
        out.push(line("BIMI", posture.bimi.verdict, None));
        out.push(line(
            "DANE",
            posture.dane.verdict,
            Some(&format!("{} TLSA record(s)", posture.dane.records.len())),
        ));

        if !posture.notes.is_empty() {
            out.push(String::new());
            out.push(self.label("Advisories:"));
            for note in &posture.notes {
                out.push(format!("  {} {}", self.warning("•"), sanitize_line(note)));
            }
        }
        out.join("\n")
    }

    pub(super) fn format_headers(&self, report: &HeaderReport) -> String {
        let mut out = vec![self.header(&format!(
            "HTTP security headers: {}",
            sanitize_line(&report.domain)
        ))];

        // Color the grade by band so the headline verdict is readable at a
        // glance: A/A+ passing, B–C partial, D and below failing.
        let grade = match report.grade.as_str() {
            "A+" | "A" => self.success(&report.grade),
            "B" | "C" => self.warning(&report.grade),
            _ => self.error(&report.grade),
        };
        let score = self.value(&report.score.to_string());
        let url = self.value(&sanitize_line(&report.url));
        let status = self.dim(&format!("[HTTP {}]", report.status));
        let mut rows = self.rows(&mut out, "");
        rows.kv("Grade", format!("{grade} ({score}/100)"));
        rows.kv("URL", format!("{url} {status}"));
        if report.redirects > 0 {
            rows.kv(
                "Redirects followed",
                self.value(&report.redirects.to_string()),
            );
        }

        out.push(String::new());
        for finding in &report.headers {
            let mut line = format!(
                "{}: {}",
                self.label(&sanitize_line(&finding.header)),
                self.verdict(finding.verdict),
            );
            if let Some(value) = &finding.value {
                line.push_str(&format!(" {}", self.dim(&sanitize_line(value))));
            }
            out.push(line);
        }

        if !report.cookies.is_empty() {
            out.push(String::new());
            out.push(self.label("Cookies:"));
            for cookie in &report.cookies {
                let flags = [
                    ("Secure", cookie.secure),
                    ("HttpOnly", cookie.http_only),
                    ("SameSite", cookie.same_site.is_some()),
                ]
                .iter()
                .map(|(name, set)| {
                    if *set {
                        self.success(name)
                    } else {
                        self.error(&format!("no {name}"))
                    }
                })
                .collect::<Vec<_>>()
                .join(", ");
                out.push(format!(
                    "  {} [{}] {}",
                    self.value(&sanitize_line(&cookie.name)),
                    self.verdict(cookie.verdict),
                    self.dim(&flags),
                ));
            }
        }

        if !report.disclosures.is_empty() {
            out.push(String::new());
            out.push(self.label("Disclosed software:"));
            for d in &report.disclosures {
                let value = sanitize_line(&d.value);
                out.push(format!(
                    "  {}: {}",
                    self.dim(&sanitize_line(&d.header)),
                    if d.versioned {
                        self.warning(&value)
                    } else {
                        self.value(&value)
                    },
                ));
            }
        }

        if !report.notes.is_empty() {
            out.push(String::new());
            out.push(self.label("Advisories:"));
            for note in &report.notes {
                out.push(format!("  {} {}", self.warning("•"), sanitize_line(note)));
            }
        }
        out.join("\n")
    }

    pub(super) fn format_takeover(&self, report: &TakeoverReport) -> String {
        let mut out =
            vec![self.header(&format!("Takeover scan: {}", sanitize_line(&report.domain)))];

        out.push(format!(
            "{} host(s) checked — {} vulnerable, {} potential",
            self.value(&report.hosts_checked.to_string()),
            if report.vulnerable > 0 {
                self.error(&report.vulnerable.to_string())
            } else {
                self.success("0")
            },
            if report.potential > 0 {
                self.warning(&report.potential.to_string())
            } else {
                self.success("0")
            },
        ));
        if report.inconclusive > 0 {
            out.push(self.warning(&format!(
                "{} host(s) could not be checked",
                report.inconclusive
            )));
        }
        if report.hosts_skipped > 0 {
            out.push(self.warning(&format!(
                "{} more host(s) exceeded the scan cap and were not examined",
                report.hosts_skipped
            )));
        }

        if !report.findings.is_empty() {
            out.push(String::new());
            for f in &report.findings {
                let label = takeover_label(f.verdict);
                let verdict = match f.verdict {
                    TakeoverVerdict::Vulnerable => self.error(label),
                    TakeoverVerdict::Potential => self.warning(label),
                    TakeoverVerdict::Inconclusive => self.dim(label),
                    TakeoverVerdict::Safe => self.success(label),
                };
                let mut line = format!("{}  [{}]", self.value(&sanitize_line(&f.host)), verdict);
                if let Some(provider) = &f.provider {
                    line.push_str(&format!("  {}", self.dim(&sanitize_line(provider))));
                }
                out.push(line);
                if let Some(cname) = &f.cname {
                    out.push(format!(
                        "    {} {}",
                        self.label("CNAME →"),
                        self.dim(&sanitize_line(cname)),
                    ));
                }
                // The matched fingerprint is the evidence for a VULNERABLE
                // claim — always show it so the finding can be verified.
                if let Some(evidence) = &f.evidence {
                    out.push(format!(
                        "    {} {}",
                        self.label("Evidence:"),
                        self.error(&sanitize_line(evidence)),
                    ));
                }
                if let Some(note) = &f.probe_note {
                    out.push(format!("    {}", self.dim(&sanitize_line(note))));
                }
            }
        }

        if !report.notes.is_empty() {
            out.push(String::new());
            for note in &report.notes {
                out.push(format!("{} {}", self.warning("•"), sanitize_line(note)));
            }
        }
        out.join("\n")
    }

    pub(super) fn format_caa(&self, policy: &CaaPolicy) -> String {
        let mut out = vec![self.header("CAA Policy")];
        out.extend(self.render_caa_block(policy, ""));
        let mut rows = self.rows(&mut out, "  ");
        if !policy.iodef.is_empty() {
            rows.text("iodef (incident reporting)", &policy.iodef.join(", "));
        }
        if let Some(note) = &policy.wildcard_note {
            rows.kv("Wildcard", self.warning(&sanitize_line(note)));
        }
        self.push_caa_note_footer(&mut out, policy);
        out.join("\n")
    }

    pub(super) fn format_confusables(&self, report: &ConfusableReport) -> String {
        let mut out = vec![self.header(&format!("Look-alikes: {}", sanitize_line(&report.domain)))];
        out.push(format!(
            "{} candidates generated, {} registered",
            self.value(&report.candidates_generated.to_string()),
            self.value(&report.registered.len().to_string()),
        ));
        if report.registered.is_empty() {
            out.push(self.success("No registered look-alikes found"));
        } else {
            out.push(String::new());
            for r in &report.registered {
                let created = r
                    .creation_date
                    .map(|d| d.format("%Y-%m-%d").to_string())
                    .unwrap_or_else(|| "unknown".to_string());
                let registrar = r.registrar.as_deref().unwrap_or("-");
                out.push(format!(
                    "{}  [{}]  registered {}  via {}",
                    self.warning(&sanitize_line(&r.domain)),
                    self.dim(&sanitize_line(&r.technique)),
                    self.value(&created),
                    self.dim(&sanitize_line(registrar)),
                ));
            }
        }
        out.join("\n")
    }

    pub(super) fn format_subdomain_classification(
        &self,
        result: &SubdomainClassification,
    ) -> String {
        let mut out = vec![self.header(&format!("Subdomains: {}", sanitize_line(&result.domain)))];
        if result.wildcard_detected {
            out.push(
                self.warning(
                    "Wildcard DNS detected — some \"live\" verdicts may be zone wildcards",
                ),
            );
        }
        let live = result
            .subdomains
            .iter()
            .filter(|s| s.status == SubdomainStatus::Live)
            .count();
        out.push(format!(
            "{} names — {} live, {} takeover-risk",
            self.value(&result.subdomains.len().to_string()),
            self.value(&live.to_string()),
            self.value(
                &result
                    .subdomains
                    .iter()
                    .filter(|s| s.takeover_risk.is_some())
                    .count()
                    .to_string()
            ),
        ));
        if result.names_skipped > 0 {
            out.push(self.warning(&format!(
                "{} more names exceeded the classification cap and were not resolved",
                result.names_skipped
            )));
        }
        out.push(String::new());
        for s in &result.subdomains {
            let label = subdomain_status_label(s.status);
            let status = match s.status {
                SubdomainStatus::Live => self.success(label),
                SubdomainStatus::Dead => self.dim(label),
                SubdomainStatus::Wildcard | SubdomainStatus::Unknown => self.warning(label),
            };
            let mut line = format!("{}  [{}]", self.value(&sanitize_line(&s.name)), status);
            if let Some(cname) = &s.cname {
                line.push_str(&format!(
                    "  {} {}",
                    self.label("CNAME →"),
                    self.dim(&sanitize_line(cname))
                ));
            }
            if let Some(risk) = &s.takeover_risk {
                line.push_str(&format!(
                    "  {}",
                    self.error(&format!("takeover risk: {}", sanitize_line(risk)))
                ));
            }
            out.push(line);
        }
        out.join("\n")
    }
}