Files
pentest.intro/lib/cmd_provision.sh
T
quoije 279f4cc547 Phase 5: AD role scripts, WinRM provisioning and lab wordlist
dc01 promotes the forest then runs a startup-scheduled post stage for OUs,
users, groups, the SPN on svc_backup, pre-auth disabled on j.contractor,
GenericAll over t.smith, the www A record, flags and advanced audit policy.
fs01 installs IIS, shares the web root's public/ as \\FS01\Public and joins.
ws01 joins, then adds WS Admins to local Administrators.

provision uploads over WinRM, starts scripts detached, waits on a .done
marker and survives the promotion reboot.
2026-10-21 19:29:51 -04:00

122 lines
3.5 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"
}
_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"
_winrm "$ip" put "$LAB_DIR/windows/lib/common.ps1" 'C:\norbourg\lib\common.ps1' \
|| die "$name: could not upload common.ps1"
_winrm "$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 "$ip" put "$work/$role-post.ps1" "C:\\norbourg\\roles\\$role-post.ps1" \
|| die "$name: could not upload $role-post.ps1"
fi
_winrm "$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"
}