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