Skip to content

Commit

Permalink
Added inject_library examples (#171)
Browse files Browse the repository at this point in the history
* added inject_library examples

* Use LazyLock instead of lazy_static
  • Loading branch information
Xoffio authored Oct 30, 2024
1 parent 56c4f4d commit d0306ed
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 0 deletions.
13 changes: 13 additions & 0 deletions examples/core/inject_lib_blob/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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" }
15 changes: 15 additions & 0 deletions examples/core/inject_lib_blob/README.md
Original file line number Diff line number Diff line change
@@ -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 <PID>

# Examples:
../../../target/release/inject_lib_blob 4178767
../../../target/release/inject_lib_blob $(ps -ax | grep Twitter | grep -v "grep" | awk '{print $1}')
```
9 changes: 9 additions & 0 deletions examples/core/inject_lib_blob/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
23 changes: 23 additions & 0 deletions examples/core/inject_lib_blob/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use frida::{Frida, Inject};
use std::sync::LazyLock;

static FRIDA: LazyLock<Frida> = 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<String> = 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);
}
}
13 changes: 13 additions & 0 deletions examples/core/inject_lib_file/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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" }
15 changes: 15 additions & 0 deletions examples/core/inject_lib_file/README.md
Original file line number Diff line number Diff line change
@@ -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 <PID> <LIB_FILE_PATH>

# 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
```
9 changes: 9 additions & 0 deletions examples/core/inject_lib_file/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
23 changes: 23 additions & 0 deletions examples/core/inject_lib_file/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use frida::{Frida, Inject};
use std::sync::LazyLock;

static FRIDA: LazyLock<Frida> = 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<String> = std::env::args().collect();
let pid = args[1].parse::<u32>().unwrap();
let path = args[2].parse::<String>().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);
}
}

0 comments on commit d0306ed

Please sign in to comment.