zond-engine 0.14.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
// 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

//! # Combining what several sources say about one host
//!
//! A stack's shape is one way to learn what a machine runs. A banner it
//! volunteered is another, and the hardware address it answered from is a third.
//! They are read from different places, fail in different ways, and this is where
//! they are put together.
//!
//! ## The independence claim, and where it stops
//!
//! Combining evidence means assuming the pieces are independent, and that
//! assumption is *false* within a single reply: the hop counter, the window and
//! the option layout of one packet are consequences of one stack build and agree
//! with each other by construction. Scoring them separately would triple-count
//! one observation.
//!
//! So [`classify`](super::classify) already collapses a whole reply into
//! one item. What arrives here is one item per genuinely distinct source,
//! and independence is claimed only between those, a stack, a banner, a
//! hardware address, where it is close enough to true to build on.
//!
//! That is also why the arithmetic below is [noisy-OR] rather than a sum. Two
//! sources agreeing raise confidence without either being trusted more than it
//! deserves, and no amount of agreement reaches certainty.
//!
//! [noisy-OR]: https://en.wikipedia.org/wiki/Noisy-or_model
//!
//! ## Two axes, because they are two questions
//!
//! What a machine *runs* and what it *is* are independent, and a source may
//! know either without the other. A hop counter says infrastructure and never a
//! vendor; an SNMP agent names a printer down to its firmware and never its
//! kernel. So the family is one axis, the device class another, and a source
//! with nothing to say on either simply says nothing there; see
//! [`OsEvidence::family`].
//!
//! ## Disagreement lowers the answer rather than picking a winner
//!
//! When sources name different families, the leader is reduced by whatever the
//! runner-up carries. Two sources at odds are a worse position than one source
//! alone, and a resolver that took the larger and reported it at full strength
//! would be hiding the conflict at exactly the moment it matters. Below the floor
//! the result is no answer, which is the honest outcome for a host two techniques
//! disagree about.

use crate::model::host::OsEvidence;
use std::collections::BTreeMap;

use super::verdict::{MIN_REPORTABLE_ACCURACY, OsVerdict};

/// The most any combination of sources may claim.
///
/// Noisy-OR approaches certainty without reaching it, which is the right shape
/// and not enough on its own: at twenty agreeing sources the arithmetic lands
/// close enough that rounding to a percentage produces 100, and a scan reporting
/// an operating system as certain on accumulated inference would be claiming
/// something none of its sources said.
///
/// Above the 85 that marks high confidence, so agreement between genuinely
/// independent sources can still get there, that is the whole point of having
/// more than one. Below 100, which stays reserved for a host that identified
/// itself rather than one that was worked out.
pub const MAX_FUSED_ACCURACY: u8 = 95;

