use std::cmp::Ordering;
use std::io::BufRead;
use std::sync::OnceLock;
use serde::Deserialize;
use crate::model::confidence::Confidence;
use crate::model::finding::{
DetectionClass, DetectionId, Excerpt, Finding, Reference, Severity, Version,
};
use crate::model::host::Host;
use crate::model::port::Protocol;
use crate::record::wire;
use crate::report::ScanReport;
use crate::version::version_cmp;
const CORRELATOR_ID: &str = "zond:cve-kev";
const RESERVED_PREFIX: &str = "zond:";
const SEED_VERSION: Version = Version::new(0, 2, 0);
const EMBEDDED_DB: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/cve_catalogue.bin"));
pub const MAX_DOCUMENT_BYTES: u64 = 16 * 1024 * 1024;
#[non_exhaustive]
#[derive(Debug, thiserror::Error)]
pub enum CatalogueError {
#[error("the catalogue could not be read: {0}")]
Io(#[from] std::io::Error),
#[error("the catalogue is malformed: {0}")]
Malformed(String),
#[error("a catalogue may not name itself '{id}': '{RESERVED_PREFIX}' is reserved")]
ReservedId {
id: String,
},
#[error("the catalogue is longer than the {limit} byte limit")]
TooLarge {
limit: u64,
},
#[error("'{version}' is not a version: expected major.minor.patch")]
UnreadableVersion {
version: String,
},
}
const MAX_SUMMARY_BYTES: usize = 80;
pub fn correlate(host: &mut Host) {
correlate_with(host, Catalogue::embedded());
}
pub fn correlate_with(host: &mut Host, catalogue: &Catalogue) {
let hits: Vec<(u16, Protocol, Finding)> = host
.ports()
.flat_map(|port| {
let number = port.number();
let protocol = port.protocol();
port.service()
.into_iter()
.flat_map(|service| service.cpes())
.flat_map(|cpe| catalogue.findings_for(cpe))
.map(move |finding| (number, protocol, finding))
.collect::<Vec<_>>()
})
.collect();
for (number, protocol, finding) in hits {
host.add_port_finding(number, protocol, finding);
}
}
pub fn correlate_report(report: &mut ScanReport, catalogue: &Catalogue) {
for host in report.hosts_mut() {
correlate_with(host, catalogue);
}
}
#[derive(Debug, Clone)]
pub struct Catalogue {
id: String,
version: Version,
content_hash: String,
pool: Vec<String>,
vulnerability: Vec<Entry>,
}
#[derive(Debug, Clone, serde::Serialize, Deserialize)]
struct Entry {
cve: u32,
title: u32,
severity: u32,
vendor: u32,
product: u32,
affected: u32,
cwe: Option<u32>,
remediation: Option<u32>,
}
struct Vulnerability<'a> {
cve: &'a str,
title: &'a str,
severity: &'a str,
vendor: &'a str,
product: &'a str,
affected: &'a str,
cwe: Option<u32>,
remediation: Option<&'a str>,
}
#[derive(Default)]
struct Interner {
pool: Vec<String>,
seen: std::collections::HashMap<String, u32>,
}
impl Interner {
fn intern(&mut self, value: &str) -> u32 {
if let Some(index) = self.seen.get(value) {
return *index;
}
let index = self.pool.len() as u32;
self.pool.push(value.to_string());
self.seen.insert(value.to_string(), index);
index
}
fn maybe(&mut self, value: Option<&str>) -> Option<u32> {
value.map(|value| self.intern(value))
}
}
impl Catalogue {
pub fn embedded() -> &'static Self {
static EMBEDDED: OnceLock<Catalogue> = OnceLock::new();
EMBEDDED.get_or_init(|| {
let (pool, vulnerability) = bincode::deserialize(EMBEDDED_DB)
.expect("the embedded CVE catalogue is the shape build.rs writes");
Catalogue {
id: CORRELATOR_ID.to_string(),
version: SEED_VERSION,
content_hash: content_hash(EMBEDDED_DB),
pool,
vulnerability,
}
})
}
pub fn read(input: &mut dyn BufRead) -> Result<Self, CatalogueError> {
let mut source = String::new();
let read = {
use std::io::Read as _;
std::io::Read::take(input, MAX_DOCUMENT_BYTES.saturating_add(1))
.read_to_string(&mut source)?
};
if read as u64 > MAX_DOCUMENT_BYTES {
return Err(CatalogueError::TooLarge {
limit: MAX_DOCUMENT_BYTES,
});
}
let document: CatalogueDocument = toml::from_str(&source)
.map_err(|error| CatalogueError::Malformed(error.to_string()))?;
let id = document
.id
.ok_or_else(|| CatalogueError::Malformed("the document names no 'id'".to_string()))?;
if id.starts_with(RESERVED_PREFIX) {
return Err(CatalogueError::ReservedId { id });
}
let version = document.version.ok_or_else(|| {
CatalogueError::Malformed("the document names no 'version'".to_string())
})?;
let parsed = version
.parse()
.map_err(|_| CatalogueError::UnreadableVersion { version })?;
let (pool, vulnerability) = intern_all(&document.vulnerability);
Ok(Self {
id,
version: parsed,
content_hash: content_hash(source.as_bytes()),
pool,
vulnerability,
})
}
pub fn id(&self) -> &str {
&self.id
}
pub fn version(&self) -> Version {
self.version
}
pub fn content_hash(&self) -> &str {
&self.content_hash
}
pub fn len(&self) -> usize {
self.vulnerability.len()
}
pub fn is_empty(&self) -> bool {
self.vulnerability.is_empty()
}
fn view(&self, entry: &Entry) -> Vulnerability<'_> {
let at = |index: u32| self.pool[index as usize].as_str();
Vulnerability {
cve: at(entry.cve),
title: at(entry.title),
severity: at(entry.severity),
vendor: at(entry.vendor),
product: at(entry.product),
affected: at(entry.affected),
cwe: entry.cwe,
remediation: entry.remediation.map(at),
}
}
fn findings_for(&self, cpe: &str) -> Vec<Finding> {
let Some(parsed) = Cpe::parse(cpe) else {
return Vec::new();
};
let mut matched: Vec<Vulnerability<'_>> = self
.vulnerability
.iter()
.map(|entry| self.view(entry))
.filter(|vulnerability| vulnerability.matches(&parsed))
.collect();
matched.sort_by(|a, b| {
wire::severity(b.severity)
.cmp(&wire::severity(a.severity))
.then_with(|| a.cve.cmp(b.cve))
});
matched.dedup_by(|a, b| a.cve == b.cve);
let (checked, unchecked): (Vec<_>, Vec<_>) = matched
.into_iter()
.partition(|vulnerability| vulnerability.constrains_the_version());
[checked, unchecked]
.into_iter()
.filter_map(|group| match group.as_slice() {
[] => None,
[only] => only.to_finding(cpe, &parsed, self),
many => self.summary_of(cpe, &parsed, many),
})
.collect()
}
fn summary_of(
&self,
cpe: &str,
parsed: &Cpe,
matched: &[Vulnerability<'_>],
) -> Option<Finding> {
let worst = matched.first()?;
let severity = wire::severity(worst.severity)?;
let detection =
DetectionId::new(self.id.clone(), self.version, self.content_hash.clone()).ok()?;
let checked = worst.constrains_the_version();
let confidence = match checked {
true => Confidence::Probable,
false => Confidence::Weak,
};
let counted = |wanted: Severity| {
matched
.iter()
.filter(|entry| wire::severity(entry.severity) == Some(wanted))
.count()
};
let critical = counted(Severity::Critical);
let high = counted(Severity::High);
let software = match parsed.version.is_empty() {
true => worst.product.to_string(),
false => format!("{} {}", worst.product, parsed.version),
};
let title = format!("{software} has {} known vulnerabilities", matched.len());
let named: Vec<&str> = matched
.iter()
.take(MAX_NAMED_IN_EXCERPT)
.map(|entry| entry.cve)
.collect();
let excerpt = match checked {
true => format!(
"{cpe} matches {} known vulnerabilities, {critical} critical and {high} high. The worst are {}",
matched.len(),
named.join(", ")
),
false => format!(
"{cpe} matches {} entries naming this software at any version: the version found was not checked against anything. They include {}",
matched.len(),
named.join(", ")
),
};
let mut finding = Finding::new(
detection,
title,
severity,
confidence,
DetectionClass::Passive,
)
.ok()?
.with_excerpt(Excerpt::new(excerpt));
for entry in matched.iter() {
if let Some(reference) = Reference::cve(entry.cve) {
finding = finding.with_reference(reference);
}
}
if let Some(cwe) = worst.cwe {
finding = finding.with_reference(Reference::cwe(cwe));
}
Some(finding)
}
}
#[derive(Debug, Default, Deserialize)]
struct CatalogueDocument {
#[serde(default)]
id: Option<String>,
#[serde(default)]
version: Option<String>,
#[serde(default)]
vulnerability: Vec<DocumentEntry>,
}
fn content_hash(bytes: &[u8]) -> String {
let digest = ring::digest::digest(&ring::digest::SHA256, bytes);
let mut hex = String::with_capacity(digest.as_ref().len() * 2);
for byte in digest.as_ref() {
hex.push_str(&format!("{byte:02x}"));
}
hex
}
const MAX_NAMED_IN_EXCERPT: usize = 3;
fn intern_all(entries: &[DocumentEntry]) -> (Vec<String>, Vec<Entry>) {
let mut interner = Interner::default();
let interned = entries
.iter()
.map(|entry| Entry {
cve: interner.intern(&entry.cve),
title: interner.intern(&entry.title),
severity: interner.intern(&entry.severity),
vendor: interner.intern(&entry.vendor),
product: interner.intern(&entry.product),
affected: interner.intern(&entry.affected),
cwe: entry.cwe,
remediation: interner.maybe(entry.remediation.as_deref()),
})
.collect();
(interner.pool, interned)
}
#[derive(Debug, Clone, Deserialize)]
struct DocumentEntry {
cve: String,
title: String,
severity: String,
vendor: String,
product: String,
affected: String,
#[serde(default)]
cwe: Option<u32>,
#[serde(default)]
remediation: Option<String>,
}
impl Vulnerability<'_> {
fn matches(&self, cpe: &Cpe) -> bool {
self.vendor.eq_ignore_ascii_case(&cpe.vendor)
&& self.product.eq_ignore_ascii_case(&cpe.product)
&& version_matches(&cpe.version, self.affected)
}
fn constrains_the_version(&self) -> bool {
self.affected.trim() != "*"
}
fn to_finding(&self, cpe: &str, parsed: &Cpe, catalogue: &Catalogue) -> Option<Finding> {
let severity = wire::severity(self.severity)?;
let detection = DetectionId::new(
catalogue.id.clone(),
catalogue.version,
catalogue.content_hash.clone(),
)
.ok()?;
let (confidence, excerpt) = match self.constrains_the_version() {
true => (
Confidence::Probable,
format!(
"{cpe} matches {} {} {}",
self.vendor, self.product, self.affected
),
),
false => (
Confidence::Weak,
format!(
"{cpe} is {} {}, which this entry names at any version: \
the version found was not checked against anything",
self.vendor, self.product
),
),
};
let (title, excerpt) = match self.title.len() <= MAX_SUMMARY_BYTES {
true => (self.title.to_string(), excerpt),
false => {
let software = match parsed.version.is_empty() {
true => self.product.to_string(),
false => format!("{} {}", self.product, parsed.version),
};
(
format!("{software} has 1 known vulnerability"),
format!("{excerpt}. {}", self.title),
)
}
};
let mut finding = Finding::new(
detection,
title,
severity,
confidence,
DetectionClass::Passive,
)
.ok()?
.with_reference(Reference::cve(self.cve)?)
.with_excerpt(Excerpt::new(excerpt));
if let Some(cwe) = self.cwe {
finding = finding.with_reference(Reference::cwe(cwe));
}
if let Some(remediation) = self.remediation {
finding = finding.with_remediation(remediation);
}
Some(finding)
}
}
struct Cpe {
vendor: String,
product: String,
version: String,
}
impl Cpe {
fn parse(cpe: &str) -> Option<Self> {
let body = cpe
.strip_prefix("cpe:/")
.or_else(|| cpe.strip_prefix("cpe:2.3:"))?;
let mut fields = body.split(':');
let _part = fields.next()?; let vendor = fields.next()?;
let product = fields.next()?;
let version = fields.next().unwrap_or("");
let update = fields
.next()
.filter(|value| !value.is_empty() && *value != "*" && *value != "-");
Some(Self {
vendor: vendor.to_ascii_lowercase(),
product: product.to_ascii_lowercase(),
version: match update {
Some(update) => format!("{version}{update}"),
None => version.to_string(),
},
})
}
}
fn version_matches(version: &str, predicate: &str) -> bool {
let predicate = predicate.trim();
if predicate == "*" {
return true;
}
if version.is_empty() || version == "-" || version == "*" {
return false;
}
predicate
.split(',')
.all(|clause| clause_holds(version, clause.trim()))
}
fn clause_holds(version: &str, clause: &str) -> bool {
let (op, bound) = ["<=", ">=", "==", "<", ">", "="]
.into_iter()
.find_map(|op| clause.strip_prefix(op).map(|rest| (op, rest.trim())))
.unwrap_or(("==", clause));
let ordering = version_cmp(version, bound);
match op {
"<" => ordering == Ordering::Less,
"<=" => ordering != Ordering::Greater,
">" => ordering == Ordering::Greater,
">=" => ordering != Ordering::Less,
"==" | "=" => ordering == Ordering::Equal,
_ => false,
}
}
#[cfg(test)]
mod tests {
use std::collections::BTreeSet;
const UNCOVERED: &[(&str, &str)] = &[
("avocent:dsview", "no records in NVD"),
("darkhttpd_project:darkhttpd", "no records in NVD"),
("mcafee:webshield", "no records in NVD"),
("novell:netware_enterprise_web_server", "no records in NVD"),
("zaphoyd:websocketpp", "no records in NVD"),
(
"treck:tcp%2fip",
"import artifact in the identifier, and no reachable records",
),
];
fn covered() -> BTreeSet<String> {
let catalogue = Catalogue::embedded();
catalogue
.vulnerability
.iter()
.map(|entry| {
format!(
"{}:{}",
catalogue.pool[entry.vendor as usize], catalogue.pool[entry.product as usize]
)
})
.collect()
}
#[test]
fn a_description_too_long_to_be_a_summary_becomes_one_and_moves_to_the_excerpt() {
let long = "The IP Security feature in Microsoft Internet Information Services (IIS) \
8.0 and 8.5 does not properly process wildcard allow and deny rules for \
domains within";
let document = format!(
"id = \"test:cve\"\nversion = \"1.0.0\"\n\n\
[[vulnerability]]\ncve = \"CVE-2014-4078\"\ntitle = \"{long}\"\n\
severity = \"medium\"\nvendor = \"microsoft\"\n\
product = \"internet_information_services\"\naffected = \"== 8.5\"\n"
);
let catalogue =
Catalogue::read(&mut document.as_bytes()).expect("a catalogue naming one entry");
let findings = catalogue.findings_for("cpe:/a:microsoft:internet_information_services:8.5");
let finding = findings.first().expect("the entry matches");
assert_eq!(
finding.title(),
"internet_information_services 8.5 has 1 known vulnerability"
);
let excerpt = finding.excerpt().as_str();
assert!(
excerpt.contains("IP Security feature"),
"the description is kept as evidence, not discarded: {excerpt}"
);
}
#[test]
fn a_title_short_enough_to_be_a_summary_is_used_as_one() {
let document = "id = \"test:cve\"\nversion = \"1.0.0\"\n\n\
[[vulnerability]]\ncve = \"CVE-2021-41773\"\n\
title = \"Apache HTTP Server path traversal and RCE\"\n\
severity = \"critical\"\nvendor = \"apache\"\nproduct = \"http_server\"\n\
affected = \"== 2.4.49\"\n";
let catalogue = Catalogue::read(&mut document.as_bytes()).expect("a catalogue");
let findings = catalogue.findings_for("cpe:/a:apache:http_server:2.4.49");
assert_eq!(
findings.first().expect("the entry matches").title(),
"Apache HTTP Server path traversal and RCE"
);
}
#[test]
fn every_versioned_product_is_covered_or_listed() {
let covered = covered();
let listed: BTreeSet<&str> = UNCOVERED.iter().map(|(key, _)| *key).collect();
let unexplained: Vec<&String> = crate::fingerprint::SignatureDb::global()
.versioned_products()
.iter()
.filter(|key| !covered.contains(*key) && !listed.contains(key.as_str()))
.collect();
assert!(
unexplained.is_empty(),
"a scan can put a version to these and the catalogue has no row for any of them: \
{unexplained:?}. Regenerate the catalogue, correct the identifier, or add it to \
UNCOVERED with the reason."
);
}
#[test]
fn nothing_listed_as_uncovered_is_covered() {
let covered = covered();
let stale: Vec<&str> = UNCOVERED
.iter()
.map(|(key, _)| *key)
.filter(|key| covered.contains(*key))
.collect();
assert!(
stale.is_empty(),
"these are listed as having no rows and the catalogue has rows for them: {stale:?}. \
Remove them from UNCOVERED."
);
}
#[test]
fn the_two_cpe_forms_of_one_release_read_the_same_version() {
let uri = Cpe::parse("cpe:/a:openbsd:openssh:9.6p1").expect("the URI form");
let two_three =
Cpe::parse("cpe:2.3:a:openbsd:openssh:9.6:p1:*:*:*:*:*:*").expect("the 2.3 form");
assert_eq!(uri.version, two_three.version);
assert_eq!(two_three.version, "9.6p1");
}
#[test]
fn an_unset_update_field_is_not_appended() {
for cpe in [
"cpe:2.3:a:nginx:nginx:1.21.0:*:*:*:*:*:*:*",
"cpe:2.3:a:nginx:nginx:1.21.0:-:*:*:*:*:*:*",
"cpe:2.3:a:nginx:nginx:1.21.0",
] {
assert_eq!(Cpe::parse(cpe).expect("parses").version, "1.21.0", "{cpe}");
}
}
#[test]
fn a_pre_release_satisfies_a_bound_written_against_its_release() {
assert!(version_matches("1.0.0-rc1", "<1.0.0"));
assert!(version_matches("0.9.9", "<1.0.0"));
assert!(!version_matches("1.0.0", "<1.0.0"));
assert!(!version_matches("1.21.0-1ubuntu2", "<1.21.0"));
}
use super::*;
use crate::model::finding::Severity;
#[test]
fn version_predicates_hold_and_fail_at_the_boundaries() {
assert!(version_matches("2.4.49", "== 2.4.49"));
assert!(!version_matches("2.4.50", "== 2.4.49"));
assert!(version_matches("9.6", ">= 8.5, < 9.8"));
assert!(!version_matches("9.8", ">= 8.5, < 9.8")); assert!(!version_matches("8.4", ">= 8.5, < 9.8"));
assert!(version_matches("anything", "*"));
assert!(!version_matches("", ">= 1.0"));
assert!(!version_matches("-", ">= 1.0"));
}
#[test]
fn many_matches_become_one_finding_that_counts_them() {
let mut document = String::from("id = \"acme:advisories\"\nversion = \"1.0.0\"\n");
for (index, severity) in std::iter::once("critical")
.chain(std::iter::once("high"))
.chain(std::iter::repeat_n("medium", 24))
.enumerate()
{
document.push_str(&format!(
"\n[[vulnerability]]\ncve = \"CVE-2024-{:04}\"\ntitle = \"Something {index}\"\n\
severity = \"{severity}\"\nvendor = \"apache\"\nproduct = \"tomcat\"\n\
affected = \"== 9.0.71\"\n",
index + 1
));
}
let catalogue = Catalogue::read(&mut document.as_bytes()).expect("a valid document");
let hits = catalogue.findings_for("cpe:/a:apache:tomcat:9.0.71");
assert_eq!(hits.len(), 1, "twenty-six entries, one finding");
let summary = &hits[0];
assert_eq!(
summary.title(),
"tomcat 9.0.71 has 26 known vulnerabilities"
);
assert_eq!(
summary.severity(),
Severity::Critical,
"the summary is as bad as the worst of them"
);
assert!(summary.excerpt().as_str().contains("1 critical and 1 high"));
let cves: Vec<&str> = summary
.references()
.filter_map(|reference| match reference {
Reference::Cve(id) => Some(id.as_str()),
_ => None,
})
.collect();
assert_eq!(cves.len(), 26, "the title says 26 and 26 are cited");
assert!(cves.contains(&"CVE-2024-0001"), "the critical one is named");
assert!(cves.contains(&"CVE-2024-0002"), "and the high one");
}
#[test]
fn a_summary_never_averages_the_two_kinds_of_claim() {
let mut document = String::from("id = \"acme:advisories\"\nversion = \"1.0.0\"\n");
for (index, affected) in ["== 2.4.49", "== 2.4.49", "*", "*"].iter().enumerate() {
document.push_str(&format!(
"\n[[vulnerability]]\ncve = \"CVE-2021-{:04}\"\ntitle = \"Entry {index}\"\n\
severity = \"high\"\nvendor = \"apache\"\nproduct = \"http_server\"\n\
affected = \"{affected}\"\n",
index + 1
));
}
let catalogue = Catalogue::read(&mut document.as_bytes()).expect("a valid document");
let hits = catalogue.findings_for("cpe:/a:apache:http_server:2.4.49");
assert_eq!(hits.len(), 2, "one summary per kind of claim");
let checked = hits
.iter()
.find(|finding| finding.confidence() == Confidence::Probable)
.expect("the version-checked summary");
assert!(
checked
.excerpt()
.as_str()
.contains("2 known vulnerabilities")
);
let unchecked = hits
.iter()
.find(|finding| finding.confidence() == Confidence::Weak)
.expect("the unchecked summary");
assert!(
unchecked.excerpt().as_str().contains("not checked"),
"a summary of unbounded entries still says the version was never in question"
);
}
#[test]
fn an_entry_that_names_no_version_is_weaker_than_one_that_does() {
use crate::model::confidence::Confidence;
let document = r#"
id = "acme:advisories"
version = "1.0.0"
[[vulnerability]]
cve = "CVE-2021-41773"
title = "Bounded to the affected releases"
severity = "critical"
vendor = "apache"
product = "http_server"
affected = "== 2.4.49"
[[vulnerability]]
cve = "CVE-2021-44228"
title = "Named at any version"
severity = "critical"
vendor = "apache"
product = "http_server"
affected = "*"
"#;
let catalogue = Catalogue::read(&mut document.as_bytes()).expect("a valid document");
let hits = catalogue.findings_for("cpe:/a:apache:http_server:2.4.49");
assert_eq!(hits.len(), 2);
let bounded = hits
.iter()
.find(|finding| finding.title().contains("Bounded"))
.expect("the version-bounded entry matched");
assert_eq!(bounded.confidence(), Confidence::Probable);
assert!(bounded.excerpt().as_str().contains("2.4.49"));
let unbounded = hits
.iter()
.find(|finding| finding.title().contains("any version"))
.expect("the unbounded entry matched");
assert_eq!(unbounded.confidence(), Confidence::Weak);
assert!(
unbounded.excerpt().as_str().contains("not checked"),
"the excerpt has to say the version was never in question"
);
let patched = catalogue.findings_for("cpe:/a:apache:http_server:2.4.62");
assert_eq!(patched.len(), 1);
assert_eq!(patched[0].confidence(), Confidence::Weak);
}
#[test]
fn the_embedded_seed_parses_and_is_non_empty() {
assert!(!Catalogue::embedded().vulnerability.is_empty());
}
fn embedded_cves(cpe: &str) -> Vec<String> {
Catalogue::embedded()
.findings_for(cpe)
.iter()
.flat_map(|finding| finding.references())
.filter_map(|reference| match reference {
Reference::Cve(id) => Some(id.clone()),
_ => None,
})
.collect()
}
#[test]
fn a_matching_cpe_yields_a_probable_finding_a_safe_version_does_not() {
let hits = Catalogue::embedded().findings_for("cpe:/a:apache:http_server:2.4.49");
let finding = hits.first().expect("2.4.49 is a vulnerable build");
assert_eq!(finding.severity(), Severity::Critical);
assert_eq!(
finding.confidence(),
Confidence::Probable,
"a version match is potentially affected, unverified — never certain"
);
assert_eq!(finding.detection().id(), "zond:cve-kev");
assert!(
embedded_cves("cpe:/a:apache:http_server:2.4.49")
.contains(&"CVE-2021-41773".to_string())
);
assert!(
!embedded_cves("cpe:/a:apache:http_server:2.4.51")
.contains(&"CVE-2021-41773".to_string()),
"the range stops at 2.4.49 and the fixed build is outside it"
);
assert!(
Catalogue::embedded()
.findings_for("cpe:/a:nginx:nginx:1.24.0")
.is_empty(),
"a vendor nothing is keyed under does not match: nginx is `f5:nginx`"
);
assert!(
embedded_cves("cpe:/a:openbsd:openssh:9.6p1").contains(&"CVE-2024-6387".to_string())
);
assert!(
!embedded_cves("cpe:/a:openbsd:openssh:9.8p1").contains(&"CVE-2024-6387".to_string()),
"the fixed OpenSSH release is outside the range"
);
assert!(
embedded_cves("cpe:/a:vsftpd_project:vsftpd:2.3.4")
.contains(&"CVE-2011-2523".to_string())
);
assert!(
!embedded_cves("cpe:/a:vsftpd_project:vsftpd:3.0.5")
.contains(&"CVE-2011-2523".to_string())
);
}
#[test]
fn a_catalogue_read_from_outside_stamps_its_own_identity_on_what_it_finds() {
use crate::model::host::Host;
use crate::model::port::{Port, PortState, Service};
use std::io::Cursor;
let document = r#"
id = "acme:advisories"
version = "3.2.1"
[[vulnerability]]
cve = "CVE-2021-41773"
title = "Apache HTTP Server path traversal and RCE"
severity = "critical"
vendor = "apache"
product = "http_server"
affected = "== 2.4.49"
"#;
let catalogue =
Catalogue::read(&mut Cursor::new(document)).expect("a well-formed catalogue");
assert_eq!(catalogue.id(), "acme:advisories");
assert_eq!(catalogue.version(), Version::new(3, 2, 1));
assert_eq!(catalogue.len(), 1);
let mut host = Host::new("192.0.2.1".parse().expect("an address"));
let service = Service::new("http", 90).with_cpe("cpe:/a:apache:http_server:2.4.49");
host.add_port(Port::new(80, Protocol::Tcp, PortState::Open).with_service(service));
correlate_with(&mut host, &catalogue);
let port = host
.ports()
.find(|port| port.number() == 80)
.expect("the port survives");
let finding = port.findings().next().expect("the catalogue matched");
assert_eq!(finding.detection().id(), "acme:advisories");
assert_eq!(finding.detection().version(), Version::new(3, 2, 1));
assert_eq!(
finding.detection().content_hash(),
catalogue.content_hash(),
"the finding names the bytes it was drawn from"
);
assert_ne!(
catalogue.content_hash(),
Catalogue::embedded().content_hash(),
"two catalogues are two documents"
);
}
#[test]
fn a_report_correlated_against_a_second_catalogue_carries_both_attributions() {
use crate::model::host::Host;
use crate::model::port::{Port, PortState, Service};
use crate::report::ScanReport;
use std::io::Cursor;
let document = r#"
id = "acme:advisories"
version = "3.2.1"
[[vulnerability]]
cve = "CVE-2021-41773"
title = "Apache, as our own analysts wrote it up"
severity = "high"
vendor = "apache"
product = "http_server"
affected = "== 2.4.49"
"#;
let mut host = Host::new("192.0.2.1".parse().expect("an address"));
let service = Service::new("http", 90).with_cpe("cpe:/a:apache:http_server:2.4.49");
host.add_port(Port::new(80, Protocol::Tcp, PortState::Open).with_service(service));
correlate(&mut host);
let mut report =
ScanReport::new(crate::export::fixture::report().phases()[0].clone(), [host]);
let catalogue =
Catalogue::read(&mut Cursor::new(document)).expect("a well-formed catalogue");
correlate_report(&mut report, &catalogue);
let attributions: Vec<&str> = report
.hosts()
.flat_map(|host| host.ports())
.flat_map(|port| port.findings())
.map(|finding| finding.detection().id())
.collect();
assert!(attributions.contains(&CORRELATOR_ID), "{attributions:?}");
assert!(
attributions.contains(&"acme:advisories"),
"{attributions:?}"
);
}
#[test]
fn a_catalogue_longer_than_the_ceiling_is_refused_without_being_held() {
use std::io::Cursor;
let mut over = String::from("id = \"oversize\"\nversion = \"1.0.0\"\n");
let filler = MAX_DOCUMENT_BYTES as usize + 1 - over.len();
over.push_str(&"#".repeat(filler));
assert_eq!(over.len() as u64, MAX_DOCUMENT_BYTES + 1);
let error =
Catalogue::read(&mut Cursor::new(&over)).expect_err("an oversize document is refused");
assert!(
matches!(error, CatalogueError::TooLarge { limit } if limit == MAX_DOCUMENT_BYTES),
"got {error:?}"
);
let at = &over[..MAX_DOCUMENT_BYTES as usize];
assert!(
Catalogue::read(&mut Cursor::new(at)).is_ok(),
"a document exactly at the ceiling was refused"
);
}
#[test]
fn a_catalogue_may_not_name_itself_in_this_engines_namespace() {
use std::io::Cursor;
let document = "id = \"zond:cve-kev\"\nversion = \"1.0.0\"\n";
let error = Catalogue::read(&mut Cursor::new(document))
.expect_err("the reserved prefix is refused");
assert!(
matches!(&error, CatalogueError::ReservedId { id } if id == "zond:cve-kev"),
"got {error:?}"
);
}
#[test]
fn a_catalogue_that_does_not_name_itself_is_refused() {
use std::io::Cursor;
for document in [
"version = \"1.0.0\"\n",
"id = \"acme:advisories\"\n",
"id = \"acme:advisories\"\nversion = \"today\"\n",
] {
assert!(
Catalogue::read(&mut Cursor::new(document)).is_err(),
"accepted {document:?}"
);
}
}
#[test]
fn correlate_records_a_finding_on_the_matching_port_and_does_not_double() {
use crate::model::host::Host;
use crate::model::port::{Port, PortState, Service};
use std::net::{IpAddr, Ipv4Addr};
let mut host = Host::new(IpAddr::V4(Ipv4Addr::new(192, 0, 2, 1)));
let service = Service::new("http", 90).with_cpe("cpe:/a:apache:http_server:2.4.49");
host.add_port(Port::new(80, Protocol::Tcp, PortState::Open).with_service(service));
correlate(&mut host);
let port = host.ports().find(|p| p.number() == 80).unwrap();
assert_eq!(port.findings().count(), 1, "the vulnerable service got one");
correlate(&mut host);
let port = host.ports().find(|p| p.number() == 80).unwrap();
assert_eq!(port.findings().count(), 1);
}
}