Crates.io | rhook |
lib.rs | rhook |
version | 0.8.0 |
source | src |
created_at | 2021-04-03 20:35:19.937774 |
updated_at | 2021-04-15 17:10:47.316828 |
description | Hook libc functions with an easy API |
homepage | |
repository | |
max_upload_size | |
id | 378452 |
size | 191,890 |
Hook libc functions with an easy API
1- Import the trait [RunHook]
2- Create an Command with Command::new and add hooks to it via add_hook and add_hooks methods
3- Confirm the hooks with set_hooks method this step is necessary
3.1- Hooks are closures that takes no input and return an option of the libc function as output.
If the closure return None
that is equivalent to returning Some(original_function(args))
in
other words it will run and use the original function output
Inside the closure you have access to the libc function input + some imports from std (see src/scaffold.rs)
4- Now you can carry on with the usual Command methods (output, spawn,status,..)
Tricks:
The closure used for hooks have acess to many things: (imported by https://github.com/sigmaSd/Rhook/blob/master/src/scaffold.rs)
original_$libcfn
this is useful in particular to avoid recursiontransmute
ManuallyDrop
CString
and a static mut COUNTER
.map_err(|e|println("{}",e))
after set_hooks
in order to prettify the dynamic library compiling error while debuggingSay you want to limit the bandwidth of a program
Usually downloading calls libc::recv
function
So our goal is to throttle it with a simple sleep
To do that with this crate: (taking speedtest program as an example)
1- Look up its doc's here recv to see what the function's input/output is
2- use this crate
use rhook::{RunHook, Hook};
std::process::Command::new("speedtest").add_hook(Hook::recv(stringify!(||{
std::thread::sleep(std::time::Duration::from_millis(10));
// since we're not doing any modification to the output you can just return None here
Some(original_recv(socket, buf, len, flags))
}))).set_hooks().unwrap().spawn();
Thats it!
Note that you have acess inside the closure to the original function denoted by the prefix
original_
+ the function name
Check out the examples for more info
License: MIT