Skip to content

Instantly share code, notes, and snippets.

@shicky
Last active June 27, 2024 12:25
Show Gist options
  • Save shicky/a06a57de6ab027abe18047d5706819b8 to your computer and use it in GitHub Desktop.
Save shicky/a06a57de6ab027abe18047d5706819b8 to your computer and use it in GitHub Desktop.
LD_PRELOAD linux function hook example
# helloworld.c
# gcc helloworld.c -o helloworld
#include <stdio.h>
#include <unistd.h>
int main() {
puts("Hello world!\n");
return 0;
}
# libexample.c
# gcc libexample.c -o libexample.so -fPIC -shared -ldl -D_GNU_SOURCE
#include <stdio.h>
#include <unistd.h>
#include <dlfcn.h>
#include <string.h>
int puts(const char *message) {
int (*new_puts)(const char *message);
int result;
new_puts = dlsym(RTLD_NEXT, "puts");
if (strcmp(message, "Hello world!\n") == 0) {
result = new_puts("Goodbye FUCK!\n");
} else {
result = new_puts(message);
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment