pub mod wire;
use std::net::IpAddr;
use std::time::{Duration, SystemTime};
use serde::{Deserialize, Serialize};
use crate::config::DetectionEnvelope;
use crate::config::{IdleScan, RetryConfig, TimeoutScale};
use crate::info;
use crate::model::capture::CaptureCounts;
use crate::model::confidence::Confidence;
use crate::model::finding::{
DetectionClass, DetectionId, Excerpt, Finding, Reference, Severity, Version,
};
use crate::model::host::os::OsFingerprint;
use crate::model::host::path::Hop;
use crate::model::host::telemetry::HostTelemetry;
use crate::model::host::{
EvidenceSource, HardwareInfo, Host, IpProtocolState, StatusProtocol, StatusReason,
};
use crate::model::host::{OsEvidence, OsSource};
use crate::model::ip::range::{IpRange, Ipv4Range, Ipv6Range};
use crate::model::ip::scoped::Zone;
use crate::model::ip::set::IpSet;
use crate::model::mac::MacAddr;
use crate::model::port::discovery::{Discovery, ScanResponse};
use crate::model::port::security::{CertificateInfo, Security};
use crate::model::port::{Port, PortSet, PortState, Service};
use crate::model::target::{TargetMap, TargetSet};
use crate::model::tls::{
CipherSuite, Interruption, TlsSupport, TlsVersion, UnfinishedVersion, VersionSupport,
};
use crate::report::ScannerKind;
use crate::report::WindowSummary;
use crate::report::{
Attachment, AttachmentSource, EvasionRecord, PhaseOrigin, PhaseParts, PortScope, ProbeStats,
ProbeStatsParts, Refusal, ScanKind, ScanPhase, ScanSettings, ScannerFailure, ScopeParts,
StopReason, TargetScope,
};
use crate::system::privilege::Privilege;
use std::num::{NonZeroU8, NonZeroU32};
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct HostRecord {
pub primary_ip: IpAddr,
#[serde(default)]
pub ips: Vec<IpAddr>,
#[serde(default)]
pub hostname: Option<String>,
pub status: String,
#[serde(default)]
pub reasons: Vec<StatusReasonRecord>,
#[serde(default)]
pub os: Option<OsRecord>,
#[serde(default)]
pub os_evidence: Vec<OsEvidenceRecord>,
#[serde(default)]
pub hardware: Option<HardwareRecord>,
#[serde(default)]
pub zone: Option<ZoneRecord>,
#[serde(default)]
pub telemetry: TelemetryRecord,
#[serde(default)]
pub path: Vec<HopRecord>,
#[serde(default)]
pub roles: Vec<String>,
#[serde(default)]
pub filtering: Vec<String>,
#[serde(default)]
pub ip_protocols: Vec<IpProtocolRecord>,
pub first_seen: SystemTime,
pub last_seen: SystemTime,
#[serde(default)]
pub ports: Vec<PortRecord>,
#[serde(default)]
pub findings: Vec<FindingRecord>,
}
impl From<&Host> for HostRecord {
fn from(host: &Host) -> Self {
Self {
primary_ip: host.primary_ip(),
ips: host.ips().iter().copied().collect(),
hostname: host.hostname().map(str::to_owned),
status: wire::host_status_name(host.status()).to_owned(),
reasons: {
let mut reasons: Vec<_> = host
.reasons()
.iter()
.map(StatusReasonRecord::from)
.collect();
reasons.sort();
reasons
},
os: host.os().map(OsRecord::from),
os_evidence: host.os_evidence().map(OsEvidenceRecord::from).collect(),
hardware: host.hardware().map(HardwareRecord::from),
zone: host.zone().map(ZoneRecord::from),
telemetry: TelemetryRecord::from(host.telemetry()),
path: host.path().hops().iter().map(HopRecord::from).collect(),
roles: {
let mut roles: Vec<_> = host
.network_roles()
.iter()
.map(|role| wire::network_role_name(*role).to_owned())
.collect();
roles.sort();
roles
},
filtering: {
let mut filtering: Vec<_> = host
.filtering()
.iter()
.map(|f| wire::filtering_name(*f).to_owned())
.collect();
filtering.sort();
filtering
},
ip_protocols: host
.ip_protocols()
.iter()
.map(|(number, state)| IpProtocolRecord {
protocol: *number,
state: wire::ip_protocol_state_name(*state).to_owned(),
})
.collect(),
first_seen: host.first_seen(),
last_seen: host.last_seen(),
ports: host.ports().map(PortRecord::from).collect(),
findings: host.findings().map(FindingRecord::from).collect(),
}
}
}
impl From<&HostRecord> for Host {
fn from(record: &HostRecord) -> Self {
let mut host = Host::new(record.primary_ip);
host.extend_ips(record.ips.iter().copied());
if let Some(hostname) = &record.hostname {
host.set_hostname(Some(hostname.clone()));
}
if let Some(status) = wire::host_status(&record.status) {
host.set_status(status);
}
for reason in &record.reasons {
host.add_reason(reason.into());
}
if let Some(os) = &record.os {
host.set_os(os.into());
}
for evidence in &record.os_evidence {
host.record_os_evidence(evidence.into());
}
if let Some(hardware) = record.hardware.as_ref().and_then(HardwareRecord::rebuild) {
host.set_hardware(hardware);
}
if let Some(zone) = &record.zone {
host.set_zone(zone.into());
}
record.telemetry.restore(&mut host);
for hop in &record.path {
host.record_hop(hop.into());
}
for role in record.roles.iter().filter_map(|r| wire::network_role(r)) {
host.add_network_role(role);
}
for filtering in record.filtering.iter().filter_map(|f| wire::filtering(f)) {
host.add_filtering(filtering);
}
for entry in &record.ip_protocols {
let state = wire::ip_protocol_state(&entry.state).unwrap_or(IpProtocolState::Unasked);
host.record_ip_protocol(entry.protocol, state);
}
for entry in &record.ports {
let Some(port) = entry.rebuild() else {
info!(
verbosity = 2,
"port {}/{} was recorded under a protocol this build cannot read; \
leaving it out rather than filing it under another",
entry.port,
entry.protocol
);
continue;
};
host.add_port(port);
}
for finding in record.findings.iter().filter_map(FindingRecord::rebuild) {
host.add_finding(finding);
}
host.restore_seen(record.first_seen, record.last_seen);
host
}
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct StatusReasonRecord {
pub protocol: String,
#[serde(default)]
pub source: Option<IpAddr>,
#[serde(default)]
pub source_withheld: bool,
#[serde(default)]
pub details: Option<String>,
}
impl From<&StatusReason> for StatusReasonRecord {
fn from(reason: &StatusReason) -> Self {
Self {
protocol: wire::status_protocol_name(&reason.protocol).into_owned(),
source: reason.source.address(),
source_withheld: reason.source.is_withheld(),
details: reason.details.as_ref().map(|d| d.to_string()),
}
}
}
impl From<&StatusReasonRecord> for StatusReason {
fn from(record: &StatusReasonRecord) -> Self {
let protocol = wire::status_protocol(&record.protocol)
.unwrap_or_else(|| StatusProtocol::Custom(record.protocol.as_str().into()));
let mut reason = StatusReason::new(protocol, "");
reason.details = record.details.as_deref().map(Into::into);
reason.source = match record.source {
_ if record.source_withheld => EvidenceSource::Withheld,
Some(address) => EvidenceSource::Intermediary(address),
None => EvidenceSource::Host,
};
reason
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct OsRecord {
pub name: String,
pub accuracy: u8,
#[serde(default)]
pub device: Option<String>,
#[serde(default)]
pub family: Option<String>,
#[serde(default)]
pub generation: Option<String>,
#[serde(default)]
pub vendor: Option<String>,
#[serde(default)]
pub kernel: Option<String>,
#[serde(default)]
pub arch: Option<String>,
#[serde(default)]
pub detail_accuracy: Option<u8>,
#[serde(default)]
pub evidence: Option<String>,
#[serde(default)]
pub cpes: Vec<String>,
}
impl From<&OsFingerprint> for OsRecord {
fn from(os: &OsFingerprint) -> Self {
Self {
name: os.name().to_owned(),
accuracy: os.accuracy(),
family: os.family().map(str::to_owned),
device: os.device().map(str::to_owned),
generation: os.generation().map(str::to_owned),
vendor: os.vendor().map(str::to_owned),
kernel: os.kernel().map(str::to_owned),
arch: os.arch().map(str::to_owned),
detail_accuracy: os.detail_accuracy(),
evidence: os.evidence().map(str::to_owned),
cpes: os.cpes().iter().map(|cpe| cpe.to_string()).collect(),
}
}
}
impl From<&OsRecord> for OsFingerprint {
fn from(record: &OsRecord) -> Self {
let mut os = OsFingerprint::new(record.name.clone(), record.accuracy);
if let Some(family) = &record.family {
os = os.with_family(family.clone());
}
if let Some(device) = &record.device {
os = os.with_device(device.clone());
}
if let Some(generation) = &record.generation {
os = os.with_generation(generation.clone());
}
if let Some(vendor) = &record.vendor {
os = os.with_vendor(vendor.clone());
}
if let Some(kernel) = &record.kernel {
os = os.with_kernel(kernel.clone());
}
if let Some(arch) = &record.arch {
os = os.with_arch(arch.clone());
}
if let Some(accuracy) = record.detail_accuracy {
os = os.with_detail_accuracy(accuracy);
}
if let Some(evidence) = &record.evidence {
os = os.with_evidence(evidence.clone());
}
for cpe in &record.cpes {
os.add_cpe(cpe.clone());
}
os
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct OsEvidenceRecord {
pub source: String,
#[serde(default)]
pub family: Option<String>,
#[serde(default)]
pub device: Option<String>,
#[serde(default)]
pub vendor: Option<String>,
#[serde(default)]
pub product: Option<String>,
#[serde(default)]
pub version: Option<String>,
#[serde(default)]
pub kernel: Option<String>,
#[serde(default)]
pub arch: Option<String>,
#[serde(default)]
pub cpe: Option<String>,
pub confidence: f32,
pub evidence: String,
}
impl From<&OsEvidence> for OsEvidenceRecord {
fn from(evidence: &OsEvidence) -> Self {
Self {
source: wire::os_source_name(evidence.source).to_owned(),
family: evidence.family.clone(),
device: evidence.device.clone(),
vendor: evidence.vendor.clone(),
product: evidence.product.clone(),
version: evidence.version.clone(),
kernel: evidence.kernel.clone(),
arch: evidence.arch.clone(),
cpe: evidence.cpe.clone(),
confidence: evidence.confidence,
evidence: evidence.evidence.clone(),
}
}
}
impl From<&OsEvidenceRecord> for OsEvidence {
fn from(record: &OsEvidenceRecord) -> Self {
OsEvidence {
source: wire::os_source(&record.source).unwrap_or(OsSource::Hostname),
family: record.family.clone(),
device: record.device.clone(),
vendor: record.vendor.clone(),
product: record.product.clone(),
version: record.version.clone(),
kernel: record.kernel.clone(),
arch: record.arch.clone(),
cpe: record.cpe.clone(),
confidence: record.confidence,
evidence: record.evidence.clone(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct HardwareRecord {
pub macs: Vec<(String, SystemTime)>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub vendor: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub product: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub family: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub cpe23: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub model: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub version: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub serial_number: Option<String>,
}
impl From<&HardwareInfo> for HardwareRecord {
fn from(hardware: &HardwareInfo) -> Self {
let mut macs: Vec<(String, SystemTime)> = hardware
.macs()
.iter()
.map(|(mac, at)| (mac.to_string(), *at))
.collect();
if let Some(vendor) = hardware.vendor() {
let source = macs.iter().position(|(mac, _)| {
mac.parse::<MacAddr>()
.ok()
.and_then(|mac| crate::model::mac::vendor(&mac))
.is_some_and(|found| found == vendor)
});
if let Some(source) = source {
macs.swap(0, source);
}
}
let derivable = hardware.vendor().is_some_and(|vendor| {
macs.iter().any(|(mac, _)| {
mac.parse::<MacAddr>()
.ok()
.and_then(|mac| crate::model::mac::vendor(&mac))
.is_some_and(|found| found == vendor)
})
});
Self {
macs,
vendor: (!derivable)
.then(|| hardware.vendor().map(str::to_string))
.flatten(),
product: hardware.product().map(str::to_string),
family: hardware.family().map(str::to_string),
cpe23: hardware.cpe23().map(str::to_string),
model: hardware.model().map(str::to_string),
version: hardware.hardware_version().map(str::to_string),
serial_number: hardware.serial_number().map(str::to_string),
}
}
}
impl HardwareRecord {
pub fn rebuild(&self) -> Option<HardwareInfo> {
let mut macs = self
.macs
.iter()
.filter_map(|(mac, at)| mac.parse::<MacAddr>().ok().map(|mac| (mac, *at)));
let described = HardwareInfo::described(crate::model::host::HardwareDescription {
vendor: self.vendor.as_deref(),
product: self.product.as_deref(),
family: self.family.as_deref(),
cpe23: self.cpe23.as_deref(),
model: self.model.as_deref(),
version: self.version.as_deref(),
serial_number: self.serial_number.as_deref(),
});
let Some((first, first_at)) = macs.next() else {
return described;
};
let mut hardware = HardwareInfo::new(first);
hardware.record_mac_seen_at(first, first_at);
for (mac, at) in macs {
hardware.record_mac_seen_at(mac, at);
}
if let Some(described) = described {
hardware.merge(described);
}
Some(hardware)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ZoneRecord {
#[serde(default)]
pub index: Option<u32>,
pub name: String,
}
impl From<&Zone> for ZoneRecord {
fn from(zone: &Zone) -> Self {
Self {
index: zone.index(),
name: zone.name().to_owned(),
}
}
}
impl From<&ZoneRecord> for Zone {
fn from(record: &ZoneRecord) -> Self {
match record.index {
Some(index) => Zone::new(index, record.name.clone()),
None => Zone::unresolved(record.name.clone()),
}
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct TelemetryRecord {
#[serde(default)]
pub rtts: Vec<Duration>,
#[serde(default)]
pub hop_counter: Option<u8>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub rtt_protocol: Option<String>,
}
impl From<&HostTelemetry> for TelemetryRecord {
fn from(telemetry: &HostTelemetry) -> Self {
Self {
rtts: telemetry
.history()
.iter()
.map(|sample| sample.rtt)
.collect(),
hop_counter: telemetry.hop_counter(),
rtt_protocol: telemetry
.rtt_protocol()
.map(|protocol| wire::status_protocol_name(&protocol).into_owned()),
}
}
}
impl TelemetryRecord {
fn restore(&self, host: &mut Host) {
match self.rtt_protocol.as_deref().and_then(wire::status_protocol) {
Some(protocol) => {
for rtt in &self.rtts {
host.add_rtt_from(*rtt, protocol.clone());
}
}
None => host.add_rtts(self.rtts.iter().copied()),
}
if let Some(arrived) = self.hop_counter {
host.record_hop_counter(arrived);
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct HopRecord {
pub distance: u8,
#[serde(default)]
pub address: Option<IpAddr>,
#[serde(default)]
pub rtt: Option<Duration>,
#[serde(default)]
pub inferred: bool,
#[serde(default)]
pub withheld: bool,
}
impl From<&Hop> for HopRecord {
fn from(hop: &Hop) -> Self {
Self {
distance: hop.distance(),
address: hop.address(),
rtt: hop.rtt(),
inferred: hop.inferred(),
withheld: hop.is_withheld(),
}
}
}
impl From<&HopRecord> for Hop {
fn from(record: &HopRecord) -> Self {
let hop = match record.address {
_ if record.withheld => Hop::withheld(record.distance),
Some(address) => Hop::answered(record.distance, address, record.rtt),
None => Hop::silent(record.distance),
};
if record.inferred {
hop.as_inferred()
} else {
hop
}
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct PortRecord {
pub port: u16,
pub protocol: String,
pub state: String,
#[serde(default)]
pub service: Option<ServiceRecord>,
#[serde(default)]
pub security: Option<SecurityRecord>,
#[serde(default)]
pub discovery: Option<DiscoveryRecord>,
#[serde(default)]
pub findings: Vec<FindingRecord>,
}
impl From<&Port> for PortRecord {
fn from(port: &Port) -> Self {
Self {
port: port.number(),
protocol: wire::protocol_name(port.protocol()).to_owned(),
state: wire::port_state_name(port.state()).to_owned(),
service: port.service().map(ServiceRecord::from),
security: port.security().map(SecurityRecord::from),
discovery: port.discovery().map(DiscoveryRecord::from),
findings: port.findings().map(FindingRecord::from).collect(),
}
}
}
impl PortRecord {
pub fn rebuild(&self) -> Option<Port> {
let protocol = wire::protocol(&self.protocol)?;
let state = wire::port_state(&self.state).unwrap_or(PortState::Unasked);
let mut port = Port::new(self.port, protocol, state);
if let Some(service) = &self.service {
port = port.with_service(service.into());
}
if let Some(security) = &self.security {
port = port.with_security(security.into());
}
if let Some(discovery) = &self.discovery {
port = port.with_discovery(discovery.into());
}
for finding in self.findings.iter().filter_map(FindingRecord::rebuild) {
port.add_finding(finding);
}
Some(port)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct IpProtocolRecord {
pub protocol: u8,
pub state: String,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct FindingRecord {
pub detection: DetectionIdRecord,
pub title: String,
pub severity: String,
pub confidence: String,
pub class: String,
#[serde(default)]
pub excerpt: Option<String>,
#[serde(default)]
pub references: Vec<ReferenceRecord>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub remediation: Option<String>,
}
impl From<&Finding> for FindingRecord {
fn from(finding: &Finding) -> Self {
Self {
detection: DetectionIdRecord::from(finding.detection()),
title: finding.title().to_owned(),
severity: wire::severity_name(finding.severity()).to_owned(),
confidence: wire::confidence_name(finding.confidence()).to_owned(),
class: wire::detection_class_name(finding.class()).to_owned(),
excerpt: (!finding.excerpt().is_empty()).then(|| finding.excerpt().as_str().to_owned()),
references: finding.references().map(ReferenceRecord::from).collect(),
remediation: finding.remediation().map(str::to_owned),
}
}
}
impl FindingRecord {
pub fn rebuild(&self) -> Option<Finding> {
let severity = wire::severity(&self.severity).unwrap_or(Severity::Info);
let confidence = wire::confidence(&self.confidence).unwrap_or(Confidence::Heuristic);
let class = wire::detection_class(&self.class).unwrap_or(DetectionClass::Passive);
let mut finding = Finding::new(
self.detection.rebuild()?,
self.title.clone(),
severity,
confidence,
class,
)
.ok()?;
if let Some(excerpt) = &self.excerpt {
finding = finding.with_excerpt(Excerpt::new(excerpt.clone()));
}
if let Some(remediation) = &self.remediation {
finding = finding.with_remediation(remediation.clone());
}
for reference in self.references.iter().filter_map(ReferenceRecord::rebuild) {
finding = finding.with_reference(reference);
}
Some(finding)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct DetectionIdRecord {
pub id: String,
pub version: String,
pub content_hash: String,
}
impl From<&DetectionId> for DetectionIdRecord {
fn from(detection: &DetectionId) -> Self {
Self {
id: detection.id().to_owned(),
version: detection.version().to_string(),
content_hash: detection.content_hash().to_owned(),
}
}
}
impl DetectionIdRecord {
pub fn rebuild(&self) -> Option<DetectionId> {
let version = self.version.parse().unwrap_or(Version::new(0, 0, 0));
DetectionId::new(self.id.clone(), version, self.content_hash.clone()).ok()
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ReferenceRecord {
pub kind: String,
pub value: String,
}
impl From<&Reference> for ReferenceRecord {
fn from(reference: &Reference) -> Self {
Self {
kind: wire::reference_kind_name(reference).to_owned(),
value: match reference {
Reference::Cve(id) | Reference::Url(id) => id.clone(),
Reference::Cwe(number) => number.to_string(),
},
}
}
}
impl ReferenceRecord {
pub fn rebuild(&self) -> Option<Reference> {
wire::reference(&self.kind, &self.value)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ServiceRecord {
pub name: String,
pub confidence: u8,
#[serde(default)]
pub product: Option<String>,
#[serde(default)]
pub vendor: Option<String>,
#[serde(default)]
pub version: Option<String>,
#[serde(default)]
pub extrainfo: Option<String>,
#[serde(default)]
pub cpes: Vec<String>,
}
impl From<&Service> for ServiceRecord {
fn from(service: &Service) -> Self {
Self {
name: service.name().to_owned(),
confidence: service.confidence(),
product: service.product().map(str::to_owned),
vendor: service.vendor().map(str::to_owned),
version: service.version().map(str::to_owned),
extrainfo: service.extrainfo().map(str::to_owned),
cpes: service.cpes().iter().map(|cpe| cpe.to_string()).collect(),
}
}
}
impl From<&ServiceRecord> for Service {
fn from(record: &ServiceRecord) -> Self {
let mut service = Service::new(record.name.clone(), record.confidence);
if let Some(product) = &record.product {
service = service.with_product(product.clone());
}
if let Some(vendor) = &record.vendor {
service = service.with_vendor(vendor.clone());
}
if let Some(version) = &record.version {
service = service.with_version(version.clone());
}
if let Some(extrainfo) = &record.extrainfo {
service = service.with_extrainfo(extrainfo.clone());
}
for cpe in &record.cpes {
service.add_cpe(cpe.clone());
}
service
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SecurityRecord {
#[serde(default)]
pub tls_version: Option<String>,
#[serde(default)]
pub cipher_suite: Option<String>,
#[serde(default)]
pub alpn: Vec<String>,
#[serde(default)]
pub certificate: Option<CertificateRecord>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub accepts: Vec<AcceptedVersionRecord>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub unfinished: Vec<UnfinishedVersionRecord>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct UnfinishedVersionRecord {
pub version: String,
pub interruption: String,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct AcceptedVersionRecord {
pub version: String,
#[serde(default)]
pub suites: Vec<u16>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub unrecognised: Vec<u16>,
}
impl From<&Security> for SecurityRecord {
fn from(security: &Security) -> Self {
Self {
tls_version: security.tls_version().map(str::to_owned),
cipher_suite: security.cipher_suite().map(str::to_owned),
alpn: security.alpn().iter().map(|p| p.to_string()).collect(),
certificate: security.certificate().map(CertificateRecord::from),
accepts: security
.support()
.versions()
.iter()
.map(|held| AcceptedVersionRecord {
version: held.version().name().to_owned(),
suites: held.suites().iter().map(|suite| suite.code()).collect(),
unrecognised: held.unrecognised().to_vec(),
})
.collect(),
unfinished: security
.support()
.unfinished()
.iter()
.map(|held| UnfinishedVersionRecord {
version: held.version().name().to_owned(),
interruption: held.interruption().name().to_owned(),
})
.collect(),
}
}
}
impl From<&SecurityRecord> for Security {
fn from(record: &SecurityRecord) -> Self {
let mut security = Security::new();
if let Some(version) = &record.tls_version {
security = security.with_tls_version(version.clone());
}
if let Some(cipher) = &record.cipher_suite {
security = security.with_cipher_suite(cipher.clone());
}
for protocol in &record.alpn {
security.add_alpn(protocol.clone());
}
if let Some(certificate) = &record.certificate {
security = security.with_certificate(certificate.into());
}
let mut support = TlsSupport::new();
for held in &record.accepts {
let Ok(version) = held.version.parse::<TlsVersion>() else {
continue;
};
let mut suites = Vec::with_capacity(held.suites.len());
let mut unrecognised = held.unrecognised.clone();
for code in &held.suites {
match CipherSuite::from_code(*code) {
Some(suite) => suites.push(suite),
None => unrecognised.push(*code),
}
}
support.record(VersionSupport::new(version, suites, unrecognised));
}
for held in &record.unfinished {
let Ok(version) = held.version.parse::<TlsVersion>() else {
continue;
};
let interruption =
Interruption::from_name(&held.interruption).unwrap_or(Interruption::Stopped);
support.record_unfinished(UnfinishedVersion::new(version, interruption));
}
security.set_support(support);
security
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct CertificateRecord {
pub common_name: String,
#[serde(default)]
pub sans: Vec<String>,
pub issuer: String,
pub validity_start: SystemTime,
pub validity_end: SystemTime,
pub fingerprint_sha256: String,
#[serde(default)]
pub pubkey_type: String,
#[serde(default)]
pub pubkey_bits: u32,
}
impl From<&CertificateInfo> for CertificateRecord {
fn from(certificate: &CertificateInfo) -> Self {
Self {
common_name: certificate.common_name().to_owned(),
sans: certificate.sans().iter().map(|s| s.to_string()).collect(),
issuer: certificate.issuer().to_owned(),
validity_start: certificate.validity_start(),
validity_end: certificate.validity_end(),
fingerprint_sha256: certificate.fingerprint_sha256().to_owned(),
pubkey_type: certificate.pubkey_type().to_owned(),
pubkey_bits: certificate.pubkey_bits(),
}
}
}
impl From<&CertificateRecord> for CertificateInfo {
fn from(record: &CertificateRecord) -> Self {
CertificateInfo::new(
record.common_name.clone(),
record.issuer.clone(),
record.validity_start,
record.validity_end,
record.fingerprint_sha256.clone(),
)
.with_sans(record.sans.iter().map(|s| s.as_str().into()))
.with_public_key(record.pubkey_type.clone(), record.pubkey_bits)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct DiscoveryRecord {
pub reason: String,
pub timestamp: SystemTime,
#[serde(default)]
pub rtt: Option<Duration>,
#[serde(default)]
pub ttl: Option<u8>,
#[serde(default)]
pub source_ip: Option<IpAddr>,
}
impl From<&Discovery> for DiscoveryRecord {
fn from(discovery: &Discovery) -> Self {
Self {
reason: wire::scan_response_name(discovery.reason()).into_owned(),
timestamp: discovery.timestamp(),
rtt: discovery.rtt(),
ttl: discovery.ttl(),
source_ip: discovery.source_ip(),
}
}
}
impl From<&DiscoveryRecord> for Discovery {
fn from(record: &DiscoveryRecord) -> Self {
let reason = wire::scan_response(&record.reason)
.unwrap_or_else(|| ScanResponse::Custom(record.reason.clone()));
let mut discovery = Discovery::new(reason).seen_at(record.timestamp);
if let Some(rtt) = record.rtt {
discovery = discovery.with_rtt(rtt);
}
if let Some(ttl) = record.ttl {
discovery = discovery.with_ttl(ttl);
}
if let Some(source) = record.source_ip {
discovery = discovery.with_source_ip(source);
}
discovery
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct PhaseRecord {
pub kind: String,
pub started_at: SystemTime,
pub elapsed: Duration,
pub privileged: Option<bool>,
pub targets: ScopeRecord,
pub settings: SettingsRecord,
#[serde(default)]
pub failures: Vec<FailureRecord>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub refusals: Vec<RefusalRecord>,
#[serde(default)]
pub unroutable: Vec<IpAddr>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub timed_out: Vec<IpAddr>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub reached_by_connect: Vec<RangeRecord>,
#[serde(default)]
pub probe_stats: Vec<ProbeStatsRecord>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub origin: Option<PhaseOriginRecord>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub attachments: Vec<AttachmentRecord>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct AttachmentRecord {
pub link: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub link_index: Option<u32>,
pub source: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub device_mac: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub device_name: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub port: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub native_vlan: Option<u16>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub management_address: Option<IpAddr>,
pub observed_at: SystemTime,
}
impl From<&Attachment> for AttachmentRecord {
fn from(attachment: &Attachment) -> Self {
Self {
link: attachment.link().name().to_owned(),
link_index: attachment.link().index(),
source: wire::attachment_source_name(attachment.source()).to_owned(),
device_mac: attachment.device_mac().map(|mac| mac.to_string()),
device_name: attachment.device_name().map(str::to_owned),
port: attachment.port().map(str::to_owned),
native_vlan: attachment.native_vlan(),
management_address: attachment.management_address(),
observed_at: attachment.observed_at(),
}
}
}
impl From<&AttachmentRecord> for Attachment {
fn from(record: &AttachmentRecord) -> Self {
let link = match record.link_index {
Some(index) => Zone::new(index, record.link.as_str()),
None => Zone::unresolved(record.link.as_str()),
};
let source = wire::attachment_source(&record.source).unwrap_or(AttachmentSource::Lldp);
let mut attachment = Attachment::new(link, source, record.observed_at);
if let Some(mac) = record
.device_mac
.as_deref()
.and_then(|mac| mac.parse().ok())
{
attachment = attachment.with_device_mac(mac);
}
if let Some(name) = record.device_name.as_deref() {
attachment = attachment.with_device_name(name);
}
if let Some(port) = record.port.as_deref() {
attachment = attachment.with_port(port);
}
if let Some(vlan) = record.native_vlan {
attachment = attachment.with_native_vlan(vlan);
}
if let Some(address) = record.management_address {
attachment = attachment.with_management_address(address);
}
attachment
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct PhaseOriginRecord {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub label: Option<String>,
pub engine_version: String,
}
impl From<&PhaseOrigin> for PhaseOriginRecord {
fn from(origin: &PhaseOrigin) -> Self {
Self {
label: origin.label().map(str::to_owned),
engine_version: origin.engine_version().to_owned(),
}
}
}
impl From<&PhaseOriginRecord> for PhaseOrigin {
fn from(record: &PhaseOriginRecord) -> Self {
let origin = PhaseOrigin::new(record.engine_version.as_str());
match &record.label {
Some(label) => origin.with_label(label.as_str()),
None => origin,
}
}
}
impl From<&ScanPhase> for PhaseRecord {
fn from(phase: &ScanPhase) -> Self {
Self {
kind: wire::scan_kind_name(phase.kind()).to_owned(),
started_at: phase.started_at(),
elapsed: phase.elapsed(),
privileged: phase.privilege().map(Privilege::is_raw),
targets: ScopeRecord::from(phase.targets()),
settings: SettingsRecord::from(phase.settings()),
failures: phase.failures().iter().map(FailureRecord::from).collect(),
refusals: phase.refusals().iter().map(RefusalRecord::from).collect(),
unroutable: phase.unroutable().to_vec(),
timed_out: phase.timed_out().to_vec(),
reached_by_connect: phase
.reached_by_connect()
.iter()
.map(RangeRecord::from)
.collect(),
probe_stats: phase
.probe_stats()
.iter()
.map(ProbeStatsRecord::from)
.collect(),
origin: phase.origin().map(PhaseOriginRecord::from),
attachments: phase
.attachments()
.iter()
.map(AttachmentRecord::from)
.collect(),
}
}
}
impl From<&PhaseRecord> for ScanPhase {
fn from(record: &PhaseRecord) -> Self {
ScanPhase::from_parts(PhaseParts {
kind: wire::scan_kind(&record.kind).unwrap_or(ScanKind::Discovery),
started_at: record.started_at,
elapsed: record.elapsed,
privilege: record.privileged.map(Privilege::from_raw),
targets: TargetScope::from(&record.targets),
settings: ScanSettings::from(&record.settings),
failures: record.failures.iter().map(ScannerFailure::from).collect(),
refusals: record.refusals.iter().map(Refusal::from).collect(),
unroutable: record.unroutable.clone(),
timed_out: record.timed_out.clone(),
reached_by_connect: record
.reached_by_connect
.iter()
.filter_map(RangeRecord::rebuild)
.collect(),
probes: record.probe_stats.iter().map(ProbeStats::from).collect(),
origin: record.origin.as_ref().map(PhaseOrigin::from),
attachments: record.attachments.iter().map(Attachment::from).collect(),
})
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ScopeRecord {
#[serde(default)]
pub ranges: Vec<RangeRecord>,
#[serde(default)]
pub links: Vec<ZoneRecord>,
#[serde(default)]
pub listened: Vec<ZoneRecord>,
pub addresses: u128,
#[serde(default)]
pub probes: Option<u128>,
#[serde(default)]
pub ports: Option<PortsRecord>,
#[serde(default)]
pub protocols: Vec<String>,
#[serde(default)]
pub excluded: Vec<RangeRecord>,
pub withheld: u128,
}
impl From<&TargetScope> for ScopeRecord {
fn from(scope: &TargetScope) -> Self {
Self {
ranges: scope.ranges().iter().map(RangeRecord::from).collect(),
links: scope.links().iter().map(ZoneRecord::from).collect(),
listened: scope.listened().iter().map(ZoneRecord::from).collect(),
addresses: scope.addresses(),
probes: scope.probes(),
ports: PortsRecord::of(scope.ports()),
protocols: scope
.protocols()
.iter()
.map(|p| wire::protocol_name(*p).to_owned())
.collect(),
excluded: scope.excluded().iter().map(RangeRecord::from).collect(),
withheld: scope.withheld(),
}
}
}
impl From<&ScopeRecord> for TargetScope {
fn from(record: &ScopeRecord) -> Self {
TargetScope::from_parts(ScopeParts {
ranges: record
.ranges
.iter()
.filter_map(RangeRecord::rebuild)
.collect(),
links: record.links.iter().map(Zone::from).collect(),
listened: record.listened.iter().map(Zone::from).collect(),
addresses: record.addresses,
probes: record.probes,
ports: record
.ports
.as_ref()
.map_or(PortScope::Unstated, PortsRecord::rebuild),
protocols: record
.protocols
.iter()
.filter_map(|p| wire::protocol(p))
.collect(),
excluded: record
.excluded
.iter()
.filter_map(RangeRecord::rebuild)
.collect(),
withheld: record.withheld,
})
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct PortsRecord {
pub kind: String,
#[serde(default)]
pub spec: String,
}
impl PortsRecord {
pub fn of(scope: &PortScope) -> Option<Self> {
match scope {
PortScope::Unstated => None,
other => Some(Self {
kind: wire::port_scope_name(other).to_owned(),
spec: other.ports().map(PortSet::to_string).unwrap_or_default(),
}),
}
}
pub fn rebuild(&self) -> PortScope {
let ports = PortSet::try_from(self.spec.as_str())
.ok()
.filter(|ports| !ports.is_empty());
wire::port_scope(&self.kind, ports).unwrap_or(PortScope::Unstated)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct RangeRecord {
pub start: IpAddr,
pub end: IpAddr,
#[serde(default)]
pub zone: Option<u32>,
}
impl From<&IpRange> for RangeRecord {
fn from(range: &IpRange) -> Self {
Self {
start: range.start_addr(),
end: range.end_addr(),
zone: match range {
IpRange::V6(range) => range.zone(),
IpRange::V4(_) => None,
},
}
}
}
impl RangeRecord {
pub fn rebuild(&self) -> Option<IpRange> {
match (self.start, self.end) {
(IpAddr::V4(start), IpAddr::V4(end)) => {
Ipv4Range::new(start, end).ok().map(IpRange::V4)
}
(IpAddr::V6(start), IpAddr::V6(end)) => Ipv6Range::scoped(start, end, self.zone)
.ok()
.map(IpRange::V6),
_ => None,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct RefusalRecord {
pub scanner: String,
pub reason: String,
}
impl From<&Refusal> for RefusalRecord {
fn from(refusal: &Refusal) -> Self {
Self {
scanner: wire::scanner_kind_name(refusal.scanner()).to_owned(),
reason: refusal.reason().to_owned(),
}
}
}
impl From<&RefusalRecord> for Refusal {
fn from(record: &RefusalRecord) -> Self {
Refusal::new(
wire::scanner_kind(&record.scanner).unwrap_or(ScannerKind::Composite),
record.reason.clone(),
)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct FailureRecord {
pub scanner: String,
pub reason: String,
pub at: SystemTime,
}
impl From<&ScannerFailure> for FailureRecord {
fn from(failure: &ScannerFailure) -> Self {
Self {
scanner: wire::scanner_kind_name(failure.scanner()).to_owned(),
reason: failure.reason().to_owned(),
at: failure.at(),
}
}
}
impl From<&FailureRecord> for ScannerFailure {
fn from(record: &FailureRecord) -> Self {
ScannerFailure::new(
wire::scanner_kind(&record.scanner).unwrap_or(ScannerKind::Composite),
record.reason.clone(),
)
.recorded_at(record.at)
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct SettingsRecord {
pub send_mode: String,
pub tcp_technique: String,
#[serde(default)]
pub sctp_technique: String,
pub retry_effort: String,
#[serde(default)]
pub retry_max_attempts: Option<u8>,
#[serde(default)]
pub retry_timeout_scale: Option<f64>,
pub retry_dampen_silent_hosts: bool,
#[serde(default)]
pub max_probe_rate: Option<u32>,
#[serde(default)]
pub min_probe_rate: Option<u32>,
#[serde(default)]
pub host_probe_interval: Option<Duration>,
#[serde(default)]
pub host_timeout: Option<Duration>,
#[serde(default)]
pub scan_timeout: Option<Duration>,
pub dns_enabled: bool,
pub redact: bool,
pub os_detection: String,
pub service_detection: String,
#[serde(default)]
pub detection: String,
pub traceroute: bool,
#[serde(default)]
pub characterise: bool,
#[serde(default)]
pub ip_protocols: Vec<u8>,
#[serde(default)]
pub tls_enumeration: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub evasion: Option<EvasionSettingsRecord>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub idle_scan: Option<IdleScanRecord>,
#[serde(default)]
pub icmp_evidence: bool,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct IdleScanRecord {
pub zombie: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub zombie_port: Option<u16>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct EvasionSettingsRecord {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub source_port: Option<u16>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub ttl: Option<u8>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub padding: Option<u16>,
#[serde(default, skip_serializing_if = "is_false")]
pub bad_tcp_checksum: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub spoof_mac: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub fragment: Option<u16>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub decoys: Vec<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub flags: Option<String>,
}
fn is_false(value: &bool) -> bool {
!*value
}
impl From<&ScanSettings> for SettingsRecord {
fn from(settings: &ScanSettings) -> Self {
Self {
send_mode: settings.send_mode.name().to_owned(),
tcp_technique: settings.tcp_technique.name().to_owned(),
sctp_technique: settings.sctp_technique.name().to_owned(),
retry_effort: settings.retry.effort.name().to_owned(),
retry_max_attempts: settings.retry.max_attempts.map(NonZeroU8::get),
retry_timeout_scale: settings.retry.timeout_scale.map(TimeoutScale::get),
retry_dampen_silent_hosts: settings.retry.dampen_silent_hosts,
max_probe_rate: settings.max_probe_rate.map(NonZeroU32::get),
min_probe_rate: settings.min_probe_rate.map(NonZeroU32::get),
host_probe_interval: settings.host_probe_interval,
host_timeout: settings.host_timeout,
scan_timeout: settings.scan_timeout,
dns_enabled: settings.dns_enabled,
redact: settings.redact,
os_detection: settings.os_detection.name().to_owned(),
service_detection: settings.service_detection.name().to_owned(),
detection: wire::detection_ceiling_name(settings.detection.ceiling()).to_owned(),
traceroute: settings.traceroute,
characterise: settings.characterise,
ip_protocols: settings.ip_protocols.clone(),
tls_enumeration: settings.tls_enumeration,
evasion: settings.evasion.as_ref().map(|e| EvasionSettingsRecord {
source_port: e.source_port,
ttl: e.ttl,
padding: e.padding,
bad_tcp_checksum: e.bad_tcp_checksum,
spoof_mac: e.spoof_mac.map(|mac| mac.to_string()),
fragment: e.fragment,
decoys: e.decoys.iter().map(|ip| ip.to_string()).collect(),
flags: e.flags.map(wire::tcp_flags_name),
}),
idle_scan: settings.idle_scan.map(|i| IdleScanRecord {
zombie: i.zombie.to_string(),
zombie_port: i.zombie_port,
}),
icmp_evidence: settings.icmp_evidence,
}
}
}
impl From<&SettingsRecord> for ScanSettings {
fn from(record: &SettingsRecord) -> Self {
Self {
send_mode: record.send_mode.parse().unwrap_or_default(),
tcp_technique: record.tcp_technique.parse().unwrap_or_default(),
sctp_technique: record.sctp_technique.parse().unwrap_or_default(),
retry: RetryConfig {
effort: record.retry_effort.parse().unwrap_or_default(),
max_attempts: record.retry_max_attempts.and_then(NonZeroU8::new),
timeout_scale: record.retry_timeout_scale.and_then(TimeoutScale::new),
dampen_silent_hosts: record.retry_dampen_silent_hosts,
},
max_probe_rate: record.max_probe_rate.and_then(NonZeroU32::new),
min_probe_rate: record.min_probe_rate.and_then(NonZeroU32::new),
host_probe_interval: record.host_probe_interval,
host_timeout: record.host_timeout,
scan_timeout: record.scan_timeout,
dns_enabled: record.dns_enabled,
redact: record.redact,
os_detection: record.os_detection.parse().unwrap_or_default(),
service_detection: record.service_detection.parse().unwrap_or_default(),
detection: match record.detection.as_str() {
"off" => DetectionEnvelope::none(),
name => wire::detection_class(name)
.map(DetectionEnvelope::up_to)
.unwrap_or_default(),
},
traceroute: record.traceroute,
characterise: record.characterise,
ip_protocols: record.ip_protocols.clone(),
tls_enumeration: record.tls_enumeration,
evasion: record.evasion.as_ref().map(|e| EvasionRecord {
source_port: e.source_port,
ttl: e.ttl,
padding: e.padding,
bad_tcp_checksum: e.bad_tcp_checksum,
spoof_mac: e.spoof_mac.as_ref().and_then(|s| s.parse().ok()),
fragment: e.fragment,
decoys: e.decoys.iter().filter_map(|s| s.parse().ok()).collect(),
flags: e.flags.as_deref().map(wire::tcp_flags),
}),
idle_scan: record.idle_scan.as_ref().and_then(|i| {
i.zombie.parse().ok().map(|zombie| IdleScan {
zombie,
zombie_port: i.zombie_port,
})
}),
icmp_evidence: record.icmp_evidence,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ProbeStatsRecord {
pub scanner: String,
pub targets: u128,
pub stop_reason: String,
pub elapsed: Duration,
pub sends_attempted: u64,
pub sends_failed: u64,
#[serde(default)]
pub sends_witnessed: u64,
pub segments_seen: u64,
#[serde(default)]
pub window: Option<WindowRecord>,
pub segments_off_target: u64,
pub replies_without_rtt: u64,
pub hosts_found: u64,
#[serde(default)]
pub answered_on: Vec<u64>,
pub answered_unattributed: u64,
#[serde(default)]
pub first_reply: Option<Duration>,
#[serde(default)]
pub last_reply: Option<Duration>,
#[serde(default)]
pub found_at: Vec<u64>,
#[serde(default)]
pub capture: Option<CaptureRecord>,
}
impl From<&ProbeStats> for ProbeStatsRecord {
fn from(stats: &ProbeStats) -> Self {
Self {
scanner: wire::scanner_kind_name(stats.scanner()).to_owned(),
targets: stats.targets(),
stop_reason: wire::stop_reason_name(stats.stop_reason()).to_owned(),
elapsed: stats.elapsed(),
sends_attempted: stats.sends_attempted(),
sends_failed: stats.sends_failed(),
sends_witnessed: stats.sends_witnessed(),
segments_seen: stats.segments_seen(),
window: stats.window().map(WindowRecord::from),
segments_off_target: stats.segments_off_target(),
replies_without_rtt: stats.replies_without_rtt(),
hosts_found: stats.hosts_found(),
answered_on: stats.answered_on().to_vec(),
answered_unattributed: stats.answered_unattributed(),
first_reply: stats.first_reply(),
last_reply: stats.last_reply(),
found_at: stats.found_at().to_vec(),
capture: stats.capture().map(CaptureRecord::from),
}
}
}
impl From<&ProbeStatsRecord> for ProbeStats {
fn from(record: &ProbeStatsRecord) -> Self {
fn fill<const N: usize>(from: &[u64]) -> [u64; N] {
let mut into = [0u64; N];
for (slot, value) in into.iter_mut().zip(from) {
*slot = *value;
}
into
}
ProbeStats::from_parts(ProbeStatsParts {
scanner: wire::scanner_kind(&record.scanner).unwrap_or(ScannerKind::Composite),
targets: record.targets,
stop_reason: wire::stop_reason(&record.stop_reason)
.unwrap_or(StopReason::DeadlineExpired),
elapsed: record.elapsed,
sends_attempted: record.sends_attempted,
sends_failed: record.sends_failed,
sends_witnessed: record.sends_witnessed,
segments_seen: record.segments_seen,
window: record.window.as_ref().map(WindowSummary::from),
segments_off_target: record.segments_off_target,
replies_without_rtt: record.replies_without_rtt,
hosts_found: record.hosts_found,
answered_on: fill(&record.answered_on),
answered_unattributed: record.answered_unattributed,
first_reply: record.first_reply,
last_reply: record.last_reply,
found_at: fill(&record.found_at),
capture: record.capture.as_ref().map(CaptureCounts::from),
})
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct WindowRecord {
pub capacity: usize,
pub peak: usize,
pub reductions: u32,
pub adaptive: bool,
pub at_floor: bool,
}
impl From<WindowSummary> for WindowRecord {
fn from(window: WindowSummary) -> Self {
Self {
capacity: window.capacity,
peak: window.peak,
reductions: window.reductions,
adaptive: window.adaptive,
at_floor: window.at_floor,
}
}
}
impl From<&WindowRecord> for WindowSummary {
fn from(record: &WindowRecord) -> Self {
Self {
capacity: record.capacity,
peak: record.peak,
reductions: record.reductions,
adaptive: record.adaptive,
at_floor: record.at_floor,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct CaptureRecord {
pub received: u64,
pub dropped: u64,
pub if_dropped: u64,
#[serde(default)]
pub stopped_early: u64,
}
impl From<CaptureCounts> for CaptureRecord {
fn from(counts: CaptureCounts) -> Self {
Self {
received: counts.received,
dropped: counts.dropped,
if_dropped: counts.if_dropped,
stopped_early: counts.stopped_early,
}
}
}
impl From<&CaptureRecord> for CaptureCounts {
fn from(record: &CaptureRecord) -> Self {
Self {
received: record.received,
dropped: record.dropped,
if_dropped: record.if_dropped,
stopped_early: record.stopped_early,
}
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct PlanRecord {
#[serde(default)]
pub units: Vec<UnitRecord>,
}
impl PlanRecord {
pub fn addresses(&self) -> IpSet {
let mut ips = IpSet::new();
for unit in &self.units {
for range in unit.ranges.iter().filter_map(RangeRecord::rebuild) {
ips.insert_range(range);
}
}
ips.canonicalize();
ips
}
}
impl From<&IpSet> for PlanRecord {
fn from(addresses: &IpSet) -> Self {
Self {
units: vec![UnitRecord {
ranges: ranges_of(addresses),
spec: String::new(),
enumerated_ports: Vec::new(),
}],
}
}
}
fn ranges_of(ips: &IpSet) -> Vec<RangeRecord> {
ips.v4()
.iter()
.map(|range| RangeRecord::from(&IpRange::V4(*range)))
.chain(
ips.v6()
.iter()
.map(|range| RangeRecord::from(&IpRange::V6(*range))),
)
.collect()
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct UnitRecord {
#[serde(default)]
pub ranges: Vec<RangeRecord>,
#[serde(default, skip_serializing_if = "String::is_empty")]
pub spec: String,
#[serde(default, rename = "ports", skip_serializing_if = "Vec::is_empty")]
pub enumerated_ports: Vec<(u16, String)>,
}
impl UnitRecord {
fn port_set(&self) -> PortSet {
if !self.spec.is_empty()
&& let Ok(ports) = PortSet::try_from(self.spec.as_str())
{
return ports;
}
self.enumerated_ports
.iter()
.filter_map(|(port, protocol)| wire::protocol(protocol).map(|p| (*port, p)))
.collect()
}
}
impl From<&TargetMap> for PlanRecord {
fn from(plan: &TargetMap) -> Self {
Self {
units: plan
.units
.iter()
.map(|unit| UnitRecord {
ranges: ranges_of(unit.ips()),
spec: unit.ports().to_string(),
enumerated_ports: Vec::new(),
})
.collect(),
}
}
}
impl From<&PlanRecord> for TargetMap {
fn from(record: &PlanRecord) -> Self {
let mut plan = TargetMap::new();
for unit in &record.units {
let mut ips = IpSet::new();
for range in unit.ranges.iter().filter_map(RangeRecord::rebuild) {
ips.insert_range(range);
}
let ports = unit.port_set();
plan.add_unit(TargetSet::new(ips, ports));
}
plan
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::model::host::{HostStatus, NetworkRole};
use crate::model::mac::MacAddr;
use crate::model::port::Protocol;
use std::net::Ipv4Addr;
use std::time::UNIX_EPOCH;
fn at(seconds: u64) -> SystemTime {
UNIX_EPOCH + Duration::from_secs(seconds)
}
fn maximal_host() -> Host {
let mut host = Host::new(IpAddr::V4(Ipv4Addr::new(192, 0, 2, 1)));
host.add_ip(IpAddr::V4(Ipv4Addr::new(192, 0, 2, 2)));
host.add_ip("2001:db8::1".parse().expect("an address"));
host.set_hostname(Some("router.example".to_string()));
host.set_status(HostStatus::Up);
let mut arp = StatusReason::new(StatusProtocol::Arp, "reply from gateway");
arp.source = EvidenceSource::Intermediary(IpAddr::V4(Ipv4Addr::new(192, 0, 2, 254)));
host.add_reason(arp);
let mut unreachable = StatusReason::basic(StatusProtocol::IcmpUnreachable);
unreachable.source = EvidenceSource::Withheld;
host.add_reason(unreachable);
host.add_reason(StatusReason::new(
StatusProtocol::Custom("a-strategy".into()),
"said so",
));
let mut os = OsFingerprint::new("Linux 5.15", 95)
.with_family("Unix-like")
.with_generation("5.15.0")
.with_vendor("Canonical")
.with_kernel("5.15.0-91-generic")
.with_detail_accuracy(70)
.with_evidence("syn-ack hops>=64 opts=M,S,T,N,W");
os.add_cpe("cpe:/o:linux:linux_kernel:5.15.0");
os.add_cpe("cpe:/o:canonical:ubuntu_linux:22.04");
host.set_os(os);
host.record_os_evidence(OsEvidence {
source: OsSource::TcpStack,
family: Some("Linux".to_string()),
device: None,
vendor: Some("Canonical".to_string()),
product: Some("Ubuntu".to_string()),
version: Some("22.04".to_string()),
kernel: Some("5.15.0".to_string()),
arch: None,
cpe: Some("cpe:/o:canonical:ubuntu_linux:22.04".to_string()),
confidence: 0.65,
evidence: "stack reading".to_string(),
});
host.record_os_evidence(OsEvidence {
source: OsSource::ServiceBanner,
family: Some("Linux".to_string()),
device: None,
vendor: None,
product: None,
version: None,
kernel: None,
arch: None,
cpe: None,
confidence: 0.4,
evidence: "ssh banner".to_string(),
});
host.record_mac(MacAddr::new(0x2c, 0xcf, 0x67, 0x00, 0x00, 0x01));
host.record_mac(MacAddr::new(0x00, 0x1a, 0x2b, 0x3c, 0x4d, 0x5e));
host.set_zone(Zone::new(7, "eth0"));
host.add_rtts([
Duration::from_micros(1_200),
Duration::from_micros(1_800),
Duration::from_micros(3_000),
]);
host.record_hop_counter(58);
host.record_hop(Hop::answered(
3,
IpAddr::V4(Ipv4Addr::new(198, 51, 100, 1)),
Some(Duration::from_micros(4_100)),
));
host.record_hop(Hop::silent(2));
host.record_hop(
Hop::answered(1, IpAddr::V4(Ipv4Addr::new(192, 0, 2, 254)), None).as_inferred(),
);
host.record_hop(Hop::withheld(4).as_inferred());
host.add_network_role(NetworkRole::Tarpit);
host.add_network_role(NetworkRole::Truncated);
host.record_ip_protocol(47, IpProtocolState::Open);
host.record_ip_protocol(89, IpProtocolState::Closed);
host.record_ip_protocol(103, IpProtocolState::Unasked);
host.add_port(maximal_port());
host.add_port(Port::new(53, Protocol::Udp, PortState::OpenFiltered));
host.add_port(Port::new(80, Protocol::Tcp, PortState::Closed));
host.add_finding(maximal_finding());
host.restore_seen(at(1_700_000_000), at(1_700_003_600));
host
}
fn maximal_port() -> Port {
let service = Service::new("http", 100)
.with_product("nginx")
.with_vendor("F5")
.with_version("1.24.0")
.with_extrainfo("Ubuntu")
.with_cpe("cpe:/a:nginx:nginx:1.24.0");
let certificate = CertificateInfo::new(
"example.com",
"R3",
at(1_690_000_000),
at(1_720_000_000),
"ab:cd:ef",
)
.with_sans(["example.com".into(), "www.example.com".into()])
.with_public_key("rsa", 2048);
let support = TlsSupport::new()
.accepting(VersionSupport::new(
TlsVersion::Tls12,
vec![CipherSuite::from_code(0xC02F).expect("a registered suite")],
vec![0xFF01],
))
.leaving_unfinished(UnfinishedVersion::new(
TlsVersion::Tls12,
Interruption::Unanswered,
));
let security = Security::new()
.with_tls_version("TLSv1.3")
.with_cipher_suite("TLS_AES_256_GCM_SHA384")
.with_alpn("h2")
.with_alpn("http/1.1")
.with_certificate(certificate)
.with_support(support);
let discovery = Discovery::new(ScanResponse::TcpSynAck)
.seen_at(at(1_700_000_500))
.with_rtt(Duration::from_micros(2_400))
.with_ttl(58)
.with_source_ip(IpAddr::V4(Ipv4Addr::new(192, 0, 2, 1)));
let mut port = Port::new(443, Protocol::Tcp, PortState::Open)
.with_service(service)
.with_security(security)
.with_discovery(discovery);
port.add_finding(maximal_finding());
port
}
#[test]
fn a_fully_populated_host_survives_a_round_trip() {
let original = maximal_host();
let rebuilt = Host::from(&HostRecord::from(&original));
let options = crate::export::ExportOptions::default();
let render = |host: &Host| {
serde_json::to_value(crate::export::schema::HostDto::new(host, &options))
.expect("a host renders")
};
assert_eq!(
render(&original),
render(&rebuilt),
"a field was lost in the round trip"
);
}
fn maximal_finding() -> Finding {
Finding::new(
DetectionId::new("grafana-path-traversal", Version::new(1, 2, 3), "deadbeef").unwrap(),
"Grafana plugin path traversal",
Severity::Critical,
Confidence::Strong,
DetectionClass::ActiveBenign,
)
.unwrap()
.with_excerpt(Excerpt::new("root:x:0:0:root:/root:/bin/bash"))
.with_reference(Reference::cve("CVE-2021-43798").unwrap())
.with_reference(Reference::cwe(22))
.with_reference(Reference::url("https://grafana.com/security/"))
.with_remediation("Upgrade to 8.3.1 or later.")
}
#[test]
fn a_fully_populated_finding_survives_a_round_trip() {
let original = maximal_finding();
let rebuilt = FindingRecord::from(&original)
.rebuild()
.expect("a finding rebuilds");
assert_eq!(original, rebuilt, "a field was lost in the round trip");
}
#[test]
fn a_finding_record_reads_unknown_names_downward_and_a_nameless_one_away() {
let softened = FindingRecord {
detection: DetectionIdRecord {
id: "det".into(),
version: "not-a-version".into(), content_hash: "h".into(),
},
title: "A finding".into(),
severity: "catastrophic".into(), confidence: "absolute".into(), class: "nosy".into(), excerpt: None,
references: vec![ReferenceRecord {
kind: "mystery".into(),
value: "x".into(),
}],
remediation: None,
};
let finding = softened
.rebuild()
.expect("an unparseable-but-named finding still rebuilds");
assert_eq!(finding.severity(), Severity::Info);
assert_eq!(finding.confidence(), Confidence::Heuristic);
assert_eq!(finding.class(), DetectionClass::Passive);
assert_eq!(finding.detection().version(), Version::new(0, 0, 0));
assert_eq!(
finding.references().count(),
0,
"an unknown reference is dropped, the finding kept"
);
let nameless = FindingRecord {
detection: DetectionIdRecord {
id: " ".into(),
..softened.detection.clone()
},
..softened.clone()
};
assert!(nameless.rebuild().is_none());
let titleless = FindingRecord {
title: " ".into(),
..softened.clone()
};
assert!(titleless.rebuild().is_none());
}
#[test]
fn a_hosts_findings_survive_a_record_round_trip() {
let mut host = Host::new(IpAddr::V4(Ipv4Addr::new(192, 0, 2, 9)));
host.add_finding(maximal_finding());
let rebuilt = Host::from(&HostRecord::from(&host));
let original: Vec<_> = host.findings().cloned().collect();
let round: Vec<_> = rebuilt.findings().cloned().collect();
assert_eq!(original, round, "a host's findings were lost in the record");
}
#[test]
fn a_ports_findings_survive_a_record_round_trip() {
let mut port = Port::new(3000, Protocol::Tcp, PortState::Open);
port.add_finding(maximal_finding());
let rebuilt = PortRecord::from(&port)
.rebuild()
.expect("a TCP port rebuilds");
let original: Vec<_> = port.findings().cloned().collect();
let round: Vec<_> = rebuilt.findings().cloned().collect();
assert_eq!(original, round, "a port's findings were lost in the record");
}
#[test]
fn the_record_is_stable_across_a_rebuild() {
let record = HostRecord::from(&maximal_host());
let rebuilt = HostRecord::from(&Host::from(&record));
assert_eq!(record, rebuilt);
}
#[test]
fn the_record_survives_json() {
let record = HostRecord::from(&maximal_host());
let text = serde_json::to_string(&record).expect("serializes");
let read: HostRecord = serde_json::from_str(&text).expect("deserializes");
assert_eq!(record, read);
}
#[test]
fn the_times_a_host_was_seen_survive() {
let rebuilt = Host::from(&HostRecord::from(&maximal_host()));
assert_eq!(rebuilt.first_seen(), at(1_700_000_000));
assert_eq!(rebuilt.last_seen(), at(1_700_003_600));
}
#[test]
fn unknown_names_read_downwards() {
let mut record = HostRecord::from(&maximal_host());
let recorded = record.ports.len();
let unreadable = (record.ports[0].port, record.ports[0].protocol.clone());
record.status = "ascended".to_string();
record.ports[0].state = "ajar".to_string();
record.ports[0].protocol = "dccp".to_string();
let rebuilt = Host::from(&record);
assert_eq!(rebuilt.status(), HostStatus::Unknown);
assert_eq!(
rebuilt.ports().count(),
recorded - 1,
"the one port under an unreadable protocol is dropped and the rest stay"
);
assert!(
!rebuilt.ports().any(|port| port.number() == unreadable.0
&& wire::protocol_name(port.protocol()) == unreadable.1),
"and it is not filed under the protocol it was originally written as"
);
let mut record = HostRecord::from(&maximal_host());
record.ports[0].state = "ajar".to_string();
let number = record.ports[0].port;
let protocol = wire::protocol(&record.ports[0].protocol).expect("a readable protocol");
let rebuilt = Host::from(&record);
let port = rebuilt
.ports()
.find(|port| port.number() == number && port.protocol() == protocol)
.expect("the port is still recorded");
assert_eq!(
port.state(),
PortState::Unasked,
"a verdict this build cannot read is no verdict, which is what the \
bottom of the ordering says"
);
}
#[test]
fn an_unreadable_protocol_does_not_contaminate_the_port_it_would_collide_with() {
let mut host = Host::new("192.0.2.1".parse().expect("literal"));
host.set_status(HostStatus::Up);
host.add_port(Port::new(443, Protocol::Tcp, PortState::Open));
let record = HostRecord::from(&host);
let mut value: serde_json::Value = serde_json::to_value(&record).expect("serialises");
let mut future = value["ports"][0].clone();
future["protocol"] = serde_json::json!("quic");
future["state"] = serde_json::json!("closed");
future["service"] = serde_json::json!({"name": "quic-only", "confidence": 95});
value["ports"]
.as_array_mut()
.expect("an array")
.push(future);
let read: HostRecord = serde_json::from_value(value).expect("reads back");
let rebuilt = Host::from(&read);
let ports: Vec<_> = rebuilt.ports().collect();
assert_eq!(ports.len(), 1, "the readable port, and only it");
assert_eq!(ports[0].protocol(), Protocol::Tcp);
assert_eq!(ports[0].state(), PortState::Open);
assert!(
ports[0].service().is_none(),
"the TCP port must not acquire a service named on an endpoint this \
build could not read"
);
}
#[test]
fn a_port_record_under_an_unreadable_transport_rebuilds_as_no_port() {
let mut record = PortRecord::from(&Port::new(443, Protocol::Tcp, PortState::Open));
record.protocol = "quic".to_string();
assert_eq!(
record.rebuild(),
None,
"a transport this build cannot read is not TCP, or any other it can"
);
let mut record = PortRecord::from(&Port::new(443, Protocol::Tcp, PortState::Open));
record.state = "ajar".to_string();
let port = record.rebuild().expect("a readable transport rebuilds");
assert_eq!(port.protocol(), Protocol::Tcp);
assert_eq!(port.state(), PortState::Unasked);
}
#[test]
fn a_plans_ports_round_trip_as_a_specification() {
use crate::model::ip::set::IpSet;
use crate::model::target::TargetSet;
let mut plan = TargetMap::new();
plan.add_unit(TargetSet::new(
"192.0.2.0/30".parse::<IpSet>().expect("a range"),
"80,443,1000-1010,u:53".parse::<PortSet>().expect("ports"),
));
let record = PlanRecord::from(&plan);
assert!(
record.units[0].enumerated_ports.is_empty(),
"the enumerated form is read and never written"
);
assert_eq!(
TargetMap::from(&record).iter().collect::<Vec<_>>(),
plan.iter().collect::<Vec<_>>(),
"the enumeration a position is counted through has to be identical"
);
assert!(record.units[0].spec.len() < 40, "{}", record.units[0].spec);
let legacy = PlanRecord {
units: vec![UnitRecord {
ranges: record.units[0].ranges.clone(),
spec: String::new(),
enumerated_ports: plan.units[0]
.ports()
.iter()
.map(|(port, protocol)| (port, wire::protocol_name(protocol).to_owned()))
.collect(),
}],
};
assert_eq!(
TargetMap::from(&legacy).iter().collect::<Vec<_>>(),
plan.iter().collect::<Vec<_>>()
);
}
#[test]
fn a_phase_survives_a_round_trip() {
let report = crate::export::fixture::report();
for phase in report.phases() {
let rebuilt = ScanPhase::from(&PhaseRecord::from(phase));
let options = crate::export::ExportOptions::default();
let render = |phase: &ScanPhase| {
serde_json::to_value(crate::export::schema::PhaseDto::new(phase, &options))
.expect("a phase renders")
};
assert_eq!(render(phase), render(&rebuilt), "a field was lost");
}
}
#[test]
fn a_capture_that_stopped_early_survives_the_journal() {
let counts = CaptureCounts {
received: 271,
dropped: 4,
if_dropped: 1,
stopped_early: 2,
};
let record = CaptureRecord::from(counts);
assert_eq!(CaptureCounts::from(&record), counts, "a field was lost");
let json = serde_json::to_string(&record).expect("a record serializes");
assert!(json.contains("stopped_early"), "{json}");
let read: CaptureRecord = serde_json::from_str(&json).expect("and reads back");
assert_eq!(CaptureCounts::from(&read), counts);
let older = r#"{"received":271,"dropped":4,"if_dropped":1}"#;
let read: CaptureRecord = serde_json::from_str(older).expect("an older journal opens");
assert_eq!(CaptureCounts::from(&read).stopped_early, 0);
assert_eq!(CaptureCounts::from(&read).received, 271);
}
#[test]
fn privilege_crosses_the_record_boundary_as_the_boolean_the_format_promised() {
let report = crate::export::fixture::report();
let original = report.phases().first().expect("the fixture has a phase");
for (privilege, written) in [
(Some(Privilege::Raw), Some(true)),
(Some(Privilege::Connect), Some(false)),
(None, None),
] {
let mut record = PhaseRecord::from(original);
record.privileged = written;
let phase = ScanPhase::from(&record);
assert_eq!(phase.privilege(), privilege, "reading {written:?}");
assert_eq!(
PhaseRecord::from(&phase).privileged,
written,
"writing {privilege:?}"
);
}
}
#[test]
fn the_probe_behind_a_round_trip_survives_the_record() {
use crate::model::host::StatusProtocol;
let mut host = Host::new(IpAddr::V4(Ipv4Addr::new(192, 0, 2, 9)));
host.add_rtt_from(Duration::from_micros(90), StatusProtocol::Arp);
host.add_rtt_from(Duration::from_micros(110), StatusProtocol::Arp);
let record = HostRecord::from(&host);
assert_eq!(record.telemetry.rtt_protocol.as_deref(), Some("arp"));
let mut restored = Host::new(IpAddr::V4(Ipv4Addr::new(192, 0, 2, 9)));
record.telemetry.restore(&mut restored);
assert_eq!(restored.rtt_protocol(), Some(StatusProtocol::Arp));
assert_eq!(restored.min_rtt(), host.min_rtt());
}
#[test]
fn asking_for_icmp_evidence_survives_the_settings_round_trip() {
use crate::config::ZondConfig;
let mut settings = ScanSettings::from(&ZondConfig::default());
assert!(settings.icmp_evidence, "on unless declined");
settings.icmp_evidence = false;
assert!(!ScanSettings::from(&SettingsRecord::from(&settings)).icmp_evidence);
settings.icmp_evidence = true;
assert!(ScanSettings::from(&SettingsRecord::from(&settings)).icmp_evidence);
}
#[test]
fn the_detection_envelope_survives_the_settings_round_trip() {
use crate::config::DetectionEnvelope;
use crate::config::ZondConfig;
use crate::model::finding::DetectionClass;
use crate::report::ScanSettings;
let mut settings = ScanSettings::from(&ZondConfig::default());
settings.detection = DetectionEnvelope::up_to(DetectionClass::Exploit);
let rebuilt = ScanSettings::from(&SettingsRecord::from(&settings));
assert_eq!(
rebuilt.detection.ceiling(),
Some(DetectionClass::Exploit),
"the envelope ceiling was lost in the round trip"
);
}
#[test]
fn a_phase_survives_json() {
let report = crate::export::fixture::report();
for phase in report.phases() {
let record = PhaseRecord::from(phase);
let text = serde_json::to_string(&record).expect("serializes");
let read: PhaseRecord = serde_json::from_str(&text).expect("deserializes");
assert_eq!(record, read);
}
}
#[test]
fn a_hosts_measurements_survive_the_round_trip() {
use crate::model::host::{HostStatus, StatusProtocol, StatusReason};
use std::time::Duration;
let mut host = Host::new("192.0.2.1".parse().expect("an address"));
host.set_status(HostStatus::Up);
for protocol in [
StatusProtocol::Arp,
StatusProtocol::IcmpEcho,
StatusProtocol::TcpSyn,
] {
host.record_evidence(HostStatus::Up, StatusReason::new(protocol, "answered"));
}
host.add_rtts([
Duration::from_micros(5_100),
Duration::from_micros(6_060),
Duration::from_micros(6_540),
]);
let restored = Host::from(&HostRecord::from(&host));
assert_eq!(
restored.reasons().len(),
host.reasons().len(),
"a protocol that answered was lost"
);
assert_eq!(restored.min_rtt(), host.min_rtt(), "the fastest round trip");
assert_eq!(restored.max_rtt(), host.max_rtt(), "the slowest");
assert_eq!(restored.average_rtt(), host.average_rtt(), "the mean");
}
}