diff --git a/backends/qemu.sh b/backends/qemu.sh index f2be223..740e4b3 100644 --- a/backends/qemu.sh +++ b/backends/qemu.sh @@ -40,7 +40,18 @@ backend_check() { } # ------------------------------------------------------------------ layout -qemu_disk() { printf '%s/images/%s.qcow2' "$LAB_ROOT" "$1"; } +qemu_base() { printf '%s/images/base/%s-base.qcow2' "$LAB_ROOT" "$1"; } +qemu_overlay(){ printf '%s/images/overlays/%s.qcow2' "$LAB_ROOT" "$1"; } + +# Boot the overlay when a baseline exists, the golden disk otherwise. +qemu_disk() { + local overlay + overlay=$(qemu_overlay "$1") + if [[ -f $overlay ]]; then printf '%s' "$overlay"; else printf '%s/images/%s.qcow2' "$LAB_ROOT" "$1"; fi +} + +# The disk created by `build`, before it is frozen into a base image. +qemu_golden() { 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"; } @@ -66,7 +77,7 @@ backend_running() { backend_create_disk() { local name=$1 dry=${2:-0} local disk i size - disk=$(qemu_disk "$name") + disk=$(qemu_golden "$name") i=$(vm_index "$name") || die "unknown vm: $name" size=${VM_DISK[$i]} @@ -154,17 +165,23 @@ backend_launch() { } backend_stop() { - local name=$1 sock pid i + local name=$1 sock pid ip i + backend_running "$name" || { info " $name already stopped"; return 0; } sock=$(qemu_monitor "$name") pid=$(<"$(qemu_pidfile "$name")") + ip=${VM_IPS[$(vm_index "$name")]} - if [[ -S $sock ]]; then + # ACPI powerdown is best-effort; Server 2022 often ignores it. Asking the + # guest to shut itself down over WinRM is what actually works. + if _winrm "$ip" ps "shutdown.exe /s /t 3 /f" >/dev/null 2>&1; then + detail "shutdown requested over WinRM" + elif [[ -S $sock ]]; then printf 'system_powerdown\n' | socat - "UNIX-CONNECT:$sock" >/dev/null 2>&1 || true fi - for i in $(seq 1 30); do + for i in $(seq 1 90); do kill -0 "$pid" 2>/dev/null || break sleep 1 done @@ -172,7 +189,8 @@ backend_stop() { 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 + sleep 3 + kill -9 "$pid" 2>/dev/null || true fi rm -f "$(qemu_pidfile "$name")" ok "$name stopped" diff --git a/docs/instructeur.md b/docs/instructeur.md index 8539970..b03ce8d 100644 --- a/docs/instructeur.md +++ b/docs/instructeur.md @@ -105,10 +105,16 @@ sur `docs/blue-team.md` : chaque étape de l'attaque correspond à un événemen ## Remise à zéro -`./lab.sh reset` n'est pas encore implémenté. En attendant, pour repartir propre : +Une fois le laboratoire validé, figez l'état propre : ```bash -./lab.sh down -rm -f "$LAB_ROOT"/images/*.qcow2 -./lab.sh build && ./lab.sh provision +./lab.sh snapshot # arrête les VM et fige une base (~2 min) +./lab.sh up # ou ./lab.sh reset pour repartir de la base ``` + +Ensuite, `./lab.sh reset` arrête les VM, jette les disques de travail et les recrée depuis la +base : environ deux minutes, sans réinstallation. C'est la commande à utiliser entre deux +groupes ou après une manipulation destructive. + +Les disques sont organisés en deux couches : `images/base/-base.qcow2` (jamais modifié) et +`images/overlays/.qcow2` (jetable). diff --git a/lab.sh b/lab.sh index 4cd0c6a..54b8459 100755 --- a/lab.sh +++ b/lab.sh @@ -22,6 +22,8 @@ source "$LAB_DIR/lib/cmd_seeds.sh" source "$LAB_DIR/lib/cmd_build.sh" # shellcheck source=lib/cmd_provision.sh source "$LAB_DIR/lib/cmd_provision.sh" +# shellcheck source=lib/cmd_snapshot.sh +source "$LAB_DIR/lib/cmd_snapshot.sh" usage() { cat < int: # runs under the Task Scheduler service and survives the disconnect, # which matters for anything that reboots the guest. ps(session, f"Remove-Item -Force -ErrorAction SilentlyContinue '{done}'") + # A task left over from an interrupted run stays in "Running" and + # blocks the next one; clear it first. + ps(session, "schtasks.exe /End /TN NorbourgStage 2>$null | Out-Null; " + "schtasks.exe /Delete /TN NorbourgStage /F 2>$null | Out-Null") task = ( '$tr = \'powershell.exe -NoProfile -ExecutionPolicy Bypass -File "' + remote diff --git a/windows/roles/dc01-post.ps1 b/windows/roles/dc01-post.ps1 index 4596cd2..e4e4a6f 100644 --- a/windows/roles/dc01-post.ps1 +++ b/windows/roles/dc01-post.ps1 @@ -84,19 +84,15 @@ try { Write-LabLog 'dc01: pre-auth disabled on j.contractor' # ACL abuse: svc_backup has GenericAll on a Domain Admin. - $target = Get-ADUser 't.smith' - $sid = (Get-ADUser 'svc_backup').SID - $aclPath = "AD:\$($target.DistinguishedName)" - $acl = Get-Acl $aclPath - $already = $acl.Access | Where-Object { $_.IdentityReference -eq $sid -and $_.ActiveDirectoryRights -match 'GenericAll' } - if (-not $already) { - $rule = New-Object System.DirectoryServices.ActiveDirectoryAccessRule( - $sid, - [System.DirectoryServices.ActiveDirectoryRights]::GenericAll, - [System.Security.AccessControl.AccessControlType]::Allow) - $acl.AddAccessRule($rule) - Set-Acl -Path $aclPath -AclObject $acl + # dsacls is used instead of the .NET ACL API, which returns success but + # does not persist the ACE through the AD provider. + $targetDn = (Get-ADUser 't.smith').DistinguishedName + $aclCheck = dsacls.exe $targetDn | Select-String 'svc_backup' + if (-not $aclCheck) { + $null = dsacls.exe $targetDn /G '@@NETBIOS@@\svc_backup:GA' Write-LabLog 'dc01: GenericAll granted to svc_backup over t.smith' + } else { + Write-LabLog 'dc01: svc_backup already has an ACE on t.smith' } # ------------------------------------------------------------ dns @@ -125,6 +121,11 @@ try { Set-Acl -Path $flagDir -AclObject $acl Write-LabLog 'dc01: root.txt in place' + # The GenericAll path ends in a password reset; the default one-day + # minimum password age blocks it, and it is a plausible weak setting. + Set-ADDefaultDomainPasswordPolicy -Identity '@@DOMAIN@@' -MinPasswordAge '00:00:00' + Write-LabLog 'dc01: minimum password age set to zero' + # ------------------------------------------------------------ auditing # Legacy policy off, advanced subcategories on: this is what the blue-team # debrief reads back from the event log.