I read this article recently, and I tried overriding the libc printf function with a doing the following for fun :-
- Create an executable that uses printf to print
this is a test(printer.c) - Create an c file with a custom puts to print
muhahaha, this is a test(custom.c) - Create an object file
gcc -fPIC -g -c -Wall custom.c - Create an so file
gcc -shared -Wl,-soname,libmystuff.so.1 -o libmystuff.so.1.0.1 custom.o - I add the directory which contains the so file into the LD_PRELOAD environment variable.
export LD_PRELOAD=$(pwd) - Try running printer
I’d imagine that muhahaha, this is a test would be printed out but it seems like im doing something wrong. Have I got some concept wrong? Or am I just doing something wrong?
[EDIT]
The code snippets involved are :-
// printer.c
int main() {
printf("this is a test");
return 0;
}
// custom.c
void printf(char *t) {
puts("muhahaha, this is a test");
}
You’re supposed to name the library in the LD_PRELOAD environment variable, not the directory.