use crate::model::port::Service;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Default)]
pub enum Confidence {
#[default]
Heuristic,
Weak,
Probable,
Strong,
Certain,
}
impl Confidence {
pub fn as_score(self) -> u8 {
match self {
Confidence::Heuristic => 0,
Confidence::Weak => 40,
Confidence::Probable => 70,
Confidence::Strong => 90,
Confidence::Certain => 100,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SourceId {
PortHeuristic,
BannerRegex,
TlsCert,
HttpHeaders,
Ssh,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Tunnel {
Tls,
}
impl Tunnel {
fn scheme(self) -> &'static str {
match self {
Tunnel::Tls => "ssl",
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Evidence {
pub service: Option<String>,
pub product: Option<String>,
pub version: Option<String>,
pub vendor: Option<String>,
pub extrainfo: Option<String>,
pub cpe: Option<String>,
pub tunnel: Option<Tunnel>,
pub port_confirmed: bool,
pub confidence: Confidence,
pub source: SourceId,
pub os: Option<crate::fingerprint::os::OsEvidence>,
}
impl Evidence {
pub fn new(source: SourceId, confidence: Confidence) -> Self {
Self {
service: None,
product: None,
version: None,
vendor: None,
extrainfo: None,
cpe: None,
tunnel: None,
port_confirmed: false,
os: None,
confidence,
source,
}
}
pub fn with_service(mut self, service: impl Into<String>) -> Self {
self.service = Some(service.into());
self
}
pub fn with_product(mut self, product: impl Into<String>) -> Self {
self.product = Some(product.into());
self
}
pub fn with_version(mut self, version: impl Into<String>) -> Self {
self.version = Some(version.into());
self
}
pub fn with_vendor(mut self, vendor: impl Into<String>) -> Self {
self.vendor = Some(vendor.into());
self
}
pub fn with_extrainfo(mut self, extrainfo: impl Into<String>) -> Self {
self.extrainfo = Some(extrainfo.into());
self
}
pub fn with_tunnel(mut self, tunnel: Tunnel) -> Self {
self.tunnel = Some(tunnel);
self
}
}
#[derive(Debug, Clone, Default, PartialEq)]
pub struct ServiceVerdict {
pub service: Option<String>,
pub product: Option<String>,
pub version: Option<String>,
pub vendor: Option<String>,
pub extrainfo: Option<String>,
pub cpe: Option<String>,
pub tunnel: Option<Tunnel>,
pub confidence: Confidence,
pub evidence: Vec<Evidence>,
}
impl ServiceVerdict {
pub fn resolve(mut evidence: Vec<Evidence>) -> Self {
evidence.sort_by(|a, b| {
b.confidence
.cmp(&a.confidence)
.then(b.port_confirmed.cmp(&a.port_confirmed))
});
let mut verdict = ServiceVerdict {
confidence: evidence
.first()
.map(|e| e.confidence)
.unwrap_or(Confidence::Heuristic),
..Default::default()
};
for ev in &evidence {
if verdict.service.is_none() && ev.service.is_some() {
verdict.tunnel = ev.tunnel;
}
fill(&mut verdict.service, &ev.service);
fill(&mut verdict.version, &ev.version);
fill(&mut verdict.vendor, &ev.vendor);
fill(&mut verdict.extrainfo, &ev.extrainfo);
fill(&mut verdict.cpe, &ev.cpe);
}
verdict.product = evidence
.iter()
.filter_map(|ev| ev.product.as_deref())
.find(|product| Some(*product) != verdict.service.as_deref())
.or_else(|| evidence.iter().find_map(|ev| ev.product.as_deref()))
.map(str::to_string);
verdict.evidence = evidence;
verdict
}
pub fn is_empty(&self) -> bool {
self.service.is_none() && self.product.is_none()
}
pub fn to_service(&self) -> Option<Service> {
let name = self.service.clone().or_else(|| self.product.clone())?;
let name = match self.tunnel {
Some(tunnel) => format!("{}/{name}", tunnel.scheme()),
None => name,
};
let mut service = Service::new(name, self.confidence.as_score());
if let Some(product) = &self.product {
service = service.with_product(product.clone());
}
if let Some(vendor) = &self.vendor {
service = service.with_vendor(vendor.clone());
}
if let Some(version) = &self.version {
service = service.with_version(version.clone());
}
if let Some(extrainfo) = &self.extrainfo {
service = service.with_extrainfo(extrainfo.clone());
}
Some(service)
}
}
fn fill(slot: &mut Option<String>, value: &Option<String>) {
if slot.is_none()
&& let Some(v) = value
{
*slot = Some(v.clone());
}
}
#[cfg(test)]
mod tests {
use super::*;
fn ev(conf: Confidence) -> Evidence {
Evidence::new(SourceId::BannerRegex, conf)
}
#[test]
fn confidence_is_ordered() {
assert!(Confidence::Certain > Confidence::Strong);
assert!(Confidence::Strong > Confidence::Probable);
assert!(Confidence::Probable > Confidence::Heuristic);
}
#[test]
fn resolve_prefers_strongest_evidence_per_field() {
let weak = ev(Confidence::Weak)
.with_service("http")
.with_product("generic");
let strong = ev(Confidence::Strong)
.with_service("http")
.with_product("nginx");
let verdict = ServiceVerdict::resolve(vec![weak, strong]);
assert_eq!(verdict.product.as_deref(), Some("nginx"));
assert_eq!(verdict.confidence, Confidence::Strong);
assert_eq!(verdict.evidence.len(), 2);
}
#[test]
fn informative_product_beats_a_service_echo_at_equal_confidence() {
let generic = ev(Confidence::Probable)
.with_service("http")
.with_product("http");
let specific = ev(Confidence::Probable)
.with_service("http")
.with_product("cloudflare");
let verdict = ServiceVerdict::resolve(vec![generic, specific]);
assert_eq!(verdict.service.as_deref(), Some("http"));
assert_eq!(verdict.product.as_deref(), Some("cloudflare"));
}
#[test]
fn service_echo_product_is_kept_when_nothing_more_specific_exists() {
let verdict = ServiceVerdict::resolve(vec![
ev(Confidence::Probable)
.with_service("http")
.with_product("http"),
]);
assert_eq!(verdict.product.as_deref(), Some("http"));
}
#[test]
fn port_confirmed_match_wins_the_service_at_equal_confidence() {
let global_smtp = ev(Confidence::Probable).with_service("smtp");
let mut port_ftp = ev(Confidence::Probable).with_service("ftp");
port_ftp.port_confirmed = true;
let verdict = ServiceVerdict::resolve(vec![global_smtp, port_ftp]);
assert_eq!(verdict.service.as_deref(), Some("ftp"));
}
#[test]
fn confidence_still_dominates_port_confirmation() {
let mut weak_port = ev(Confidence::Probable).with_service("ftp");
weak_port.port_confirmed = true;
let strong_global = ev(Confidence::Strong)
.with_service("smtp")
.with_product("Postfix");
let verdict = ServiceVerdict::resolve(vec![weak_port, strong_global]);
assert_eq!(verdict.service.as_deref(), Some("smtp"));
}
#[test]
fn resolve_merges_fields_across_evidence() {
let a = ev(Confidence::Strong)
.with_service("https")
.with_product("nginx");
let b = ev(Confidence::Probable).with_version("1.25.3");
let verdict = ServiceVerdict::resolve(vec![a, b]);
assert_eq!(verdict.product.as_deref(), Some("nginx"));
assert_eq!(verdict.version.as_deref(), Some("1.25.3"));
}
#[test]
fn empty_verdict_maps_to_no_service() {
assert!(ServiceVerdict::resolve(Vec::new()).to_service().is_none());
}
#[test]
fn tunnel_prefixes_the_service_label_and_travels_with_service() {
let tunnelled = ev(Confidence::Strong)
.with_service("http")
.with_product("nginx")
.with_tunnel(Tunnel::Tls);
let verdict = ServiceVerdict::resolve(vec![tunnelled]);
assert_eq!(verdict.service.as_deref(), Some("http")); assert_eq!(verdict.tunnel, Some(Tunnel::Tls));
assert_eq!(verdict.to_service().unwrap().name(), "ssl/http");
let plain = ev(Confidence::Strong).with_service("http");
let verdict = ServiceVerdict::resolve(vec![plain]);
assert_eq!(verdict.tunnel, None);
assert_eq!(verdict.to_service().unwrap().name(), "http");
}
#[test]
fn vendor_and_extrainfo_resolve_and_reach_the_service() {
let server = ev(Confidence::Strong)
.with_service("http")
.with_product("Apache")
.with_vendor("Apache Software Foundation");
let powered_by = ev(Confidence::Probable)
.with_service("http")
.with_extrainfo("PHP/8.2.1");
let verdict = ServiceVerdict::resolve(vec![server, powered_by]);
assert_eq!(
verdict.vendor.as_deref(),
Some("Apache Software Foundation")
);
assert_eq!(verdict.extrainfo.as_deref(), Some("PHP/8.2.1"));
let service = verdict.to_service().expect("names a service");
assert_eq!(service.product(), Some("Apache"));
assert_eq!(service.vendor(), Some("Apache Software Foundation"));
assert_eq!(service.extrainfo(), Some("PHP/8.2.1"));
}
#[test]
fn verdict_projects_onto_service_model() {
let verdict = ServiceVerdict::resolve(vec![
ev(Confidence::Strong)
.with_service("ssh")
.with_product("OpenSSH")
.with_version("9.6"),
]);
let service = verdict.to_service().expect("names a service");
assert_eq!(service.confidence(), Confidence::Strong.as_score());
}
}