use crate::error::{IronError, Result};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::net::{IpAddr, SocketAddr, TcpStream, ToSocketAddrs};
use std::process::Command;
use std::sync::{Arc, Mutex};
use std::thread;
use std::time::{Duration, Instant};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PingResult {
pub host: String,
pub ip_address: Option<String>,
pub packets_sent: u32,
pub packets_received: u32,
pub packets_lost: u32,
pub packet_loss_percent: f64,
pub rtt_min_ms: Option<f64>,
pub rtt_max_ms: Option<f64>,
pub rtt_avg_ms: Option<f64>,
pub rtt_stddev_ms: Option<f64>,
pub ping_times: Vec<Option<f64>>,
pub is_reachable: bool,
}
impl PingResult {
pub fn new(host: &str) -> Self {
Self {
host: host.to_string(),
ip_address: None,
packets_sent: 0,
packets_received: 0,
packets_lost: 0,
packet_loss_percent: 100.0,
rtt_min_ms: None,
rtt_max_ms: None,
rtt_avg_ms: None,
rtt_stddev_ms: None,
ping_times: Vec::new(),
is_reachable: false,
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TracerouteHop {
pub ttl: u8,
pub address: Option<String>,
pub hostname: Option<String>,
pub rtt_ms: Option<f64>,
pub rtt_probes: Vec<Option<f64>>,
#[serde(default)]
pub rtt_below_1ms: bool,
pub responded: bool,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TracerouteResult {
pub target: String,
pub target_ip: Option<String>,
pub hops: Vec<TracerouteHop>,
pub destination_reached: Option<bool>,
pub total_hops: usize,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum PortStatus {
Open,
Closed,
Filtered,
Error,
}
impl std::fmt::Display for PortStatus {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
PortStatus::Open => write!(f, "open"),
PortStatus::Closed => write!(f, "closed"),
PortStatus::Filtered => write!(f, "filtered"),
PortStatus::Error => write!(f, "error"),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PortScanResult {
pub port: u16,
pub status: PortStatus,
pub service: Option<String>,
pub connect_time_ms: Option<f64>,
pub banner: Option<String>,
}
pub struct NetworkTools {
pub timeout: Duration,
}
impl Default for NetworkTools {
fn default() -> Self {
Self::new()
}
}
impl NetworkTools {
pub fn new() -> Self {
Self {
timeout: Duration::from_secs(3),
}
}
pub fn with_timeout(mut self, timeout: Duration) -> Self {
self.timeout = timeout;
self
}
}
pub fn ping(host: &str, count: u32) -> Result<PingResult> {
let mut result = PingResult::new(host);
result.packets_sent = count;
#[cfg(target_os = "windows")]
let output = Command::new("ping")
.args(["-n", &count.to_string(), host])
.output()
.map_err(|e| IronError::Other(format!("Failed to execute ping: {}", e)))?;
#[cfg(not(target_os = "windows"))]
let output = Command::new("ping")
.args(["-c", &count.to_string(), host])
.output()
.map_err(|e| IronError::Other(format!("Failed to execute ping: {}", e)))?;
let stdout = String::from_utf8_lossy(&output.stdout);
if !parse_ping_output(&stdout, &mut result) {
return Err(IronError::Parse(format!(
concat!(
"ping ran ({}) but its output had no statistics line this parser ",
"recognises. Only English output is parsed, so a reachability verdict ",
"here would be a guess: {}"
),
output.status,
stdout
.lines()
.find(|l| !l.trim().is_empty())
.unwrap_or("(no output)")
)));
}
Ok(result)
}
fn loss_percent(sent: u32, received: u32) -> f64 {
if sent == 0 {
0.0
} else {
f64::from(sent.saturating_sub(received)) / f64::from(sent) * 100.0
}
}
fn parse_ping_output(output: &str, result: &mut PingResult) -> bool {
let lines: Vec<&str> = output.lines().collect();
let mut recognised = false;
for line in &lines {
let line_lower = line.to_lowercase();
if line_lower.contains("time=") || line_lower.contains("time<") {
if let Some(time_start) = line.find("time=").or_else(|| line.find("time<")) {
if line[time_start..].starts_with("time<") {
result.ping_times.push(None);
} else {
let time_str = &line[time_start + 5..];
if let Some(end) = time_str.find(|c: char| !c.is_ascii_digit() && c != '.') {
if let Ok(time) = time_str[..end].parse::<f64>() {
result.ping_times.push(Some(time));
}
}
}
}
}
if line_lower.contains("packets: sent") {
let count = |label: &str| {
let rest = &line[line.find(label)? + label.len()..];
rest[..rest.find(',')?].trim().parse::<u32>().ok()
};
if let (Some(sent), Some(received)) = (count("Sent = "), count("Received = ")) {
recognised = true;
result.packets_sent = sent;
result.packets_received = received;
result.packet_loss_percent = loss_percent(sent, received);
}
}
if line_lower.contains("packets transmitted") {
let mut parts = line.split(',');
let mut leading = || parts.next()?.split_whitespace().next()?.parse::<u32>().ok();
if let (Some(sent), Some(received)) = (leading(), leading()) {
recognised = true;
result.packets_sent = sent;
result.packets_received = received;
result.packet_loss_percent = loss_percent(sent, received);
}
}
if line_lower.contains("minimum =") {
let millis = |label: &str| {
let rest = &line[line.find(label)? + label.len()..];
rest[..rest.find("ms")?].trim().parse::<f64>().ok()
};
result.rtt_min_ms = millis("Minimum = ");
result.rtt_max_ms = millis("Maximum = ");
result.rtt_avg_ms = millis("Average = ");
}
if line_lower.contains("rtt min/avg/max") || line_lower.contains("round-trip min/avg/max") {
if let Some(eq_pos) = line.find('=') {
let stats_str = line[eq_pos + 1..].trim();
let parts: Vec<&str> = stats_str.split('/').collect();
if parts.len() >= 3 {
result.rtt_min_ms = parts[0].trim().parse().ok();
result.rtt_avg_ms = parts[1].trim().parse().ok();
result.rtt_max_ms = parts[2]
.split_whitespace()
.next()
.and_then(|s| s.parse().ok());
if parts.len() >= 4 {
result.rtt_stddev_ms = parts[3]
.split_whitespace()
.next()
.and_then(|s| s.parse().ok());
}
}
}
}
}
result.packets_lost = result.packets_sent.saturating_sub(result.packets_received);
result.is_reachable = result.packets_received > 0;
if result.rtt_avg_ms.is_none() {
let valid_times: Vec<f64> = result.ping_times.iter().filter_map(|&t| t).collect();
if !valid_times.is_empty() {
result.rtt_min_ms = valid_times.iter().copied().reduce(f64::min);
result.rtt_max_ms = valid_times.iter().copied().reduce(f64::max);
result.rtt_avg_ms = Some(valid_times.iter().sum::<f64>() / valid_times.len() as f64);
}
}
recognised
}
pub fn traceroute(host: &str, max_hops: u8) -> Result<TracerouteResult> {
let mut result = TracerouteResult {
target: host.to_string(),
target_ip: None,
hops: Vec::new(),
destination_reached: None,
total_hops: 0,
};
#[cfg(target_os = "windows")]
let output = Command::new("tracert")
.args(["-h", &max_hops.to_string(), "-d", host])
.output()
.map_err(|e| IronError::Other(format!("Failed to execute tracert: {}", e)))?;
#[cfg(not(target_os = "windows"))]
let output = Command::new("traceroute")
.args(["-m", &max_hops.to_string(), "-n", host])
.output()
.map_err(|e| IronError::Other(format!("Failed to execute traceroute: {}", e)))?;
let stdout = String::from_utf8_lossy(&output.stdout);
parse_traceroute_output(&stdout, &mut result);
Ok(result)
}
fn traceroute_target_ip(line: &str) -> Option<String> {
let candidate = match line.strip_prefix("traceroute to ") {
Some(rest) => {
let start = rest.find('(')?;
let end = rest.find(')')?;
rest.get(start + 1..end)?
}
None => {
let rest = line.strip_prefix("Tracing route to ")?;
if let (Some(start), Some(end)) = (rest.find('['), rest.find(']')) {
rest.get(start + 1..end)?
} else {
rest.split_whitespace().next()?
}
}
};
candidate
.trim()
.parse::<IpAddr>()
.ok()
.map(|ip| ip.to_string())
}
fn parse_traceroute_output(output: &str, result: &mut TracerouteResult) {
let lines: Vec<&str> = output.lines().collect();
for line in lines {
let line = line.trim();
if line.is_empty() {
continue;
}
if line.starts_with("traceroute to")
|| line.starts_with("Tracing route")
|| line.contains("maximum")
|| line.contains("over a maximum")
{
if let Some(ip) = traceroute_target_ip(line) {
result.target_ip = Some(ip);
}
continue;
}
if line.contains("Trace complete") {
continue;
}
let parts: Vec<&str> = line.split_whitespace().collect();
if parts.is_empty() {
continue;
}
if let Ok(ttl) = parts[0].parse::<u8>() {
let mut hop = TracerouteHop {
ttl,
address: None,
hostname: None,
rtt_ms: None,
rtt_probes: Vec::new(),
rtt_below_1ms: false,
responded: false,
};
hop.address = parts[1..]
.iter()
.find(|p| p.parse::<IpAddr>().is_ok())
.map(|p| p.to_string());
let mut i = 1;
while i < parts.len() {
let token = parts[i];
let attached = token.ends_with("ms");
let spaced = !attached && parts.get(i + 1) == Some(&"ms");
if !attached && !spaced {
i += 1;
continue;
}
let value = token.trim_start_matches('<').trim_end_matches("ms");
if token.starts_with('<') {
if value.parse::<f64>().is_ok() {
hop.rtt_probes.push(None);
hop.rtt_below_1ms = true;
}
} else if let Ok(rtt) = value.parse::<f64>() {
hop.rtt_probes.push(Some(rtt));
}
i += if attached { 1 } else { 2 };
}
hop.rtt_ms = hop.rtt_probes.iter().copied().flatten().next();
hop.responded = hop.address.is_some() || !hop.rtt_probes.is_empty();
result.hops.push(hop);
}
}
result.destination_reached = result.target_ip.as_ref().map(|target| {
result
.hops
.iter()
.any(|hop| hop.address.as_deref() == Some(target.as_str()))
});
result.total_hops = result.hops.len();
}
pub fn scan_ports(host: &str, ports: &[u16]) -> Result<Vec<PortScanResult>> {
scan_ports_with_timeout(host, ports, Duration::from_millis(1000))
}
pub fn scan_ports_with_timeout(
host: &str,
ports: &[u16],
timeout: Duration,
) -> Result<Vec<PortScanResult>> {
let mut results = Vec::with_capacity(ports.len());
let addrs: Vec<SocketAddr> = format!("{}:0", host)
.to_socket_addrs()
.map_err(|e| IronError::Other(format!("Failed to resolve host: {}", e)))?
.collect();
let addr = addrs
.first()
.ok_or_else(|| IronError::Other("Could not resolve host".to_string()))?;
for &port in ports {
let socket_addr = SocketAddr::new(addr.ip(), port);
let start = Instant::now();
let status = match TcpStream::connect_timeout(&socket_addr, timeout) {
Ok(_stream) => {
PortStatus::Open
}
Err(e) => {
let err_str = e.to_string().to_lowercase();
if err_str.contains("refused") || err_str.contains("reset") {
PortStatus::Closed
} else if err_str.contains("timed out") || err_str.contains("timeout") {
PortStatus::Filtered
} else {
PortStatus::Error
}
}
};
let connect_time = if status == PortStatus::Open {
Some(start.elapsed().as_secs_f64() * 1000.0)
} else {
None
};
let banner = if status == PortStatus::Open {
grab_banner(host, port, timeout)
} else {
None
};
results.push(PortScanResult {
port,
status,
service: get_service_name(port),
connect_time_ms: connect_time,
banner,
});
}
Ok(results)
}
pub fn scan_port_range(
host: &str,
start_port: u16,
end_port: u16,
timeout: Duration,
) -> Result<Vec<PortScanResult>> {
let ports: Vec<u16> = (start_port..=end_port).collect();
scan_ports_with_timeout(host, &ports, timeout)
}
pub fn common_ports() -> Vec<u16> {
vec![
20, 21, 22, 23, 25, 53, 67, 68, 69, 80, 110, 119, 123, 135, 137, 138, 139, 143, 161, 162,
179, 194, 389, 443, 445, 465, 514, 515, 587, 636, 993, 995, 1080, 1433, 1434, 1521, 1723,
2049, 2082, 2083, 2086, 2087, 2095, 2096, 3306, 3389, 5432, 5900, 5901, 6379, 8080, 8443,
8888, 9000, 9090, 27017,
]
}
pub fn parallel_scan(
host: &str,
ports: &[u16],
timeout: Duration,
max_threads: usize,
) -> Result<Vec<PortScanResult>> {
if ports.is_empty() {
return Ok(Vec::new());
}
let addrs: Vec<SocketAddr> = format!("{}:0", host)
.to_socket_addrs()
.map_err(|e| IronError::Other(format!("Failed to resolve host: {}", e)))?
.collect();
let addr = addrs
.first()
.ok_or_else(|| IronError::Other("Could not resolve host".to_string()))?;
let ip = addr.ip();
let results = Arc::new(Mutex::new(Vec::with_capacity(ports.len())));
let chunk_size = max_threads.max(1).min(ports.len());
let port_chunks: Vec<Vec<u16>> = ports.chunks(chunk_size).map(|c| c.to_vec()).collect();
for chunk in port_chunks {
let mut handles = Vec::with_capacity(chunk.len());
for port in chunk {
let results = Arc::clone(&results);
let handle = thread::spawn(move || {
let socket_addr = SocketAddr::new(ip, port);
let start = Instant::now();
let status = match TcpStream::connect_timeout(&socket_addr, timeout) {
Ok(_stream) => PortStatus::Open,
Err(e) => {
let err_str = e.to_string().to_lowercase();
if err_str.contains("refused") || err_str.contains("reset") {
PortStatus::Closed
} else if err_str.contains("timed out") || err_str.contains("timeout") {
PortStatus::Filtered
} else {
PortStatus::Error
}
}
};
let connect_time = if status == PortStatus::Open {
Some(start.elapsed().as_secs_f64() * 1000.0)
} else {
None
};
let result = PortScanResult {
port,
status,
service: get_service_name(port),
connect_time_ms: connect_time,
banner: None,
};
let mut results = results.lock().unwrap();
results.push(result);
});
handles.push(handle);
}
for handle in handles {
let _ = handle.join();
}
}
let mut final_results = Arc::try_unwrap(results)
.map_err(|_| IronError::Other("Failed to unwrap results".to_string()))?
.into_inner()
.map_err(|e| IronError::Other(format!("Mutex error: {}", e)))?;
final_results.sort_by_key(|r| r.port);
Ok(final_results)
}
pub fn parallel_scan_with_banners(
host: &str,
ports: &[u16],
timeout: Duration,
max_threads: usize,
) -> Result<Vec<PortScanResult>> {
let mut results = parallel_scan(host, ports, timeout, max_threads)?;
for result in &mut results {
if result.status == PortStatus::Open {
result.banner = grab_banner(host, result.port, timeout);
}
}
Ok(results)
}
pub fn get_service_name(port: u16) -> Option<String> {
let name = match port {
20 => "ftp-data",
21 => "ftp",
22 => "ssh",
23 => "telnet",
25 => "smtp",
53 => "dns",
67 => "dhcp-server",
68 => "dhcp-client",
69 => "tftp",
80 => "http",
110 => "pop3",
119 => "nntp",
123 => "ntp",
135 => "msrpc",
137 => "netbios-ns",
138 => "netbios-dgm",
139 => "netbios-ssn",
143 => "imap",
161 => "snmp",
162 => "snmptrap",
179 => "bgp",
194 => "irc",
389 => "ldap",
443 => "https",
445 => "microsoft-ds",
465 => "smtps",
514 => "syslog",
515 => "printer",
587 => "submission",
636 => "ldaps",
993 => "imaps",
995 => "pop3s",
1080 => "socks",
1433 => "mssql",
1434 => "mssql-m",
1521 => "oracle",
1723 => "pptp",
2049 => "nfs",
3306 => "mysql",
3389 => "rdp",
5432 => "postgresql",
5900 => "vnc",
5901 => "vnc-1",
6379 => "redis",
8080 => "http-proxy",
8443 => "https-alt",
8888 => "sun-answerbook",
9000 => "cslistener",
9090 => "zeus-admin",
27017 => "mongodb",
_ => return None,
};
Some(name.to_string())
}
pub fn check_port(host: &str, port: u16, timeout: Duration) -> Result<bool> {
let addr_str = format!("{}:{}", host, port);
let addrs: Vec<SocketAddr> = addr_str
.to_socket_addrs()
.map_err(|e| IronError::Other(format!("Failed to resolve: {}", e)))?
.collect();
if let Some(addr) = addrs.first() {
match TcpStream::connect_timeout(addr, timeout) {
Ok(_) => Ok(true),
Err(_) => Ok(false),
}
} else {
Ok(false)
}
}
pub fn dns_lookup(hostname: &str) -> Result<Vec<IpAddr>> {
let addrs: Vec<SocketAddr> = format!("{}:0", hostname)
.to_socket_addrs()
.map_err(|e| IronError::Other(format!("DNS lookup failed: {}", e)))?
.collect();
Ok(addrs.into_iter().map(|a| a.ip()).collect())
}
pub fn reverse_dns(ip: &str) -> Result<Option<String>> {
if let Ok(_ip_addr) = ip.parse::<IpAddr>() {
#[cfg(target_os = "windows")]
{
let output = Command::new("powershell")
.args([
"-NoProfile",
"-Command",
&format!("[System.Net.Dns]::GetHostEntry('{}').HostName", ip),
])
.output()
.ok();
if let Some(out) = output {
let hostname = String::from_utf8_lossy(&out.stdout).trim().to_string();
if !hostname.is_empty() && !hostname.contains("Exception") {
return Ok(Some(hostname));
}
}
}
#[cfg(not(target_os = "windows"))]
{
let output = Command::new("host").arg(ip).output().ok();
if let Some(out) = output {
let result = String::from_utf8_lossy(&out.stdout);
if let Some(line) = result.lines().next() {
if line.contains("domain name pointer") {
if let Some(hostname) = line.split("pointer").nth(1) {
let hostname = hostname.trim().trim_end_matches('.');
return Ok(Some(hostname.to_string()));
}
}
}
}
}
}
Ok(None)
}
pub fn check_connectivity(hosts: &[&str]) -> HashMap<String, Option<bool>> {
let mut results = HashMap::new();
for host in hosts {
let reachable = ping(host, 1).ok().map(|r| r.is_reachable);
results.insert(host.to_string(), reachable);
}
results
}
pub fn latency_test(host: &str, count: u32) -> Result<Vec<f64>> {
let result = ping(host, count)?;
Ok(result.ping_times.into_iter().flatten().collect())
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ServiceInfo {
pub port: u16,
pub service: String,
#[serde(default)]
pub service_by_port: bool,
pub version: Option<String>,
pub banner: Option<String>,
pub product: Option<String>,
pub extra_info: Option<String>,
}
const OS_CONFIDENCE_WINDOWS_PORTS: u8 = 75;
const OS_CONFIDENCE_LINUX_SSH_PORT: u8 = 60;
const OS_CONFIDENCE_BANNER_DEBIAN_UBUNTU: u8 = 85;
const OS_CONFIDENCE_BANNER_RHEL: u8 = 85;
const OS_CONFIDENCE_BANNER_WINDOWS: u8 = 85;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OsFingerprint {
pub os_family: Option<String>,
pub os_gen: Option<String>,
pub confidence: u8,
pub ttl: Option<u8>,
pub window_size: Option<u32>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NmapScanResult {
pub host: String,
pub ip_addresses: Vec<String>,
pub hostname: Option<String>,
pub is_up: Option<bool>,
pub latency_ms: Option<f64>,
pub services: Vec<ServiceInfo>,
pub os_fingerprint: Option<OsFingerprint>,
pub scan_duration_secs: f64,
}
pub fn grab_banner(host: &str, port: u16, timeout: Duration) -> Option<String> {
use std::io::{Read, Write};
let addr_str = format!("{}:{}", host, port);
let addrs: Vec<SocketAddr> = addr_str.to_socket_addrs().ok()?.collect();
let addr = addrs.first()?;
let mut stream = TcpStream::connect_timeout(addr, timeout).ok()?;
stream.set_read_timeout(Some(timeout)).ok()?;
stream.set_write_timeout(Some(timeout)).ok()?;
let probe = match port {
80 | 8080 | 8000 | 8888 => b"HEAD / HTTP/1.0\r\nHost: localhost\r\n\r\n".to_vec(),
443 | 8443 => return None, 21 => vec![], 22 => vec![], 25 | 587 => b"EHLO localhost\r\n".to_vec(),
110 => vec![], 143 => vec![], 3306 => vec![], 5432 => vec![], 6379 => b"INFO\r\n".to_vec(), 27017 => vec![], _ => vec![], };
if !probe.is_empty() {
let _ = stream.write_all(&probe);
}
let mut buffer = [0u8; 2048];
match stream.read(&mut buffer) {
Ok(n) if n > 0 => {
let banner = String::from_utf8_lossy(&buffer[..n])
.trim()
.chars()
.filter(|c| c.is_ascii_graphic() || c.is_ascii_whitespace())
.take(512)
.collect::<String>();
if !banner.is_empty() {
Some(banner)
} else {
None
}
}
_ => None,
}
}
fn parse_banner_for_version(port: u16, banner: &str) -> ServiceInfo {
let banner_lower = banner.to_lowercase();
let mut info = ServiceInfo {
port,
service: get_service_name(port).unwrap_or_else(|| "unknown".to_string()),
service_by_port: false,
version: None,
banner: Some(banner.chars().take(256).collect()),
product: None,
extra_info: None,
};
let mut named_by_banner = false;
if banner.starts_with("HTTP/") {
named_by_banner = true;
info.service = "http".to_string();
if let Some(server_line) = banner
.lines()
.find(|l| l.to_lowercase().starts_with("server:"))
{
let server = server_line[7..].trim();
info.product = Some(server.to_string());
if server.contains("Apache") {
info.product = Some("Apache".to_string());
if let Some(ver) = extract_version(server, "Apache/") {
info.version = Some(ver);
}
} else if server.contains("nginx") {
info.product = Some("nginx".to_string());
if let Some(ver) = extract_version(server, "nginx/") {
info.version = Some(ver);
}
} else if server.contains("Microsoft-IIS") {
info.product = Some("Microsoft IIS".to_string());
if let Some(ver) = extract_version(server, "Microsoft-IIS/") {
info.version = Some(ver);
}
}
}
}
else if banner_lower.starts_with("ssh-") {
named_by_banner = true;
info.service = "ssh".to_string();
let parts: Vec<&str> = banner.split_whitespace().collect();
if !parts.is_empty() {
info.version = Some(parts[0].to_string());
if parts.len() > 1 {
info.product = Some(parts[1..].join(" "));
}
}
if banner_lower.contains("openssh") {
info.product = Some("OpenSSH".to_string());
} else if banner_lower.contains("dropbear") {
info.product = Some("Dropbear".to_string());
}
}
else if banner.starts_with("220")
&& (banner_lower.contains("smtp")
|| banner_lower.contains("mail")
|| banner_lower.contains("esmtp"))
{
named_by_banner = true;
info.service = "smtp".to_string();
if banner_lower.contains("postfix") {
info.product = Some("Postfix".to_string());
} else if banner_lower.contains("sendmail") {
info.product = Some("Sendmail".to_string());
} else if banner_lower.contains("exim") {
info.product = Some("Exim".to_string());
} else if banner_lower.contains("microsoft") {
info.product = Some("Microsoft SMTP".to_string());
}
}
else if banner.starts_with("220")
&& (banner_lower.contains("ftp") || banner_lower.contains("filezilla"))
{
named_by_banner = true;
info.service = "ftp".to_string();
if banner_lower.contains("vsftpd") {
info.product = Some("vsftpd".to_string());
} else if banner_lower.contains("proftpd") {
info.product = Some("ProFTPD".to_string());
} else if banner_lower.contains("filezilla") {
info.product = Some("FileZilla".to_string());
} else if banner_lower.contains("microsoft") {
info.product = Some("Microsoft FTP".to_string());
}
}
else if banner.len() > 4 && banner.as_bytes().get(4) == Some(&0x0a) {
named_by_banner = true;
info.service = "mysql".to_string();
info.product = Some("MySQL".to_string());
}
else if banner_lower.contains("redis_version") {
named_by_banner = true;
info.service = "redis".to_string();
info.product = Some("Redis".to_string());
if let Some(ver_line) = banner.lines().find(|l| l.starts_with("redis_version:")) {
info.version = Some(ver_line[14..].to_string());
}
}
info.service_by_port = !named_by_banner && get_service_name(port).is_some();
info
}
fn extract_version(s: &str, prefix: &str) -> Option<String> {
if let Some(start) = s.find(prefix) {
let after = &s[start + prefix.len()..];
let version: String = after
.chars()
.take_while(|c| c.is_ascii_digit() || *c == '.' || *c == '-')
.collect();
if !version.is_empty() {
return Some(version);
}
}
None
}
#[allow(dead_code)]
fn guess_os_from_ttl(ttl: u8) -> Option<(&'static str, &'static str, u8)> {
match ttl {
60..=64 => Some(("Linux", "2.6+", 85)),
120..=128 => Some(("Windows", "7/10/11", 85)),
250..=255 => Some(("Network Device", "Cisco/Router", 70)),
_ if ttl > 200 => Some(("BSD/macOS", "", 60)),
_ => None,
}
}
pub fn nmap_scan(host: &str, ports: &[u16], timeout: Duration) -> Result<NmapScanResult> {
let start = Instant::now();
let ip_addresses: Vec<String> = dns_lookup(host)
.map_err(|e| IronError::Other(format!("could not resolve {host}: {e}")))?
.into_iter()
.map(|ip| ip.to_string())
.collect();
let ping_result = ping(host, 1).ok();
let is_up = ping_result.as_ref().map(|p| p.is_reachable);
let latency_ms = ping_result
.as_ref()
.and_then(|p| p.ping_times.first().copied().flatten());
let hostname = ip_addresses
.first()
.and_then(|ip| reverse_dns(ip).ok().flatten());
let mut services = Vec::new();
if is_up == Some(true) || !ip_addresses.is_empty() {
for &port in ports {
let status = scan_single_port(host, port, timeout);
if status == PortStatus::Open {
let service_info = if let Some(banner) = grab_banner(host, port, timeout) {
parse_banner_for_version(port, &banner)
} else {
ServiceInfo {
port,
service: get_service_name(port).unwrap_or_else(|| "unknown".to_string()),
service_by_port: get_service_name(port).is_some(),
version: None,
banner: None,
product: None,
extra_info: None,
}
};
services.push(service_info);
}
}
}
let os_fingerprint = ping_result.as_ref().and_then(|_p| {
let mut os = OsFingerprint {
os_family: None,
os_gen: None,
confidence: 0,
ttl: None,
window_size: None,
};
let has_windows_ports = services
.iter()
.any(|s| s.port == 135 || s.port == 139 || s.port == 445 || s.port == 3389);
let has_linux_ports = services.iter().any(|s| s.port == 22);
if has_windows_ports {
os.os_family = Some("Windows".to_string());
os.confidence = OS_CONFIDENCE_WINDOWS_PORTS;
} else if has_linux_ports {
os.os_family = Some("Linux".to_string());
os.confidence = OS_CONFIDENCE_LINUX_SSH_PORT;
}
for svc in &services {
if let Some(ref banner) = svc.banner {
let banner_lower = banner.to_lowercase();
if banner_lower.contains("ubuntu") || banner_lower.contains("debian") {
os.os_family = Some("Linux".to_string());
os.os_gen = Some("Debian/Ubuntu".to_string());
os.confidence = OS_CONFIDENCE_BANNER_DEBIAN_UBUNTU;
break;
} else if banner_lower.contains("centos")
|| banner_lower.contains("red hat")
|| banner_lower.contains("fedora")
{
os.os_family = Some("Linux".to_string());
os.os_gen = Some("RHEL/CentOS".to_string());
os.confidence = OS_CONFIDENCE_BANNER_RHEL;
break;
} else if banner_lower.contains("windows") || banner_lower.contains("microsoft") {
os.os_family = Some("Windows".to_string());
os.confidence = OS_CONFIDENCE_BANNER_WINDOWS;
break;
}
}
}
if os.confidence > 0 {
Some(os)
} else {
None
}
});
Ok(NmapScanResult {
host: host.to_string(),
ip_addresses,
hostname,
is_up,
latency_ms,
services,
os_fingerprint,
scan_duration_secs: start.elapsed().as_secs_f64(),
})
}
pub fn quick_scan(host: &str) -> Result<NmapScanResult> {
let ports = vec![
21, 22, 23, 25, 53, 80, 110, 139, 143, 443, 445, 993, 995, 3306, 3389, 5432, 8080,
];
nmap_scan(host, &ports, Duration::from_secs(1))
}
pub fn full_scan(host: &str, timeout: Duration) -> Result<NmapScanResult> {
nmap_scan(host, &common_ports(), timeout)
}
fn scan_single_port(host: &str, port: u16, timeout: Duration) -> PortStatus {
let addr_str = format!("{}:{}", host, port);
let addrs: Vec<SocketAddr> = match addr_str.to_socket_addrs() {
Ok(a) => a.collect(),
Err(_) => return PortStatus::Error,
};
if let Some(addr) = addrs.first() {
match TcpStream::connect_timeout(addr, timeout) {
Ok(_) => PortStatus::Open,
Err(e) => {
let err_str = e.to_string().to_lowercase();
if err_str.contains("refused") || err_str.contains("reset") {
PortStatus::Closed
} else if err_str.contains("timed out") || err_str.contains("timeout") {
PortStatus::Filtered
} else {
PortStatus::Error
}
}
}
} else {
PortStatus::Error
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum CaptureProtocol {
All,
Tcp,
Udp,
Icmp,
Arp,
Http,
Https,
Dns,
Ssh,
}
impl std::fmt::Display for CaptureProtocol {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
CaptureProtocol::All => write!(f, "all"),
CaptureProtocol::Tcp => write!(f, "tcp"),
CaptureProtocol::Udp => write!(f, "udp"),
CaptureProtocol::Icmp => write!(f, "icmp"),
CaptureProtocol::Arp => write!(f, "arp"),
CaptureProtocol::Http => write!(f, "http"),
CaptureProtocol::Https => write!(f, "https"),
CaptureProtocol::Dns => write!(f, "dns"),
CaptureProtocol::Ssh => write!(f, "ssh"),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CapturedPacket {
pub number: u32,
pub timestamp: String,
pub source: String,
pub destination: String,
pub protocol: String,
pub length: Option<u32>,
pub info: String,
pub src_port: Option<u16>,
pub dst_port: Option<u16>,
pub tcp_flags: Option<String>,
pub data_preview: Option<String>,
}
#[derive(Debug, Clone)]
pub struct CaptureConfig {
pub interface: Option<String>,
pub protocol: CaptureProtocol,
pub host_filter: Option<String>,
pub port_filter: Option<u16>,
pub packet_count: u32,
pub timeout_secs: u32,
pub include_data: bool,
pub custom_filter: Option<String>,
}
impl Default for CaptureConfig {
fn default() -> Self {
Self {
interface: None,
protocol: CaptureProtocol::All,
host_filter: None,
port_filter: None,
packet_count: 100,
timeout_secs: 30,
include_data: false,
custom_filter: None,
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CaptureResult {
pub interface: String,
pub filter: String,
pub packets: Vec<CapturedPacket>,
pub total_packets: u32,
pub duration_secs: f64,
pub packets_per_sec: f64,
pub total_bytes: Option<u64>,
pub protocol_stats: HashMap<String, u32>,
pub top_sources: Vec<(String, u32)>,
pub top_destinations: Vec<(String, u32)>,
}
pub fn list_capture_interfaces() -> Result<Vec<String>> {
#[cfg(target_os = "windows")]
{
let output = Command::new("netsh")
.args(["interface", "show", "interface"])
.output()
.map_err(|e| IronError::Other(format!("Failed to list interfaces: {}", e)))?;
let stdout = String::from_utf8_lossy(&output.stdout);
let mut interfaces = Vec::new();
for line in stdout.lines().skip(3) {
let parts: Vec<&str> = line.split_whitespace().collect();
if parts.len() >= 4 {
let name = parts[3..].join(" ");
if !name.is_empty() {
interfaces.push(name);
}
}
}
let ps_output = Command::new("powershell")
.args(["-NoProfile",
"-Command",
"Get-NetAdapter | Where-Object {$_.Status -eq 'Up'} | Select-Object -ExpandProperty Name",
])
.output();
if let Ok(out) = ps_output {
let ps_stdout = String::from_utf8_lossy(&out.stdout);
for line in ps_stdout.lines() {
let name = line.trim().to_string();
if !name.is_empty() && !interfaces.contains(&name) {
interfaces.push(name);
}
}
}
Ok(interfaces)
}
#[cfg(not(target_os = "windows"))]
{
let output = Command::new("ip")
.args(["link", "show"])
.output()
.or_else(|_| Command::new("ifconfig").arg("-a").output())
.map_err(|e| IronError::Other(format!("Failed to list interfaces: {}", e)))?;
let stdout = String::from_utf8_lossy(&output.stdout);
let mut interfaces = Vec::new();
for line in stdout.lines() {
if line.contains(": <") {
if let Some(name) = line.split(':').nth(1) {
let name = name.trim().to_string();
if !name.is_empty() {
interfaces.push(name);
}
}
}
else if !line.starts_with(' ') && !line.starts_with('\t') && line.contains(':') {
if let Some(name) = line.split(':').next() {
let name = name.trim().to_string();
if !name.is_empty() && !interfaces.contains(&name) {
interfaces.push(name);
}
}
}
}
Ok(interfaces)
}
}
fn build_capture_filter(config: &CaptureConfig) -> String {
if let Some(ref custom) = config.custom_filter {
return custom.clone();
}
let mut filters = Vec::new();
match config.protocol {
CaptureProtocol::All => {}
CaptureProtocol::Tcp => filters.push("tcp".to_string()),
CaptureProtocol::Udp => filters.push("udp".to_string()),
CaptureProtocol::Icmp => filters.push("icmp".to_string()),
CaptureProtocol::Arp => filters.push("arp".to_string()),
CaptureProtocol::Http => filters.push("tcp port 80".to_string()),
CaptureProtocol::Https => filters.push("tcp port 443".to_string()),
CaptureProtocol::Dns => filters.push("port 53".to_string()),
CaptureProtocol::Ssh => filters.push("tcp port 22".to_string()),
}
if let Some(ref host) = config.host_filter {
filters.push(format!("host {}", host));
}
if let Some(port) = config.port_filter {
filters.push(format!("port {}", port));
}
if filters.is_empty() {
String::new()
} else {
filters.join(" and ")
}
}
pub fn capture_packets(config: &CaptureConfig) -> Result<CaptureResult> {
let start = Instant::now();
let filter = build_capture_filter(config);
#[cfg(target_os = "windows")]
{
capture_packets_windows(config, &filter, start)
}
#[cfg(not(target_os = "windows"))]
{
capture_packets_unix(config, &filter, start)
}
}
#[cfg(target_os = "windows")]
fn capture_packets_windows(
config: &CaptureConfig,
filter: &str,
start: Instant,
) -> Result<CaptureResult> {
let tshark_result = capture_with_tshark(config, filter);
if tshark_result.is_ok() {
return finalize_capture_result(tshark_result?, start, filter);
}
let windump_result = capture_with_windump(config, filter);
if windump_result.is_ok() {
return finalize_capture_result(windump_result?, start, filter);
}
capture_with_netsh(config, filter, start)
}
#[cfg(not(target_os = "windows"))]
fn capture_packets_unix(
config: &CaptureConfig,
filter: &str,
start: Instant,
) -> Result<CaptureResult> {
let tcpdump_result = capture_with_tcpdump(config, filter);
if tcpdump_result.is_ok() {
return finalize_capture_result(tcpdump_result?, start, filter);
}
let tshark_result = capture_with_tshark(config, filter);
if tshark_result.is_ok() {
return finalize_capture_result(tshark_result?, start, filter);
}
Err(IronError::Other(
"No packet capture tool available. Install tcpdump or Wireshark.".to_string(),
))
}
fn capture_with_tshark(config: &CaptureConfig, filter: &str) -> Result<Vec<CapturedPacket>> {
let mut args = vec![
"-c".to_string(),
config.packet_count.to_string(),
"-a".to_string(),
format!("duration:{}", config.timeout_secs),
"-T".to_string(),
"fields".to_string(),
"-e".to_string(),
"frame.number".to_string(),
"-e".to_string(),
"frame.time_relative".to_string(),
"-e".to_string(),
"ip.src".to_string(),
"-e".to_string(),
"ip.dst".to_string(),
"-e".to_string(),
"_ws.col.Protocol".to_string(),
"-e".to_string(),
"frame.len".to_string(),
"-e".to_string(),
"tcp.srcport".to_string(),
"-e".to_string(),
"tcp.dstport".to_string(),
"-e".to_string(),
"tcp.flags".to_string(),
"-e".to_string(),
"_ws.col.Info".to_string(),
"-E".to_string(),
"separator=|".to_string(),
];
if let Some(ref iface) = config.interface {
args.push("-i".to_string());
args.push(iface.clone());
}
if !filter.is_empty() {
args.push("-f".to_string());
args.push(filter.to_string());
}
let output = Command::new("tshark")
.args(&args)
.output()
.map_err(|e| IronError::Other(format!("tshark not found or failed: {}", e)))?;
if !output.status.success() {
return Err(IronError::Other(format!(
"tshark failed: {}",
String::from_utf8_lossy(&output.stderr)
)));
}
parse_tshark_output(&String::from_utf8_lossy(&output.stdout))
}
fn parse_tshark_output(output: &str) -> Result<Vec<CapturedPacket>> {
let mut packets = Vec::new();
for line in output.lines() {
let parts: Vec<&str> = line.split('|').collect();
if parts.len() >= 6 {
let Ok(number) = parts[0].parse::<u32>() else {
continue;
};
let packet = CapturedPacket {
number,
timestamp: parts[1].to_string(),
source: parts[2].to_string(),
destination: parts[3].to_string(),
protocol: parts[4].to_string(),
length: parts[5].parse().ok(),
src_port: parts.get(6).and_then(|s| s.parse().ok()),
dst_port: parts.get(7).and_then(|s| s.parse().ok()),
tcp_flags: parts
.get(8)
.map(|s| s.to_string())
.filter(|s| !s.is_empty()),
info: parts.get(9).unwrap_or(&"").to_string(),
data_preview: None,
};
packets.push(packet);
}
}
Ok(packets)
}
#[cfg(not(target_os = "windows"))]
fn capture_with_tcpdump(config: &CaptureConfig, filter: &str) -> Result<Vec<CapturedPacket>> {
let mut args = vec![
"-c".to_string(),
config.packet_count.to_string(),
"-nn".to_string(), "-tttt".to_string(), "-l".to_string(), ];
if let Some(ref iface) = config.interface {
args.push("-i".to_string());
args.push(iface.clone());
}
if config.include_data {
args.push("-X".to_string()); }
if !filter.is_empty() {
args.push(filter.to_string());
}
let output = Command::new("tcpdump")
.args(&args)
.output()
.map_err(|e| IronError::Other(format!("tcpdump not found or failed: {}", e)))?;
if !output.status.success() {
let stderr = String::from_utf8_lossy(&output.stderr);
if stderr.contains("permission denied") || stderr.contains("Operation not permitted") {
return Err(IronError::Other(
"Permission denied. Run with administrator/root privileges.".to_string(),
));
}
return Err(IronError::Other(format!("tcpdump failed: {}", stderr)));
}
parse_tcpdump_output(&String::from_utf8_lossy(&output.stdout))
}
#[cfg(not(target_os = "windows"))]
fn parse_tcpdump_output(output: &str) -> Result<Vec<CapturedPacket>> {
let mut packets = Vec::new();
let mut packet_num = 0u32;
for line in output.lines() {
if line.is_empty() || line.starts_with('\t') || line.starts_with("0x") {
continue;
}
let parts: Vec<&str> = line.splitn(4, ' ').collect();
if parts.len() >= 4 {
packet_num += 1;
let timestamp = format!("{} {}", parts[0], parts[1]);
let proto = parts[2].to_string();
let rest = parts[3];
let (source, destination, info) = parse_tcpdump_addresses(rest);
let packet = CapturedPacket {
number: packet_num,
timestamp,
source,
destination,
protocol: proto,
length: extract_length_from_tcpdump(rest),
src_port: None, dst_port: None,
tcp_flags: extract_tcp_flags(rest),
info,
data_preview: None,
};
packets.push(packet);
}
}
Ok(packets)
}
#[cfg(not(target_os = "windows"))]
fn parse_tcpdump_addresses(line: &str) -> (String, String, String) {
let parts: Vec<&str> = line.split(" > ").collect();
if parts.len() >= 2 {
let source = parts[0].to_string();
let rest: Vec<&str> = parts[1].splitn(2, ':').collect();
let destination = rest[0].to_string();
let info = rest.get(1).unwrap_or(&"").trim().to_string();
(source, destination, info)
} else {
(String::new(), String::new(), line.to_string())
}
}
fn extract_length_from_tcpdump(line: &str) -> Option<u32> {
let after = &line[line.find("length ")? + 7..];
after
.chars()
.take_while(|c| c.is_ascii_digit())
.collect::<String>()
.parse()
.ok()
}
fn extract_tcp_flags(line: &str) -> Option<String> {
if let Some(start) = line.find("Flags [") {
if let Some(end) = line[start..].find(']') {
return Some(line[start + 7..start + end].to_string());
}
}
None
}
#[cfg(target_os = "windows")]
fn capture_with_windump(config: &CaptureConfig, filter: &str) -> Result<Vec<CapturedPacket>> {
let mut args = vec![
"-c".to_string(),
config.packet_count.to_string(),
"-nn".to_string(),
"-tttt".to_string(),
];
if let Some(ref iface) = config.interface {
args.push("-i".to_string());
args.push(iface.clone());
}
if !filter.is_empty() {
args.push(filter.to_string());
}
let output = Command::new("windump")
.args(&args)
.output()
.map_err(|e| IronError::Other(format!("windump not found: {}", e)))?;
if !output.status.success() {
return Err(IronError::Other(format!(
"windump failed: {}",
String::from_utf8_lossy(&output.stderr)
)));
}
parse_windump_output(&String::from_utf8_lossy(&output.stdout))
}
#[cfg(target_os = "windows")]
fn parse_windump_output(output: &str) -> Result<Vec<CapturedPacket>> {
let mut packets = Vec::new();
let mut packet_num = 0u32;
for line in output.lines() {
if line.is_empty() || line.starts_with('\t') {
continue;
}
let parts: Vec<&str> = line.splitn(4, ' ').collect();
if parts.len() >= 4 {
packet_num += 1;
let packet = CapturedPacket {
number: packet_num,
timestamp: format!(
"{} {}",
parts.first().unwrap_or(&""),
parts.get(1).unwrap_or(&"")
),
source: String::new(),
destination: String::new(),
protocol: parts.get(2).unwrap_or(&"").to_string(),
length: extract_length_from_tcpdump(parts.get(3).unwrap_or(&"")),
src_port: None,
dst_port: None,
tcp_flags: extract_tcp_flags(parts.get(3).unwrap_or(&"")),
info: parts.get(3).unwrap_or(&"").to_string(),
data_preview: None,
};
packets.push(packet);
}
}
Ok(packets)
}
#[cfg(target_os = "windows")]
fn capture_with_netsh(
_config: &CaptureConfig,
_filter: &str,
_start: Instant,
) -> Result<CaptureResult> {
Err(IronError::Other(
"No packet capture tool found. Install Wireshark (tshark) or WinDump for packet capture."
.to_string(),
))
}
fn finalize_capture_result(
packets: Vec<CapturedPacket>,
start: Instant,
filter: &str,
) -> Result<CaptureResult> {
let duration = start.elapsed().as_secs_f64();
let total_packets = packets.len() as u32;
let total_bytes: Option<u64> = packets.iter().map(|p| p.length.map(u64::from)).sum();
let mut protocol_stats: HashMap<String, u32> = HashMap::new();
let mut source_counts: HashMap<String, u32> = HashMap::new();
let mut dest_counts: HashMap<String, u32> = HashMap::new();
for pkt in &packets {
*protocol_stats.entry(pkt.protocol.clone()).or_insert(0) += 1;
if !pkt.source.is_empty() {
*source_counts.entry(pkt.source.clone()).or_insert(0) += 1;
}
if !pkt.destination.is_empty() {
*dest_counts.entry(pkt.destination.clone()).or_insert(0) += 1;
}
}
let mut top_sources: Vec<(String, u32)> = source_counts.into_iter().collect();
top_sources.sort_by_key(|p| std::cmp::Reverse(p.1));
top_sources.truncate(10);
let mut top_destinations: Vec<(String, u32)> = dest_counts.into_iter().collect();
top_destinations.sort_by_key(|p| std::cmp::Reverse(p.1));
top_destinations.truncate(10);
Ok(CaptureResult {
interface: "default".to_string(),
filter: filter.to_string(),
packets,
total_packets,
duration_secs: duration,
packets_per_sec: if duration > 0.0 {
total_packets as f64 / duration
} else {
0.0
},
total_bytes,
protocol_stats,
top_sources,
top_destinations,
})
}
pub fn quick_capture(packet_count: u32) -> Result<CaptureResult> {
capture_packets(&CaptureConfig {
packet_count,
timeout_secs: 30,
..Default::default()
})
}
pub fn capture_tcp(host: Option<&str>, port: Option<u16>, count: u32) -> Result<CaptureResult> {
capture_packets(&CaptureConfig {
protocol: CaptureProtocol::Tcp,
host_filter: host.map(String::from),
port_filter: port,
packet_count: count,
..Default::default()
})
}
pub fn capture_http(count: u32) -> Result<CaptureResult> {
capture_packets(&CaptureConfig {
protocol: CaptureProtocol::Http,
packet_count: count,
..Default::default()
})
}
pub fn capture_dns(count: u32) -> Result<CaptureResult> {
capture_packets(&CaptureConfig {
protocol: CaptureProtocol::Dns,
packet_count: count,
..Default::default()
})
}
pub fn is_capture_available() -> bool {
#[cfg(target_os = "windows")]
{
Command::new("tshark").arg("--version").output().is_ok()
|| Command::new("windump").arg("-h").output().is_ok()
}
#[cfg(not(target_os = "windows"))]
{
Command::new("tcpdump").arg("--version").output().is_ok()
|| Command::new("tshark").arg("--version").output().is_ok()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_get_service_name() {
assert_eq!(get_service_name(22), Some("ssh".to_string()));
assert_eq!(get_service_name(80), Some("http".to_string()));
assert_eq!(get_service_name(443), Some("https".to_string()));
assert_eq!(get_service_name(12345), None);
}
#[test]
fn test_common_ports() {
let ports = common_ports();
assert!(ports.contains(&22));
assert!(ports.contains(&80));
assert!(ports.contains(&443));
}
}
#[cfg(test)]
mod traceroute_parse_tests {
use super::*;
const WINDOWS_REACHED: &str = r#"
Tracing route to example.com [93.184.216.34]
over a maximum of 30 hops:
1 <1 ms <1 ms <1 ms 192.168.1.1
2 2 ms 1 ms 1 ms 10.0.0.1
3 * * * Request timed out.
4 12 ms 11 ms 13 ms 203.0.113.5
5 20 ms 19 ms 21 ms 93.184.216.34
Trace complete.
"#;
const WINDOWS_NOT_REACHED: &str = r#"
Tracing route to 8.8.8.8 over a maximum of 30 hops:
1 1 ms 1 ms 1 ms 192.168.1.1
2 * * * Request timed out.
3 9 ms 9 ms 8 ms 10.20.0.1
4 * * * Destination host unreachable.
Trace complete.
"#;
const WINDOWS_UNRESOLVED: &str = r#"
Unable to resolve target system name no-such-host.invalid.
"#;
const LINUX_REACHED: &str = r#"traceroute to 93.184.216.34 (93.184.216.34), 15 hops max, 60 byte packets
1 192.168.1.1 0.543 ms 0.412 ms 0.398 ms
2 * * *
3 10.0.0.1 4.120 ms 4.011 ms *
4 93.184.216.34 20.113 ms 19.998 ms 20.201 ms
"#;
fn parse(output: &str) -> TracerouteResult {
let mut result = TracerouteResult {
target: "host".to_string(),
target_ip: None,
hops: Vec::new(),
destination_reached: None,
total_hops: 0,
};
parse_traceroute_output(output, &mut result);
result
}
#[test]
fn bracketed_windows_header_names_the_target_and_reaching_it_is_seen() {
let r = parse(WINDOWS_REACHED);
assert_eq!(r.target_ip.as_deref(), Some("93.184.216.34"));
assert_eq!(r.destination_reached, Some(true));
assert_eq!(r.total_hops, 5);
}
#[test]
fn bare_address_windows_header_names_the_target_and_missing_it_is_seen() {
let r = parse(WINDOWS_NOT_REACHED);
assert_eq!(r.target_ip.as_deref(), Some("8.8.8.8"));
assert_eq!(r.destination_reached, Some(false));
}
#[test]
fn without_a_target_header_whether_the_route_arrived_is_unknown() {
let r = parse(WINDOWS_UNRESOLVED);
assert_eq!(r.target_ip, None);
assert_eq!(r.destination_reached, None);
assert!(r.hops.is_empty());
}
#[test]
fn linux_header_names_the_target_in_parentheses() {
let r = parse(LINUX_REACHED);
assert_eq!(r.target_ip.as_deref(), Some("93.184.216.34"));
assert_eq!(r.destination_reached, Some(true));
}
#[test]
fn windows_sub_millisecond_reply_is_a_reply_with_no_invented_time() {
let r = parse(WINDOWS_REACHED);
let hop = &r.hops[0];
assert_eq!(hop.address.as_deref(), Some("192.168.1.1"));
assert!(hop.responded);
assert!(hop.rtt_below_1ms);
assert_eq!(hop.rtt_probes, vec![None, None, None]);
assert_eq!(hop.rtt_ms, None);
let hop = &r.hops[1];
assert_eq!(hop.rtt_ms, Some(2.0));
assert!(!hop.rtt_below_1ms);
assert_eq!(hop.rtt_probes, vec![Some(2.0), Some(1.0), Some(1.0)]);
}
#[test]
fn a_silent_hop_is_not_a_reply_even_though_its_line_has_words() {
let r = parse(WINDOWS_REACHED);
let silent = &r.hops[2];
assert_eq!(silent.address, None);
assert!(silent.rtt_probes.is_empty());
assert_eq!(silent.rtt_ms, None);
assert!(!silent.responded);
let r = parse(WINDOWS_NOT_REACHED);
assert!(
!r.hops[3].responded,
"destination unreachable is not a reply"
);
}
#[test]
fn linux_silent_hop_is_unresponded_and_partial_hop_keeps_its_probes() {
let r = parse(LINUX_REACHED);
assert!(!r.hops[1].responded);
assert!(r.hops[2].responded);
assert_eq!(r.hops[2].rtt_probes, vec![Some(4.12), Some(4.011)]);
assert_eq!(r.hops[2].rtt_ms, Some(4.12));
}
}
#[cfg(test)]
mod banner_parse_tests {
use super::*;
const POSTFIX: &str = "220 mx.example.com ESMTP Postfix";
const VSFTPD: &str = "220 (vsFTPd 3.0.3)";
const PLAIN_220: &str = "220 Welcome to the service";
#[test]
fn smtp_greeting_is_not_read_as_ftp() {
let info = parse_banner_for_version(25, POSTFIX);
assert_eq!(info.service, "smtp");
assert_eq!(info.product.as_deref(), Some("Postfix"));
assert!(!info.service_by_port);
}
#[test]
fn ftp_greeting_with_a_marker_is_ftp() {
let info = parse_banner_for_version(21, VSFTPD);
assert_eq!(info.service, "ftp");
assert_eq!(info.product.as_deref(), Some("vsftpd"));
assert!(!info.service_by_port);
}
#[test]
fn a_220_without_a_marker_does_not_name_ftp() {
let on_ftp_port = parse_banner_for_version(21, PLAIN_220);
assert_eq!(on_ftp_port.service, "ftp");
assert!(on_ftp_port.service_by_port);
assert_eq!(on_ftp_port.product, None);
let unnamed = parse_banner_for_version(2525, PLAIN_220);
assert_eq!(unnamed.service, "unknown");
assert!(!unnamed.service_by_port);
}
#[test]
fn unrecognised_banner_on_a_known_port_is_the_port_guess() {
let info = parse_banner_for_version(22, "garbage");
assert_eq!(info.service, "ssh");
assert!(info.service_by_port);
}
}
#[cfg(test)]
mod parallel_scan_tests {
use super::*;
#[test]
fn empty_port_list_scans_nothing_and_does_not_panic() {
let results = parallel_scan("127.0.0.1", &[], Duration::from_millis(50), 10).unwrap();
assert!(results.is_empty());
}
#[test]
fn zero_thread_budget_still_scans_every_port() {
let results = parallel_scan("127.0.0.1", &[1, 2], Duration::from_millis(50), 0).unwrap();
assert_eq!(results.len(), 2);
}
}
#[cfg(test)]
mod tshark_parse_tests {
use super::*;
const TSHARK: &str = "1|0.000000000|192.168.1.10|93.184.216.34|TCP|74|51234|443|0x0002|51234 → 443 [SYN] Seq=0 Win=64240 Len=0
2|0.012345678|93.184.216.34|192.168.1.10|TCP|74|443|51234|0x0012|443 → 51234 [SYN, ACK] Seq=0 Ack=1 Win=65535 Len=0
|0.020000000|192.168.1.10|8.8.8.8|UDP|72|||||
3|0.030000000|192.168.1.10|8.8.8.8|UDP|72|53211|53||DNS Standard query A example.com
";
#[test]
fn rows_keep_their_frame_number_and_a_row_without_one_is_dropped() {
let packets = parse_tshark_output(TSHARK).unwrap();
let numbers: Vec<u32> = packets.iter().map(|p| p.number).collect();
assert_eq!(numbers, vec![1, 2, 3]);
assert!(packets.iter().all(|p| p.number != 0));
}
#[test]
fn tshark_fields_are_read_per_column() {
let packets = parse_tshark_output(TSHARK).unwrap();
let syn = &packets[0];
assert_eq!(syn.source, "192.168.1.10");
assert_eq!(syn.protocol, "TCP");
assert_eq!(syn.length, Some(74));
assert_eq!(syn.src_port, Some(51234));
assert_eq!(syn.dst_port, Some(443));
assert_eq!(syn.tcp_flags.as_deref(), Some("0x0002"));
let dns = &packets[2];
assert_eq!(dns.tcp_flags, None);
assert_eq!(dns.info, "DNS Standard query A example.com");
}
}
#[cfg(test)]
mod ping_parse_tests {
use super::*;
const WINDOWS_REPLY: &str = "
Pinging 127.0.0.1 with 32 bytes of data:
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Ping statistics for 127.0.0.1:
Packets: Sent = 2, Received = 2, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 0ms, Maximum = 0ms, Average = 0ms
";
const WINDOWS_TIMEOUT: &str = "
Pinging 192.0.2.1 with 32 bytes of data:
Request timed out.
Ping statistics for 192.0.2.1:
Packets: Sent = 1, Received = 0, Lost = 1 (100% loss),
";
const LINUX_REPLY: &str = "PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=1.88 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.077 ms
--- 127.0.0.1 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1005ms
rtt min/avg/max/mdev = 0.077/0.978/1.880/0.901 ms
";
const LINUX_TIMEOUT: &str = "PING 192.0.2.1 (192.0.2.1) 56(84) bytes of data.
--- 192.0.2.1 ping statistics ---
1 packets transmitted, 0 received, 100% packet loss, time 0ms
";
fn parse(output: &str) -> (bool, PingResult) {
let mut result = PingResult::new("host");
let recognised = parse_ping_output(output, &mut result);
(recognised, result)
}
#[test]
fn english_output_gives_a_verdict_either_way() {
for (output, reachable, received) in [
(WINDOWS_REPLY, true, 2),
(WINDOWS_TIMEOUT, false, 0),
(LINUX_REPLY, true, 2),
(LINUX_TIMEOUT, false, 0),
] {
let (recognised, r) = parse(output);
assert!(recognised, "not recognised:\n{output}");
assert_eq!(r.is_reachable, reachable, "{output}");
assert_eq!(r.packets_received, received, "{output}");
}
}
#[test]
fn a_host_that_never_answered_has_no_round_trip_time() {
for output in [WINDOWS_TIMEOUT, LINUX_TIMEOUT] {
let (_, r) = parse(output);
assert_eq!(
(r.rtt_min_ms, r.rtt_avg_ms, r.rtt_max_ms),
(None, None, None),
"{output}"
);
assert_eq!(r.packet_loss_percent, 100.0);
}
let (_, r) = parse(LINUX_REPLY);
assert_eq!(
(r.rtt_min_ms, r.rtt_avg_ms, r.rtt_max_ms),
(Some(0.077), Some(0.978), Some(1.880))
);
assert_eq!(r.rtt_stddev_ms, Some(0.901));
let (_, r) = parse(WINDOWS_REPLY);
assert_eq!(r.rtt_avg_ms, Some(0.0));
assert_eq!(r.packet_loss_percent, 0.0);
}
#[test]
fn windows_sub_millisecond_replies_record_no_time() {
const WINDOWS_MIXED: &str = "
Pinging 192.168.1.1 with 32 bytes of data:
Reply from 192.168.1.1: bytes=32 time=2ms TTL=64
Reply from 192.168.1.1: bytes=32 time<1ms TTL=64
Reply from 192.168.1.1: bytes=32 time=1ms TTL=64
Ping statistics for 192.168.1.1:
Packets: Sent = 3, Received = 3, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 0ms, Maximum = 2ms, Average = 1ms
";
let (recognised, r) = parse(WINDOWS_MIXED);
assert!(recognised);
assert_eq!(r.ping_times, vec![Some(2.0), None, Some(1.0)]);
assert_eq!(r.rtt_min_ms, Some(0.0));
assert_eq!(r.rtt_avg_ms, Some(1.0));
let (_, r) = parse(WINDOWS_REPLY);
assert_eq!(r.ping_times, vec![None, None]);
assert!(r.is_reachable);
let mut r = PingResult::new("host");
parse_ping_output(
"Reply from 10.0.0.1: bytes=32 time<1ms TTL=64\n\
Reply from 10.0.0.1: bytes=32 time=4ms TTL=64\n",
&mut r,
);
assert_eq!(r.ping_times, vec![None, Some(4.0)]);
assert_eq!(
(r.rtt_min_ms, r.rtt_avg_ms, r.rtt_max_ms),
(Some(4.0), Some(4.0), Some(4.0))
);
}
#[test]
fn linux_replies_record_each_exact_time() {
let (_, r) = parse(LINUX_REPLY);
assert_eq!(r.ping_times, vec![Some(1.88), Some(0.077)]);
}
#[test]
fn unrecognised_output_gives_no_verdict_rather_than_unreachable() {
let relabelled = WINDOWS_REPLY
.replace("Packets: Sent", "Paquetes: Enviados")
.replace("Received", "Recibidos")
.replace("time<", "tiempo<")
.replace("Minimum", "Minimo");
let (recognised, _) = parse(&relabelled);
assert!(
!recognised,
"the relabelled output was matched:\n{relabelled}"
);
}
}