From d0306eda50e661b13e72665d62d56f006feca506 Mon Sep 17 00:00:00 2001 From: xoffio <38369407+Xoffio@users.noreply.github.com> Date: Wed, 30 Oct 2024 02:28:19 -0400 Subject: [PATCH] Added inject_library examples (#171) * added inject_library examples * Use LazyLock instead of lazy_static --- examples/core/inject_lib_blob/Cargo.toml | 13 +++++++++++++ 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 | 23 +++++++++++++++++++++++ examples/core/inject_lib_file/Cargo.toml | 13 +++++++++++++ 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 | 23 +++++++++++++++++++++++ 8 files changed, 120 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..a12410a --- /dev/null +++ b/examples/core/inject_lib_blob/Cargo.toml @@ -0,0 +1,13 @@ +[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" } 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..428307a --- /dev/null +++ b/examples/core/inject_lib_blob/src/main.rs @@ -0,0 +1,23 @@ +use frida::{Frida, Inject}; +use std::sync::LazyLock; + +static FRIDA: LazyLock = LazyLock::new(|| 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..24f19bc --- /dev/null +++ b/examples/core/inject_lib_file/Cargo.toml @@ -0,0 +1,13 @@ +[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" } 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..f4cc43a --- /dev/null +++ b/examples/core/inject_lib_file/src/main.rs @@ -0,0 +1,23 @@ +use frida::{Frida, Inject}; +use std::sync::LazyLock; + +static FRIDA: LazyLock = LazyLock::new(|| 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); + } +}