use std::net::{IpAddr, SocketAddr};
use std::sync::OnceLock;
use crate::fingerprint::PortContext;
use crate::model::finding::Finding;
use crate::record::wire;
use super::budget::RunOutcome;
use super::record::DetectionRunRecord;
use super::rhai::{RhaiModule, RhaiRuntime};
use super::runtime::{ComputeRuntime, LoadError, ModuleBody};
use super::schema::ComputeDetection;
use super::stage::{self, LoadedDetection};
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum ReplayError {
#[error("the corpus no longer holds the detection this run named")]
UnknownDetection,
#[error("the run names a transport this build does not know: {0}")]
UnknownTransport(String),
#[error("the detection's identity would not resolve into a grant")]
GrantFailed,
#[error("the module could not be instantiated for replay: {0}")]
Instantiate(#[source] LoadError),
#[error("the replay ended abnormally rather than reproducing the run")]
Run(RunOutcome),
#[error("the tape was too short to reproduce the run")]
Diverged,
}
const EMBEDDED: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/detect_modules.bin"));
static DB: OnceLock<ComputeDb> = OnceLock::new();
pub(crate) struct ComputeDb {
runtime: RhaiRuntime,
detections: Vec<LoadedDetection<RhaiModule>>,
}
impl ComputeDb {
pub(crate) fn global() -> &'static ComputeDb {
DB.get_or_init(ComputeDb::from_embedded)
}
pub(crate) fn from_embedded() -> ComputeDb {
let runtime = RhaiRuntime::new();
let detections = load_embedded(&runtime);
ComputeDb {
runtime,
detections,
}
}
pub(crate) fn from_parts(
runtime: RhaiRuntime,
detections: Vec<LoadedDetection<RhaiModule>>,
) -> ComputeDb {
ComputeDb {
runtime,
detections,
}
}
pub(crate) fn runtime(&self) -> &RhaiRuntime {
&self.runtime
}
pub(crate) fn detections(&self) -> &[LoadedDetection<RhaiModule>] {
&self.detections
}
pub(crate) fn detection_by_hash(
&self,
content_hash: &str,
) -> Option<&LoadedDetection<RhaiModule>> {
self.detections
.iter()
.find(|detection| detection.content_hash() == content_hash)
}
}
pub fn replay_run(run: &DetectionRunRecord) -> Result<Vec<Finding>, ReplayError> {
let db = ComputeDb::global();
let detection = db
.detection_by_hash(&run.detection.content_hash)
.ok_or(ReplayError::UnknownDetection)?;
let protocol = wire::protocol(&run.protocol)
.ok_or_else(|| ReplayError::UnknownTransport(run.protocol.clone()))?;
let addr = run
.host
.parse::<IpAddr>()
.ok()
.map(|ip| SocketAddr::new(ip, run.port));
let ctx = PortContext {
port: run.port,
protocol,
addr,
tunnel: None,
speaks_http: false,
detection: crate::config::ServiceDetection::default(),
host_name: run.host_name.clone(),
};
let responses: Vec<Vec<u8>> = run
.responses
.iter()
.cloned()
.map(String::into_bytes)
.collect();
let slices: Vec<&[u8]> = responses.iter().map(Vec::as_slice).collect();
stage::replay_over_tape(db.runtime(), detection, &ctx, &slices, run.tape.rebuild())
}
fn load_module(
runtime: &RhaiRuntime,
content_hash: &str,
toml: &str,
) -> LoadedDetection<RhaiModule> {
let detection: ComputeDetection = toml::from_str(toml)
.expect("an embedded module was validated at build but did not re-parse at runtime");
let source = detection
.compute
.source
.expect("the build normalises every module to an inline source");
let module = runtime.load(&ModuleBody::Rhai(source)).unwrap_or_else(|error| {
panic!(
"the embedded module '{}' was validated at build but did not compile at runtime: {error}",
detection.detection.id
)
});
LoadedDetection::new(detection.detection, module, content_hash)
}
pub(crate) fn load_embedded(runtime: &RhaiRuntime) -> Vec<LoadedDetection<RhaiModule>> {
let entries: Vec<(String, String)> =
bincode::deserialize(EMBEDDED).expect("embedded module database failed to deserialize");
entries
.into_iter()
.map(|(content_hash, toml)| load_module(runtime, &content_hash, &toml))
.collect()
}
pub(crate) fn compile_compute_source(
runtime: &RhaiRuntime,
toml: &str,
content_hash: &str,
) -> Result<LoadedDetection<RhaiModule>, String> {
use crate::detect::flow::validate::{RESERVED_ID_PREFIX, is_version_triple};
let detection: ComputeDetection = toml::from_str(toml).map_err(|error| error.to_string())?;
let manifest = &detection.detection;
if manifest.id.trim().is_empty() {
return Err("a compute detection needs a non-empty id".to_string());
}
if manifest.id.starts_with(RESERVED_ID_PREFIX) {
return Err(format!(
"a compute detection id `{}` claims the reserved `{RESERVED_ID_PREFIX}` namespace",
manifest.id
));
}
if !is_version_triple(&manifest.version) {
return Err(format!(
"a compute detection has version `{}`, not a major.minor.patch triple",
manifest.version
));
}
let source = detection
.compute
.source
.ok_or_else(|| "a compute detection needs an inline `source`".to_string())?;
let module = runtime
.load(&ModuleBody::Rhai(source))
.map_err(|error| error.to_string())?;
Ok(LoadedDetection::new(
detection.detection,
module,
content_hash,
))
}
#[cfg(test)]
mod tests {
use super::*;
use crate::config::DetectionEnvelope;
use crate::detect::compute::{CapError, Capabilities, ScanInstant};
use crate::fingerprint::PortContext;
use crate::model::finding::Severity;
use crate::model::port::Protocol;
use std::net::IpAddr;
struct NoCaps;
impl Capabilities for NoCaps {
fn speak(&mut self, _bytes: &[u8]) -> Result<Vec<u8>, CapError> {
Ok(Vec::new())
}
fn resolve(&mut self, _name: &str) -> Result<Vec<IpAddr>, CapError> {
Ok(Vec::new())
}
fn now(&mut self) -> ScanInstant {
ScanInstant::from_millis(0)
}
}
fn http_ctx() -> PortContext {
PortContext {
port: 80,
protocol: Protocol::Tcp,
addr: None,
tunnel: None,
speaks_http: false,
detection: crate::config::ServiceDetection::default(),
host_name: None,
}
}
#[test]
fn the_embedded_corpus_compiles_every_module_it_ships() {
let embedded: Vec<(String, String)> =
bincode::deserialize(EMBEDDED).expect("the module database decodes");
assert!(!embedded.is_empty(), "the module corpus is empty");
let db = ComputeDb::global();
assert_eq!(
db.detections().len(),
embedded.len(),
"the module corpus did not load one detection per embedded entry"
);
}
#[test]
fn the_header_detection_flags_a_bare_response_and_clears_a_hardened_one() {
let db = ComputeDb::global();
let run = |response: &[u8]| {
super::super::stage::detect_port(
db.runtime(),
db.detections(),
&DetectionEnvelope::default(),
Some("http"),
&http_ctx(),
&[response],
|_grant| Some(Box::new(NoCaps)),
|_, _| {},
)
.findings
};
let bare = b"HTTP/1.1 200 OK\r\nServer: nginx\r\nContent-Type: text/html\r\n\r\n";
let findings = run(bare);
let finding = findings
.iter()
.find(|f| f.detection().id() == "http-missing-security-headers")
.expect("the header detection fired on a bare response");
assert_eq!(finding.severity(), Severity::Medium);
let hardened = b"HTTP/1.1 200 OK\r\n\
Strict-Transport-Security: max-age=31536000\r\n\
Content-Security-Policy: default-src 'self'\r\n\
X-Frame-Options: DENY\r\n\
X-Content-Type-Options: nosniff\r\n\r\n";
assert!(
!run(hardened)
.iter()
.any(|f| f.detection().id() == "http-missing-security-headers"),
"a hardened server was flagged"
);
let redirect = b"HTTP/1.1 308 Permanent Redirect\r\n\
Location: https://example.com/\r\n\
Content-Length: 0\r\n\r\n";
assert!(
!run(redirect)
.iter()
.any(|f| f.detection().id() == "http-missing-security-headers"),
"a bare http-to-https redirect was graded as a missing-headers finding"
);
}
#[test]
fn a_journalled_run_of_a_shipped_detection_replays() {
use crate::detect::compute::{CapTape, CapTapeRecord, DetectionRunRecord};
use crate::record::DetectionIdRecord;
let db = ComputeDb::global();
let detection = db
.detections()
.iter()
.find(|d| d.manifest().id == "http-missing-security-headers")
.expect("the http detection ships");
let run = DetectionRunRecord {
host: "127.0.0.1".to_string(),
host_name: None,
port: 80,
protocol: "tcp".to_string(),
detection: DetectionIdRecord {
id: "http-missing-security-headers".to_string(),
version: "1.0.0".to_string(),
content_hash: detection.content_hash().to_string(),
},
responses: vec![
"HTTP/1.1 200 OK\r\nServer: nginx\r\nContent-Type: text/html\r\n\r\n".to_string(),
],
tape: CapTapeRecord::from(&CapTape::default()),
};
let findings = replay_run(&run).expect("the run replays against the shipped detection");
assert!(
findings
.iter()
.any(|f| f.detection().id() == "http-missing-security-headers"),
"the replay did not reproduce the finding"
);
}
}