description | cover | coverY |
---|---|---|
10/30/2023 |
59 |
"As if this stuff can't get any cooler..."
In Linux, LD_PRELOAD
is an environment variable that allows us to be able to manipulate which library is used at runtime for a binary.
- In other words, force a binary to use a specific library
This allows us to be able to change program behavior, how certain functions work, and logic.
{% embed url="https://github.com/zardus/preeny" %}
- Write your own library:
library.h:
#include <stdio.h>
foo bar(arg1, arg2)
{
return 0;
}
- Compile and set
LD_LIBRARY_PATH
variable to current directory:
cc -o <library.hso> -shared library.c -ldl
LD_LIBRARY_PATH=./$LD_LIBRARY_PATH
a library.so
file will be created, this is our shared library that we want our binary to use.
- Set the
LD_PRELOAD
environment variable to the compiled shared library and execute the program with ourLD_PRELOAD
:
LD_PRELOAD=library.so ./<target_binary>
This will force our target binary to use our newly created shared library.
Ultimately, changing the binary's behavior.