Skip to content

Instantly share code, notes, and snippets.

@luhenry
Created May 9, 2021 12:52
Show Gist options
  • Save luhenry/f20f9d0683dcacf4f3907de66d455287 to your computer and use it in GitHub Desktop.
Save luhenry/f20f9d0683dcacf4f3907de66d455287 to your computer and use it in GitHub Desktop.
#include <stdio.h>
int foo(void) {
printf("hello from helper\n");
return 1;
}
#include <stdio.h>
int foo(void) {
printf("hello from hijack\n");
return 0;
}
#include <stdlib.h>
#define _GNU_SOURCE 1
#include <dlfcn.h>
// int foo(void);
int main(void) {
void *libhandle = dlopen("libhelper.so", RTLD_LAZY | RTLD_GLOBAL);
int (*foo)(void) = dlsym(NULL, "foo");
return foo();
}
a.out: main.c
gcc $< -o $@ -ldl
libhijack.so: hijack.c
gcc $< -shared -o $@
libhelper.so: helper.c
gcc $< -shared -o $@
run: a.out libhijack.so libhelper.so
LD_DEBUG=libs,symbols LD_LIBRARY_PATH=`pwd` $(if $(HIJACK),LD_PRELOAD=libhijack.so) ./$<
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment