|
| 1 | +# Requires -Version 7.0 |
| 2 | + |
| 3 | +<# |
| 4 | +.SYNOPSIS |
| 5 | + MSADPT_start2.ps1 - Initial setup script for the MSADPT tool. |
| 6 | + This script accepts all required input via command-line parameters, |
| 7 | + logs the initial environment, and discovers Domain Controllers and ADCS servers |
| 8 | + within the specified domain. It produces CSV outputs for environment details, |
| 9 | + discovered DCs, and discovered ADCS servers. |
| 10 | +
|
| 11 | +.DESCRIPTION |
| 12 | + This script is the entry point for the MSADPT (Microsoft AD Pen Test) tool. |
| 13 | + It performs the following steps: |
| 14 | + 1. Imports the MSADPT helper module for standardized logging. |
| 15 | + 2. Gathers and logs initial environmental information (date, computer name, IP config, domain info). |
| 16 | + 3. Verifies the presence of necessary PowerShell modules (e.g., ActiveDirectory). |
| 17 | + 4. Discovers all accessible Domain Controllers in the specified domain. |
| 18 | + 5. Discovers all accessible Active Directory Certificate Services (ADCS) servers. |
| 19 | + 6. Stores the gathered information into structured CSV files for use by subsequent scripts. |
| 20 | +
|
| 21 | +.PARAMETER Credential |
| 22 | + Domain credential to use for all Active Directory enumeration operations. |
| 23 | + Example: |
| 24 | + -Credential (Get-Credential) |
| 25 | +
|
| 26 | +.PARAMETER DomainFQDN |
| 27 | + Fully qualified domain name to enumerate. |
| 28 | + Example: |
| 29 | + -DomainFQDN "foo.net" |
| 30 | +
|
| 31 | +.PARAMETER EnvironmentOutputCsvPath |
| 32 | + Output CSV path for the environment details. |
| 33 | + Example: |
| 34 | + -EnvironmentOutputCsvPath "C:\temp\MSADPT_Output\MSADPT_Environment.csv" |
| 35 | +
|
| 36 | +.PARAMETER DCOutputCsvPath |
| 37 | + Output CSV path for the discovered Domain Controllers. |
| 38 | + Example: |
| 39 | + -DCOutputCsvPath "C:\temp\MSADPT_Output\MSADPT_DCs.csv" |
| 40 | +
|
| 41 | +.PARAMETER ADCSOutputCsvPath |
| 42 | + Output CSV path for the discovered ADCS servers. |
| 43 | + Example: |
| 44 | + -ADCSOutputCsvPath "C:\temp\MSADPT_Output\MSADPT_ADCS.csv" |
| 45 | +
|
| 46 | +.PARAMETER AdServer |
| 47 | + bootstrap Domain Controller / ADWS-capable server to use. |
| 48 | +
|
| 49 | +
|
| 50 | +.OUTPUTS |
| 51 | + - Environment CSV at the path specified by -EnvironmentOutputCsvPath |
| 52 | + - Domain Controllers CSV at the path specified by -DCOutputCsvPath |
| 53 | + - ADCS servers CSV at the path specified by -ADCSOutputCsvPath |
| 54 | +
|
| 55 | +.EXAMPLE |
| 56 | + .\MSADPT_start2.ps1 ` |
| 57 | + -Credential (Get-Credential) ` |
| 58 | + -DomainFQDN "foo.net" ` |
| 59 | + -AdServer "dc03.foo.net" ` |
| 60 | + -EnvironmentOutputCsvPath "C:\temp\MSADPT_Output\MSADPT_Environment.csv" ` |
| 61 | + -DCOutputCsvPath "C:\temp\MSADPT_Output\MSADPT_DCs.csv" ` |
| 62 | + -ADCSOutputCsvPath "C:\temp\MSADPT_Output\MSADPT_ADCS.csv" |
| 63 | +#> |
| 64 | + |
| 65 | +param( |
| 66 | + [Parameter(Mandatory)] |
| 67 | + [PSCredential]$Credential, |
| 68 | + |
| 69 | + [Parameter(Mandatory)] |
| 70 | + [string]$DomainFQDN, |
| 71 | + |
| 72 | + [Parameter(Mandatory)] |
| 73 | + [string]$EnvironmentOutputCsvPath, |
| 74 | + |
| 75 | + [Parameter(Mandatory)] |
| 76 | + [string]$DCOutputCsvPath, |
| 77 | + |
| 78 | + [Parameter(Mandatory)] |
| 79 | + [string]$ADCSOutputCsvPath, |
| 80 | + |
| 81 | + [Parameter(Mandatory)] |
| 82 | + [string]$AdServer |
| 83 | +) |
| 84 | + |
| 85 | +# --- Ensure parent folders exist for all output files --- |
| 86 | +$allOutputPaths = @( |
| 87 | + $EnvironmentOutputCsvPath, |
| 88 | + $DCOutputCsvPath, |
| 89 | + $ADCSOutputCsvPath |
| 90 | +) | Where-Object { -not [string]::IsNullOrWhiteSpace($_) } |
| 91 | + |
| 92 | +foreach ($path in $allOutputPaths) { |
| 93 | + $parent = Split-Path -Path $path -Parent |
| 94 | + if (-not [string]::IsNullOrWhiteSpace($parent) -and -not (Test-Path -LiteralPath $parent -PathType Container)) { |
| 95 | + New-Item -Path $parent -ItemType Directory -Force | Out-Null |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +# --- Import helper module --- |
| 100 | +$helpersModulePath = Join-Path $PSScriptRoot 'MSADPT.Helpers.psm1' |
| 101 | + |
| 102 | +if (-not (Test-Path -LiteralPath $helpersModulePath -PathType Leaf)) { |
| 103 | + Write-Error "Required helper module not found at '$helpersModulePath'. Aborting." |
| 104 | + exit 1 |
| 105 | +} |
| 106 | + |
| 107 | +Import-Module $helpersModulePath -Force -ErrorAction Stop |
| 108 | + |
| 109 | +$rootDSE = Test-MSADPTADConnectivity -Credential $Credential -AdServer $AdServer |
| 110 | + |
| 111 | +if (-not $rootDSE) { |
| 112 | + Write-MSADPTLog -Message "Cannot continue because AD connectivity pre-flight failed." -Level 'ERROR' |
| 113 | + exit 1 |
| 114 | +} |
| 115 | + |
| 116 | +Write-MSADPTLog -Message "MSADPT_start.ps1 started. Beginning initial environment and AD/ADCS discovery." -Level 'INFO' |
| 117 | +Write-MSADPTLog -Message "DomainFQDN: $DomainFQDN" -Level 'INFO' |
| 118 | +Write-MSADPTLog -Message "Environment output CSV: $EnvironmentOutputCsvPath" -Level 'INFO' |
| 119 | +Write-MSADPTLog -Message "DC output CSV: $DCOutputCsvPath" -Level 'INFO' |
| 120 | +Write-MSADPTLog -Message "ADCS output CSV: $ADCSOutputCsvPath" -Level 'INFO' |
| 121 | + |
| 122 | +# --- Module and tool pre-checks --- |
| 123 | +Write-MSADPTLog -Message "Checking required PowerShell modules and tools." -Level 'INFO' |
| 124 | + |
| 125 | +$RequiredModules = @('ActiveDirectory') |
| 126 | +foreach ($module in $RequiredModules) { |
| 127 | + if (-not (Get-Module -ListAvailable -Name $module)) { |
| 128 | + Write-MSADPTLog -Message "Required PowerShell module '$module' is not available. Aborting." -Level 'ERROR' |
| 129 | + exit 1 |
| 130 | + } |
| 131 | + |
| 132 | + if (-not (Get-Module -Name $module)) { |
| 133 | + Write-MSADPTLog -Message "Importing PowerShell module: $module" -Level 'INFO' |
| 134 | + Import-Module $module -ErrorAction Stop |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +$RequiredCommands = @('nltest', 'net', 'wmic', 'dsregcmd', 'gpresult') |
| 139 | +foreach ($cmd in $RequiredCommands) { |
| 140 | + if (-not (Get-Command -Name $cmd -ErrorAction SilentlyContinue)) { |
| 141 | + Write-MSADPTLog -Message "Required command-line tool '$cmd' was not found in PATH. Aborting." -Level 'ERROR' |
| 142 | + exit 1 |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +Write-MSADPTLog -Message "All required modules and tools are available." -Level 'INFO' |
| 147 | + |
| 148 | +# --- Determine bootstrap AD server --- |
| 149 | +$BootstrapServer = $null |
| 150 | + |
| 151 | +try { |
| 152 | + if (-not [string]::IsNullOrWhiteSpace($AdServer)) { |
| 153 | + $BootstrapServer = $AdServer |
| 154 | + Write-MSADPTLog -Message "Using operator-supplied AD server '$BootstrapServer' as bootstrap server." -Level 'INFO' |
| 155 | + } |
| 156 | + else { |
| 157 | + $BootstrapServer = (Get-ADDomainController ` |
| 158 | + -Discover ` |
| 159 | + -ForceDiscover ` |
| 160 | + -DomainName $DomainFQDN ` |
| 161 | + -Service ADWS ` |
| 162 | + -Credential $Credential ` |
| 163 | + -ErrorAction Stop).HostName |
| 164 | + |
| 165 | + Write-MSADPTLog -Message "Discovered ADWS-capable Domain Controller '$BootstrapServer' for bootstrap operations." -Level 'WARNING' |
| 166 | + } |
| 167 | +} |
| 168 | +catch { |
| 169 | + Write-MSADPTLog -Message "Failed to determine a bootstrap AD server for domain '$DomainFQDN': $($_.Exception.Message)" -Level 'ERROR' |
| 170 | + exit 1 |
| 171 | +} |
| 172 | + |
| 173 | +# --- Environment Logging --- |
| 174 | +Write-MSADPTLog -Message "Gathering initial environmental information." -Level 'INFO' |
| 175 | + |
| 176 | +<# When a command inside that object construction fails badly enough, the whole assignment can fail to |
| 177 | +produce a usable object, leaving $EnvironmentInfo as $null. |
| 178 | +So If you cannot connect to the DC, say over a VPN, this is normal... #> |
| 179 | + |
| 180 | +$computerInfo = Get-ComputerInfo |
| 181 | +$ipConfigText = (Get-NetIPAddress | Select-Object IPAddress, InterfaceAlias, AddressFamily | Out-String).Trim() |
| 182 | + |
| 183 | +$EnvironmentInfo = [PSCustomObject]@{ |
| 184 | + Timestamp = (Get-Date).ToString("yyyy-MM-dd HH:mm:ss") |
| 185 | + Username = $Credential.UserName |
| 186 | + LocalComputerName = $computerInfo.CsName |
| 187 | + OperatingSystem = $computerInfo.OsDisplayVersion |
| 188 | + PowerShellVersion = $PSVersionTable.PSVersion.ToString() |
| 189 | + DomainName = $(try { (Get-ADDomain -Server $BootstrapServer -Credential $Credential -ErrorAction Stop).NetBIOSName } catch { $null }) |
| 190 | + DomainFQDN = $DomainFQDN |
| 191 | + BootstrapServer = $BootstrapServer |
| 192 | + IPConfiguration = $ipConfigText |
| 193 | + IsHybridJoined = (dsregcmd /status | Select-String "AzureAdJoined").ToString().Contains("YES") |
| 194 | + LocalAdminCheck = $(try { ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) } catch { $false }) |
| 195 | +} |
| 196 | + |
| 197 | +if ($null -ne $EnvironmentInfo) { |
| 198 | + $EnvironmentInfo | Export-Csv -Path $EnvironmentOutputCsvPath -NoTypeInformation -Force -ErrorAction Stop |
| 199 | + Write-MSADPTLog -Message "Initial environment information saved to '$EnvironmentOutputCsvPath'." -Level 'WARNING' |
| 200 | +} |
| 201 | +else { |
| 202 | + Write-MSADPTLog -Message "Environment information object was null; environment CSV export skipped." -Level 'ERROR' |
| 203 | + exit 1 |
| 204 | +} |
| 205 | + |
| 206 | +# --- Discover Domain Controllers --- |
| 207 | +<# If this fails, the DC cannot be infered, likely the machine is not domain joined. |
| 208 | +Easy workaround, add a domainFQN entry in the .config file. #> |
| 209 | +Write-MSADPTLog -Message "Discovering Domain Controllers in '$DomainFQDN'." -Level 'INFO' |
| 210 | + |
| 211 | +$DCs = @() |
| 212 | +try { |
| 213 | + $DCs = Get-ADDomainController ` |
| 214 | + -Filter * ` |
| 215 | + -Server $BootstrapServer ` |
| 216 | + -Credential $Credential ` |
| 217 | + -ErrorAction Stop | |
| 218 | + Select-Object Name, HostName, IPv4Address, IsGlobalCatalog, IsReadOnly, Site, OperatingSystem, Forest, Domain |
| 219 | + |
| 220 | + if (@($DCs).Count -gt 0) { |
| 221 | + $DCs | Export-Csv -Path $DCOutputCsvPath -NoTypeInformation -Force -ErrorAction Stop |
| 222 | + Write-MSADPTLog -Message "Discovered $(@($DCs).Count) Domain Controller(s). Details saved to '$DCOutputCsvPath'." -Level 'WARNING' |
| 223 | + |
| 224 | + $DCs | ForEach-Object { |
| 225 | + Write-MSADPTLog -Message " - $($_.Name) ($($_.IPv4Address))" -Level 'INFO' |
| 226 | + } |
| 227 | + } |
| 228 | + else { |
| 229 | + Write-MSADPTLog -Message "No Domain Controllers were returned for '$DomainFQDN'." -Level 'WARNING' |
| 230 | + } |
| 231 | +} |
| 232 | +catch { |
| 233 | + Write-MSADPTLog -Message "Failed to discover Domain Controllers: $($_.Exception.Message)" -Level 'ERROR' |
| 234 | + exit 1 |
| 235 | +} |
| 236 | + |
| 237 | +# --- Discover Active Directory Certificate Services (ADCS) Servers --- |
| 238 | +Write-MSADPTLog -Message "Discovering Active Directory Certificate Services (ADCS) servers by querying pKIEnrollmentService objects." -Level 'INFO' |
| 239 | + |
| 240 | +$AllADCSservers = @() |
| 241 | + |
| 242 | +try { |
| 243 | + foreach ($DC in $DCs) { |
| 244 | + $DCServer = if (-not [string]::IsNullOrWhiteSpace($DC.HostName)) { $DC.HostName } else { $DC.Name } |
| 245 | + |
| 246 | + if ([string]::IsNullOrWhiteSpace($DCServer)) { |
| 247 | + Write-MSADPTLog -Message "Skipping a DC entry because neither HostName nor Name is populated." -Level 'WARNING' |
| 248 | + continue |
| 249 | + } |
| 250 | + |
| 251 | + try { |
| 252 | + Write-MSADPTLog -Message "Querying DC '$DCServer' for ADCS Enterprise CA objects." -Level 'INFO' |
| 253 | + |
| 254 | + $rootDSE = Get-ADRootDSE -Server $DCServer -Credential $Credential -ErrorAction Stop |
| 255 | + $searchBase = "CN=Enrollment Services,CN=Public Key Services,CN=Services,$($rootDSE.configurationNamingContext)" |
| 256 | + |
| 257 | + Write-MSADPTLog -Message "ADCS search base for '$DCServer': $searchBase" -Level 'INFO' |
| 258 | + |
| 259 | + $CurrentADCS = Get-ADObject ` |
| 260 | + -LDAPFilter "(objectClass=pKIEnrollmentService)" ` |
| 261 | + -SearchBase $searchBase ` |
| 262 | + -SearchScope OneLevel ` |
| 263 | + -Properties objectGUID, cn, dNSHostName, displayName, description, certificateTemplates, cACertificate ` |
| 264 | + -Server $DCServer ` |
| 265 | + -Credential $Credential ` |
| 266 | + -ErrorAction Stop | |
| 267 | + Select-Object ` |
| 268 | + Name, |
| 269 | + objectGUID, |
| 270 | + cn, |
| 271 | + dNSHostName, |
| 272 | + displayName, |
| 273 | + description, |
| 274 | + certificateTemplates, |
| 275 | + @{Name='ServerRole';Expression={'Enterprise CA'}}, |
| 276 | + @{Name='DiscoveredFromDC';Expression={$DCServer}} |
| 277 | + |
| 278 | + if (@($CurrentADCS).Count -gt 0) { |
| 279 | + Write-MSADPTLog -Message "DC '$DCServer' returned $(@($CurrentADCS).Count) ADCS Enterprise CA object(s)." -Level 'INFO' |
| 280 | + $AllADCSservers += $CurrentADCS |
| 281 | + } |
| 282 | + else { |
| 283 | + Write-MSADPTLog -Message "DC '$DCServer' returned no ADCS Enterprise CA objects." -Level 'WARNING' |
| 284 | + } |
| 285 | + } |
| 286 | + catch { |
| 287 | + Write-MSADPTLog -Message "Failed querying DC '$DCServer' for ADCS objects: $($_.Exception.Message)" -Level 'ERROR' |
| 288 | + } |
| 289 | + } |
| 290 | + |
| 291 | + $UniqueADCSservers = $AllADCSservers | |
| 292 | + Group-Object { |
| 293 | + if ($_.objectGUID) { |
| 294 | + $_.objectGUID.Guid |
| 295 | + } |
| 296 | + elseif (-not [string]::IsNullOrWhiteSpace($_.dNSHostName)) { |
| 297 | + $_.dNSHostName.ToLowerInvariant() |
| 298 | + } |
| 299 | + elseif (-not [string]::IsNullOrWhiteSpace($_.cn)) { |
| 300 | + $_.cn.ToLowerInvariant() |
| 301 | + } |
| 302 | + else { |
| 303 | + $_.Name.ToLowerInvariant() |
| 304 | + } |
| 305 | + } | |
| 306 | + ForEach-Object { $_.Group[0] } | |
| 307 | + Sort-Object dNSHostName, cn |
| 308 | + |
| 309 | + if (@($UniqueADCSservers).Count -gt 0) { |
| 310 | + $UniqueADCSservers | |
| 311 | + Select-Object Name, cn, dNSHostName, displayName, description, certificateTemplates, ServerRole | |
| 312 | + Export-Csv -Path $ADCSOutputCsvPath -NoTypeInformation -Force -ErrorAction Stop |
| 313 | + |
| 314 | + Write-MSADPTLog -Message "Discovered $(@($UniqueADCSservers).Count) unique ADCS server(s). Details saved to '$ADCSOutputCsvPath'." -Level 'WARNING' |
| 315 | + |
| 316 | + $UniqueADCSservers | ForEach-Object { |
| 317 | + $hostLabel = if (-not [string]::IsNullOrWhiteSpace($_.dNSHostName)) { $_.dNSHostName } else { $_.cn } |
| 318 | + Write-MSADPTLog -Message " - $hostLabel (Role: $($_.ServerRole))" -Level 'INFO' |
| 319 | + } |
| 320 | + } |
| 321 | + else { |
| 322 | + Write-MSADPTLog -Message "No ADCS servers were found after querying all discovered DCs." -Level 'WARNING' |
| 323 | + } |
| 324 | +} |
| 325 | +catch { |
| 326 | + Write-MSADPTLog -Message "Failed during ADCS discovery: $($_.Exception.Message)" -Level 'ERROR' |
| 327 | + exit 1 |
| 328 | +} |
| 329 | + |
| 330 | +Write-MSADPTLog -Message "MSADPT_start.ps1 completed successfully." -Level 'INFO' |
| 331 | +Write-MSADPTLog -Message "The collected data can now be used as input for subsequent analysis scripts." -Level 'INFO' |
0 commit comments