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/findThe find command is used as an example, can be any other command
Check 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
The extension .so represents a shared object
Run a sudo command specifying the LD_PRELOAD option with the script
$command is any command with sudo permissions found using sudo -l
Abusing SUID/SGID permissions on reading binaries
Verify executables with special permissions
base64 is used as an example, can be any other executable with data visualization properties, such as nano,cat, vim, nvim, etc...
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
nano is used as an example, can be any other executable with the same reading properties, such as vim, nvim, etc...
Abuse privileges to create users with privileges
$username is an arbitrary name and the /root:/bin/bash specify we are requesting a root shell
Abusing capabilities on binaries
Verify executables with set capabilities
vim is used as an example, can be any other executable
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
file.sh is an arbitrary name used as an example. It can be another bash file or executable.
Go to the file location and change the content to get a reverse shell
To see some example scripts for a Reverse Shell, you can go here
Create a listening port with netcat to receive the shell
$port is the port specified in the reverse shell script
Abusing deleted cron jobs
Verify executables set as cron jobs
file.sh is an arbitrary name used as an example. It can be another bash file or executable
We have to keep in mind the path of the /etc/crontab file
Verify if the executable was deleted and create one with the same name
Note that the folder we navigate to is the one that was listed in /etc/crontab
Create a listening port with netcat to receive the shell
$port is the port specified in the reverse shell script
Abusing the PATH environment variable (Path Hijacking)
Search for any writable folder and compare it to PATH
We note there is a home folder where we can write
In case we haven't found anything, we have to write to /tmp
Add the folder to PATH and go to this location
Write a simple script in C to search for an executable
The system will search for an executable named root, which is an arbitrary name
Compile the script and give it SUID permissions
Create a file to ask for a shell and give it permissions
Note that it is the same executable we called previously on the C script
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
Standard key files are called id_rsa
Copy the keys to a file, assign permissions to the file, and use it to log in using the key
Use chmod 600 id_rsa to assign restrictive permissions, and SSH does not block this method
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
This will generate the file with a private key for the user and a .pub file with a public key
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
We use an image for Alpine, but any other light-weight distro can be built and used
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
The shell will be generated as the root user, having complete permissions on 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