-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* New license: The MIT license * New Readme
- Loading branch information
Showing
5 changed files
with
76 additions
and
146 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2014-2015 Dmitry Rodionov | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,157 +1,76 @@ | ||
[![Build Status](https://travis-ci.org/rodionovd/rd_route.svg?branch=master)](https://travis-ci.org/rodionovd/rd_route) | ||
## rd_route | ||
Replace (aka «hook» or «override» or «route») implementation of any C function in runtime. Works on OS X with Mach-O binaries. | ||
|
||
> Do not use this code. It can destroy everthing. | ||
> But if you do, I wish you a luck. | ||
**NOTE**: `rd_route` **won't work on iOS**. You should take a look at [`libevil`](https://github.com/landonf/libevil_patch) instead. | ||
|
||
`rd_route` a tiny library for runtime replacing implementations of C functions in Mach-O executables. | ||
I've created this library because [`mach_override`](https://github.com/rentzsch/mach_override) is dramatically **assembly-dependent** which means that you have to either [use some disassembler library](https://github.com/rentzsch/mach_override/commit/9261b8efc94b87c5c7ad906759179ecf127d01e6) with it, or hardcode every(?) known opcodes to properly patch a function's prologue. It's *a lot of fking work*, so I just leave it. | ||
And let's be honest, `mach_override`'s code is quite old and ugly (IMO), but it rocks anyway. | ||
|
||
The source code is based on Landon Fuller's (@landonf) gorgeous [`libevil`](https://github.com/landonf/libevil_patch) library. | ||
|
||
**NOTE**: `rd_route` **won't work on iOS**. You should take a look at `libevil` instead. | ||
|
||
## Exported functions | ||
|
||
### rd_route() | ||
`int rd_route(void *function, void *replacement, void **original_ptr)` | ||
|
||
Override `function` to jump directly into `replacement` location. Caller can later access an original function's implementation via `original_ptr` (if passed). | ||
|
||
##### Arguments | ||
### Usage | ||
|
||
Argument | Type (in/out) | Description | ||
:--------: | :-----------: | :---------- | ||
`function` | in | _**(required)**_ pointer to a function to override | ||
`replecement` | in| _**(required)**_ pointer to new implementation | ||
`original_ptr` | out | *(optional)* will be set to an address of the original implementation's copy. | ||
|
||
Note that `original_ptr` won't be equal to `function` due to remapping the latter into a different memory space. | ||
|
||
##### Under the hood | ||
|
||
1. It calls `rd_duplicate_function()` ([see below](#rd_duplicate_function)) to remap an entire macho image, containing the `function` to override, into a new memory address | ||
2. The first bytes at `function` address are patched with a JMP instruction to the `replacement` address *(using a relative jump (0xE9) for i386 and an absolyte one (0xFF) for x86_64)*. | ||
3. The `original` pointer (if passed) is set to the remapped location of the `function` implementation. | ||
|
||
##### Usage | ||
```c | ||
#include <assert.h> | ||
#include "rd_route.h" | ||
|
||
static char* my_strerror(int err) | ||
{ | ||
return "It's OK"; | ||
} | ||
|
||
. . . | ||
|
||
void *(*original)(int) = NULL; | ||
int err = 2; | ||
|
||
printf("Error(%d): %s", err, strerror(err)); | ||
// >> No such file or directory | ||
|
||
rd_route(strerror, my_strerror, (void **)&original); | ||
|
||
printf("Error(%d): %s", err, strerror(err)); | ||
// >> "It's OK" | ||
printf("Error(%d): %s", err, original(err)); | ||
// >> No such file or directory | ||
``` | ||
------ | ||
### rd_route_byname() | ||
`int rd_route_byname(const char *function, const char *image, void *replacement, void **original);` | ||
The same as `rd_route()`, but the target function is defined with its name, not its symbol pointer. | ||
##### Arguments | ||
Argument | Type (in/out) | Description | ||
:--------: | :-----------: | :---------- | ||
`function` | in | _**(required)**_ name of a function to override | ||
`image` | in| *(optional)* name of a mach-o image containing the function. It may be NULL, a full file path or just a file name (e.g. "CoreFoundation") | ||
`replacement` | in | _**(required)**_ pointer to new implementation of the function | ||
`original` | out | *(optional)* will be set to an address of the original implementation's copy | ||
##### Under the hood | ||
*See `rd_route()` for more information.* | ||
If the `image_name` provided, `rd_route_byname()` looks for the function within it. Otherwise it iterates all images loaded into the current process' address space and, if there is more than one image containing a function with the specifed name, it will choose the first one. | ||
##### Usage | ||
```c | ||
#include "rd_route.h" | ||
|
||
static char* my_strerror(int err) | ||
{ | ||
return "It's OK"; | ||
} | ||
|
||
. . . | ||
int main (void) | ||
{ | ||
|
||
void *(*original)(int) = NULL; | ||
int err = 2; | ||
void *(*original)(int) = NULL; | ||
int err = 2; | ||
|
||
printf("Error(%d): %s", err, strerror(err)); | ||
// >> No such file or directory | ||
printf("Error(%d): %s", err, strerror(err)); | ||
// >> No such file or directory | ||
|
||
rd_route_byname("strerror", NULL, my_strerror, (void **)&original); | ||
rd_route(strerror, my_strerror, (void **)&original); | ||
|
||
// See if the patch works | ||
assert(0 == strcmp("It's OK", strerror(err))); | ||
// See if an original implementation is still available | ||
assert(0 == strcmp("No such file or directory", original(err))); | ||
|
||
printf("Error(%d): %s", err, strerror(err)); | ||
// >> "It's OK" | ||
printf("Error(%d): %s", err, original(err)); | ||
// >> No such file or directory | ||
return 0; | ||
} | ||
``` | ||
------ | ||
|
||
### rd_duplicate_function() | ||
`int rd_duplicate_function(void *function, void **duplicate)` | ||
|
||
Copy `function` implementation into another (first available) memory region. | ||
### Integration | ||
#### Using git submodules | ||
##### Arguments | ||
```bash | ||
$ cd /your/project/path | ||
$ git submodule add https://github.com/rodionovd/rd_route | ||
``` | ||
|
||
Argument | Type (in/out) | Description | ||
:--------: | :-----------: | :---------- | ||
`function` | in | _**(required)**_ pointer to a function to override | ||
`duplicate` | out| _**(required)**_ will be set to an address of the function's implementation copy | ||
#### Using CocoaPods | ||
|
||
##### Under the hood | ||
*Coming soon.* | ||
|
||
1. Remaps an entire mach-o image (containing the `function` address) into some new location *(for now, it's just a first available memory space we can find)*. | ||
2. Sets a `duplicate` pointer to address of the `function` symbol inside the duplicated mach-o image. | ||
|
||
##### Usage | ||
---- | ||
|
||
```c | ||
#include <stdint.h> | ||
#include "rd_route.h" | ||
### But wait, we already have `mach_override` for this stuff | ||
|
||
/* VIF */ | ||
static uint64_t very_important_function(uint64_t code) | ||
{ | ||
return (code + code % 13); | ||
} | ||
I've created this library because [`mach_override`](https://github.com/rentzsch/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 | ||
|
||
void *(*VIF_copy)(uint64_t) = NULL; | ||
uint64_t code = 0xDEADBEAF; | ||
/* | ||
* We don't want to lose our VIF at any point so backup it. | ||
*/ | ||
rd_duplicate_function(very_important_function, &VIF_copy); | ||
* The source code is based on Landon Fuller's (@landonf) gorgeous [`libevil`](https://github.com/landonf/libevil_patch) library. | ||
|
||
* I'm also glade we have Jonathan 'Wolf' Rentzsch out there with his classy [`mach_override`](https://github.com/rentzsch/mach_override) :+1: | ||
|
||
if (very_important_function(code) != VIF_copy(code)) { | ||
printf("Backup failed\n"); | ||
} else { | ||
printf("You can now do whatever you want with original very_important_function()\n"); | ||
``` | ||
------ | ||
|
||
If you found any bug(s) or something, please open an issue or a pull request — I'd appreciate your help! `(^,,^)` | ||
If you found any bug(s) or something, please open an issue or a pull request — I'd appreciate your help! `(^,,^)` | ||
|
||
------ | ||
|
||
*Dmitry Rodionov, 2014* | ||
*Dmitry Rodionov, 2014-2015* | ||
*[email protected]* | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
// Copyright © 2014 Dmitry Rodionov [email protected] | ||
// This work is free. You can redistribute it and/or modify it | ||
// under the terms of the Do What The Fuck You Want To Public License, Version 2, | ||
// as published by Sam Hocevar. See the COPYING file for more details. | ||
// rd_route.c | ||
// Copyright (c) 2014-2015 Dmitry Rodionov | ||
// | ||
// This software may be modified and distributed under the terms | ||
// of the MIT license. See the LICENSE file for details. | ||
|
||
#include <stdlib.h> // realloc() | ||
#include <libgen.h> // basename() | ||
#include <assert.h> // assert() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
// Copyright © 2014 Dmitry Rodionov [email protected] | ||
// This work is free. You can redistribute it and/or modify it | ||
// under the terms of the Do What The Fuck You Want To Public License, Version 2, | ||
// as published by Sam Hocevar. See the COPYING file for more details. | ||
|
||
// rd_route.h | ||
// Copyright (c) 2014-2015 Dmitry Rodionov | ||
// | ||
// This software may be modified and distributed under the terms | ||
// of the MIT license. See the LICENSE file for details. | ||
|
||
#ifndef RD_ROUTE | ||
#define RD_ROUTE | ||
|
@@ -23,7 +23,7 @@ | |
* | ||
* @return KERN_SUCCESS if succeeded, or other value if failed | ||
*/ | ||
int rd_route(void *function, void *replacement, void **original); | ||
int rd_route(void *function, void *replacement, void **original); | ||
|
||
/** | ||
* The same as rd_route(), but the target function is defined with its name, not its symbol pointer. | ||
|
@@ -39,7 +39,7 @@ | |
* | ||
* @return see rd_route() for the list of possible return values | ||
*/ | ||
int rd_route_byname(const char *function_name, const char *image_name, void *replacement, void **original); | ||
int rd_route_byname(const char *function_name, const char *image_name, void *replacement, void **original); | ||
|
||
/** | ||
* Copy `function` implementation into another (first available) memory region. | ||
|
@@ -48,7 +48,7 @@ | |
* | ||
* @return KERN_SUCCESS if succeeded, or other value if failed | ||
*/ | ||
int rd_duplicate_function(void *function, void **duplicate); | ||
int rd_duplicate_function(void *function, void **duplicate); | ||
|
||
#ifdef __cplusplus | ||
} | ||
|