use std::collections::BTreeSet;
use std::sync::Arc;
use crate::model::host::os::MAX_CPES_PER_OS;
pub const MAX_CPES_PER_SERVICE: usize = MAX_CPES_PER_OS;
#[must_use]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Service {
name: Arc<str>,
confidence: u8,
product: Option<Arc<str>>,
vendor: Option<Arc<str>>,
version: Option<Arc<str>>,
extrainfo: Option<Arc<str>>,
cpe: BTreeSet<Arc<str>>,
}
impl Service {
pub fn new(name: impl Into<Arc<str>>, confidence: u8) -> Self {
Self {
name: name.into(),
confidence: confidence.min(100),
product: None,
vendor: None,
version: None,
extrainfo: None,
cpe: BTreeSet::new(),
}
}
pub fn name(&self) -> &str {
&self.name
}
pub fn is_inferred(&self) -> bool {
self.confidence == 0
}
pub fn confidence(&self) -> u8 {
self.confidence
}
pub fn product(&self) -> Option<&str> {
self.product.as_deref()
}
pub fn vendor(&self) -> Option<&str> {
self.vendor.as_deref()
}
pub fn version(&self) -> Option<&str> {
self.version.as_deref()
}
pub fn extrainfo(&self) -> Option<&str> {
self.extrainfo.as_deref()
}
pub fn cpes(&self) -> &BTreeSet<Arc<str>> {
&self.cpe
}
pub fn with_product(mut self, product: impl Into<Arc<str>>) -> Self {
self.product = Some(product.into());
self
}
pub fn with_vendor(mut self, vendor: impl Into<Arc<str>>) -> Self {
self.vendor = Some(vendor.into());
self
}
pub fn with_version(mut self, version: impl Into<Arc<str>>) -> Self {
self.version = Some(version.into());
self
}
pub fn with_extrainfo(mut self, extrainfo: impl Into<Arc<str>>) -> Self {
self.extrainfo = Some(extrainfo.into());
self
}
pub fn add_cpe(&mut self, cpe: impl Into<Arc<str>>) {
if self.cpe.len() < MAX_CPES_PER_SERVICE {
self.cpe.insert(cpe.into());
}
}
pub fn with_cpe(mut self, cpe: impl Into<Arc<str>>) -> Self {
self.add_cpe(cpe);
self
}
pub fn merge(&mut self, other: Service) {
let Service {
name,
confidence,
product,
vendor,
version,
extrainfo,
cpe,
} = other;
if confidence > self.confidence {
self.name = name;
self.confidence = confidence;
self.product = product.or(self.product.take());
self.vendor = vendor.or(self.vendor.take());
self.version = version.or(self.version.take());
self.extrainfo = extrainfo.or(self.extrainfo.take());
} else {
self.product = self.product.take().or(product);
self.vendor = self.vendor.take().or(vendor);
self.version = self.version.take().or(version);
self.extrainfo = self.extrainfo.take().or(extrainfo);
}
for cpe in cpe {
if self.cpe.len() >= MAX_CPES_PER_SERVICE {
break;
}
self.cpe.insert(cpe);
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_service_carries_everything_an_analyzer_can_establish() {
let service = Service::new("http", 85)
.with_product("Apache")
.with_vendor("Apache Software Foundation")
.with_version("2.4.57")
.with_extrainfo("PHP/8.2.1")
.with_cpe("cpe:/a:apache:http_server:2.4.57");
assert_eq!(service.name(), "http");
assert_eq!(service.confidence(), 85);
assert_eq!(service.product(), Some("Apache"));
assert_eq!(service.vendor(), Some("Apache Software Foundation"));
assert_eq!(service.version(), Some("2.4.57"));
assert_eq!(service.extrainfo(), Some("PHP/8.2.1"));
assert_eq!(service.cpes().len(), 1);
}
#[test]
fn a_confidence_above_100_is_clamped_rather_than_kept() {
assert_eq!(Service::new("ssh", 101).confidence(), 100);
}
#[test]
fn the_surer_identification_names_the_service_and_the_other_fills_its_gaps() {
let mut guess = Service::new("http", 50).with_product("nginx");
guess.merge(
Service::new("http", 100)
.with_product("Apache")
.with_version("2.4"),
);
assert_eq!(guess.product(), Some("Apache"), "the surer product wins");
assert_eq!(guess.confidence(), 100);
assert_eq!(guess.version(), Some("2.4"));
let mut established = Service::new("http", 85).with_product("nginx");
established.merge(Service::new("unknown", 10).with_version("2.0"));
assert_eq!(established.name(), "http", "a guess does not rename it");
assert_eq!(established.product(), Some("nginx"));
assert_eq!(established.confidence(), 85);
assert_eq!(
established.version(),
Some("2.0"),
"but a gap is worth filling from any source"
);
}
#[test]
fn cpes_are_unioned_across_a_merge_whatever_the_confidence() {
let mut ssh = Service::new("ssh", 100).with_cpe("cpe:/a:openbsd:openssh");
ssh.merge(
Service::new("ssh", 10)
.with_cpe("cpe:/o:linux:linux_kernel")
.with_cpe("cpe:/a:openbsd:openssh"),
);
assert_eq!(ssh.cpes().len(), 2, "one new, one already held");
}
#[test]
fn a_services_cpe_list_is_bounded_like_an_os_fingerprints() {
let mut service = Service::new("http", 50);
for i in 0..(MAX_CPES_PER_SERVICE * 2) {
service.add_cpe(format!("cpe:/a:vendor:product:{i}"));
}
assert_eq!(service.cpes().len(), MAX_CPES_PER_SERVICE);
let mut other = Service::new("http", 50);
for i in 0..MAX_CPES_PER_SERVICE {
other.add_cpe(format!("cpe:/a:other:product:{i}"));
}
service.merge(other);
assert_eq!(service.cpes().len(), MAX_CPES_PER_SERVICE);
}
}