From c66b5641b6276b9ef1b394276eb8149137dac40e Mon Sep 17 00:00:00 2001 From: Mathieu Moran Date: Wed, 21 Oct 2026 18:46:31 -0400 Subject: [PATCH] Phase 3: unattended install, seed ISOs and VM lifecycle autounattend template (q35/AHCI/e1000, no virtio injection), per-VM seed ISOs with a bootstrap that configures the static IP, enables WinRM/RDP and chains into the role script. build --dry-run, up/down/status/vnc verbs. Static addressing happens in PowerShell rather than in the unattend file: Microsoft-Windows-TCPIP matches adapters by connection name and fails silently when the name differs. --- README.md | 23 +++-- backends/qemu.sh | 141 ++++++++++++++++++++++++++++++ lab.conf | 9 ++ lab.sh | 49 +++++++---- lib/cmd_build.sh | 149 ++++++++++++++++++++++++++++++++ lib/cmd_isos.sh | 51 +++++++++++ lib/cmd_seeds.sh | 66 ++++++++++++++ lib/common.sh | 17 ++++ seed/Autounattend.xml.tmpl | 173 +++++++++++++++++++++++++++++++++++++ seed/payload/bootstrap.cmd | 4 + seed/payload/bootstrap.ps1 | 61 +++++++++++++ 11 files changed, 719 insertions(+), 24 deletions(-) create mode 100644 lib/cmd_build.sh create mode 100644 lib/cmd_isos.sh create mode 100644 lib/cmd_seeds.sh create mode 100644 seed/Autounattend.xml.tmpl create mode 100644 seed/payload/bootstrap.cmd create mode 100644 seed/payload/bootstrap.ps1 diff --git a/README.md b/README.md index 76ebaf8..b23c4c6 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # Laboratoire Norbourg — atelier d'introduction au pentest de domaine -> **État : phase 1 (échafaudage).** Aucune machine virtuelle n'est encore déployée. -> Les commandes marquées « à venir » existent dans l'interface mais ne font rien pour l'instant. +> **État : phase 3.** L'échafaudage, la préparation du site et l'installation automatique sont +> prêts. Le réseau isolé demande encore une commande `sudo`, et le domaine n'est pas monté. Laboratoire Active Directory isolé, déployé par script, pour initier des collègues au pentest de domaine. L'objectif de l'atelier : partir d'un accès anonyme et obtenir les droits de @@ -43,19 +43,26 @@ br-lab 10.10.10.230 ./lab.sh check # vérifie l'hôte, le stockage, le réseau et la configuration ./lab.sh check -v # avec le détail ./lab.sh init # crée l'arborescence de stockage +./lab.sh isos # télécharge les images Windows dans le cache ISO +./lab.sh seeds # génère les fichiers de réponse et les ISO d'amorçage ``` `check` refuse de continuer si le stockage est inadapté (NTFS, système de fichiers en erreur, -espace insuffisant). Les avertissements réseau sont normaux tant que la phase 2 n'est pas faite. +espace insuffisant). Les avertissements réseau sont normaux tant que le pont n'existe pas. -Prochaines étapes, dans l'ordre : +Ensuite, une seule commande demande `sudo` : ```bash -sudo ./scripts/setup-network.sh # à venir — pont br-lab et isolation -./lab.sh build # à venir — installation des VM -./lab.sh provision # à venir — domaine, comptes, ACL +sudo ./scripts/setup-network.sh # pont br-lab, isolation, redirection IP +./lab.sh build # disques + installation des 3 VM, sans intervention +./lab.sh status +./lab.sh vnc dc01 # console graphique pendant l'installation +./lab.sh provision # à venir (phase 5) : domaine, comptes, ACL ``` +`build` attend que chaque VM réponde sur WinRM (port 5985) et signale celles qui tardent. +`./lab.sh build --dry-run` affiche les commandes QEMU sans rien créer. + ## Structure du dépôt | Chemin | Rôle | @@ -67,7 +74,7 @@ sudo ./scripts/setup-network.sh # à venir — pont br-lab et isolation | `seed/` | fichiers de réponse Windows et fabrication des ISO d'amorçage | | `windows/roles/` | scripts PowerShell par machine (à venir) | | `site-src/` | site Norbourg 2002 : miroir, images retouchées, scripts d'origine | -| `web/` | site assemblé pour FS01 et fichiers pièges (à venir) | +| `web/` | site assemblé pour FS01, fichiers pièges | | `docs/` | documentation de l'atelier (à venir) | | `answers/` | solution complète — **ignoré par git** | diff --git a/backends/qemu.sh b/backends/qemu.sh index 8336321..4c82d3d 100644 --- a/backends/qemu.sh +++ b/backends/qemu.sh @@ -1,5 +1,8 @@ # shellcheck shell=bash # QEMU/KVM backend — local development and dry runs. +# +# Hardware model matches the Proxmox target on purpose: q35 + AHCI (SATA) and +# an e1000 NIC, so the unattended file never needs virtio drivers. backend_check() { if qemu-system-x86_64 -M help 2>/dev/null | grep -q '^q35'; then @@ -35,3 +38,141 @@ backend_check() { _c_warn "qemu-bridge-helper not found: unprivileged bridge attach will fail" fi } + +# ------------------------------------------------------------------ layout +qemu_disk() { printf '%s/images/%s.qcow2' "$LAB_ROOT" "$1"; } +qemu_pidfile(){ printf '%s/run/%s.pid' "$LAB_ROOT" "$1"; } +qemu_log() { printf '%s/run/%s.log' "$LAB_ROOT" "$1"; } +qemu_monitor(){ printf '%s/run/%s.monitor' "$LAB_ROOT" "$1"; } + +# Deterministic MAC from the last two octets: 10.10.10.31 -> 52:54:00:0a:1f +qemu_mac_for() { + local ip=$1 + local b3=${ip##*.} + local rest=${ip%.*} + local b2=${rest##*.} + printf '52:54:00:%02x:%02x' "$b2" "$b3" +} + +backend_running() { + local pidfile pid + pidfile=$(qemu_pidfile "$1") + [[ -r $pidfile ]] || return 1 + pid=$(<"$pidfile") + [[ -n $pid ]] && kill -0 "$pid" 2>/dev/null +} + +backend_create_disk() { + local name=$1 dry=${2:-0} + local disk i size + disk=$(qemu_disk "$name") + i=$(vm_index "$name") || die "unknown vm: $name" + size=${VM_DISK[$i]} + + if [[ -f $disk ]]; then + ok "$(printf '%-6s disk present (%s)' "$name" "$(du -h "$disk" | cut -f1)")" + return + fi + mkdir -p "$LAB_ROOT/images" + if (( dry )); then + info " + qemu-img create -f qcow2 $disk ${size}G" + return + fi + qemu-img create -q -f qcow2 "$disk" "${size}G" + ok "$(printf '%-6s disk created (%sG)' "$name" "$size")" +} + +backend_launch() { + local name=$1 mode=${2:-run} dry=${3:-0} + local i ip ram vcpu disk seed win vnc + local boot_iso="" args=() cd=1 + + i=$(vm_index "$name") || die "unknown vm: $name" + ip=${VM_IPS[$i]}; ram=${VM_RAM[$i]}; vcpu=${VM_VCPU[$i]} + disk=$(qemu_disk "$name") + seed="$LAB_ROOT/seeds/$name-seed.iso" + win=$WINDOWS_ISO + vnc=$(( i + 1 )) + + [[ -f $seed ]] || (( dry )) || die "missing seed ISO for $name (run ./lab.sh seeds)" + [[ -f $disk ]] || (( dry )) || die "missing disk for $name (run ./lab.sh build)" + + if [[ $mode == install ]] && ! (( dry )); then + [[ -f $win ]] || die "missing Windows ISO: $win (run ./lab.sh isos)" + fi + + args=( + -name "norbourg-$name" + -machine q35,accel=kvm + -cpu host + -smp "${vcpu},sockets=1,cores=${vcpu}" + -m "$ram" + -rtc base=localtime,clock=host + -drive "if=none,id=disk,file=$disk,format=qcow2,cache=none,aio=native,discard=unmap" + -device ide-hd,drive=disk,bus=ide.0 + -netdev "bridge,id=net0,br=$BRIDGE" + -device "e1000,netdev=net0,mac=$(qemu_mac_for "$ip")" + -monitor "unix:$(qemu_monitor "$name"),server,nowait" + -display none + -vnc "127.0.0.1:${vnc}" + ) + + if [[ $mode == install ]]; then + (( dry )) || [[ -f $win ]] || die "missing Windows ISO: $win (run ./lab.sh isos)" + args+=( -drive "if=none,id=cd-install,media=cdrom,file=$win" + -device "ide-cd,drive=cd-install,bus=ide.${cd}" ) + cd=$(( cd + 1 )) + fi + args+=( -drive "if=none,id=cd-seed,media=cdrom,file=$seed" + -device "ide-cd,drive=cd-seed,bus=ide.${cd}" ) + + if [[ $mode == install ]]; then + # once=d: boot the install media this time only, then always from disk. + # Without `once`, Setup would re-enter itself after its own reboot. + args+=( -boot order=c,once=d ) + else + args+=( -boot order=c ) + fi + + if (( dry )); then + info " + qemu-system-x86_64 ${args[*]}" + return + fi + + mkdir -p "$LAB_ROOT/run" + nohup qemu-system-x86_64 "${args[@]}" >>"$(qemu_log "$name")" 2>&1 & + printf '%s\n' "$!" >"$(qemu_pidfile "$name")" + sleep 1 + + if backend_running "$name"; then + ok "$(printf '%-6s started · vnc 127.0.0.1:%d · pid %s' "$name" "$(( 5900 + vnc ))" "$(<"$(qemu_pidfile "$name")")")" + else + fail "$name failed to start — see $(qemu_log "$name")" + return 1 + fi +} + +backend_stop() { + local name=$1 sock pid i + backend_running "$name" || { info " $name already stopped"; return 0; } + + sock=$(qemu_monitor "$name") + pid=$(<"$(qemu_pidfile "$name")") + + if [[ -S $sock ]]; then + printf 'system_powerdown\n' | socat - "UNIX-CONNECT:$sock" >/dev/null 2>&1 || true + fi + + for i in $(seq 1 30); do + kill -0 "$pid" 2>/dev/null || break + sleep 1 + done + + if kill -0 "$pid" 2>/dev/null; then + warn "$name did not shut down cleanly, sending TERM" + kill "$pid" 2>/dev/null || true + sleep 2 + fi + rm -f "$(qemu_pidfile "$name")" + ok "$name stopped" +} diff --git a/lab.conf b/lab.conf index 587bad4..992a5f3 100644 --- a/lab.conf +++ b/lab.conf @@ -34,6 +34,10 @@ LAB_ROOT="/home/quoije/lab-vms/pentest-lab" LAB_ISO_CACHE="/media/amy/pentest-lab-iso" MIN_FREE_GB=20 +# Resolved by `lab.sh isos`. +WINDOWS_ISO="$LAB_ISO_CACHE/SERVER_EVAL_x64FRE_en-us.iso" +VIRTIO_ISO="$LAB_ISO_CACHE/virtio-win.iso" + # ---------------------------------------------------------------- vms # name ip ram_mb disk_gb vcpu role VMS=$(cat <<'EOF' @@ -44,6 +48,11 @@ EOF ) # ---------------------------------------------------------------- windows +# Local administrator password used by the unattend file. Override it in +# lab.conf.local for anything that is not a throwaway lab. +LAB_ADMIN_PASSWORD="${LAB_ADMIN_PASSWORD:-Norbourg!2026}" +# Index of the image inside the eval ISO (2 = Standard, Desktop Experience). +WINDOWS_IMAGE_INDEX=2 # Local milestone uses Server 2022 for every role (no TPM/Secure Boot friction). # Proxmox enables Windows 11 workstations and the WS02 decoy. WORKSTATION_OS="srv2022" # srv2022 | win11 diff --git a/lab.sh b/lab.sh index 6955782..25c6580 100755 --- a/lab.sh +++ b/lab.sh @@ -14,6 +14,12 @@ source "$LAB_DIR/lib/check.sh" source "$LAB_DIR/lib/cmd_init.sh" # shellcheck source=lib/cmd_web.sh source "$LAB_DIR/lib/cmd_web.sh" +# shellcheck source=lib/cmd_isos.sh +source "$LAB_DIR/lib/cmd_isos.sh" +# shellcheck source=lib/cmd_seeds.sh +source "$LAB_DIR/lib/cmd_seeds.sh" +# shellcheck source=lib/cmd_build.sh +source "$LAB_DIR/lib/cmd_build.sh" usage() { cat < 0 )); do break ;; -*) - die "unknown option: $1 (try --help)" + # Before the command this is a typo; after it, the flag belongs to the verb. + if [[ -z $cmd ]]; then + die "unknown option: $1 (try --help)" + fi + break ;; *) if [[ -z $cmd ]]; then @@ -119,19 +132,23 @@ esac # ------------------------------------------------------------------ dispatch case $cmd in - check) cmd_check || exit $? ;; - init) cmd_init ;; - build) planned build "phase 3" ;; - provision) planned provision "phase 5" ;; - snapshot) planned snapshot "phase 8" ;; - up|down) planned "$cmd" "phase 3" ;; - reset) planned reset "phase 8" ;; - status) planned status "phase 8" ;; - web) cmd_web ;; - doctor) planned doctor "phase 8" ;; - peers) planned peers "phase 11" ;; + check) cmd_check || exit $? ;; + init) cmd_init "$@" ;; + isos) cmd_isos "$@" ;; + seeds) cmd_seeds "$@" ;; + web) cmd_web "$@" ;; + build) cmd_build "$@" ;; + up) cmd_up "$@" ;; + down) cmd_down "$@" ;; + status) cmd_status "$@" ;; + vnc) cmd_vnc "$@" ;; + provision) planned provision "phase 5" ;; + snapshot) planned snapshot "phase 8" ;; + reset) planned reset "phase 8" ;; + doctor) planned doctor "phase 8" ;; + peers) planned peers "phase 11" ;; export-kali) planned export-kali "phase 9" ;; - answers) planned answers "phase 9" ;; - destroy) planned destroy "phase 8" ;; - *) die "unknown command: ${cmd} (try --help)" ;; + answers) planned answers "phase 9" ;; + destroy) planned destroy "phase 8" ;; + *) die "unknown command: ${cmd} (try --help)" ;; esac diff --git a/lib/cmd_build.sh b/lib/cmd_build.sh new file mode 100644 index 0000000..b67d51c --- /dev/null +++ b/lib/cmd_build.sh @@ -0,0 +1,149 @@ +# 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 · 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" + info " next: ./lab.sh status" +} + +cmd_up() { + local name + step "start" + for name in "${VM_NAMES[@]}"; do + if backend_running "$name"; then + info " $name already running" + else + backend_launch "$name" run || fail "$name did not start" + fi + done +} + +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 (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 )))" +} diff --git a/lib/cmd_isos.sh b/lib/cmd_isos.sh new file mode 100644 index 0000000..30bd209 --- /dev/null +++ b/lib/cmd_isos.sh @@ -0,0 +1,51 @@ +# shellcheck shell=bash +# cmd_isos — fetch the Windows media into LAB_ISO_CACHE. +# Read-only content, so an NTFS/FUSE cache directory is fine here. + +# name|url|minimum size in MB +ISO_ITEMS=( + "SERVER_EVAL_x64FRE_en-us.iso|https://go.microsoft.com/fwlink/?linkid=2195280|2000" + "virtio-win.iso|https://fedorapeople.org/groups/virt/virtio-win/direct-downloads/stable-virtio/virtio-win.iso|200" +) + +cmd_isos() { + have_cmd curl || die "curl is required" + mkdir -p "$LAB_ISO_CACHE" || die "cannot create $LAB_ISO_CACHE" + + local item name url min_mb target tmp size_mb + for item in "${ISO_ITEMS[@]}"; do + IFS='|' read -r name url min_mb <<< "$item" + target="$LAB_ISO_CACHE/$name" + tmp="$target.part" + + if [[ -f $target ]]; then + size_mb=$(( $(stat -c %s "$target") / 1024 / 1024 )) + if (( size_mb >= min_mb )); then + ok "$name present (${size_mb} MB)" + continue + fi + warn "$name is only ${size_mb} MB, re-downloading" + rm -f "$target" + fi + + step "download $name" + info " from $url" + + local curl_opts=(-L --fail --retry 3 --retry-delay 5 -C - -o "$tmp") + if [[ -t 1 ]]; then + curl "${curl_opts[@]}" --progress-bar "$url" || { rm -f "$tmp"; die "download failed: $name"; } + else + curl "${curl_opts[@]}" -sS "$url" || { rm -f "$tmp"; die "download failed: $name"; } + fi + + mv -f "$tmp" "$target" + size_mb=$(( $(stat -c %s "$target") / 1024 / 1024 )) + if (( size_mb < min_mb )); then + die "$name looks truncated: ${size_mb} MB (expected >= ${min_mb} MB)" + fi + ok "$name ready (${size_mb} MB)" + done + + printf '\n' + ok "ISO cache complete: ${LAB_ISO_CACHE}" +} diff --git a/lib/cmd_seeds.sh b/lib/cmd_seeds.sh new file mode 100644 index 0000000..ac00043 --- /dev/null +++ b/lib/cmd_seeds.sh @@ -0,0 +1,66 @@ +# shellcheck shell=bash +# cmd_seeds — render one unattend + payload per VM and pack a seed ISO. +# Nothing here needs root or a downloaded ISO; it is pure file assembly. + +cmd_seeds() { + local tmpl="$LAB_DIR/seed/Autounattend.xml.tmpl" + local payload="$LAB_DIR/seed/payload" + + [[ -r $tmpl ]] || die "missing template: $tmpl" + [[ -d $payload ]] || die "missing payload dir: $payload" + have_cmd genisoimage || have_cmd xorriso || die "genisoimage or xorriso is required" + : "${LAB_ADMIN_PASSWORD:?LAB_ADMIN_PASSWORD is not set in lab.conf}" + + mkdir -p "$LAB_ROOT/seeds" + + local i name ip role work iso volid dns + for i in "${!VM_NAMES[@]}"; do + name=${VM_NAMES[$i]} + ip=${VM_IPS[$i]} + role=${VM_ROLES[$i]} + dns=$LAB_DC_IP # every lab VM resolves through the DC + + work="$LAB_ROOT/seeds/$name" + rm -rf "$work" + mkdir -p "$work/norbourg/roles" + + sed -e "s|@@HOSTNAME@@|${name}|g" \ + -e "s|@@ADMIN_PASSWORD@@|${LAB_ADMIN_PASSWORD}|g" \ + -e "s|@@IMAGE_INDEX@@|${WINDOWS_IMAGE_INDEX:-2}|g" \ + "$tmpl" > "$work/Autounattend.xml" + + cp "$payload/bootstrap.cmd" "$work/norbourg/" + cp "$payload/bootstrap.ps1" "$work/norbourg/" + + cat > "$work/norbourg/config.ps1" < 0 )) || die "no VMs defined in VMS (lab.conf)" } +vm_index() { + local name=$1 i + for i in "${!VM_NAMES[@]}"; do + if [[ ${VM_NAMES[$i]} == "$name" ]]; then + printf '%s\n' "$i" + return 0 + fi + done + return 1 +} + +# Bash-native TCP probe: no nc dependency, 1 s timeout. +tcp_open() { + local host=$1 port=$2 + timeout 1 bash -c "exec 3<>/dev/tcp/${host}/${port}" 2>/dev/null +} + sum_vm_ram_mb() { local total=0 r for r in "${VM_RAM[@]}"; do total=$(( total + r )); done diff --git a/seed/Autounattend.xml.tmpl b/seed/Autounattend.xml.tmpl new file mode 100644 index 0000000..9ff0508 --- /dev/null +++ b/seed/Autounattend.xml.tmpl @@ -0,0 +1,173 @@ + + + + + + + + en-US + + en-US + en-US + en-US + en-US + + + + + + 0 + true + + + 1 + Primary + 500 + + + 2 + Primary + true + + + + + 1 + 1 + true + NTFS + + + + 2 + 2 + NTFS + + C + + + + OnError + + + + + + + /IMAGE/INDEX + @@IMAGE_INDEX@@ + + + + 0 + 2 + + + + + + true + Norbourg + Norbourg Services Financiers + + + + + + + @@HOSTNAME@@ + Eastern Standard Time + Norbourg + Norbourg Services Financiers + + + + true + + + + false + false + + + + + + en-US + en-US + en-US + en-US + + + + + true + true + true + true + true + Work + 3 + true + true + + + + + @@ADMIN_PASSWORD@@ + true</PlainText> + </AdministratorPassword> + </UserAccounts> + + <AutoLogon> + <Enabled>true</Enabled> + <LogonCount>3</LogonCount> + <Username>Administrator</Username> + <Password> + <Value>@@ADMIN_PASSWORD@@</Value> + <PlainText>true</PlainText> + </Password> + </AutoLogon> + + <FirstLogonCommands> + <SynchronousCommand wcm:action="add"> + <Order>1</Order> + <Description>Norbourg bootstrap</Description> + <CommandLine>cmd /c for %d in (D E F G H I J K) do if exist %d:\norbourg\bootstrap.cmd call %d:\norbourg\bootstrap.cmd</CommandLine> + <RequiresUserInput>false</RequiresUserInput> + </SynchronousCommand> + </FirstLogonCommands> + </component> + </settings> + +</unattend> diff --git a/seed/payload/bootstrap.cmd b/seed/payload/bootstrap.cmd new file mode 100644 index 0000000..fb167d9 --- /dev/null +++ b/seed/payload/bootstrap.cmd @@ -0,0 +1,4 @@ +@echo off +rem Norbourg lab — first-logon entry point (runs from the seed ISO). +if not exist C:\norbourg mkdir C:\norbourg +powershell -NoProfile -ExecutionPolicy Bypass -File "%~dp0bootstrap.ps1" >> C:\norbourg\bootstrap.log 2>&1 diff --git a/seed/payload/bootstrap.ps1 b/seed/payload/bootstrap.ps1 new file mode 100644 index 0000000..46397fd --- /dev/null +++ b/seed/payload/bootstrap.ps1 @@ -0,0 +1,61 @@ +# Norbourg lab — first-boot bootstrap, runs once from the seed ISO. +# The unattend file creates the account and triggers this; everything that +# touches the network happens here because unattend TCPIP settings fail +# silently when the adapter name does not match. + +$ErrorActionPreference = 'Stop' +$base = 'C:\norbourg' +New-Item -ItemType Directory -Force -Path $base | Out-Null +Start-Transcript -Path "$base\bootstrap.log" -Append | Out-Null + +$here = Split-Path -Parent $MyInvocation.MyCommand.Path +. (Join-Path $here 'config.ps1') + +Write-Host "bootstrap: host=$($Config.Hostname) role=$($Config.Role) ip=$($Config.IP)" + +# ------------------------------------------------------------------ network +$nic = Get-NetAdapter | + Where-Object { $_.Status -eq 'Up' -and $_.InterfaceDescription -notmatch 'Loopback' } | + Select-Object -First 1 + +if (-not $nic) { + Write-Host 'bootstrap: no up adapter, network step skipped' +} else { + Remove-NetIPAddress -InterfaceIndex $nic.ifIndex -Confirm:$false -ErrorAction SilentlyContinue + Remove-NetRoute -InterfaceIndex $nic.ifIndex -Confirm:$false -ErrorAction SilentlyContinue + + New-NetIPAddress -InterfaceIndex $nic.ifIndex ` + -IPAddress $Config.IP -PrefixLength $Config.Prefix ` + -DefaultGateway $Config.Gateway | Out-Null + + Set-DnsClientServerAddress -InterfaceIndex $nic.ifIndex -ServerAddresses $Config.DNS + Set-NetConnectionProfile -InterfaceIndex $nic.ifIndex -NetworkCategory Private -ErrorAction SilentlyContinue + + Write-Host "bootstrap: $($nic.Name) -> $($Config.IP)/$($Config.Prefix)" +} + +# ------------------------------------------------------------------ WinRM +# The host provisions over WinRM; no guest agent needed for that. +Enable-PSRemoting -SkipNetworkProfileCheck -Force | Out-Null +Set-NetFirewallRule -DisplayGroup 'Windows Remote Management' -Enabled True -Profile Any -ErrorAction SilentlyContinue + +# RDP: useful for the instructor when a step misbehaves. +Set-ItemProperty 'HKLM:\System\CurrentControlSet\Control\Terminal Server' ` + -Name fDenyTSConnections -Value 0 +Enable-NetFirewallRule -DisplayGroup 'Remote Desktop' -ErrorAction SilentlyContinue + +# ------------------------------------------------------------------ marker +"$env:COMPUTERNAME|$($Config.Role)|$($Config.IP)|$((Get-Date).ToString('s'))" | + Set-Content -Path "$base\stamp.txt" -Encoding ascii + +# ------------------------------------------------------------------ role +$role = Join-Path $here "roles\$($Config.Role).ps1" +if (Test-Path $role) { + Write-Host "bootstrap: running role $($Config.Role)" + & $role +} else { + Write-Host "bootstrap: no role script for '$($Config.Role)' yet (phase 5)" +} + +Write-Host 'bootstrap: done' +Stop-Transcript | Out-Null