forge
Explore
Log in

Don't you dare go Hollow

polkit elevation from UI: pkexec re-exec button, capture gating, no-CLI flow

Browse files at this commit

35195ecb1c585b1cdbd8c3cee1a442c497f55146Ahmet Kaan GümüşParent: 108d870
docs/USAGE.mdmodified+9 -2
@@ -5,13 +5,20 @@ crack it on the GPU (CPU fallback). Single binary, GPUI shell.
55
66 ## Running
77
8-The real backend is the default and requires root:
8+No terminal privileges needed. Start the app normally:
99
1010 ```sh
1111 cargo build --release
12-sudo ./target/release/wifilab --iface wlp14s0
12+./target/release/wifilab
1313 ```
1414
15+Privileges are handled **inside the UI**: when not running as root the
16+header shows a **"Elevate via polkit"** button — one click pops the
17+system polkit dialog, and the app restarts itself elevated (same window,
18+same selected interface). The unprivileged window closes automatically
19+once the elevated instance is up. Starting a capture while unprivileged
20+just logs a reminder; nothing fails mid-attack.
21+
1522 - `--iface <name>` picks the wireless interface (default: first detected
1623 wireless interface, falling back to `wlp14s0`). The header bar's picker
1724 can also switch interfaces while capture is stopped.
src/iface.rsmodified+41 -0
@@ -170,6 +170,47 @@ pub fn is_root() -> bool {
170170 (unsafe { libc::geteuid() }) == 0
171171 }
172172
173+/// True when `pkexec` (polkit) is available for GUI privilege elevation.
174+pub fn pkexec_available() -> bool {
175+ Command::new("pkexec")
176+ .arg("--version")
177+ .stdout(std::process::Stdio::null())
178+ .stderr(std::process::Stdio::null())
179+ .status()
180+ .map(|s| s.success())
181+ .unwrap_or(false)
182+}
183+
184+/// Re-exec this binary via polkit (`pkexec`) as root, preserving the desktop
185+/// session environment so the elevated instance can open its own window.
186+///
187+/// Blocks until `pkexec` returns. A dismissed/cancelled authentication
188+/// dialog surfaces as a non-zero exit status (126/127). MUST be called off
189+/// the UI thread.
190+pub fn relaunch_elevated(extra_args: &[&str]) -> Result<std::process::ExitStatus> {
191+ let exe = std::env::current_exe().context("resolving current_exe for pkexec re-exec")?;
192+ let mut cmd = Command::new("pkexec");
193+ cmd.arg("env");
194+ // GUI + session bus env the elevated child needs to open a window and
195+ // talk to the user's services (GPU/DRI inherits via the session).
196+ for var in [
197+ "WAYLAND_DISPLAY",
198+ "XDG_RUNTIME_DIR",
199+ "DISPLAY",
200+ "DBUS_SESSION_BUS_ADDRESS",
201+ "XAUTHORITY",
202+ ] {
203+ if let Ok(v) = std::env::var(var) {
204+ cmd.arg(format!("{var}={v}"));
205+ }
206+ }
207+ cmd.arg(exe).args(extra_args);
208+ let status = cmd
209+ .status()
210+ .context("spawning pkexec — polkit not installed or no agent running")?;
211+ Ok(status)
212+}
213+
173214 #[cfg(test)]
174215 mod tests {
175216 use super::*;
src/main.rsmodified+1 -1
@@ -51,7 +51,7 @@ fn main() {
5151 } else if iface::is_root() {
5252 "monitor mode + injection available"
5353 } else {
54- "WARNING: not root, monitor mode and injection will fail (run with sudo)"
54+ "not root — use the \"Elevate via polkit\" button in the UI header"
5555 }
5656 );
5757 ui::run_app(cli.iface, cli.mock);
src/ui/app.rsmodified+98 -4
@@ -3,6 +3,8 @@
33 //! task that polls the backend event channel.
44
55 use std::collections::BTreeMap;
6+use std::sync::Arc;
7+use std::sync::atomic::{AtomicU8, Ordering};
68 use std::time::Duration;
79
810 use crossbeam_channel::{Receiver, Sender};
@@ -79,6 +81,10 @@ pub struct AppRoot {
7981 /// Toast queued by the event loop (no Window there) — shown on the
8082 /// next render pass.
8183 pub pending_toast: Option<SharedString>,
84+ /// Elevate-via-polkit handshake: Some while a pkexec re-exec is in
85+ /// flight. 0 = waiting on the polkit dialog, 1 = elevated instance
86+ /// launched (quit this one), 2 = declined/failed (message was logged).
87+ pub elevation: Option<Arc<AtomicU8>>,
8288 /// Used to invalidate stale cracker simulation ticks after Stop/re-Start.
8389 pub crack_epoch: u64,
8490 pub ap_sort: Option<SortDescriptor>,
@@ -151,6 +157,7 @@ impl AppRoot {
151157 attacking_pmkid: false,
152158 crack_inflight: false,
153159 pending_toast: None,
160+ elevation: None,
154161 crack_epoch: 0,
155162 ap_sort: None,
156163 client_sort: None,
@@ -181,9 +188,14 @@ impl AppRoot {
181188 })
182189 .detach();
183190
184- if !this.is_root {
185- cx.toast("not root: monitor mode and injection will fail — run with sudo")
186- .show(window, cx);
191+ if !this.is_root && !this.is_mock {
192+ if crate::iface::pkexec_available() {
193+ cx.toast("not root — click \"Elevate via polkit\" in the header to gain privileges")
194+ .show(window, cx);
195+ } else {
196+ cx.toast("not root and polkit (pkexec) unavailable — monitor mode and injection will fail")
197+ .show(window, cx);
198+ }
187199 }
188200
189201 // Poll the backend event channel: wait on the background executor,
@@ -233,6 +245,13 @@ impl AppRoot {
233245 self.state.push_log("ui: capture stopped, interface restored");
234246 cx.notify();
235247 } else {
248+ if !self.is_root {
249+ self.state.push_log(
250+ "ui: capture needs root — click \"Elevate via polkit\" in the header first".to_string(),
251+ );
252+ cx.notify();
253+ return;
254+ }
236255 let ifname = self.state.ifname.clone();
237256 match Backend::start_scan(&ifname, self.tx.clone()) {
238257 Ok(be) => {
@@ -250,9 +269,61 @@ impl AppRoot {
250269 }
251270 }
252271
272+ /// Re-exec this app as root via polkit (pkexec). The polkit dialog is
273+ /// fully OS-driven; on success the elevated instance opens its own
274+ /// window and this one closes itself.
275+ pub fn elevate(&mut self, cx: &mut Context<Self>) {
276+ if self.is_root || self.is_mock || self.elevation.is_some() {
277+ return;
278+ }
279+ if self.backend.is_some() {
280+ self.state.push_log("ui: stop capture before elevating");
281+ cx.notify();
282+ return;
283+ }
284+ let flag = Arc::new(AtomicU8::new(0));
285+ self.elevation = Some(flag.clone());
286+ let ifname = self.state.ifname.clone();
287+ let tx = self.tx.clone();
288+ self.state.push_log(format!(
289+ "ui: requesting administrative privileges via polkit (restarting elevated on {ifname})…"
290+ ));
291+ cx.notify();
292+ std::thread::spawn(move || {
293+ match crate::iface::relaunch_elevated(&["--iface", ifname.as_str()]) {
294+ Ok(st) if st.success() => flag.store(1, Ordering::SeqCst),
295+ Ok(st) => {
296+ flag.store(2, Ordering::SeqCst);
297+ let _ = tx.send(Event::Log(format!(
298+ "ui: elevation dialog dismissed or failed (pkexec exit {st})"
299+ )));
300+ }
301+ Err(e) => {
302+ flag.store(2, Ordering::SeqCst);
303+ let _ = tx.send(Event::Error(format!("polkit elevation failed: {e:#}")));
304+ }
305+ }
306+ });
307+ }
308+
253309 /// Drain every queued event, track handshake progress, refresh widgets
254310 /// derived from the capture set.
255311 fn drain_events(&mut self, cx: &mut Context<Self>) {
312+ // Elevation handshake: success → the new elevated window is up,
313+ // close this one; failure → allow retry (message already queued).
314+ if let Some(flag) = &self.elevation {
315+ match flag.load(Ordering::SeqCst) {
316+ 1 => {
317+ self.state
318+ .push_log("ui: elevated instance started — closing this window");
319+ self.elevation = None;
320+ cx.quit();
321+ return;
322+ }
323+ 2 => self.elevation = None,
324+ _ => {}
325+ }
326+ }
256327 let mut changed = false;
257328 let mut captures_changed = false;
258329 while let Ok(event) = self.rx.try_recv() {
@@ -370,10 +441,32 @@ impl AppRoot {
370441
371442 let privilege = if self.is_root {
372443 badge("root").secondary()
444+ } else if self.is_mock {
445+ badge("not root (mock)").secondary()
373446 } else {
374- badge("not root — attacks need sudo").destructive()
447+ badge("not root — elevate to attack").destructive()
375448 };
376449
450+ let elevate_btn = (!self.is_root && !self.is_mock && crate::iface::pkexec_available())
451+ .then(|| {
452+ let waiting = self.elevation.is_some();
453+ button(
454+ "elevate-btn",
455+ if waiting {
456+ "waiting for polkit…"
457+ } else {
458+ "Elevate via polkit"
459+ },
460+ )
461+ .disabled(waiting)
462+ .on_click({
463+ let view = view.clone();
464+ move |_, _window, cx| {
465+ view.update(cx, |app, cx| app.elevate(cx));
466+ }
467+ })
468+ });
469+
377470 let toggle = button(
378471 "monitor-toggle",
379472 if running {
@@ -405,6 +498,7 @@ impl AppRoot {
405498 .bg(theme.surface())
406499 .child(div().text_lg().font_weight(FontWeight::BOLD).child("wifilab"))
407500 .child(privilege)
501+ .children(elevate_btn)
408502 .child(div().w(px(220.)).child(self.iface_select.clone()))
409503 .child(toggle)
410504 .child(div().flex_1())