A WiFi security auditing software
handshake: detect WPA handshakes natively instead of via aircrack-ng
Martin Olivier9b33cd3b9edb
diff --git a/Cargo.lock b/Cargo.lockindex 0b538fa..053f289 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -59,7 +59,8 @@ dependencies = [name = "airgorah-common"version = "0.8.1"dependencies = [- "regex",+ "libwifi",+ "radiotap","serde","serde_json","which",diff --git a/crates/agent/src/backend/pcap.rs b/crates/agent/src/backend/pcap.rsindex 4e0a826..84cbc5e 100644--- a/crates/agent/src/backend/pcap.rs+++ b/crates/agent/src/backend/pcap.rs@@ -3,12 +3,12 @@//! The frames we pull off the monitor interface are radiotap-prefixed 802.11//! frames, so the file is written with link-type//! `LINKTYPE_IEEE802_11_RADIOTAP` (127) — the same format `airodump-ng`-//! produced. This keeps everything downstream (handshake detection via-//! `aircrack-ng`, `mergecap`, and the GUI capture export) working unchanged.+//! produced. This keeps everything downstream (native handshake detection, the+//! `mergecap` accumulation, and the GUI capture export) working unchanged.//!//! Only the classic libpcap format is implemented (global header + per-record-//! header + raw bytes); that is all `aircrack-ng`/`mergecap` need and it avoids-//! pulling in a libpcap dependency.+//! header + raw bytes); that is all the readers need and it avoids pulling in a+//! libpcap dependency.use std::fs::File;use std::io::{self, BufWriter, Write};diff --git a/crates/common/Cargo.toml b/crates/common/Cargo.tomlindex 3037e99..9b738f3 100644--- a/crates/common/Cargo.toml+++ b/crates/common/Cargo.toml@@ -11,5 +11,6 @@ documentation.workspace = true[dependencies]serde.workspace = trueserde_json.workspace = true-regex.workspace = truewhich.workspace = true+libwifi = "0.5"+radiotap = "1"diff --git a/crates/common/src/deps.rs b/crates/common/src/deps.rsindex 50ac8b6..8ea8322 100644--- a/crates/common/src/deps.rs+++ b/crates/common/src/deps.rs@@ -50,7 +50,7 @@ pub const TOOLS: &[Tool] = &[},Tool {name: "aircrack-ng",- requirer: Requirer::Both,+ requirer: Requirer::Gui,optional: false,},Tool {diff --git a/crates/common/src/handshake.rs b/crates/common/src/handshake.rsindex 44eb6e2..fea0858 100644--- a/crates/common/src/handshake.rs+++ b/crates/common/src/handshake.rs@@ -1,51 +1,329 @@-//! Handshake detection via `aircrack-ng`.+//! Native WPA handshake detection.//!-//! Shared because both sides need it against different files: the agent scans-//! the root-owned live/old captures to flag APs, while the GUI inspects-//! a user-selected capture before offering it for decryption. Reading a capture-//! and running `aircrack-ng` needs no privilege — only file access — so the logic-//! is identical on both sides.--use regex::Regex;-use std::process::Command;--/// Return `(bssid, essid)` for every AP that has at least one captured WPA-/// handshake in the given capture file(s).-pub fn get_handshakes<I, S>(args: I) -> std::io::Result<Vec<(String, String)>>+//! Reads capture file(s) and reports which access points have a crackable WPA+//! 4-way handshake — the in-house replacement for shelling out to `aircrack-ng`.+//! Shared because both sides need it against different files: the agent scans the+//! root-owned live/old captures to flag APs while scanning, and the GUI inspects a+//! user-selected capture before offering it for decryption. Reading a capture and+//! parsing it needs no privilege, so the logic is identical on both sides.+//!+//! Only the classic libpcap format is read, with link type+//! `LINKTYPE_IEEE802_11_RADIOTAP` (127, what airgorah itself writes) or plain+//! `LINKTYPE_IEEE802_11` (105). Frames are decoded with `radiotap` + `libwifi`;+//! a data frame carrying an EAPOL key is classified into one of the four handshake+//! messages, and an AP is reported once a crackable combination has been seen.++use std::collections::{HashMap, HashSet};+use std::path::Path;++use libwifi::Addresses;+use libwifi::Frame;+use libwifi::frame::EapolKey;+use libwifi::frame::components::{DataHeader, ManagementHeader, StationInfo};++const LINKTYPE_IEEE802_11: u32 = 105;+const LINKTYPE_IEEE802_11_RADIOTAP: u32 = 127;++/// Return `(bssid, essid)` for every AP that has a captured WPA 4-way handshake in+/// the given capture file(s). Unreadable or unparsable files are skipped.+pub fn get_handshakes<I, S>(paths: I) -> std::io::Result<Vec<(String, String)>>whereI: IntoIterator<Item = S>,- S: AsRef<std::ffi::OsStr>,+ S: AsRef<Path>,{- let capture_output = Command::new("aircrack-ng").args(args).output()?;+ let mut essids: HashMap<String, String> = HashMap::new();+ // Per (bssid, station): a bitmask of which handshake messages M1..M4 were seen.+ let mut messages: HashMap<(String, String), u8> = HashMap::new();- let output = String::from_utf8_lossy(&capture_output.stdout).to_string();+ for path in paths {+ if let Ok(data) = std::fs::read(path.as_ref()) {+ scan_capture(&data, &mut essids, &mut messages);+ }+ }- // Pattern is a compile-time constant, so construction cannot fail.- let re = Regex::new(r"\s+(\d+)\s+([\w:]+)\s+(.*)\s+WPA \((\d+)\s+handshake.*\)")- .expect("valid handshake regex");+ // A crackable 4-way handshake needs M2 — the only carrier of the SNonce, plus a+ // MIC — together with an ANonce source (M1 or M3).+ let mut bssids: HashSet<String> = HashSet::new();+ for ((bssid, _station), seen) in &messages {+ let m1 = seen & 0b0001 != 0;+ let m2 = seen & 0b0010 != 0;+ let m3 = seen & 0b0100 != 0;+ if m2 && (m1 || m3) {+ bssids.insert(bssid.clone());+ }+ }++ Ok(bssids+ .into_iter()+ .map(|bssid| {+ let essid = essids+ .get(&bssid)+ .cloned()+ .unwrap_or_else(|| "hidden".to_string());+ (bssid, essid)+ })+ .collect())+}- let mut handshakes = vec![];+/// Walk a single capture's frames, accumulating ESSIDs and handshake messages.+fn scan_capture(+ data: &[u8],+ essids: &mut HashMap<String, String>,+ messages: &mut HashMap<(String, String), u8>,+) {+ let Some(mut reader) = PcapReader::new(data) else {+ return;+ };+ let link_type = reader.link_type;+ if link_type != LINKTYPE_IEEE802_11 && link_type != LINKTYPE_IEEE802_11_RADIOTAP {+ return;+ }- for line in output.lines() {- let caps = match re.captures(line) {- Some(caps) => caps,- None => continue,+ while let Some(record) = reader.next_frame() {+ let (body, fcs) = if link_type == LINKTYPE_IEEE802_11_RADIOTAP {+ let Ok((radiotap, rest)) = radiotap::Radiotap::parse(record) else {+ continue;+ };+ if radiotap.flags.as_ref().is_some_and(|f| f.bad_fcs) {+ continue;+ }+ (rest, radiotap.flags.as_ref().is_some_and(|f| f.fcs))+ } else {+ (record, false)};- let bssid = caps[2].to_string();- let essid = caps[3].trim().to_string();- let handshake_count = caps[4].to_string();-- if handshake_count.parse::<i32>().unwrap_or(0) > 0 {- handshakes.push((- bssid,- match essid.is_empty() {- true => "hidden".to_string(),- false => essid,- },- ));+ // The FCS flag is best-effort; fall back to parsing without it so a+ // mislabelled trailer never hides a handshake.+ let frame = match libwifi::parse_frame(body, fcs) {+ Ok(frame) => frame,+ Err(_) if fcs => match libwifi::parse_frame(body, false) {+ Ok(frame) => frame,+ Err(_) => continue,+ },+ Err(_) => continue,+ };++ match frame {+ Frame::Beacon(beacon) => record_essid(&beacon.header, &beacon.station_info, essids),+ Frame::ProbeResponse(resp) => record_essid(&resp.header, &resp.station_info, essids),+ Frame::Data(data) => record_eapol(&data.header, &data.eapol_key, messages),+ Frame::QosData(data) => record_eapol(&data.header, &data.eapol_key, messages),+ _ => {}}}+}++/// Remember an AP's ESSID from a beacon / probe response (ignoring hidden ones).+fn record_essid(+ header: &ManagementHeader,+ info: &StationInfo,+ essids: &mut HashMap<String, String>,+) {+ let Some(bssid) = header.bssid() else {+ return;+ };+ if let Some(essid) = info.essid()+ && !essid.is_empty()+ && !essid.starts_with("<hidden")+ {+ essids.entry(bssid.to_long_string()).or_insert(essid);+ }+}++/// Classify an EAPOL data frame and record which handshake message it is.+fn record_eapol(+ header: &DataHeader,+ eapol: &Option<EapolKey>,+ messages: &mut HashMap<(String, String), u8>,+) {+ let Some(key) = eapol else {+ return;+ };+ let Some(message) = classify_message(key.key_information) else {+ return;+ };- Ok(handshakes)+ // M1/M3 flow AP->station (from_ds); M2/M4 flow station->AP (to_ds).+ let fc = &header.frame_control;+ let (bssid, station) = if fc.to_ds() && !fc.from_ds() {+ (header.ra(), header.ta())+ } else if fc.from_ds() && !fc.to_ds() {+ (header.ta(), header.ra())+ } else {+ return;+ };++ let pair = (bssid.to_long_string(), station.to_long_string());+ *messages.entry(pair).or_insert(0) |= 1 << (message - 1);+}++/// Which 4-way handshake message (1..4) an EAPOL key frame is, from its Key+/// Information field, or `None` if it is not a pairwise handshake message.+fn classify_message(key_information: u16) -> Option<u8> {+ // Key Type bit (0x0008) distinguishes the pairwise 4-way from group rekeying.+ if key_information & 0x0008 == 0 {+ return None;+ }+ let ack = key_information & 0x0080 != 0;+ let mic = key_information & 0x0100 != 0;+ let secure = key_information & 0x0200 != 0;++ match (ack, mic, secure) {+ (true, false, _) => Some(1), // ANonce, no MIC+ (false, true, false) => Some(2), // SNonce + MIC+ (true, true, _) => Some(3), // ANonce + MIC, install/secure+ (false, true, true) => Some(4), // MIC, secure+ _ => None,+ }+}++/// A minimal reader over the records of a classic libpcap file.+struct PcapReader<'a> {+ data: &'a [u8],+ offset: usize,+ big_endian: bool,+ link_type: u32,+}++impl<'a> PcapReader<'a> {+ /// Parse the 24-byte global header, detecting endianness from the magic.+ /// Returns `None` if the header is missing or not a libpcap magic.+ fn new(data: &'a [u8]) -> Option<Self> {+ if data.len() < 24 {+ return None;+ }+ let magic = u32::from_le_bytes(data[0..4].try_into().ok()?);+ let big_endian = match magic {+ // microsecond / nanosecond magics, same-endian as this reader+ 0xa1b2_c3d4 | 0xa1b2_3c4d => false,+ // byte-swapped+ 0xd4c3_b2a1 | 0x4d3c_b2a1 => true,+ _ => return None,+ };+ let link_type = read_u32(&data[20..24], big_endian);+ Some(Self {+ data,+ offset: 24,+ big_endian,+ link_type,+ })+ }++ /// Yield the next record's captured bytes, or `None` at the end (or on a+ /// truncated trailing record, which a live capture being read mid-write can+ /// present).+ fn next_frame(&mut self) -> Option<&'a [u8]> {+ if self.offset + 16 > self.data.len() {+ return None;+ }+ let incl_len = read_u32(+ &self.data[self.offset + 8..self.offset + 12],+ self.big_endian,+ ) as usize;+ let start = self.offset + 16;+ let end = start.checked_add(incl_len)?;+ if end > self.data.len() {+ return None;+ }+ self.offset = end;+ Some(&self.data[start..end])+ }+}++fn read_u32(bytes: &[u8], big_endian: bool) -> u32 {+ let arr = [bytes[0], bytes[1], bytes[2], bytes[3]];+ if big_endian {+ u32::from_be_bytes(arr)+ } else {+ u32::from_le_bytes(arr)+ }+}++#[cfg(test)]+mod tests {+ use super::*;++ #[test]+ fn classifies_the_four_handshake_messages() {+ // Canonical Key Information values for a WPA2 4-way handshake.+ assert_eq!(classify_message(0x008a), Some(1)); // pairwise + ACK+ assert_eq!(classify_message(0x010a), Some(2)); // pairwise + MIC+ assert_eq!(classify_message(0x13ca), Some(3)); // pairwise + ACK + MIC + install/secure+ assert_eq!(classify_message(0x030a), Some(4)); // pairwise + MIC + secure+ // Group rekey (Key Type bit clear) and empty are not handshake messages.+ assert_eq!(classify_message(0x0382), None);+ assert_eq!(classify_message(0x0000), None);+ }++ /// The rule get_handshakes applies: M2 (SNonce+MIC) plus an ANonce source.+ fn is_complete(bits: u8) -> bool {+ let m1 = bits & 0b0001 != 0;+ let m2 = bits & 0b0010 != 0;+ let m3 = bits & 0b0100 != 0;+ m2 && (m1 || m3)+ }++ #[test]+ fn a_crackable_handshake_needs_m2_plus_anonce() {+ assert!(is_complete(0b0011)); // M1 + M2+ assert!(is_complete(0b0110)); // M2 + M3+ assert!(!is_complete(0b0010)); // M2 alone (no ANonce)+ assert!(!is_complete(0b0101)); // M1 + M3 (no SNonce)+ assert!(!is_complete(0b1000)); // M4 alone+ }++ fn push_record(data: &mut Vec<u8>, payload: &[u8]) {+ data.extend_from_slice(&0u32.to_le_bytes()); // ts_sec+ data.extend_from_slice(&0u32.to_le_bytes()); // ts_usec+ data.extend_from_slice(&(payload.len() as u32).to_le_bytes()); // incl_len+ data.extend_from_slice(&(payload.len() as u32).to_le_bytes()); // orig_len+ data.extend_from_slice(payload);+ }++ fn global_header(link_type: u32) -> Vec<u8> {+ let mut header = Vec::new();+ header.extend_from_slice(&0xa1b2_c3d4u32.to_le_bytes()); // magic (little-endian)+ header.extend_from_slice(&2u16.to_le_bytes()); // version major+ header.extend_from_slice(&4u16.to_le_bytes()); // version minor+ header.extend_from_slice(&0i32.to_le_bytes()); // thiszone+ header.extend_from_slice(&0u32.to_le_bytes()); // sigfigs+ header.extend_from_slice(&65535u32.to_le_bytes()); // snaplen+ header.extend_from_slice(&link_type.to_le_bytes()); // network+ header+ }++ #[test]+ fn pcap_reader_reads_records() {+ let mut data = global_header(LINKTYPE_IEEE802_11);+ push_record(&mut data, &[1, 2, 3]);+ push_record(&mut data, &[4, 5]);++ let mut reader = PcapReader::new(&data).expect("valid header");+ assert_eq!(reader.link_type, LINKTYPE_IEEE802_11);+ assert_eq!(reader.next_frame(), Some(&[1u8, 2, 3][..]));+ assert_eq!(reader.next_frame(), Some(&[4u8, 5][..]));+ assert_eq!(reader.next_frame(), None);+ }++ #[test]+ fn pcap_reader_drops_a_truncated_trailing_record() {+ // A live capture read mid-write can end with a partially-written record.+ let mut data = global_header(LINKTYPE_IEEE802_11_RADIOTAP);+ push_record(&mut data, &[1, 2, 3]);+ data.extend_from_slice(&0u32.to_le_bytes()); // ts_sec+ data.extend_from_slice(&0u32.to_le_bytes()); // ts_usec+ data.extend_from_slice(&10u32.to_le_bytes()); // incl_len claims 10 bytes+ data.extend_from_slice(&10u32.to_le_bytes());+ data.extend_from_slice(&[9, 9]); // ...but only 2 are present++ let mut reader = PcapReader::new(&data).expect("valid header");+ assert_eq!(reader.next_frame(), Some(&[1u8, 2, 3][..]));+ assert_eq!(reader.next_frame(), None); // truncated record ignored, no panic+ }++ #[test]+ fn rejects_non_pcap_data() {+ assert!(PcapReader::new(&[0u8; 4]).is_none()); // too short+ assert!(PcapReader::new(&[0u8; 24]).is_none()); // right size, bad magic+ }}