zond-engine 0.18.0

The core networking library powering Zond. Raw packet injection, protocol parsing, service and OS fingerprinting, and asynchronous network scanning.
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
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
// Copyright (c) 2026 Erik Lening (hollowpointer) and Contributors
//
// This file is part of Zond Engine, licensed under the GNU Affero General
// Public License, version 3 or later. See the LICENSE file for details, or
// <https://www.gnu.org/licenses/agpl-3.0.html>.
//
// SPDX-License-Identifier: AGPL-3.0-or-later

//! # The comparison document
//!
//! What a consumer parses, and the single site where a change is given its name.
//! [`ChangeDto::of_host`] and [`ChangeDto::of_port`] turn the engine's typed
//! deltas into that vocabulary, and a front end printing one line per change
//! calls them too, so a comparison in a terminal and one in a queue name the same
//! event the same way.
//!
//! The conventions of the report document hold here without exception:
//! timestamps are RFC 3339 in UTC, objects have a fixed shape with `null` rather
//! than an absent field, order is deterministic, and unknown fields may appear.
//! See [`export::schema`](crate::export::schema) for the whole of them.

use serde::Serialize;
use serde::ser::{SerializeSeq, Serializer};

use crate::diff::host::Reassessment;
use crate::diff::{
    CertificateChange, Confirmed, Coverage, DiffSummary, HostChange, HostDelta, PortChange,
    PortDelta, Presence, ScanDiff, SecurityChange, ServiceChange, Significance,
};
use crate::export::schema::{EngineDto, HostDto};
use crate::export::{ExportOptions, HostRedaction};
use crate::format::time::rfc3339;
use crate::model::finding::Finding;
use crate::model::host::os::OsFingerprint;
use crate::model::host::{HostName, IpProtocolState, ip_protocol_name};
use crate::record::wire::{
    host_status_name, ip_protocol_state_name, name_kind_name, name_source_name, port_state_name,
    protocol_name, scan_kind_name,
};

pub use crate::format::{DIFF_SCHEMA_VERSION, ENGINE_NAME};

// ---------------------------------------------------------------------------
// The names a change is known by
//
// Public because they are the contract: an alerting rule keys on one of these
// strings, and a front end printing a comparison prints the same ones.
// ---------------------------------------------------------------------------

/// Whether a host or an endpoint is in one scan or both.
pub fn presence_name(presence: Presence) -> &'static str {
    match presence {
        Presence::Both => "both",
        Presence::Added { .. } => "added",
        Presence::Removed { .. } => "removed",
    }
}

/// What a report says about having walked a target.
pub fn coverage_name(coverage: Coverage) -> &'static str {
    match coverage {
        Coverage::Covered => "covered",
        Coverage::Withheld => "withheld",
        Coverage::OutOfScope => "out_of_scope",
        Coverage::Unreached => "unreached",
        Coverage::Unstated => "unstated",
    }
}

/// How much a change is worth somebody's attention.
pub fn significance_name(significance: Significance) -> &'static str {
    match significance {
        Significance::Routine => "routine",
        Significance::Notable => "notable",
        Significance::Urgent => "urgent",
    }
}

// ---------------------------------------------------------------------------
// The document
// ---------------------------------------------------------------------------

/// The root of a comparison document.
///
/// Borrows the comparison rather than copying it, and hands its host deltas to
/// [`HostDeltasDto`], which renders one at a time. A delta carries both sides'
/// whole records, so collecting them would hold a second copy of both reports on
/// top of the comparison they came from.
#[non_exhaustive]
#[derive(Debug, Serialize)]
pub struct DiffDto<'a> {
    /// The version of this document's shape. Counted apart from the report's.
    pub schema_version: u32,
    /// Which build wrote the document. Which produced either scan is `baseline`
    /// and `current`.
    pub engine: EngineDto,
    /// When the comparison was taken.
    pub generated_at: String,
    /// The earlier scan.
    pub baseline: ProvenanceDto,
    /// The later scan.
    pub current: ProvenanceDto,
    /// Whether the two scans describe the same network.
    ///
    /// Derivable from an empty `hosts`, and stated because it is the first
    /// question every consumer asks.
    pub unchanged: bool,
    /// How much the strongest change anywhere below is worth somebody's
    /// attention: `routine`, `notable` or `urgent`.
    ///
    /// The field a scheduled comparison is triaged by. `routine` for a document
    /// where nothing moved, since nothing to do and nothing worth doing rank the
    /// same; `unchanged` is what separates them.
    pub significance: &'static str,
    /// Counts over everything below.
    pub summary: SummaryDto,
    /// Every host that differs, ascending by address. Hosts that did not are
    /// not here.
    pub hosts: HostDeltasDto<'a>,
}

/// The comparison's host deltas, serialized one at a time.
///
/// The counterpart of the report document's host array. Each delta carries the
/// whole record from each side that has one, so collecting them would hold a
/// second copy of both reports on top of the comparison they came from. One is
/// rendered, written and dropped before the next is built.
#[non_exhaustive]
#[derive(Debug)]
pub struct HostDeltasDto<'a> {
    deltas: &'a [HostDelta],
    options: &'a ExportOptions,
}

impl<'a> HostDeltasDto<'a> {
    /// The deltas this will render, for a caller that wants them typed rather
    /// than serialized.
    pub fn deltas(&self) -> &'a [HostDelta] {
        self.deltas
    }

    /// How many hosts differ.
    pub fn len(&self) -> usize {
        self.deltas.len()
    }

    /// Whether the two scans describe the same network.
    pub fn is_empty(&self) -> bool {
        self.deltas.is_empty()
    }
}

impl Serialize for HostDeltasDto<'_> {
    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        let mut seq = serializer.serialize_seq(Some(self.deltas.len()))?;
        for delta in self.deltas {
            seq.serialize_element(&HostDeltaDto::new(delta, self.options))?;
        }
        seq.end()
    }
}

impl<'a> DiffDto<'a> {
    /// Renders a comparison, applying the redaction policy in `options`.
    pub fn new(diff: &'a ScanDiff, options: &'a ExportOptions) -> Self {
        Self {
            schema_version: DIFF_SCHEMA_VERSION,
            engine: EngineDto {
                name: ENGINE_NAME,
                version: crate::report::ENGINE_VERSION,
            },
            generated_at: rfc3339(std::time::SystemTime::now()),
            baseline: ProvenanceDto::new(diff.baseline()),
            current: ProvenanceDto::new(diff.current()),
            unchanged: diff.is_empty(),
            significance: significance_name(diff.significance()),
            summary: SummaryDto::new(&diff.summary()),
            hosts: HostDeltasDto {
                deltas: diff.hosts(),
                options,
            },
        }
    }
}

/// Which scan one side of the comparison was.
#[non_exhaustive]
#[derive(Debug, Serialize)]
pub struct ProvenanceDto {
    /// The engine that produced the report, as it attributed itself. A report
    /// built from another tool's output says so, giving `nmap 7.94` rather than
    /// this crate.
    pub engine_version: String,
    /// The moment the scan is judged to have happened, and the moment its
    /// certificates were judged against.
    pub at: String,
    /// How many hosts the report held.
    pub hosts: usize,
    /// Which phases it recorded, in the order they ran.
    pub kinds: Vec<&'static str>,
    /// Whether the report says what it covered.
    ///
    /// `false` makes every coverage answer about this side `unstated`, so
    /// nothing appearing or disappearing against it can be confirmed.
    pub states_scope: bool,
}

impl ProvenanceDto {
    /// Renders one side's provenance.
    pub fn new(provenance: &crate::diff::Provenance) -> Self {
        Self {
            engine_version: provenance.engine_version().to_owned(),
            at: rfc3339(provenance.at()),
            hosts: provenance.hosts(),
            kinds: provenance
                .kinds()
                .iter()
                .copied()
                .map(scan_kind_name)
                .collect(),
            states_scope: provenance.states_scope(),
        }
    }
}

