Rust Dylib Hijacking Framework
This project provides a framework for hooking functions loaded from dynamic libraries on Linux. It allows you to intercept and modify the behavior of functions at runtime, making it useful for debugging, monitoring, or altering the behavior of existing applications.
Example
To run the example provided in this project, follow these steps:
-
Navigate to the
examples/block_readingdirectory: -
View the contents of
/etc/passwdto observe the original behavior:
-
Build the example:
-
Use the
LD_PRELOADenvironment variable to load the hook and run the example:LD_PRELOAD=target/debug/libblock_reading.so
Usage
Installation
To use this framework, configure your Cargo.toml file as follows:
[]
= ["cdylib"]
[]
= "0.4"
= "0.2"
= "0.1"
libc is a required addition, ctor provides a helpful way of adding hooks on load.
Creating a Hook
Use the create_hook! macro to define a hook for a specific function. For example:
create_hook!;
Or, use the create_hooks! macro to define multiple hooks at once. For example:
create_hooks!;
This will generate hooks for both open and openat functions.
Adding a Hook
Define a hook function and add it using the add_hook method. The hook function's signature must match the original function's signature, with an additional Chain parameter as the last argument. For example:
add_hook;
The Chain parameter allows you to continue the chain of hooks towards the original function. You can also modify the parameters before calling the chain and adjust the result after the call.
Calling the Original Function
You can bypass hooks and call the original function directly:
let fd = call_orig;
Disabling and Enabling Hooks
You can manage the behavior of hooks using the following methods:
- Temporarily bypass hooks: Use
bypass_hooksto execute a block of code without triggering any hooks. - Permanently disable all hooks: Use
disable_hooksto stop hooks from being triggered globally until explicitly re-enabled. - Re-enable all hooks: Use
enable_hooksto restore hook functionality globally after it has been disabled.
Examples:
use File;
use ;
// Temporarily bypass all hooks
bypass_hooks;
// Permanently disable all hooks
disable_hooks;
// Use Rust's standard library to open and read a file without triggering hooks
let mut file = open.unwrap;
let mut contents = Stringnew;
file.read_to_string.unwrap;
println!;
// Re-enable all hooks
enable_hooks;