Added all hook types and possibility for user data

This commit is contained in:
xavo95 2025-01-13 16:44:33 +01:00
parent 282da6f98b
commit 418aef083c
Signed by: xavo95
GPG key ID: CBF8ADED6DEBB783

View file

@ -1,5 +1,6 @@
use ilhook::x64::{ use ilhook::x64::{
CallbackOption, Hooker, HookFlags, HookPoint, HookType, JmpBackRoutine, RetnRoutine, CallbackOption, Hooker, HookFlags, HookPoint, HookType, JmpBackRoutine, JmpToAddrRoutine,
JmpToRetRoutine, RetnRoutine,
}; };
pub struct Interceptor { pub struct Interceptor {
@ -11,38 +12,98 @@ impl Interceptor {
Self { hooks: Vec::new() } Self { hooks: Vec::new() }
} }
#[allow(dead_code)] /// Creates an attach hook.
///
/// # Parameters
///
/// * `addr` - The being-hooked address.
/// * `routine` - The callback routine.
/// * `user_data` - The ptr to user defined data, caller must ensure data remains valid, otherwise its UB.
pub fn attach( pub fn attach(
&mut self, &mut self,
addr: usize, addr: usize,
routine: JmpBackRoutine, routine: JmpBackRoutine,
user_data: Option<usize>,
) -> Result<(), ilhook::HookError> { ) -> Result<(), ilhook::HookError> {
let hooker = Hooker::new( self.hook(Hooker::new(
addr, addr,
HookType::JmpBack(routine), HookType::JmpBack(routine),
CallbackOption::None, CallbackOption::None,
0, user_data.unwrap_or(0),
HookFlags::empty(), HookFlags::empty(),
); ))
let hook_point = unsafe { hooker.hook() }?;
self.hooks.push(hook_point);
Ok(())
} }
/// Creates a replace hook.
///
/// # Parameters
///
/// * `addr` - The being-hooked address.
/// * `routine` - The callback routine.
/// * `user_data` - The ptr to user defined data, caller must ensure data remains valid, otherwise its UB.
pub fn replace( pub fn replace(
&mut self, &mut self,
addr: usize, addr: usize,
routine: RetnRoutine, routine: RetnRoutine,
user_data: Option<usize>,
) -> Result<(), ilhook::HookError> { ) -> Result<(), ilhook::HookError> {
let hooker = Hooker::new( self.hook(Hooker::new(
addr, addr,
HookType::Retn(routine), HookType::Retn(routine),
CallbackOption::None, CallbackOption::None,
0, user_data.unwrap_or(0),
HookFlags::empty(), HookFlags::empty(),
); ))
}
/// Creates a jmp_to_ret hook.
///
/// # Parameters
///
/// * `addr` - The being-hooked address.
/// * `routine` - The callback routine.
/// * `user_data` - The ptr to user defined data, caller must ensure data remains valid, otherwise its UB.
pub fn jmp_to_ret(
&mut self,
addr: usize,
routine: JmpToRetRoutine,
user_data: Option<usize>,
) -> Result<(), ilhook::HookError> {
self.hook(Hooker::new(
addr,
HookType::JmpToRet(routine),
CallbackOption::None,
user_data.unwrap_or(0),
HookFlags::empty(),
))
}
/// Creates a jmp_to_addr hook.
///
/// # Parameters
///
/// * `addr` - The being-hooked address.
/// * `routine` - The callback routine.
/// * `jmp_addr` - The address to jump back to once done.
/// * `user_data` - The ptr to user defined data, caller must ensure data remains valid, otherwise its UB.
pub fn jmp_to_addr(
&mut self,
addr: usize,
routine: JmpToAddrRoutine,
jmp_addr: usize,
user_data: Option<usize>,
) -> Result<(), ilhook::HookError> {
self.hook(Hooker::new(
addr,
HookType::JmpToAddr(jmp_addr, routine),
CallbackOption::None,
user_data.unwrap_or(0),
HookFlags::empty(),
))
}
#[inline(always)]
fn hook(&mut self, hooker: Hooker) -> Result<(), ilhook::HookError> {
let hook_point = unsafe { hooker.hook() }?; let hook_point = unsafe { hooker.hook() }?;
self.hooks.push(hook_point); self.hooks.push(hook_point);
Ok(()) Ok(())