|
| 1 | +--- |
| 2 | +title: "OverTheWire Bandit: Level 21 to Level 22" |
| 3 | +date: 2026-07-03 08:33:00 +0500 |
| 4 | +categories: [CTF, Bandit] |
| 5 | +tags: [linux, cron, crontab, scheduled-tasks, overthewire, beginner] |
| 6 | +--- |
| 7 | + |
| 8 | +## The Goal |
| 9 | + |
| 10 | +A program is running automatically at regular intervals via cron. Find what it does and use it to get the password for bandit22. |
| 11 | + |
| 12 | +## What is Cron |
| 13 | + |
| 14 | +Cron is a time-based job scheduler built into Linux. It runs commands automatically at specified times without any user interaction. You define tasks in a crontab file and cron executes them in the background on schedule. |
| 15 | + |
| 16 | +## What I Did |
| 17 | + |
| 18 | +Navigated to `/etc/cron.d/` where cron job configurations are stored: |
| 19 | + |
| 20 | +```bash |
| 21 | +bandit21@bandit:~$ cd /etc/cron.d/ |
| 22 | +bandit21@bandit:/etc/cron.d$ ls |
| 23 | +behemoth4_cleanup clean_tmp cronjob_bandit22 cronjob_bandit23 cronjob_bandit24 e2scrub_all leviathan5_cleanup manpage3_resetpw_job otw-tmp-dir |
| 24 | +``` |
| 25 | + |
| 26 | +Read the bandit22 cron job: |
| 27 | + |
| 28 | +```bash |
| 29 | +bandit21@bandit:/etc/cron.d$ cat cronjob_bandit22 |
| 30 | +@reboot bandit22 /usr/bin/cronjob_bandit22.sh &> /dev/null |
| 31 | +* * * * * bandit22 /usr/bin/cronjob_bandit22.sh &> /dev/null |
| 32 | +``` |
| 33 | + |
| 34 | +Two entries for the same script — `@reboot` runs it once when the server boots, `* * * * *` runs it every minute. The `&> /dev/null` discards all output so nothing is printed to any terminal. |
| 35 | + |
| 36 | +Read the script itself: |
| 37 | + |
| 38 | +```bash |
| 39 | +bandit21@bandit:/etc/cron.d$ cat /usr/bin/cronjob_bandit22.sh |
| 40 | +#!/bin/bash |
| 41 | +chmod 644 /tmp/t7O6lds9S0RqQh9aMcz6ShpAoZKF7fgv |
| 42 | +cat /etc/bandit_pass/bandit22 > /tmp/t7O6lds9S0RqQh9aMcz6ShpAoZKF7fgv |
| 43 | +``` |
| 44 | + |
| 45 | +The script does two things every minute: |
| 46 | +1. Sets the permissions on a file in `/tmp` to `644` — world readable |
| 47 | +2. Writes bandit22's password into that file |
| 48 | + |
| 49 | +Since the file is world readable, anyone can read it including bandit21: |
| 50 | + |
| 51 | +```bash |
| 52 | +bandit21@bandit:/tmp$ cat /tmp/t7O6lds9S0RqQh9aMcz6ShpAoZKF7fgv |
| 53 | +RYVux2rHEm9tiXHmLFzuR7Vhx6AZQMEz |
| 54 | +``` |
| 55 | + |
| 56 | +## Understanding the Crontab Format |
| 57 | + |
| 58 | +The five fields before the username define the schedule: |
| 59 | + |
| 60 | +``` |
| 61 | +* * * * * bandit22 /usr/bin/cronjob_bandit22.sh |
| 62 | +│ │ │ │ │ |
| 63 | +│ │ │ │ └── day of week (0-7, 0 and 7 = Sunday) |
| 64 | +│ │ │ └──── month (1-12) |
| 65 | +│ │ └────── day of month (1-31) |
| 66 | +│ └──────── hour (0-23) |
| 67 | +└────────── minute (0-59) |
| 68 | +``` |
| 69 | + |
| 70 | +`*` means every value for that field. So `* * * * *` runs every minute of every hour of every day. |
| 71 | + |
| 72 | +Some examples: |
| 73 | +- `0 * * * *` — every hour at minute 0 |
| 74 | +- `0 9 * * *` — every day at 9am |
| 75 | +- `0 9 * * 1` — every Monday at 9am |
| 76 | +- `30 6 1 * *` — 6:30am on the 1st of every month |
| 77 | + |
| 78 | +`@reboot` is a special keyword that runs the job once when the system starts, regardless of time. |
| 79 | + |
| 80 | +## What I Tried That Didn't Work |
| 81 | + |
| 82 | +Tried to list and navigate `/tmp` directly — permission denied. The `/tmp` directory itself was restricted so you can't browse it with `ls` or `find`. But you can still read a specific file inside it if you know the exact path and the file has read permissions. The script set `chmod 644` on the file which made it readable by everyone, so `cat /tmp/filename` worked even though listing the directory didn't. |
| 83 | + |
| 84 | +## What I Learned |
| 85 | + |
| 86 | +**Cron jobs run as a specific user.** The username in the crontab entry (`bandit22`) determines whose privileges the script runs with. This script runs as bandit22, which is why it can read `/etc/bandit_pass/bandit22`. |
| 87 | + |
| 88 | +**`/etc/cron.d/` contains system-wide cron jobs.** Individual user cron jobs live in `/var/spool/cron/crontabs/`. The `/etc/cron.d/` directory is readable by all users — useful for reconnaissance when you have access to a system. |
| 89 | + |
| 90 | +**Cron jobs writing to world-readable files is a misconfiguration.** If a privileged cron job writes sensitive data to a location other users can read, that data is exposed. This is a real-world finding in penetration tests. |
| 91 | + |
| 92 | +**You don't need to list a directory to read a file in it.** If you know the exact filename and the file has read permission, `cat /path/to/file` works regardless of whether you can `ls` the directory. |
| 93 | + |
| 94 | +## Commands Used |
| 95 | + |
| 96 | +| Command | What it did | |
| 97 | +|---|---| |
| 98 | +| `ls /etc/cron.d/` | Found the cron job configurations | |
| 99 | +| `cat cronjob_bandit22` | Read the schedule and script path | |
| 100 | +| `cat /usr/bin/cronjob_bandit22.sh` | Read what the script actually does | |
| 101 | +| `cat /tmp/t7O6lds9S0RqQh9aMcz6ShpAoZKF7fgv` | Read the password written by the cron job | |
0 commit comments