Labs / Linux Exploitation / Escalate to root

Lab 4 of 4 · Escalate to root

A locked-down sudo rule undone by its PATH

One NOPASSWD wrapper, no arguments, no obvious hole — and a world-writable directory at the front of secure_path turns it into a root shell.

Start this lab 5 steps

The idea

Selma has a signed engagement against Droitwich Retail and a foothold on a point-of-sale back-office host as the service account till. This box looks careful: no broad sudo rule, no stray setuid bit. till may run exactly one root command with no password — a "nightly check" wrapper, with no arguments to abuse.

The hole is not in the wrapper's text; it is in how it finds its tools. The script calls date and uptime by bare name, leaving the shell to resolve them through PATH. And sudo's secure_path — the PATH it hands a root command — has a world-writable directory at its front. Put a program named date there and the trusted wrapper runs your date, as root. You will read the rule, spot the two halves of the mistake, and use them.

Steps

  1. The foothold, and the wall it stops at

    You are on Selma's Kali box. The foothold is an SSH login on the POS host at 10.0.1.20 as the service account till. Click Start to open a shell there, confirm who you are with id, then try /etc/shadow and watch it refused. That wall is what the sudo rule — used the wrong way — will get you past.

    kali:~# ssh box
  2. Read the one rule you have

    First question on any foothold: what may this account run as root? sudo -n -l lists it without prompting for a password (-n, non-interactive — it either tells you or fails, it never hangs). Read the whole answer, not only the command line: sudo prints the Defaults that apply to you as well, and that is where secure_path is.

    kali:~# ssh box 'sudo -n -l'
  3. Two innocent facts that are not

    Look at the wrapper and at that first secure_path directory. The script is world-readable, so read it. It calls date and uptime by bare name — no /usr/bin/ in front — so the shell resolves them through PATH. And /opt/ops/bin, first in that PATH, is world-writable. Either alone is a shrug; together, anything you drop in /opt/ops/bin named date is what the root wrapper runs.

    kali:~# ssh box 'cat /usr/local/bin/ops-health; echo ---; ls -ld /opt/ops/bin'
  4. Shadow the tool, run the wrapper

    Plant a date of your own in /opt/ops/bin — a tiny script that prints who it is and reads the hash file — then trigger the wrapper with sudo. sudo runs ops-health as root with the box's secure_path; the wrapper's bare date resolves to your /opt/ops/bin/date first, and it runs as root. Your id comes back uid=0(root), and /etc/shadow — refused in step one — prints.

    kali:~# ssh box 'printf "#!/bin/sh\nid\ncat /etc/shadow\n" > /opt/ops/bin/date && chmod +x /opt/ops/bin/date && ls -l /opt/ops/bin/date'
    kali:~# ssh box 'sudo /usr/local/bin/ops-health'
  5. What Droitwich's admin should change

    Selma's finding is not "sudo is dangerous" or "ops-health is bad" — the rule was meant to be tight. It is that a root program that resolves its tools through a writable PATH is a root shell. The fixes, any one of which closes it:

    • Call tools by absolute path. In ops-health, /usr/bin/date and /usr/bin/uptime instead of bare names. Then no PATH entry can shadow them, writable or not.
    • Fix secure_path. It should list only root-owned, root-writable directories — never /opt/ops/bin while it is 0777. Either tighten the directory to 0755 root-owned, or drop it from secure_path.
    • Audit for writable PATH directories. find every directory on any root PATH or secure_path that a non-root user can write (find … -writable), and close it. The same check catches the next one.

    One honest line: running any of this against a system whose owner has not authorised you in writing is unauthorised access, an offence under laws like the UK Computer Misuse Act and the United States' CFAA. Selma has a signed engagement; that permission is the only thing separating this from a crime.

    kali:~# ssh box 'ls -ld /opt/ops/bin && grep secure_path /etc/sudoers.d/ops-health'

Run these steps on real machines

Keep reading

Learn it on real machines.

Every lab is a small network of real Linux machines in your browser. Sign in with Google or email to start.

Start this lab