/// A count, and how much of it the other scan is known to have looked for.
#[non_exhaustive]
#[derive(Debug, Serialize)]
pub struct ConfirmedDto {
    /// How many, whatever the other scan covered.
    pub total: usize,
    /// How many the other scan is known to have covered. A consumer alerting on
    /// one of these numbers should alert on this one.
    pub confirmed: usize,
}

impl ConfirmedDto {
    /// Renders a split count.
    pub fn new(count: Confirmed) -> Self {
        Self {
            total: count.total,
            confirmed: count.confirmed,
        }
    }
}

/// Counts over the whole comparison.
#[non_exhaustive]
#[derive(Debug, Serialize)]
pub struct SummaryDto {
    /// Hosts only the later scan has a record for.
    pub hosts_added: ConfirmedDto,
    /// Hosts only the earlier scan has a record for.
    pub hosts_removed: ConfirmedDto,
    /// Hosts both scans have, that differ.
    pub hosts_changed: usize,
    /// Endpoints accepting connections now that were not before.
    pub ports_opened: ConfirmedDto,
    /// Endpoints that were accepting connections and are not now.
    pub ports_closed: ConfirmedDto,
    /// Endpoints both scans have, that differ.
    pub ports_changed: usize,
    /// Endpoints where what is listening changed, or was identified where it
    /// was not.
    pub services_changed: usize,
    /// Endpoints presenting a different certificate than before.
    pub certificates_rotated: usize,
    /// Endpoints whose certificate is now inside the expiry threshold and was
    /// not when the earlier scan ran.
    pub certificates_expiring: usize,
    /// Endpoints whose certificate has lapsed since the earlier scan.
    pub certificates_expired: usize,
}

impl SummaryDto {
    /// Renders the derived counts.
    pub fn new(summary: &DiffSummary) -> Self {
        Self {
            hosts_added: ConfirmedDto::new(summary.hosts_added),
            hosts_removed: ConfirmedDto::new(summary.hosts_removed),
            hosts_changed: summary.hosts_changed,
            ports_opened: ConfirmedDto::new(summary.ports_opened),
            ports_closed: ConfirmedDto::new(summary.ports_closed),
            ports_changed: summary.ports_changed,
            services_changed: summary.services_changed,
            certificates_rotated: summary.certificates_rotated,
            certificates_expiring: summary.certificates_expiring,
            certificates_expired: summary.certificates_expired,
        }
    }
}

/// One host, as the two scans between them hold it.
#[non_exhaustive]
#[derive(Debug, Serialize)]
pub struct HostDeltaDto<'a> {
    /// The address the host is reported under: the later scan's primary where it
    /// has a record, and the earlier scan's where it does not.
    pub address: String,
    /// `both`, `added` or `removed`.
    pub presence: &'static str,
    /// What the scan *lacking* a record says about having covered this address.
    /// `null` when both hold one, where the question does not arise.
    pub coverage: Option<&'static str>,
    /// Whether this is a finding about the network rather than about the scan.
    ///
    /// True when both scans hold a record, and when the one that does not is
    /// known to have covered the address anyway.
    pub confirmed: bool,
    /// How much the strongest change on this host, or on any of its endpoints, is
    /// worth somebody's attention: `routine`, `notable` or `urgent`.
    ///
    /// The field to alert on, and already `routine` where `confirmed` is false,
    /// so a rule keying on this one does not have to read that one as well.
    pub significance: &'static str,
    /// How many records each scan held for this host. `{1, 1}` ordinarily.
    pub records: RecordsDto,
    /// Whether the two scans grouped this host's addresses differently: what one
    /// holds as a single record the other holds as several. Both sides are still
    /// compared, merged.
    pub regrouped: bool,
    /// What moved about the host itself.
    pub changes: Vec<ChangeDto>,
    /// Every endpoint that moved, ascending by number and then transport.
    pub ports: Vec<PortDeltaDto>,
    /// The earlier scan's whole record, in the report document's schema.
    ///
    /// Its text is masked by every name either scan knew the host by, as the
    /// changes are: the earlier scan can hold a reply naming the machine
    /// before any service stated the name, and the later one knows it.
    pub baseline: Option<HostDto<'a>>,
    /// The later scan's whole record, masked the same way.
    pub current: Option<HostDto<'a>>,
}

