From 418aef083cc6768201f0512fdbdca5c03aa4f787 Mon Sep 17 00:00:00 2001 From: xavo95 Date: Mon, 13 Jan 2025 16:44:33 +0100 Subject: [PATCH] Added all hook types and possibility for user data --- src/lib.rs | 85 ++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 73 insertions(+), 12 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 81209e7..4dcada9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,6 @@ use ilhook::x64::{ - CallbackOption, Hooker, HookFlags, HookPoint, HookType, JmpBackRoutine, RetnRoutine, + CallbackOption, Hooker, HookFlags, HookPoint, HookType, JmpBackRoutine, JmpToAddrRoutine, + JmpToRetRoutine, RetnRoutine, }; pub struct Interceptor { @@ -11,38 +12,98 @@ impl Interceptor { 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( &mut self, addr: usize, routine: JmpBackRoutine, + user_data: Option, ) -> Result<(), ilhook::HookError> { - let hooker = Hooker::new( + self.hook(Hooker::new( addr, HookType::JmpBack(routine), CallbackOption::None, - 0, + user_data.unwrap_or(0), 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( &mut self, addr: usize, routine: RetnRoutine, + user_data: Option, ) -> Result<(), ilhook::HookError> { - let hooker = Hooker::new( + self.hook(Hooker::new( addr, HookType::Retn(routine), CallbackOption::None, - 0, + user_data.unwrap_or(0), 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, + ) -> 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, + ) -> 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() }?; self.hooks.push(hook_point); Ok(())