Skip to content

Commit

Permalink
added inject_library examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Xoffio committed Oct 27, 2024
1 parent 62a7e6b commit abd0d65
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 0 deletions.
14 changes: 14 additions & 0 deletions examples/core/inject_lib_blob/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
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);
}
}
}
25 changes: 25 additions & 0 deletions examples/core/inject_lib_blob/src/main.rs
Original file line number Diff line number Diff line change
@@ -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<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);
}
}
14 changes: 14 additions & 0 deletions examples/core/inject_lib_file/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
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);
}
}
}
25 changes: 25 additions & 0 deletions examples/core/inject_lib_file/src/main.rs
Original file line number Diff line number Diff line change
@@ -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<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 abd0d65

Please sign in to comment.