/// Folds every source's opinion into one answer, or none.
///
/// Returns `None` when there is nothing to go on, when the sources disagree
/// badly enough that what survives falls below [`MIN_REPORTABLE_ACCURACY`], or
/// when what survives says nothing about the software.
///
/// # Abstention is not dissent
///
/// Only the sources that [name a family](OsEvidence::family) vote on it. The
/// rest fold their finer parts into whichever family wins and never count
/// against it, which is the difference between a second opinion and a second
/// question. A hop counter of 255 says *network device*; an SNMP agent saying
/// `Brother NC-8700w` says which one. Scored as rival families those two
/// readings annihilated each other and the host was reported as nothing, on
/// real hardware, with both answers sitting in the record.
///
/// Where nobody names a family the abstentions are the whole answer, and it is
/// reported without one.
pub fn resolve(evidence: Vec<OsEvidence>) -> Option<OsVerdict> {
    // Grouped by family, because that is the level a source that has one can
    // speak to. A `BTreeMap` so two runs over the same evidence resolve the same
    // way: with scores this close together, iteration order would otherwise
    // decide ties.
    let mut by_family: BTreeMap<&str, Vec<&OsEvidence>> = BTreeMap::new();
    let mut abstained: Vec<&OsEvidence> = Vec::new();
    for item in &evidence {
        match item.family.as_deref() {
            Some(family) => by_family.entry(family).or_default().push(item),
            None => abstained.push(item),
        }
    }

    let mut scored: Vec<(&str, f32, Vec<&OsEvidence>)> = by_family
        .into_iter()
        .map(|(family, items)| {
            let score = combine(items.iter().map(|item| item.confidence));
            (family, score, items)
        })
        .collect();
    // Descending by score, and by family name where scores tie, so the answer
    // does not depend on which source happened to be added first.
    scored.sort_by(|a, b| {
        b.1.partial_cmp(&a.1)
            .unwrap_or(std::cmp::Ordering::Equal)
            .then(a.0.cmp(b.0))
    });

    // Everything naming a *different* family, combined, is what the leader has to
    // survive. One dissenting source is a doubt; several agreeing with each other
    // against the leader is close to a refutation.
    let mut answer = scored.first().map(|(family, score, items)| {
        let against = combine(scored.iter().skip(1).map(|(_, score, _)| *score));
        (
            Some(*family),
            score * (1.0 - against),
            against,
            items.clone(),
        )
    });

    // A family that cannot clear the floor is not an answer, but it is also not
    // the only thing on the table. What abstained never competed for it, was
    // never reduced by the dissent that sank it, and can still be worth
    // reporting on its own, an agent naming a make and model outright while two
    // stack rules argue about what class of box it is. Dropping the whole
    // verdict there would discard the best-attested thing the scan learned
    // because of a quarrel it took no part in.
    if answer
        .as_ref()
        .is_none_or(|(_, survived, ..)| percent(*survived) < MIN_REPORTABLE_ACCURACY)
        && !abstained.is_empty()
    {
        let alone = combine(abstained.iter().map(|item| item.confidence));
        if answer
            .as_ref()
            .is_none_or(|(_, survived, ..)| alone > *survived)
        {
            answer = Some((None, alone, 0.0, Vec::new()));
        }
    }

    let (family, survived, against, mut items) = answer?;
    items.extend_from_slice(&abstained);

    let accuracy = percent(survived);
    if accuracy < MIN_REPORTABLE_ACCURACY {
        return None;
    }

    // The finer parts of the path are kept where every source that *spoke to*
    // them agrees. A source saying nothing about a version abstains rather than
    // dissenting: a stack rule can name a family and never a release, so
    // counting its silence as disagreement would mean a banner that read
    // "Ubuntu 22.04" off the wire loses it the moment a stack rule corroborates
    // the family, which is more evidence producing a less specific answer. Two
    // sources naming different values still yield nothing.
    let agreed = |part: fn(&OsEvidence) -> &Option<String>| -> Option<String> {
        let mut stated = items.iter().filter_map(|item| part(item).as_deref());
        let candidate = stated.next()?;
        stated
            .all(|other| other == candidate)
            .then(|| candidate.to_owned())
    };

    // Attributed to whichever source contributed most, since that is the one a
    // reader would want to argue with first.
    let strongest = items
        .iter()
        .max_by(|a, b| {
            a.confidence
                .partial_cmp(&b.confidence)
                .unwrap_or(std::cmp::Ordering::Equal)
        })
        .copied()?;

    let mut lines: Vec<&str> = items.iter().map(|item| item.evidence.as_str()).collect();
    lines.sort_unstable();
    lines.dedup();

    let (vendor, product, version, kernel, arch, cpe, device) = (
        agreed(|item| &item.vendor),
        agreed(|item| &item.product),
        agreed(|item| &item.version),
        agreed(|item| &item.kernel),
        agreed(|item| &item.arch),
        agreed(|item| &item.cpe),
        agreed(|item| &item.device),
    );

    // Something has to have been established. Every one of these is a real
    // answer on its own: a device class says the box is infrastructure, which is
    // the most a hop counter of 255 can support and is worth reporting about a
    // host nothing else describes.
    //
    // This guard used to exclude a device class, on the reading that a source
    // knowing only what kind of box this is has identified no software. That was
    // true while a class could only arrive beside a product, from a text rule.
    // It stopped being true when the rules that read a hop counter began stating
    // a class instead of writing one into the family field.
    if family.is_none() && device.is_none() && vendor.is_none() && product.is_none() {
        return None;
    }

    // What the finer parts are worth, as distinct from the family.
    //
    // Every source can speak to a family, and `accuracy` above is their
    // agreement about it. A release is usually named by exactly one of them, so
    // reporting it under that figure launders one weaker claim through the
    // agreement of several stronger ones, measured, on a real host: two sources
    // agreeing on Linux scored 84 while the release rested on a single banner
    // worth 55.
    //
    // Combined over the sources that actually stated something finer, and
    // reduced by the same dissent the family had to survive: a contested family
    // does not leave its release uncontested.
    let refined = vendor.is_some()
        || product.is_some()
        || version.is_some()
        || kernel.is_some()
        || arch.is_some()
        || cpe.is_some()
        || device.is_some();
    let detail_accuracy = refined.then(|| {
        let stated = items
            .iter()
            .filter(|item| {
                item.vendor.is_some()
                    || item.product.is_some()
                    || item.version.is_some()
                    || item.kernel.is_some()
                    || item.arch.is_some()
                    || item.cpe.is_some()
                    || item.device.is_some()
            })
            .map(|item| item.confidence);

        percent(combine(stated) * (1.0 - against))
    });

    Some(OsVerdict {
        family: family.map(ToOwned::to_owned),
        device,
        vendor,
        product,
        version,
        kernel,
        arch,
        cpe,
        accuracy,
        detail_accuracy,
        source: strongest.source,
        evidence: lines.join(" | "),
    })
}

