use async_trait::async_trait;
use super::db::SignatureDb;
use super::model::{Evidence, SourceId, Tunnel};
use super::response::{Collected, ResponseSet};
use crate::config::ServiceDetection;
use crate::model::port::Protocol;
#[non_exhaustive]
#[derive(Debug, Clone)]
pub struct PortContext {
pub port: u16,
pub protocol: Protocol,
pub addr: Option<std::net::SocketAddr>,
pub tunnel: Option<Tunnel>,
pub speaks_http: bool,
pub detection: ServiceDetection,
pub host_name: Option<String>,
}
impl PortContext {
#[must_use]
pub fn new(port: u16, protocol: Protocol) -> Self {
Self {
port,
protocol,
addr: None,
tunnel: None,
speaks_http: false,
detection: ServiceDetection::default(),
host_name: None,
}
}
#[must_use]
pub fn with_addr(mut self, addr: Option<std::net::SocketAddr>) -> Self {
self.addr = addr;
self
}
#[must_use]
pub fn with_speaks_http(mut self, speaks_http: bool) -> Self {
self.speaks_http = speaks_http;
self
}
#[must_use]
pub fn with_tunnel(mut self, tunnel: Option<Tunnel>) -> Self {
self.tunnel = tunnel;
self
}
#[must_use]
pub fn with_detection(mut self, detection: ServiceDetection) -> Self {
self.detection = detection;
self
}
#[must_use]
pub fn with_host_name(mut self, host_name: Option<String>) -> Self {
self.host_name = host_name;
self
}
}
#[async_trait]
pub trait Analyzer: Send + Sync {
fn id(&self) -> SourceId;
fn interested(&self, ctx: &PortContext) -> bool;
async fn collect(&self, _ctx: &PortContext, _responses: &ResponseSet) -> Collected {
Collected::default()
}
fn analyze(
&self,
ctx: &PortContext,
responses: &ResponseSet,
collected: &Collected,
) -> Vec<Evidence>;
}
pub struct BannerRegexAnalyzer;
#[async_trait]
impl Analyzer for BannerRegexAnalyzer {
fn id(&self) -> SourceId {
SourceId::BannerRegex
}
fn interested(&self, _ctx: &PortContext) -> bool {
true
}
fn analyze(
&self,
ctx: &PortContext,
responses: &ResponseSet,
_collected: &Collected,
) -> Vec<Evidence> {
let db = SignatureDb::global();
responses
.banners
.iter()
.filter_map(|response| db.identify(ctx.port, ctx.protocol, response))
.map(|found| stamp(found, ctx))
.collect()
}
}
fn stamp(mut evidence: Evidence, ctx: &PortContext) -> Evidence {
evidence.tunnel = ctx.tunnel;
evidence
}
#[cfg(test)]
mod tests {
use super::*;
use crate::model::confidence::Confidence;
struct EchoAnalyzer;
#[async_trait]
impl Analyzer for EchoAnalyzer {
fn id(&self) -> SourceId {
SourceId::BannerRegex
}
fn interested(&self, _ctx: &PortContext) -> bool {
true
}
async fn collect(&self, ctx: &PortContext, _responses: &ResponseSet) -> Collected {
Collected {
frames: vec![vec![0xff, ctx.port as u8]],
}
}
fn analyze(
&self,
_ctx: &PortContext,
_responses: &ResponseSet,
collected: &Collected,
) -> Vec<Evidence> {
collected
.frames
.iter()
.map(|frame| {
Evidence::new(SourceId::BannerRegex, Confidence::Weak)
.with_product(format!("{frame:?}"))
})
.collect()
}
}
#[tokio::test]
async fn collect_output_reaches_analyze_as_raw_bytes() {
let ctx = PortContext {
port: 7,
protocol: crate::model::port::Protocol::Tcp,
addr: None,
tunnel: None,
speaks_http: false,
detection: crate::config::ServiceDetection::default(),
host_name: None,
};
let collected = EchoAnalyzer.collect(&ctx, &ResponseSet::default()).await;
assert_eq!(collected.frames, vec![vec![0xff, 7]]);
let evidence = EchoAnalyzer.analyze(&ctx, &ResponseSet::default(), &collected);
assert_eq!(evidence.len(), 1);
assert_eq!(evidence[0].product.as_deref(), Some("[255, 7]"));
}
#[tokio::test]
async fn default_collect_is_a_silent_no_op() {
let ctx = PortContext {
port: 80,
protocol: crate::model::port::Protocol::Tcp,
addr: None,
tunnel: None,
speaks_http: false,
detection: crate::config::ServiceDetection::default(),
host_name: None,
};
assert!(
BannerRegexAnalyzer
.collect(&ctx, &ResponseSet::default())
.await
.frames
.is_empty()
);
}
}