impl<'a> HostDeltaDto<'a> {
    /// Renders one host's comparison.
    pub fn new(delta: &'a HostDelta, options: &ExportOptions) -> Self {
        let (baseline_records, current_records) = delta.records();
        let masking = options.redaction.for_delta(delta);

        Self {
            address: delta.address().to_string(),
            presence: presence_name(delta.presence()),
            coverage: delta.presence().counterpart_coverage().map(coverage_name),
            confirmed: delta.presence().is_confirmed(),
            significance: significance_name(delta.significance()),
            records: RecordsDto {
                baseline: baseline_records,
                current: current_records,
            },
            regrouped: delta.is_regrouped(),
            changes: delta
                .changes()
                .iter()
                .flat_map(|change| ChangeDto::of_host(change, &masking))
                .collect(),
            ports: delta
                .ports()
                .iter()
                .map(|port| PortDeltaDto::new(port, &masking))
                .collect(),
            baseline: delta
                .baseline()
                .map(|host| HostDto::masked(host, options, &masking)),
            current: delta
                .current()
                .map(|host| HostDto::masked(host, options, &masking)),
        }
    }
}

/// How many records each scan held for one host.
#[non_exhaustive]
#[derive(Debug, Serialize)]
pub struct RecordsDto {
    /// The earlier scan's count.
    pub baseline: usize,
    /// The later scan's count.
    pub current: usize,
}

/// One endpoint, as the two scans between them hold it.
#[non_exhaustive]
#[derive(Debug, Serialize)]
pub struct PortDeltaDto {
    /// The port number, the same in both scans.
    pub port: u16,
    /// The transport, the same in both scans.
    pub protocol: &'static str,
    /// `both`, `added` or `removed`.
    pub presence: &'static str,
    /// What the scan lacking a record says about having probed this endpoint.
    /// `null` when both hold one.
    pub coverage: Option<&'static str>,
    /// Whether this is a finding about the network.
    pub confirmed: bool,
    /// How much the strongest change on this endpoint is worth somebody's
    /// attention: `routine`, `notable` or `urgent`. The field to alert on, and
    /// already `routine` where `confirmed` is false.
    pub significance: &'static str,
    /// Whether the endpoint accepts connections now and did not before.
    pub opened: bool,
    /// Whether it accepted connections before and does not now.
    pub closed: bool,
    /// What moved about the endpoint.
    pub changes: Vec<ChangeDto>,
}

impl PortDeltaDto {
    /// Renders one endpoint's comparison, on the host `masking` was made for.
    pub fn new(delta: &PortDelta, masking: &HostRedaction) -> Self {
        Self {
            port: delta.number(),
            protocol: protocol_name(delta.protocol()),
            presence: presence_name(delta.presence()),
            coverage: delta.presence().counterpart_coverage().map(coverage_name),
            confirmed: delta.presence().is_confirmed(),
            significance: significance_name(delta.significance()),
            opened: delta.is_opened(),
            closed: delta.is_closed(),
            changes: delta
                .changes()
                .iter()
                .flat_map(|change| ChangeDto::of_port(change, masking))
                .collect(),
        }
    }
}