/// A combined probability on the `0..=100` scale a report states, never above
/// what any amount of agreement is allowed to claim.
fn percent(probability: f32) -> u8 {
    (probability * 100.0)
        .round()
        .clamp(0.0, f32::from(MAX_FUSED_ACCURACY)) as u8
}

/// Combines independent probabilities for one hypothesis: the chance that *at
/// least one* of them is right.
///
/// `1 - Π(1 - p)`. Two sources at 0.5 give 0.75 rather than 1.0, and nothing
/// short of a certain source ever reaches 1, which is the property that
/// matters, because a stack of agreeing guesses must not become a fact.
fn combine(confidences: impl Iterator<Item = f32>) -> f32 {
    let doubt = confidences
        .map(|confidence| 1.0 - confidence.clamp(0.0, 1.0))
        .product::<f32>();
    1.0 - doubt
}

// ╔════════════════════════════════════════════╗
// ║ ████████╗███████╗███████╗████████╗███████╗ ║
// ║ ╚══██╔══╝██╔════╝██╔════╝╚══██╔══╝██╔════╝ ║
// ║    ██║   █████╗  ███████╗   ██║   ███████╗ ║
// ║    ██║   ██╔══╝  ╚════██║   ██║   ╚════██║ ║
// ║    ██║   ███████╗███████║   ██║   ███████║ ║
// ║    ╚═╝   ╚══════╝╚══════╝   ╚═╝   ╚══════╝ ║
// ╚════════════════════════════════════════════╝

#[cfg(test)]
mod tests {
    use super::*;
    use crate::model::host::OsSource;

    /// The Brother print server this behaviour was found on, as its agent
    /// answered on 2026-08-26: a make, a model and a firmware, and not one word
    /// about an operating system.
    fn a_named_appliance() -> OsEvidence {
        OsEvidence {
            source: OsSource::SnmpAgent,
            family: None,
            device: Some("Printer".to_string()),
            vendor: Some("Brother".to_string()),
            product: Some("NC-8700w".to_string()),
            version: Some("ZL".to_string()),
            kernel: None,
            arch: None,
            cpe: None,
            confidence: 0.56,
            evidence: "snmp agent names NC-8700w".to_string(),
        }
    }

    fn evidence(family: &str, confidence: f32, source: OsSource) -> OsEvidence {
        OsEvidence {
            source,
            family: Some(family.to_string()),
            device: None,
            vendor: None,
            product: None,
            version: None,
            kernel: None,
            arch: None,
            cpe: None,
            confidence,
            evidence: format!("{source:?} says {family}"),
        }
    }

    /// The point of a second source. A stack reading alone is capped below the
    /// threshold that stops further probing, because one packet's fields are one
    /// observation; a second, genuinely independent source agreeing with it is
    /// what may legitimately carry the answer past that.
    #[test]
    fn two_agreeing_sources_are_worth_more_than_either() {
        let stack = evidence("Linux", 0.65, OsSource::TcpStack);
        let hardware = evidence("Linux", 0.30, OsSource::HardwareVendor);

        let alone = resolve(vec![stack.clone()]).expect("one source names it");
        let together = resolve(vec![stack, hardware]).expect("two sources name it");

        assert!(together.accuracy > alone.accuracy);
        assert_eq!(together.accuracy, 76, "1 - (0.35 x 0.70)");
    }

