### Build and Install xHook Native Libraries Source: https://github.com/iqiyi/xhook/blob/master/README.md Builds and installs the native libraries for xHook. Ensure you have Android NDK r16b set up in your PATH. ```bash ./build_libs.sh ./install_libs.sh ``` -------------------------------- ### Install xHook Demo App Source: https://github.com/iqiyi/xhook/blob/master/README.md Builds and installs the debug version of the xHook demo application. This is typically done after building the native libraries. ```bash cd ./xhookwrapper/ ./gradlew assembleDebug adb install ./app/build/outputs/apk/debug/app-debug.apk ``` -------------------------------- ### Register and Ignore Hooks for Tracking Source: https://github.com/iqiyi/xhook/blob/master/README.md Register hooks for mmap and munmap in system and vendor paths, while ignoring linker and linker64. ```c //tracking (ignore linker and linker64) xhook_register("^/system/.*$", "mmap", my_mmap, NULL); xhook_register("^/vendor/.*$", "munmap", my_munmap, NULL); xhook_ignore (".*/linker$", "mmap"); xhook_ignore (".*/linker$", "munmap"); xhook_ignore (".*/linker64$", "mmap"); xhook_ignore (".*/linker64$", "munmap"); ``` -------------------------------- ### xhook_enable_sigsegv_protection Source: https://github.com/iqiyi/xhook/blob/master/README.md Enables or disables segmentation fault protection (SFP). ```APIDOC ## xhook_enable_sigsegv_protection ### Description Enables or disables segmentation fault protection (SFP). ### Function Signature ```c void xhook_enable_sigsegv_protection(int flag); ``` ### Parameters - **flag** (int) - Pass `1` to enable SFP, `0` to disable. SFP is enabled by default. ### Notes - SFP is recommended for release builds to prevent app crashes. Disable SFP for debug builds to catch coding mistakes. - SFP uses `sigaction()`, `SIGSEGV`, `siglongjmp()`, and `sigsetjmp()` to prevent harmless crashes. ``` -------------------------------- ### Apply Registered Hooks Source: https://github.com/iqiyi/xhook/blob/master/README.md Performs the actual PLT hooking operations based on registered information. Can be executed asynchronously by passing 1 to 'async'. ```c int xhook_refresh(int async); ``` -------------------------------- ### Register Hooks for Socket Lifecycle Inspection Source: https://github.com/iqiyi/xhook/blob/master/README.md Register hooks for socket-related functions to inspect their lifecycle. ```c //inspect sockets lifecycle xhook_register(".*\\.so$", "getaddrinfo", my_getaddrinfo, NULL); xhook_register(".*\\.so$", "socket", my_socket, NULL); xhook_register(".*\\.so$", "setsockopt" my_setsockopt, NULL); xhook_register(".*\\.so$", "bind", my_bind, NULL); xhook_register(".*\\.so$", "listen", my_listen, NULL); xhook_register(".*\\.so$", "connect", my_connect, NULL); xhook_register(".*\\.so$", "shutdown", my_shutdown, NULL); xhook_register(".*\\.so$", "close", my_close, NULL); ``` -------------------------------- ### Register Hook to Fix System Bugs Source: https://github.com/iqiyi/xhook/blob/master/README.md Register a hook to fix a specific system bug in a vendor library. ```c //fix some system bug xhook_register(".*some_vendor.*/libvictim\\.so$", "bad_func", my_nice_func, NULL); ``` -------------------------------- ### xhook_enable_debug Source: https://github.com/iqiyi/xhook/blob/master/README.md Enables or disables debug information logging to logcat. ```APIDOC ## xhook_enable_debug ### Description Enables or disables debug information logging to logcat. ### Function Signature ```c void xhook_enable_debug(int flag); ``` ### Parameters - **flag** (int) - Pass `1` to enable debug info, `0` to disable. Debug info is disabled by default. ### Notes - Debug info will be sent to logcat with the tag `xhook`. ``` -------------------------------- ### Enable/Disable Segmentation Fault Protection (SFP) Source: https://github.com/iqiyi/xhook/blob/master/README.md Enables or disables the segmentation fault protection mechanism. SFP is enabled by default and recommended for release builds to prevent crashes. Disable for debug builds to catch coding errors. ```c void xhook_enable_sigsegv_protection(int flag); ``` -------------------------------- ### Register Hooks for Defense Against Injection Attacks Source: https://github.com/iqiyi/xhook/blob/master/README.md Register hooks to defend against injection attacks, such as always returning NULL for malloc or recording connect calls. ```c //defense to some injection attacks xhook_register(".*com\\.hacker.*\\.so$", "malloc", my_malloc_always_return_NULL, NULL); xhook_register(".*/libhacker\\.so$", "connect", my_connect_with_recorder, NULL); ``` -------------------------------- ### Ignore All Hooks in a Specific Library Source: https://github.com/iqiyi/xhook/blob/master/README.md Ignore all hooks for the libwebviewchromium.so library. ```c //ignore all hooks in libwebviewchromium.so xhook_ignore(".*/libwebviewchromium.so$", NULL); ``` -------------------------------- ### xhook_register Source: https://github.com/iqiyi/xhook/blob/master/README.md Registers a hook for a specific symbol in ELF files matching a given pathname regular expression. The original function pointer is saved. ```APIDOC ## xhook_register ### Description Registers a hook for a specific symbol in ELF files matching a given pathname regular expression. The original function pointer is saved. ### Function Signature ```c int xhook_register(const char *pathname_regex_str, const char *symbol, void *new_func, void **old_func); ``` ### Parameters - **pathname_regex_str** (const char *) - Regular expression string to match ELF file paths. - **symbol** (const char *) - The symbol name to hook. - **new_func** (void *) - Pointer to the new function to replace the original. - **old_func** (void **) - Pointer to a variable where the original function pointer will be stored. ### Return Value Returns zero if successful, non-zero otherwise. ### Notes - The regular expression for `pathname_regex_str` only supports POSIX BRE. - The `new_func` must have the same function declaration as the original one. ``` -------------------------------- ### xhook_refresh Source: https://github.com/iqiyi/xhook/blob/master/README.md Performs the actual hook operations based on registered hook information. ```APIDOC ## xhook_refresh ### Description Performs the actual hook operations based on registered hook information. ### Function Signature ```c int xhook_refresh(int async); ``` ### Parameters - **async** (int) - Pass `1` for asynchronous hook, `0` for synchronous hook. ### Return Value Returns zero if successful, non-zero otherwise. ### Notes - This function updates a global cache of ELF loading info from `/proc/self/maps` to determine which newly loaded ELFs need hooking. ``` -------------------------------- ### xhook_clear Source: https://github.com/iqiyi/xhook/blob/master/README.md Clears all cache owned by xhook and resets global flags. ```APIDOC ## xhook_clear ### Description Clears all cache owned by xhook and resets all global flags to their default values. ### Function Signature ```c void xhook_clear(); ``` ### Notes - This function can be called after confirming all desired PLT entries have been hooked to save memory. ``` -------------------------------- ### Register Hooks for Memory Leak Detection Source: https://github.com/iqiyi/xhook/blob/master/README.md Register hooks for malloc, calloc, realloc, and free functions to detect memory leaks in .so files. ```c //detect memory leaks xhook_register(".*\\.so$", "malloc", my_malloc, NULL); xhook_register(".*\\.so$", "calloc", my_calloc, NULL); xhook_register(".*\\.so$", "realloc", my_realloc, NULL); xhook_register(".*\\.so$", "free", my_free, NULL); ``` -------------------------------- ### Refresh Hooks Source: https://github.com/iqiyi/xhook/blob/master/README.md Refresh all registered hooks. The argument '1' indicates an immediate refresh. ```c //hook now! xhook_refresh(1); ``` -------------------------------- ### Register a PLT Hook Source: https://github.com/iqiyi/xhook/blob/master/README.md Registers a hook for a specific symbol within ELF files matching a pathname regex. The original function pointer is saved in old_func. Requires the new_func to have the same signature as the original. ```c int xhook_register(const char *pathname_regex_str, const char *symbol, void *new_func, void **old_func); ``` -------------------------------- ### Register Hooks for Android Log Filtering Source: https://github.com/iqiyi/xhook/blob/master/README.md Register hooks for Android logging functions to filter and save logs to a local file. ```c //filter off and save some android log to local file xhook_register(".*\\.so$", "__android_log_write", my_log_write, NULL); xhook_register(".*\\.so$", "__android_log_print", my_log_print, NULL); xhook_register(".*\\.so$", "__android_log_vprint", my_log_vprint, NULL); xhook_register(".*\\.so$", "__android_log_assert", my_log_assert, NULL); ``` -------------------------------- ### xhook_ignore Source: https://github.com/iqiyi/xhook/blob/master/README.md Ignores previously registered hook information based on pathname and symbol. ```APIDOC ## xhook_ignore ### Description Ignores previously registered hook information based on pathname and symbol. ### Function Signature ```c int xhook_ignore(const char *pathname_regex_str, const char *symbol); ``` ### Parameters - **pathname_regex_str** (const char *) - Regular expression string to match ELF file paths. - **symbol** (const char *) - The symbol name to ignore. If NULL, all symbols from matching ELFs are ignored. ### Return Value Returns zero if successful, non-zero otherwise. ### Notes - The regular expression for `pathname_regex_str` only supports POSIX BRE. ``` -------------------------------- ### Enable/Disable Debug Logging Source: https://github.com/iqiyi/xhook/blob/master/README.md Enables or disables debug information output to logcat with the tag 'xhook'. Debugging is disabled by default. ```c void xhook_enable_debug(int flag); ``` -------------------------------- ### Clear xHook Cache Source: https://github.com/iqiyi/xhook/blob/master/README.md Clears all internal caches and resets global flags. This can be called after confirming all desired hooks have been applied to free up memory. ```c void xhook_clear(); ``` -------------------------------- ### Ignore a Registered Hook Source: https://github.com/iqiyi/xhook/blob/master/README.md Ignores previously registered hook information based on a pathname regex and symbol. If the symbol is NULL, all symbols from matching ELFs are ignored. ```c int xhook_ignore(const char *pathname_regex_str, const char *symbol); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.