Labs / Linux Exploitation / Escalate to root

Lab 2 of 4 · Escalate to root

Sudo misconfiguration: a backup tool to root

Turn one NOPASSWD sudo rule for tar into a root shell with the GTFObins tar escape, then read the box's hashes.

Start this lab 5 steps

The idea

Calla has a signed engagement against Quillon Analytics and a foothold on their reporting box as rowan, an ordinary account. No root password, no obvious hole — but Quillon left the data team one convenience: rowan may run the nightly backup, tar, as root with no password.

That is the whole way up. Escalation is often one line, and the line here is a backup command — because tar can be told to run another program at a checkpoint, and sudo runs tar as root. You will read the rule, abuse it with the real GTFObins tar escape, and end at uid=0.

Steps

  1. The foothold, and the wall it stops at

    You are on Calla's Kali box. The foothold is an SSH login on the reporting box at 10.0.1.20 as rowan. Click Start to open a shell there, confirm who you are with id, then try the classic prize — /etc/shadow, the file of password hashes — and watch it refused. That wall is what the sudo rule will get you past.

    kali:~# ssh box
  2. What the box lets you run as root

    Before reaching for an exploit, read what you have already been given. sudo -l lists what rowan may run with sudo, as whom, and whether a password is asked. Read it as an attacker, not an admin: the question is not "what am I allowed to do" but "which of these can run a program I choose".

    A rule is only safe if the command it grants does one fixed thing and cannot launch another. Many cannot help it — an archiver, an editor, a pager and a dozen others all have a way to shell out, which is exactly what GTFObins catalogues.

    kali:~# ssh box 'sudo -l'
  3. The checkpoint that runs a command

    tar can print progress every so many records — a checkpoint — and --checkpoint-action=exec=… runs a command at each one. That is a legitimate feature (a progress hook), but run as root by sudo, the command it executes is root too.

    So you archive anything (/dev/null into /dev/null does nothing), ask for a checkpoint at the first record, and hand the action the command you actually want. To prove control cleanly, run id; swap it for /bin/sh and the same line drops you into an interactive root shell.

    kali:~# ssh box 'sudo tar -cf /dev/null /dev/null --checkpoint=1 --checkpoint-action="exec=id"'
  4. Own the box

    Root reads anything. Point the same escape at /etc/shadow — the file rowan was refused in step one — and read it as root. These hashes are the real loot: the material a cracking step feeds on, and proof the box is yours.

    kali:~# ssh box 'sudo tar -cf /dev/null /dev/null --checkpoint=1 --checkpoint-action="exec=cat /etc/shadow"'
  5. What Quillon's admin should change

    Calla's finding is not "tar is dangerous" — tar is fine. It is that a NOPASSWD rule for a general-purpose program is a root shell handed to anyone who holds that account. The fixes, in order of how much they help:

    • Grant a wrapper, not a tool. If the data team needs the nightly backup, let them run one small script that does exactly that one backup — (root) NOPASSWD: /usr/local/sbin/nightly-backup — not tar with any arguments they like. The script takes no caller input it passes to a shell.
    • Never grant a GTFObins binary. tar, vi, less, awk, find, env and many more can all spawn a command; none belongs in a sudo rule. GTFObins is the list to check a proposed rule against before you write it.
    • Scope and log it. sudo logs every command it runs to the auth log, so Quillon's own SOC would see rowan running tar … --checkpoint-action=exec=… — an archiver asked to execute a command is the tell. Least privilege stops the escalation; the log is how you catch an attempt at it.

    One more 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. Calla has a signed engagement; that permission is the only thing separating this from a crime.

    kali:~# ssh box 'sudo -l | grep -i tar'

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