Skip to content

Commit

Permalink
Merge branch 'main' into xo-rpc-1
Browse files Browse the repository at this point in the history
  • Loading branch information
Xoffio authored Sep 5, 2024
2 parents b61065b + 2eea427 commit 80ae226
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 11 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
10 changes: 10 additions & 0 deletions examples/core/get_processes/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
16 changes: 16 additions & 0 deletions examples/core/get_processes/src/main.rs
Original file line number Diff line number Diff line change
@@ -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());
}
}
2 changes: 1 addition & 1 deletion examples/gum/hook_instruction/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
2 changes: 1 addition & 1 deletion examples/gum/open/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
4 changes: 4 additions & 0 deletions frida-gum/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand All @@ -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"),
Expand Down
36 changes: 28 additions & 8 deletions frida-gum/src/interceptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,22 @@ impl<'a> Interceptor<'a> {
&mut self,
f: NativePointer,
listener: &mut I,
) -> NativePointer {
) -> Result<NativePointer> {
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.
Expand All @@ -71,12 +81,22 @@ impl<'a> Interceptor<'a> {
&mut self,
instr: NativePointer,
listener: &mut I,
) -> NativePointer {
) -> Result<NativePointer> {
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.
Expand Down
7 changes: 6 additions & 1 deletion frida/src/device_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Device<'a>> {
self.get_device_by_type(device::DeviceType::Local)
}

/// Returns the device with the specified id.
///
/// # Example
Expand Down

0 comments on commit 80ae226

Please sign in to comment.