diff --git a/docs/USAGE.md b/docs/USAGE.md index 665359f..8bdab5f 100644 --- a/docs/USAGE.md +++ b/docs/USAGE.md @@ -5,13 +5,20 @@ crack it on the GPU (CPU fallback). Single binary, GPUI shell. ## Running -The real backend is the default and requires root: +No terminal privileges needed. Start the app normally: ```sh cargo build --release -sudo ./target/release/wifilab --iface wlp14s0 +./target/release/wifilab ``` +Privileges are handled **inside the UI**: when not running as root the +header shows a **"Elevate via polkit"** button — one click pops the +system polkit dialog, and the app restarts itself elevated (same window, +same selected interface). The unprivileged window closes automatically +once the elevated instance is up. Starting a capture while unprivileged +just logs a reminder; nothing fails mid-attack. + - `--iface ` picks the wireless interface (default: first detected wireless interface, falling back to `wlp14s0`). The header bar's picker can also switch interfaces while capture is stopped. diff --git a/src/iface.rs b/src/iface.rs index f65b342..1456f55 100644 --- a/src/iface.rs +++ b/src/iface.rs @@ -170,6 +170,47 @@ pub fn is_root() -> bool { (unsafe { libc::geteuid() }) == 0 } +/// True when `pkexec` (polkit) is available for GUI privilege elevation. +pub fn pkexec_available() -> bool { + Command::new("pkexec") + .arg("--version") + .stdout(std::process::Stdio::null()) + .stderr(std::process::Stdio::null()) + .status() + .map(|s| s.success()) + .unwrap_or(false) +} + +/// Re-exec this binary via polkit (`pkexec`) as root, preserving the desktop +/// session environment so the elevated instance can open its own window. +/// +/// Blocks until `pkexec` returns. A dismissed/cancelled authentication +/// dialog surfaces as a non-zero exit status (126/127). MUST be called off +/// the UI thread. +pub fn relaunch_elevated(extra_args: &[&str]) -> Result { + let exe = std::env::current_exe().context("resolving current_exe for pkexec re-exec")?; + let mut cmd = Command::new("pkexec"); + cmd.arg("env"); + // GUI + session bus env the elevated child needs to open a window and + // talk to the user's services (GPU/DRI inherits via the session). + for var in [ + "WAYLAND_DISPLAY", + "XDG_RUNTIME_DIR", + "DISPLAY", + "DBUS_SESSION_BUS_ADDRESS", + "XAUTHORITY", + ] { + if let Ok(v) = std::env::var(var) { + cmd.arg(format!("{var}={v}")); + } + } + cmd.arg(exe).args(extra_args); + let status = cmd + .status() + .context("spawning pkexec — polkit not installed or no agent running")?; + Ok(status) +} + #[cfg(test)] mod tests { use super::*; diff --git a/src/main.rs b/src/main.rs index 593eebd..6ac1cdd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -51,7 +51,7 @@ fn main() { } else if iface::is_root() { "monitor mode + injection available" } else { - "WARNING: not root, monitor mode and injection will fail (run with sudo)" + "not root — use the \"Elevate via polkit\" button in the UI header" } ); ui::run_app(cli.iface, cli.mock); diff --git a/src/ui/app.rs b/src/ui/app.rs index e566465..3024f60 100644 --- a/src/ui/app.rs +++ b/src/ui/app.rs @@ -3,6 +3,8 @@ //! task that polls the backend event channel. use std::collections::BTreeMap; +use std::sync::Arc; +use std::sync::atomic::{AtomicU8, Ordering}; use std::time::Duration; use crossbeam_channel::{Receiver, Sender}; @@ -79,6 +81,10 @@ pub struct AppRoot { /// Toast queued by the event loop (no Window there) — shown on the /// next render pass. pub pending_toast: Option, + /// Elevate-via-polkit handshake: Some while a pkexec re-exec is in + /// flight. 0 = waiting on the polkit dialog, 1 = elevated instance + /// launched (quit this one), 2 = declined/failed (message was logged). + pub elevation: Option>, /// Used to invalidate stale cracker simulation ticks after Stop/re-Start. pub crack_epoch: u64, pub ap_sort: Option, @@ -151,6 +157,7 @@ impl AppRoot { attacking_pmkid: false, crack_inflight: false, pending_toast: None, + elevation: None, crack_epoch: 0, ap_sort: None, client_sort: None, @@ -181,9 +188,14 @@ impl AppRoot { }) .detach(); - if !this.is_root { - cx.toast("not root: monitor mode and injection will fail — run with sudo") - .show(window, cx); + if !this.is_root && !this.is_mock { + if crate::iface::pkexec_available() { + cx.toast("not root — click \"Elevate via polkit\" in the header to gain privileges") + .show(window, cx); + } else { + cx.toast("not root and polkit (pkexec) unavailable — monitor mode and injection will fail") + .show(window, cx); + } } // Poll the backend event channel: wait on the background executor, @@ -233,6 +245,13 @@ impl AppRoot { self.state.push_log("ui: capture stopped, interface restored"); cx.notify(); } else { + if !self.is_root { + self.state.push_log( + "ui: capture needs root — click \"Elevate via polkit\" in the header first".to_string(), + ); + cx.notify(); + return; + } let ifname = self.state.ifname.clone(); match Backend::start_scan(&ifname, self.tx.clone()) { Ok(be) => { @@ -250,9 +269,61 @@ impl AppRoot { } } + /// Re-exec this app as root via polkit (pkexec). The polkit dialog is + /// fully OS-driven; on success the elevated instance opens its own + /// window and this one closes itself. + pub fn elevate(&mut self, cx: &mut Context) { + if self.is_root || self.is_mock || self.elevation.is_some() { + return; + } + if self.backend.is_some() { + self.state.push_log("ui: stop capture before elevating"); + cx.notify(); + return; + } + let flag = Arc::new(AtomicU8::new(0)); + self.elevation = Some(flag.clone()); + let ifname = self.state.ifname.clone(); + let tx = self.tx.clone(); + self.state.push_log(format!( + "ui: requesting administrative privileges via polkit (restarting elevated on {ifname})…" + )); + cx.notify(); + std::thread::spawn(move || { + match crate::iface::relaunch_elevated(&["--iface", ifname.as_str()]) { + Ok(st) if st.success() => flag.store(1, Ordering::SeqCst), + Ok(st) => { + flag.store(2, Ordering::SeqCst); + let _ = tx.send(Event::Log(format!( + "ui: elevation dialog dismissed or failed (pkexec exit {st})" + ))); + } + Err(e) => { + flag.store(2, Ordering::SeqCst); + let _ = tx.send(Event::Error(format!("polkit elevation failed: {e:#}"))); + } + } + }); + } + /// Drain every queued event, track handshake progress, refresh widgets /// derived from the capture set. fn drain_events(&mut self, cx: &mut Context) { + // Elevation handshake: success → the new elevated window is up, + // close this one; failure → allow retry (message already queued). + if let Some(flag) = &self.elevation { + match flag.load(Ordering::SeqCst) { + 1 => { + self.state + .push_log("ui: elevated instance started — closing this window"); + self.elevation = None; + cx.quit(); + return; + } + 2 => self.elevation = None, + _ => {} + } + } let mut changed = false; let mut captures_changed = false; while let Ok(event) = self.rx.try_recv() { @@ -370,10 +441,32 @@ impl AppRoot { let privilege = if self.is_root { badge("root").secondary() + } else if self.is_mock { + badge("not root (mock)").secondary() } else { - badge("not root — attacks need sudo").destructive() + badge("not root — elevate to attack").destructive() }; + let elevate_btn = (!self.is_root && !self.is_mock && crate::iface::pkexec_available()) + .then(|| { + let waiting = self.elevation.is_some(); + button( + "elevate-btn", + if waiting { + "waiting for polkit…" + } else { + "Elevate via polkit" + }, + ) + .disabled(waiting) + .on_click({ + let view = view.clone(); + move |_, _window, cx| { + view.update(cx, |app, cx| app.elevate(cx)); + } + }) + }); + let toggle = button( "monitor-toggle", if running { @@ -405,6 +498,7 @@ impl AppRoot { .bg(theme.surface()) .child(div().text_lg().font_weight(FontWeight::BOLD).child("wifilab")) .child(privilege) + .children(elevate_btn) .child(div().w(px(220.)).child(self.iface_select.clone())) .child(toggle) .child(div().flex_1())