Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added get_device_by_id to the device_manager and example #117

Merged
merged 4 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand All @@ -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",
]
12 changes: 12 additions & 0 deletions examples/core/usb_device/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "usb_device"
version = "0.1.0"
authors = ["Andras Marczell <[email protected]>"]
edition = "2018"
license = "wxWindows"
publish = false

[dependencies]
frida = { path = "../../../frida" }
frida-sys = { path = "../../../frida-sys" }
lazy_static = "1.4"
17 changes: 17 additions & 0 deletions examples/core/usb_device/src/main.rs
Original file line number Diff line number Diff line change
@@ -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());
}
34 changes: 34 additions & 0 deletions frida/src/device_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

use frida_sys::_FridaDeviceManager;
use std::ffi::CString;
use std::marker::PhantomData;

use crate::device::Device;
Expand Down Expand Up @@ -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 = "<some 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<Device<'a>> {
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> {
Expand Down
Loading