-
Notifications
You must be signed in to change notification settings - Fork 215
/
Find-Fruit.ps1
585 lines (461 loc) · 19.3 KB
/
Find-Fruit.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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
function Invoke-ThreadedFunction
{
[CmdletBinding()]
param (
[Parameter(Position = 0, Mandatory = $false)]
[String[]]$ComputerName,
[String[]]$VulnLinks,
[Parameter(Position = 1, Mandatory = $True)]
[System.Management.Automation.ScriptBlock]$ScriptBlock,
[Parameter(Position = 2)]
[Hashtable]$ScriptParameters,
[Int]$Threads = 20,
[Int]$Timeout = 100,
[Int]$Hostcount
)
begin
{
if ($PSBoundParameters['Debug'])
{
$DebugPreference = 'Continue'
}
if ($ComputerName)
{
Write-Verbose "[*] Total number of hosts: $($ComputerName.count)"
}
elseif ($VulnLinks)
{
Write-Verbose "[*] Total number of URL's: $($VulnLinks.count*$Hostcount)"
}
# Adapted from:
# http://powershell.org/wp/forums/topic/invpke-parallel-need-help-to-clone-the-current-runspace/
$SessionState = [System.Management.Automation.Runspaces.InitialSessionState]::CreateDefault()
$SessionState.ApartmentState = [System.Threading.Thread]::CurrentThread.GetApartmentState()
# threading adapted from
# https://github.com/darkoperator/Posh-SecMod/blob/master/Discovery/Discovery.psm1#L407
# Thanks Carlos!
# create a pool of maxThread runspaces
$Pool = [runspacefactory]::CreateRunspacePool(1, $Threads, $SessionState, $Host)
$Pool.Open()
$Jobs = @()
$PS = @()
$Wait = @()
$Counter = 0
}
process
{
if ($ComputerName)
{
ForEach ($Computer in $ComputerName)
{
# make sure we get a server name
if ($Computer -ne '')
{
While ($($Pool.GetAvailableRunspaces()) -le 0)
{
Start-Sleep -MilliSeconds $Timeout
}
# create a "powershell pipeline runner"
$PS += [powershell]::create()
$PS[$Counter].runspacepool = $Pool
# add the script block + arguments
$Null = $PS[$Counter].AddScript($ScriptBlock).AddParameter('ComputerName', $Computer)
if ($ScriptParameters)
{
ForEach ($Param in $ScriptParameters.GetEnumerator())
{
$Null = $PS[$Counter].AddParameter($Param.Name, $Param.Value)
}
}
# start job
$Jobs += $PS[$Counter].BeginInvoke();
# store wait handles for WaitForAll call
$Wait += $Jobs[$Counter].AsyncWaitHandle
}
$Counter = $Counter + 1
}
}
elseif ($VulnLinks)
{
ForEach ($testlink in $VulnLinks)
{
# make sure we get a server name
if ($testlink -ne '')
{
While ($($Pool.GetAvailableRunspaces()) -le 0)
{
Start-Sleep -MilliSeconds $Timeout
}
# create a "powershell pipeline runner"
$PS += [powershell]::create()
$PS[$Counter].runspacepool = $Pool
# add the script block + arguments
$Null = $PS[$Counter].AddScript($ScriptBlock).AddParameter('VulnLinks', $testlink)
if ($ScriptParameters)
{
ForEach ($Param in $ScriptParameters.GetEnumerator())
{
$Null = $PS[$Counter].AddParameter($Param.Name, $Param.Value)
}
}
# start job
$Jobs += $PS[$Counter].BeginInvoke();
# store wait handles for WaitForAll call
$Wait += $Jobs[$Counter].AsyncWaitHandle
}
$Counter = $Counter + 1
}
}
}
end
{
Write-Verbose "Waiting for scanning threads to finish..."
$WaitTimeout = Get-Date
# set a 60 second timeout for the scanning threads
while ($($Jobs | Where-Object { $_.IsCompleted -eq $False }).count -gt 0 -or $($($(Get-Date) - $WaitTimeout).totalSeconds) -gt 60)
{
Start-Sleep -MilliSeconds $Timeout
}
# end async call
for ($y = 0; $y -lt $Counter; $y++)
{
try
{
# complete async job
$PS[$y].EndInvoke($Jobs[$y])
}
catch
{
Write-Warning "error: $_"
}
finally
{
$PS[$y].Dispose()
}
}
$Pool.Dispose()
Write-Verbose "All threads completed!"
}
}
function Find-Fruit
{
<#
.SYNOPSIS
Search for "low hanging fruit".
.DESCRIPTION
A script to find potentially easily exploitable web servers on a target network.
.PARAMETER Rhosts
Targets in CIDR or comma separated format.
.PARAMETER Port
Specifies the port to connect to.
.PARAMETER Path
Path to custom dictionary.
.PARAMETER Timeout
Timeout for each connection in milliseconds.
.PARAMETER UseSSL
Use an SSL connection.
.PARAMETER Threads
The maximum concurrent threads to execute..
.PARAMETER WebProxy
Specify an http proxy
.PARAMETER ProxyPort
Specify the http proxy port
.EXAMPLE
C:\PS> Find-Fruit -Rhosts 192.168.1.0/24 -Port 8080
C:\PS> Find-Fruit -Rhosts 192.168.1.0/24 -Path dictionary.txt -Port 8443 -UseSSL
C:\PS> Find-Fruit -Rhosts 192.168.1.0/24 -Port 8080 -WebProxy 127.0.0.1 -ProxyPort 8080
.NOTES
Credits to mattifestation for Get-HttpStatus
HTTP Status Codes: 100 - Informational * 200 - Success * 300 - Redirection * 400 - Client Error * 500 - Server Error
#>
[CmdletBinding()]
param (
[Parameter(Mandatory = $True)]
[String]$Rhosts,
[Parameter(Mandatory=$False)]
[Int]$Port,
[Parameter(Mandatory=$False)]
[String]$Path,
[Parameter(Mandatory=$False)]
[Int]$Timeout = 110,
[Parameter(Mandatory=$False)]
[Switch]$UseSSL,
[Parameter(Mandatory=$False)]
[ValidateRange(1, 100)]
[Int]$Threads,
[Parameter(Mandatory=$False)]
[String]$WebProxy,
[Parameter(Mandatory=$False)]
[String]$ProxyPort
)
begin {
$hostList = New-Object System.Collections.ArrayList
$iHosts = $Rhosts -split ","
foreach ($iHost in $iHosts) {
$iHost = $iHost.Replace(" ", "")
if (!$iHost) {
continue
}
if ($iHost.contains("/")) {
$netPart = $iHost.split("/")[0]
[uint32]$maskPart = $iHost.split("/")[1]
$address = [System.Net.IPAddress]::Parse($netPart)
if ($maskPart -ge $address.GetAddressBytes().Length * 8) {
throw "Bad host mask"
}
$numhosts = [System.math]::Pow(2, (($address.GetAddressBytes().Length * 8) - $maskPart))
$startaddress = $address.GetAddressBytes()
[array]::Reverse($startaddress)
$startaddress = [System.BitConverter]::ToUInt32($startaddress, 0)
[uint32]$startMask = ([System.math]::Pow(2, $maskPart) - 1) * ([System.Math]::Pow(2, (32 - $maskPart)))
$startAddress = $startAddress -band $startMask
#in powershell 2.0 there are 4 0 bytes padded, so the [0..3] is necessary
$startAddress = [System.BitConverter]::GetBytes($startaddress)[0..3]
[array]::Reverse($startaddress)
$address = [System.Net.IPAddress][byte[]]$startAddress
$Null = $hostList.Add($address.IPAddressToString)
for ($i = 0; $i -lt $numhosts - 1; $i++) {
$nextAddress = $address.GetAddressBytes()
[array]::Reverse($nextAddress)
$nextAddress = [System.BitConverter]::ToUInt32($nextAddress, 0)
$nextAddress++
$nextAddress = [System.BitConverter]::GetBytes($nextAddress)[0..3]
[array]::Reverse($nextAddress)
$address = [System.Net.IPAddress][byte[]]$nextAddress
$Null = $hostList.Add($address.IPAddressToString)
}
}
else {
$Null = $hostList.Add($iHost)
}
}
$HostEnumBlock = {
param($ComputerName, $UseSSL, $Port, $Path, $Timeout)
if ($UseSSL -and $Port -eq 0) {
# Default to 443 if SSL is specified but no port is specified
$Port = 443
}
elseif ($Port -eq 0) {
# Default to port 80 if no port is specified
$Port = 80
}
if ($UseSSL) {
$SSL = 's'
# Ignore invalid SSL certificates
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = { $True }
}
else {
$SSL = ''
}
if (($Port -eq 80) -or ($Port -eq 443)) {
$PortNum = ''
}
else {
$PortNum = ":$Port"
}
if ($Path) {
if (!(Test-Path -Path $Path)) { Throw "File doesnt exist" }
$VulnLinks = @()
foreach ($Link in Get-Content $Path) {
$VulnLinks = $VulnLinks + $Link
}
}
else {
$VulnLinks = @()
$VulnLinks = $VulnLinks + "jmx-console/" # Jboss
$VulnLinks = $VulnLinks + "web-console/ServerInfo.jsp" # Jboss
$VulnLinks = $VulnLinks + "invoker/JMXInvokerServlet" # Jboss
$VulnLinks = $VulnLinks + "system/console" # OSGi console
$VulnLinks = $VulnLinks + "axis2/axis2-admin/" # Apache Axis2
$VulnLinks = $VulnLinks + "manager/html/" # Tomcat
$VulnLinks = $VulnLinks + "tomcat/manager/html/" # Tomcat
$VulnLinks = $VulnLinks + "wp-admin" # Wordpress
$VulnLinks = $VulnLinks + "workorder/FileDownload.jsp" #Manage Engine
$VulnLinks = $VulnLinks + "ibm/console/logon.jsp?action=OK" # WebSphere
$VulnLinks = $VulnLinks + "data/login" # Dell iDrac
$VulnLinks = $VulnLinks + "script/" # Jenkins Script Conosle
$VulnLinks = $VulnLinks + "opennms/" # OpenNMS
$VulnLinks = $VulnLinks + "RDWeb/Pages/en-US/Default.aspx" #RDS Remote Desktop
$VulnLinks = $VulnLinks + "securityRealm/user/admin/" #Jenkins Vuln Check
}
# Check Http status for each entry in the host
foreach ($Target in $ComputerName) {
foreach ($Item in $Vulnlinks) {
$WebTarget = "http$($SSL)://$($Target)$($PortNum)/$($Item)"
$URI = New-Object Uri($WebTarget)
try {
$WebRequest = [System.Net.WebRequest]::Create($URI)
$WebRequest.Headers.Add('UserAgent', $UserAgent)
$ProxyAddress = New-Object System.Net.WebProxy("http://$($WebProxy):$($ProxyPort)",$true)
$WebRequest.Proxy = $ProxyAddress
$WebResponse = $WebRequest.Timeout = $Timeout
$WebResponse = $WebRequest.GetResponse()
$WebStatus = $WebResponse.StatusCode
$ResultObject += $ScanObject
$WebResponse.Close()
}
catch {
$WebStatus = $Error[0].Exception.InnerException.Response.StatusCode
if ($WebStatus -eq $null) {
# Not every exception returns a StatusCode.
# If that is the case, return the Status.
$WebStatus = $Error[0].Exception.InnerException.Status
}
}
$Result = @{
Status = $WebStatus;
URL = $WebTarget
}
New-Object -TypeName PSObject -Property $Result | Where-Object {$_.Status -eq 'OK'}
}
}
}
}
process {
if($Threads) {
Write-Verbose "Using threading with threads = $Threads"
# if we're using threading, kick off the script block with Invoke-ThreadedFunction
$ScriptParams = @{
'UseSSL' = $UseSSL
'Port' = $Port
'Path' = $Path
'Timeout' = $Timeout
'UserAgent' = $UserAgent
'WebProxy' = $WebProxy
'ProxyPort' = $ProxyPort
}
# kick off the threaded script block + arguments
Invoke-ThreadedFunction -ComputerName $hostList -ScriptBlock $HostEnumBlock -ScriptParameters $ScriptParams
}
else {
Invoke-Command -ScriptBlock $HostEnumBlock -ArgumentList $HostList, $UseSSL, $Port, $Path, $Timeout, $UserAgent
}
}
}
function Brute-Fruit
{
<#
.SYNOPSIS
Search for web directories and files at scale across multiple web servers. Think "Dirbusting across a broad range of hosts".
.DESCRIPTION
A script to find directories and files across multiple web servers.
.PARAMETER Dictionary
Path to custom dictionary of files or directories.
Here's a good place to start:
https://github.com/danielmiessler/SecLists/tree/master/Discovery/Web-Content
https://github.com/DanMcInerney/pentest-machine/blob/master/wordlists/dirs-files-6000.list
.PARAMATER UrlList
List of URL's to scan. These should be one per line in the format of "http://domain.com", "https://domain.com:8443", etc...
.PARAMETER Timeout
Timeout for each connection in milliseconds.
.PARAMETER Threads
The maximum concurrent threads to execute.
.PARAMETER FoundOnly
Only display found URI's
.EXAMPLE
C:\PS> Brute-Fruit -Dictionary C:\temp\dictionary-of-files-to-test.txt -UrlList C:\temp\list-of-hosts.txt -Timeout 3000
C:\PS> Brute-Fruit -Dictionary C:\temp\dictionary-of-files-to-test.txt -UrlList C:\temp\list-of-hosts.txt -Timeout 3000 -Threads 10 -FoundOnly -Verbose
.NOTES
Credits to mattifestation for Get-HttpStatus
HTTP Status Codes: 100 - Informational * 200 - Success * 300 - Redirection * 400 - Client Error * 500 - Server Error
#>
[CmdletBinding()]
param (
[Parameter(Mandatory = $false)]
[String]$Dictionary,
[Parameter(Mandatory=$False)]
[Int]$Timeout = 110,
[Parameter(Mandatory=$False)]
[ValidateRange(1, 100)]
[Int]$Threads,
[Parameter(Mandatory=$False)]
[Switch]$FoundOnly,
[Parameter(Mandatory=$False)]
[String]$UrlList
)
begin
{
if (!(Test-Path -Path $UrlList)) { Throw "File doesn't exist" }
$hostlist = @()
$hostlist
foreach ($hostobject in Get-Content $UrlList)
{
$hostlist += $hostobject
}
if (!(Test-Path -Path $Dictionary)) { Throw "Dictionary file doesn't exist" }
$VulnLinks = @()
foreach ($Link in Get-Content $Dictionary)
{
$VulnLinks = $VulnLinks + $Link
}
$HostEnumBlock = {
param($ComputerName, $Dictionary, $Timeout, $FoundOnly, $VulnLinks)
foreach ($Item in $Vulnlinks)
{
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = { $True }
foreach ($Target in $ComputerName)
{
$WebTarget = "$Target/$Item"
$URI = New-Object Uri($WebTarget)
try
{
$WebRequest = [System.Net.WebRequest]::Create($URI)
$ProxyAddress = New-Object System.Net.WebProxy("http://$($WebProxy):$($ProxyPort)",$true)
$WebRequest.Proxy = $ProxyAddress
$WebResponse = $WebRequest.Timeout = $Timeout
$WebResponse = $WebRequest.GetResponse()
$WebStatus = $WebResponse.StatusCode
$Stream = $WebResponse.GetResponseStream()
$Reader = New-Object IO.StreamReader($Stream)
$html = $reader.ReadToEnd()
$WebSize = $html.Length
$ResultObject += $ScanObject
$WebResponse.Close()
}
catch
{
$WebStatus = $Error[0].Exception.InnerException.Response.StatusCode
if ($WebStatus -eq $null)
{
# Not every exception returns a StatusCode.
# If that is the case, return the Status.
$WebStatus = $Error[0].Exception.InnerException.Status
}
}
$Result = @{
Status = $WebStatus;
URL = $WebTarget
Size = $WebSize
}
if ($FoundOnly) {
New-Object -TypeName PSObject -Property $Result | Where-Object {$_.Status -eq 'OK'}
} else {
New-Object -TypeName PSObject -Property $Result
}
}
}
}
}
process {
if($Threads) {
Write-Verbose "Using threading with threads = $Threads"
# if we're using threading, kick off the script block with Invoke-ThreadedFunction
$ScriptParams = @{
'UseSSL' = $UseSSL
'Port' = $Port
'Dictionary' = $Dictionary
'Timeout' = $Timeout
'FoundOnly' = $FoundOnly
'ComputerName' = $hostlist
'WebProxy' = $WebProxy
'ProxyPort' = $ProxyPort
}
# kick off the threaded script block + arguments
$Hostcount = $hostlist.count
Invoke-ThreadedFunction -VulnLinks $VulnLinks -HostCount $Hostcount -ScriptBlock $HostEnumBlock -ScriptParameters $ScriptParams
}
else {
Invoke-Command -ScriptBlock $HostEnumBlock -ArgumentList $HostList, $Dictionary, $Timeout, $FoundOnly, $VulnLinks
}
}
}