/// One field that moved, as one scalar fact.
///
/// The document's unit of change, and the vocabulary a rule keys on. A set that
/// gained two members produces two of these rather than one carrying a list. See
/// the [module documentation](super) for why.
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct ChangeDto {
    /// Which field moved, by wire name. The whole list is in
    /// [`of_host`](Self::of_host) and [`of_port`](Self::of_port).
    pub kind: &'static str,
    /// What the earlier scan found. `null` where it found nothing: a value
    /// gained, a service identified, a certificate first presented.
    pub before: Option<String>,
    /// What the later scan found. `null` where it finds nothing: a value lost, a
    /// service no longer identified, a certificate withdrawn.
    pub after: Option<String>,
}

impl ChangeDto {
    /// A change with both values.
    fn between(kind: &'static str, before: impl Into<String>, after: impl Into<String>) -> Self {
        Self {
            kind,
            before: Some(before.into()),
            after: Some(after.into()),
        }
    }

    /// A value the later scan found and the earlier did not.
    fn gained(kind: &'static str, after: impl Into<String>) -> Self {
        Self {
            kind,
            before: None,
            after: Some(after.into()),
        }
    }

    /// A value the earlier scan found and the later does not.
    fn lost(kind: &'static str, before: impl Into<String>) -> Self {
        Self {
            kind,
            before: Some(before.into()),
            after: None,
        }
    }

    /// A pair of optionals, as however many changes it amounts to.
    fn optional(kind: &'static str, before: Option<&str>, after: Option<&str>) -> Vec<Self> {
        match (before, after) {
            (None, None) => Vec::new(),
            (None, Some(after)) => vec![Self::gained(kind, after)],
            (Some(before), None) => vec![Self::lost(kind, before)],
            (Some(before), Some(after)) => vec![Self::between(kind, before, after)],
        }
    }

    /// A set's arrivals and departures, one change each.
    fn set<T: ToString>(
        gained_kind: &'static str,
        lost_kind: &'static str,
        gained: &[T],
        lost: &[T],
    ) -> Vec<Self> {
        gained
            .iter()
            .map(|value| Self::gained(gained_kind, value.to_string()))
            .chain(
                lost.iter()
                    .map(|value| Self::lost(lost_kind, value.to_string())),
            )
            .collect()
    }

