Skip to content

Instantly share code, notes, and snippets.

@SF-Zhou
Created October 19, 2019 03:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SF-Zhou/ee35c02a597c7645756c704cc2df33fe to your computer and use it in GitHub Desktop.
Save SF-Zhou/ee35c02a597c7645756c704cc2df33fe to your computer and use it in GitHub Desktop.
Linux Sys Func Hook
#include <assert.h>
#include <dlfcn.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
static bool is_hook = false;
int main() {
int fd = open("/dev/null", O_WRONLY);
const char *buffer = "hello dlsym";
size_t len = write(fd, buffer, strlen(buffer));
assert(len == strlen(buffer));
assert(is_hook);
close(fd);
}
typedef ssize_t (*WriteFunc)(int fd, const void *buf, size_t nbyte);
static WriteFunc sys_write = (WriteFunc)dlsym(RTLD_NEXT, "write"); // need C++
ssize_t write(int fd, const void *buf, size_t nbyte) {
is_hook = true;
return sys_write(fd, buf, nbyte);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment