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

add get_frontmost_application #196

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
32 changes: 32 additions & 0 deletions frida/src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use crate::process::Process;
use crate::session::Session;
use crate::variant::Variant;
use crate::{Error, Result, SpawnOptions};
use crate::frida_application::FridaApplication;

/// Access to a Frida device.
pub struct Device<'a> {
Expand Down Expand Up @@ -150,6 +151,37 @@ impl<'a> Device<'a> {
processes
}

/// Returns front most application.
pub fn get_frontmost_application<'b>(&'a self) -> Result<FridaApplication<'b>>
where
'a: 'b,
{
let mut error: *mut frida_sys::GError = std::ptr::null_mut();

let app_ptr = unsafe {
frida_sys::frida_device_get_frontmost_application_sync(
self.device_ptr,
std::ptr::null_mut(),
std::ptr::null_mut(),
&mut error,
)
};

if !error.is_null() {
let message = unsafe { CString::from_raw((*error).message) }
.into_string()
.unwrap_or_else(|_| "Unknown error".to_string());
panic!("Failed to get frontmost application: {}", message);
}

if app_ptr.is_null() {
Err(Error::DeviceGetFrontMostApplicationError)
} else {
Ok(FridaApplication::from_raw(app_ptr))
}

}

/// Creates [`Session`] and attaches the device to the current PID.
pub fn attach<'b>(&'a self, pid: u32) -> Result<Session<'b>>
where
Expand Down
5 changes: 5 additions & 0 deletions frida/src/device_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ impl<'a> DeviceManager<'a> {
self.get_device_by_type(device::DeviceType::Local)
}

/// Returns the usb device.
pub fn get_usb_device(&'a self) -> Result<Device<'a>> {
self.get_device_by_type(device::DeviceType::USB)
}

/// Returns the device with the specified id.
///
/// # Example
Expand Down
4 changes: 4 additions & 0 deletions frida/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ pub enum Error {
#[error("Failed to lookup device")]
DeviceLookupFailed,

/// Failed to get front most application.
#[error("Failed to get front most application")]
DeviceGetFrontMostApplicationError,

/// Failed to detach a session.
#[error("Failed to detach the current session")]
SessionDetachError,
Expand Down
39 changes: 39 additions & 0 deletions frida/src/frida_application.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use frida_sys::_FridaApplication;
use std::ffi::CStr;
use std::marker::PhantomData;

/// Application in Frida.
pub struct FridaApplication<'a> {
application_ptr: *mut _FridaApplication,
phantom: PhantomData<&'a _FridaApplication>,
}

impl<'a> FridaApplication<'a> {
pub(crate) fn from_raw(application_ptr: *mut _FridaApplication) -> FridaApplication<'a> {
FridaApplication {
application_ptr,
phantom: PhantomData,
}
}

/// Returns the app's name.
pub fn get_name(&self) -> &str {
let name = unsafe {
CStr::from_ptr(frida_sys::frida_application_get_name(self.application_ptr) as _)
};
name.to_str().unwrap_or_default()
}

/// Returns the app's pid.
pub fn get_pid(&self) -> u32 {
unsafe { frida_sys::frida_application_get_pid(self.application_ptr) }
}

/// Returns the app's identifier.
pub fn get_identifier(&self) -> &str {
let id = unsafe {
CStr::from_ptr(frida_sys::frida_application_get_identifier(self.application_ptr) as _)
};
id.to_str().unwrap_or_default()
}
}
3 changes: 3 additions & 0 deletions frida/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ pub use injector::*;
mod process;
pub use process::*;

mod frida_application;
pub use frida_application::*;

mod script;
pub use script::*;

Expand Down