Notes
Enumeration first
Privilege escalation starts with enumeration: looking, not attacking. The attacker checks who they are and their groups, what sudo lets them run, which SUID files exist, which root-owned files they can write, what runs on a schedule, which services and versions are present, and whether secrets sit in the environment or shell history. A tool called LinPEAS runs all these checks at once and highlights the promising ones. Defenders run the same tool, and fix what it flags before an attacker sees it.
- who am i, what groups
- sudo -l: what can I run?
- SUID/SGID files
- writable files owned by root
- cron jobs and timers
- running services and versions
- secrets in env, config, history
- LinPEAS automates all of it
Path 1 · sudo and SUID
The first and most common path is the sudo and SUID misconfiguration from the last episode. If sudo lets the user run an editor, an interpreter or a tool with a run-a-command option, they open a root shell through it. If a custom program is SUID root and can run a command, same result. The fix is the same both ways: grant only exact sudo commands, remove the SUID bit from anything that does not strictly need it, and mount scratch directories so SUID is ignored there.
- sudo -l shows an editor, interpreter, or tool with a shell escape → root
- a custom SUID-root binary that runs a command → root
- fix: exact sudo commands only fix: remove needless SUID, mount scratch nosuid
Path 2 · Writable files
The second path: you can write a file that a root process trusts. A cron job runs a script that any user can edit, so you add your own line. A systemd unit, or the script it launches, is writable. Or a root script calls a program by its short name and you can place your own earlier in the PATH. Each turns your write access into root execution. The fix is to make everything a privileged process runs owned by root, mode seven five five, with no write for group or others, and to call programs by full path.
- a cron job runs /opt/app/run.sh that any user can edit
- a systemd unit or its ExecStart script is writable
- a root-run script calls a program by name, and you control PATH or that program
- fix: root-owned, 755, no group/other write
Path 3 · cron and timers
The third path is scheduled jobs. They run as root, reliably, which is exactly what an attacker wants for both escalation and persistence. A root cron script sitting in a writable folder. A cron command that uses a wildcard over a directory an attacker can add files to, so a crafted filename becomes a command-line option. Or a drop-in folder like cron dot daily that a user can write to. Fix by locking the scripts and their folders to root, and never running wildcard commands over directories users control.
- a root cron script in a writable directory
- a wildcard in a cron tar/rsync command → argument injection
- a job that runs everything in /etc/cron.daily you can add to fix: lock down the scripts and their folders; avoid wildcards on untrusted paths
Path 4 · caps, kernel
The fourth path is more advanced. Linux capabilities split root's power into pieces, and a binary granted the wrong one, like changing user id or bypassing file permissions, can be as dangerous as SUID. An unpatched kernel may have a known local exploit that hands over root directly. A writable critical file, such as the password file, is an instant win. Defenders list capabilities with getcap, grant the minimum, patch the kernel, and keep critical files root-owned and not writable.
- getcap -r / : a binary with cap_setuid or cap_dac_override can be as strong as SUID
- an unpatched kernel with a known local exploit
- a writable or shared /etc/passwd
- fix: minimal capabilities, patch the kernel, verify critical file ownership
Path 5 · secrets
The fifth path needs no exploit at all: a credential left where it can be read. A password in a config file, a dot env, or shell history. An SSH private key with loose permissions. A cloud key in a service's environment, which is how the Part zero attack escalated. Or a password reused for a stronger account. This is the most common escalation in the real world. The fix: a secret manager, tight file permissions, no secrets in environment variables or history, and no reused passwords.
- passwords in config, .env, and shell history
- SSH private keys readable by the wrong user
- cloud keys in a service's environment
- a reused password that also works for root or sudo
- fix: secret manager, tight perms, no secrets in env
Defender view
Put the defender's hat back on. Run the same enumeration tools, LinPEAS or linux smart enumeration, on your own servers, and fix what they highlight before an attacker runs them. Keep least privilege so services run as their own low users. Keep secrets out of the environment and off disk. Patch the kernel and packages. Audit SUID, sudoers, capabilities, cron and writable root-owned files on a schedule. And alert when a new SUID file appears or a user escalates to root, because that is rarely normal.
- run LinPEAS/linux-smart-enum on your own servers
- least privilege: services as their own low users
- no secrets in env or on disk
- patch the kernel and packages
- audit SUID, sudoers, caps, cron, writable root files
- alert on new SUID and on sudo to root
Detection
Prevention is never perfect, so detection matters. The auth log records sudo and su use. The audit framework, auditd, can log when a service account runs a shell, which is almost always suspicious. Watch for new SUID files, cron edits and new timers. A web-server account like www data suddenly spawning an interactive shell is one of the strongest signals of a foothold. Ship all of this to a central place, which is exactly what Part thirteen builds.
- auth.log: sudo and su use
- auditd: execve of shells by service accounts
- new SUID files appearing
- cron edits and new timers
- a www-data shell spawning bash is a strong signal
- ship these to the SIEM
Common mistakes
Mistakes that keep machines easy to escalate on. Running services as root instead of their own users. Storing secrets in environment variables and dot env files. Broad sudo rules and forgotten SUID binaries. Writable root scripts and cron jobs. Patching kernels late. Never running enumeration tools on your own fleet, so you learn about a path only when an attacker uses it. And collecting no alert when an account becomes root.
- services running as root
- secrets in env and .env files
- broad sudo and stray SUID
- writable root scripts and cron
- kernels patched late
- no enumeration of your own machines
- no alert on sudo-to-root
Say it like this
In the interview: I prevent privilege escalation by removing the paths. Services run as their own low-privilege users. sudo grants exact commands only. SUID and capabilities are kept to the minimum. Scripts, units and cron jobs that root runs are owned by root and not writable by others. Secrets stay out of the environment, and kernels are patched. Then I run the same enumeration tools attackers use on my own machines, and alert when any account escalates to root.
- "Remove the paths: least
- privilege services, exact sudo
- rules, minimal SUID and capabilities, root-owned
- non-writable scripts and cron, no secrets in env, patched
- kernels. I enumerate my own
- boxes with LinPEAS and alert
- on sudo-to-root."