Files
pentest.intro/lib/cmd_provision.sh
T
quoije bb8a6a6eb0 Run role scripts as a scheduled task; add workshop docs
A Start-Process child dies with the WinRM shell, so the promotion never
completed. Role scripts now run under the Task Scheduler as SYSTEM, which
survives the disconnect and the reboot. Uploads retry on transient auth
errors while a guest is busy installing features.

Docs: participant guide, instructor runbook, blue-team debrief, and an
instructor wordlist sized for a two-minute crack.
2026-10-21 19:45:38 -04:00

135 lines
3.9 KiB
Bash

# shellcheck shell=bash
# cmd_provision — upload role scripts over WinRM, run them detached, wait for
# their .done marker, survive reboots, then run the optional -post stage.
LAB_USER=${LAB_USER:-Administrator}
_winrm() {
LAB_PASSWORD=$LAB_ADMIN_PASSWORD python3 "$LAB_DIR/scripts/labwinrm.py" \
"$@" --user "$LAB_USER" --password "$LAB_ADMIN_PASSWORD"
}
# A guest that is installing features or restarting WinRM answers intermittently.
_winrm_retry() {
local n=1
while (( n <= 5 )); do
if _winrm "$@"; then return 0; fi
warn "winrm attempt ${n}/5 failed, retrying"
sleep 15
n=$(( n + 1 ))
done
return 1
}
_wait_winrm() {
local host=$1 timeout=${2:-1800}
local deadline=$(( SECONDS + timeout ))
while (( SECONDS < deadline )); do
if tcp_open "$host" 5985; then
return 0
fi
sleep 10
done
return 1
}
_render_role() {
local src=$1 dst=$2
sed -e "s|@@DOMAIN@@|${DOMAIN}|g" \
-e "s|@@NETBIOS@@|${NETBIOS}|g" \
-e "s|@@BASE_DN@@|${BASE_DN}|g" \
-e "s|@@ADMIN_PASSWORD@@|${LAB_ADMIN_PASSWORD}|g" \
-e "s|@@SAFE_MODE_PASSWORD@@|${LAB_SAFE_MODE_PASSWORD}|g" \
"$src" > "$dst"
}
_provision_one() {
local name=$1 ip=$2 role=$3
local work="$LAB_ROOT/provision/$name"
step "$name ($role, $ip)"
_wait_winrm "$ip" 600 || { fail "$name: WinRM unreachable"; return 1; }
rm -rf "$work"; mkdir -p "$work"
_render_role "$LAB_DIR/windows/roles/$role.ps1" "$work/$role.ps1"
[[ -s $work/$role.ps1 ]] || die "$name: role script missing or empty: windows/roles/$role.ps1"
_winrm_retry "$ip" put "$LAB_DIR/windows/lib/common.ps1" 'C:\norbourg\lib\common.ps1' \
|| die "$name: could not upload common.ps1"
_winrm_retry "$ip" put "$work/$role.ps1" "C:\\norbourg\\roles\\$role.ps1" \
|| die "$name: could not upload $role.ps1"
local post="$LAB_DIR/windows/roles/$role-post.ps1"
if [[ -f $post ]]; then
_render_role "$post" "$work/$role-post.ps1"
_winrm_retry "$ip" put "$work/$role-post.ps1" "C:\\norbourg\\roles\\$role-post.ps1" \
|| die "$name: could not upload $role-post.ps1"
fi
_winrm_retry "$ip" start "C:\\norbourg\\roles\\$role.ps1" "C:\\norbourg\\roles\\$role.log" \
|| die "$name: could not start $role.ps1"
info " running, waiting for the stage marker"
local marker
marker=$(_winrm "$ip" wait "C:\\norbourg\\roles\\$role.ps1" 3600) || {
fail "$name: $role.ps1 did not finish"
return 1
}
if [[ $marker == FAIL* ]]; then
fail "$name: $role.ps1 failed — $marker"
return 1
fi
ok "$name: $marker"
# A reboot stage drops WinRM; wait for the guest to come back before post.
if [[ $marker == *reboot* ]] || [[ -f $post ]]; then
info " waiting for the guest to come back"
_wait_winrm "$ip" 900 || { fail "$name: did not come back after reboot"; return 1; }
fi
if [[ -f $post ]]; then
marker=$(_winrm "$ip" wait "C:\\norbourg\\roles\\$role-post.ps1" 3600) || {
fail "$name: $role-post.ps1 did not finish"
return 1
}
if [[ $marker == FAIL* ]]; then
fail "$name: $role-post.ps1 failed — $marker"
return 1
fi
ok "$name: $marker"
fi
}
cmd_provision() {
local only=""
while (( $# > 0 )); do
case $1 in
--role) only=$2; shift 2 ;;
-h|--help) info "usage: ./lab.sh provision [--role NAME]"; return 0 ;;
*) die "unknown argument: $1" ;;
esac
done
: "${LAB_SAFE_MODE_PASSWORD:?LAB_SAFE_MODE_PASSWORD is not set}"
BASE_DN="DC=${DOMAIN%%.*},DC=${DOMAIN#*.}"
local i name ip role
for i in "${!VM_NAMES[@]}"; do
name=${VM_NAMES[$i]}
ip=${VM_IPS[$i]}
role=${VM_ROLES[$i]}
[[ -n $only && $only != "$name" && $only != "$role" ]] && continue
# The domain must exist before anyone joins it.
if [[ $role != dc ]]; then
_wait_winrm "$LAB_DC_IP" 300 || die "DC01 is not reachable; provision dc01 first"
fi
_provision_one "$name" "$ip" "$role" || die "provision failed for $name"
done
printf '\n'
ok "provisioning complete"
}