- dc01-post: drop the vestigial Serveurs-Admins group. It only existed to be nested into the DC's administrators, which was removed; leaving it would suggest a privilege that no longer exists. - prerequis: participants who bring their own Kali are told where to get norbourg.txt, and the address table no longer promises a provided VM. - qemu check: assert the firewalld isolation zone and policy. They persist across reboots but nothing verified them, so a missing policy would go unnoticed until a VM reached the office LAN. - up/reset: skip the directory wait when dc01 is not in the table. - deck: no implication that a Kali VM is always provided. Redeployed from scratch without Kali: build and provision completed in one pass, doctor reports 0 warnings, and the documented path reproduces through DCSync with no shortcut (C$ denied, decoy ACE absent).
210 lines
5.5 KiB
Bash
210 lines
5.5 KiB
Bash
# shellcheck shell=bash
|
|
# cmd_build / cmd_up / cmd_down / cmd_status — VM lifecycle for the qemu backend.
|
|
|
|
cmd_build() {
|
|
local dry=0 arg
|
|
for arg in "$@"; do
|
|
case $arg in
|
|
--dry-run) dry=1 ;;
|
|
-h|--help) info "usage: ./lab.sh build [--dry-run]"; return 0 ;;
|
|
*) die "unknown argument: $arg" ;;
|
|
esac
|
|
done
|
|
|
|
step "answer files"
|
|
if (( dry )); then
|
|
info " + render ${#VM_NAMES[@]} unattend file(s) and pack seed ISOs"
|
|
else
|
|
cmd_seeds >/dev/null || die "seed build failed"
|
|
ok "${#VM_NAMES[@]} seed ISO(s) ready"
|
|
fi
|
|
|
|
if [[ ! -f $WINDOWS_ISO ]]; then
|
|
(( dry )) || die "missing Windows ISO: $WINDOWS_ISO (run ./lab.sh isos)"
|
|
fi
|
|
|
|
step "virtual disks"
|
|
local name
|
|
for name in "${VM_NAMES[@]}"; do
|
|
backend_create_disk "$name" "$dry"
|
|
done
|
|
|
|
step "launch"
|
|
for name in "${VM_NAMES[@]}"; do
|
|
if backend_running "$name"; then
|
|
info " $name already running"
|
|
continue
|
|
fi
|
|
backend_launch "$name" install "$dry" || die "could not start $name"
|
|
done
|
|
|
|
if (( dry )); then
|
|
printf '\n'
|
|
ok "dry run complete — nothing was created"
|
|
return 0
|
|
fi
|
|
|
|
_wait_for_boot
|
|
}
|
|
|
|
_wait_for_boot() {
|
|
local deadline=$(( SECONDS + ${BUILD_TIMEOUT:-2700} ))
|
|
local i name ip
|
|
local -A pending=()
|
|
|
|
for i in "${!VM_NAMES[@]}"; do
|
|
backend_running "${VM_NAMES[$i]}" && pending["${VM_NAMES[$i]}"]=${VM_IPS[$i]}
|
|
done
|
|
|
|
step "waiting for unattended installs"
|
|
info " poll every 15 s, up to $(( ${BUILD_TIMEOUT:-2700} / 60 )) min"
|
|
info " watch one: ./lab.sh vnc <vm> · logs: ${LAB_ROOT}/run/"
|
|
|
|
local reported=1
|
|
while (( ${#pending[@]} > 0 )); do
|
|
if (( SECONDS > deadline )); then
|
|
fail "timed out waiting for: ${!pending[*]}"
|
|
return 1
|
|
fi
|
|
for name in "${!pending[@]}"; do
|
|
if tcp_open "${pending[$name]}" 5985; then
|
|
ok "$(printf '%-6s installed and reachable on 5985' "$name")"
|
|
unset 'pending[$name]'
|
|
fi
|
|
done
|
|
if (( ${#pending[@]} > 0 )); then
|
|
if (( SECONDS - reported >= 60 )); then
|
|
info " still waiting: ${!pending[*]}"
|
|
reported=$SECONDS
|
|
fi
|
|
sleep 15
|
|
fi
|
|
done
|
|
|
|
printf '\n'
|
|
ok "all VMs installed and reachable"
|
|
|
|
if declare -F backend_finalize >/dev/null; then
|
|
step "finalize"
|
|
for name in "${VM_NAMES[@]}"; do
|
|
backend_finalize "$name" || warn "$name: finalize step failed"
|
|
done
|
|
fi
|
|
|
|
info " next: ./lab.sh status"
|
|
}
|
|
|
|
# Start the DC first and wait for it: a member that boots before the DC
|
|
# loses its machine-account channel and fails to authenticate (NO_LOGON_SERVERS).
|
|
_start_lab() {
|
|
local name
|
|
|
|
if vm_index dc01 >/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
|
|
backend_ready dc01 240 || warn "dc01 did not answer 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"; return 1; }
|
|
fi
|
|
done
|
|
|
|
# Return only once every guest answers: running `doctor` the instant `up`
|
|
# printed its last line used to report false failures during boot.
|
|
step "wait for the guests"
|
|
local notready=0
|
|
for name in "${VM_NAMES[@]}"; do
|
|
if backend_ready "$name" 300; then
|
|
ok "$(printf '%-6s ready' "$name")"
|
|
else
|
|
warn "$name did not answer in time"
|
|
notready=1
|
|
fi
|
|
done
|
|
if (( notready )); then
|
|
warn "skipping the directory check: not every guest answered"
|
|
return 1
|
|
fi
|
|
|
|
# WinRM answers before the directory does. A doctor run right after up saw
|
|
# "stable path missing" and "replication rights missing" on a healthy lab
|
|
# because the DC was still starting AD.
|
|
if ! vm_index dc01 >/dev/null 2>&1; then
|
|
return 0
|
|
fi
|
|
step "wait for the domain"
|
|
local deadline=$(( SECONDS + 240 ))
|
|
while (( SECONDS < deadline )); do
|
|
if backend_ps dc01 'Import-Module ActiveDirectory; (Get-ADDomain).DNSRoot' >/dev/null 2>&1; then
|
|
ok "directory services answer"
|
|
return 0
|
|
fi
|
|
sleep 5
|
|
done
|
|
warn "the domain did not answer in time — check with ./lab.sh doctor"
|
|
return 1
|
|
}
|
|
|
|
cmd_up() {
|
|
step "start"
|
|
_start_lab
|
|
}
|
|
|
|
cmd_down() {
|
|
local name
|
|
step "stop"
|
|
for name in "${VM_NAMES[@]}"; do
|
|
backend_stop "$name"
|
|
done
|
|
}
|
|
|
|
cmd_status() {
|
|
local i name ip pid state sync
|
|
printf '\n%s%-7s %-14s %-9s %-8s %s%s\n' "$C_BOLD" VM IP STATE PID VNC "$C_RESET"
|
|
for i in "${!VM_NAMES[@]}"; do
|
|
name=${VM_NAMES[$i]}
|
|
ip=${VM_IPS[$i]}
|
|
if backend_running "$name"; then
|
|
state="running"
|
|
pid=$(<"$(qemu_pidfile "$name")")
|
|
else
|
|
state="stopped"
|
|
pid="-"
|
|
fi
|
|
printf '%-7s %-14s %-9s %-8s %s\n' "$name" "$ip" "$state" "$pid" "$(( 5900 + i + 1 ))"
|
|
done
|
|
|
|
# Reachability is only meaningful once the guest has bootstrapped.
|
|
printf '\n'
|
|
for i in "${!VM_NAMES[@]}"; do
|
|
name=${VM_NAMES[$i]}
|
|
ip=${VM_IPS[$i]}
|
|
backend_running "$name" || continue
|
|
sync="no"
|
|
tcp_open "$ip" 5985 && sync="yes"
|
|
printf ' %-6s winrm: %s\n' "$name" "$sync"
|
|
done
|
|
}
|
|
|
|
cmd_vnc() {
|
|
local name=${1:-}
|
|
local i
|
|
[[ -n $name ]] || die "usage: ./lab.sh vnc <vm> (VMs: ${VM_NAMES[*]})"
|
|
i=$(vm_index "$name") || die "unknown vm: $name"
|
|
local port=$(( 5900 + i + 1 ))
|
|
if have_cmd remote-viewer; then
|
|
exec remote-viewer "vnc://127.0.0.1:${port}"
|
|
elif have_cmd vncviewer; then
|
|
exec vncviewer "127.0.0.1:${port}"
|
|
fi
|
|
info "no VNC client found. Connect to 127.0.0.1:${port} (display :$(( i + 1 )))"
|
|
}
|