Files
pentest.intro/session1/content/windows/roles/fs01.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

96 lines
3.5 KiB
PowerShell

# Norbourg lab — FS01: file server and the archived site.
# The web root's "public" folder is both an IIS directory and the SMB share,
# so the web and SMB discovery paths lead to the same file.
. "$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 'fs01: starting'
try {
# Re-running provision must not try to join a machine that already joined.
if ((Get-CimInstance Win32_ComputerSystem).Domain -eq '@@DOMAIN@@') {
Write-LabLog 'fs01: already joined'
Complete-Stage $self 'ok'
return
}
$features = @(
'Web-Server', 'Web-Common-Http', 'Web-Default-Doc',
'Web-Static-Content', 'Web-Dir-Browsing', 'Web-Http-Logging'
)
foreach ($f in $features) {
if (-not (Get-WindowsFeature -Name $f).Installed) {
Write-LabLog "fs01: installing $f"
$null = Install-WindowsFeature -Name $f -IncludeManagementTools
}
}
# IIS ships no MIME mapping for .url; the archived "Your Love" shortcut
# needs one or IIS answers 404.3.
Import-Module WebAdministration
# -Name '.' returns the staticContent element, not its entries, so the
# existence test always failed and a re-run died on a duplicate entry.
# The collection of mappings is -Name 'collection'.
$urlMime = Get-WebConfigurationProperty -PSPath 'MACHINE/WEBROOT/APPHOST' `
-Filter 'system.webServer/staticContent' -Name 'collection' |
Where-Object { $_.fileExtension -eq '.url' }
if (-not $urlMime) {
Add-WebConfigurationProperty -PSPath 'MACHINE/WEBROOT/APPHOST' `
-Filter 'system.webServer/staticContent' -Name '.' `
-Value @{ fileExtension = '.url'; mimeType = 'application/internet-shortcut' }
Write-LabLog 'fs01: .url MIME mapping added'
} else {
Write-LabLog 'fs01: .url MIME mapping already present'
}
$root = 'C:\inetpub\wwwroot'
$public = Join-Path $root 'public'
New-Item -ItemType Directory -Force -Path $public | Out-Null
# Directory browsing only where the lab wants it: the leftover upload dir.
$webconfig = @'
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<directoryBrowse enabled="true" />
</system.webServer>
</configuration>
'@
Set-Content -Path (Join-Path $public 'web.config') -Value $webconfig -Encoding UTF8
if (-not (Get-SmbShare -Name 'Public' -ErrorAction SilentlyContinue)) {
New-SmbShare -Name 'Public' -Path $public -ReadAccess 'Everyone' | Out-Null
Write-LabLog 'fs01: share Public created'
}
# Placeholder so a fresh box is not an empty directory listing.
$placeholder = Join-Path $public 'lisez-moi.txt'
if (-not (Test-Path $placeholder)) {
Set-Content -Path $placeholder -Encoding ascii -Value @'
Depot public du site norbourg.lab
Les formulaires du site deposent leurs pieces jointes ici.
Ne pas supprimer.
'@
}
$cred = New-Object System.Management.Automation.PSCredential(
'@@DOMAIN@@\Administrator',
(ConvertTo-SecureString '@@ADMIN_PASSWORD@@' -AsPlainText -Force))
Write-LabLog 'fs01: joining the domain'
Add-Computer -DomainName '@@DOMAIN@@' -Credential $cred `
-OUPath 'OU=Ordinateurs,OU=Norbourg,@@BASE_DN@@' -Force
Complete-Stage $self 'ok reboot'
Restart-Computer -Force
} catch {
Fail-Stage $self $_.Exception.Message
} finally {
Exit-StageLock $self
}