use std::collections::BTreeSet;
use crate::model::confidence::Confidence;
use crate::model::finding::{DetectionClass, DetectionId, Excerpt, Finding, Version};
use crate::record::wire;
use super::schema::{FindingSpec, HostDetection};
pub struct LoadedHostDetection {
detection: HostDetection,
content_hash: String,
}
impl LoadedHostDetection {
pub fn new(detection: HostDetection, content_hash: impl Into<String>) -> Self {
Self {
detection,
content_hash: content_hash.into(),
}
}
pub(crate) fn id(&self) -> &str {
&self.detection.detection.id
}
pub(crate) fn title(&self) -> &str {
&self.detection.detection.title
}
pub(crate) fn version(&self) -> &str {
&self.detection.detection.version
}
pub(crate) fn ports_open(&self) -> &[u16] {
&self.detection.detection.host.ports_open
}
pub(crate) fn services(&self) -> &[String] {
&self.detection.detection.host.services
}
pub(crate) fn content_hash(&self) -> &str {
&self.content_hash
}
}
pub(crate) fn detect_host(
detections: &[LoadedHostDetection],
open_ports: &BTreeSet<u16>,
services: &BTreeSet<&str>,
) -> Vec<Finding> {
let mut findings = Vec::new();
for loaded in detections {
let manifest = &loaded.detection.detection;
if !manifest.host.matches(open_ports, services) {
continue;
}
let version = manifest.version.parse().unwrap_or(Version::new(0, 0, 0));
let Ok(id) = DetectionId::new(manifest.id.clone(), version, &loaded.content_hash) else {
continue;
};
for spec in &loaded.detection.finding {
if let Some(finding) = build_finding(spec, &id, &manifest.title) {
findings.push(finding);
}
}
}
findings
}
fn build_finding(spec: &FindingSpec, id: &DetectionId, fallback_title: &str) -> Option<Finding> {
let title = spec
.title
.as_deref()
.filter(|title| !title.trim().is_empty())
.or_else(|| Some(spec.summary.as_str()).filter(|summary| !summary.trim().is_empty()))
.unwrap_or(fallback_title)
.to_string();
let confidence = spec
.confidence
.as_deref()
.and_then(wire::confidence)
.unwrap_or(Confidence::Certain);
let mut finding = Finding::new(
id.clone(),
title,
spec.severity.into_model(),
confidence,
DetectionClass::Passive,
)
.ok()?;
if let Some(detail) = spec.detail.as_deref().filter(|d| !d.trim().is_empty()) {
finding = finding.with_excerpt(Excerpt::new(detail.to_owned()));
}
for reference in &spec.references {
if let Some(reference) = reference.to_model() {
finding = finding.with_reference(reference);
}
}
if let Some(remediation) = spec.remediation.as_deref().filter(|r| !r.trim().is_empty()) {
finding = finding.with_remediation(remediation.to_owned());
}
Some(finding)
}
#[cfg(test)]
mod tests {
use super::super::authoring::Severity;
use super::super::schema::{FindingSpec, HostGate, HostManifest};
use super::*;
use crate::model::finding::Severity as ModelSeverity;
fn domain_controller() -> LoadedHostDetection {
LoadedHostDetection::new(
HostDetection {
detection: HostManifest {
id: "domain-controller".to_string(),
version: "1.0.0".to_string(),
title: "Windows domain controller".to_string(),
host: HostGate {
ports_open: vec![88, 389, 445],
services: Vec::new(),
},
},
finding: vec![FindingSpec {
severity: Severity::Info,
summary: "Kerberos, LDAP and SMB open together: a domain controller"
.to_string(),
title: None,
detail: None,
confidence: None,
references: Vec::new(),
remediation: None,
}],
},
"hash",
)
}
#[test]
fn a_host_presenting_every_gate_port_draws_the_finding() {
let open: BTreeSet<u16> = [53, 88, 135, 389, 445].into_iter().collect();
let findings = detect_host(&[domain_controller()], &open, &BTreeSet::new());
assert_eq!(findings.len(), 1);
assert_eq!(findings[0].detection().id(), "domain-controller");
assert_eq!(findings[0].severity(), ModelSeverity::Info);
}
#[test]
fn a_host_missing_one_gate_port_draws_nothing() {
let open: BTreeSet<u16> = [88, 389].into_iter().collect();
let findings = detect_host(&[domain_controller()], &open, &BTreeSet::new());
assert!(
findings.is_empty(),
"a host missing SMB was still called a domain controller"
);
}
}