Skip to content

Latest commit

History

87 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

rd_route

Replace (aka «hook» or «override» or «route») implementation of any C function in runtime. Works on MacOS with Mach–O binaries.

NOTE: rd_routewon't work on iOS.

Usage

#include<assert.h>#include<string.h>#include<stdio.h>#include"rd_route.h"staticchar*my_strerror(interr)
{
return"It's OK";
}
staticchar*my_super_strerror(interr)
{
return"It's super OK";
}
intmain (void)
{
void*(*original)(int) =NULL;
interr=2;
// hook strerror with my_strerror and backup implementation at originalrd_route(strerror, my_strerror, (void**)&original);
// See if the patch worksassert(0==strcmp("It's OK", strerror(err)));
// See if an original implementation is still availableassert(0==strcmp("No such file or directory", original(err)));
// hook my_strerror by name with my_super_strerror and backup patched implementation at originalrd_route_byname("my_strerror", NULL, my_super_strerror, (void**)&original);
// See if the patch by name worksassert(0==strcmp("It's super OK", my_strerror(err)));
assert(0==strcmp("It's super OK", strerror(err)));
// See if an original patched implementation is still availableassert(0==strcmp("It's OK", original(err)));
return0;
}

Integration

Using git submodules

$ cd /your/project/path
$ git submodule add https://github.com/rodionovd/rd_route

Not using git submodules

Just copy rd_route.h and rd_route.c files into your project's directory.


But wait, we already have mach_override for this stuff

I've created this library because mach_override requires an external disassembler in order to work properly. For those of us who don't want another few thousands of lines of foreign code in their projects, the only option is to hard-code every function prologue they know in order to patch it correctly — which isn't a great alternative to have, to be honest.

Credits

  • The source code is based on Landon Fuller's (@landonf) gorgeous libevil library.

  • I'm also glade we have Jonathan 'Wolf' Rentzsch out there with his classy mach_override 👍


If you found any bug(s) or something, please open an issue or a pull request — I'd appreciate your help! (^,,^)


Dmitry Rodionov, 2014-2015
i.am.rodionovd@gmail.com

About

Function hooking for macOS

Resources

Stars

9 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages