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.
This commit is contained in:
2026-10-21 19:29:51 -04:00
parent a38acd3946
commit 279f4cc547
13 changed files with 2570 additions and 6 deletions
+152
View File
@@ -0,0 +1,152 @@
# Norbourg lab — DC01 stage 2: directory content, misconfigurations, flags.
# Runs once at startup as SYSTEM, right after the promotion reboot.
. "$PSScriptRoot\..\lib\common.ps1"
$self = $PSCommandPath
Write-LabLog 'dc01: post stage starting'
try {
Wait-ForDomain 900
Import-Module ActiveDirectory
Write-LabLog 'dc01: directory services reachable'
$dom = Get-ADDomain
$base = $dom.DistinguishedName
# ------------------------------------------------------------ structure
$ous = @('Norbourg', 'Utilisateurs', 'TI', 'Groupes', 'Ordinateurs')
foreach ($ou in $ous) {
$path = "OU=$ou,$base"
if ($ou -eq 'Norbourg') { $path = "OU=Norbourg,$base" }
if (-not (Get-ADOrganizationalUnit -Filter "Name -eq '$ou'" -SearchBase $base -ErrorAction SilentlyContinue)) {
New-ADOrganizationalUnit -Name $ou -Path $(if ($ou -eq 'Norbourg') { $base } else { "OU=Norbourg,$base" }) -ProtectedFromAccidentalDeletion $false
Write-LabLog "dc01: OU $ou created"
}
}
# ------------------------------------------------------------ users
$users = @(
@{ Sam='m.legault'; Given='Marie'; Surname='Legault'; Pwd='Legault2024!'; OU='TI'; Title='Directrice des TI' }
@{ Sam='s.bergeron'; Given='Sophie'; Surname='Bergeron'; Pwd='Bergeron2024!'; OU='TI'; Title='Technicienne, soutien aux usagers' }
@{ Sam='helpdesk1'; Given='Sylvie'; Surname='Tremblay'; Pwd='Welcome2024!'; OU='TI'; Title='Compte partage du helpdesk' }
@{ Sam='svc_backup'; Given='Service'; Surname='Sauvegarde'; Pwd='Backup2023!'; OU='TI'; Title='Compte de service, sauvegarde' }
@{ Sam='t.smith'; Given='Thomas'; Surname='Smith'; Pwd='Str0ngAdmin!2026'; OU='TI'; Title='Administrateur de domaine' }
@{ Sam='j.contractor'; Given='Jacques'; Surname='Contractor'; Pwd='Winter2024!'; OU='Utilisateurs'; Title='Consultant, migration' }
@{ Sam='a.gagnon'; Given='Annie'; Surname='Gagnon'; Pwd='Gagnon2023!'; OU='Utilisateurs'; Title='Comptabilite' }
@{ Sam='p.roy'; Given='Pierre'; Surname='Roy'; Pwd='Roy2022!'; OU='Utilisateurs'; Title='Analyste, operations' }
)
foreach ($u in $users) {
if (Get-ADUser -Filter "SamAccountName -eq '$($u.Sam)'" -ErrorAction SilentlyContinue) {
Write-LabLog "dc01: user $($u.Sam) exists"
continue
}
$ou = if ($u.OU -eq 'TI') { "OU=TI,OU=Norbourg,$base" } else { "OU=Utilisateurs,OU=Norbourg,$base" }
New-ADUser -SamAccountName $u.Sam `
-Name "$($u.Given) $($u.Surname)" `
-GivenName $u.Given -Surname $u.Surname `
-DisplayName "$($u.Given) $($u.Surname)" `
-Title $u.Title `
-Path $ou `
-AccountPassword (ConvertTo-SecureString $u.Pwd -AsPlainText -Force) `
-Enabled $true -PasswordNeverExpires $true -ChangePasswordAtLogon $false
Write-LabLog "dc01: user $($u.Sam) created"
}
# ------------------------------------------------------------ groups
$groups = @{
'TI-Support' = @('s.bergeron', 'helpdesk1')
'WS Admins' = @('svc_backup')
'Serveurs-Admins' = @('m.legault')
}
foreach ($name in $groups.Keys) {
if (-not (Get-ADGroup -Filter "Name -eq '$name'" -ErrorAction SilentlyContinue)) {
New-ADGroup -Name $name -GroupScope Global -GroupCategory Security -Path "OU=Groupes,OU=Norbourg,$base"
Write-LabLog "dc01: group $name created"
}
foreach ($member in $groups[$name]) {
try { Add-ADGroupMember -Identity $name -Members $member -ErrorAction Stop }
catch { Write-LabLog "dc01: $member already in $name" }
}
}
try { Add-ADGroupMember -Identity 'Domain Admins' -Members 't.smith' -ErrorAction Stop }
catch { Write-LabLog 'dc01: t.smith already Domain Admin' }
# ------------------------------------------------------------ misconfigs
# Kerberoastable: an SPN on a normal user account.
Set-ADUser -Identity 'svc_backup' -ServicePrincipalNames @{ Add = 'MSSQLSvc/fs01.norbourg.lab:1433' }
Write-LabLog 'dc01: SPN set on svc_backup'
# AS-REP roastable: pre-authentication disabled.
Set-ADAccountControl -Identity 'j.contractor' -DoesNotRequirePreAuth $true
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
Write-LabLog 'dc01: GenericAll granted to svc_backup over t.smith'
}
# ------------------------------------------------------------ dns
try {
Add-DnsServerResourceRecordA -Name 'www' -ZoneName '@@DOMAIN@@' -IPv4Address '10.10.10.20' -ErrorAction Stop
Write-LabLog 'dc01: www A record created'
} catch {
Write-LabLog 'dc01: www A record already present'
}
# ------------------------------------------------------------ flags
$flagDir = 'C:\flag'
New-Item -ItemType Directory -Force -Path $flagDir | Out-Null
$rootPath = Join-Path $flagDir 'root.txt'
if (-not (Test-Path $rootPath)) {
Set-Content -Path $rootPath -Value 'NORBOURG{do_m41n_4dm1n_0n_dc01}' -Encoding ascii
}
# Administrators only: SYSTEM + Domain Admins inherit through the default ACL.
$acl = Get-Acl $flagDir
$acl.SetAccessRuleProtection($true, $false)
foreach ($id in @('BUILTIN\Administrators', 'NT AUTHORITY\SYSTEM')) {
$rule = New-Object System.Security.AccessControl.FileSystemAccessRule(
$id, 'FullControl', 'ContainerInherit,ObjectInherit', 'None', 'Allow')
$acl.AddAccessRule($rule)
}
Set-Acl -Path $flagDir -AclObject $acl
Write-LabLog 'dc01: root.txt in place'
# ------------------------------------------------------------ auditing
# Legacy policy off, advanced subcategories on: this is what the blue-team
# debrief reads back from the event log.
$lsa = 'HKLM:\SYSTEM\CurrentControlSet\Control\Lsa'
Set-ItemProperty -Path $lsa -Name SCENoApplyLegacyAuditPolicy -Value 1 -Type DWord
$subcategories = @(
'Kerberos Authentication Service',
'Kerberos Service Ticket Operations',
'Logon',
'User Account Management',
'Directory Service Access',
'Directory Service Changes',
'Computer Account Management'
)
foreach ($sub in $subcategories) {
$null = auditpol.exe /set /subcategory:"$sub" /success:enable /failure:enable
}
Write-LabLog 'dc01: advanced audit policy enabled'
$null = schtasks.exe /Delete /TN 'NorbourgDcPost' /F
Complete-Stage $self 'ok'
} catch {
Fail-Stage $self $_.Exception.Message
}
+44
View File
@@ -0,0 +1,44 @@
# 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
Write-LabLog 'dc01: stage 1 starting'
try {
$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'
}
$post = Join-Path $PSScriptRoot 'dc01-post.ps1'
if (-not (Test-Path $post)) { Fail-Stage $self 'dc01-post.ps1 missing next to dc01.ps1' }
# Run the account/ACL stage once, at next startup, as SYSTEM.
$null = schtasks.exe /Create /TN 'NorbourgDcPost' /F /SC ONSTART /RU SYSTEM /RL HIGHEST `
/TR "powershell.exe -NoProfile -ExecutionPolicy Bypass -File `"$post`""
Write-LabLog 'dc01: scheduled dc01-post.ps1 at startup'
$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 `
-ForestMode WinThreshold `
-DomainMode WinThreshold `
-NoRebootOnCompletion `
-Force
Complete-Stage $self 'ok reboot'
Restart-Computer -Force
} catch {
Fail-Stage $self $_.Exception.Message
}
+64
View File
@@ -0,0 +1,64 @@
# 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
Write-LabLog 'fs01: starting'
try {
$features = @(
'Web-Server', 'Web-Common-Http', 'Web-Default-Doc',
'Web-Static-Content', 'Web-Directory-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
}
}
$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
}
+40
View File
@@ -0,0 +1,40 @@
# Norbourg lab — WS01 stage 2: local privileges and the workstation flag.
. "$PSScriptRoot\..\lib\common.ps1"
$self = $PSCommandPath
Write-LabLog 'ws01: post stage starting'
try {
Wait-ForDomain 600
# svc_backup becomes a local admin through this domain group.
try {
Add-LocalGroupMember -Group 'Administrators' -Member '@@NETBIOS@@\WS Admins' -ErrorAction Stop
Write-LabLog 'ws01: WS Admins added to local Administrators'
} catch {
Write-LabLog 'ws01: WS Admins already local admin'
}
$flagDir = 'C:\flag'
New-Item -ItemType Directory -Force -Path $flagDir | Out-Null
$userFlag = Join-Path $flagDir 'user.txt'
if (-not (Test-Path $userFlag)) {
Set-Content -Path $userFlag -Value 'NORBOURG{w0rkst4t10n_l4t3r4l_m0v3}' -Encoding ascii
}
$acl = Get-Acl $flagDir
$acl.SetAccessRuleProtection($true, $false)
foreach ($id in @('BUILTIN\Administrators', 'NT AUTHORITY\SYSTEM')) {
$rule = New-Object System.Security.AccessControl.FileSystemAccessRule(
$id, 'FullControl', 'ContainerInherit,ObjectInherit', 'None', 'Allow')
$acl.AddAccessRule($rule)
}
Set-Acl -Path $flagDir -AclObject $acl
Write-LabLog 'ws01: user.txt in place'
$null = schtasks.exe /Delete /TN 'NorbourgWsPost' /F
Complete-Stage $self 'ok'
} catch {
Fail-Stage $self $_.Exception.Message
}
+29
View File
@@ -0,0 +1,29 @@
# Norbourg lab — WS01: domain-joined workstation.
# Stage 2 runs after the join reboot because local group membership needs the
# domain group to resolve.
. "$PSScriptRoot\..\lib\common.ps1"
$self = $PSCommandPath
Write-LabLog 'ws01: starting'
try {
$post = Join-Path $PSScriptRoot 'ws01-post.ps1'
if (-not (Test-Path $post)) { Fail-Stage $self 'ws01-post.ps1 missing' }
$null = schtasks.exe /Create /TN 'NorbourgWsPost' /F /SC ONSTART /RU SYSTEM /RL HIGHEST `
/TR "powershell.exe -NoProfile -ExecutionPolicy Bypass -File `"$post`""
$cred = New-Object System.Management.Automation.PSCredential(
'@@DOMAIN@@\Administrator',
(ConvertTo-SecureString '@@ADMIN_PASSWORD@@' -AsPlainText -Force))
Write-LabLog 'ws01: 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
}