    /// Agreement must not become certainty. Noisy-OR is chosen over anything that
    /// sums precisely because a stack of agreeing guesses has to stay a stack of
    /// guesses however many of them there are.
    #[test]
    fn no_amount_of_agreement_reaches_certainty() {
        let many: Vec<OsEvidence> = (0..20)
            .map(|_| evidence("Linux", 0.6, OsSource::TcpStack))
            .collect();

        let resolved = resolve(many).expect("named");
        assert_eq!(
            resolved.accuracy, MAX_FUSED_ACCURACY,
            "twenty agreeing sources may be highly confident and must not be certain"
        );
        assert!(
            resolved.accuracy < 100,
            "certainty is reserved for a host that identified itself"
        );
    }

    /// Sources at odds are a worse position than one source alone, and the
    /// resolver has to say so rather than take the larger and report it at full
    /// strength. Hiding a conflict is worst exactly where it matters most.
    #[test]
    fn disagreement_lowers_the_answer_rather_than_picking_a_winner() {
        let uncontested = resolve(vec![evidence("Linux", 0.65, OsSource::TcpStack)])
            .expect("one source names it")
            .accuracy;

        let contested = resolve(vec![
            evidence("Linux", 0.65, OsSource::TcpStack),
            evidence("Windows", 0.30, OsSource::HardwareVendor),
        ])
        .expect("the leader survives one dissenter");

        assert_eq!(contested.family.as_deref(), Some("Linux"));
        assert!(
            contested.accuracy < uncontested,
            "a contested verdict must not score what an uncontested one does"
        );
    }

    /// And a conflict bad enough leaves nothing worth reporting. A host two
    /// techniques flatly disagree about is a host this engine cannot name, and
    /// saying so beats naming it at reduced confidence.
    #[test]
    fn an_even_conflict_names_nothing() {
        assert!(
            resolve(vec![
                evidence("Linux", 0.65, OsSource::TcpStack),
                evidence("Windows", 0.65, OsSource::HardwareVendor),
            ])
            .is_none()
        );
    }

    /// A source that says nothing about a product **abstains**; it does not
    /// dissent.
    ///
    /// A hardware vendor is read out of an address registration and has no way
    /// to hold an opinion about a distribution. Counting its silence as
    /// disagreement would mean the only source capable of naming one loses the
    /// name the moment anything else corroborates the family, which is more
    /// evidence producing a less specific answer and the wrong direction for
    /// evidence to move.
    ///
    /// This is the same rule the matcher already applies one layer down, where a
    /// predicate a rule does not state is "do not care" rather than "must be
    /// absent".
    #[test]
    fn a_source_with_nothing_to_say_about_a_product_does_not_veto_one() {
        let mut precise = evidence("Linux", 0.65, OsSource::TcpStack);
        precise.product = Some("Ubuntu".to_string());
        let vague = evidence("Linux", 0.30, OsSource::HardwareVendor);

        let resolved = resolve(vec![precise, vague]).expect("named");
        assert_eq!(resolved.family.as_deref(), Some("Linux"));
        assert_eq!(
            resolved.product.as_deref(),
            Some("Ubuntu"),
            "the only source that could name a product named one, and nothing contradicted it"
        );
    }

    /// Abstention is not agreement with anything, though. Two sources naming
    /// *different* products cannot both be right, and nothing here can say
    /// which is, so the answer keeps the family they share and drops the part
    /// they contest.
    #[test]
    fn two_sources_naming_different_products_keep_neither() {
        let mut stack = evidence("Linux", 0.65, OsSource::TcpStack);
        stack.product = Some("Ubuntu".to_string());
        let mut banner = evidence("Linux", 0.65, OsSource::ServiceBanner);
        banner.product = Some("Debian".to_string());

        let resolved = resolve(vec![stack, banner]).expect("the family is agreed");
        assert_eq!(resolved.family.as_deref(), Some("Linux"));
        assert_eq!(resolved.product, None);
    }

