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 questionsBy the end of this lesson
Note
I can find a scheduled job an ordinary account can turn into a root shell.
Key words
| Word | What it means | Example |
|---|---|---|
| cron | a service that runs jobs on a schedule | |
| World-writable | any account may change the file | rwxrwxrwx |
| PATH | the folders a shell searches for a command | |
| PATH hijack | planting a command in a folder searched first | |
| Absolute path | the 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 questionsA 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
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
for i in $(seq 14); do [ -s /tmp/cron-proof.txt ] && break; sleep 5; done; cat /tmp/cron-proof.txtShown here, not run. Press Start lab to type it into your own lab’s terminal.
Check
Have a go
Check
3 questionsLock 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
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>&1Shown 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
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.shShown here, not run. Press Start lab to type it into your own lab’s terminal.
intern plants its own backup-now
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
sudo /opt/reports/daily.sh && cat /tmp/path-proof.txtShown here, not run. Press Start lab to type it into your own lab’s terminal.
Try it: which call can be hijacked?
# 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
sudo chmod 755 /opt/reports/bin && printf '#!/bin/bash\n/usr/bin/rsync -a /srv /backup\n' | sudo tee /opt/reports/daily.shShown 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 questionsHow 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.
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