Last active
June 27, 2024 12:25
-
-
Save shicky/a06a57de6ab027abe18047d5706819b8 to your computer and use it in GitHub Desktop.
LD_PRELOAD linux function hook example
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
# 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