Files
pentest.intro/lib/cmd_provision.sh
T
quoije 645adea875 Provisioning fixes: auth-based readiness, domain join guards, site push
- wait for WinRM with an authenticated call, not a TCP probe: the port opens
  before the service can authenticate
- skip promotion/join when the machine is already there, so provision is
  idempotent
- probe the domain with System.DirectoryServices: Get-ADDomain needs RSAT,
  which a member server does not have
- passwords must not contain the account name (Legault2024! is rejected)
- Web-Dir-Browsing is the real feature name
- push the site over SMB from C$: WinRM caps the command line at 8 KB
2026-10-21 20:02:43 -04:00

142 lines
4.4 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 <= 10 )); do
if _winrm "$@"; then return 0; fi
warn "winrm attempt ${n}/10 failed, retrying"
sleep 15
n=$(( n + 1 ))
done
return 1
}
# The TCP port opens before WinRM can authenticate, so probe with a real call.
_wait_winrm() {
local host=$1 timeout=${2:-1800}
local deadline=$(( SECONDS + timeout ))
while (( SECONDS < deadline )); do
if _winrm "$host" ps 'exit 0' >/dev/null 2>&1; 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 [[ -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; }
# Clear a marker from an earlier attempt, then run the stage ourselves —
# an ONSTART task has already fired by the time we get here.
_winrm_retry "$ip" ps "Remove-Item -Force -ErrorAction SilentlyContinue 'C:\\norbourg\\roles\\$role-post.ps1.done'" >/dev/null || true
_winrm_retry "$ip" start "C:\\norbourg\\roles\\$role-post.ps1" || {
fail "$name: could not start $role-post.ps1"
return 1
}
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"
}