|
11 | 11 | | Solves | 0 | |
12 | 12 | | Author | OverTheWire | |
13 | 13 | | Solved by | leeadrian841 | |
14 | | -| Status | In Progress | |
15 | | -| Tags | Linux | |
| 14 | +| Status | Solved | |
| 15 | +| Tags | Linux, cron, Privilege Escalation, Bash Scripting, File Permissions | |
16 | 16 |
|
17 | 17 | ## TL;DR |
18 | | -{One or two sentences: the bug class or trick, and the key insight.} |
| 18 | +Code execution through a writable cron drop folder: a job running as bandit24 executes every bandit23-owned script left in `/var/spool/bandit24/foo`, then deletes it. Dropping a script there that copies bandit24's password to a world-writable file in `/tmp` reads a password I otherwise can't. |
19 | 19 |
|
20 | 20 | ## Challenge |
21 | 21 | > A program is running automatically at regular intervals from cron, the time-based job scheduler. Look in /etc/cron.d/ for the configuration and see what command is being executed. |
22 | 22 | > NOTE: This level requires you to create your own first shell-script. This is a very big step and you should be proud of yourself when you beat this level! |
23 | 23 | > NOTE: Keep in mind that your shell script is removed once executed, so you may want to keep a copy around… |
24 | 24 |
|
25 | | -- Files: `{filename}` (sha256: `{hash}`) |
26 | | -- Service: `{chal.example.lab:port}` (event infrastructure, now offline) |
| 25 | +- Files: `/etc/cron.d/cronjob_bandit24`, `/usr/bin/cronjob_bandit24.sh` |
| 26 | +- Service: `bandit23@bandit.labs.overthewire.org:2220` (online) |
27 | 27 |
|
28 | 28 | ## First look |
29 | | -{What you checked first and why. Paste key output as text, trimmed.} |
| 29 | +This is the third cron level, but it flips the pattern. Levels 21 and 22 left a password in a file for me to read. This one runs **my** script as bandit24, so instead of reading output, I write the code that reads the password. |
30 | 30 |
|
| 31 | +Reading the script `/usr/bin/cronjob_bandit24.sh` shows what it does every minute: |
| 32 | + |
| 33 | +```bash |
| 34 | +bandit23@bandit:/usr/bin$ head -n -0 cronjob_bandit24.sh |
| 35 | +#!/bin/bash |
| 36 | + |
| 37 | +shopt -s nullglob |
| 38 | + |
| 39 | +myname=$(whoami) |
| 40 | + |
| 41 | +cd /var/spool/"$myname"/foo || exit |
| 42 | +echo "Executing and deleting all scripts in /var/spool/$myname/foo:" |
| 43 | +for i in * .*; |
| 44 | +do |
| 45 | + if [ "$i" != "." ] && [ "$i" != ".." ]; |
| 46 | + then |
| 47 | + echo "Handling $i" |
| 48 | + owner="$(stat --format "%U" "./$i")" |
| 49 | + if [ "${owner}" = "bandit23" ] && [ -f "$i" ]; then |
| 50 | + timeout -s 9 60 "./$i" |
| 51 | + fi |
| 52 | + rm -rf "./$i" |
| 53 | + fi |
| 54 | +done |
31 | 55 | ``` |
32 | | -{command} |
33 | | -{trimmed output} |
34 | | -``` |
| 56 | + |
| 57 | +The job runs as bandit24, so for every file in `/var/spool/bandit24/foo` that is **owned by bandit23**, it runs that file as bandit24, then deletes it. I am bandit23, so a script I drop there passes the owner check and runs with bandit24's permissions. |
| 58 | + |
| 59 | +### Theory |
| 60 | +- **`$(whoami)` inside the script:** `whoami` returns the user the script runs *as*, not the user who wrote it. The cron job runs as bandit24, so `$myname` is `bandit24` and the folder it scans is `/var/spool/bandit24/foo`, not `bandit23`. |
| 61 | +- **The drop folder:** a directory where a privileged job runs whatever it finds. If an unprivileged user can write to it, they can run code as the job's user. That is the whole weakness. |
| 62 | +- **Directory permissions for cross-user writes:** creating a file *inside* a directory needs write and execute permission **on that directory**. For bandit24 to write its output into my folder, the folder must let bandit24 in. |
| 63 | +- **`/tmp` sticky bit (`1733`):** anyone can enter `/tmp` and read a file whose exact name they know, but cannot list it. That's why output goes to `/tmp` and both users can reach it. |
35 | 64 |
|
36 | 65 | ## Dead ends |
37 | | -- {Idea}: {what happened} -> {why it didn't work} |
38 | | -- {Idea}: {what happened} -> {why it didn't work} |
| 66 | +- Copying the script to `/var/spool/bandit23/foo`: `cp` failed with "No such file or directory" -> I read the script as myself and assumed `$myname` was `bandit23`. The job runs as `bandit24`, so `$myname` is `bandit24` and the real folder is `/var/spool/bandit24/foo`. The lesson: resolve a privileged script's variables as the user it runs as, not as you. |
| 67 | +- Using `mktemp -d` for the working folder: the cron job never wrote `password.txt` into it -> `mktemp -d` creates the folder with mode `700` (owner only). `bandit24` has no write or execute permission on a `700` folder, so its redirect into the folder silently fails. A plain `mkdir` plus `chmod 777` leaves it open for `bandit24` to write. |
| 68 | +- Trying to `cd` into the `/tmp` hash file from Level 22, and `ls /tmp`: `cd` said "Not a directory" and `ls` said "Permission denied" -> the target is a file, not a folder, and `/tmp`'s sticky bit blocks listing. Use `cat` with the full path instead of exploring. |
39 | 69 |
|
40 | 70 | ## Solution |
41 | | -### Step 1: {what this step achieves} |
42 | | -{Why you took this step, what you expected, what you saw.} |
| 71 | +### Step 1: Make a working folder in `/tmp` that bandit24 can write to |
| 72 | +I need a known place that `bandit24` can write to and I can read. `/tmp` is world-writable, so I made a folder there and opened its permissions fully. `700` from `mktemp -d` would lock bandit24 out, so I used `mkdir` and `chmod 777`. |
43 | 73 |
|
44 | | -### Step 2: {what this step achieves} |
45 | | -{Reasoning first, then the minimum command or code.} |
| 74 | +```bash |
| 75 | +bandit23@bandit:~$ mkdir -p /tmp/adrian23 |
| 76 | +bandit23@bandit:~$ chmod -R 777 /tmp/adrian23 |
| 77 | +``` |
| 78 | + |
| 79 | +### Step 2: Write the script that will run as bandit24 |
| 80 | +When the cron job runs this as `bandit24`, `cat /etc/bandit_pass/bandit24` succeeds, because `bandit24` can read its own password. It writes the password into my folder, and `chmod 666` makes that output readable by me. The quoted `'EOF'` stops my shell expanding anything inside the here-document. |
46 | 81 |
|
47 | | -```{language} |
48 | | -# solve script: minimal, commented, no hardcoded secrets |
49 | | -{code} |
| 82 | +```bash |
| 83 | +bandit23@bandit:~$ cat > /tmp/adrian23/getpass.sh << 'EOF' |
| 84 | +#!/bin/bash |
| 85 | +cat /etc/bandit_pass/bandit24 > /tmp/adrian23/password.txt |
| 86 | +chmod 666 /tmp/adrian23/password.txt |
| 87 | +EOF |
| 88 | +bandit23@bandit:~$ chmod -R 777 /tmp/adrian23 |
50 | 89 | ``` |
51 | 90 |
|
52 | | - |
| 91 | +### Step 3: Drop the script in the folder the cron job scans |
| 92 | +The job scans `/var/spool/bandit24/foo` (because `$myname` is `bandit24`), and runs only files owned by `bandit23`. I **copy** the script in, not move it, so my master copy survives when the job deletes the planted one. |
| 93 | + |
| 94 | +```bash |
| 95 | +bandit23@bandit:~$ cp /tmp/adrian23/getpass.sh /var/spool/bandit24/foo/ |
| 96 | +``` |
| 97 | + |
| 98 | +### Step 4: Wait for the job to run, then read the password |
| 99 | +The job runs about every minute. Until it fires, `password.txt` does not exist, so I waited and then read it. |
| 100 | + |
| 101 | +```bash |
| 102 | +bandit23@bandit:~$ cat /tmp/adrian23/password.txt |
| 103 | +{Password for bandit24} |
| 104 | +``` |
53 | 105 |
|
54 | 106 | ## Flag |
55 | | -`CTF7{...}` {or: "Omitted at the organizer's request."} |
| 107 | +`Password for bandit24` |
56 | 108 |
|
57 | 109 | ## Lessons learned |
58 | | -- {What you'd check first next time} |
59 | | -- {Tool or technique you learned} |
60 | | -- {What made the challenge click} |
| 110 | +- Resolve a privileged script's variables as the user it runs as. `$(whoami)` in the cron job is `bandit24`, not me, which decides the real folder path. |
| 111 | +- Directory permissions, not file permissions, govern who can create files inside. A `700` folder from `mktemp -d` blocks every other user, which is why bandit24 couldn't write there. |
| 112 | +- A cron job that runs scripts from a folder an unprivileged user can write to is a privilege escalation path. Drop a script, wait one cycle, run as the job's user. |
| 113 | +- Keep a master copy of a planted script: the job deletes whatever it executes. |
| 114 | +- This is the same insecure-permissions weakness (CWE-732) as the whole cron trilogy, seen from the attacker's side. The loose permissions I needed to make it work are exactly what an auditor flags to prevent it (MITRE ATT&CK T1053.003, Scheduled Task/Job: Cron). |
61 | 115 |
|
62 | 116 | ## References |
63 | | -- {Docs, papers, other writeups that helped, with links} |
64 | | -- Thanks to {author} for the challenge and {teammates} for {help}. |
| 117 | +- [cron](https://man7.org/linux/man-pages/man8/cron.8.html) |
| 118 | +- [chmod and permission bits](https://man7.org/linux/man-pages/man1/chmod.1.html) |
| 119 | +- [mktemp](https://man7.org/linux/man-pages/man1/mktemp.1.html) |
| 120 | +- [Bash here-documents](https://www.gnu.org/software/bash/manual/html_node/Redirections.html) |
| 121 | +- [CWE-732: Incorrect Permission Assignment for Critical Resource](https://cwe.mitre.org/data/definitions/732.html) |
| 122 | +- [MITRE ATT&CK T1053.003: Cron](https://attack.mitre.org/techniques/T1053/003/) |
0 commit comments