    /// What a host-level change amounts to, in this document's vocabulary.
    ///
    /// | `kind` | |
    /// |---|---|
    /// | `status` | whether the host answers |
    /// | `hostname` | its resolved name |
    /// | `name_gained`, `name_lost` | one name it gave for itself each, as `ntlm domain: corp.example` |
    /// | `address_gained`, `address_lost` | one address each |
    /// | `os` | what it was identified as running |
    /// | `mac_gained`, `mac_lost` | one hardware address each |
    /// | `vendor` | the vendor its hardware address resolves to |
    /// | `role_gained`, `role_lost` | one inferred role each |
    /// | `filtering_gained`, `filtering_lost` | one conclusion about the filter in front of it each |
    /// | `ip_protocol` | one IP protocol its stack takes delivery of, or no longer does |
    /// | `finding_appeared`, `finding_resolved` | one finding each, as its severity and title |
    /// | `finding_reassessed` | one claim both scans make, graded differently |
    ///
    /// Never `finding_unsettled`. The evidence a later scan can leave a claim
    /// unsettled on, a TLS walk or a certificate, belongs to an endpoint, so
    /// only [`of_port`](Self::of_port) emits it.
    ///
    /// Matched exhaustively and with no wildcard, so a variant added to
    /// [`HostChange`] stops this compiling until somebody decides what it is
    /// called on the wire, as [`export::schema`](crate::export::schema) does for
    /// the report document.
    ///
    /// `masking` is made for the host the change is on, from both of its
    /// records ([`Redaction::for_delta`](crate::export::Redaction::for_delta)).
    /// A name and a hardware address are masked as their fields are anywhere,
    /// and every value then goes through [`HostRedaction::text`], since an
    /// operating system's name and a finding's title are the host's words.
    pub fn of_host(change: &HostChange, masking: &HostRedaction) -> Vec<Self> {
        let redaction = masking.redaction();

        let changes = match change {
            HostChange::Status(status) => vec![Self::between(
                "status",
                host_status_name(status.before),
                host_status_name(status.after),
            )],
            HostChange::Hostname(name) => Self::optional(
                "hostname",
                name.before
                    .as_deref()
                    .map(|n| redaction.hostname(n))
                    .as_deref(),
                name.after
                    .as_deref()
                    .map(|n| redaction.hostname(n))
                    .as_deref(),
            ),
            // The protocol and the kind lead, spelled as the report spells them,
            // so a rule can key on `ntlm domain:` without parsing the name, and
            // the name alone is masked, as it is everywhere a report carries it.
            HostChange::Names { gained, lost } => {
                let describe = |name: &HostName| {
                    format!(
                        "{} {}: {}",
                        name_source_name(name.source()),
                        name_kind_name(name.kind()),
                        redaction.hostname(name.name())
                    )
                };
                let gained: Vec<String> = gained.iter().map(describe).collect();
                let lost: Vec<String> = lost.iter().map(describe).collect();
                Self::set("name_gained", "name_lost", &gained, &lost)
            }
            HostChange::Addresses { gained, lost } => {
                Self::set("address_gained", "address_lost", gained, lost)
            }
            HostChange::Os(os) => Self::optional(
                "os",
                os.before.as_ref().map(identify).as_deref(),
                os.after.as_ref().map(identify).as_deref(),
            ),
            HostChange::Macs { gained, lost } => {
                let gained: Vec<String> = gained.iter().map(|mac| redaction.mac(mac)).collect();
                let lost: Vec<String> = lost.iter().map(|mac| redaction.mac(mac)).collect();
                Self::set("mac_gained", "mac_lost", &gained, &lost)
            }
            HostChange::Vendor(vendor) => {
                Self::optional("vendor", vendor.before.as_deref(), vendor.after.as_deref())
            }
            HostChange::Roles { gained, lost } => {
                let name = crate::record::wire::network_role_name;
                let gained: Vec<&'static str> = gained.iter().copied().map(name).collect();
                let lost: Vec<&'static str> = lost.iter().copied().map(name).collect();
                Self::set("role_gained", "role_lost", &gained, &lost)
            }
            HostChange::Filtering { gained, lost } => {
                let name = crate::record::wire::filtering_name;
                let gained: Vec<&'static str> = gained.iter().copied().map(name).collect();
                let lost: Vec<&'static str> = lost.iter().copied().map(name).collect();
                Self::set("filtering_gained", "filtering_lost", &gained, &lost)
            }
            // One change per protocol, so a rule keys on the number rather than
            // parsing a list, which is the same flattening every set change here
            // gets. The number leads the value because it is the subject: a
            // consumer alerting on `47` wants the line to say 47.
            HostChange::IpProtocols { changed } => changed
                .iter()
                .map(|moved| {
                    Self::between(
                        "ip_protocol",
                        Self::describe_ip_protocol(moved.protocol, moved.state.before),
                        Self::describe_ip_protocol(moved.protocol, moved.state.after),
                    )
                })
                .collect(),
            HostChange::Findings {
                appeared,
                resolved,
                reassessed,
            } => Self::findings(appeared, resolved, &[], reassessed),
        };
        Self::masked(changes, masking)
    }

    /// `changes` with each value read through `masking`.
    fn masked(changes: Vec<Self>, masking: &HostRedaction) -> Vec<Self> {
        let mask = |value: Option<String>| {
            value.map(|value| match masking.text(&value) {
                std::borrow::Cow::Borrowed(_) => value,
                std::borrow::Cow::Owned(masked) => masked,
            })
        };
        changes
            .into_iter()
            .map(|change| Self {
                kind: change.kind,
                before: mask(change.before),
                after: mask(change.after),
            })
            .collect()
    }

