Skip to content
Tutorials Center
My learning

Topics

Lesson 26 of 38 ยท Post-exploitation and Privilege Escalation

Linux Privilege Escalation Review

Review the common Linux misconfigurations that allow privilege escalation, such as SUID binaries, sudo rules and writable cron jobs, and learn how to audit and fix them.

Privilege escalation means gaining more rights than you started with. On Linux the target is usually root, the account that can do anything. A tester who lands as a low-privilege service account (for example www-data after a web flaw) checks whether the system is configured so that the account can climb higher.

Most real Linux escalation paths are not clever exploits. They are configuration mistakes: a program that runs with extra rights when it should not, a sudo rule that is too generous, a script owned by root but writable by everyone. That is good news for defenders, because configuration can be audited. This lesson reviews the categories and builds stand-in auditors that read sample data. The commands shown are standard administration commands you would run on your own lab machine or an authorized host.

Start with situational awareness

First answer basic questions about where you are:

Terminal
id                       # which user and groups
hostname; uname -r       # which machine and kernel
cat /etc/os-release      # distribution and version
sudo -l                  # what may this user run with sudo?
ss -tlnp                 # which services listen locally
ps aux                   # which processes run, and as whom

These are read-only and noisy only in the sense that a well-monitored host may log them. They tell you what to examine next.

The main categories

Category The mistake The fix
SUID/SGID binaries A program marked "run as owner" is owned by root and can be made to run commands or read files Remove the SUID bit unless required; keep binaries patched
Sudo rules NOPASSWD for programs that can launch a shell or edit files Grant exact commands with fixed arguments; avoid editors, interpreters and file tools
Cron and timers A root job runs a script that a lower user can modify Root-run scripts must be root-owned in non-writable directories
Writable files and PATH Root runs a script that calls a program by relative name in a writable directory Use absolute paths; fix permissions
Capabilities A binary has Linux capabilities that are broader than needed Remove or narrow with setcap
Credentials at rest Passwords in scripts, shell history, config files, backups Move to a secrets manager; rotate
Kernel and package bugs Unpatched local flaws Patch; reboot into the new kernel
Containers Container with --privileged, host mounts or the Docker socket Drop privileges; never mount the Docker socket

A public catalog such as GTFOBins documents which common binaries become dangerous when SUID or when allowed through sudo; defenders can use it as a checklist of rules to remove.

Where Linux escalation hides

SUID Programs that run as root
Sudo Rules that are too wide
Cron Root jobs with writable scripts
Files Secrets and loose permissions

Most paths are configuration, not exploits.

1 / 2

Auditing SUID binaries

A SUID file shows an s in its permissions (-rwsr-xr-x). Listing them is routine:

Terminal
find / -perm -4000 -type f 2>/dev/null

Normal systems have a short, familiar list (passwd, sudo, mount...). A reviewer's job is to spot entries that do not belong: an editor, an interpreter or a custom binary in an odd directory. The stand-in below compares a sample listing with an allowlist of standard paths.

Python Flag unexpected SUID binaries

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The three flagged lines are the ones to explain in a report. For each, the fix is chmod u-s <file> or replacing the need for the SUID bit with a proper service or a narrowly scoped sudo rule.

Reviewing sudo rules

sudo -l shows what the current user may run. Two things make a rule dangerous: NOPASSWD (no authentication) and a command that can do more than it appears to, such as an editor, a pager, a scripting language or a file copier, because those can spawn a shell or overwrite any file.

Python Audit sudoers rules

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

deploy is fine because the arguments are fixed; backup, helper and ops each give away more than intended. The remedy is to grant the exact command with exact arguments, or to use a purpose-built service instead of broad sudo.

Cron jobs and writable scripts

Look at what root runs on a schedule (/etc/crontab, /etc/cron.d, systemctl list-timers) and then at who can write those scripts and their directories (ls -l). A root job that executes /opt/scripts/cleanup.sh is only as safe as the permissions on that file and its parent directory. The review rule is simple: anything root executes must be owned by root and not writable by anyone else.

Terminal
ls -ld /opt/scripts /opt/scripts/cleanup.sh
# drwxrwxrwx 2 root root 4096 ... /opt/scripts          <- world-writable directory: bad
# -rwxr-xr-x 1 root root  212 ... /opt/scripts/cleanup.sh

Even if the script itself is locked down, a world-writable directory may let someone replace it. Fix both.

Credentials and secrets

Search for secrets in places people forget: shell history, application .env and config files, backup archives, SSH keys without passphrases, and scripts with passwords on the command line (visible in ps). Finding a database password here often leads straight to the next system, so it belongs in the report even without a technical exploit.

Remediation checklist

  • Patch the kernel and packages regularly; reboot to apply kernel updates.
  • Audit SUID/SGID and capabilities on a schedule and after installs.
  • Use least-privilege sudo with exact commands; remove NOPASSWD ALL.
  • Make root-run scripts non-writable by others; use absolute paths.
  • Run services as dedicated unprivileged users, with systemd hardening options such as NoNewPrivileges=yes.
  • Keep secrets out of files and history; rotate anything found.
  • Monitor for unusual sudo use and new SUID files with an integrity tool such as AIDE or auditd rules.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which is the most common cause of Linux privilege escalation in real audits?

    Configuration mistakes are far more common than exotic exploits, and they can be audited.
  2. 2. Why is a sudo rule allowing NOPASSWD for vim risky?

    Programs that can spawn shells or write arbitrary files turn a narrow rule into full root access.
  3. 3. A root cron job runs /opt/scripts/cleanup.sh. Which condition makes it exploitable?

    If a lower user can change what root runs, they control root.
  4. 4. What is the best first fix for an unneeded SUID binary?

    Removing the unneeded privilege removes the path.

Summary

  • Linux escalation is mostly about configuration: SUID/SGID, sudo, cron, permissions, capabilities, secrets and containers.
  • Start with read-only awareness commands (id, sudo -l, ss, ps).
  • Compare SUID lists and sudo rules against what the system really needs.
  • Root must never execute anything that a lower user can modify.
  • Fixes are least privilege, patching, hardening and regular auditing.

Next: Windows privilege escalation review