use std::{collections::BTreeSet, sync::Arc};
pub const MAX_CPES_PER_OS: usize = 50;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct OsFingerprint {
name: Arc<str>,
family: Option<Arc<str>>,
generation: Option<Arc<str>>,
vendor: Option<Arc<str>>,
accuracy: u8,
cpe: BTreeSet<Arc<str>>,
evidence: Option<Arc<str>>,
}
impl OsFingerprint {
pub fn new(name: impl Into<Arc<str>>, accuracy: u8) -> Self {
Self {
name: name.into(),
family: None,
generation: None,
vendor: None,
accuracy: accuracy.min(100),
cpe: BTreeSet::new(),
evidence: None,
}
}
pub fn name(&self) -> &str {
&self.name
}
pub fn family(&self) -> Option<&str> {
self.family.as_deref()
}
pub fn generation(&self) -> Option<&str> {
self.generation.as_deref()
}
pub fn vendor(&self) -> Option<&str> {
self.vendor.as_deref()
}
pub fn accuracy(&self) -> u8 {
self.accuracy
}
pub fn with_family(mut self, family: impl Into<Arc<str>>) -> Self {
self.family = Some(family.into());
self
}
pub fn with_generation(mut self, generation: impl Into<Arc<str>>) -> Self {
self.generation = Some(generation.into());
self
}
pub fn with_vendor(mut self, vendor: impl Into<Arc<str>>) -> Self {
self.vendor = Some(vendor.into());
self
}
pub fn with_evidence(mut self, evidence: impl Into<Arc<str>>) -> Self {
self.evidence = Some(evidence.into());
self
}
pub fn evidence(&self) -> Option<&str> {
self.evidence.as_deref()
}
pub fn add_cpe(&mut self, cpe: impl Into<Arc<str>>) {
if self.cpe.len() < MAX_CPES_PER_OS {
self.cpe.insert(cpe.into());
}
}
pub fn cpes(&self) -> &BTreeSet<Arc<str>> {
&self.cpe
}
pub fn is_highly_confident(&self) -> bool {
self.accuracy >= 85
}
pub fn merge(&mut self, other: OsFingerprint) {
let OsFingerprint {
name,
family,
generation,
vendor,
accuracy,
cpe,
evidence,
} = other;
if accuracy > self.accuracy {
self.name = name;
self.accuracy = accuracy;
self.family = family.or(self.family.take());
self.generation = generation.or(self.generation.take());
self.vendor = vendor.or(self.vendor.take());
self.evidence = evidence.or(self.evidence.take());
} else if accuracy == self.accuracy {
self.evidence = self.evidence.take().or(evidence);
self.family = self.family.take().or(family);
self.generation = self.generation.take().or(generation);
self.vendor = self.vendor.take().or(vendor);
}
for cpe in cpe {
if self.cpe.len() >= MAX_CPES_PER_OS {
break;
}
self.cpe.insert(cpe);
}
}
}
impl std::fmt::Display for OsFingerprint {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.name)?;
if let Some(ref generation) = self.generation {
write!(f, " {}", generation)?;
}
write!(f, " [{}%]", self.accuracy)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn an_accuracy_above_100_is_clamped_rather_than_kept() {
assert_eq!(OsFingerprint::new("Linux", 200).accuracy(), 100);
assert_eq!(OsFingerprint::new("Linux", 100).accuracy(), 100);
}
#[test]
fn high_confidence_starts_at_85() {
assert!(OsFingerprint::new("Linux", 85).is_highly_confident());
assert!(!OsFingerprint::new("Linux", 84).is_highly_confident());
}
#[test]
fn the_more_accurate_finding_names_the_host() {
let mut banner = OsFingerprint::new("Linux", 50);
banner.merge(OsFingerprint::new("Ubuntu", 90));
assert_eq!(banner.name(), "Ubuntu");
assert_eq!(banner.accuracy(), 90);
}
#[test]
fn a_tie_keeps_the_incumbent_and_fills_only_its_gaps() {
let mut first = OsFingerprint::new("Linux", 80).with_family("Unix-like");
first.merge(
OsFingerprint::new("Linux", 80)
.with_family("Something else")
.with_generation("5.15.0"),
);
assert_eq!(first.family(), Some("Unix-like"), "already recorded");
assert_eq!(first.generation(), Some("5.15.0"), "a gap, so filled");
}
#[test]
fn a_more_accurate_finding_takes_the_identity_but_not_at_the_cost_of_cpes() {
let mut banner = OsFingerprint::new("Linux", 40).with_vendor("Canonical");
banner.add_cpe("cpe:/o:canonical:ubuntu_linux");
let mut stack = OsFingerprint::new("Ubuntu 22.04", 90);
stack.add_cpe("cpe:/o:canonical:ubuntu_linux:22.04");
banner.merge(stack);
assert_eq!(banner.name(), "Ubuntu 22.04", "the surer name wins");
assert_eq!(banner.accuracy(), 90);
assert_eq!(
banner.vendor(),
Some("Canonical"),
"and a field the surer finding left empty is not erased"
);
assert_eq!(banner.cpes().len(), 2, "both identifiers still apply");
}
#[test]
fn the_cpe_list_is_bounded_by_both_routes_into_it() {
let mut os = OsFingerprint::new("Windows", 100);
for i in 0..MAX_CPES_PER_OS * 2 {
os.add_cpe(format!("cpe:/o:ident:{i}"));
}
assert_eq!(os.cpes().len(), MAX_CPES_PER_OS);
let mut other = OsFingerprint::new("Windows", 100);
for i in 0..MAX_CPES_PER_OS {
other.add_cpe(format!("cpe:/o:other:{i}"));
}
os.merge(other);
assert_eq!(os.cpes().len(), MAX_CPES_PER_OS);
}
}