    /// One IP protocol verdict, as `47 gre: open`.
    ///
    /// The number, its registry keyword where it has one, and the verdict, in one
    /// string. A change is a pair of these, so a consumer reads what moved
    /// without holding the protocol number from another field.
    fn describe_ip_protocol(number: u8, state: IpProtocolState) -> String {
        let verdict = ip_protocol_state_name(state);
        match ip_protocol_name(number) {
            Some(name) => format!("{number} {name}: {verdict}"),
            None => format!("{number}: {verdict}"),
        }
    }

    /// Findings gained and lost, as one entry each.
    ///
    /// Rendered as severity and title rather than the whole finding: this
    /// document says what moved, and the report the diff was taken over carries
    /// the evidence.
    ///
    /// A claim the later scan did not settle is its own kind rather than a
    /// resolution, so a rule written against `finding_resolved` fires on a fix
    /// and never on a walk the later scan cut short, or a certificate it was
    /// not shown.
    fn findings(
        appeared: &[Finding],
        resolved: &[Finding],
        unsettled: &[Finding],
        reassessed: &[Reassessment],
    ) -> Vec<Self> {
        let name = crate::record::wire::severity_name;
        let describe =
            |finding: &Finding| format!("{}: {}", name(finding.severity()), finding.title());

        let gained: Vec<String> = appeared.iter().map(describe).collect();
        let lost: Vec<String> = resolved.iter().map(describe).collect();

        let mut entries = Self::set("finding_appeared", "finding_resolved", &gained, &lost);
        entries.extend(
            unsettled
                .iter()
                .map(|finding| Self::lost("finding_unsettled", describe(finding))),
        );
        entries.extend(reassessed.iter().map(|shift| {
            Self::between(
                "finding_reassessed",
                format!("{}: {}", name(shift.severity.before), shift.finding.title()),
                format!("{}: {}", name(shift.severity.after), shift.finding.title()),
            )
        }));
        entries
    }

    /// What an endpoint-level change amounts to.
    ///
    /// | `kind` | |
    /// |---|---|
    /// | `port_state` | the verdict |
    /// | `service_identified`, `service_lost` | something was identified here, or no longer is |
    /// | `service_name`, `service_product`, `service_vendor`, `service_version`, `service_extrainfo` | one field of it |
    /// | `cpe_gained`, `cpe_lost` | one platform identifier each |
    /// | `tls_version`, `cipher_suite` | what was negotiated |
    /// | `alpn_gained`, `alpn_lost` | one application protocol each |
    /// | `certificate_presented`, `certificate_withdrawn`, `certificate_rotated` | by SHA-256 fingerprint |
    /// | `certificate_expiring`, `certificate_expired` | a threshold crossed since the earlier scan; `after` is the validity end |
    /// | `finding_appeared`, `finding_resolved` | one finding each, as its severity and title |
    /// | `finding_unsettled` | one claim the earlier scan made that the later one neither makes nor settled |
    /// | `finding_reassessed` | one claim both scans make, graded differently |
    ///
    /// Every value goes through `masking`, made as for
    /// [`of_host`](Self::of_host): a service's product and extra information
    /// and a finding's title are filled from the host's replies, and can name
    /// it. A certificate change is rendered by fingerprint rather than subject,
    /// so it names nothing to mask.
    pub fn of_port(change: &PortChange, masking: &HostRedaction) -> Vec<Self> {
        let changes = match change {
            PortChange::State(state) => vec![Self::between(
                "port_state",
                port_state_name(state.before),
                port_state_name(state.after),
            )],
            PortChange::Service(service) => Self::of_service(service),
            PortChange::Security(security) => Self::of_security(security),
            PortChange::Findings {
                appeared,
                resolved,
                unsettled,
                reassessed,
            } => Self::findings(appeared, resolved, unsettled, reassessed),
        };
        Self::masked(changes, masking)
    }

