diff --git a/backends/qemu.sh b/backends/qemu.sh index 740e4b3..6203126 100644 --- a/backends/qemu.sh +++ b/backends/qemu.sh @@ -195,3 +195,50 @@ backend_stop() { rm -f "$(qemu_pidfile "$name")" ok "$name stopped" } + +# ------------------------------------------------------------------ baseline +backend_snapshot() { + local name=$1 + local golden base overlay + golden=$(qemu_golden "$name") + base=$(qemu_base "$name") + overlay=$(qemu_overlay "$name") + + if backend_running "$name"; then + fail "$name is still running" + return 1 + fi + if [[ -f $base ]]; then + ok "$(printf '%-6s baseline already frozen' "$name")" + return 0 + fi + if [[ ! -f $golden ]]; then + fail "$name has no disk to freeze" + return 1 + fi + + mkdir -p "$(dirname "$base")" "$(dirname "$overlay")" + mv "$golden" "$base" + # Relative backing path: the tree can move without breaking the chain. + qemu-img create -q -f qcow2 -F qcow2 -b "../base/${name}-base.qcow2" "$overlay" + ok "$(printf '%-6s baseline frozen (%s)' "$name" "$(du -h "$base" | cut -f1)")" +} + +backend_reset() { + local name=$1 + local base overlay + base=$(qemu_base "$name") + overlay=$(qemu_overlay "$name") + + if backend_running "$name"; then + backend_stop "$name" >/dev/null 2>&1 || true + fi + if [[ ! -f $base ]]; then + fail "$name has no baseline — run ./lab.sh snapshot first" + return 1 + fi + + rm -f "$overlay" + qemu-img create -q -f qcow2 -F qcow2 -b "../base/${name}-base.qcow2" "$overlay" + ok "$(printf '%-6s reverted' "$name")" +} diff --git a/lab.sh b/lab.sh index 54b8459..e11f485 100755 --- a/lab.sh +++ b/lab.sh @@ -24,6 +24,8 @@ source "$LAB_DIR/lib/cmd_build.sh" source "$LAB_DIR/lib/cmd_provision.sh" # shellcheck source=lib/cmd_snapshot.sh source "$LAB_DIR/lib/cmd_snapshot.sh" +# shellcheck source=lib/cmd_doctor.sh +source "$LAB_DIR/lib/cmd_doctor.sh" usage() { cat </dev/null 2>&1; then + if backend_running dc01; then + info " dc01 already running" + else + backend_launch dc01 run || { fail "dc01 did not start"; return 1; } + fi + _wait_winrm "$LAB_DC_IP" 240 || warn "dc01 did not answer WinRM in time" + fi + for name in "${VM_NAMES[@]}"; do + [[ $name == dc01 ]] && continue if backend_running "$name"; then info " $name already running" else - backend_launch "$name" run || fail "$name did not start" + backend_launch "$name" run || { fail "$name did not start"; return 1; } fi done } +cmd_up() { + step "start" + _start_lab +} + cmd_down() { local name step "stop" diff --git a/lib/cmd_doctor.sh b/lib/cmd_doctor.sh new file mode 100644 index 0000000..9d5bec9 --- /dev/null +++ b/lib/cmd_doctor.sh @@ -0,0 +1,98 @@ +# shellcheck shell=bash +# cmd_doctor — end-to-end health check of the running laboratory. + +_c_ok() { ok "$*"; } +_c_warn() { warn "$*"; DOCTOR_WARNS=$(( DOCTOR_WARNS + 1 )); } +_c_fail() { fail "$*"; DOCTOR_FAILS=$(( DOCTOR_FAILS + 1 )); } + +cmd_doctor() { + DOCTOR_FAILS=0 + DOCTOR_WARNS=0 + local i name ip + + step "host" + if ip link show "$BRIDGE" >/dev/null 2>&1; then + _c_ok "bridge ${BRIDGE} present" + else + _c_fail "bridge ${BRIDGE} missing — run sudo ./scripts/setup-network.sh" + fi + + step "machines" + for i in "${!VM_NAMES[@]}"; do + name=${VM_NAMES[$i]} + ip=${VM_IPS[$i]} + + if ! backend_running "$name"; then + _c_fail "$name is not running — ./lab.sh up" + continue + fi + + if tcp_open "$ip" 445; then + _c_ok "$(printf '%-6s %-14s smb up' "$name" "$ip")" + else + _c_warn "$(printf '%-6s %-14s smb not answering' "$name" "$ip")" + fi + + if [[ $name == dc01 || $name == ws01 || $name == fs01 ]]; then + if tcp_open "$ip" 5985; then + _c_ok "$(printf '%-6s %-14s winrm up' "$name" "$ip")" + else + _c_warn "$(printf '%-6s %-14s winrm not answering' "$name" "$ip")" + fi + fi + done + + step "domain" + if have_cmd dig; then + local www + www=$(dig +short +time=2 +tries=1 "@$LAB_DC_IP" www.norbourg.lab 2>/dev/null | head -1) + if [[ $www == 10.10.10.20 ]]; then + _c_ok "dns: www.norbourg.lab -> $www" + else + _c_fail "dns: www.norbourg.lab did not resolve via ${LAB_DC_IP} (got '${www:-nothing}')" + fi + local srv + srv=$(dig +short +time=2 +tries=1 "@$LAB_DC_IP" SRV _ldap._tcp.dc._msdcs.norbourg.lab 2>/dev/null | head -1) + if [[ -n $srv ]]; then + _c_ok "dns: ldap SRV record present" + else + _c_fail "dns: ldap SRV record missing — AD discovery would fail" + fi + else + _c_warn "dig not installed, DNS checks skipped" + fi + + step "web" + local code + code=$(curl -s -o /dev/null -w '%{http_code}' --max-time 5 "http://10.10.10.20/" 2>/dev/null || true) + if [[ $code == 200 ]]; then + _c_ok "site answers on 10.10.10.20" + else + _c_fail "site not answering (${code:-timeout}) — ./lab.sh web --sync" + fi + + code=$(curl -s -o /dev/null -w '%{http_code}' --max-time 5 "http://10.10.10.20/public/IT-inventaire.txt" 2>/dev/null || true) + if [[ $code == 200 ]]; then + _c_ok "entry point reachable: /public/IT-inventaire.txt" + else + _c_fail "entry point missing (${code:-timeout})" + fi + + step "share" + if have_cmd smbclient; then + if smbclient //10.10.10.20/Public -U 'NORBOURG\helpdesk1%Welcome2024!' -c 'ls' 2>/dev/null | grep -q 'IT-inventaire'; then + _c_ok "Public share reachable with the leaked credential" + else + _c_warn "Public share not readable as helpdesk1 (domain channel or DNS?)" + fi + else + _c_warn "smbclient not installed, share check skipped" + fi + + printf '\n' + if (( DOCTOR_FAILS )); then + fail "${DOCTOR_FAILS} problem(s), ${DOCTOR_WARNS} warning(s)" + return 1 + fi + ok "laboratory healthy · ${DOCTOR_WARNS} warning(s)" +} diff --git a/lib/cmd_snapshot.sh b/lib/cmd_snapshot.sh index d6c44fd..2a855ca 100644 --- a/lib/cmd_snapshot.sh +++ b/lib/cmd_snapshot.sh @@ -30,9 +30,7 @@ cmd_reset() { done step "start" - for name in "${VM_NAMES[@]}"; do - backend_launch "$name" run || fail "$name did not start" - done + _start_lab printf '\n' ok "laboratory reset — previous state discarded" diff --git a/scripts/kali-setup.sh b/scripts/kali-setup.sh new file mode 100755 index 0000000..e682902 --- /dev/null +++ b/scripts/kali-setup.sh @@ -0,0 +1,52 @@ +#!/usr/bin/env bash +# kali-setup.sh — prepare a participant's Kali for the workshop. +# +# ./scripts/kali-setup.sh +# +# Installs the toolset used in the lab and copies the targeted wordlist to +# /opt/norbourg/wordlists/. Safe to run twice. +set -euo pipefail + +HERE=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P) +WORDLIST="$HERE/wordlists/norbourg.txt" + +info() { printf '[*] %s\n' "$*"; } +ok() { printf '[ok] %s\n' "$*"; } +warn() { printf '[!] %s\n' "$*" >&2; } + +if [[ $EUID -eq 0 ]]; then + SUDO="" +else + SUDO="sudo" +fi + +info "installing packages (this takes a few minutes)" +$SUDO apt-get update -qq || warn "apt-get update failed, continuing" +$SUDO DEBIAN_FRONTEND=noninteractive apt-get install -y -qq --no-install-recommends \ + netexec impacket-scripts bloodyad evil-winrm bloodhound.py \ + hashcat seclists smbclient dnsutils python3-pip \ + || warn "some packages failed to install, see the list below" + +info "installing the lab wordlist" +$SUDO mkdir -p /opt/norbourg/wordlists +$SUDO cp "$WORDLIST" /opt/norbourg/wordlists/norbourg.txt +$SUDO chmod 0644 /opt/norbourg/wordlists/norbourg.txt + +info "checking the toolset" +check() { + if command -v "$1" >/dev/null 2>&1; then + ok "$1" + else + warn "$1 missing" + fi +} +for tool in nxc netexec evil-winrm hashcat smbclient dig; do + check "$tool" +done +command -v impacket-GetUserSPNs >/dev/null 2>&1 && ok "impacket-GetUserSPNs" \ + || warn "impacket scripts missing (impacket-scripts)" +command -v bloodhound-python >/dev/null 2>&1 && ok "bloodhound-python" \ + || warn "bloodhound-python missing" + +printf '\n' +ok "done. Lab access is configured separately: see docs/prerequis.md"