use crate::model::host::HardwareInfo;
use crate::model::host::OsEvidence;
use crate::model::host::OsSource;
pub const CONFIDENCE: f32 = 0.3;
const VENDOR_FAMILIES: &[(&str, &str)] = &[
("apple", "macOS"),
("raspberry pi", "Linux"),
];
const VENDOR_DEVICES: &[(&str, &str)] = &[
("cisco", "Network device"),
("juniper", "Network device"),
("ubiquiti", "Network device"),
("mikrotik", "Network device"),
("arista", "Network device"),
("netgear", "Network device"),
("tp-link", "Network device"),
("zyxel", "Network device"),
];
pub fn evidence_from(hardware: &HardwareInfo) -> Option<OsEvidence> {
let vendor = hardware.registered_vendor()?;
let lowered = vendor.to_ascii_lowercase();
let matches = |table: &'static [(&str, &str)]| {
table
.iter()
.find(|(prefix, _)| lowered.starts_with(prefix))
.map(|(_, value)| (*value).to_string())
};
let (family, device) = match matches(VENDOR_FAMILIES) {
Some(family) => (Some(family), None),
None => (None, Some(matches(VENDOR_DEVICES)?)),
};
Some(OsEvidence {
source: OsSource::HardwareVendor,
family,
device,
vendor: None,
product: None,
version: None,
kernel: None,
arch: None,
cpe: None,
confidence: CONFIDENCE,
evidence: format!("hardware vendor {vendor}"),
})
}
#[cfg(test)]
mod tests {
use super::*;
use crate::fingerprint::os::resolve;
use crate::model::mac::MacAddr;
fn evidence_for(mac: &str) -> Option<OsEvidence> {
let mac: MacAddr = mac.parse().expect("a hardware address");
evidence_from(&HardwareInfo::new(mac))
}
fn a_debian_banner() -> OsEvidence {
OsEvidence {
source: OsSource::ServiceBanner,
family: Some("Linux".to_string()),
device: None,
vendor: None,
product: Some("Debian".to_string()),
version: Some("12".to_string()),
kernel: None,
arch: None,
cpe: None,
confidence: 0.55,
evidence: "service banner names Debian".to_string(),
}
}
#[test]
fn an_infrastructure_vendor_does_not_destroy_what_the_banner_established() {
for mac in [
"00:1b:54:00:00:01", "50:c7:bf:00:00:01", "c0:3f:0e:00:00:01", "24:5a:4c:00:00:01", ] {
let oui = evidence_for(mac).expect("a registered infrastructure block");
assert_eq!(oui.family, None, "{mac} must abstain from the family");
assert_eq!(oui.device.as_deref(), Some("Network device"));
let alone = resolve(vec![a_debian_banner()]).expect("the banner names the host");
let with_hardware =
resolve(vec![a_debian_banner(), oui]).expect("and the address does not unname it");
assert_eq!(with_hardware.family.as_deref(), Some("Linux"));
assert_eq!(with_hardware.version.as_deref(), Some("12"));
assert_eq!(with_hardware.device.as_deref(), Some("Network device"));
assert_eq!(
with_hardware.accuracy, alone.accuracy,
"{mac} agreed about a second question, so it cost the first nothing"
);
}
}
#[test]
fn a_vendor_who_ships_the_system_names_a_family() {
let apple = evidence_for("a4:83:e7:00:00:01").expect("a registered Apple block");
assert_eq!(apple.family.as_deref(), Some("macOS"));
assert_eq!(
apple.device, None,
"an address block says nothing about the box"
);
let pi = evidence_for("b8:27:eb:00:00:01").expect("a registered Raspberry Pi block");
assert_eq!(pi.family.as_deref(), Some("Linux"));
assert_eq!(pi.device, None);
}
#[test]
fn no_address_claims_a_family_and_a_class_at_once() {
for (prefix, _) in VENDOR_FAMILIES {
assert!(
!VENDOR_DEVICES.iter().any(|(other, _)| other == prefix),
"`{prefix}` is in both tables"
);
}
}
#[test]
fn one_hardware_reading_alone_never_names_a_host() {
for mac in ["a4:83:e7:00:00:01", "50:c7:bf:00:00:01"] {
let oui = evidence_for(mac).expect("a registered block");
assert!(
resolve(vec![oui]).is_none(),
"{mac} named a host on its own"
);
}
}
#[test]
fn a_described_vendor_is_never_counted_as_the_address_reading() {
use crate::model::host::HardwareDescription;
for (prefix, _) in VENDOR_FAMILIES.iter().chain(VENDOR_DEVICES) {
let described = HardwareInfo::described(HardwareDescription {
vendor: Some(prefix),
product: Some("described by its own service"),
..HardwareDescription::default()
})
.expect("a vendor and a product name something");
assert!(
evidence_from(&described).is_none(),
"`{prefix}`, stated by a service, was counted again as the hardware's own"
);
}
let mut seen = HardwareInfo::new("a4:83:e7:00:00:01".parse().expect("an address"));
seen.merge(
HardwareInfo::described(HardwareDescription {
vendor: Some("Check Point"),
product: Some("Firewall-1"),
..HardwareDescription::default()
})
.expect("names something"),
);
assert_eq!(
seen.vendor(),
Some("Check Point"),
"test premise: the stated vendor leads"
);
assert_eq!(
evidence_from(&seen)
.and_then(|evidence| evidence.family)
.as_deref(),
Some("macOS")
);
}
#[test]
fn an_address_that_implies_nothing_is_declined() {
assert!(evidence_for("00:1b:21:00:00:01").is_none());
assert!(evidence_for("02:00:00:00:00:01").is_none());
}
}