Skip to main content

PortScanDetector

Struct PortScanDetector 

Source
pub struct PortScanDetector<K>
where K: Hash + Eq + Clone,
{ /* private fields */ }
Expand description

Per-source scanner-likelihood detector.

State is bounded: a source is normally dropped the moment its λ crosses either threshold, but a source that stays Inconclusive — one SYN from each of a million spoofed addresses, say — would otherwise persist forever. At max_sources the least-recently-touched entry is evicted. Evicting an Inconclusive source is harmless by the detector’s own semantics: it restarts at λ = 0, exactly as it would after a verdict.

Implementations§

Source§

impl<K> PortScanDetector<K>
where K: Hash + Eq + Clone,

Source

pub fn new() -> Self

Default-tuned: θ0=0.8, θ1=0.2, α=β=0.01.

Source

pub fn with_params(theta0: f64, theta1: f64, alpha: f64, beta: f64) -> Self

Custom-tuned: caller supplies success priors + error rates. Asserts the parameters are in (0, 1) open intervals.

Source

pub fn with_capacity(self, max_sources: usize) -> Self

Cap on concurrently tracked sources (default 10 000).

Only sources still awaiting a verdict occupy a slot — one that crosses either threshold is released immediately. Size this to the number of distinct hosts mid-evaluation you expect, not to your host count.

Source

pub fn evicted(&self) -> u64

Sources dropped by the capacity bound rather than by a verdict. A climbing count means scan-like traffic from more distinct sources than the detector can hold — itself worth alerting on.

Source

pub fn observe(&mut self, key: K, success: bool) -> ScanScore<K>

Record a connection outcome.

success = true for completed 3-way handshakes (or any other “connection succeeded” signal the consumer supplies); false for SYN-only / RST / timeout.

Source

pub fn forget(&mut self, key: &K)

Drop per-key state without emitting a verdict.

Source

pub fn tracked(&self) -> usize

Number of sources currently being tracked.

Trait Implementations§

Source§

impl<K> Default for PortScanDetector<K>
where K: Hash + Eq + Clone,

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<K: KeyFields + Send> Detector<K> for PortScanDetector<SrcHost>

Available on crate feature tracker only.
Source§

fn on_flow_end( &mut self, key: &K, stats: &FlowStats, history: &HistoryString, l4: Option<L4Proto>, ts: Timestamp, out: &mut Vec<OwnedAnomaly>, )

One TRW observation per finished flow, keyed by SrcHost. Success derives statelessly from the flow record (see the module-private flow_succeeded). Emits only on a Scanner verdict — TRW resets the key’s state at every terminal verdict, which is natural re-alert dedup. No-op for keys without a source IP.

Source§

fn kind(&self) -> DetectorKind

Typed identity — lands on every anomaly this detector emits and drives metric labels / ATT&CK tagging.
Source§

fn tracked(&self) -> usize

Keys currently tracked (0 for stateless detectors).
Source§

fn on_flow_start( &mut self, _key: &K, _ts: Timestamp, _out: &mut Vec<OwnedAnomaly>, )

New flow observed (FlowEvent::Started).
Source§

fn on_flow_established( &mut self, _key: &K, _ts: Timestamp, _out: &mut Vec<OwnedAnomaly>, )

TCP 3-way handshake completed (FlowEvent::Established).
Source§

fn on_flow_tick( &mut self, _key: &K, _stats: &FlowStats, _ts: Timestamp, _out: &mut Vec<OwnedAnomaly>, )

Periodic live-flow snapshot (FlowEvent::Tick — only fires when FlowTrackerConfig::flow_tick_interval is set). Note tick stats are cumulative; delta-track inside the detector if you need per-interval values.
Source§

fn on_dns_query( &mut self, _key: &K, _qname: &str, _ts: Timestamp, _out: &mut Vec<OwnedAnomaly>, )

DNS query name observed on flow key. Pre-extracted &str (e.g. from a DnsMessage::Query’s first question) — feed it via DetectorRegistry::observe_dns from your DNS slot drain, resolver log, or any other name source.
Source§

fn evict_expired(&mut self, _now: Timestamp)

Drop per-key state stale relative to now — bounds memory across key churn.

Auto Trait Implementations§

§

impl<K> Freeze for PortScanDetector<K>
where HashMap<K, ScannerState>: Freeze,

§

impl<K> RefUnwindSafe for PortScanDetector<K>
where HashMap<K, ScannerState>: RefUnwindSafe,

§

impl<K> Send for PortScanDetector<K>
where HashMap<K, ScannerState>: Send,

§

impl<K> Sync for PortScanDetector<K>
where HashMap<K, ScannerState>: Sync,

§

impl<K> Unpin for PortScanDetector<K>
where HashMap<K, ScannerState>: Unpin,

§

impl<K> UnsafeUnpin for PortScanDetector<K>
where HashMap<K, ScannerState>: UnsafeUnpin,

§

impl<K> UnwindSafe for PortScanDetector<K>
where HashMap<K, ScannerState>: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<'a, T, E> AsTaggedExplicit<'a, E> for T
where T: 'a,

Source§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self, E>

Source§

impl<'a, T, E> AsTaggedImplicit<'a, E> for T
where T: 'a,

Source§

fn implicit( self, class: Class, constructed: bool, tag: u32, ) -> TaggedParser<'a, Implicit, Self, E>

Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more