Scavenger Hunt
Check out the blog for the latest content!
Scavenger Hunt
#!/usr/bin/env bash
# Ubuntu Scavenger Hunt seeder
# Creates folders, flags, and light config to support the hunt.
# Safe to re-run; will not clobber existing student work.
set -euo pipefail
### --- Config (feel free to change) ---
FLAG1="FLAG{I_AM_HERE}" # Task 1 flag revealed via pwd alias
FLAG2="FLAG{HIDDEN_IN_PLAIN_SIGHT}" # Task 2 flag inside .readme
BIRD="ROBIN" # Task 4 bird folder/file name
CREATE_LABMATE=true # Task 7: create a 'labmate' user
LABMATE_SHELL="/bin/bash"
APT_PACKAGES=(tree htop nginx lsb-release) # Tools for tasks 4,5,10 & info
TEACHER_NOTE="~/.scavenger_teacher_notes.txt"
### ------------------------------------
# Detect distro (best-effort)
if ! command -v lsb_release >/dev/null 2>&1; then
echo "[*] Installing lsb-release for distro detection (requires sudo)..."
sudo apt-get update -y
sudo apt-get install -y lsb-release
fi
DISTRO="$(lsb_release -sd || echo Ubuntu)"
echo "[*] Detected: $DISTRO"
# Ensure bashrc exists
touch "${HOME}/.bashrc"
echo "[*] Creating hunt directories..."
mkdir -p "${HOME}/hunt/level1"
# --- Task 1 seed: show a flag when students run `pwd` ---
# We drop a file and lightly alias pwd to print it after the real path.
echo "$FLAG1" > "${HOME}/.whereami.flag"
if ! grep -q "SCAV_HUNT_PWD_ALIAS" "${HOME}/.bashrc"; then
cat >> "${HOME}/.bashrc" <<'EOF'
# SCAV_HUNT_PWD_ALIAS: show a tiny hint/flag once per shell when running `pwd`
if [ -f "$HOME/.whereami.flag" ]; then
# Avoid double aliasing if re-sourced
if ! alias pwd 2>/dev/null | grep -q 'builtin pwd'; then
alias pwd='builtin pwd; [ -f "$HOME/.whereami.flag" ] && cat "$HOME/.whereami.flag"'
fi
fi
EOF
echo "[*] Added pwd alias to ~/.bashrc (reload terminal to activate)."
else
echo "[*] pwd alias already present."
fi
# --- Task 2 seed: hidden .readme with a flag ---
if [ ! -f "${HOME}/hunt/level1/.readme" ]; then
echo "Welcome to the hunt! ${FLAG2}" > "${HOME}/hunt/level1/.readme"
echo "[*] Seeded ${HOME}/hunt/level1/.readme with flag."
else
echo "[*] ${HOME}/hunt/level1/.readme already exists (skipping)."
fi
# --- Task 4 seed: bird-named item students will see in `tree ~` ---
if [ ! -d "${HOME}/${BIRD}_notes" ] && [ ! -f "${HOME}/${BIRD}.txt" ]; then
mkdir -p "${HOME}/${BIRD}_notes"
echo "You found the ${BIRD}!" > "${HOME}/${BIRD}_notes/hello.txt"
echo "[*] Created ${HOME}/${BIRD}_notes/ (flag is BIRD name: ${BIRD})."
else
echo "[*] Bird item already present (skipping)."
fi
# --- Install packages for tasks (tree/nginx/etc.) ---
echo "[*] Installing required packages (sudo)..."
export DEBIAN_FRONTEND=noninteractive
sudo apt-get update -y
sudo apt-get install -y "${APT_PACKAGES[@]}"
# --- Task 5 prep: ensure nginx runs, but let students create clue.txt themselves ---
echo "[*] Ensuring nginx is installed. Starting service (sudo)..."
sudo systemctl daemon-reload || true
sudo systemctl enable nginx >/dev/null 2>&1 || true
sudo systemctl start nginx || true
# Create a default index if missing (harmless)
if [ ! -f /var/www/html/index.nginx-debian.html ] && [ ! -f /var/www/html/index.html ]; then
echo "<h1>Ubuntu Scavenger Hunt</h1><p>Nginx is running.</p>" | sudo tee /var/www/html/index.html >/dev/null
echo "[*] Added simple /var/www/html/index.html"
fi
# --- Task 7 optional: create labmate user (no password needed for chown demo) ---
if $CREATE_LABMATE; then
if id labmate >/dev/null 2>&1; then
echo "[*] User 'labmate' already exists (skipping)."
else
echo "[*] Creating user 'labmate' (sudo)..."
sudo useradd -m -s "$LABMATE_SHELL" labmate || true
# No password; students don't need to log in for chown/group checks.
fi
fi
# --- Teacher notes quick-reference ---
cat > "${TEACHER_NOTE}" <<EOF
Ubuntu Scavenger Hunt — Teacher Notes
====================================
Seeded items on: $(date -Iseconds)
User: $(whoami) Host: $(hostname)
Flags & Seeds:
- Task 1: ~/.whereami.flag -> ${FLAG1}
* Activated by a pwd alias injected into ~/.bashrc (reload the terminal).
- Task 2: ~/hunt/level1/.readme -> ${FLAG2}
- Task 4: ~/${BIRD}_notes/ -> BIRD flag is ${BIRD}
- Task 5: nginx installed & started; students create /var/www/html/clue.txt
- Task 7: user 'labmate' created (no password)
Packages installed:
- ${APT_PACKAGES[*]}
Clean-up suggestions:
- Remove pwd alias lines between '# SCAV_HUNT_PWD_ALIAS' and next blank in ~/.bashrc
- rm -f ~/.whereami.flag
- rm -rf ~/hunt ~/${BIRD}_notes
- sudo deluser --remove-home labmate (if desired)
- sudo apt-get remove --purge nginx tree htop lsb-release -y (optional)
EOF
echo "[*] Wrote teacher notes to ${TEACHER_NOTE}"
echo
echo "All set! 👉 Restart or open a new terminal so the pwd alias takes effect."
echo "Quick test commands:"
echo " pwd # should print your path + ${FLAG1}"
echo " cat ~/hunt/level1/.readme"
echo " tree ~ | head # should show ${BIRD}_notes"
echo " curl -I http://127.0.0.1 # nginx running"