use crate::model::host::Host;
use super::{OsEvidence, hardware_evidence, hostname_evidence, resolve};
pub fn identify(host: &mut Host, observed: impl IntoIterator<Item = OsEvidence>) -> bool {
let mut evidence: Vec<OsEvidence> = observed.into_iter().collect();
if let Some(hardware) = host.hardware().and_then(hardware_evidence) {
evidence.push(hardware);
}
if let Some(name) = hostname_evidence(host.hostname()) {
evidence.push(name);
}
let Some(resolved) = resolve(evidence) else {
return false;
};
let fingerprint = resolved.to_fingerprint();
match host.os() {
Some(existing) => {
let mut merged = existing.clone();
merged.merge(fingerprint);
host.set_os(merged);
}
None => host.set_os(fingerprint),
}
true
}
#[cfg(test)]
mod tests {
use super::*;
use crate::fingerprint::os::verdict::OsSource;
use std::net::{IpAddr, Ipv4Addr};
fn host() -> Host {
Host::new(IpAddr::V4(Ipv4Addr::new(192, 0, 2, 1)))
}
fn observed(family: &str, confidence: f32) -> OsEvidence {
OsEvidence {
source: OsSource::TcpStack,
family: family.to_owned(),
vendor: None,
product: None,
version: None,
cpe: None,
confidence,
evidence: "a synthetic stack reading".to_owned(),
}
}
#[test]
fn one_passive_source_alone_never_names_a_host() {
let mut by_hardware = host();
by_hardware.record_mac("b8:27:eb:00:00:01".parse().expect("a registered address"));
assert!(!identify(&mut by_hardware, []));
assert!(by_hardware.os().is_none());
let mut by_name = host();
by_name.set_hostname(Some("DESKTOP-FKV0V2O".to_owned()));
assert!(!identify(&mut by_name, []));
assert!(by_name.os().is_none());
}
#[test]
fn two_agreeing_passive_sources_name_a_host_between_them() {
let mut host = host();
host.record_mac(
"a4:83:e7:00:00:01"
.parse()
.expect("a registered Apple address"),
);
host.set_hostname(Some("MacBook-Pro".to_owned()));
assert!(identify(&mut host, []));
let os = host.os().expect("a fingerprint");
assert_eq!(os.name(), "macOS");
assert!(os.accuracy() >= 40, "below the reporting floor: {os}");
}
#[test]
fn a_host_with_nothing_to_go_on_is_left_unnamed() {
let mut host = host();
host.record_mac(
"02:00:5e:00:53:04"
.parse()
.expect("a locally administered address"),
);
host.set_hostname(Some("fileserver".to_owned()));
assert!(!identify(&mut host, []));
assert!(host.os().is_none());
}
#[test]
fn the_hosts_own_sources_are_consulted_alongside_what_was_observed() {
let reading = observed("Linux", 0.6);
let mut bare = host();
assert!(identify(&mut bare, [reading.clone()]));
let alone = bare.os().expect("a fingerprint").accuracy();
let mut corroborated = host();
corroborated.record_mac("b8:27:eb:00:00:01".parse().expect("a registered address"));
assert!(identify(&mut corroborated, [reading]));
let together = corroborated.os().expect("a fingerprint").accuracy();
assert!(
together > alone,
"hardware agreeing with the wire should raise the verdict: {together} vs {alone}"
);
}
#[test]
fn identifying_twice_never_loses_what_was_already_known() {
let mut host = host();
host.record_mac(
"a4:83:e7:00:00:01"
.parse()
.expect("a registered Apple address"),
);
host.set_hostname(Some("MacBook-Pro".to_owned()));
assert!(identify(&mut host, []));
let first = host.os().expect("a fingerprint").clone();
assert!(identify(&mut host, []));
let second = host.os().expect("still a fingerprint");
assert_eq!(first.name(), second.name());
assert!(second.accuracy() >= first.accuracy());
}
#[test]
fn a_passive_pass_cannot_weaken_a_reading_taken_from_the_wire() {
let mut host = host();
assert!(identify(&mut host, [observed("Linux", 0.7)]));
let from_the_wire = host.os().expect("a fingerprint").clone();
identify(&mut host, []);
let after = host.os().expect("still a fingerprint");
assert_eq!(after.name(), from_the_wire.name());
assert!(after.accuracy() >= from_the_wire.accuracy());
}
}