| 1 | |
| 2 | |
| 3 | |
| 4 | use std::path::{Path, PathBuf}; |
| 5 | use std::time::{Duration, SystemTime, UNIX_EPOCH}; |
| 6 | |
| 7 | use gpui::{ |
| 8 | Context, Entity, IntoElement, ParentElement, Styled, Window, div, prelude::FluentBuilder, px, |
| 9 | }; |
| 10 | use gpuikit::elements::badge::badge; |
| 11 | use gpuikit::elements::button::button; |
| 12 | use gpuikit::elements::card::card; |
| 13 | use gpuikit::elements::loading_indicator::loading_indicator; |
| 14 | use gpuikit::elements::text_field::text_field; |
| 15 | use gpuikit::elements::typography::{h3, small, text}; |
| 16 | use gpuikit::layout::{h_stack, v_stack}; |
| 17 | use gpuikit::theme::{ActiveTheme, Themeable}; |
| 18 | |
| 19 | use crate::common::{Cmd, Event, HandshakeCapture, MacAddr, PmkidCapture}; |
| 20 | use crate::ui::app::AppRoot; |
| 21 | use crate::ui::mock; |
| 22 | |
| 23 | fn unix_secs() -> u64 { |
| 24 | SystemTime::now() |
| 25 | .duration_since(UNIX_EPOCH) |
| 26 | .map(|d| d.as_secs()) |
| 27 | .unwrap_or(0) |
| 28 | } |
| 29 | |
| 30 | fn hex(bytes: &[u8]) -> String { |
| 31 | let mut out = String::with_capacity(bytes.len() * 2); |
| 32 | for b in bytes { |
| 33 | out.push_str(&format!("{b:02x}")); |
| 34 | } |
| 35 | out |
| 36 | } |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | fn export_hc22000(view: &Entity<AppRoot>, _window: &mut Window, cx: &mut gpui::App) { |
| 42 | let (lines, lossless_note) = { |
| 43 | let app = view.read(cx); |
| 44 | let mut lines: Vec<String> = app |
| 45 | .state |
| 46 | .handshakes |
| 47 | .iter() |
| 48 | .map(crate::handshake::handshake_hc22000) |
| 49 | .chain(app.state.pmkids.iter().map(crate::handshake::pmkid_hc22000)) |
| 50 | .collect(); |
| 51 | lines.sort_unstable(); |
| 52 | lines.dedup(); |
| 53 | (lines, app.state.handshakes.len() + app.state.pmkids.len()) |
| 54 | }; |
| 55 | if lines.is_empty() { |
| 56 | view.update(cx, |app, cx| { |
| 57 | app.state.push_log("export: no captures yet"); |
| 58 | cx.notify(); |
| 59 | }); |
| 60 | return; |
| 61 | } |
| 62 | |
| 63 | let suggested = format!("wifilab-{}.hc22000", unix_secs()); |
| 64 | let prompt = cx.prompt_for_new_path(Path::new("./captures"), Some(&suggested)); |
| 65 | let body = lines.join("\n") + "\n"; |
| 66 | let weak = view.downgrade(); |
| 67 | cx.spawn(async move |cx| { |
| 68 | |
| 69 | let path: Option<PathBuf> = match prompt.await { |
| 70 | Ok(Ok(Some(p))) => Some(p), |
| 71 | Ok(Ok(None)) => None, |
| 72 | _ => Some(PathBuf::from("./captures").join(&suggested)), |
| 73 | }; |
| 74 | let _ = weak.update(cx, |app, cx| { |
| 75 | match path { |
| 76 | None => app.state.push_log("export: cancelled"), |
| 77 | Some(path) => { |
| 78 | if let Some(parent) = path.parent() { |
| 79 | let _ = std::fs::create_dir_all(parent); |
| 80 | } |
| 81 | match std::fs::write(&path, &body) { |
| 82 | Ok(()) => app |
| 83 | .state |
| 84 | .push_log(format!("export: {} captures → {}", lossless_note, path.display())), |
| 85 | Err(e) => app |
| 86 | .state |
| 87 | .push_log(format!("export FAILED: {e} ({})", path.display())), |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | cx.notify(); |
| 92 | }); |
| 93 | }) |
| 94 | .detach(); |
| 95 | } |
| 96 | |
| 97 | |
| 98 | fn target_info(app: &AppRoot) -> Option<(MacAddr, u8, String)> { |
| 99 | let ap = app.selected?; |
| 100 | let row = app.state.aps.get(&ap)?; |
| 101 | Some((ap, row.channel, row.ssid.clone())) |
| 102 | } |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | fn capture_handshake(view: &Entity<AppRoot>, _window: &mut Window, cx: &mut gpui::App) { |
| 109 | view.update(cx, |app, cx| { |
| 110 | let Some(ap) = app.selected else { |
| 111 | app.state.push_log("handshake: no target selected"); |
| 112 | cx.notify(); |
| 113 | return; |
| 114 | }; |
| 115 | if app.attacking_handshake { |
| 116 | return; |
| 117 | } |
| 118 | if !app.is_mock && !app.state.iface_running { |
| 119 | app.state |
| 120 | .push_log("handshake: start capture first (attacks need the radio in monitor mode)"); |
| 121 | cx.notify(); |
| 122 | return; |
| 123 | } |
| 124 | app.attacking_handshake = true; |
| 125 | cx.notify(); |
| 126 | |
| 127 | if !app.is_mock { |
| 128 | let Some((ap, channel, essid)) = target_info(app) else { |
| 129 | app.state |
| 130 | .push_log("handshake: target AP not in scan table yet"); |
| 131 | app.attacking_handshake = false; |
| 132 | cx.notify(); |
| 133 | return; |
| 134 | }; |
| 135 | app.state.push_log(format!( |
| 136 | "handshake capture on {essid} ({ap}, ch {channel})…" |
| 137 | )); |
| 138 | if let Some(svc) = &app.service { |
| 139 | svc.cmd(&Cmd::CaptureHs { ap, channel, essid }); |
| 140 | } |
| 141 | cx.notify(); |
| 142 | return; |
| 143 | } |
| 144 | |
| 145 | |
| 146 | let essid = app |
| 147 | .state |
| 148 | .aps |
| 149 | .get(&ap) |
| 150 | .map(|a| a.ssid.clone()) |
| 151 | .unwrap_or_else(|| ap.to_string()); |
| 152 | |
| 153 | let client = app |
| 154 | .state |
| 155 | .clients |
| 156 | .values() |
| 157 | .find(|c| c.ap == Some(ap)) |
| 158 | .map(|c| c.mac) |
| 159 | .unwrap_or(mock::MOCK_CLIENTS[0]); |
| 160 | app.state |
| 161 | .push_log(format!("handshake capture on {essid} ({ap}), targeting {client}…")); |
| 162 | cx.notify(); |
| 163 | |
| 164 | let tx = app.tx.clone(); |
| 165 | let essid_ev = essid.clone(); |
| 166 | let salt = unix_secs(); |
| 167 | cx.spawn(async move |this, cx| { |
| 168 | let _ = tx.send(Event::HandshakeProgress { |
| 169 | ap, |
| 170 | client, |
| 171 | have: 0b0001, |
| 172 | }); |
| 173 | cx.background_executor() |
| 174 | .timer(Duration::from_millis(900)) |
| 175 | .await; |
| 176 | let _ = tx.send(Event::HandshakeProgress { |
| 177 | ap, |
| 178 | client, |
| 179 | have: 0b0011, |
| 180 | }); |
| 181 | cx.background_executor() |
| 182 | .timer(Duration::from_millis(900)) |
| 183 | .await; |
| 184 | let cap = mock::fabricate_handshake(ap, client, &essid_ev, salt); |
| 185 | let _ = tx.send(Event::HandshakeComplete(Box::new(cap))); |
| 186 | let _ = this.update(cx, |app, cx| { |
| 187 | app.attacking_handshake = false; |
| 188 | cx.notify(); |
| 189 | }); |
| 190 | }) |
| 191 | .detach(); |
| 192 | }); |
| 193 | } |
| 194 | |
| 195 | |
| 196 | fn pmkid_attack(view: &Entity<AppRoot>, _window: &mut Window, cx: &mut gpui::App) { |
| 197 | view.update(cx, |app, cx| { |
| 198 | let Some(ap) = app.selected else { |
| 199 | app.state.push_log("pmkid: no target selected"); |
| 200 | cx.notify(); |
| 201 | return; |
| 202 | }; |
| 203 | if app.attacking_pmkid { |
| 204 | return; |
| 205 | } |
| 206 | if !app.is_mock && !app.state.iface_running { |
| 207 | app.state |
| 208 | .push_log("pmkid: start capture first (attacks need the radio in monitor mode)"); |
| 209 | cx.notify(); |
| 210 | return; |
| 211 | } |
| 212 | app.attacking_pmkid = true; |
| 213 | cx.notify(); |
| 214 | |
| 215 | if !app.is_mock { |
| 216 | let Some((ap, channel, essid)) = target_info(app) else { |
| 217 | app.state |
| 218 | .push_log("pmkid: target AP not in scan table yet"); |
| 219 | app.attacking_pmkid = false; |
| 220 | cx.notify(); |
| 221 | return; |
| 222 | }; |
| 223 | app.state |
| 224 | .push_log(format!("pmkid attack on {essid} ({ap}, ch {channel})…")); |
| 225 | if let Some(svc) = &app.service { |
| 226 | svc.cmd(&Cmd::Pmkid { ap, channel, essid }); |
| 227 | } |
| 228 | cx.notify(); |
| 229 | return; |
| 230 | } |
| 231 | |
| 232 | |
| 233 | let essid = app |
| 234 | .state |
| 235 | .aps |
| 236 | .get(&ap) |
| 237 | .map(|a| a.ssid.clone()) |
| 238 | .unwrap_or_else(|| ap.to_string()); |
| 239 | app.state |
| 240 | .push_log(format!("pmkid attack on {essid} ({ap})…")); |
| 241 | cx.notify(); |
| 242 | |
| 243 | let tx = app.tx.clone(); |
| 244 | let salt = unix_secs() ^ 0x77; |
| 245 | cx.spawn(async move |this, cx| { |
| 246 | cx.background_executor() |
| 247 | .timer(Duration::from_millis(1200)) |
| 248 | .await; |
| 249 | let cap = mock::fabricate_pmkid(ap, mock::MOCK_CLIENTS[3], &essid, salt); |
| 250 | let _ = tx.send(Event::PmkidCaptured(cap)); |
| 251 | let _ = this.update(cx, |app, cx| { |
| 252 | app.attacking_pmkid = false; |
| 253 | cx.notify(); |
| 254 | }); |
| 255 | }) |
| 256 | .detach(); |
| 257 | }); |
| 258 | } |
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | fn deauth(view: &Entity<AppRoot>, _window: &mut Window, cx: &mut gpui::App) { |
| 264 | view.update(cx, |app, cx| { |
| 265 | let Some(ap) = app.selected else { |
| 266 | app.state.push_log("deauth: no target selected"); |
| 267 | cx.notify(); |
| 268 | return; |
| 269 | }; |
| 270 | let count64: u64 = app |
| 271 | .deauth_input |
| 272 | .read(cx) |
| 273 | .content() |
| 274 | .trim() |
| 275 | .parse() |
| 276 | .unwrap_or(16); |
| 277 | |
| 278 | if !app.is_mock { |
| 279 | if !app.state.iface_running { |
| 280 | app.state |
| 281 | .push_log("deauth: start capture first (injection needs monitor mode)"); |
| 282 | cx.notify(); |
| 283 | return; |
| 284 | } |
| 285 | let Some(svc) = &app.service else { |
| 286 | app.state |
| 287 | .push_log("deauth: start capture first (the service owns the radio)"); |
| 288 | cx.notify(); |
| 289 | return; |
| 290 | }; |
| 291 | let Some((ap, channel, _essid)) = target_info(app) else { |
| 292 | app.state |
| 293 | .push_log("deauth: target AP not in scan table yet"); |
| 294 | cx.notify(); |
| 295 | return; |
| 296 | }; |
| 297 | |
| 298 | |
| 299 | |
| 300 | let target = app |
| 301 | .selected_client |
| 302 | .filter(|mac| { |
| 303 | app.state |
| 304 | .clients |
| 305 | .get(mac) |
| 306 | .is_some_and(|c| c.ap == Some(ap)) |
| 307 | }) |
| 308 | .unwrap_or(MacAddr::BROADCAST); |
| 309 | let count = count64.min(u32::MAX as u64) as u32; |
| 310 | app.state.push_log(format!( |
| 311 | "deauth: {count} rounds → {target} (AP {ap}, ch {channel})" |
| 312 | )); |
| 313 | svc.cmd(&Cmd::Deauth { |
| 314 | ap, |
| 315 | target, |
| 316 | channel, |
| 317 | count, |
| 318 | }); |
| 319 | cx.notify(); |
| 320 | return; |
| 321 | } |
| 322 | |
| 323 | let _ = app.tx.send(Event::DeauthSent { |
| 324 | ap, |
| 325 | target: MacAddr::BROADCAST, |
| 326 | count: count64, |
| 327 | }); |
| 328 | cx.notify(); |
| 329 | }); |
| 330 | } |
| 331 | |
| 332 | fn kv(label: &str, value: impl Into<String>) -> gpui::Div { |
| 333 | h_stack() |
| 334 | .gap_2() |
| 335 | .child(div().w(px(90.)).text_xs().child(label.to_string())) |
| 336 | .child(text(value.into()).code().small().into_any_element()) |
| 337 | } |
| 338 | |
| 339 | fn handshake_card(hs: &HandshakeCapture, crackable: bool) -> gpuikit::elements::card::Card { |
| 340 | card() |
| 341 | .title(format!("{} — handshake", hs.essid)) |
| 342 | .description(format!("{} ⇄ {}", hs.ap, hs.client)) |
| 343 | .body( |
| 344 | v_stack() |
| 345 | .gap_1() |
| 346 | .child(kv("key version", format!("{}", hs.key_ver))) |
| 347 | .child(kv("anonce", hex(&hs.anonce[..8]))) |
| 348 | .child( |
| 349 | h_stack().gap_2().child(if crackable { |
| 350 | badge("crackable").secondary() |
| 351 | } else { |
| 352 | badge("partial") |
| 353 | }), |
| 354 | ) |
| 355 | .into_any_element(), |
| 356 | ) |
| 357 | } |
| 358 | |
| 359 | fn pmkid_card(pm: &PmkidCapture) -> gpuikit::elements::card::Card { |
| 360 | card() |
| 361 | .title(format!("{} — PMKID", pm.essid)) |
| 362 | .description(format!("AP {}", pm.ap)) |
| 363 | .body( |
| 364 | v_stack() |
| 365 | .gap_1() |
| 366 | .child(kv("pmkid", hex(&pm.pmkid))) |
| 367 | .child(kv("via (our mac)", pm.client.to_string())) |
| 368 | .child(h_stack().gap_2().child(badge("crackable").secondary())) |
| 369 | .into_any_element(), |
| 370 | ) |
| 371 | } |
| 372 | |
| 373 | pub fn render( |
| 374 | app: &mut AppRoot, |
| 375 | view: &Entity<AppRoot>, |
| 376 | _window: &mut Window, |
| 377 | cx: &mut Context<AppRoot>, |
| 378 | ) -> gpui::Div { |
| 379 | let theme = cx.theme().clone(); |
| 380 | |
| 381 | let target_block = match app.selected.and_then(|b| { |
| 382 | app.state |
| 383 | .aps |
| 384 | .get(&b) |
| 385 | .map(|ap| (ap.ssid.clone(), b, ap.channel)) |
| 386 | }) { |
| 387 | Some((ssid, bssid, ch)) => h_stack() |
| 388 | .gap_3() |
| 389 | .items_center() |
| 390 | .child(h3(format!("target: {ssid}"))) |
| 391 | .child(text(format!("{bssid} · ch {ch}")).code().small().into_any_element()) |
| 392 | .into_any_element(), |
| 393 | None => h_stack() |
| 394 | .gap_3() |
| 395 | .items_center() |
| 396 | .child(h3("target: none selected")) |
| 397 | .child(small("pick an AP on the Networks page")) |
| 398 | .into_any_element(), |
| 399 | }; |
| 400 | |
| 401 | let no_target = app.selected.is_none(); |
| 402 | |
| 403 | |
| 404 | let not_running = !app.is_mock && !app.state.iface_running; |
| 405 | let blocked = no_target || not_running; |
| 406 | let hs_running = app.attacking_handshake; |
| 407 | let pm_running = app.attacking_pmkid; |
| 408 | |
| 409 | let actions = h_stack() |
| 410 | .gap_3() |
| 411 | .items_center() |
| 412 | .child( |
| 413 | button("btn-handshake", "Capture Handshake") |
| 414 | .disabled(blocked || hs_running) |
| 415 | .on_click({ |
| 416 | let view = view.clone(); |
| 417 | move |_, window, cx| capture_handshake(&view, window, cx) |
| 418 | }), |
| 419 | ) |
| 420 | .child( |
| 421 | button("btn-pmkid", "PMKID Attack") |
| 422 | .disabled(blocked || pm_running) |
| 423 | .on_click({ |
| 424 | let view = view.clone(); |
| 425 | move |_, window, cx| pmkid_attack(&view, window, cx) |
| 426 | }), |
| 427 | ) |
| 428 | .child( |
| 429 | button("btn-deauth", "Deauth") |
| 430 | .destructive() |
| 431 | .disabled(blocked) |
| 432 | .on_click({ |
| 433 | let view = view.clone(); |
| 434 | move |_, window, cx| deauth(&view, window, cx) |
| 435 | }), |
| 436 | ) |
| 437 | .child( |
| 438 | div() |
| 439 | .w(px(90.)) |
| 440 | .child(text_field(&app.deauth_input, cx).placeholder("count")), |
| 441 | ) |
| 442 | .when(hs_running || pm_running, |el| { |
| 443 | el.child(loading_indicator().small()).child(small("working…")) |
| 444 | }) |
| 445 | .when(not_running, |el| { |
| 446 | el.child(small("start capture first to enable attacks")) |
| 447 | }); |
| 448 | |
| 449 | |
| 450 | let progress_cards: Vec<gpui::AnyElement> = app |
| 451 | .hs_progress |
| 452 | .iter() |
| 453 | .map(|((ap, client), have)| { |
| 454 | card() |
| 455 | .title(format!("collecting 4-way… {}/4", have.count_ones())) |
| 456 | .description(format!("{ap} ⇄ {client}")) |
| 457 | .body( |
| 458 | h_stack() |
| 459 | .gap_2() |
| 460 | .child(loading_indicator().small()) |
| 461 | .into_any_element(), |
| 462 | ) |
| 463 | .into_any_element() |
| 464 | }) |
| 465 | .collect(); |
| 466 | |
| 467 | let mut page = v_stack() |
| 468 | .size_full() |
| 469 | .gap_3() |
| 470 | .p_4() |
| 471 | .child(target_block) |
| 472 | .child(actions) |
| 473 | .children(progress_cards) |
| 474 | .child(gpuikit::elements::separator::separator()) |
| 475 | .child( |
| 476 | h_stack() |
| 477 | .items_center() |
| 478 | .gap_3() |
| 479 | .child(h3("Captures")) |
| 480 | .child(small(format!( |
| 481 | "{} handshakes · {} PMKIDs", |
| 482 | app.state.handshakes.len(), |
| 483 | app.state.pmkids.len() |
| 484 | ))) |
| 485 | .child(div().flex_1()) |
| 486 | .child( |
| 487 | button("btn-export", "Export .hc22000").on_click({ |
| 488 | let view = view.clone(); |
| 489 | move |_, window, cx| export_hc22000(&view, window, cx) |
| 490 | }), |
| 491 | ), |
| 492 | ); |
| 493 | |
| 494 | let cards: Vec<gpui::AnyElement> = app |
| 495 | .state |
| 496 | .handshakes |
| 497 | .iter() |
| 498 | .map(|hs| handshake_card(hs, true).into_any_element()) |
| 499 | .chain(app.state.pmkids.iter().map(|pm| pmkid_card(pm).into_any_element())) |
| 500 | .collect(); |
| 501 | if cards.is_empty() { |
| 502 | page = page.child(small("nothing captured yet — run an attack")); |
| 503 | } else { |
| 504 | page = page.child(v_stack().gap_2().children(cards)); |
| 505 | } |
| 506 | |
| 507 | page.text_color(theme.fg()) |
| 508 | } |