-
Notifications
You must be signed in to change notification settings - Fork 2
/
Test-ADPasswords.ps1
252 lines (197 loc) · 6.78 KB
/
Test-ADPasswords.ps1
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
<#
.SYNOPSIS
Securely test the quality of Active Directory Passwords against breached data lists
.NOTES
Script Name: Test-ADPasswords.ps1
Created By: Gavin Townsend
Date: October 2019
.DESCRIPTION
The script performs the follow actions:
- Creates an offline Active Directory database (ntds.dit file) and registry key
- Compares hashes against a custom 'bad password list' and breached password dataset (https://haveibeenpwned.com/Passwords)
- Generates a password quality report
- Optionally;
- Send an email to users explaining thier password was found in a breached data list
- Flags a password reset at next logon
.EXAMPLE
.\Test-ADPasswords.ps1
.REQUIREMENTS
1. Access and permissions to create an Active Directory backup
Typically on Domain Controller as a domain administrator. Ensure to give a heads-up to your security team!
2. Local copy of the Pwned Passwords list - https://haveibeenpwned.com/Passwords
Download "NTLM ordered by hash" and unzip locally (eg c:\audit\pwned-passwords-ntlm-ordered-by-hash-v5.txt)
Pwnded Passwords V6 Download file is around 8GB (20GB unzipped) and contains 570+ million hashes
3. DSInternals Framework - https://github.com/MichaelGrafnetter/DSInternals
If using PowerShell v5 simply run: Install-Module -Name DSInternals -Force
Else follow "Offline Module Distribution" steps via link above
4. Active Directory PowerShell plugin - https://docs.microsoft.com/en-us/powershell/module/addsadministration/?view=win10-ps
Optional if sending emails and forcing password reset
5. Ensure to update function variables as required
.VERSION HISTORY
1.0 Oct 2019 Gavin Townsend Original Build
1.1 Jun 2020 Gavin Townsend Update to reference Pwned Passwords V6
#>
#Location of HIBP dictionary file and working directory
$AuditPath = "c:\audit"
#Enable functions as required
$CreateNTDS = $TRUE
$TestPasswords = $TRUE
$SendEmail = $FALSE
$ResetPassword = $FALSE
#Functions
#---------
Function Write-Both {
Param ([string]$LogString)
$LogFile = "$AuditPath\Audit.log"
Add-content $Logfile -value $LogString
Write-Host $LogString
}
Write-Both "$(Get-Date) - New script run from $Env:Computername by $env:username"
Function Create-NTDS{
param(
[Parameter(Mandatory = $true)][System.IO.FileInfo] $AuditPath
)
Begin{
Write-Both "`t START: Function CreateNTDS"
}
process{
Try{
if ($CreateNTDS -eq $TRUE){
ntdsutil "activate instance ntds" ifm "create Full $AuditPath" q q
Write-Both "`t FINISH: Active Directory offline copy completed to $AuditPath"
}
else{
Write-Both "`t CreateNTDS = FALSE (function skipped)"
}
}
Catch{
Write-Both "ERROR: $($_.Exception.Message)"
break
}
}
end{}
}
Function Test-Passwords{
param(
[Parameter(Mandatory = $true)][System.IO.FileInfo] $AuditPath
)
Begin{
Write-Both "`t START: Function Test-Passwords"
}
process{
$Key = Get-BootKey -SystemHivePath "$AuditPath\registry\SYSTEM"
$DB = "$AuditPath\Active Directory\ntds.dit"
$Dictionary = "$AuditPath\pwned-passwords-ntlm-ordered-by-hash-v5.txt"
$Report = "$AuditPath\AD Password Quality Report $(get-date -f yyyy-MM-dd).txt"
$BadPasswordList = @("Winter2020","Spring2019","Summer2019","Autumn2020","Password123!","Password","12345678","Pa$$w0rd","qwertyuiop")
$OU = "*OU=Users,DC=MyDomain,DC=com" # <- Ensure to include the wildcard
Try{
if ($TestPasswords -eq $TRUE){
$Results = Get-ADDBAccount -All -DBPath $DB -BootKey $Key | `
where DistinguishedName -like $OU | `
Test-PasswordQuality -WeakPasswords $BadPasswordList -WeakPasswordHashesFile $Dictionary
$Results | Out-File $Report
Write-Both "`t FINISH: Report saved to $Report"
Invoke-Item $Report
}
else{
Write-Both "`t TestPasswords = FALSE (function skipped)"
}
}
Catch{
Write-Both "ERROR: $($_.Exception.Message)"
break
}
}
end{
Return $Results
}
}
Function Send-Email{
Param (
[string]$MailBody,
[string]$To
)
process{
$SMTPServer = "smtp.mydomain.com"
$From = "[email protected]"
Try{
if ($SendEmail -eq $TRUE){
Send-MailMessage -To $To -From $From -Subject "Password Audit" -SmtpServer $SMTPServer -body $MailBody -BodyAsHtml
Write-Both "`t Email sent to $To"
}
else{
Write-Both "`t Send Email = FALSE (email would have gone to $To)"
}
}
Catch{
Write-Both "ERROR: $($_.Exception.Message)"
break
}
}
end{}
}
Function Reset-Password{
param(
[string]$Account
)
process{
Try{
if ($ResetPassword -eq $TRUE){
Get-ADUser -Identity $Account | Set-ADUser CannotChangePassword:$FALSE -PasswordNeverExpires:$FALSE -ChangePasswordAtLogon:$TRUE
Write-Both "`t Password reset flagged for $Account"
}
else{
Write-Both "`t Reset Password = FALSE (password reset would have been flagged for $Account)"
}
}
Catch{
Write-Both "ERROR: $($_.Exception.Message)"
break
}
}
end{}
}
#Runtime
#-------
#AD Backup
Create-NTDS $AuditPath
#Password Audit
Test-Passwords $AuditPath
$BreachAccounts = $Results.WeakPassword
#Send email
Write-Both "`t START: Function Sending Emails"
$MailBody = "In accordance with recommended best practice, the Information Security Team audits the quality of our login passwords against breached data lists.</br>
During a recent audit, your computer login password was found in a blocklist or the <a href='https://haveibeenpwned.com/Passwords'>Pwned Passwords dataset</a>.</br>
</br>
<b>What does this mean?</b></br>
Passwords that are found in breached data are what we call 'known bad'. That is, these passwords have been exposed on the internet and represent a risk to those using them.</br>
</br>
Whilst it may be a coincidence, passwords are typically on the list because:</br>
- You have re-used the same password elsewhere (and that service was compromised in the past).</br>
- You are using a weak password which is commonly used by other people.</br>
</br>
<b>What do I need to do?</b></br>
Your password must be changed as soon as possible</br>
Make sure your password is long, unique and random. Here is a short (3min) video on some <a href='https://youtu.be/Nl-VA9w9cZk'>good and bad password habits</a>.</br>
</br>
NB. We do not know your password. The audit is conducted securely by our Information Security team, by comparing offline 'hash files'. </br>
If you have any questions, please contact your local IT Service Desk.</br>
</br>
Regards </br>
Information Security</br>"
foreach($Account in $BreachAccounts){
Try{
$ADUser = Get-ADUser -Identity $Account -Properties mail
$To = $ADUser.mail
Send-Email $MailBody $To
}
Catch{
Write-Both "ERROR: $($_.Exception.Message)"
}
}
#Reset Password
Write-Both "`t START: Function Reset-Password"
foreach($Account in $BreachAccounts){
Reset-Password $Account
}