Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added inject_library examples #171

Merged
merged 2 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Xoffio marked this conversation as resolved.
Show resolved Hide resolved

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);
}
}
Loading