use std::net::IpAddr;
use super::capability::{CapError, Capabilities, ScanInstant};
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SpeakExchange {
pub sent: Vec<u8>,
pub reply: Result<Vec<u8>, CapError>,
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ResolveExchange {
pub name: String,
pub result: Result<Vec<IpAddr>, CapError>,
}
#[non_exhaustive]
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct CapTape {
pub speaks: Vec<SpeakExchange>,
pub resolves: Vec<ResolveExchange>,
pub nows: Vec<u64>,
}
pub struct RecordingCapabilities<C: Capabilities> {
inner: C,
tape: CapTape,
}
impl<C: Capabilities> RecordingCapabilities<C> {
pub fn new(inner: C) -> Self {
Self {
inner,
tape: CapTape::default(),
}
}
pub fn tape(&self) -> &CapTape {
&self.tape
}
pub fn into_tape(self) -> CapTape {
self.tape
}
}
impl<C: Capabilities> Capabilities for RecordingCapabilities<C> {
fn speak(&mut self, bytes: &[u8]) -> Result<Vec<u8>, CapError> {
let reply = self.inner.speak(bytes);
self.tape.speaks.push(SpeakExchange {
sent: bytes.to_vec(),
reply: reply.clone(),
});
reply
}
fn resolve(&mut self, name: &str) -> Result<Vec<IpAddr>, CapError> {
let result = self.inner.resolve(name);
self.tape.resolves.push(ResolveExchange {
name: name.to_string(),
result: result.clone(),
});
result
}
fn now(&mut self) -> ScanInstant {
let instant = self.inner.now();
self.tape.nows.push(instant.millis());
instant
}
}
pub struct RecordedCapabilities {
tape: CapTape,
speak_cursor: usize,
resolve_cursor: usize,
now_cursor: usize,
diverged: bool,
}
impl RecordedCapabilities {
pub fn from_tape(tape: CapTape) -> Self {
Self {
tape,
speak_cursor: 0,
resolve_cursor: 0,
now_cursor: 0,
diverged: false,
}
}
pub fn tape(&self) -> &CapTape {
&self.tape
}
pub fn diverged(&self) -> bool {
self.diverged
}
}
impl Capabilities for RecordedCapabilities {
fn speak(&mut self, _bytes: &[u8]) -> Result<Vec<u8>, CapError> {
let exchange = self.tape.speaks.get(self.speak_cursor);
self.speak_cursor += 1;
match exchange {
Some(exchange) => exchange.reply.clone(),
None => {
self.diverged = true;
Ok(Vec::new())
}
}
}
fn resolve(&mut self, _name: &str) -> Result<Vec<IpAddr>, CapError> {
let exchange = self.tape.resolves.get(self.resolve_cursor);
self.resolve_cursor += 1;
match exchange {
Some(exchange) => exchange.result.clone(),
None => {
self.diverged = true;
Ok(Vec::new())
}
}
}
fn now(&mut self) -> ScanInstant {
let millis = self.tape.nows.get(self.now_cursor).copied();
self.now_cursor += 1;
match millis {
Some(millis) => ScanInstant::from_millis(millis),
None => {
self.diverged = true;
ScanInstant::from_millis(0)
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::detect::compute::{
Budget, ComputeRuntime, Grant, LiveCapabilities, ModuleBody, RhaiRuntime,
};
use crate::fingerprint::PortContext;
use crate::model::finding::{DetectionClass, DetectionId, Severity, Version};
use crate::model::port::Protocol;
use std::io::{Read, Write};
use std::net::TcpListener;
use std::thread;
use std::time::Duration;
fn budget() -> Budget {
Budget {
fuel: 1_000_000,
deadline: Duration::from_secs(2),
max_memory: 65_536,
max_bytes: 8_192,
max_connections: 4,
}
}
fn grant() -> Grant {
Grant {
detection: DetectionId::new("replay-test", Version::new(1, 0, 0), "hash").unwrap(),
class: DetectionClass::ActiveBenign,
budget: budget(),
speak: true,
resolve: false,
}
}
fn ctx(port: u16) -> PortContext {
PortContext {
port,
protocol: Protocol::Tcp,
addr: None,
tunnel: None,
speaks_http: false,
detection: crate::config::ServiceDetection::default(),
}
}
#[test]
fn a_live_run_replays_byte_identically_from_its_tape() {
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
let addr = listener.local_addr().unwrap();
thread::spawn(move || {
if let Ok((mut sock, _)) = listener.accept() {
let mut probe = [0u8; 64];
let _ = sock.read(&mut probe);
let _ = sock.write_all(b"redis_version:7.2.4");
}
});
let source = r#"
fn analyze(ctx, responses) {
let reply = speak(blob(4, 0x41));
[ #{
severity: "medium",
summary: "answered " + text(reply) + " at " + now(),
} ]
}
"#;
let runtime = RhaiRuntime::new();
let module = runtime
.load(&ModuleBody::Rhai(source.to_string()))
.expect("the module compiles");
let mut recording =
RecordingCapabilities::new(LiveCapabilities::new(addr, Protocol::Tcp, None, &budget()));
let mut instance = runtime
.instantiate(&module, &grant())
.expect("instantiates");
let live = runtime
.run(&mut instance, &ctx(addr.port()), &[], &mut recording)
.expect("a clean live run");
let tape = recording.into_tape();
assert_eq!(tape.speaks.len(), 1, "the speak was not recorded");
assert!(!tape.nows.is_empty(), "the clock read was not recorded");
let mut replayed_caps = RecordedCapabilities::from_tape(tape);
let mut instance = runtime
.instantiate(&module, &grant())
.expect("instantiates");
let replayed = runtime
.run(&mut instance, &ctx(addr.port()), &[], &mut replayed_caps)
.expect("a clean replay");
assert_eq!(
live, replayed,
"the replay did not reproduce the live findings"
);
}
#[test]
fn a_recorded_now_tick_replays_from_the_tape() {
let tape = CapTape {
nows: vec![4242],
..CapTape::default()
};
let source = r#"
fn analyze(ctx, responses) {
[ #{ severity: "info", summary: "read at " + now() } ]
}
"#;
let runtime = RhaiRuntime::new();
let module = runtime
.load(&ModuleBody::Rhai(source.to_string()))
.expect("the module compiles");
let mut instance = runtime
.instantiate(&module, &grant())
.expect("instantiates");
let mut caps = RecordedCapabilities::from_tape(tape);
let findings = runtime
.run(&mut instance, &ctx(80), &[], &mut caps)
.expect("a clean run");
assert_eq!(findings.len(), 1);
assert!(
findings[0].title().contains("4242"),
"the clock tick was not served from the tape: {}",
findings[0].title()
);
}
#[test]
fn a_recorded_error_reply_replays_as_the_same_error() {
let tape = CapTape {
speaks: vec![SpeakExchange {
sent: b"x".to_vec(),
reply: Err(CapError::ConnectionRefused),
}],
..CapTape::default()
};
let source = r#"
fn analyze(ctx, responses) {
let severity = "low";
let summary = "the port answered";
try {
speak(blob(1, 0x78));
} catch (err) {
severity = "high";
summary = "the port refused the probe";
}
[ #{ severity: severity, summary: summary } ]
}
"#;
let runtime = RhaiRuntime::new();
let module = runtime
.load(&ModuleBody::Rhai(source.to_string()))
.expect("the module compiles");
let mut instance = runtime
.instantiate(&module, &grant())
.expect("instantiates");
let mut caps = RecordedCapabilities::from_tape(tape);
let findings = runtime
.run(&mut instance, &ctx(6379), &[], &mut caps)
.expect("a clean run");
assert_eq!(findings.len(), 1);
assert_eq!(
findings[0].severity(),
Severity::High,
"the module did not see the recorded error"
);
}
}