Rebind symbols with fishhook
mainTo dynamically rebind symbols in Mach-O binaries on iOS (simulator or device), add fishhook.h and fishhook.c to your project and use the rebind_symbols function.
To use it, you must:
- Define function pointers to hold the original implementations (e.g.,
static int (*orig_close)(int);). - Create replacement functions that perform your desired logic and then call the original function pointer.
- Call
rebind_symbolswith an array ofstruct rebindingand the count of rebindings.
#import <dlfcn.h>
#import <UIKit/UIKit.h>
#import "fishhook.h"
static int (*orig_close)(int);
int my_close(int fd) {
printf("Calling real close(%d)\n", fd);
return orig_close(fd);
}
int main(int argc, char * argv[])
{
@autoreleasepool {
// Rebind 'close' to 'my_close' and store the original in 'orig_close'
rebind_symbols((struct rebinding[1]){{"close", my_close, (void *)&orig_close}}, 1);
close(0);
return 0;
}
}