fishhook

repository·main·Indexed 26 days ago

https://github.com/facebook/fishhook

A lightweight library for dynamically rebinding symbols in Mach-O binaries on iOS (simulator and device). It enables developers to intercept and hook function calls using the rebind_symbols function and struct rebinding, providing functionality similar to DYLD_INTERPOSE on macOS for debugging, tracing, and auditing system calls.

Tokens
498
Snippets
1
Records
2
Agent score
40%

What's inside fishhook

  1. Rebind symbols with fishhook

    main

    To 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:

    1. Define function pointers to hold the original implementations (e.g., static int (*orig_close)(int);).
    2. Create replacement functions that perform your desired logic and then call the original function pointer.
    3. Call rebind_symbols with an array of struct rebinding and 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;
      }
    }
  2. Use rebind_symbols() to hook functions

    main

    The rebind_symbols function is the primary API for intercepting function calls. It takes an array of struct rebinding structures and the number of elements in that array.

    Signature Concept: rebind_symbols(struct rebinding *symbols, size_t nbinds)

    struct rebinding members:

    • char *name: The name of the symbol to rebind (e.g., "close", "open").
    • void *replacement: A pointer to your replacement function.
    • void **orig: A pointer to a variable that will hold the address of the original function, allowing you to call it from your replacement.