### Install Hook Function for Library Call Source: https://github.com/kubo/plthook/blob/master/README.md Use this code to intercept a function call (e.g., 'recv') within a specified shared library ('libfoo.so.1'). Ensure 'plthook.h' and the appropriate C file are included in your project. This method is suitable when the hook function is not part of the library being modified. ```c #include "plthook.h" /* This function is called instead of recv() called by libfoo.so.1 */ static ssize_t my_recv(int sockfd, void *buf, size_t len, int flags) { ssize_t rv; ... do your task: logging, etc. ... rv = recv(sockfd, buf, len, flags); /* call real recv(). */ ... do your task: logging, check received data, etc. ... return rv; } int install_hook_function() { plthook_t *plthook; if (plthook_open(&plthook, "libfoo.so.1") != 0) { printf("plthook_open error: %s\n", plthook_error()); return -1; } if (plthook_replace(plthook, "recv", (void*)my_recv, NULL) != 0) { printf("plthook_replace error: %s\n", plthook_error()); plthook_close(plthook); return -1; } plthook_close(plthook); return 0; } ``` -------------------------------- ### Install Hook Function When Hook is Inside Modified File Source: https://github.com/kubo/plthook/blob/master/README.md This code handles the scenario where the hook function resides within the same file being modified by PLTHook. It uses `plthook_open_by_address` and passes the address of the original function pointer to `plthook_replace`. Platform-specific handling for obtaining the original function address (e.g., using `dlsym` on Unix) is included. ```c static ssize_t (*recv_func)(int sockfd, void *buf, size_t len, int flags); /* This function is called instead of recv() called by libfoo.so.1 */ static ssize_t my_recv(int sockfd, void *buf, size_t len, int flags) { ssize_t rv; ... do your task: logging, etc. ... rv = (*recv_func)(sockfd, buf, len, flags); /* call real recv(). */ ... do your task: logging, check received data, etc. ... return rv; } int install_hook_function() { plthook_t *plthook; if (plthook_open_by_address(&plthook, &recv_func) != 0) { printf("plthook_open error: %s\n", plthook_error()); return -1; } if (plthook_replace(plthook, "recv", (void*)my_recv, (void**)&recv_func) != 0) { printf("plthook_replace error: %s\n", plthook_error()); plthook_close(plthook); return -1; } #ifndef WIN32 // The address passed to the fourth argument of plthook_replace() is // available on Windows. But not on Unixes. Get the real address by dlsym(). recv_func = (ssize_t (*)(int, void *, size_t, int))dlsym(RTLD_DEFAULT, "recv"); #endif plthook_close(plthook); return 0; } ``` -------------------------------- ### Enumerate PLT/IAT Entries in C Source: https://github.com/kubo/plthook/blob/master/README.md Use this function to iterate through and print information about PLT/IAT entries within a specified executable file. Ensure the plthook_t pointer is initialized to NULL and the position counter is set to zero before calling. ```c void print_plt_entries(const char *filename) { plthook_t *plthook; unsigned int pos = 0; /* This must be initialized with zero. */ const char *name; void **addr; if (plthook_open(&plthook, filename) != 0) { printf("plthook_open error: %s\n", plthook_error()); return -1; } while (plthook_enum(plthook, &pos, &name, &addr) == 0) { printf("%p(%p) %s\n", addr, *addr, name); } plthook_close(plthook); return 0; } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.