Files
pentest.intro/session1/content/windows/roles/dc01.ps1
T
quoije ca72e1aa2e windows: fix first-boot races and non-idempotent stages
A from-scratch rebuild surfaced three defects that only appear on a clean
deployment:

- dc01-post: `Set-ADObject -Identity <sAMAccountName>` raises
  ADIdentityNotFoundException even right after New-ADUser, so resolve the
  DN once and reuse it. The stage aborted before DNS, flags, DHCP and the
  audit policy, leaving a half-built domain.
- bootstrap ran the role script at first boot while `provision` also ran
  it. fs01 tried to join a domain that did not exist yet, and dc01 ran two
  promotions concurrently; the loser wrote "FAIL Stream was not readable".
  Roles are now only started by the harness, which orders them.
- fs01: the `.url` MIME existence test read the staticContent element
  (-Name '.') instead of its collection, so any re-run died on a duplicate
  entry. Test -Name 'collection'.

Every stage now takes a PID-based lock: a duplicate start exits instead of
racing the real one.

Validated on a fresh build: doctor 0 warnings, and the full path
(site -> SMB -> AS-REP -> Kerberoast -> WinRM -> ACL -> replication ->
DCSync) reproduces.
2026-10-24 16:38:37 -04:00

47 lines
1.5 KiB
PowerShell

# Norbourg lab — DC01 stage 1: install AD DS and create the forest.
# Promotion reboots the machine, so the account/ACL work lives in dc01-post.ps1
# and is scheduled as a startup task before the reboot.
. "$PSScriptRoot\..\lib\common.ps1"
$self = $PSCommandPath
if (-not (Enter-StageLock $self)) {
Write-LabLog 'stage already running on this guest; duplicate start ignored'
return
}
Write-LabLog 'dc01: stage 1 starting'
try {
# Re-running provision must not try to promote a server that already is one.
if ((Get-Service NTDS -ErrorAction SilentlyContinue).Status -eq 'Running') {
Complete-Stage $self 'ok already-dc'
return
}
$feature = Get-WindowsFeature -Name AD-Domain-Services
if (-not $feature.Installed) {
Write-LabLog 'dc01: installing AD-Domain-Services'
$null = Install-WindowsFeature -Name AD-Domain-Services -IncludeManagementTools
} else {
Write-LabLog 'dc01: AD-Domain-Services already installed'
}
$safeMode = ConvertTo-SecureString '@@SAFE_MODE_PASSWORD@@' -AsPlainText -Force
Write-LabLog 'dc01: creating forest norbourg.lab'
$null = Install-ADDSForest `
-DomainName '@@DOMAIN@@' `
-DomainNetbiosName '@@NETBIOS@@' `
-InstallDns `
-SafeModeAdministratorPassword $safeMode `
-NoRebootOnCompletion `
-Force
Complete-Stage $self 'ok reboot'
Restart-Computer -Force
} catch {
Fail-Stage $self $_.Exception.Message
} finally {
Exit-StageLock $self
}