Linux Privilege Escalation
Here are some techniques for achieving privilege escalation on Linux systems:
Abusing Sudo execution permissions
Verify which commands can be run with sudo
$ sudo -l
#Example results
User user may run the following commands on the host:
(ALL) NOPASSWD: /usr/bin/findCheck Sudo abuse for the binary on GTFOBins and use it on the target machine to get privileges
sudo find . -exec /bin/sh \; -quit
#This will return a root shellAbusing LD_PRELOAD (Library Hijacking)
Verify the LD_PRELOAD option in env_keep
sudo -l
#Example results
Matching Defaults entries for user on ip-0-0-0-0:
env_reset, env_keep+=LD_PRELOADWrite a simple script in C
Then compile into a shared library
Run a sudo command specifying the LD_PRELOAD option with the script
Abusing SUID/SGID permissions on reading binaries
Verify executables with special permissions
Check binary SUID abuse on GTFOBins
Abuse privileges to read important files and copy its content locally
Use John to make a crackable file and crack passwords
Abusing SUID/SGID permissions on writing binaries
Verify executables with special permissions
Abuse privileges to create users with privileges
Abusing capabilities on binaries
Verify executables with set capabilities
Check binary capabilities abuse on GTFOBins
Go to the binary location and execute the payload
Abusing existing cron jobs
Verify the executables set as cron jobs
Go to the file location and change the content to get a reverse shell
Create a listening port with netcat to receive the shell
Abusing deleted cron jobs
Verify executables set as cron jobs
Verify if the executable was deleted and create one with the same name
Create a listening port with netcat to receive the shell
Abusing the PATH environment variable (Path Hijacking)
Search for any writable folder and compare it to PATH
Add the folder to PATH and go to this location
Write a simple script in C to search for an executable
Compile the script and give it SUID permissions
Create a file to ask for a shell and give it permissions
Run the path script to get privileges
In other cases, when an application with SUID permissions is calling a binary and isn't specifying its absolute path, we can just create an arbitrary file with the same name, and add it to the path
Abusing the NFS misconfiguration
Check the NFS configuration, and search for the no_root_squash value
On the host machine, check for mountable shares
On the host machine, create a shared folder with the target
Write a simple script in C to ask for a shell
Compile the script and give it SUID Permissions
On the target machine, run the script to get privileges
Abusing SSH keys
Check if we have reading permissions on the SSH private keys of a user
Copy the keys to a file, assign permissions to the file, and use it to log in using the key
When having writing permissions on the /root/.ssh/ directory, we can generate an SSH key with the current user and pass it to the system
Pass the public key to the authorized keys file of the root user
Use this to log in as root using the key
Abusing lxd
Verify lxd is installed on the system
Check if the lxd group exists
Add the current user to the lxd group (in case it isn't there yet)
On our machine, install the required lxd components
Activate the lxd service and start it
Download an image or build it if necessary
Mount a server to import and download the image to the target host
On the target, import the image and use it to create a container specifying high privileges and mounting the complete filesystem
Start the container, ask for a shell from it, and access the mount point of the filesystem
Abusing the Python eval function
The eval function in Python is well-known for being vulnerable, as it can act in a global scope. If there isn't proper sanitization of the input, this can be leveraged to execute arbitrary system commands.
We find a code that is run as the root user and uses the eval function improperly
If it can't be inserted directly, we can add a validation that returns true, and then the payload
Abusing Docker configurations
This could be used to escape a Docker container and gain access to the host system or to directly escalate privileges
(Optional) Try to determine if we are in a container environment
If the container was started with
--privilegedwe could mount the host filesystem
We can also abuse the Docker socket if it's exposed
We can also abuse capabilities to mount the file system
Abusing Python Libraries (Python Library Hijacking)
A dependency-level attack that allows the creation of a malicious Python file that will be executed instead of a legitimate library intended to be imported.
It occurs because Pythonβs module import mechanism takes whichever module it resolves first in this priority search order:
Directory of the executing script
Standard library paths
Installed site-packages
Entries added via environment variables
If an attacker can set a malicious file to be read first than the actual location of an imported module, Python will import that file and not the real library.
For example, suppose we have a Python script that runs as the root user, and imports the base64 module to call the b64encode function from this module
We also know that the original location of this library is on /usr/lib/python3.7/base64.py
We can create a file with the same name as the module, in the same folder where the script is located. Then
After this, we can run the script, which will import our malicious module and execute the desired payload as a privileged user
Last updated