library_hook/
library_hook.rs1use substrate::utils;
2use substrate::MSHookFunction;
3use std::ffi::c_void;
4
5static mut OLD_FUNCTION: *mut c_void = std::ptr::null_mut();
6
7unsafe extern "C" fn my_hooked_function(value: i32) -> i32 {
8 println!("✓ Hook executed! Input: {}", value);
9
10 if !OLD_FUNCTION.is_null() {
11 let original: extern "C" fn(i32) -> i32 = std::mem::transmute(OLD_FUNCTION);
12 original(value)
13 } else {
14 value * 2
15 }
16}
17
18fn main() {
19 println!("=== Library Function Hook Example ===\n");
20
21 println!("This example demonstrates hooking a function in a shared library.");
22 println!("Note: This requires a target library to be loaded.\n");
23
24 let library_name = "libexample.so";
25 let function_offset = 0x1234;
26
27 unsafe {
28 println!("Checking if {} is loaded...", library_name);
29
30 if utils::is_library_loaded(library_name) {
31 println!("✓ Library is loaded!");
32
33 match utils::get_absolute_address(library_name, function_offset) {
34 Ok(addr) => {
35 println!("Target address: 0x{:x}", addr);
36 println!("Installing hook...");
37
38 MSHookFunction(
39 addr as *mut c_void,
40 my_hooked_function as *mut c_void,
41 &mut OLD_FUNCTION
42 );
43
44 println!("✓ Hook installed!");
45 println!("Original function: {:p}", OLD_FUNCTION);
46 }
47 Err(e) => {
48 eprintln!("✗ Failed to get address: {}", e);
49 }
50 }
51 } else {
52 println!("✗ Library '{}' is not loaded", library_name);
53 println!("\nTo use this example:");
54 println!("1. Replace 'libexample.so' with your target library");
55 println!("2. Replace 0x1234 with the actual function offset");
56 println!("3. Make sure the library is loaded before running");
57 }
58 }
59}