GithubHelp home page GithubHelp logo

tempbottle / detour-rs Goto Github PK

View Code? Open in Web Editor NEW

This project forked from darfink/detour-rs

0.0 1.0 0.0 4.19 MB

A cross-platform detour library written in Rust

License: Other

Shell 1.55% Rust 98.45%

detour-rs's Introduction

detour-rs

Travis build status Appveyor build status crates.io version Documentation Language (Rust)

This is a cross-platform detour library developed in Rust. Beyond the basic functionality, this library handles branch redirects, RIP-relative instructions, hot-patching, NOP-padded functions, and allows the original function to be called using a trampoline whilst hooked.

This is one of few cross-platform detour libraries that exists, and to maintain this feature, not all desired functionality can be supported due to lack of cross-platform APIs. Therefore EIP relocation is not supported.

NOTE: Nightly is currently required for static_detours!.

Platforms

  • x86/x64: Windows, Linux & macOS.
  • ARM: Not implemented, but foundation exists.

Installation

Add this to your Cargo.toml:

[dependencies]
detour = "0.5.0"

... and this to your crate root:

#[macro_use]
extern crate detour;

Example

  • A static detour (one of three different detours):
#[macro_use] extern crate detour;

extern "C" fn add(x: i32, y: i32) -> i32 {
    x + y
}

static_detours! {
    struct DetourAdd: extern "C" fn(i32, i32) -> i32;
}

fn main() {
    // Replaces the 'add' function with a closure that subtracts
    let mut hook = unsafe { DetourAdd.initialize(add, |x, y| x - y).unwrap() };

    assert_eq!(add(1, 5), 6);
    assert_eq!(hook.is_enabled(), false);

    unsafe { hook.enable().unwrap(); }

    assert_eq!(add(1, 5), -4);
    assert_eq!(hook.call(1, 5), 6);

    // Change the detour whilst hooked
    hook.set_detour(|x, y| x * y);
    assert_eq!(add(5, 5), 25);

    unsafe { hook.disable().unwrap(); }

    assert_eq!(hook.is_enabled(), false);
    assert_eq!(hook.call(1, 5), 6);
    assert_eq!(add(1, 5), 6);
}

Mentions

Part of the library's external user interface was inspired by minhook-rs, created by Jascha-N, and it contains derivative code of his work.

Appendix

  • EIP relocation

    Should be performed whenever a function's prolog instructions are being executed, simultaneously as the function itself is being detoured. This is done by halting all affected threads, copying the related instructions and appending a JMP to return to the function. This is barely ever an issue, and never in single-threaded environments, but YMMV.

  • NOP-padding

    int function() { return 0; }
    // xor eax, eax
    // ret
    // nop
    // nop
    // ...

    Functions such as this one, lacking a hot-patching area, and too small to be hooked with a 5-byte jmp, are supported thanks to the detection of code padding (NOP/INT3 instructions). Therefore the required amount of trailing NOP instructions will be replaced, to make room for the detour.

detour-rs's People

Contributors

darfink avatar definitelynobody avatar flawedmatrix avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.