SECURITY ENGINEERING · PART 1 · LINUX AND WINDOWS, FROM THE INSIDE · LINUX · 10 OF 229

Linux Privilege Escalation and How to Stop It

How a limited shell becomes root on Linux, each path paired with its fix: enumeration first, then SUID and sudo misconfigurations, writable files a root process trusts, cron jobs, PATH and wildcard tricks, kernel and capability issues, and secrets left in the environment. Plus the tools defenders use (LinPEAS) and the hardening that removes the paths.

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 9
  9. 10
  10. 11
  11. 12
  12. 13
  13. 14
  14. 15
  15. 16
  16. 17
  17. 18
  18. 19
  19. 20
  20. 21
  21. 22
  22. 23
  23. 24
  24. 25
  25. 26
  26. 27
  27. 28
  28. 29
  29. 30
  30. 31
  31. 32
  32. 33
  33. 34
  34. 35
  35. 36
  36. 37
  37. 38
  38. 39
  39. 40
  40. 41
  41. 42
  42. 43
  43. 44
  44. 45
  45. 46
  46. 47
  47. 48
  48. 49
  49. 50
  50. 51
  51. 52
  52. 53
  53. 54
  54. 55
  55. 56
  56. 57
  57. 58
  58. 59
  59. 60
  60. 61
  61. 62
  62. 63
  63. 64
  64. 106
  65. 107
  66. 108
  67. 109
  68. 110
  69. 111
  70. 112
  71. 113
  72. 114
  73. 115
  74. 116
  75. 117
  76. 118
  77. 119
  78. 120
  79. 121
  80. 122
  81. 123
  82. 124
  83. 125
  84. 126
  85. 127
  86. 128
  87. 129
  88. 130
  89. 131
  90. 132
  91. 133
  92. 134
  93. 135
  94. 136
  95. 137
  96. 138
  97. 139
  98. 140
  99. 141
  100. 142
  101. 143
  102. 144
1 / 12

Press play to hear the narration, or step through at your own pace.

Key takeaways

  • Enumerate
  • sudo/SUID
  • writable trusted files
  • cron
  • capabilities and kernel
  • secrets
  • then harden and detect

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.

What a foothold checks
  • 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 / SUID misconfig
  • 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.

Writable file, run by root
  • 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.

Scheduled job traps
  • 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.

Capabilities and kernel
  • 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.

Credentials on the box
  • 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.

Close the paths
  • 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.

What to watch
  • 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.

Avoid these
  • 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.

In the interview
  • "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."

Interview questions

Q1What is privilege escalation on Linux?

Turning limited access, such as a web-server or normal user shell, into higher privileges, usually root. It is a key step after initial access, letting an attacker read any data, install persistence and move on.

Q2What is the first step of privilege escalation?

Enumeration: checking the user and groups, sudo rights, SUID/SGID files, writable root-owned files, cron jobs and timers, service versions, and secrets in the environment or history. Tools like LinPEAS automate it.

Q3What are the most common Linux privilege-escalation paths?

Misconfigured sudo or SUID binaries, writable files or scripts that a root process runs (including cron and systemd), dangerous capabilities or an unpatched kernel, and credentials left readable on the machine.

Q4How does a writable file lead to root?

If a process running as root executes or sources a file that a lower-privileged user can modify, the user edits it to run their own commands, which then execute with root privileges. Fix by making such files root-owned and not group/other writable.

Q5Why are cron jobs a privilege-escalation and persistence risk?

They run as root on a schedule, so a writable cron script, a wildcard over a user-controlled directory, or a writable drop-in folder lets a user get root execution repeatedly, which also provides persistence.

Q6What are Linux capabilities and how do they relate to escalation?

Capabilities split root’s powers into units. A binary granted a sensitive one, such as cap_setuid or cap_dac_override, can be as dangerous as SUID. Audit with getcap -r / and grant only the minimum.

Q7How do you defend a server against privilege escalation?

Run services as low-privilege users, grant exact sudo commands, minimise SUID and capabilities, keep root-run scripts and cron root-owned and non-writable, remove secrets from the environment, patch the kernel, and run enumeration tools on your own hosts.

Q8How would you detect a privilege-escalation attempt?

Watch auth.log for sudo and su, use auditd to log shells spawned by service accounts, alert on new SUID files, cron edits and new timers, and flag a web-service account spawning an interactive shell, shipping all of it to a SIEM.