Skip to content

Commit

Permalink
s1341's notes have been addressed.
Browse files Browse the repository at this point in the history
  • Loading branch information
Xoffio committed Sep 3, 2024
1 parent c54d201 commit 8a6a3a8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 21 deletions.
23 changes: 21 additions & 2 deletions examples/core/list_exports/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,41 @@
use frida::{Frida, Message};
use lazy_static::lazy_static;
use std::io::{self, Write};
use std::{thread, time::Duration};

lazy_static! {
static ref FRIDA: Frida = unsafe { Frida::obtain() };
}

fn main() {
let pid = 1134009;
let device_manager = frida::DeviceManager::obtain(&FRIDA);
let local_device = device_manager.get_device_by_type(frida::DeviceType::Local);

print!("Enter pid: ",);
io::stdout().flush().expect("Failed to flush stdout");

let mut pid_input = String::new();
io::stdin()
.read_line(&mut pid_input)
.expect("Failed to read pid");

let pid = pid_input
.trim()
.parse()
.expect("Please enter a valid number");

if let Ok(device) = local_device {
println!("[*] Frida version: {}", frida::Frida::version());
println!("[*] Device name: {}", device.get_name());

// Attach to the program
let session = device.attach(pid).unwrap();
let session = match device.attach(pid) {
Ok(s) => s,
Err(_) => {
println!("Error attaching to process {}", pid);
return;
}
};

if session.is_detached() {
println!("Session is detached");
Expand Down
29 changes: 10 additions & 19 deletions frida/src/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use frida_sys::{FridaScriptOptions, _FridaScript, g_bytes_new, g_bytes_unref};
use serde::Deserialize;
use serde_json::Value;
use std::cell::Cell;
use std::marker::PhantomData;
use std::sync::mpsc::{channel, Receiver, Sender};
use std::{
Expand Down Expand Up @@ -111,31 +110,23 @@ unsafe extern "C" fn call_on_message<I: ScriptHandler>(
.to_str()
.unwrap_or_default();

let formated_msg: Message = serde_json::from_str(c_msg).unwrap_or_else(|err| {
let formatted_msg: Message = serde_json::from_str(c_msg).unwrap_or_else(|err| {
Message::Other(serde_json::json!({
"error": err.to_string(),
"data": c_msg
}))
});

match formated_msg {
match formatted_msg {
Message::Send(msg) => {
if msg.payload.r#type == "frida:rpc" {
let callback_handler: *mut CallbackHandler = user_data as _;
on_message(callback_handler.as_mut().unwrap(), Message::Send(msg));
}
}
Message::Log(msg) => {
_ => {
let handler: &mut I = &mut *(user_data as *mut I);
handler.on_message(&Message::Log(msg));
}
Message::Error(msg) => {
let handler: &mut I = &mut *(user_data as *mut I);
handler.on_message(&Message::Error(msg));
}
Message::Other(msg) => {
let handler: &mut I = &mut *(user_data as *mut I);
handler.on_message(&Message::Other(msg));
handler.on_message(&formatted_msg);
}
}
}
Expand All @@ -155,7 +146,7 @@ pub trait ScriptHandler {
pub struct Script<'a> {
script_ptr: *mut _FridaScript,
phantom: PhantomData<&'a _FridaScript>,
rpc_id_counter: Cell<usize>,
rpc_id_counter: usize,
callback_handler: CallbackHandler,
}

Expand All @@ -164,7 +155,7 @@ impl<'a> Script<'a> {
Script {
script_ptr,
phantom: PhantomData,
rpc_id_counter: Cell::new(0),
rpc_id_counter: 0,
callback_handler: CallbackHandler::new(),
}
}
Expand Down Expand Up @@ -249,13 +240,13 @@ impl<'a> Script<'a> {
Ok(())
}

fn inc_id(&self) -> usize {
let current_id_counter = self.rpc_id_counter.get();
self.rpc_id_counter.replace(current_id_counter + 1)
fn inc_id(&mut self) -> usize {
self.rpc_id_counter += 1;
self.rpc_id_counter
}

/// List all the exported attributes from the script's rpc
pub fn list_exports(&self) -> Result<Option<Vec<String>>> {
pub fn list_exports(&mut self) -> Result<Option<Vec<String>>> {
let json_req = {
let name = "frida:rpc".into();
let id = self.inc_id().into();
Expand Down

0 comments on commit 8a6a3a8

Please sign in to comment.