Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Writable cron scripts and the PATH

After this lesson: I can find a scheduled job an ordinary account can turn into a root shell.

60 min lesson7 commandsLinux privilege escalation lab: a setuid binary, a sudo rule and a cron job to break

A quick check that you are not a bot, then your own lab opens on the interactive lesson.

Already have an account? Sign in

A tidy-up job that trusts a file

Root runs a small clean-up script every minute to delete old temporary files. Handy, until you see who may edit the script.

If an ordinary account can change what root runs, it can make root run anything. A scheduled job is only as safe as the file it trusts.

The one rule

Warning

This runs on your own lab machine. Editing a job on a machine you do not own is a crime without written permission. In the UK that is the Computer Misuse Act 1990. The job and the account here are the lab's.

Quick check: the rule

Have a go

Quick check: the rule

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

By the end of this lesson

Note

I can find a scheduled job an ordinary account can turn into a root shell.

Key words

WordWhat it meansExample
crona service that runs jobs on a schedule
World-writableany account may change the filerwxrwxrwx
PATHthe folders a shell searches for a command
PATH hijackplanting a command in a folder searched first
Absolute paththe full path to a file/usr/bin/find

The cells students fill in are left blank in this preview.

Starter

Have a go

Starter

4 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

A writable script root runs

From lesson 1 you found /opt/maintenance/cleanup.sh with mode 777, and root's cron runs it every minute. Anybody can add a line to it.

The Run steps into intern and appends one line that records who runs the script. Then you wait for the next minute.

intern adds a line to the script

Command
sudo -u intern bash -c "echo 'id > /tmp/cron-proof.txt' >> /opt/maintenance/cleanup.sh"

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Wait for root's cron, then read the proof

Command
for i in $(seq 14); do [ -s /tmp/cron-proof.txt ] && break; sleep 5; done; cat /tmp/cron-proof.txt

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Check

Have a go

Check

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Lock the script down

The fix is ownership and mode: root owns the script and only root may write it, mode 644 or 755. The folder above it must be safe too.

The Run resets the file. intern can no longer edit it.

Make it root-owned and not writable

Command
sudo chown root:root /opt/maintenance/cleanup.sh && sudo chmod 755 /opt/maintenance/cleanup.sh && sudo -u intern bash -c "echo x >> /opt/maintenance/cleanup.sh" 2>&1

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

A common mistake

Warning

Many people check only the script's mode. A script root owns at 755 is still unsafe if the folder it sits in is writable, because an account can replace the whole file. Check the folder as well as the file.

A bare command and a writable folder

A script that calls backup-now instead of /usr/bin/backup-now lets the shell search its PATH. If a folder on that PATH is writable, an account can plant its own backup-now there.

The Run sets up a practice report script root runs, with a writable folder first on its PATH. This is on your own machine.

Set up a practice report script

Command
sudo mkdir -p /opt/reports/bin && sudo chmod 777 /opt/reports/bin && printf '#!/bin/bash\nexport PATH=/opt/reports/bin:/usr/bin:/bin\nbackup-now\n' | sudo tee /opt/reports/daily.sh && sudo chmod 755 /opt/reports/daily.sh

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

intern plants its own backup-now

Command
sudo -u intern bash -c "printf '#!/bin/bash\nid > /tmp/path-proof.txt\n' > /opt/reports/bin/backup-now; chmod 755 /opt/reports/bin/backup-now"

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Trigger a report run

Command
sudo /opt/reports/daily.sh && cat /tmp/path-proof.txt

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Try it: which call can be hijacked?

Code: python
# A script root runs. Each line is a command it calls. Which lets someone else win?
script = ["export PATH=/opt/reports/bin:/usr/bin:/bin", "backup-now", "/usr/bin/tar -c /srv"]
writable = {"/opt/reports/bin"}

path = []
for line in script:
    if line.startswith("export PATH="):
        path = line.split("=", 1)[1].split(":")
    elif line.startswith("/"):
        print(line.split()[0], "-> full path, safe")
    else:
        first = next((d for d in path if d in writable), None)
        print(line, "-> found in", first, "which anybody can write" if first else "-> not hijackable")

# Remove /opt/reports/bin from writable. What changes for backup-now?

Make a free account to save your work and track your progress.

Fix it: use the full path

Command
sudo chmod 755 /opt/reports/bin && printf '#!/bin/bash\n/usr/bin/rsync -a /srv /backup\n' | sudo tee /opt/reports/daily.sh

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

What did the PATH weakness show?

Have a go

What did the PATH weakness show?

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

How a defender sees it

Watch the scheduled scripts. Record their hashes and modes, and compare; a changed script or a loosened mode is a finding.

The last lesson builds that check. The audit system can also record every write to these files.

Assignment: audit scheduled jobs

Audit a set of scheduled scripts. Each is a dictionary with path, mode (ten characters), owner and dir_writable (True when others may write the folder). Write writable_by_others(jobs): the sorted paths whose mode gives others write (the ninth character is w) or whose dir_writable is True. Write hijackable(jobs, calls), where calls is a dictionary from a path to the list of commands the script runs. Return the sorted paths of jobs that call at least one bare command (one that does not start with /) and have dir_writable True.

Code: python
def writable_by_others(jobs):
    return []


def hijackable(jobs, calls):
    return []

Make a free account to save your work and track your progress.

Exit quiz

Have a go

Exit quiz

5 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.