ilhook

Build status

This crate provides methods to inline hook binary codes of x86 and x64 instruction sets.

HOOK is a mechanism that intercepts function calls and handles them by user-defined code.

Installation

This crate works with Cargo and is on crates.io. Add it to your Cargo.toml like so:

toml [dependencies] ilhook = "1.0"

Hook Types

Ilhook supports 4 types of hooking.

Jmp-back hook

This type is used when you want to get some information, or modify some values (parameters, stack vars, heap vars, etc.) at the specified timing.

Assume we have a C++ function:

```cpp void checkserialnumber(std::string& sn){ uint32t machinehash = getmachinehash(); uint32t snhash = calc_hash(sn);

// we want to modify the result of this comparison.
if (sn_hash == machine_hash) {
    // success
}
// fail

} ```

And it compiles to the asm code:

```asm 0x401054 call getmachinehash ;getmachinehash() 0x401059 mov ebx, eax

; ...

0x401070 lea eax, sn 0x401076 push eax 0x401077 call calchash ;calchash(sn) 0x40107C add esp, 4 0x40107F cmp eax, ebx ;we want to modify the eax here! 0x401081 jnz checkfail

; check_success ```

Now let's start:

```rust use ilhook::x86::{Hooker, HookType, Registers, CallbackOption, HookFlags};

unsafe extern "cdecl" fn onchecksn( reg: mut Registers, _: usize ) { println!("m_hash: {}, sn_hash: {}", (reg).ebx, (reg).eax); (reg).eax = (*reg).ebx; //we modify the sn_hash! }

let hooker = Hooker::new( 0x40107F, HookType::JmpBack(onchecksn), CallbackOption::None, HookFlags::empty(), ); hooker.hook().unwrap(); ```

Then checkserialnumber will always go to the successful path.

Function hook

This type is used when you want to replace a function with your customized function. Note that you should only hook at the beginning of a function.

Assume we have a function:

```rust fn foo(x: u64) -> u64 { x * x }

assert_eq!(foo(5), 25); ```

And you want to let it return x*x+3, which means foo(5)==28.

Now let's hook:

```rust use ilhook::x64::{Hooker, HookType, Registers, CallbackOption, HookFlags};

unsafe extern "sysv64" fn new_foo( reg: mut Registers, _ :usize, _ :usize ) -> usize { let x = (&reg).rdi as usize; x * x + 3 }

let hooker = Hooker::new( foo as usize, HookType::Retn(newfoo), CallbackOption::None, HookFlags::empty(), ); unsafe { hooker.hook().unwrap() }; asserteq!(foo(5), 28); ```

Jmp-addr hook

This type is used when you want to change the original run path to any other you wanted.

The first element of the enum HookType::JmpToAddr indicates where you want the EIP jump to after the callback routine returns.

Jmp-ret hook

This type is used when you want to change the original run path to any other you wanted, and the destination address may change by the input arguments.

The EIP will jump to the value the callback routine returns.

Notes

This crate is not thread-safe if you don't specify HookFlags::NOTMODIFYMEMORY_PROTECT. Of course, you need to modify memory protection of the destination address by yourself if you specify that.

As rust's test run parrallelly, it may crash if not specify --test-threads=1.