Created
August 14, 2016 23:33
-
-
Save scoates/e3883858ab3ea0d6e8a21ec7df0d9dc7 to your computer and use it in GitHub Desktop.
Hook into libc's open
This file contains 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
#define _GNU_SOURCE | |
#include <stdio.h> | |
#include <dlfcn.h> | |
#include <sys/types.h> | |
#include <stdarg.h> | |
int open(const char* pathname, int flags, ...) | |
{ | |
int (*libc_open)(const char*, int, ...); | |
printf("In our own open, opening %s\n", pathname); | |
va_list ap; | |
mode_t mode; | |
va_start(ap, flags); | |
mode = va_arg(ap, mode_t); | |
va_end(ap); | |
libc_open = dlsym(RTLD_NEXT, "open"); | |
return (*libc_open)(pathname,flags,mode); | |
} | |
/* | |
build with: gcc -Wall -fPIC -shared -o loudopen.so loudopen.c -ldl | |
run with: LD_PRELOAD=./loudopen.so cmd args | |
$ echo "foo">bar.txt | |
$ LD_PRELOAD=./loudopen.so cat bar.txt | |
In our own open, opening bar.txt | |
foo | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment