Skip to content

Craft Anything: understand how any software works, and craft your own.

Rust

Frida lists devices and processes on Windows instead of hanging or finding none (#59)

valère didon26bf3635f104
diff --git a/crates/engines/src/frida.rs b/crates/engines/src/frida.rs
index 880292d0..f691166f 100644
--- a/crates/engines/src/frida.rs
+++ b/crates/engines/src/frida.rs
@@ -169,17 +169,33 @@ impl Frida {
args.push(OsStr::new(&r.address).into());
}
}
- let out = self.proc(&prog, INSTALL).args(args).run()?;
- if !out.success && out.stdout.is_empty() {
- return Err(cli_failed("frida-ls-devices", &out));
+ // On Windows this tool crashes without a real console, then hangs instead of exiting.
+ #[cfg(windows)]
+ {
+ if !prog.is_file() {
+ return Err(ToolError::new(ErrorCode::EngineMissing, "frida-ls-devices isn't next to frida.").with_remediation(INSTALL));
+ }
+ let (text, code) = craft_anything_platform::capture::console_text(&prog, &args, self.wall, self.cancel.clone())?;
+ if code != Some(0) {
+ let last = text.lines().rev().map(str::trim).find(|l| !l.is_empty()).unwrap_or("no output");
+ return Err(ToolError::new(ErrorCode::ProgramFailed, format!("frida-ls-devices failed: {last}")));
+ }
+ Ok(parse_devices(&text))
+ }
+ #[cfg(not(windows))]
+ {
+ let out = self.proc(&prog, INSTALL).args(args).run()?;
+ if !out.success && out.stdout.is_empty() {
+ return Err(cli_failed("frida-ls-devices", &out));
+ }
+ Ok(parse_devices(&out.stdout_text()))
}
- Ok(parse_devices(&out.stdout_text()))
}
/// `frida-ps` for the selected device, parsed.
pub fn list_processes(&self, selector: &Selector) -> Result<Vec<FridaProcess>, ToolError> {
let prog = self.sibling("frida-ps");
- let mut args = selector.args()?;
+ let args = selector.args()?;
// `-ai` lists installed applications with identifiers; it degrades to processes on devices
// that don't support it. We ask for applications first; failing that, plain processes.
let app_out = self
@@ -196,7 +212,6 @@ impl Frida {
return Ok(apps);
}
}
- args.push(OsStr::new("-a").into());
let out = self.proc(&prog, INSTALL).args(args).run()?;
if !out.success && out.stdout.is_empty() {
return Err(cli_failed("frida-ps", &out));
@@ -271,13 +286,15 @@ pub fn parse_devices(text: &str) -> Vec<FridaDevice> {
if trimmed.is_empty() || trimmed.starts_with("----") {
continue;
}
- let mut it = trimmed.split_whitespace();
+ // Columns are at least two spaces apart; a name has single spaces in it. A later column
+ // (newer versions add the device's OS) isn't part of the name.
+ let mut it = trimmed.split(" ").map(str::trim).filter(|c| !c.is_empty());
let Some(first) = it.next() else { continue };
let Some(second) = it.next() else { continue };
if first.eq_ignore_ascii_case("id") && second.eq_ignore_ascii_case("type") {
continue; // header
}
- let name: String = it.collect::<Vec<_>>().join(" ");
+ let name = it.next().unwrap_or_default().to_string();
out.push(FridaDevice { device_id: first.to_string(), kind: second.to_string(), name });
}
out
@@ -366,6 +383,9 @@ mod tests {
assert_eq!(d[0].device_id, "local");
assert_eq!(d[1].name, "Pixel 5");
+ let with_os = "Id Type Name OS\n----- ----- ------------ ----------\nlocal local Local System Windows 11\n";
+ assert_eq!(parse_devices(with_os)[0].name, "Local System");
+
let ps = " PID Name Identifier\n---- -------------- ----------------\n1234 Example com.example.app\n 56 init\n";
let p = parse_processes(ps);
assert_eq!(p.len(), 2);
@@ -376,6 +396,24 @@ mod tests {
assert!(p[1].identifier.is_none());
}
+ #[cfg(unix)]
+ #[test]
+ fn a_device_without_applications_lists_its_processes() {
+ // A stand-in for frida-ps that, like Windows, has processes but no applications.
+ let dir = tempfile::tempdir().unwrap();
+ let frida = {
+ use std::os::unix::fs::PermissionsExt;
+ let ps = dir.path().join("frida-ps");
+ std::fs::write(&ps, "#!/bin/sh\ncase \"$*\" in *-a*) exit 0;; esac\nprintf ' PID Name\\n---- ---------\\n 12 notes.exe\\n'\n").unwrap();
+ std::fs::set_permissions(&ps, std::fs::Permissions::from_mode(0o755)).unwrap();
+ dir.path().join("frida")
+ };
+ let selector = Selector { device_id: Some("local".into()), remote: None };
+ let processes = Frida::new(frida).list_processes(&selector).unwrap();
+ assert_eq!(processes.len(), 1, "{processes:?}");
+ assert_eq!(processes[0].pid, 12);
+ }
+
#[test]
fn missing_frida_is_provider_unavailable() {
let frida = Frida::new("/nonexistent/definitely/not/frida");
diff --git a/crates/platform/src/capture.rs b/crates/platform/src/capture.rs
index 7809bbb6..86392e46 100644
--- a/crates/platform/src/capture.rs
+++ b/crates/platform/src/capture.rs
@@ -328,6 +328,104 @@ pub fn capture(_spec: Spec, _cancel: Cancel) -> Result<Capture, ToolError> {
.with_remediation("Run the capture on a Linux or macOS host."))
}
+/// Run a program that crashes without a real console (it asks the console for its size, which
+/// fails on [`crate::process`]'s pipes) and return what it drew and its exit code. The deadline
+/// and `cancel` still end it; nothing writes to its input.
+#[cfg(windows)]
+pub fn console_text(program: &std::path::Path, args: &[OsString], timeout: Duration, cancel: Option<Cancel>) -> Result<(String, Option<i32>), ToolError> {
+ use portable_pty::{CommandBuilder, PtySize, native_pty_system};
+ use std::io::{Read, Write};
+
+ const ROWS: u16 = 50;
+ const COLS: u16 = 200;
+ let failed = |e: &dyn std::fmt::Display| ToolError::new(ErrorCode::ProgramFailed, format!("{}: {e}", program.display()));
+ let portable_pty::PtyPair { master, slave } =
+ native_pty_system().openpty(PtySize { rows: ROWS, cols: COLS, pixel_width: 0, pixel_height: 0 }).map_err(|e| failed(&e))?;
+ let mut cmd = CommandBuilder::new(program.as_os_str());
+ cmd.args(args);
+ let mut child = slave.spawn_command(cmd).map_err(|e| failed(&e))?;
+ drop(slave);
+ let mut reader = master.try_clone_reader().map_err(|e| failed(&e))?;
+ let mut writer = master.take_writer().map_err(|e| failed(&e))?;
+ // The console asks where the cursor is and starts nothing until it's told.
+ writer.write_all(b"\x1b[1;1R").map_err(|e| failed(&e))?;
+
+ let (screen_tx, screen_rx) = std::sync::mpsc::channel();
+ std::thread::Builder::new()
+ .name("console-reader".into())
+ .spawn(move || {
+ // A fixed grid, so a chatty program can't grow this.
+ let mut screen = vt100::Parser::new(ROWS, COLS, 0);
+ let mut chunk = vec![0u8; 16 * 1024];
+ while let Ok(n) = reader.read(&mut chunk) {
+ let Some(part) = chunk.get(..n).filter(|p| !p.is_empty()) else { break };
+ screen.process(part);
+ }
+ let _ = screen_tx.send(screen.screen().contents());
+ })
+ .map_err(|e| failed(&e))?;
+
+ let start = std::time::Instant::now();
+ let stopped = loop {
+ match child.try_wait() {
+ Ok(Some(status)) => break Ok(status.exit_code()),
+ Ok(None) => {}
+ Err(e) => break Err(failed(&e)),
+ }
+ if cancel.as_ref().is_some_and(|c| c()) {
+ break Err(ToolError::new(ErrorCode::Cancelled, format!("{} was stopped.", program.display())));
+ }
+ if start.elapsed() >= timeout {
+ break Err(ToolError::new(ErrorCode::TimedOut, format!("{} didn't finish in time and was stopped.", program.display())));
+ }
+ std::thread::sleep(Duration::from_millis(50));
+ };
+ if stopped.is_err() {
+ let _ = child.kill();
+ }
+ // Closing the console flushes it and ends everything attached to it, which is what ends a
+ // launcher's children here. It can block, so it happens away from here.
+ // ponytail: no Job Object (portable-pty gives no process handle, and this workspace forbids
+ // unsafe), so a program that leaves this console for one of its own survives.
+ let _ = std::thread::Builder::new().name("console-close".into()).spawn(move || drop((writer, master)));
+ let text = screen_rx.recv_timeout(Duration::from_secs(3)).unwrap_or_default();
+ Ok((text, stopped?.try_into().ok()))
+}
+
+#[cfg(all(test, windows))]
+mod console_tests {
+ use super::*;
+
+ #[test]
+ fn reads_what_the_program_drew_and_stops_at_the_deadline() {
+ let cmd = PathBuf::from(std::env::var_os("ComSpec").unwrap());
+ let echo: Vec<OsString> = ["/c", "echo hello-console & exit 3"].map(OsString::from).to_vec();
+ let (text, code) = console_text(&cmd, &echo, Duration::from_secs(30), None).unwrap();
+ assert!(text.contains("hello-console"), "{text:?}");
+ assert_eq!(code, Some(3));
+ let hang: Vec<OsString> = ["/c", "ping -n 60 127.0.0.1"].map(OsString::from).to_vec();
+ assert_eq!(console_text(&cmd, &hang, Duration::from_millis(300), None).unwrap_err().code, ErrorCode::TimedOut);
+ }
+
+ #[test]
+ fn nothing_it_started_outlives_the_deadline() {
+ // The program starts a second one that keeps adding to a file, like a launcher and its Python.
+ let dir = tempfile::tempdir().unwrap();
+ let ticks = dir.path().join("ticks");
+ std::fs::write(dir.path().join("tick.cmd"), "@echo off\r\n:again\r\necho x>>\"%~dp0ticks\"\r\nping -n 2 127.0.0.1 >nul\r\ngoto again\r\n").unwrap();
+ std::fs::write(dir.path().join("main.cmd"), "@echo off\r\nstart /b cmd /c \"%~dp0tick.cmd\"\r\nping -n 60 127.0.0.1 >nul\r\n").unwrap();
+ let cmd = PathBuf::from(std::env::var_os("ComSpec").unwrap());
+ let run = vec![OsString::from("/c"), dir.path().join("main.cmd").into_os_string()];
+ let e = console_text(&cmd, &run, Duration::from_secs(4), None).unwrap_err();
+ assert_eq!(e.code, ErrorCode::TimedOut);
+ assert!(ticks.is_file(), "the second program never ran");
+ std::thread::sleep(Duration::from_secs(2));
+ let before = std::fs::metadata(&ticks).unwrap().len();
+ std::thread::sleep(Duration::from_secs(4));
+ assert_eq!(std::fs::metadata(&ticks).unwrap().len(), before, "the second program is still running");
+ }
+}
+
/// A capture without a terminal: the program's output read through pipes. Its Job Object ends
/// every process it started when the run ends, times out or is cancelled.
#[cfg(any(windows, test))]
diff --git a/docs/ROADMAP.md b/docs/ROADMAP.md
index 1f48c85b..ab4e3381 100644
--- a/docs/ROADMAP.md
+++ b/docs/ROADMAP.md
@@ -904,6 +904,7 @@ Owner: open · Status: done
### R3.3 Frida sessions for real
Owner: open · Status: todo
- [ ] Replace the in-memory session bookkeeping with real attach, resume and detach
+- [ ] `frida_devices_find` with `remote`: `frida-ls-devices` has no `-H`, so listing a remote's devices fails
### R3.4 Decompilers for managed code (external engines)
Owner: open · Status: in progress