-
Notifications
You must be signed in to change notification settings - Fork 92
/
Copy pathTest-Port.ps1
213 lines (145 loc) · 6.77 KB
/
Test-Port.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
Function Test-Port {
<#
.SYNOPSIS
This cmdlet is used to test for general TCP and UDP ports being open. Ports using SSL/TLS seem to require more error handling I have not added yet
.DESCRIPTION
Test to see if a TCP or UDP port is open. This may need some extra work performed when a remote port is using SSL/TLS
.PARAMETER ComputerName
Define the remote device(s) by IP address, hostname, or FQDN you want to test the port on
.PARAMETER Port
Define the port to check connectivity on
.PARAMETER Protocol
Define whether you are testing TCP or UDP. TCP is the default
.PARAMETER TimeoutMilliseconds
Define the timeout in milliseconds to wait for a response
.EXAMPLE
Test-Port -ComputerName dc.osbornepro.com -Port 389
# This example tests to see if port 389/tcp is open on dc.osbornepro.com with a 100 millisecond timeout
.EXAMPLE
Test-Port -ComputerName dc.osbornepro.com -Port 389 -Protocol TCP -TimeoutMilliseconds 100
# This example tests to see if port 389/tcp is open on dc.osbornepro.com with a 100 millisecond timeout
.EXAMPLE
Test-Port -ComputerName dc.osbornepro.com -Port 123 -Protocol UDP -TimeoutMilliseconds 1000
# This example checks to see if UDP port 123 is open on dc.osbornepro.com with a 1000 millisecond timeout
.NOTES
Author: Robert H. Osborne
Alias: tobor
Contact: [email protected]
.LINK
https://osbornepro.com
https://writeups.osbornepro.com
https://encrypit.osbornepro.com
https://btpssecpack.osbornepro.com
https://github.com/tobor88
https://github.com/OsbornePro
https://gitlab.com/tobor88
https://www.powershellgallery.com/profiles/tobor
https://www.linkedin.com/in/roberthosborne/
https://www.credly.com/users/roberthosborne/badges
https://www.hackthebox.eu/profile/52286
.INPUTS
System.String[]
.OUTPUTS
PSCustomObject
#>
param(
[Parameter(
Position=0,
Mandatory=$True,
ValueFromPipeline=$True,
ValueFromPipelineByPropertyName=$False)] # End Parameter
[String[]]$ComputerName,
[Parameter(
Position=1,
Mandatory=$True,
ValueFromPipeline=$False,
ValueFromPipelineByPropertyName=$False
)] # End Parameter
[ValidateRange(1, 65535)]
[Int]$Port,
[Parameter(
Position=2,
Mandatory=$False,
ValueFromPipeline=$False,
ValueFromPipelineByPropertyName=$False
)] # End Parameter
[ValidateSet("TCP", "UDP")]
[String]$Protocol = "TCP",
[Parameter(
Position=3,
Mandatory=$False,
ValueFromPipeline=$False,
ValueFromPipelineByPropertyName=$False
)] # End Parameter
[ValidateRange(10, 99999)]
[Int]$TimeoutMilliseconds = 100
) # End param
BEGIN {
$Output = @()
} PROCESS {
ForEach ($C in $ComputerName) {
Switch ($Protocol) {
"TCP" {
Write-Verbose -Message "TCP Protocol will be used"
$TcpClient = New-Object -TypeName System.Net.Sockets.TcpClient
$Connect = $TcpClient.BeginConnect($C, $Port, $Null, $Null)
Start-Sleep -Milliseconds $TimeoutMilliseconds
If ($TcpClient.Connected) {
$PortOpen = "True"
} Else {
$PortOpen = "False"
} # End If Else
Write-Verbose -Message "Closing connection to $C on port $Port"
$TcpClient.Close()
$Output += New-Object -TypeName PSCustomObject -Property @{ComputerName=$C;Port=$Port;Protocol=$Protocol;PortOpen=$PortOpen}
} # End Switch TCP
"UDP" {
$ErrorActionPreference = "SilentlyContinue"
Write-Verbose -Message "UDP Protocol will be used"
Write-Verbose -Message "Opening UDP Connection to $C"
$SourcePort = Get-Random -Maximum 50000 -Minimum 11000
$UdpClient = New-Object -TypeName System.Net.Sockets.Udpclient($SourcePort)
$UdpClient.Client.ReceiveTimeout = $TimeoutMilliseconds
$Connect = $UdpClient.Connect($C, $Port)
Write-Verbose -Message "Attempting to catch returned UDP data"
$AsciiText = New-Object -TypeName System.Text.AsciiEncoding
$Byte = $AsciiText.GetBytes("$(Get-Date)")
[Void]$UdpClient.Send($Byte, $Byte.Length)
$RemoteEndpoint = New-Object -TypeName System.Net.IpEndpoint([System.Net.IPAddress]::Any,0)
$ReceivedBytes = $UdpClient.Receive([Ref]$RemoteEndpoint)
Try {
$ReceiveBytes = $UdpClient.Receive([Ref]$RemoteEndpoint)
$ReturnData = $AsciiText.GetString($ReceiveBytes)
If ($ReturnData) {
$UdpClient.Close()
$PortOpen = "True"
$Output += New-Object -TypeName PSCustomObject -Property @{ComputerName=$C;Port=$Port;Protocol=$Protocol;PortOpen=$PortOpen}
} # End If
} Catch {
If ($Error[0].ToString() -match "\bRespond after a period of time\b") {
$UdpClient.Close()
If (Test-Connection -ComputerName $C -Count 1 -Quiet) {
$PortOpen = "True"
$Output += New-Object -TypeName PSCustomObject -Property @{ComputerName=$C;Port=$Port;Protocol=$Protocol;PortOpen=$PortOpen}
} Else {
$PortOpen = "Undetermined"
$Output += New-Object -TypeName PSCustomObject -Property @{ComputerName=$C;Port=$Port;Protocol=$Protocol;PortOpen=$PortOpen}
} # End If Else
} ElseIf ($Error[0].ToString() -match "forcibly closed by the remote host" ) {
$UdpClient.Close()
$PortOpen = "False"
$Output += New-Object -TypeName PSCustomObject -Property @{ComputerName=$C;Port=$Port;Protocol=$Protocol;PortOpen=$PortOpen}
} Else {
$UdpClient.Close()
} # End If ElseIf Else
} Finally {
$ErrorActionPreference = "Continue"
} # End Try Catch
} # End Switch UDP
} # End Switch
} # End ForEach
} END {
$ErrorActionPreference = "Continue"
Return $Output
} # End BPE
} # End Function Test-PortOpen