    /// Two runs over the same evidence must resolve the same way. With scores
    /// this close together an unordered fold would let whichever source happened
    /// to be pushed first decide a tie, and a scan would report differently on
    /// alternate runs for no reason in the data.
    #[test]
    fn the_answer_does_not_depend_on_the_order_evidence_arrived_in() {
        let a = evidence("Linux", 0.5, OsSource::TcpStack);
        let b = evidence("macOS", 0.5, OsSource::HardwareVendor);

        assert_eq!(
            resolve(vec![a.clone(), b.clone()]),
            resolve(vec![b, a]),
            "the same evidence in the other order is the same evidence"
        );
    }

    #[test]
    fn nothing_in_names_nothing_out() {
        assert!(resolve(Vec::new()).is_none());
    }

    /// The finding this file was rewritten for.
    ///
    /// A hop counter of 255 says *network device*; an SNMP agent says *Brother
    /// NC-8700w*. Those are answers to two questions and the second is by far
    /// the better one, but scored as rival families they cancelled: 0.4 reduced
    /// by 0.385 left 25, under the floor, and a printer that had answered ARP,
    /// ICMP, TCP and SNMP was reported as unidentified.
    #[test]
    fn a_source_that_names_no_family_does_not_argue_with_one_that_does() {
        let stack = evidence("Network device", 0.4, OsSource::TcpStack);
        let resolved = resolve(vec![stack.clone(), a_named_appliance()]).expect("named");

        assert_eq!(resolved.family.as_deref(), Some("Network device"));
        assert_eq!(
            resolved.accuracy,
            percent(0.4),
            "unreduced: nothing dissented"
        );
        assert_eq!(resolved.vendor.as_deref(), Some("Brother"));
        assert_eq!(resolved.product.as_deref(), Some("NC-8700w"));
        assert_eq!(resolved.device.as_deref(), Some("Printer"));
    }

    /// And the abstention carries the answer where nothing else can name a
    /// family at all, a device on a segment whose stack said nothing.
    #[test]
    fn an_abstention_alone_is_still_an_answer() {
        let resolved = resolve(vec![a_named_appliance()]).expect("named");

        assert_eq!(resolved.family, None);
        assert_eq!(resolved.product.as_deref(), Some("NC-8700w"));
        assert_eq!(resolved.accuracy, percent(0.56));
    }

    /// A family that cannot clear the floor takes only itself down. What
    /// abstained never entered the quarrel and is not reduced by it.
    #[test]
    fn a_family_too_contested_to_report_does_not_take_the_rest_with_it() {
        let one = evidence("Linux", 0.5, OsSource::TcpStack);
        let other = evidence("Windows", 0.5, OsSource::HardwareVendor);
        assert!(
            resolve(vec![one.clone(), other.clone()]).is_none(),
            "two sources this far apart name nothing between them"
        );

        let resolved = resolve(vec![one, other, a_named_appliance()]).expect("named");
        assert_eq!(
            resolved.family, None,
            "the contested family is still refused"
        );
        assert_eq!(resolved.product.as_deref(), Some("NC-8700w"));
    }

    /// A class of box on its own **is** a verdict, and this test reverses an
    /// earlier reading that said otherwise.
    ///
    /// The old rule was that something knowing only what the hardware is has
    /// identified no software, so a class alone was refused. That held while a
    /// class could only arrive beside a product, from a rule reading text. It
    /// stopped holding when the rules that read a hop counter of 255 began
    /// stating a class instead of writing one into the family field: "this host
    /// is infrastructure" is then the whole of what a real observation
    /// established, and it is the only thing anything will ever say about a
    /// switch with no port open and no name.
    #[test]
    fn a_device_class_on_its_own_is_a_verdict() {
        let class_only = OsEvidence {
            vendor: None,
            product: None,
            version: None,
            confidence: 0.5,
            ..a_named_appliance()
        };
        let resolved = resolve(vec![class_only]).expect("the class is the answer");

        assert_eq!(resolved.device.as_deref(), Some("Printer"));
        assert_eq!(
            resolved.family, None,
            "it still says nothing about software"
        );
        assert_eq!(
            resolved.label(),
            "Printer",
            "and that is what a reader sees"
        );
    }

    /// Nothing at all is still nothing.
    #[test]
    fn evidence_that_establishes_no_part_of_an_identity_is_refused() {
        let says_nothing = OsEvidence {
            family: None,
            device: None,
            vendor: None,
            product: None,
            version: None,
            ..a_named_appliance()
        };
        assert!(resolve(vec![says_nothing]).is_none());
    }
}