    fn of_service(change: &ServiceChange) -> Vec<Self> {
        match change {
            ServiceChange::Identified(service) => {
                vec![Self::gained("service_identified", describe(service))]
            }
            ServiceChange::Unidentified(service) => {
                vec![Self::lost("service_lost", describe(service))]
            }
            ServiceChange::Name(name) => {
                vec![Self::between("service_name", &name.before, &name.after)]
            }
            ServiceChange::Product(value) => Self::optional(
                "service_product",
                value.before.as_deref(),
                value.after.as_deref(),
            ),
            ServiceChange::Vendor(value) => Self::optional(
                "service_vendor",
                value.before.as_deref(),
                value.after.as_deref(),
            ),
            ServiceChange::Version(value) => Self::optional(
                "service_version",
                value.before.as_deref(),
                value.after.as_deref(),
            ),
            ServiceChange::ExtraInfo(value) => Self::optional(
                "service_extrainfo",
                value.before.as_deref(),
                value.after.as_deref(),
            ),
            ServiceChange::Cpes { gained, lost } => {
                Self::set("cpe_gained", "cpe_lost", gained, lost)
            }
        }
    }

    fn of_security(change: &SecurityChange) -> Vec<Self> {
        match change {
            SecurityChange::TlsVersion(value) => Self::optional(
                "tls_version",
                value.before.as_deref(),
                value.after.as_deref(),
            ),
            SecurityChange::CipherSuite(value) => Self::optional(
                "cipher_suite",
                value.before.as_deref(),
                value.after.as_deref(),
            ),
            SecurityChange::Alpn { gained, lost } => {
                Self::set("alpn_gained", "alpn_lost", gained, lost)
            }
            SecurityChange::Certificate(certificate) => Self::of_certificate(certificate),
        }
    }

    /// A certificate is identified by its fingerprint, so that is what the
    /// values carry: two certificates are the same one exactly when they are
    /// byte for byte the same.
    fn of_certificate(change: &CertificateChange) -> Vec<Self> {
        match change {
            CertificateChange::Presented(certificate) => vec![Self::gained(
                "certificate_presented",
                certificate.fingerprint_sha256(),
            )],
            CertificateChange::Withdrawn(certificate) => vec![Self::lost(
                "certificate_withdrawn",
                certificate.fingerprint_sha256(),
            )],
            CertificateChange::Rotated { before, after } => vec![Self::between(
                "certificate_rotated",
                before.fingerprint_sha256(),
                after.fingerprint_sha256(),
            )],
            // The certificate did not move, the clock did. `after` is when it
            // lapses, absolute, so a consumer picks its own window.
            CertificateChange::Expiring { certificate, .. } => vec![Self::gained(
                "certificate_expiring",
                rfc3339(certificate.validity_end()),
            )],
            CertificateChange::Expired { certificate, .. } => vec![Self::gained(
                "certificate_expired",
                rfc3339(certificate.validity_end()),
            )],
        }
    }
}

/// An operating system as one line, for a value in a change.
///
/// The whole fingerprint is in the host records on either side; this is what a
/// person reads in an alert.
fn identify(os: &OsFingerprint) -> String {
    // A name carrying a digit already says which version, and appending the
    // generation gives "Linux 5.0 - 5.14 5.X". A bare family name does not, and
    // "Linux" alone is worth less than "Linux 6.1.0". Both shapes reach this,
    // from different fingerprinters.
    match os.generation() {
        Some(generation)
            if !os.name().contains(generation)
                && !os.name().contains(|c: char| c.is_ascii_digit()) =>
        {
            format!("{} {generation}", os.name())
        }
        _ => os.name().to_owned(),
    }
}

/// A service as one line, likewise.
fn describe(service: &crate::model::port::Service) -> String {
    let mut described = service.name().to_owned();

    // A fingerprint that recognised the protocol and nothing more names the
    // product after the protocol, and "http http" reads as a mistake.
    if let Some(product) = service
        .product()
        .filter(|p| !p.eq_ignore_ascii_case(&described))
    {
        described.push(' ');
        described.push_str(product);
    }
    if let Some(version) = service.version() {
        described.push(' ');
        described.push_str(version);
    }
    described
}