-
-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
199 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[package] | ||
name = "rpc_execute_function" | ||
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" | ||
serde = { version = "1.0", features = ["derive"] } | ||
serde_json = "1.0.127" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
Example to showing how to execute a JavaScript Frida function from Rust using `script.exports`. | ||
Once ran you should expect an output similar to the next one: | ||
|
||
``` | ||
[*] Frida version: 16.4.8 | ||
[*] 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" }) | ||
js_global_var: 3 | ||
- Log(MessageLog { level: Info, payload: "Bye Potato" }) | ||
total: 10 | ||
Error: Error on the JavaScript side: "unable to find method 'NonExistentFunc'" | ||
[*] Script unloaded | ||
[*] Session detached | ||
Exiting... | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
use frida::{Frida, Message}; | ||
use lazy_static::lazy_static; | ||
use serde_json::json; | ||
|
||
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(); | ||
|
||
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(0).unwrap(); | ||
|
||
let script_source = r#" | ||
var globalVar = 0; | ||
rpc.exports = { | ||
increment: function() { | ||
globalVar += 1; | ||
console.log("globalVar incremented by 1"); | ||
}, | ||
nIncrement: function(n) { | ||
globalVar += n; | ||
console.log("globalVar incremented by " + n); | ||
}, | ||
getValue: function() { | ||
return globalVar; | ||
}, | ||
sumVals: function(vals, b) { | ||
let sum = 0 | ||
for (let i=0; i < vals.length; i++) { | ||
sum += vals[i] | ||
} | ||
return sum; | ||
}, | ||
bye: function(name) { | ||
console.log("Bye " + name); | ||
} | ||
}; | ||
"#; | ||
|
||
let mut script_option = frida::ScriptOption::default(); | ||
let mut script = match session.create_script(script_source, &mut script_option) { | ||
Ok(s) => s, | ||
Err(err) => { | ||
println!("{}", err); | ||
return; | ||
} | ||
}; | ||
|
||
let msg_handler = script.handle_message(Handler); | ||
if let Err(err) = msg_handler { | ||
panic!("{:?}", err); | ||
} | ||
|
||
script.load().unwrap(); | ||
println!("[*] Script loaded."); | ||
|
||
let js_functions = script.list_exports().unwrap(); | ||
println!("{:?}", &js_functions); | ||
|
||
// Example calling a function in JS and giving the function name from `list_exports` | ||
// Expect a log message to be printed. | ||
let _ = script.exports(&js_functions[0], None); // Increment | ||
|
||
// Example calling a JS function, giving the function name as &str, and a "Number" parameter. | ||
let _ = script.exports("nIncrement", Some(json!([2]))); | ||
|
||
// Example showing how to get the returned value. | ||
let js_global_var = script.exports("getValue", None).unwrap().unwrap(); | ||
println!("js_global_var: {}", js_global_var); | ||
|
||
// Example sending a String as parameter. | ||
// Expect a log message to be printed. | ||
let _ = script.exports("bye", Some(json!(["Potato"]))); | ||
|
||
// Example showing sending multiple arguments. | ||
let total = script | ||
.exports("sumVals", Some(json!([[1, 2, 3, 4], true]))) | ||
.unwrap() | ||
.unwrap(); | ||
println!("total: {}", total); | ||
|
||
// Here I show how errors look like | ||
if let Err(err_msg) = script.exports("NonExistentFunc", Some(json!([[1, 2, 3, 4], true]))) { | ||
println!("Error: {}", err_msg); | ||
} | ||
|
||
script.unload().unwrap(); | ||
println!("[*] Script unloaded"); | ||
|
||
session.detach().unwrap(); | ||
println!("[*] Session detached"); | ||
} | ||
|
||
println!("Exiting..."); | ||
} | ||
|
||
struct Handler; | ||
|
||
impl frida::ScriptHandler for Handler { | ||
fn on_message(&mut self, message: &Message) { | ||
println!("- {:?}", message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters