use crate::model::ip::scoped::ScopedIp;
use crate::warn;
use crate::config::ServiceDetection;
use crate::config::limits::{CONNECT_CONCURRENCY, CONNECT_PROBE_TIMEOUT};
use crate::model::port::{Port, PortState, Protocol};
use crate::report::ScannerKind;
use crate::scanner::pool::ProbePool;
use crate::scanner::session::{ScanContext, Stage};
use crate::system::descriptors;
use crate::transport::dial::Egress;
pub async fn detect(ctx: &ScanContext, detection: ServiceDetection, over: Protocol) {
if !detection.connects() {
return;
}
let targets = fingerprintable_ports(ctx, over);
if targets.is_empty() {
return;
}
ctx.enter_stage(Stage::Services, Some(targets.len() as u64));
let asked = targets.len();
let mut quiet = QuietPorts::default();
let mut pool = ProbePool::new(
CONNECT_CONCURRENCY,
ctx.clone(),
ScannerKind::Service,
|attempt: Attempt, _audit| {
ctx.stage_advanced();
match attempt {
Attempt::Identified(found) => {
let Identified {
ip,
port,
about_the_host,
banners,
} = *found;
ctx.record_responses(ip.clone(), port.number(), port.protocol(), banners);
write_back(ctx, ip, port, about_the_host);
}
Attempt::Unreachable { ip, number, reason } => {
quiet.record(&ip, number, reason);
}
Attempt::Quiet => {}
}
},
);
for (target, port, protocol) in targets {
if ctx.handle.should_stop() {
break;
}
if ctx.host_expired(target.addr()) {
continue;
}
let egress = ctx.egress_toward(target.addr());
let detection = ctx.service_detection_on(detection, port, protocol);
pool.admit(fingerprint_one(target, port, protocol, detection, egress))
.await;
}
pool.drain().await;
drop(pool);
quiet.report(ctx, asked);
}
const QUIET_PORTS_WORTH_A_WORD: usize = 10;
#[derive(Debug, Default)]
struct QuietPorts {
count: usize,
first: Option<String>,
reason: Option<String>,
}
impl QuietPorts {
fn record(&mut self, ip: &ScopedIp, number: u16, reason: String) {
self.count += 1;
if self.first.is_none() {
self.first = Some(ip.endpoint(number));
self.reason = Some(reason);
}
}
fn summary(first: &str, reason: &str, count: usize) -> String {
match count - 1 {
0 => format!("{first} could not be fingerprinted: {reason}"),
1 => format!("{first} and 1 other port could not be fingerprinted: {reason}"),
rest => format!("{first} and {rest} other ports could not be fingerprinted: {reason}"),
}
}
fn report(&self, ctx: &ScanContext, asked: usize) {
let (Some(first), Some(reason)) = (&self.first, &self.reason) else {
return;
};
ctx.record_failure(
ScannerKind::Service,
Self::summary(first, reason, self.count),
);
if self.count == asked && asked >= QUIET_PORTS_WORTH_A_WORD {
warn!(
"all {asked} open ports went silent on connect; likely a middlebox, not the host"
);
}
}
}
fn fingerprintable_ports(ctx: &ScanContext, over: Protocol) -> Vec<(ScopedIp, u16, Protocol)> {
let mut targets = Vec::new();
for host in ctx.store.iter() {
let address = host.value().scoped_ip();
for port in host.value().ports() {
if port.protocol() == over
&& port.state() == PortState::Open
&& crate::fingerprint::reads_replies(port.number(), port.protocol())
{
targets.push((address.clone(), port.number(), port.protocol()));
}
}
}
targets
}
enum Attempt {
Identified(Box<Identified>),
Unreachable {
ip: ScopedIp,
number: u16,
reason: String,
},
Quiet,
}
struct Identified {
ip: ScopedIp,
port: Port,
about_the_host: crate::fingerprint::AboutTheHost,
banners: Vec<String>,
}
async fn fingerprint_one(
target: ScopedIp,
port_number: u16,
protocol: Protocol,
detection: ServiceDetection,
egress: Egress,
) -> Attempt {
let Some(addr) = target.to_socket_addr(port_number) else {
warn!(
verbosity = 2,
"cannot fingerprint {}: no interface recorded for a link-local address",
target.endpoint(port_number)
);
return Attempt::Quiet;
};
let port = crate::fingerprint::baseline_port(port_number, protocol, PortState::Open);
let _descriptor = descriptors::gate()
.acquire()
.await
.expect("the descriptor gate is never closed");
let (port, about_the_host, banners) = match protocol {
Protocol::Tcp => {
let stream = match egress.connect_timed(addr, CONNECT_PROBE_TIMEOUT).await {
Ok(stream) => stream,
Err(e) => {
let reason = if descriptors::exhausted(&e) {
descriptors::starved(descriptors::PATIENCE)
} else if e.kind() == std::io::ErrorKind::TimedOut {
format!("no answer within {CONNECT_PROBE_TIMEOUT:?}")
} else {
e.to_string()
};
return Attempt::Unreachable {
ip: target,
number: port_number,
reason,
};
}
};
crate::fingerprint::fingerprint_tcp_via(stream, port, detection, egress).await
}
Protocol::Udp => match crate::fingerprint::fingerprint_udp_via(addr, port, egress).await {
Some(fingerprinted) => fingerprinted,
None => return Attempt::Quiet,
},
Protocol::Sctp => return Attempt::Quiet,
};
Attempt::Identified(Box::new(Identified {
ip: target,
port,
about_the_host,
banners,
}))
}
fn write_back(
ctx: &ScanContext,
key: ScopedIp,
port: Port,
about_the_host: crate::fingerprint::AboutTheHost,
) {
ctx.update_host(key, |host| {
host.add_port(port);
if about_the_host.is_empty() {
return;
}
about_the_host.apply(host);
});
}
#[cfg(test)]
mod tests {
use super::*;
use crate::model::host::Host;
#[test]
fn many_quiet_ports_collapse_into_one_line() {
let mut quiet = QuietPorts::default();
let ip: ScopedIp = "192.0.2.1".parse::<IpAddr>().expect("an address").into();
for port in 0..83u16 {
quiet.record(&ip, 1000 + port, "no answer within 1.5s".to_string());
}
let summary = QuietPorts::summary(
quiet.first.as_deref().expect("a first port"),
quiet.reason.as_deref().expect("a reason"),
quiet.count,
);
assert_eq!(
summary,
"192.0.2.1:1000 and 82 other ports could not be fingerprinted: no answer within 1.5s"
);
}
#[test]
fn one_quiet_port_is_named_alone() {
assert_eq!(
QuietPorts::summary("192.0.2.1:22", "connection refused", 1),
"192.0.2.1:22 could not be fingerprinted: connection refused"
);
assert_eq!(
QuietPorts::summary("192.0.2.1:22", "connection refused", 2),
"192.0.2.1:22 and 1 other port could not be fingerprinted: connection refused"
);
}
#[test]
fn an_ipv6_endpoint_stays_bracketed() {
let mut quiet = QuietPorts::default();
let ip: ScopedIp = "2001:db8::1".parse::<IpAddr>().expect("an address").into();
quiet.record(&ip, 443, "no answer within 1.5s".to_string());
assert_eq!(quiet.first.as_deref(), Some("[2001:db8::1]:443"));
}
use crate::scanner::session::ScanSession;
use std::collections::BTreeSet;
use std::net::IpAddr;
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::TcpListener;
#[tokio::test]
async fn detect_fingerprints_an_open_tcp_port_end_to_end() {
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = listener.local_addr().unwrap();
tokio::spawn(async move {
if let Ok((mut sock, _)) = listener.accept().await {
let _ = sock.write_all(b"SSH-2.0-OpenSSH_9.6p1 Debian-3\r\n").await;
}
});
let (session, ctx) = ScanSession::new();
let ip = addr.ip();
let mut host = Host::new(ip);
host.add_port(Port::new(addr.port(), Protocol::Tcp, PortState::Open));
session.hosts().insert(ip, host);
detect(&ctx, ServiceDetection::default(), Protocol::Tcp).await;
let host = session.hosts().get(ip).unwrap();
let port = host
.ports()
.find(|p| p.number() == addr.port())
.expect("port present");
let service = port.service().expect("service identified");
assert_eq!(service.name(), "ssh");
assert_eq!(service.product(), Some("OpenSSH"));
assert_eq!(service.version(), Some("9.6p1"));
}
#[tokio::test]
async fn detection_turned_off_connects_to_nothing() {
let (session, ctx) = ScanSession::new();
let unreachable: IpAddr = "192.0.2.1".parse().expect("a documentation address");
ctx.update_host(unreachable, |host| {
host.add_port(Port::new(80, Protocol::Tcp, PortState::Open));
});
let started = std::time::Instant::now();
detect(&ctx, ServiceDetection::Off, Protocol::Tcp).await;
assert!(
started.elapsed() < CONNECT_PROBE_TIMEOUT,
"a level that connects to nothing cannot have waited on a connection"
);
drop(session);
}
#[tokio::test]
async fn detect_is_a_no_op_with_no_open_ports() {
let (session, ctx) = ScanSession::new();
let ip: IpAddr = "127.0.0.1".parse().unwrap();
let mut host = Host::new(ip);
host.add_port(Port::new(9, Protocol::Tcp, PortState::Closed));
session.hosts().insert(ip, host);
detect(&ctx, ServiceDetection::default(), Protocol::Tcp).await;
let host = session.hosts().get(ip).unwrap();
let port = host.ports().find(|p| p.number() == 9).unwrap();
assert!(port.service().is_none());
}
async fn counting_listener() -> (std::net::SocketAddr, Arc<AtomicUsize>) {
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = listener.local_addr().unwrap();
let received = Arc::new(AtomicUsize::new(0));
let count = Arc::clone(&received);
tokio::spawn(async move {
while let Ok((mut sock, _)) = listener.accept().await {
let count = Arc::clone(&count);
tokio::spawn(async move {
let mut buffer = [0u8; 1024];
while let Ok(n) = sock.read(&mut buffer).await {
if n == 0 {
break;
}
count.fetch_add(n, Ordering::SeqCst);
}
});
}
});
(addr, received)
}
#[tokio::test]
async fn a_listen_only_port_is_sent_nothing_where_any_other_is_asked() {
let mut received = Vec::new();
for listen_only in [true, false] {
let (addr, count) = counting_listener().await;
let ports = match listen_only {
true => BTreeSet::from([addr.port()]),
false => BTreeSet::new(),
};
let (session, ctx) = ScanSession::builder().listening_only_to(ports).build();
let ip = addr.ip();
let mut host = Host::new(ip);
host.add_port(Port::new(addr.port(), Protocol::Tcp, PortState::Open));
session.hosts().insert(ip, host);
detect(&ctx, ServiceDetection::Thorough, Protocol::Tcp).await;
received.push(count.load(Ordering::SeqCst));
}
assert_eq!(received[0], 0, "a listen-only port was sent a payload");
assert!(
received[1] > 0,
"the same port off the list was asked nothing, so the first half \
proves nothing"
);
}
#[tokio::test]
async fn a_port_that_refuses_a_connection_is_written_into_the_report() {
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = listener.local_addr().unwrap();
drop(listener);
let (session, ctx) = ScanSession::new();
let ip = addr.ip();
let mut host = Host::new(ip);
host.add_port(Port::new(addr.port(), Protocol::Tcp, PortState::Open));
session.hosts().insert(ip, host);
detect(&ctx, ServiceDetection::default(), Protocol::Tcp).await;
let failures = ctx.take_failures();
assert_eq!(failures.len(), 1, "one unreachable port, one line about it");
assert!(
failures[0].reason().contains(&addr.port().to_string()),
"the failure names the port it is about: {}",
failures[0].reason()
);
}
}