Linux Privilege Escalation with Library
Contains the details of the topic Privilege Escalation/Linux/Functionality/Library.
LD_PRELOAD
The LD_PRELOAD environment variable can specify to load a library before running a binary. The functions of this library take precedence over the default functions. Therefore, the idea is to have it load a malicious library.
Requirements
Have control of the
LD_PRELOAD
environment variable.Dynamic executables in SUDOERS or with SUID.
Enumeration
Attack
Create the library mylib.c
Compile the library
Use the library with LD_PRELOAD
RUNPATH
The RUNPATH setting within binaries specifies which folders take precedence over other folders to look for libraries on. If that setting points to a user-writable folder, it is possible to have it load a malicious library.
Requirements
Have a dynamic binary in SUDOERS or with SUID.
This binary must have
RUNPATH
set to a writable directory.
Enumeration
Attack
Try replacing the library in the directory pointed to by RUNPATH
with another one, ex., /lib/x86_64-linux-gnu/libc.so.6
. Check the error it returns, usually specifying the functions it cannot find.
Create the library with the same name as the library in the directory pointed to by RUNPATH, with the required functions inside it
It is also possible to put the code of _init
in the function below.
Compile the library
Run the binary normally
PYTHONPATH
Requirements
Executable python scripts such as SUDOERS.
Know the imported modules and the functions used by those modules.
Import modules that are located in writable directories or Among the PATHs that python uses to search for and import modules, we have write access to a PATH with a higher priority than the PATH in which the imported module used in the script is located. or The PYTHONPATH environment variable is manipulable (it indicates in which directory Python can look for modules to import)
Warning: The SUID bit does not work with interpreted scripts (such as Python).
Enumeration
Attack
Add the desired code inside the function of the (writable) module imported from the executable script in SUDOERS. or Create a python file with the same name as the module imported from the executable script in SUDOERS, and implement the function that is used by inserting the desired code. or Modify the PYTHONPATH environment variable to redirect the Python search functionality to a user-writable folder, continuing with the second attack.
Last updated