This sample example demonstrates how to hack libc functions, such as open\close\read\write, etc.
You could make a dynamic lib file your_write_lib.so , which implements a new write function.
/* your_write_lib.c */
#define _GNU_SOURCE
#include <stdlib.h>
#include <stdio.h>
#include <dlfcn.h>
#include <stdarg.h>
ssize_t write (int fd, const void * buf, size_t count){
ssize_t (*glibc_write)(int fd, const void * buf, size_t count);
glibc_write = dlsym(RTLD_NEXT, "write");
// do whatever you want
return glibc_write(fd, buffer, strlen(buffer));
}
compile your_write_lib.c into .so file:
gcc -Wall -fPIC -shared -o your_write_lib.so your_write_lib.c -ldl
Then run your Application like this:
LD_PRELOAD=./your_write_lib.so your_application
With the help of LD_PRELOAD=./your_write_lib.so, the Linux OS will load your_write_lib.so first, then the glibc. The new open will hide the origin open function of glibc to your Application. You CAN do anything in the new open function, such as "do something before writing".