From abd0d65e21236514cc81b64bf64710b975d43a35 Mon Sep 17 00:00:00 2001 From: xoffio <38369407+Xoffio@users.noreply.github.com> Date: Sun, 27 Oct 2024 14:30:48 -0400 Subject: [PATCH 1/2] added inject_library examples --- examples/core/inject_lib_blob/Cargo.toml | 14 +++++++++++++ examples/core/inject_lib_blob/README.md | 15 ++++++++++++++ examples/core/inject_lib_blob/src/lib.rs | 9 ++++++++ examples/core/inject_lib_blob/src/main.rs | 25 +++++++++++++++++++++++ examples/core/inject_lib_file/Cargo.toml | 14 +++++++++++++ examples/core/inject_lib_file/README.md | 15 ++++++++++++++ examples/core/inject_lib_file/src/lib.rs | 9 ++++++++ examples/core/inject_lib_file/src/main.rs | 25 +++++++++++++++++++++++ 8 files changed, 126 insertions(+) create mode 100644 examples/core/inject_lib_blob/Cargo.toml create mode 100644 examples/core/inject_lib_blob/README.md create mode 100644 examples/core/inject_lib_blob/src/lib.rs create mode 100644 examples/core/inject_lib_blob/src/main.rs create mode 100644 examples/core/inject_lib_file/Cargo.toml create mode 100644 examples/core/inject_lib_file/README.md create mode 100644 examples/core/inject_lib_file/src/lib.rs create mode 100644 examples/core/inject_lib_file/src/main.rs diff --git a/examples/core/inject_lib_blob/Cargo.toml b/examples/core/inject_lib_blob/Cargo.toml new file mode 100644 index 0000000..c492925 --- /dev/null +++ b/examples/core/inject_lib_blob/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "inject_lib_blob" +edition = "2021" +authors = ["Ricardo J Marques Montilla / Xoffio"] +publish = false + +[lib] +name = "inject_example" +crate-type = ["cdylib"] + +[dependencies] +frida = { path = "../../../frida" } +frida-sys = { path = "../../../frida-sys" } +lazy_static = "1.5.0" diff --git a/examples/core/inject_lib_blob/README.md b/examples/core/inject_lib_blob/README.md new file mode 100644 index 0000000..b2cd551 --- /dev/null +++ b/examples/core/inject_lib_blob/README.md @@ -0,0 +1,15 @@ +```sh +# Enter the example directory +cd examples/core/inject_lib_blob/ + +# Build the library and the executable +cargo build --release --lib +cargo build --release + +# Execute it +../../../target/release/inject_lib_blob + +# Examples: +../../../target/release/inject_lib_blob 4178767 +../../../target/release/inject_lib_blob $(ps -ax | grep Twitter | grep -v "grep" | awk '{print $1}') +``` diff --git a/examples/core/inject_lib_blob/src/lib.rs b/examples/core/inject_lib_blob/src/lib.rs new file mode 100644 index 0000000..91d8512 --- /dev/null +++ b/examples/core/inject_lib_blob/src/lib.rs @@ -0,0 +1,9 @@ +#[no_mangle] +pub fn injected_function(data: *const std::os::raw::c_char) { + unsafe { + if let Some(c_str) = data.as_ref() { + let message = std::ffi::CStr::from_ptr(c_str).to_string_lossy(); + println!("injected_function called with data: '{}'", message); + } + } +} diff --git a/examples/core/inject_lib_blob/src/main.rs b/examples/core/inject_lib_blob/src/main.rs new file mode 100644 index 0000000..765ae4e --- /dev/null +++ b/examples/core/inject_lib_blob/src/main.rs @@ -0,0 +1,25 @@ +use frida::{Frida, Inject}; +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(); + let args: Vec = std::env::args().collect(); + let pid = args[1].parse().unwrap(); + + if let Ok(mut device) = local_device { + println!("[*] Frida version: {}", frida::Frida::version()); + println!("[*] Device name: {}", device.get_name()); + + let script_source = include_bytes!("../../../../target/release/libinject_example.so"); + let id = device + .inject_library_blob_sync(pid, script_source, "injected_function", "w00t") + .unwrap(); + + println!("*** Injected, id={}", id); + } +} diff --git a/examples/core/inject_lib_file/Cargo.toml b/examples/core/inject_lib_file/Cargo.toml new file mode 100644 index 0000000..16079cf --- /dev/null +++ b/examples/core/inject_lib_file/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "inject_lib_file" +edition = "2021" +authors = ["Ricardo J Marques Montilla / Xoffio"] +publish = false + +[lib] +name = "inject_example" +crate-type = ["cdylib"] + +[dependencies] +frida = { path = "../../../frida" } +frida-sys = { path = "../../../frida-sys" } +lazy_static = "1.5.0" diff --git a/examples/core/inject_lib_file/README.md b/examples/core/inject_lib_file/README.md new file mode 100644 index 0000000..9ab5d15 --- /dev/null +++ b/examples/core/inject_lib_file/README.md @@ -0,0 +1,15 @@ +```sh +# Enter the example directory +cd examples/core/inject_lib_file/ + +# Build the library and the executable +cargo build --release --lib +cargo build --release + +# Execute it +../../../target/release/inject_lib_file + +# Examples: +../../../target/release/inject_lib_file 4178767 ../../../target/release/libinject_example.so +../../../target/release/inject_lib_file $(ps -ax | grep Twitter | grep -v "grep" | awk '{print $1}') ../../../target/release/libinject_example.so +``` diff --git a/examples/core/inject_lib_file/src/lib.rs b/examples/core/inject_lib_file/src/lib.rs new file mode 100644 index 0000000..91d8512 --- /dev/null +++ b/examples/core/inject_lib_file/src/lib.rs @@ -0,0 +1,9 @@ +#[no_mangle] +pub fn injected_function(data: *const std::os::raw::c_char) { + unsafe { + if let Some(c_str) = data.as_ref() { + let message = std::ffi::CStr::from_ptr(c_str).to_string_lossy(); + println!("injected_function called with data: '{}'", message); + } + } +} diff --git a/examples/core/inject_lib_file/src/main.rs b/examples/core/inject_lib_file/src/main.rs new file mode 100644 index 0000000..46f6284 --- /dev/null +++ b/examples/core/inject_lib_file/src/main.rs @@ -0,0 +1,25 @@ +use frida::{Frida, Inject}; +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(); + let args: Vec = std::env::args().collect(); + let pid = args[1].parse::().unwrap(); + let path = args[2].parse::().unwrap(); + + if let Ok(mut device) = local_device { + println!("[*] Frida version: {}", frida::Frida::version()); + println!("[*] Device name: {}", device.get_name()); + + let id = device + .inject_library_file_sync(pid, path, "injected_function", "w00t") + .unwrap(); + + println!("*** Injected, id={}", id); + } +} From b5f18f28be529131af50c8ed40a0ebc0b3c76bed Mon Sep 17 00:00:00 2001 From: xoffio <38369407+Xoffio@users.noreply.github.com> Date: Tue, 29 Oct 2024 20:12:00 -0400 Subject: [PATCH 2/2] Use LazyLock instead of lazy_static --- examples/core/inject_lib_blob/Cargo.toml | 1 - examples/core/inject_lib_blob/src/main.rs | 6 ++---- examples/core/inject_lib_file/Cargo.toml | 1 - examples/core/inject_lib_file/src/main.rs | 6 ++---- 4 files changed, 4 insertions(+), 10 deletions(-) diff --git a/examples/core/inject_lib_blob/Cargo.toml b/examples/core/inject_lib_blob/Cargo.toml index c492925..a12410a 100644 --- a/examples/core/inject_lib_blob/Cargo.toml +++ b/examples/core/inject_lib_blob/Cargo.toml @@ -11,4 +11,3 @@ crate-type = ["cdylib"] [dependencies] frida = { path = "../../../frida" } frida-sys = { path = "../../../frida-sys" } -lazy_static = "1.5.0" diff --git a/examples/core/inject_lib_blob/src/main.rs b/examples/core/inject_lib_blob/src/main.rs index 765ae4e..428307a 100644 --- a/examples/core/inject_lib_blob/src/main.rs +++ b/examples/core/inject_lib_blob/src/main.rs @@ -1,9 +1,7 @@ use frida::{Frida, Inject}; -use lazy_static::lazy_static; +use std::sync::LazyLock; -lazy_static! { - static ref FRIDA: Frida = unsafe { Frida::obtain() }; -} +static FRIDA: LazyLock = LazyLock::new(|| unsafe { Frida::obtain() }); fn main() { let device_manager = frida::DeviceManager::obtain(&FRIDA); diff --git a/examples/core/inject_lib_file/Cargo.toml b/examples/core/inject_lib_file/Cargo.toml index 16079cf..24f19bc 100644 --- a/examples/core/inject_lib_file/Cargo.toml +++ b/examples/core/inject_lib_file/Cargo.toml @@ -11,4 +11,3 @@ crate-type = ["cdylib"] [dependencies] frida = { path = "../../../frida" } frida-sys = { path = "../../../frida-sys" } -lazy_static = "1.5.0" diff --git a/examples/core/inject_lib_file/src/main.rs b/examples/core/inject_lib_file/src/main.rs index 46f6284..f4cc43a 100644 --- a/examples/core/inject_lib_file/src/main.rs +++ b/examples/core/inject_lib_file/src/main.rs @@ -1,9 +1,7 @@ use frida::{Frida, Inject}; -use lazy_static::lazy_static; +use std::sync::LazyLock; -lazy_static! { - static ref FRIDA: Frida = unsafe { Frida::obtain() }; -} +static FRIDA: LazyLock = LazyLock::new(|| unsafe { Frida::obtain() }); fn main() { let device_manager = frida::DeviceManager::obtain(&FRIDA);