Linux Privilege Escalation: A Read-Only Audit
After code execution as a service account, the next question in an assessment is whether that account can become root. On a patched Linux server the answer almost never involves an exploit. It is usually a configuration that trusts something the low-privileged account can change: a sudo rule, a root cron job, a service binary, a file capability. Privilege Escalation in Security Foundations introduced these classes. This lesson is the tester's version: a read-only audit script that finds them on a copy of a server's configuration, how each finding is reported without being used, how escalation is detected, and the fix for each.
Read-only is the point. The audit only reads (find, stat, grep, awk); it changes nothing and runs nothing it finds. In a real engagement the RoE decides whether a found path may be demonstrated; this course never does, and the finding is complete without it: "the service account can write a script that root runs every 15 minutes" is the whole proof.
The classes and what to look for
| Class | The audit checks | Why it gives root |
|---|---|---|
| sudo rules | NOPASSWD entries for programs that can start a shell or write files (less, vim, tar, find, interpreters), and programs allowed with any arguments |
the program runs as root and can be told to run something else |
| Writable cron jobs | scripts referenced by root's crontab that are group- or world-writable; commands without an absolute path, and writable directories in cron's PATH |
whatever the script says next runs as root |
| Writable service files | User=root units whose ExecStart binary, or its directory, is writable by others |
replace the binary, wait for a restart |
| File capabilities | programs with capabilities such as cap_setuid, cap_dac_read_search, cap_sys_admin, cap_sys_ptrace |
cap_setuid on an interpreter is equivalent to SUID root |
| SUID files | set-user-ID programs not in the image's baseline | an unexpected SUID root program is a door (baseline check) |
| PATH | writable directories early in the PATH of root's jobs or services |
a program called by bare name is resolved there first |
The audit, in your sandbox
The session unpacks a copy of a lab server's configuration under ./t (lab data: sudoers fragment, crontab, a systemd unit, a few scripts, and the saved output of getcap -r / from the lab host), then runs the audit against it. At the end it also counts SUID files and file capabilities on the sandbox itself. Because every file in the copy belongs to the sandbox user, the audit judges writability by the permission bits (group- or world-writable); on a real host it would also check that the owner is root.
# --- a copy of a lab server's configuration (lab data), unpacked under ./t
mkdir -p t/etc/sudoers.d t/etc/cron.d t/etc/systemd/system t/opt/backup t/opt/agent t/usr/local/bin
cat > t/etc/sudoers.d/ops <<'S'
deploy ALL=(root) NOPASSWD: /usr/bin/systemctl restart shop
deploy ALL=(root) NOPASSWD: /usr/bin/less /var/log/shop/*
backup ALL=(root) NOPASSWD: /usr/bin/tar
monitor ALL=(root) NOPASSWD: /usr/local/bin/health.sh
S
cat > t/etc/crontab <<'S'
PATH=/opt/backup:/usr/local/bin:/usr/bin:/bin
*/15 * * * * root /opt/backup/run.sh
0 3 * * * root cleanup-tmp
S
cat > t/etc/systemd/system/agent.service <<'S'
[Service]
User=root
ExecStart=/opt/agent/agent --config /etc/agent.conf
S
printf '#!/bin/sh\ntar czf /var/backups/www.tgz /var/www\n' > t/opt/backup/run.sh
printf '#!/bin/sh\nexit 0\n' > t/opt/agent/agent
printf '#!/bin/sh\nuptime\n' > t/usr/local/bin/health.sh
chmod 0777 t/opt/backup/run.sh; chmod 0777 t/opt/agent; chmod 0755 t/opt/agent/agent t/usr/local/bin/health.sh; chmod 0755 t/opt/backup
cat > t/getcap.txt <<'S'
/usr/bin/ping cap_net_raw=ep
/usr/bin/python3.12 cap_setuid=ep
/usr/lib/x86_64-linux-gnu/gstreamer1.0/gst-ptp-helper cap_net_bind_service,cap_net_admin,cap_sys_nice=ep
S
# --- the audit: read-only (find, stat, grep, awk), never changes anything
cat > audit.sh <<'A'
#!/bin/bash
R=${1:?root of the copy}
w() { [ -n "$(find "$1" -maxdepth 0 -perm /022 2>/dev/null)" ]; } # group- or world-writable?
echo "[sudo] rules that hand out a shell or take any argument"
grep -hv '^#' "$R"/etc/sudoers.d/* | while read -r user rest; do
cmd=${rest#*NOPASSWD: }; prog=${cmd%% *}
case "${prog##*/}" in less|more|vi|vim|nano|tar|find|awk|python3|perl|env|bash|sh)
echo " $user: $prog can start a shell (GTFOBins-style)";; esac
[ "$cmd" = "$prog" ] && echo " $user: $prog allowed with any arguments"
[ -e "$R$prog" ] && w "$R$prog" && echo " $user: $prog is writable by non-owners"
done
echo "[cron] root jobs whose script or PATH can be changed"
path=$(grep -m1 '^PATH=' "$R/etc/crontab" | cut -d= -f2)
grep -E '^[^#A-Z].* root ' "$R/etc/crontab" | awk '{print $7}' | while read -r job; do
if [ "${job#/}" = "$job" ]; then
IFS=: read -ra dirs <<< "$path"
wd=$(for d in "${dirs[@]}"; do w "$R$d" && printf '%s ' "$d"; done)
echo " '$job' is called without a path (cron PATH starts with ${dirs[0]}); writable PATH dirs: ${wd:-none}"
elif w "$R$job"; then echo " $job runs as root and is writable: $(stat -c %A "$R$job")"; fi
done
echo "[systemd] root services whose binary or its directory is writable"
for u in "$R"/etc/systemd/system/*.service; do
grep -q '^User=root' "$u" || continue
bin=$(grep -m1 '^ExecStart=' "$u" | cut -d= -f2 | cut -d' ' -f1)
w "$R$(dirname "$bin")" && echo " $(basename "$u"): directory $(dirname "$bin") is $(stat -c %A "$R$(dirname "$bin")")"
w "$R$bin" && echo " $(basename "$u"): $bin is writable"
done
echo "[capabilities] file capabilities that amount to root"
grep -E 'cap_(setuid|setgid|dac_override|dac_read_search|sys_admin|sys_ptrace|sys_module)' "$R/getcap.txt" | sed 's/^/ /'
A
chmod +x audit.sh && ./audit.sh t
echo "[this sandbox] SUID files: $(find / -xdev -perm -4000 -type f 2>/dev/null | wc -l), file capabilities: $(getcap -r / 2>/dev/null | wc -l)"[sudo] rules that hand out a shell or take any argument
deploy: /usr/bin/less can start a shell (GTFOBins-style)
backup: /usr/bin/tar can start a shell (GTFOBins-style)
backup: /usr/bin/tar allowed with any arguments
monitor: /usr/local/bin/health.sh allowed with any arguments
[cron] root jobs whose script or PATH can be changed
/opt/backup/run.sh runs as root and is writable: -rwxrwxrwx
'cleanup-tmp' is called without a path (cron PATH starts with /opt/backup); writable PATH dirs: none
[systemd] root services whose binary or its directory is writable
agent.service: directory /opt/agent is drwxrwxrwx
[capabilities] file capabilities that amount to root
/usr/bin/python3.12 cap_setuid=ep
[this sandbox] SUID files: 8, file capabilities: 0
Reading the findings
Each line is a finding with its own fix. Written up, they look like this:
| Finding | Evidence | Fix |
|---|---|---|
deploy may run less on logs as root without a password |
sudoers line | less can run commands (!) and open other files: grant read access to the logs through a group (adm, or ACLs with setfacl) instead of sudo |
backup may run tar as root with any arguments |
sudoers line | tar options can execute programs; replace with a root-owned wrapper script with fixed arguments, or a dedicated backup tool with its own service account |
monitor may run health.sh with any arguments |
sudoers line | low risk if the script ignores arguments, but write health.sh "" in sudoers to forbid them, and keep the script root-owned and not writable |
root runs /opt/backup/run.sh every 15 minutes; the file is world-writable (-rwxrwxrwx) |
crontab line, stat |
chown root:root, chmod 0755, and keep it in a root-owned directory; add a file-integrity rule |
root's cron calls cleanup-tmp by bare name with /opt/backup first in PATH |
crontab | no writable PATH directory now, so informational, but one chmod away from a finding: use absolute paths and a standard PATH |
agent.service runs as root from a world-writable directory |
unit file, stat |
root-owned directory and binary, 0755; run the agent as its own user if it can; systemd hardening options |
python3.12 has cap_setuid=ep |
getcap output |
remove it (setcap -r); a capability that lets any user switch to UID 0 makes the interpreter a root shell |
None of the seven needed a vulnerability in any software. All seven are fixed by ownership, permissions and narrower rules, and the sandbox line at the end is the clean baseline to compare with: eight standard SUID programs and no file capabilities.
How escalation is detected
- sudo logs every use with the full command line (
COMMAND=/usr/bin/tar …) to the auth log or journal; a sudo'd program spawning a shell is visible in process telemetry assudo → tar → sh. - File changes: auditd watches (
-w /opt/backup/ -p wa) or a file-integrity monitor alert when a root-run script or service binary changes. - Process lineage: cron or systemd starting an unexpected child, or any process whose effective UID becomes 0 from a non-root parent (
auditdrules onexecvewitheuid=0andauid>=1000). - Capability use: EDR products and the audit log record processes that change their UID through capabilities.
How it is prevented
Configuration management that owns these files (so ownership and modes are reset on every run), a sudo policy reviewed like code (exact commands with fixed arguments, no editors, pagers or interpreters, Defaults secure_path and use_pty), root-owned and non-writable paths for everything root executes, no file capabilities beyond a reviewed list, and the audit above run in CI against the server image. Lynis and the CIS benchmarks automate many of the same checks.
Pitfalls
- Using the finding to prove it. Writing to the cron script "to show it works" changes a production file run by root. The permission bits are the evidence.
- Only checking files. A writable directory lets an attacker replace a non-writable file inside it.
- Missing indirect paths. A root-owned script that sources a writable file, or reads a writable configuration, is as dangerous as a writable script.
- Treating a container's root as the host's. Check privilege boundaries before claiming impact.
Practice
- Extend the audit to flag root cron scripts that are not writable themselves but sit in a writable directory. Which
findtest do you need? - Rewrite the
backupsudo rule safely.
Answers
- For each absolute job path, also test its directory:
w "$R$(dirname "$job")" && echo " $(dirname "$job") is writable: the script can be replaced". The same-perm /022test works on directories. - Create a root-owned script
/usr/local/sbin/backup-www(mode 0755) that runstar czf /var/backups/www.tgz /var/wwwwith fixed paths, and grantbackup ALL=(root) NOPASSWD: /usr/local/sbin/backup-www ""(the empty string forbids arguments). Better still, run the backup as a dedicated user that can read/var/wwwand write/var/backups, with no sudo at all.
Test yourself
Why is a NOPASSWD sudo rule for less a privilege-escalation finding?
- less is slow
- less can run commands and open other files, and it runs as root under sudo
- less modifies files
- It isn't
What is the evidence for 'root runs a world-writable script'?
- Editing the script and waiting for root to run it
- The crontab line and the file's permission bits (stat output)
- A screenshot of a root shell
- The script's contents
Which file capability on an interpreter is equivalent to SUID root?
- cap_net_bind_service
- cap_net_raw
- cap_setuid
- cap_chown on a log file
Root's cron calls cleanup-tmp without a path, but no PATH directory is writable. How is it reported?
- Critical
- Informational: not exploitable now, but one permission change away; use absolute paths
- Not at all
- As a false positive