From 2974a5408e0493a05c32a099707746804fcbd75b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Mon, 5 Oct 2026 20:45:00 +0300 Subject: [PATCH] :sparkles: feat(dot11): AKM suites, assoc/reassoc parsing, seq/IE-fingerprint/WPS UUID-E correlation hooks --- src/common.rs | 222 ++++++++++++++++++++++-- src/dot11.rs | 466 +++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 671 insertions(+), 17 deletions(-) diff --git a/src/common.rs b/src/common.rs index b8afde7..3474256 100644 --- a/src/common.rs +++ b/src/common.rs @@ -4,6 +4,9 @@ //! serializes through `serde_json` lines. Signatures and field meanings are //! FIXED; implementations must conform exactly. Times are `last_seen_ms` //! (millis since the process-local epoch, see [`now_ms`]), RSSI in dBm. +//! Fields added after the wire format froze carry serde defaults so older +//! records deserialize losslessly (e.g. `ClientInfo.connected`, serde +//! default true). use std::fmt; use std::time::Instant; @@ -95,6 +98,32 @@ impl Security { } } +/// Authentication key-management suites parsed from the RSN/WPA IE. +#[derive(Clone, Copy, PartialEq, Eq, Debug, Serialize, Deserialize)] +pub enum Akm { + Psk, + Sae, + Owe, + FtPsk, + PskSha256, + OneX, + Other(u8), +} + +impl Akm { + pub fn label(self) -> &'static str { + match self { + Akm::Psk => "PSK", + Akm::Sae => "SAE", + Akm::Owe => "OWE", + Akm::FtPsk => "FT-PSK", + Akm::PskSha256 => "PSK-SHA256", + Akm::OneX => "802.1X", + Akm::Other(_) => "other", + } + } +} + /// A discovered access point. Live-updated by the scanner. #[derive(Clone, Debug, Serialize, Deserialize)] pub struct AccessPoint { @@ -107,6 +136,10 @@ pub struct AccessPoint { /// Millis since the process-local epoch ([`now_ms`]) of the last frame /// seen from this AP. Age display = `now_ms() - last_seen_ms`. pub last_seen_ms: u64, + /// AKM suites advertised in the RSN/WPA IE; empty = unknown (old peers, + /// non-RSN APs). + #[serde(default)] + pub akms: Vec, } /// A discovered client station. @@ -120,6 +153,25 @@ pub struct ClientInfo { /// Millis since the process-local epoch ([`now_ms`]) of the last frame /// seen from this station. pub last_seen_ms: u64, + /// Station liveness: true while the station is active/associated; the + /// scanner flips it false when it observes a deauth/disassoc affecting + /// the station, and any later frame from the station flips it back + /// true. Serde default is true so pre-field wire records deserialize + /// as connected. + #[serde(default = "crate::common::default_true")] + pub connected: bool, + /// MAC-randomization pairing: device-group id from the scanner + /// (`Event::ClientIdent`), when this MAC was correlated. + #[serde(default)] + pub ident_group: Option, + #[serde(default)] + pub ident_conf: Option, +} + +/// Serde `default` for fields whose absent wire value means `true` +/// ([`ClientInfo::connected`]). +pub fn default_true() -> bool { + true } /// A complete, crackable WPA 4-way handshake capture (M1+M2 minimum). @@ -177,6 +229,56 @@ pub struct PmkidCapture { pub pmkid: [u8; 16], } +/// Cumulative per-channel frame counters (scanner → UI snapshot). +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +pub struct ChannelStat { + pub channel: u8, + pub frames: u64, + pub beacons: u64, +} + +/// One observed probe request (client asking for a network). +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +pub struct ProbeInfo { + pub client: MacAddr, + /// SSID of the probe; `None` for broadcast/wildcard probes. + pub ssid: Option, + pub rssi_dbm: i8, + /// Millis since the process-local epoch ([`now_ms`]). + pub at_ms: u64, +} + +/// Strength of a MAC-randomization pairing decision ([`Event::ClientIdent`]). +/// Ordered weakest→strongest; UI displays it as `?`/`~`/`=` per `label()`. +#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)] +pub enum IdentConfidence { + /// Only the ordered-IE fingerprint matched — driver-class witness. + Probable, + /// Fingerprint match plus a per-frame witness: shared probed SSID or + /// uninterrupted 802.11 sequence numbers across the rotation. + Strong, + /// WPS UUID-E matched — the device identifies itself. + Exact, +} + +impl IdentConfidence { + pub fn label(self) -> &'static str { + match self { + IdentConfidence::Probable => "?", + IdentConfidence::Strong => "~", + IdentConfidence::Exact => "=", + } + } +} + +/// Which of the two capture kinds was imported from a file (store::StoredCapture +/// stays in store.rs; common must not depend on store). +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +pub enum ImportedCapture { + Handshake(Box), + Pmkid(PmkidCapture), +} + /// Events flowing from worker threads to the UI. In the real architecture /// the privileged service serializes them as `serde_json` lines on the /// child's stdout; in mock mode a thread pushes them onto a @@ -186,6 +288,9 @@ pub enum Event { ApUpdated(AccessPoint), ApLost(MacAddr), ClientUpdated(ClientInfo), + /// Station silent past the scanner's client-expiry window — the UI + /// drops it from the clients table. + ClientLost(MacAddr), /// Progress while collecting 4-way messages. HandshakeProgress { ap: MacAddr, @@ -197,16 +302,46 @@ pub enum Event { PmkidCaptured(PmkidCapture), /// Count of deauth/disassoc frames actually written to the wire. DeauthSent { ap: MacAddr, target: MacAddr, count: u64 }, - /// Candidates checked so far (`done`) and current throughput (`hps`) - /// of a running crack job. - CrackProgress { done: u64, hps: f64 }, + /// Candidates checked so far (`done`), current throughput (`hps`), + /// and the worst-case candidate total (`total`, when the attack + /// mode's space is countable) of a running crack job. Worst-case + /// remaining time = (total - done) / hps. + CrackProgress { done: u64, hps: f64, total: Option }, /// A crack job recovered the passphrase. CrackFound { password: String }, /// The active crack job ended: `found` distinguishes success from /// exhaustion/user-stop (emitted after [`Event::CrackFound`] too). CrackStopped { found: bool }, + /// Throughput of a rainbow-table build job (PMKs written so far); + /// `total` is the wordlist's line count (worst-case table size). + RainbowProgress { done: u64, hps: f64, total: Option }, + /// A rainbow-table build job ended (always exactly one; `entries` is + /// what actually landed in `path`, 0 on failure). + RainbowBuilt { path: String, entries: u64 }, Log(String), Error(String), + /// Cumulative per-channel counters snapshot (emitted every ~2 s). + ChannelStats(Vec), + /// A novel probe request was observed. + ProbeObserved(ProbeInfo), + /// Deauth/disassoc storm detected on a (source, target) pair that we + /// did not cause — a foreign deauth attack in progress. + DeauthAlert { + source: MacAddr, + target: MacAddr, + count: u32, + }, + /// A capture was imported from an external file (pcap / hc22000). + CaptureImported(ImportedCapture), + /// `mac` was correlated with a device group (MAC-randomization pairing): + /// every MAC witnessed on the same physical radio shares `group`. + ClientIdent { + mac: MacAddr, + group: u64, + confidence: IdentConfidence, + }, + /// Crack-speed benchmark finished. + BenchmarkDone { gpu_hps: Option, cpu_hps: f64 }, } /// Channel list for hopper: all enabled 2.4 GHz + 5 GHz UNII channels of the @@ -256,15 +391,35 @@ pub enum Cmd { /// instead of hopping). `None`/empty = full hop list. #[serde(default)] channels: Option>, + /// Pcap spool file for the scan: every received raw frame (with + /// radiotap header) is appended to it. `None` = no spooling. + #[serde(default)] + spool: Option, + /// Truncate the spool file on scan start (`false` = append if it + /// exists). UI sets `true` on a fresh scan from toggle_monitor, + /// `false` when re-parking channels mid-scan. + #[serde(default)] + fresh_spool: bool, }, /// Stop the scanner, restore managed mode. StopScan, - /// Blast `count` deauth rounds at `target` (broadcast allowed) from - /// `ap`, after tuning to `channel`. + /// Blast `count` deauth rounds per `(bssid, channel)` entry at + /// `target` (broadcast allowed). Multiple entries cover dual-band + /// networks: the worker walks the list sequentially (one channel at a + /// time is all a single radio can do). Deauth { - ap: MacAddr, + aps: Vec<(MacAddr, u8)>, target: MacAddr, + count: u32, + }, + /// Deauth every client of one AP at once: pins the channel, blasts + /// `count` rounds at each target in `targets`, then one broadcast + /// blast. `ap` is also recorded so the service's foreign-deauth + /// detector suppresses its own frames. + DeauthAll { + ap: MacAddr, channel: u8, + targets: Vec, count: u32, }, /// Clientless PMKID grab against `ap`. @@ -273,10 +428,12 @@ pub enum Cmd { channel: u8, essid: String, }, - /// Park on `ap`'s channel and track its 4-way handshakes. + /// Track 4-way handshakes of every BSSID in `targets` (dual-band + /// networks send the exchange on whichever band the client chose — the + /// parked SCANNER hops the union of channels, so a single listen + /// covers all bands). One channel → the worker pins it itself. CaptureHs { - ap: MacAddr, - channel: u8, + targets: Vec<(MacAddr, u8)>, essid: String, }, /// Stop NetworkManager/wpa_supplicant so they stop pinning the radio. @@ -298,6 +455,7 @@ mod tests { rssi_dbm: -42, beacons: 7, last_seen_ms: 123_456, + akms: vec![Akm::Psk], } } @@ -308,6 +466,9 @@ mod tests { rssi_dbm: -55, frames: 3, last_seen_ms: 654_321, + connected: true, + ident_group: Some(7), + ident_conf: Some(IdentConfidence::Strong), } } @@ -369,14 +530,41 @@ mod tests { target: MacAddr::BROADCAST, count: 64, }, - Event::CrackProgress { done: 1000, hps: 1234.5 }, + Event::CrackProgress { done: 1000, hps: 1234.5, total: Some(5000) }, Event::CrackFound { password: "hunter2".into(), }, Event::CrackStopped { found: true }, Event::CrackStopped { found: false }, + Event::RainbowProgress { done: 42, hps: 9.5, total: None }, + Event::RainbowBuilt { + path: "/tmp/t.pmk".into(), + entries: 7, + }, Event::Log("hello".into()), Event::Error("boom".into()), + Event::ChannelStats(vec![ + ChannelStat { channel: 6, frames: 1234, beacons: 56 }, + ChannelStat { channel: 36, frames: 78, beacons: 9 }, + ]), + Event::ProbeObserved(ProbeInfo { + client: MacAddr([0x42; 6]), + ssid: Some("HOME-WIFI".into()), + rssi_dbm: -61, + at_ms: 987_654, + }), + Event::DeauthAlert { + source: MacAddr([0xde; 6]), + target: MacAddr::BROADCAST, + count: 17, + }, + Event::CaptureImported(ImportedCapture::Handshake(Box::new(handshake_obj()))), + Event::ClientIdent { + mac: MacAddr([0x02, 0xa0, 0xb1, 0xc2, 0xd3, 0xe4]), + group: 42, + confidence: IdentConfidence::Exact, + }, + Event::BenchmarkDone { gpu_hps: Some(1_250_000.0), cpu_hps: 310_000.0 }, ]; for ev in &cases { let json = serde_json::to_string(ev).expect("serialize"); @@ -413,12 +601,13 @@ mod tests { Cmd::StartScan { ifname: "wlp14s0".into(), channels: None, + spool: None, + fresh_spool: false, }, Cmd::StopScan, Cmd::Deauth { - ap: MacAddr([1; 6]), + aps: vec![(MacAddr([1; 6]), 6)], target: MacAddr::BROADCAST, - channel: 6, count: 16, }, Cmd::Pmkid { @@ -427,12 +616,17 @@ mod tests { essid: "corp-wpa3".into(), }, Cmd::CaptureHs { - ap: MacAddr([3; 6]), - channel: 11, + targets: vec![(MacAddr([3; 6]), 11), (MacAddr([4; 6]), 60)], essid: "coffeeshop".into(), }, Cmd::StopNetworkManager, Cmd::Shutdown, + Cmd::DeauthAll { + ap: MacAddr([5; 6]), + channel: 44, + targets: vec![MacAddr([6; 6]), MacAddr([7; 6])], + count: 8, + }, ]; for cmd in &cases { let json = serde_json::to_string(cmd).expect("serialize"); diff --git a/src/dot11.rs b/src/dot11.rs index d3cc137..5a31afa 100644 --- a/src/dot11.rs +++ b/src/dot11.rs @@ -5,15 +5,15 @@ //! captures may or may not include one — parsers simply ignore trailing //! bytes beyond what they need). -use crate::common::{MacAddr, Security}; +use crate::common::{Akm, MacAddr, Security}; /// LLC/SNAP header prefixing an EAPOL payload inside a data frame. const SNAP_EAPOL: [u8; 8] = [0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00, 0x88, 0x8e]; -const IE_SSID: u8 = 0; +pub(crate) const IE_SSID: u8 = 0; const IE_DS_PARAM: u8 = 3; const IE_RSN: u8 = 48; -const IE_VENDOR: u8 = 221; +pub(crate) const IE_VENDOR: u8 = 221; /// Microsoft (WPA) vendor IE OUI+type prefix. const WPA_OUI: [u8; 4] = [0x00, 0x50, 0xf2, 0x01]; @@ -65,6 +65,40 @@ pub struct MgmtInfo { pub ssid: Option, // None if hidden/zero-len pub channel: Option, // DS parameter set IE pub security: Security, // from RSN/WPA IEs + capability privacy bit + /// AKM suites from the RSN/WPA IE; empty for open/WEP or unparsed frames. + pub akms: Vec, +} + +/// Map an RSN AKM suite type byte to `Akm`. +fn akm_from_type(t: u8) -> Akm { + match t { + 2 => Akm::Psk, + 8 => Akm::Sae, + 18 => Akm::Owe, + 4 => Akm::FtPsk, + 6 => Akm::PskSha256, + 1 => Akm::OneX, + other => Akm::Other(other), + } +} + +/// Walk the AKM suite list of an RSN IE body, collecting mapped `Akm`s. +/// Layout: version(2) group(4) pairwise_cnt(2) pairwise[4n] akm_cnt(2) akm[4m]. +fn rsn_akms(body: &[u8], out: &mut Vec) { + let Some(pair_cnt) = u16le(body, 6).map(|c| c as usize) else { + return; + }; + let mut o = 8usize + 4 * pair_cnt; + let Some(akm_cnt) = u16le(body, o).map(|c| c as usize) else { + return; + }; + o += 2; + for i in 0..akm_cnt { + let Some(suite) = body.get(o + 4 * i..o + 4 * i + 4) else { + break; + }; + out.push(akm_from_type(suite[3])); + } } /// True if the RSN IE body lists AKM suite 00:0f:ac:8 (SAE → WPA3). @@ -109,6 +143,7 @@ pub fn parse_beacon_or_probe(f: &[u8]) -> Option { let mut has_rsn = false; let mut has_sae = false; let mut has_wpa = false; + let mut akms = Vec::new(); walk_ies(f, 36, |id, body| match id { IE_SSID => { if !body.is_empty() { @@ -121,10 +156,13 @@ pub fn parse_beacon_or_probe(f: &[u8]) -> Option { IE_RSN => { has_rsn = true; has_sae |= rsn_has_akm(body, 8); + rsn_akms(body, &mut akms); } IE_VENDOR => { if body.len() >= 4 && body[..4] == WPA_OUI { has_wpa = true; + // WPA1 vendor IE: only PSK key management exists. + akms.push(Akm::Psk); } } _ => {} @@ -140,12 +178,23 @@ pub fn parse_beacon_or_probe(f: &[u8]) -> Option { Security::Open }; + // An AP advertising WPA (vendor IE) AND WPA2 (RSN IE, e.g. mixed or + // transition mode) otherwise surfaces PSK twice. First-seen order wins. + let mut uniq: Vec = Vec::with_capacity(akms.len()); + for a in akms { + if !uniq.contains(&a) { + uniq.push(a); + } + } + let akms = uniq; + Some(MgmtInfo { bssid: mac_at(f, 16)?, transmitter: mac_at(f, 10)?, ssid, channel, security, + akms, }) } @@ -169,6 +218,179 @@ pub fn parse_probe_req(f: &[u8]) -> Option<(MacAddr, Option)> { Some((sta, ssid)) } +/// Association/reassociation request → (station, bssid, ssid). +/// None for other/broken frames. SSID is None for hidden (`\0`-padded or +/// empty) networks. +pub fn parse_assoc_req(f: &[u8]) -> Option<(MacAddr, MacAddr, Option)> { + let fc = u16le(f, 0)?; + let (ty, st) = type_subtype(fc); + // subtype 0 = assoc request, 1 = reassoc request; mgmt type only. + if ty != 0 || st > 1 { + return None; + } + let bssid = mac_at(f, 4)?; // addr1 + let sta = mac_at(f, 10)?; // addr2 + // Fixed params: capability(2) + listen interval(2) at offset 24, so IEs + // start at 28; reassoc adds current-AP MAC(6) → IEs start at 34. + let ie_off = if st == 0 { 28 } else { 34 }; + let mut ssid = None; + walk_ies(f, ie_off, |id, body| { + if id == IE_SSID { + // hidden networks send a zero or NUL-padded SSID + let s: &[u8] = body; + let meaningful = s.iter().take_while(|&&b| b != 0).count(); + if meaningful > 0 { + ssid = Some(String::from_utf8_lossy(&s[..meaningful]).into_owned()); + } + } + }); + // ensure the fixed params fit (frames shorter than ie_off are bogus) + if f.len() < ie_off { + return None; + } + Some((sta, bssid, ssid)) +} + +// --------------------------------------------------------------------------- +// device correlation (pairing rotated/randomized MACs of one radio) +// --------------------------------------------------------------------------- + +/// 12-bit per-transmitter sequence number (bytes 22-24, high nibble is the +/// fragment number). A device's sequence counter survives MAC rotation, so +/// near-continuity between two MACs' frames is a strong pairing witness. +pub fn seq_of(f: &[u8]) -> Option { + Some(u16le(f, 22)? >> 4) +} + +fn fnv_add(h: &mut u32, b: u8) { + *h ^= b as u32; + *h = h.wrapping_mul(0x0100_0193); +} + +/// Ordered-IE device fingerprint (probe/assoc request frames): FNV-1a over +/// the (tag, len) sequence, plus full payloads of the rate tags and the +/// OUI+type tuple of vendor IEs. Stable per driver/firmware build and +/// independent of the station's (possibly randomized) MAC and of which +/// SSIDs it is probing. Different drivers of the same model usually share +/// it, so it is a *probable* witness, not identity — pair it with UUID-E or +/// sequence continuity for certainty. +pub fn ie_fingerprint(f: &[u8], ie_off: usize) -> Option { + let mut h = 0x811c_9dc5u32; + let mut n = 0u32; + walk_ies(f, ie_off, |id, body| { + fnv_add(&mut h, id); + // SSID tag: content-agnostic. Directed and wildcard probes of the + // same radio alternate freely and differ only in this length; the + // probed-network signal comes from the group's SSID set instead. + if id != IE_SSID { + fnv_add(&mut h, body.len() as u8); + } + match id { + // rates + extended rates: full payload is model-specific + 1 | 50 => body.iter().for_each(|&b| fnv_add(&mut h, b)), + // vendor IE: OUI+type+len, not the (device-varying) payload + IE_VENDOR if body.len() >= 4 => body[..4].iter().for_each(|&b| fnv_add(&mut h, b)), + _ => {} + } + n += 1; + }); + (n > 0).then_some(h) +} + +/// WPS UUID-E (attribute 0x1047) from a WPS vendor IE, if present. A stable +/// per-device identifier leaked in probe requests by devices with WPS on — +/// an exact pairing witness across MAC rotation. +pub fn wps_uuid_e(f: &[u8], ie_off: usize) -> Option<[u8; 16]> { + // WPS vendor IE: OUI 00:50:f2, type 0x04; attributes are BE (id, len). + const WPS_OUI: [u8; 4] = [0x00, 0x50, 0xf2, 0x04]; + const ATTR_UUID_E: u16 = 0x1047; + let mut out = None; + walk_ies(f, ie_off, |id, body| { + if id != IE_VENDOR || body.len() < 4 || body[..4] != WPS_OUI { + return; + } + let mut cur = 4; + while cur + 4 <= body.len() { + let attr = u16::from_be_bytes([body[cur], body[cur + 1]]); + let len = u16::from_be_bytes([body[cur + 2], body[cur + 3]]) as usize; + let Some(val) = body.get(cur + 4..cur + 4 + len) else { + break; + }; + if attr == ATTR_UUID_E && len == 16 { + out = Some(val.try_into().expect("len checked")); + } + cur += 4 + len; + } + }); + out +} + +/// Device-correlation material parsed from a probe request. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct ProbeIdent { + pub sta: MacAddr, + pub ssid: Option, // None for the wildcard probe + pub fingerprint: u32, + pub uuid: Option<[u8; 16]>, +} + +/// Same, from an association/reassociation request (adds the target bssid). +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct AssocIdent { + pub sta: MacAddr, + pub bssid: MacAddr, + pub ssid: Option, + pub fingerprint: u32, + pub uuid: Option<[u8; 16]>, +} + +/// Probe request with device-correlation material; None for other frames or +/// probes with an empty/malformed IE chain (fingerprint impossible). +pub fn parse_probe_ident(f: &[u8]) -> Option { + let fc = u16le(f, 0)?; + if type_subtype(fc) != (0, 4) { + return None; + } + let sta = mac_at(f, 10)?; + let fingerprint = ie_fingerprint(f, 24)?; + let mut ssid = None; + walk_ies(f, 24, |id, body| { + if id == IE_SSID && !body.is_empty() { + ssid = Some(String::from_utf8_lossy(body).into_owned()); + } + }); + let uuid = wps_uuid_e(f, 24); + Some(ProbeIdent { sta, ssid, fingerprint, uuid }) +} + +/// Assoc/reassoc request with device-correlation material; None for other +/// frames. SSID is None for hidden (empty/NUL-padded) networks. +pub fn parse_assoc_ident(f: &[u8]) -> Option { + let fc = u16le(f, 0)?; + let (ty, st) = type_subtype(fc); + if ty != 0 || st > 1 { + return None; + } + let ie_off = if st == 0 { 28 } else { 34 }; + if f.len() < ie_off { + return None; + } + let bssid = mac_at(f, 4)?; + let sta = mac_at(f, 10)?; + let fingerprint = ie_fingerprint(f, ie_off)?; + let mut ssid = None; + walk_ies(f, ie_off, |id, body| { + if id == IE_SSID { + let meaningful = body.iter().take_while(|&&b| b != 0).count(); + if meaningful > 0 { + ssid = Some(String::from_utf8_lossy(&body[..meaningful]).into_owned()); + } + } + }); + let uuid = wps_uuid_e(f, ie_off); + Some(AssocIdent { sta, bssid, ssid, fingerprint, uuid }) +} + /// Parse deauth/disassoc → (from-ap MacAddr as transmitter, target addr1). pub fn parse_deauth(f: &[u8]) -> Option<(MacAddr, MacAddr)> { let fc = u16le(f, 0)?; @@ -333,6 +555,19 @@ pub fn build_disassoc(ap: &MacAddr, target: &MacAddr, reason: u16) -> Vec { v } +/// Probe request sta → broadcast: empty `ssid` = wildcard (every AP on +/// the channel should answer with a probe response), otherwise directed. +/// Used by the injection canary: a response proves injected frames +/// actually radiate. +pub fn build_probe_req(ssid: &[u8], sa: &MacAddr) -> Vec { + let mut v = hdr3(0x40, &MacAddr::BROADCAST, sa, &MacAddr::BROADCAST, 0); + v.push(IE_SSID); + v.push(ssid.len().min(32) as u8); + v.extend_from_slice(&ssid[..ssid.len().min(32)]); + v.extend_from_slice(&[0x01, 0x04, 0x82, 0x84, 0x8b, 0x96]); // supported rates + v +} + /// Open-system authentication request sta → ap (algo 0, transaction 1). pub fn build_auth_open(ap: &MacAddr, sta: &MacAddr, seq: u16) -> Vec { let mut v = hdr3(0xB0, ap, sta, ap, seq); @@ -414,6 +649,219 @@ mod tests { assert_eq!(info.security, Security::Wpa2); } + /// RSN IE with several AKM suites listed in order. + fn rsn_ie_multi(akm_types: &[u8]) -> Vec { + let mut b = vec![ + 0x01, 0x00, // version + 0x00, 0x0f, 0xac, 0x04, // group cipher CCMP + 0x01, 0x00, // pairwise count + 0x00, 0x0f, 0xac, 0x04, // pairwise CCMP + ]; + b.extend_from_slice(&(akm_types.len() as u16).to_le_bytes()); + for &t in akm_types { + b.extend_from_slice(&[0x00, 0x0f, 0xac, t]); + } + b.extend_from_slice(&[0x00, 0x00]); // rsn capabilities + let b = b.split_off(0); + ie(IE_RSN, &b) + } + + /// Beacon like `parafin_beacon` but with the RSN IE replaced by `rsn`. + fn beacon_with_rsn(rsn: Vec) -> Vec { + let mut v = parafin_beacon(); + v.splice(v.len() - rsn_ie(2).len().., rsn); + v + } + + #[test] + fn akm_list_parsed() { + // WPA2/WPA3 transition: PSK + SAE both advertised. + let f = beacon_with_rsn(rsn_ie_multi(&[2, 8])); + let info = parse_beacon_or_probe(&f).expect("beacon parses"); + assert_eq!(info.akms, vec![Akm::Psk, Akm::Sae]); + // security label logic untouched: SAE present → Wpa3 + assert_eq!(info.security, Security::Wpa3); + + // OWE (type 18) + let f = beacon_with_rsn(rsn_ie_multi(&[18])); + let info = parse_beacon_or_probe(&f).unwrap(); + assert_eq!(info.akms, vec![Akm::Owe]); + // no SAE → Wpa2 label + assert_eq!(info.security, Security::Wpa2); + + // unknown suite type → Other, PSK still mapped + let f = beacon_with_rsn(rsn_ie_multi(&[2, 99])); + let info = parse_beacon_or_probe(&f).unwrap(); + assert_eq!(info.akms, vec![Akm::Psk, Akm::Other(99)]); + + // plain WPA2-PSK beacon: single PSK entry + let info = parse_beacon_or_probe(¶fin_beacon()).unwrap(); + assert_eq!(info.akms, vec![Akm::Psk]); + + // open network: no security IEs → empty akms + let mut f = parafin_beacon(); + f.truncate(36); + f.extend_from_slice(&ie(IE_SSID, b"cafe")); + f[34] = 0x01; // clear privacy bit + f[35] = 0x04; + let info = parse_beacon_or_probe(&f).unwrap(); + assert_eq!(info.security, Security::Open); + assert_eq!(info.akms, Vec::::new()); + } + + #[test] + fn wpa1_vendor_ie_yields_psk_akm() { + let mut f = parafin_beacon(); + f.truncate(36); + f.extend_from_slice(&ie(IE_SSID, b"legacy")); + // WPA_OUI vendor IE: oui+type, then version(2) — content beyond the + // OUI is not inspected, so the minimal fixture suffices. + f.extend_from_slice(&ie(IE_VENDOR, &[0x00, 0x50, 0xf2, 0x01, 0x01, 0x00])); + let info = parse_beacon_or_probe(&f).unwrap(); + assert_eq!(info.security, Security::Wpa); + assert_eq!(info.akms, vec![Akm::Psk]); + } + + #[test] + fn akms_deduped_across_wpa_and_rsn_ies() { + // Mixed WPA/WPA2 AP: RSN IE with PSK plus the WPA vendor IE — the + // vendor arm must not surface a duplicate PSK. + let mut f = beacon_with_rsn(rsn_ie_multi(&[2])); + f.extend_from_slice(&ie(IE_VENDOR, &[0x00, 0x50, 0xf2, 0x01, 0x01, 0x00])); + let info = parse_beacon_or_probe(&f).unwrap(); + assert_eq!(info.akms, vec![Akm::Psk], "no PSK+PSK duplication"); + } + + #[test] + fn seq_number_extracted() { + let mut f = parafin_beacon(); + // seq field at bytes 22-24: seq<<4 | frag + f[22] = 0x30; // seq 3, frag 0 + f[23] = 0x02; // → 0x230 >> 4 = 0x23 = 35 + assert_eq!(seq_of(&f), Some(0x23)); + assert_eq!(seq_of(&f[..20]), None); + } + + /// Probe builder mirroring the scanner tests' shape. + fn probe_with(sta: &MacAddr, ssid: Option<&str>, ies: &[Vec], seq: u16) -> Vec { + let mut v = Vec::new(); + v.extend_from_slice(&0x0040u16.to_le_bytes()); + v.extend_from_slice(&0u16.to_le_bytes()); + v.extend_from_slice(&MacAddr::BROADCAST.0); + v.extend_from_slice(&sta.0); + v.extend_from_slice(&MacAddr::BROADCAST.0); + v.extend_from_slice(&(seq << 4).to_le_bytes()); + match ssid { + Some(s) => v.extend_from_slice(&ie(IE_SSID, s.as_bytes())), + None => v.extend_from_slice(&ie(IE_SSID, b"")), + } + for b in ies { + v.extend_from_slice(b); + } + v + } + + #[test] + fn fingerprint_stable_directed_vs_wildcard() { + // The same radio alternating directed/wildcard probes must hash + // identically; a different IE chain must not. + let rates = ie(1, &[0x82, 0x84, 0x8b, 0x96]); + let f1 = probe_with(&STA, Some("parafin"), &[rates.clone()], 7); + let f2 = probe_with(&STA, None, &[rates.clone()], 8); + let i1 = parse_probe_ident(&f1).expect("parses"); + let i2 = parse_probe_ident(&f2).expect("parses"); + assert_eq!(i1.fingerprint, i2.fingerprint); + assert_eq!(i1.ssid.as_deref(), Some("parafin")); + assert_eq!(i2.ssid, None); + + let other = probe_with(&STA, None, &[ie(1, &[0x82, 0x84])], 9); + let i3 = parse_probe_ident(&other).expect("parses"); + assert_ne!(i1.fingerprint, i3.fingerprint); + + // Off a probe frame entirely → None. + assert!(parse_probe_ident(¶fin_beacon()).is_none()); + } + + #[test] + fn wps_uuid_e_parsed_from_probe() { + let uuid = [0xabu8; 16]; + let mut wps_body = vec![0x00, 0x50, 0xf2, 0x04]; + wps_body.extend_from_slice(&0x1047u16.to_be_bytes()); + wps_body.extend_from_slice(&16u16.to_be_bytes()); + wps_body.extend_from_slice(&uuid); + let wps = ie(IE_VENDOR, &wps_body); + let f = probe_with(&STA, None, &[wps], 7); + let ident = parse_probe_ident(&f).expect("parses"); + assert_eq!(ident.uuid, Some(uuid)); + // Truncated attribute body → no uuid, no panic. + let short = ie(IE_VENDOR, &[0x00, 0x50, 0xf2, 0x04, 0x10, 0x47, 0x00, 0x10, 0xaa, 0xbb]); + let f = probe_with(&STA, None, &[short], 7); + assert_eq!(parse_probe_ident(&f).unwrap().uuid, None); + } + + #[test] + fn assoc_ident_parsed() { + let f = build_assoc_req(&AP, &STA, b"testap"); + let ident = parse_assoc_ident(&f).expect("assoc ident parses"); + assert_eq!(ident.sta, STA); + assert_eq!(ident.bssid, AP); + assert_eq!(ident.ssid.as_deref(), Some("testap")); + assert_eq!(ident.uuid, None); + assert!(parse_assoc_ident(¶fin_beacon()).is_none()); + for cut in 0..f.len() { + let _ = parse_assoc_ident(&f[..cut]); + } + } + + #[test] + fn assoc_req_parsed() { + // assoc request: addr1=bssid(AP), addr2=sta, ssid "testap" + let f = build_assoc_req(&AP, &STA, b"testap"); + let (sta, bssid, ssid) = parse_assoc_req(&f).expect("assoc-req parses"); + assert_eq!(sta, STA); + assert_eq!(bssid, AP); + assert_eq!(ssid.as_deref(), Some("testap")); + + // reassoc request (subtype 1): extra current-AP field before IEs + let mut r = hdr3(0x10, &AP, &STA, &AP, 0); + r.extend_from_slice(&0x0431u16.to_le_bytes()); // capability + r.extend_from_slice(&10u16.to_le_bytes()); // listen interval + r.extend_from_slice(&STA.0); // current AP (assoc) — arbitrary MAC here + r.extend_from_slice(&ie(IE_SSID, b"home-net")); + let (sta, bssid, ssid) = parse_assoc_req(&r).expect("reassoc-req parses"); + assert_eq!(sta, STA); + assert_eq!(bssid, AP); + assert_eq!(ssid.as_deref(), Some("home-net")); + + // empty SSID → None (hidden/wildcard attempt) + let mut g = hdr3(0x00, &AP, &STA, &AP, 0); + g.extend_from_slice(&0x0431u16.to_le_bytes()); + g.extend_from_slice(&10u16.to_le_bytes()); + g.extend_from_slice(&ie(IE_SSID, b"")); + let (_, _, ssid) = parse_assoc_req(&g).unwrap(); + assert_eq!(ssid, None); + + // NUL-padded SSID → None + let mut h = hdr3(0x00, &AP, &STA, &AP, 0); + h.extend_from_slice(&0x0431u16.to_le_bytes()); + h.extend_from_slice(&10u16.to_le_bytes()); + h.extend_from_slice(&ie(IE_SSID, &[0x00, 0x00, 0x00])); + let (_, _, ssid) = parse_assoc_req(&h).unwrap(); + assert_eq!(ssid, None); + + // truncated frames: header cut anywhere → None or no panic + for cut in 0..f.len() { + let _ = parse_assoc_req(&f[..cut]); + } + // frame too short to hold the fixed params → None + assert_eq!(parse_assoc_req(&f[..24]), None); + + // non-assoc frames rejected + assert_eq!(parse_assoc_req(¶fin_beacon()), None); + let pr = build_probe_req(b"x", &STA); + assert_eq!(parse_assoc_req(&pr), None); + } + #[test] fn beacon_security_matrix() { // WPA3: same beacon but SAE AKM @@ -499,6 +947,18 @@ mod tests { let (sta, ssid) = parse_probe_req(&f).unwrap(); assert_eq!(sta, STA); assert_eq!(ssid.as_deref(), Some("parafin")); + } + + #[test] + fn build_probe_req_roundtrips_wildcard_and_directed() { + let wild = build_probe_req(&[], &STA); + let (sta, ssid) = parse_probe_req(&wild).unwrap(); + assert_eq!(sta, STA); + assert_eq!(ssid, None, "wildcard SSID (len 0) parses as None"); + let dir = build_probe_req(b"parafin", &STA); + let (sta, ssid) = parse_probe_req(&dir).unwrap(); + assert_eq!(sta, STA); + assert_eq!(ssid.as_deref(), Some("parafin")); // broadcast probe: zero-len SSID → None let mut b = hdr3(0x40, &MacAddr::BROADCAST, &STA, &MacAddr::BROADCAST, 0);