Skip to main content

Crate imping_hook

Crate imping_hook 

Source
Expand description

In-process function hooking for the imping toolkit.

Installs typed function detours and code injections at arbitrary addresses in the running process, and depends on nothing else in imping. Relocation, near-memory allocation and thread-safe patching come from the [detour] crate, wrapped behind this crate’s types.

§Examples

use imping_hook::Hook;

#[inline(never)]
extern "C" fn add(a: i32, b: i32) -> i32 { a + b }
extern "C" fn sub(a: i32, b: i32) -> i32 { a - b }

// SAFETY: `add` is a real function with the signature below, and `sub`
// shares it.
let hook = unsafe {
    Hook::at(add as *const ())
        .redirect(sub as extern "C" fn(i32, i32) -> i32)
        .install()?
};

hook.enable()?;
assert_eq!(add(3, 2), 1);
assert_eq!(hook.original().call(3, 2), 5);

§Main items

  • Hook redirects a function to another of the same signature and keeps the original callable. Its docs cover threads, batches and dropping.
  • Injection runs machine code, assembly or a register callback at an address, then the original code.
  • scan finds code by a byte pattern; Hook::scan and Injection::scan patch what it finds.
  • batch applies many enables and disables with one thread suspension.
  • Context holds the registers a register callback sees.
  • Error lists everything that can fail.

Modules§

asm
Re-exports the assembler used by InjectionBuilder::asm.
scan
Finding code by a byte pattern.

Structs§

Context
The x86-64 registers at an injection site: general-purpose registers, flags, instruction pointer and SSE registers.
Hook
A typed function detour, and the RAII guard that owns it.
HookBuilder
A hook builder holding the target address, returned by Hook::at.
HookScan
A hook builder holding a byte pattern to find the target by, returned by Hook::scan.
HookWith
A hook builder holding the target and detour, returned by HookBuilder::redirect and HookScan::redirect.
Injection
A code injection, and the RAII guard that owns it.
InjectionBuilder
An injection builder holding the target address, returned by Injection::at.
InjectionScan
An injection builder holding a byte pattern to find the target by, returned by Injection::scan.
InjectionWith
An injection builder holding the target and what to inject, returned by the InjectionBuilder and InjectionScan methods.
Original
The original, unhooked function of a Hook, borrowed from it.
OsError
A failed operating system call, such as allocating or protecting memory or suspending a thread.
Xmm
The 128 bits of one SSE (xmm) register.

Enums§

Error
Everything that can go wrong when finding, installing, enabling or disabling a hook.

Traits§

Function
A function pointer type that can be a Hook target and detour.

Functions§

batch
Runs f, then applies the enables and disables it made on this thread in one transaction, so other threads are suspended once rather than per patch.

Type Aliases§

Result
The result of a hooking operation.