Skip to content
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
5 changes: 4 additions & 1 deletion examples/core/console_log/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ struct Handler;

impl ScriptHandler for Handler {
fn on_message(&mut self, message: frida::Message, _data: Option<Vec<u8>>) {
println!("{:?}", message);
match message {
frida::Message::Log(msg) => println!("[*] {:?}: {:?}", msg.level, msg.payload),
_ => println!("{:?}", message),
}
}
}
12 changes: 7 additions & 5 deletions examples/core/list_exports/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ Example to showing how to use `script.list_exports()`.
Once ran you should expect an output similar to the next one:

```
[*] Frida version: 16.4.8
[*] Frida version: 17.5.1
[*] Device name: Local System
- Log(MessageLog { level: Info, payload: "Logging message from JS" })
- Log(MessageLog { level: Warning, payload: "Warning message from JS" })
- Log(MessageLog { level: Debug, payload: "Debug message from JS" })
- Log(MessageLog { level: Error, payload: "Error message from JS" })
[*] Info: "Logging message from JS"
[*] Warning: "Warning message from JS"
[*] Debug: "Debug message from JS"
[*] Error: "Error message from JS"
[*] Send: String("Send message from JS")
[*] Send: String("Send message with data"), Data: [1, 2, 3]
[*] Script loaded.
["increment", "getvalue"]
["increment", "getvalue"]
Expand Down
18 changes: 16 additions & 2 deletions examples/core/list_exports/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ fn main() {
console.warn("Warning message from JS");
console.debug("Debug message from JS");
console.error("Error message from JS");
send("Send message from JS");

let send_data = [1, 2 ,3];
send("Send message with data", send_data);

rpc.exports = {
increment: function() {
Expand Down Expand Up @@ -74,7 +78,17 @@ fn main() {
struct Handler;

impl frida::ScriptHandler for Handler {
fn on_message(&mut self, message: Message, _data: Option<Vec<u8>>) {
println!("- {:?}", message);
fn on_message(&mut self, message: Message, data: Option<Vec<u8>>) {
match message {
Message::Log(msg) => println!("[*] {:?}: {:?}", msg.level, msg.payload),
Message::Send(msg) => {
if let Some(data) = data {
println!("[*] Send: {:?}, Data: {:?}", msg.payload, data);
} else {
println!("[*] Send: {:?}", msg.payload);
}
}
_ => println!("- {:?}", message),
}
}
}
10 changes: 5 additions & 5 deletions examples/core/rpc_execute_function/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ Example to showing how to execute a JavaScript Frida function from Rust using `s
Once ran you should expect an output similar to the next one:

```
[*] Frida version: 16.4.8
[*] Frida version: 17.5.1
[*] Device name: Local System
[*] Script loaded.
["increment", "nIncrement", "getValue", "sumVals", "bye"]
- Log(MessageLog { level: Info, payload: "globalVar incremented by 1" })
- Log(MessageLog { level: Info, payload: "globalVar incremented by 2" })
[*] Info: "globalVar incremented by 1"
[*] Info: "globalVar incremented by 2"
js_global_var: 3
- Log(MessageLog { level: Info, payload: "Bye Potato" })
[*] Info: "Bye Potato"
total: 10
This is an error from JS: Error on the JavaScript side: "unable to find method 'NonExistentFunc'"
This is an error from JS: Error on the JavaScript side: unable to find method 'NonExistentFunc'
[*] Script unloaded
[*] Session detached
Exiting...
Expand Down
5 changes: 4 additions & 1 deletion examples/core/rpc_execute_function/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ struct Handler;

impl frida::ScriptHandler for Handler {
fn on_message(&mut self, message: Message, _data: Option<Vec<u8>>) {
println!("- {:?}", message);
match message {
frida::Message::Log(msg) => println!("[*] {:?}: {:?}", msg.level, msg.payload),
_ => println!("{:?}", message),
}
}
}
49 changes: 25 additions & 24 deletions frida/src/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub enum Message {
#[derive(Deserialize, Debug)]
pub struct MessageSend {
/// Payload of a Send Message.
pub payload: SendPayload,
pub payload: Value,
}

/// Log Message.
Expand Down Expand Up @@ -81,19 +81,6 @@ pub enum MessageLogLevel {
Error,
}

/// Represents a MessageSend's payload.
#[derive(Deserialize, Debug)]
pub struct SendPayload {
/// Send message type
pub r#type: String,
/// Send message ID
pub id: usize,
/// Send message result.
pub result: String,
/// Send message returns.
pub returns: Value,
}

unsafe extern "C" fn call_on_message<I: ScriptHandler>(
_script_ptr: *mut _FridaScript,
message: *const i8,
Expand All @@ -112,7 +99,9 @@ unsafe extern "C" fn call_on_message<I: ScriptHandler>(
});

match formatted_msg {
Message::Send(ref msg) if msg.payload.r#type == "frida:rpc" => {
Message::Send(ref msg)
if msg.payload.get(0).and_then(|v| v.as_str()) == Some("frida:rpc") =>
{
let callback_handler: *mut CallbackHandler = user_data as _;
on_message(callback_handler.as_mut().unwrap(), formatted_msg);
}
Expand Down Expand Up @@ -313,12 +302,14 @@ impl<'a> Script<'a> {
Message::Send(r) => {
let tmp_list: Vec<String> = r
.payload
.returns
.as_array()
.unwrap_or(&Vec::new())
.iter()
.map(|i| i.as_str().unwrap_or("").to_string())
.collect();
.get(3)
.and_then(|v| v.as_array())
.map(|arr| {
arr.iter()
.map(|i| i.as_str().unwrap_or("").to_string())
.collect::<Vec<String>>()
})
.unwrap_or_default();

tmp_list
}
Expand Down Expand Up @@ -359,15 +350,25 @@ impl Exports<'_> {

match rpc_result {
Message::Send(r) => {
if r.payload.result == "ok" {
let returns = r.payload.returns;
if r.payload.get(2).and_then(|v| v.as_str()) == Some("ok") {
let returns = match r.payload.get(3) {
Some(v) => v.clone(),
None => return Err(Error::RpcUnexpectedMessage),
};

match returns {
Value::Null => Ok(None),
_ => Ok(Some(returns)),
}
} else {
let err_msg = r.payload.returns.to_string();
let err_msg = r
.payload
.get(3)
.and_then(|v| v.as_str())
.unwrap_or(
"RPC call failed. Result is not ok and no error message provided.",
)
.to_string();
Err(Error::RpcJsError { message: err_msg })
}
}
Expand Down
Loading