LD_PRELOAD (ine)
In this lab, when we check the sudo configuration for the student user, we find that they can run apache2 as root and they can set the environment variable LD_PRELOAD
ld is a common linker found on linux systems. A linker takes different object files and links them into a final executable binary. The LD_PRELOAD environment variable lets us set a path to an object file which will be loaded by ld before anything else.
From an attacker's point of view, this opens an opportunity to perform malicious acts on the compromised machine.
In this example, we can exploit this to gain elevated privileges.
We first of all need to create a malicious c file which will open a bash session using the uid and gid of the root user - on linux this is the number 0
In the example above, I have used the echo command with the -e flag so I can use escape characters. I have done this as I wanted to include newlines using \n
The c code looks as follows:
#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>
void _init() {
unsetenv("LD_PRELOAD");
setgid(0);
setuid(0);
system("/bin/bash");
}
The next step was to compile this code into a shared object file using:
gcc -fPIC -shared -o puzz00.so puzz00.c -nostartfiles
Next, we need to set the LD_PRELOAD environment variable to point to the absolute path of our malicious shared object file. We then need to open something as the root user. In this example, we can only open apache2 as root so this is what we use.
The ld linker checked the LD_PRELOAD environment variable and loaded our malicious .so file before anything else. We also call the apache2 program. This leads to the execution of the _init() function in our .so file which opens a root bash session.
This lab helps us to understand how to exploit an LD_PRELOAD vulnerability so we can gain root privileges. It is always worth keeping an eye out for:
env_keep+=LD_PRELOAD
when we run sudo -l
The LD_PRELOAD envar can also be hijacked to perform other malicious actions on a system such as hiding nefarious processes from tools such as ps and top
I hope this has been of some use.
puzz00