A simple hooking library for C/C++
Go to file
2018-11-04 22:26:08 +06:00
cmake Fix FindYasm error on Windows 2018-09-01 17:23:03 +06:00
tests Fix CMake 2.8.12 error and bump min CMake version 2018-09-08 00:49:09 +06:00
.editorconfig Convert tabs to spaces 2016-07-24 05:18:51 +06:00
.gitattributes Add .gitattributes 2015-01-09 14:48:58 +06:00
.travis.yml Add missing apt packages to .travis.yml 2018-09-08 01:27:28 +06:00
appveyor.yml Fix issues with 64-bit build of test_exe 2018-09-01 16:32:09 +06:00
CMakeLists.txt Add option to disable #inclusion of platform-specific files into subhook.c 2018-11-04 21:32:38 +06:00
README.md Update known issues 2018-11-04 15:15:56 +06:00
subhook_private.h Rename "options" to "flags" 2018-09-06 23:59:35 +06:00
subhook_unix.c Clean up and improve comments 2018-09-03 20:10:28 +06:00
subhook_windows.c Clean up and improve comments 2018-09-03 20:10:28 +06:00
subhook_x86.c Add support for for XOR, ADD, AND, OR, ENTER, LEAVE, INT 3 2018-11-04 22:26:08 +06:00
subhook.c Add option to disable #inclusion of platform-specific files into subhook.c 2018-11-04 21:32:38 +06:00
subhook.h Rename "options" to "flags" 2018-09-06 23:59:35 +06:00

Build Status Build Status - Windows

SubHook is a super-simple hooking library for C and C++ that works on Windows, Linux and macOS. It supports x86 only (32-bit and 64-bit).

Installation

Simply copy the files to your project and include subhook.c in your build. The other source files wil be #included by the main C file automatically depending on the OS and achitecture.

Use of CMake is not mandatory, the library can be built wihtout it (no extra build configuration required).

Examples

In the following examples foo is some function or a function pointer that takes a single argument of type int and uses the same calling convention as my_foo (depends on compiler).

Basic usage

#include <stdio.h>
#include <subhook.h>

subhook_t foo_hook;

void my_foo(int x) {
  /* Remove the hook so that you can call the original function. */
  subhook_remove(foo_hook);

  printf("foo(%d) called\n", x);
  foo(x);

  /* Install the hook back to intercept further calls. */
  subhook_install(foo_hook);
}

int main() {
  /* Create a hook that will redirect all foo() calls to to my_foo(). */
  foo_hook = subhook_new((void *)foo, (void *)my_foo, 0);

  /* Install it. */
  subhook_install(foo_hook);

  foo(123);

  /* Remove the hook and free memory when you're done. */
  subhook_remove(foo_hook);
  subhook_free(foo_hook);
}

Trampolines

Using trampolines allows you to jump to the original code without removing and re-installing hooks every time your function gets called.

typedef void (*foo_func)(int x);

void my_foo(int x) {
  printf("foo(%d) called\n", x);

  /* Call foo() via trampoline. */
  ((foo_func)subhook_get_trampoline(foo_hook))(x);
}

int main() {
   /* Same code as in previous example. */
}

Please note that subhook has a very simple length disassmebler engine (LDE) that works only with most common prologue instructions like push, mov, call, etc. When it encounters an unknown instruction subhook_get_trampoline() will return NULL.

C++

#include <iostream>
#include <subhook.h>

subhook::Hook foo_hook;
subhook::Hook foo_hook_tr;

typedef void (*foo_func)(int x);

void my_foo(int x) {
  // ScopedHookRemove removes the specified hook and automatically re-installs
  // it when the objectt goes out of scope (thanks to C++ destructors).
  subhook::ScopedHookRemove remove(&foo_hook);

  std::cout << "foo(" << x << ") called" << std::endl;
  foo(x + 1);
}

void my_foo_tr(int x) {
  std::cout << "foo(" << x << ") called" << std::endl;

  // Call the original function via trampoline.
  ((foo_func)foo_hook_tr.GetTrampoline())(x + 1);
}

int main() {
  foo_hook.Install((void *)foo, (void *)my_foo);
  foo_hook_tr.Install((void *)foo, (void *)my_foo_tr);
}

Known issues

  • subhook_get_trampoline() may return NULL because only a small subset of x86 instructions is supported by the disassembler in this library (only common prologue instructions). As a workaround you can plug in a more advanced disassembler engine in subhook_disasm() (currently there is no simple way to do it, you have to modify the code manually).

  • If a target function (the function you are hooking) is less than N bytes in length, for example if it's a short 2-byte jump to a nearby location (sometimes compilers generate code like this), then you will not be able to hook it.

    N is 5 by default: 1 byte for jmp opcode + 4 bytes for offset. But if you enable the use of 64-bit offsets in 64-bit mode N becomes 14 (see the definition of subhook_jmp64).

  • Some systems protect executable code form being modified at runtime, which will not allow you to install hooks, or don't allow to mark heap-allocated memory as executable, which prevents the use of trampolines.

    For example, on Fedora you can have such problems because of SELinux (though you can disable it or exclude your files).

License

Licensed under the 2-clause BSD license.