///|
#derive(Debug, ToJson)
pub(all) struct Finding {
id : String
name : String
evidence : String
}
///|
#derive(Debug, ToJson)
pub struct DetectionReport {
findings : Array[Finding]
errors : Array[String]
truncated : Bool
}
///|
/// Trusted, statically linked detectors inspect existing evidence. The interface
/// supplies no network client: detectors must not issue additional requests.
/// Implementations must yield during long computations to honor cancellation.
pub(open) trait Detector {
fn id(Self) -> String
async fn inspect(Self, ScanResult) -> Array[Finding]
}
///|
/// Evaluate each detector with a time budget. Ordinary detector failures are
/// reported separately and never change the TCP port state. Findings are capped.
pub async fn detect(
result : ScanResult,
detectors : ArrayView[&Detector],
timeout_ms~ : Int32 = 100,
max_findings~ : Int32 = 32,
) -> DetectionReport {
guard timeout_ms > 0 && max_findings > 0 && max_findings <= 1024 else {
raise InputError(
"detector timeout must be positive; finding limit must be in 1..1024",
)
}
let findings = []
let errors = []
let mut truncated = false
for detector in detectors {
try @async.with_timeout(timeout_ms, () => detector.inspect(result)) catch {
err => errors.push("\{detector.id()}: \{err}")
} noraise {
records =>
for finding in records {
if findings.length() < max_findings {
findings.push(finding)
} else {
truncated = true
}
}
}
}
{ findings, errors, truncated, }
}
///|
pub fn ScanResult::with_detection(
self : ScanResult,
report : DetectionReport,
) -> ScanResult {
{ ..self, detection: Some(report), }
}
///|
/// Basic inventory observations. These findings are not vulnerability claims.
pub struct InventoryDetector {
priv version : Int32
}
///|
pub fn InventoryDetector::new() -> InventoryDetector {
{ version: 1, }
}
///|
pub impl Detector for InventoryDetector with fn id(self) {
"inventory.v\{self.version}"
}
///|
pub impl Detector for InventoryDetector with fn inspect(_, result) {
@async.pause()
let findings = []
if result.service is Some(info) {
if info.protocol == "ssh" && info.banner is Some(banner) {
findings.push({ id: "ssh.banner", name: "SSH service", evidence: banner, })
}
if info.server is Some(server) {
findings.push({
id: "http.server",
name: "HTTP Server header",
evidence: server,
})
}
if info.certificate_der_base64 is Some(_) {
findings.push({
id: "tls.certificate",
name: "TLS peer certificate collected",
evidence: "service.certificate_der_base64; trust verification disabled",
})
}
}
findings
}