diff --git a/Cargo.toml b/Cargo.toml index c2cbe7c7..58cc75ca 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,6 +18,7 @@ members = [ "examples/core/usb_device", "examples/core/console_log", "examples/core/list_exports", + "examples/core/get_processes", ] # We miss our linux_no_std example from the default members since `cargo check` # and `cargo test` both attempt to link the `std` library into it in error. diff --git a/examples/core/get_processes/Cargo.toml b/examples/core/get_processes/Cargo.toml new file mode 100644 index 00000000..9a5ce3d0 --- /dev/null +++ b/examples/core/get_processes/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "get_processes" +version = "0.1.0" +edition = "2021" +authors = ["Ricardo J Marques Montilla / Xoffio"] + +[dependencies] +frida = { path = "../../../frida" } +frida-sys = { path = "../../../frida-sys" } +lazy_static = "1.5.0" diff --git a/examples/core/get_processes/src/main.rs b/examples/core/get_processes/src/main.rs new file mode 100644 index 00000000..5a7076de --- /dev/null +++ b/examples/core/get_processes/src/main.rs @@ -0,0 +1,16 @@ +use frida::Frida; +use lazy_static::lazy_static; + +lazy_static! { + static ref FRIDA: Frida = unsafe { Frida::obtain() }; +} + +fn main() { + let device_manager = frida::DeviceManager::obtain(&FRIDA); + let local_device = device_manager.get_local_device().unwrap(); + let processes = local_device.enumerate_processes(); + + for process in processes { + println!("{} {:?}", process.get_name(), process.get_pid()); + } +} diff --git a/examples/gum/hook_instruction/src/lib.rs b/examples/gum/hook_instruction/src/lib.rs index 88fa4713..0ec1b996 100644 --- a/examples/gum/hook_instruction/src/lib.rs +++ b/examples/gum/hook_instruction/src/lib.rs @@ -22,5 +22,5 @@ fn init() { let mut interceptor = Interceptor::obtain(&GUM); let open = Module::find_export_by_name(None, "open").unwrap(); let mut listener = OpenProbeListener; - interceptor.attach_instruction(open, &mut listener); + interceptor.attach_instruction(open, &mut listener).unwrap(); } diff --git a/examples/gum/open/src/lib.rs b/examples/gum/open/src/lib.rs index fd0587c3..9723aa52 100644 --- a/examples/gum/open/src/lib.rs +++ b/examples/gum/open/src/lib.rs @@ -40,5 +40,5 @@ extern "C" fn example_agent_main(_user_data: *const c_void, resident: *mut c_int } let open = Module::find_export_by_name(None, "open").unwrap(); - interceptor.attach(open, &mut listener); + interceptor.attach(open, &mut listener).unwrap(); } diff --git a/frida-gum/src/error.rs b/frida-gum/src/error.rs index e3312893..03ced84f 100644 --- a/frida-gum/src/error.rs +++ b/frida-gum/src/error.rs @@ -13,6 +13,9 @@ pub enum Error { /// Function is already replaced during Interceptor operation InterceptorAlreadyReplaced, + /// Function is already attached during Interceptor operation + InterceptorAlreadyAttached, + /// Policy violation PolicyViolation, @@ -30,6 +33,7 @@ impl fmt::Display for Error { match self { Error::InterceptorBadSignature => write!(fmt, "Bad signature"), Error::InterceptorAlreadyReplaced => write!(fmt, "Function already replaced"), + Error::InterceptorAlreadyAttached => write!(fmt, "Function already attached"), Error::PolicyViolation => write!(fmt, "Policy violation"), Error::InterceptorError => write!(fmt, "Interceptor error"), Error::MemoryAccessError => write!(fmt, "Memory access error"), diff --git a/frida-gum/src/interceptor.rs b/frida-gum/src/interceptor.rs index 00310950..d5693d05 100644 --- a/frida-gum/src/interceptor.rs +++ b/frida-gum/src/interceptor.rs @@ -52,12 +52,22 @@ impl<'a> Interceptor<'a> { &mut self, f: NativePointer, listener: &mut I, - ) -> NativePointer { + ) -> Result { let listener = invocation_listener_transform(listener); - unsafe { + match unsafe { gum_sys::gum_interceptor_attach(self.interceptor, f.0, listener, ptr::null_mut()) - }; - NativePointer(listener as *mut c_void) + } { + gum_sys::GumAttachReturn_GUM_ATTACH_OK => Ok(NativePointer(listener as *mut c_void)), + gum_sys::GumAttachReturn_GUM_ATTACH_WRONG_SIGNATURE => { + Err(Error::InterceptorBadSignature) + } + gum_sys::GumAttachReturn_GUM_ATTACH_ALREADY_ATTACHED => { + Err(Error::InterceptorAlreadyAttached) + } + gum_sys::GumAttachReturn_GUM_ATTACH_POLICY_VIOLATION => Err(Error::PolicyViolation), + gum_sys::GumAttachReturn_GUM_ATTACH_WRONG_TYPE => Err(Error::WrongType), + _ => Err(Error::InterceptorError), + } } /// Attach a listener to an instruction address. @@ -71,12 +81,22 @@ impl<'a> Interceptor<'a> { &mut self, instr: NativePointer, listener: &mut I, - ) -> NativePointer { + ) -> Result { let listener = probe_listener_transform(listener); - unsafe { + match unsafe { gum_sys::gum_interceptor_attach(self.interceptor, instr.0, listener, ptr::null_mut()) - }; - NativePointer(listener as *mut c_void) + } { + gum_sys::GumAttachReturn_GUM_ATTACH_OK => Ok(NativePointer(listener as *mut c_void)), + gum_sys::GumAttachReturn_GUM_ATTACH_WRONG_SIGNATURE => { + Err(Error::InterceptorBadSignature) + } + gum_sys::GumAttachReturn_GUM_ATTACH_ALREADY_ATTACHED => { + Err(Error::InterceptorAlreadyAttached) + } + gum_sys::GumAttachReturn_GUM_ATTACH_POLICY_VIOLATION => Err(Error::PolicyViolation), + gum_sys::GumAttachReturn_GUM_ATTACH_WRONG_TYPE => Err(Error::WrongType), + _ => Err(Error::InterceptorError), + } } /// Detach an attached listener. diff --git a/frida/src/device_manager.rs b/frida/src/device_manager.rs index 94f47ea7..e57ab111 100644 --- a/frida/src/device_manager.rs +++ b/frida/src/device_manager.rs @@ -8,7 +8,7 @@ use frida_sys::_FridaDeviceManager; use std::ffi::CString; use std::marker::PhantomData; -use crate::device::Device; +use crate::device::{self, Device}; use crate::DeviceType; use crate::Error; use crate::Frida; @@ -107,6 +107,11 @@ impl<'a> DeviceManager<'a> { return Ok(Device::from_raw(device_ptr)); } + /// Returns the local device. + pub fn get_local_device(&'a self) -> Result> { + self.get_device_by_type(device::DeviceType::Local) + } + /// Returns the device with the specified id. /// /// # Example