Files
pentest.intro/session1/content/lib/cmd_build.sh
T
quoije 59e2594c1e dc01: make the delegated ACL the only way out, and stabilise the state
Three shortcuts and one race made the lab either easier than documented or
non-deterministic:

- Serveurs-Admins was nested in BUILTIN\Administrators on the DC, so
  m.legault was a de-facto domain-controller admin (C$, NTDS.dit) and the
  whole ACL lesson could be skipped. It also made SDProp treat her as
  protected: an hour after provisioning, the intended GenericAll ACE was
  wiped. Removed the nesting; adminCount is explicitly reset.
- the t.smith decoy ACE gave a working path to Domain Admin until SDProp
  ran (up to an hour after a fresh build). The role now grants it and
  revokes it immediately, so the state is deterministic.
- the replication-rights grant stacked duplicate ACEs on every re-run; it
  now removes its own ACEs first.
- up/reset/save returned as soon as WinRM answered, while the directory
  was still starting; doctor right after could report a missing ACE and
  missing replication rights on a healthy lab. They now wait for
  Get-ADDomain on dc01.

doctor: a decoy ACE that is still present is now a failure, not a success.
The deck's wording matches the new behaviour.
2026-10-25 14:05:55 -04:00

207 lines
5.4 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.
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 )))"
}