use crate::config::ServiceDetection;
use crate::error;
use crate::journal::settle::{Outcome, Settled};
use crate::model::host::{Host, HostStatus, NetworkRole, StatusProtocol, StatusReason};
use crate::model::ip::set::IpSet;
use crate::model::port::{Port, PortSet, PortState, Protocol};
use crate::model::target::PlannedTarget;
use crate::scanner::audit::ProbeAudit;
use crate::scanner::dispatcher::shuffled_addresses;
use crate::scanner::handle::ScanHandle;
use crate::scanner::pacing::limits::{CONNECT_PROBE_TIMEOUT, DISCOVERY_CONCURRENCY};
use crate::scanner::payload;
use crate::scanner::pool::ProbePool;
use crate::scanner::report::StopReason;
use crate::scanner::session::{ScanContext, ScannerKind};
use crate::scanner::strategy::{HostScanner, PortScanner, StrategyError};
use async_trait::async_trait;
use std::io::ErrorKind;
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
use std::sync::Arc;
use std::time::Instant;
use tokio::net::TcpStream;
use tokio::sync::mpsc;
use tokio::time::timeout;
pub struct ConnectScanner {
ips: IpSet,
ctx: ScanContext,
}
impl ConnectScanner {
pub fn new(ips: IpSet, ctx: ScanContext) -> Self {
Self { ips, ctx }
}
}
#[async_trait]
impl HostScanner for ConnectScanner {
fn kind(&self) -> ScannerKind {
ScannerKind::Connect
}
async fn discover_hosts(&mut self) -> Result<(), StrategyError> {
discover(std::mem::take(&mut self.ips), self.ctx.clone()).await
}
}
struct Probed {
ip: IpAddr,
port: Option<Port>,
answered: bool,
outcome: Outcome,
role: Option<NetworkRole>,
}
type ProbedPort = Option<Probed>;
pub struct ConnectPortScanner {
ctx: ScanContext,
concurrency: usize,
detection: ServiceDetection,
}
impl ConnectPortScanner {
pub fn new(ctx: ScanContext, concurrency: usize, detection: ServiceDetection) -> Self {
Self {
ctx,
concurrency,
detection,
}
}
}
#[async_trait]
impl PortScanner for ConnectPortScanner {
fn kind(&self) -> ScannerKind {
ScannerKind::Connect
}
fn supported_protocols(&self) -> Vec<Protocol> {
vec![Protocol::Tcp]
}
async fn scan(&mut self, rx: mpsc::Receiver<PlannedTarget>) -> Result<(), StrategyError> {
scan(rx, self.concurrency, self.ctx.clone(), self.detection).await
}
}
pub struct ConnectUdpPortScanner {
ctx: ScanContext,
concurrency: usize,
}
impl ConnectUdpPortScanner {
pub fn new(ctx: ScanContext, concurrency: usize) -> Self {
Self { ctx, concurrency }
}
}
#[async_trait]
impl PortScanner for ConnectUdpPortScanner {
fn kind(&self) -> ScannerKind {
ScannerKind::ConnectUdp
}
fn supported_protocols(&self) -> Vec<Protocol> {
vec![Protocol::Udp]
}
async fn scan(&mut self, mut rx: mpsc::Receiver<PlannedTarget>) -> Result<(), StrategyError> {
let ctx = self.ctx.clone();
let mut pool = ProbePool::new(
self.concurrency,
self.ctx.clone(),
self.kind(),
|probed, audit: &mut ProbeAudit| absorb_probe(&ctx, probed, audit),
);
let mut probes = 0u128;
let mut reason = StopReason::AttemptsSpent;
while let Some(target) = rx.recv().await {
if self.ctx.handle.should_stop() {
reason = StopReason::Aborted;
break;
}
probes += 1;
pool.audit().record_send(true);
pool.admit(udp_port_prober(target)).await;
}
pool.drain().await;
finish(&self.ctx, pool.into_audit(), self.kind(), probes, reason);
Ok(())
}
}
pub async fn scan(
mut rx: mpsc::Receiver<PlannedTarget>,
concurrency_limit: usize,
ctx: ScanContext,
detection: ServiceDetection,
) -> Result<(), StrategyError> {
let folder = ctx.clone();
let mut pool = ProbePool::new(
concurrency_limit,
ctx.clone(),
ScannerKind::Connect,
|probed, audit: &mut ProbeAudit| absorb_probe(&folder, probed, audit),
);
let mut probes = 0u128;
let mut reason = StopReason::AttemptsSpent;
while let Some(target) = rx.recv().await {
if ctx.handle.should_stop() {
reason = StopReason::Aborted;
ctx.record_outcome(Outcome::Unasked);
break;
}
probes += 1;
pool.audit().record_send(true);
pool.admit(port_prober(target, detection)).await;
}
while rx.try_recv().is_ok() {
ctx.record_outcome(Outcome::Unasked);
}
pool.drain().await;
finish(
&ctx,
pool.into_audit(),
ScannerKind::Connect,
probes,
reason,
);
Ok(())
}
fn absorb_probe(ctx: &ScanContext, probed: ProbedPort, audit: &mut ProbeAudit) {
let Some(probed) = probed else {
return;
};
ctx.record_outcome(probed.outcome);
if probed.answered {
audit.record_host_found(None);
}
if probed.port.is_none() && !probed.answered && probed.role.is_none() {
return;
}
ctx.update_host(probed.ip, |host| {
if let Some(port) = probed.port.clone() {
host.add_port(port);
}
if let Some(role) = probed.role {
host.add_network_role(role);
}
if probed.answered {
host.record_evidence(
HostStatus::Up,
StatusReason::new(StatusProtocol::TcpSyn, "tcp connect answered by the host"),
);
}
});
}
async fn port_prober(planned: PlannedTarget, detection: ServiceDetection) -> ProbedPort {
let target = planned.target;
if target.protocol == Protocol::Udp {
return None;
}
let position = planned.position;
let socket_addr = SocketAddr::new(target.ip, target.port);
match timeout(CONNECT_PROBE_TIMEOUT, TcpStream::connect(socket_addr)).await {
Ok(Ok(stream)) => {
let port =
crate::fingerprint::baseline_port(target.port, Protocol::Tcp, PortState::Open);
let port = crate::fingerprint::fingerprint_tcp(stream, port, detection).await;
Some(Probed {
ip: target.ip,
port: Some(port),
answered: true,
outcome: Outcome::Answered { position },
role: None,
})
}
Ok(Err(e)) => {
match e.kind() {
ErrorKind::ConnectionRefused => Some(Probed {
ip: target.ip,
port: Some(crate::fingerprint::baseline_port(
target.port,
Protocol::Tcp,
PortState::Closed,
)),
answered: true,
outcome: Outcome::Answered { position },
role: None,
}),
_ => Some(Probed {
ip: target.ip,
port: Some(crate::fingerprint::baseline_port(
target.port,
Protocol::Tcp,
PortState::Filtered,
)),
answered: false,
outcome: Outcome::Unroutable,
role: None,
}),
}
}
Err(_) => Some(Probed {
ip: target.ip,
port: Some(crate::fingerprint::baseline_port(
target.port,
Protocol::Tcp,
PortState::Filtered,
)),
answered: false,
outcome: Outcome::Exhausted { position },
role: None,
}),
}
}
fn wildcard_for(target: IpAddr) -> SocketAddr {
match target {
IpAddr::V4(_) => SocketAddr::new(IpAddr::V4(Ipv4Addr::UNSPECIFIED), 0),
IpAddr::V6(_) => SocketAddr::new(IpAddr::V6(Ipv6Addr::UNSPECIFIED), 0),
}
}
async fn udp_port_prober(planned: PlannedTarget) -> ProbedPort {
let target = planned.target;
if target.protocol != Protocol::Udp {
return None;
}
let position = planned.position;
let socket_addr = SocketAddr::new(target.ip, target.port);
let record = |state, answered, outcome| {
Some(Probed {
ip: target.ip,
port: Some(crate::fingerprint::baseline_port(
target.port,
Protocol::Udp,
state,
)),
answered,
outcome,
role: None,
})
};
let socket = match tokio::net::UdpSocket::bind(wildcard_for(target.ip)).await {
Ok(socket) => socket,
Err(e) => {
error!(
verbosity = 2,
"no UDP socket for probing {socket_addr}: {e}"
);
return None;
}
};
if let Err(e) = socket.connect(socket_addr).await {
error!(
verbosity = 2,
"cannot address UDP probe to {socket_addr}: {e}"
);
return None;
}
if let Err(e) = socket.send(payload::for_port(target.port)).await {
return match e.kind() {
ErrorKind::ConnectionRefused => {
record(PortState::Closed, false, Outcome::Answered { position })
}
_ => {
error!(
verbosity = 2,
"failed to send UDP probe to {socket_addr}: {e}"
);
None
}
};
}
let mut buf = [0u8; 1024];
match timeout(CONNECT_PROBE_TIMEOUT, socket.recv(&mut buf)).await {
Ok(Ok(read)) => {
record(PortState::Open, true, Outcome::Answered { position }).map(|probed| Probed {
role: payload::declared_role(target.port, &buf[..read]),
..probed
})
}
Ok(Err(e)) if e.kind() == ErrorKind::ConnectionRefused => {
record(PortState::Closed, false, Outcome::Answered { position })
}
Ok(Err(e)) => {
error!(
verbosity = 2,
"UDP probe to {socket_addr} failed after sending: {e}"
);
record(PortState::OpenFiltered, false, Outcome::Unroutable)
}
Err(_) => record(
PortState::OpenFiltered,
false,
Outcome::Exhausted { position },
),
}
}
fn finish(
ctx: &ScanContext,
audit: ProbeAudit,
scanner: ScannerKind,
probes: u128,
reason: StopReason,
) {
audit.report("connect", probes, reason, None, None);
ctx.record_probe_stats(audit.stats(scanner, probes, reason, None, None));
}
pub async fn discover(ips: IpSet, ctx: ScanContext) -> Result<(), StrategyError> {
let ports: Arc<[u16]> = PortSet::common_discovery()
.iter()
.map(|(port, _)| port)
.collect::<Vec<_>>()
.into();
let mut rx = shuffled_addresses(ips, 1024, &ctx.handle);
let folder = ctx.clone();
let mut pool = ProbePool::new(
DISCOVERY_CONCURRENCY,
ctx.clone(),
ScannerKind::Connect,
|probed, audit: &mut ProbeAudit| absorb_host(&folder, probed, audit),
);
let mut probes = 0u128;
let mut reason = StopReason::AttemptsSpent;
while let Some(ip) = rx.recv().await {
if ctx.handle.should_stop() {
reason = StopReason::Aborted;
ctx.record_outcome(Outcome::Unasked);
break;
}
probes += 1;
pool.audit().record_send(true);
pool.admit(prober(ip, Arc::clone(&ports), ctx.handle.clone()))
.await;
}
while rx.try_recv().is_ok() {
ctx.record_outcome(Outcome::Unasked);
}
pool.drain().await;
finish(
&ctx,
pool.into_audit(),
ScannerKind::Connect,
probes,
reason,
);
Ok(())
}
struct ProbedHost {
ip: IpAddr,
fate: Fate,
}
enum Fate {
Answered(Box<Host>),
Exhausted,
Unroutable,
Interrupted,
Unasked,
}
fn absorb_host(ctx: &ScanContext, probed: ProbedHost, audit: &mut ProbeAudit) {
match probed.fate {
Fate::Answered(host) => {
let ip = host.primary_ip();
audit.record_host_found(None);
ctx.settle_address(probed.ip, Settled::Answered);
ctx.update_host(ip, |existing| existing.merge(*host));
}
Fate::Exhausted => ctx.settle_address(probed.ip, Settled::Exhausted),
Fate::Unroutable => ctx.record_outcome(Outcome::Unroutable),
Fate::Interrupted => ctx.record_outcome(Outcome::Interrupted),
Fate::Unasked => ctx.record_outcome(Outcome::Unasked),
}
}
async fn prober(ip: IpAddr, ports: Arc<[u16]>, handle: ScanHandle) -> ProbedHost {
let start = Instant::now();
let mut asked = false;
for &port in ports.iter() {
if handle.should_stop() {
return ProbedHost {
ip,
fate: if asked {
Fate::Interrupted
} else {
Fate::Unasked
},
};
}
let attempt = timeout(
CONNECT_PROBE_TIMEOUT,
TcpStream::connect(SocketAddr::new(ip, port)),
)
.await;
match attempt {
Ok(Ok(_)) => return answered(ip, start),
Ok(Err(e))
if matches!(
e.kind(),
ErrorKind::ConnectionRefused
| ErrorKind::ConnectionReset
| ErrorKind::ConnectionAborted
) =>
{
return answered(ip, start);
}
Ok(Err(_)) => {}
Err(_elapsed) => asked = true,
}
}
ProbedHost {
ip,
fate: if asked {
Fate::Exhausted
} else {
Fate::Unroutable
},
}
}
fn answered(ip: IpAddr, start: Instant) -> ProbedHost {
let mut host = Host::new(ip).with_rtt(start.elapsed());
host.record_evidence(
HostStatus::Up,
StatusReason::new(StatusProtocol::TcpSyn, "tcp connect answered by the host"),
);
ProbedHost {
ip,
fate: Fate::Answered(Box::new(host)),
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::model::target::Target;
use tokio::net::UdpSocket;
fn udp_target(ip: IpAddr, port: u16) -> PlannedTarget {
PlannedTarget::new(
u64::from(port),
Target {
ip,
port,
protocol: Protocol::Udp,
},
)
}
async fn closed_loopback_udp_port(ip: IpAddr) -> u16 {
let socket = UdpSocket::bind((ip, 0)).await.expect("bind to reserve");
let port = socket.local_addr().expect("reserved addr").port();
drop(socket);
port
}
#[test]
fn probe_socket_binds_the_target_family() {
assert!(wildcard_for(IpAddr::V4(Ipv4Addr::LOCALHOST)).is_ipv4());
assert!(wildcard_for(IpAddr::V6(Ipv6Addr::LOCALHOST)).is_ipv6());
}
#[tokio::test]
async fn closed_ipv6_port_is_classified_not_dropped() {
let ip = IpAddr::V6(Ipv6Addr::LOCALHOST);
let port = closed_loopback_udp_port(ip).await;
let probed = udp_port_prober(udp_target(ip, port)).await;
let probed = probed.expect("an IPv6 target must produce a verdict");
let probed_port = probed.port.expect("a closed port is still a verdict");
assert_eq!(probed.ip, ip);
assert_eq!(probed_port.number(), port);
assert_eq!(probed_port.state(), PortState::Closed);
}
#[tokio::test]
async fn closed_ipv4_port_is_closed() {
let ip = IpAddr::V4(Ipv4Addr::LOCALHOST);
let port = closed_loopback_udp_port(ip).await;
let probed = udp_port_prober(udp_target(ip, port)).await;
assert_eq!(
probed
.and_then(|probed| probed.port)
.expect("a verdict")
.state(),
PortState::Closed
);
}
#[tokio::test]
async fn a_listener_that_answers_is_open() {
for ip in [
IpAddr::V4(Ipv4Addr::LOCALHOST),
IpAddr::V6(Ipv6Addr::LOCALHOST),
] {
let service = UdpSocket::bind((ip, 0)).await.expect("bind service");
let port = service.local_addr().unwrap().port();
tokio::spawn(async move {
let mut buf = [0u8; 64];
if let Ok((_, from)) = service.recv_from(&mut buf).await {
let _ = service.send_to(b"pong", from).await;
}
});
let probed = udp_port_prober(udp_target(ip, port)).await;
assert_eq!(
probed
.and_then(|probed| probed.port)
.expect("a verdict")
.state(),
PortState::Open,
"a live {ip} listener must read as open"
);
}
}
#[tokio::test]
async fn tcp_targets_are_skipped() {
let target = PlannedTarget::new(
0,
Target {
ip: IpAddr::V4(Ipv4Addr::LOCALHOST),
port: 80,
protocol: Protocol::Tcp,
},
);
assert!(udp_port_prober(target).await.is_none());
}
}