From a00469bcc9cbd9d5fcaa41a31956dc7b67e3edad Mon Sep 17 00:00:00 2001 From: Andras <30615058+andy-3000@users.noreply.github.com> Date: Sat, 18 Nov 2023 14:28:41 +0100 Subject: [PATCH 1/4] Add get_device_by_id to the device_manager and example --- Cargo.toml | 2 ++ examples/core/usb_device/Cargo.toml | 12 ++++++++++ examples/core/usb_device/src/main.rs | 17 ++++++++++++++ frida/src/device_manager.rs | 34 ++++++++++++++++++++++++++++ 4 files changed, 65 insertions(+) create mode 100644 examples/core/usb_device/Cargo.toml create mode 100644 examples/core/usb_device/src/main.rs diff --git a/Cargo.toml b/Cargo.toml index 151fd0a..24faf57 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,6 +15,7 @@ members = [ "examples/gum/linux_no_std", "examples/gum/memory_access_monitor", "examples/core/hello", + "examples/core/usb_device", "examples/core/console_log", ] # We miss our linux_no_std example from the default members since `cargo check` @@ -33,5 +34,6 @@ default-members = [ "examples/gum/fast_interceptor", "examples/gum/memory_access_monitor", "examples/core/hello", + "examples/core/usb_device", "examples/core/console_log", ] diff --git a/examples/core/usb_device/Cargo.toml b/examples/core/usb_device/Cargo.toml new file mode 100644 index 0000000..a74b7de --- /dev/null +++ b/examples/core/usb_device/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "usb_device" +version = "0.1.0" +authors = ["Andras Marczell "] +edition = "2018" +license = "wxWindows" +publish = false + +[dependencies] +frida = { path = "../../../frida" } +frida-sys = { path = "../../../frida-sys" } +lazy_static = "1.4" diff --git a/examples/core/usb_device/src/main.rs b/examples/core/usb_device/src/main.rs new file mode 100644 index 0000000..9b9c4d7 --- /dev/null +++ b/examples/core/usb_device/src/main.rs @@ -0,0 +1,17 @@ +use frida::DeviceType; + +fn main() { + let frida = unsafe { frida::Frida::obtain() }; + let device_manager = frida::DeviceManager::obtain(&frida); + + // get the first usb device (assuming there is one attached) + let device = device_manager.get_device_by_type(DeviceType::USB).unwrap(); + assert_eq!(device.get_type(), DeviceType::USB); + println!("found {} with type: {}", device.get_name(), device.get_type()); + + // get the device id and use it to obtain a the device by the id + let device_id = device.get_id(); + let device = device_manager.get_device_by_id(device_id).unwrap(); + assert_eq!(device.get_id(), device_id); + println!("found {} with id: {}", device.get_name(), device.get_id()); +} \ No newline at end of file diff --git a/frida/src/device_manager.rs b/frida/src/device_manager.rs index 0dbcf39..9db89d4 100644 --- a/frida/src/device_manager.rs +++ b/frida/src/device_manager.rs @@ -5,6 +5,7 @@ */ use frida_sys::_FridaDeviceManager; +use std::ffi::CString; use std::marker::PhantomData; use crate::device::Device; @@ -83,6 +84,39 @@ impl<'a> DeviceManager<'a> { return Ok(Device::from_raw(device_ptr)); } + + /// Returns the device with the specified id. + /// + /// # Example + /// ``` + /// + /// let frida = unsafe { frida::Frida::obtain() }; + /// let device_manager = frida::DeviceManager::obtain(&frida); + /// + /// let id = ""; + /// let device = device_manager.get_device_by_id(id).unwrap(); + /// assert_eq!(device.get_id(), id); + /// ``` + pub fn get_device_by_id(&'a self, device_id: &str) -> Result> { + let mut error: *mut frida_sys::GError = std::ptr::null_mut(); + let cstring = CString::new(device_id).unwrap(); + + let device_ptr = unsafe { + frida_sys::frida_device_manager_get_device_by_id_sync( + self.manager_ptr, + cstring.as_ptr() as *const i8, + 0, + std::ptr::null_mut(), + &mut error, + ) + }; + + if !error.is_null() { + return Err(Error::DeviceLookupFailed); + } + + return Ok(Device::from_raw(device_ptr)); + } } impl<'a> Drop for DeviceManager<'a> { From 9a3b8731accdbeb716019ce0c08c451280ce8a34 Mon Sep 17 00:00:00 2001 From: Andras <=> Date: Wed, 13 Dec 2023 11:33:37 +0100 Subject: [PATCH 2/4] fixed typo in rust doc that caused doc test to fail --- frida/src/device_manager.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/frida/src/device_manager.rs b/frida/src/device_manager.rs index 9db89d4..66bdf8b 100644 --- a/frida/src/device_manager.rs +++ b/frida/src/device_manager.rs @@ -88,7 +88,6 @@ impl<'a> DeviceManager<'a> { /// Returns the device with the specified id. /// /// # Example - /// ``` /// /// let frida = unsafe { frida::Frida::obtain() }; /// let device_manager = frida::DeviceManager::obtain(&frida); @@ -96,7 +95,7 @@ impl<'a> DeviceManager<'a> { /// let id = ""; /// let device = device_manager.get_device_by_id(id).unwrap(); /// assert_eq!(device.get_id(), id); - /// ``` + /// pub fn get_device_by_id(&'a self, device_id: &str) -> Result> { let mut error: *mut frida_sys::GError = std::ptr::null_mut(); let cstring = CString::new(device_id).unwrap(); From 2ae9157c07b664d971adfb3188b7f68409965320 Mon Sep 17 00:00:00 2001 From: Andras <=> Date: Wed, 13 Dec 2023 15:05:22 +0100 Subject: [PATCH 3/4] Fixed format with rust fmt --- examples/core/usb_device/src/main.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/examples/core/usb_device/src/main.rs b/examples/core/usb_device/src/main.rs index 9b9c4d7..c64d532 100644 --- a/examples/core/usb_device/src/main.rs +++ b/examples/core/usb_device/src/main.rs @@ -7,11 +7,15 @@ fn main() { // get the first usb device (assuming there is one attached) let device = device_manager.get_device_by_type(DeviceType::USB).unwrap(); assert_eq!(device.get_type(), DeviceType::USB); - println!("found {} with type: {}", device.get_name(), device.get_type()); + println!( + "found {} with type: {}", + device.get_name(), + device.get_type() + ); // get the device id and use it to obtain a the device by the id let device_id = device.get_id(); let device = device_manager.get_device_by_id(device_id).unwrap(); assert_eq!(device.get_id(), device_id); println!("found {} with id: {}", device.get_name(), device.get_id()); -} \ No newline at end of file +} From 51dc150a870e7e5f6b019b49c2471782298e1895 Mon Sep 17 00:00:00 2001 From: Andras <=> Date: Wed, 13 Dec 2023 17:39:00 +0100 Subject: [PATCH 4/4] fixed unnecessary cast --- frida/src/device_manager.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frida/src/device_manager.rs b/frida/src/device_manager.rs index 66bdf8b..75e6dab 100644 --- a/frida/src/device_manager.rs +++ b/frida/src/device_manager.rs @@ -103,7 +103,7 @@ impl<'a> DeviceManager<'a> { let device_ptr = unsafe { frida_sys::frida_device_manager_get_device_by_id_sync( self.manager_ptr, - cstring.as_ptr() as *const i8, + cstring.as_ptr(), 0, std::ptr::null_mut(), &mut error,