hooking 0.2.0

hooking libs in rust
Documentation

hooking-rs

github-badge crates-hooking-badge docs-hooking-badge License

Function hooking in rust

A library for hooking and intercepting functions in rust for windows and linux

Example

Each function creates a stub in memory that consists of

Section Description
Original fn detour stub address A function pointer the generated detour stub to call the original function
Hooking stub A small stub that adds some metadata (like adding detour stub address to r10 reg) before calling the hook
Original fn detour stub stub that re-creates the original fn call instructions and patches the instructions to work with ling jumps, then calls the hooked function

A simple hook:

unsafe extern "C" fn hook_destination(
    _: *mut std::ffi::c_void,
    lp_text: *const i8,
    lp_caption: *const i8,
    _: u32,
) -> i32 {
    let original_msgbox: extern "C" fn(*mut std::ffi::c_void, *const i8, *const i8, u32) -> i32 =
        unsafe { std::mem::transmute(hooking::original_function_ptr().as_ptr()) };

    original_msgbox(
        std::ptr::null_mut(),
        c"msgbox was hooked!".as_ptr(),
        c"Intercepted hook".as_ptr(),
        0,
    )
}

unsafe {
    let mut hook = Hook::by_name(
        Some(c"user32.dll"),
        c"MessageBoxA",
        hook_destination as *mut _,
    ).unwrap();

    hook.apply_hook().unwrap();
}

You can see more examples in the example directory of the repository.