Created
October 19, 2019 03:37
-
-
Save SF-Zhou/ee35c02a597c7645756c704cc2df33fe to your computer and use it in GitHub Desktop.
Linux Sys Func Hook
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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