-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTest-Port.ps1
More file actions
470 lines (418 loc) · 19.8 KB
/
Test-Port.ps1
File metadata and controls
470 lines (418 loc) · 19.8 KB
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
function Test-Port {
<#
.SYNOPSIS
Tests network connection to a specific port on a remote computer.
.DESCRIPTION
Tests network connection to a specific port on a remote computer.
.PARAMETER ComputerName
Name of ComputerName to test the port connection on.
.PARAMETER Port
Port to test
This parameter is mandatory when using protocols TCP or UDP
.PARAMETER Protocol
TCP, UDP, RPC or ICMP
.PARAMETER TimeOut
Sets a timeout for the port query. (In milliseconds, Default is 1000)
.NOTES
Name: Test-Port.ps1
Author: Josua Burkard
DateCreated: 16.05.2019
List of Ports: http://www.iana.org/assignments/port-numbers
To Do:
Add capability to run background jobs for each host to shorten the time to scan.
.LINK
http://www.burkard.it
.EXAMPLE
Test-Port -Computer 'ComputerName' -Port 80
Checks port 80 on ComputerName 'ComputerName' to see if it is listening
.EXAMPLE
Test-Port -Computer dc1 -port 17 -Protocol UDP -timeout 10000
.EXAMPLE
Test-Port -Computer dc1 -port 17 -Protocol RPC
.EXAMPLE
Test-Port -Computer dc1 -port 17 -Protocol ICMP
.OUTPUTS
ComputerName : dc1
RemoteAddress : 192.168.1.100
RemotePort : 17
SourceAddress : 192.168.20.20
Protocol : UDP
TestSucceeded : True
Notes :
#>
[cmdletbinding(
ConfirmImpact = 'low'
)]
Param(
[Parameter(Mandatory = $True)]
[string]$ComputerName
,
[Parameter(Mandatory = $False)]
[string]$Port
,
[Parameter(Mandatory = $False)]
[int]$timeout=1000
,
[Parameter(Mandatory = $False)]
[Validateset('TCP','UDP', 'RPC', 'ICMP')]
[string]$Protocol = 'TCP'
)
Begin {
#Typically you never do this, but in this case I felt it was for the benefit of the function
#as any errors will be noted in the output of the report
$ErrorActionPreference = "SilentlyContinue"
$report = @()
}
Process {
function ConvertTo-IPv4Address {
param (
$ip
)
$ipObj = [System.Net.IPAddress]::Parse($ip)
if ($ipObj.IsIPv4MappedToIPv6) {
$ipv4 = $ipObj.MapToIPv4().ToString()
Write-Verbose "Converted to IPv4: $ipv4"
return $ipv4
} else {
Write-Verbose "Address is not IPv4-mapped IPv6: $($ipObj.ToString())"
return $ipObj.ToString()
}
}
function Test-ValidIPAddress {
param (
[string]$ip
)
return [System.Net.IPAddress]::TryParse($ip, [ref]$null)
}
function Resolve-Address {
param (
[string]$address
)
if (Test-ValidIPAddress -ip $address) {
Write-Verbose "'$address' is a valid IP address."
return $address
} else {
Write-Verbose "'$address' is not a valid IP address. Checking if it's a DNS name..."
try {
$hostEntry = [System.Net.Dns]::GetHostEntry($address)
Write-Verbose "Resolved IP addresses:"
$hostEntry.AddressList | ForEach-Object { Write-Verbose $_.ToString() }
return $hostEntry.AddressList
} catch {
throw "Could not resolve DNS name to an IP address."
}
}
}
$RemoteAddress = Resolve-Address -address $ComputerName
#Create temporary holder
$temp = "" | Select-Object ComputerName, RemoteAddress, RemotePort, SourceAddress, Protocol, TestSucceeded, Notes
$temp.ComputerName = $ComputerName
$temp.RemoteAddress = ConvertTo-IPv4Address($RemoteAddress)
$temp.RemotePort = $Port
$temp.Protocol = $Protocol
switch ( $Protocol ) {
'TCP' {
$failed = $false
#Create object for connecting to port on computer
$tcpClient = new-Object System.Net.Sockets.TcpClient
#Connect to remote machine's port
$connect = $tcpClient.BeginConnect($ComputerName,$Port,$null,$null)
#Configure a timeout before quitting
$wait = $connect.AsyncWaitHandle.WaitOne($timeout,$false)
#If timeout
If(!$wait) {
#Close connection
$tcpClient.Close()
Write-Verbose "Connection Timeout"
$temp.TestSucceeded = $false
$temp.Notes = "Connection to Port Timed Out"
} Else {
$error.Clear()
try {
$tcpClient.EndConnect($connect) | out-Null
}
catch {
}
#If error
If($error[0]){
#Begin making error more readable in report
[string]$string = ($error[0].exception).message
$message = (($string.split(":")[1]).replace('"',"")).TrimStart()
$failed = $true
}
#Close connection
try {
if ($tcpClient.Client -and $tcpClient.Client.LocalEndPoint) {
$temp.SourceAddress = ConvertTo-IPv4Address($tcpClient.Client.LocalEndPoint.Address.ToString())
}
} catch {
Write-Verbose "Could not retrieve source address: $_"
}
try {
$tcpClient.Close()
Write-Verbose "Closed TCP connection"
}
catch {
Write-Verbose "couldn't close TCP connection"
}
#If unable to query port to due failure
If ( [boolean]$failed ) {
Write-Verbose "tcp test failed"
$temp.TestSucceeded = $false
$temp.Notes = "$message"
}
else {
Write-Verbose "tcp test succeeded"
$temp.TestSucceeded = $true
$temp.Notes = ""
}
}
#Reset failed value
$failed = $Null
}
'UDP' {
#Create object for connecting to port on computer
$udpClient = New-Object System.Net.Sockets.Udpclient
#Set a timeout on receiving message
$udpClient.client.ReceiveTimeout = $Timeout
#Connect to remote machine's port
Write-Verbose "Making UDP connection to remote ComputerName"
$udpClient.Connect("$ComputerName",$Port)
$temp.SourceAddress = ConvertTo-IPv4Address($udpClient.Client.LocalEndPoint.Address)
#Sends a message to the host to which you have connected.
Write-Verbose "Sending message to remote host"
$a = new-object system.text.asciiencoding
$byte = $a.GetBytes("$(Get-Date)")
[void]$udpClient.Send($byte,$byte.length)
#IPEndPoint object will allow us to read datagrams sent from any source.
Write-Verbose "Creating remote endpoint"
$remoteendpoint = New-Object System.Net.IPEndPoint([System.Net.IPAddress]::Any,0)
Try {
#Blocks until a message returns on this socket from a remote host.
Write-Verbose "Waiting for message return"
$receivebytes = $udpClient.Receive([ref]$remoteendpoint)
[string]$returndata = $a.GetString($receivebytes)
If ($returndata) {
Write-Verbose "Connection Successful"
#Build report
$temp.TestSucceeded = $true
$temp.Notes = $returndata
$udpClient.close()
}
} Catch {
If ($Error[0].ToString() -match "\bRespond after a period of time\b") {
#Close connection
$udpClient.Close()
#Make sure that the host is online and not a false positive that it is TestSucceeded
If (Test-Connection -comp $ComputerName -count 1 -quiet) {
Write-Verbose "Connection TestSucceeded"
#Build report
$temp.TestSucceeded = $true
$temp.Notes = ""
} Else {
<#
It is possible that the host is not online or that the host is online,
but ICMP is blocked by a firewall and this port is actually TestSucceeded.
#>
Write-Verbose "Host maybe unavailable"
#Build report
$temp.ComputerName = $ComputerName
$temp.RemoteAddress = $RemoteAddress
$temp.RemotePort = $Port
$temp.Protocol = "UDP"
$temp.TestSucceeded = $false
$temp.Notes = "Unable to verify if port is TestSucceeded or if host is unavailable."
}
} ElseIf ($Error[0].ToString() -match "forcibly closed by the remote host" ) {
#Close connection
$udpClient.Close()
Write-Verbose "Connection Timeout"
#Build report
$temp.ComputerName = $ComputerName
$temp.RemoteAddress = $RemoteAddress
$temp.RemotePort = $Port
$temp.Protocol = 'UDP'
$temp.TestSucceeded = $false
$temp.Notes = "Connection to Port Timed Out"
} Else {
$udpClient.close()
}
}
}
'RPC' {
$temp.RemotePort = 135
Set-StrictMode -Version Latest
$PInvokeCode = @'
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
public class Rpc
{
// I found this crud in RpcDce.h
[DllImport("Rpcrt4.dll", CharSet = CharSet.Auto)]
public static extern int RpcBindingFromStringBinding(string StringBinding, out IntPtr Binding);
[DllImport("Rpcrt4.dll")]
public static extern int RpcBindingFree(ref IntPtr Binding);
[DllImport("Rpcrt4.dll", CharSet = CharSet.Auto)]
public static extern int RpcMgmtEpEltInqBegin(IntPtr EpBinding,
int InquiryType, // 0x00000000 = RPC_C_EP_ALL_ELTS
int IfId,
int VersOption,
string ObjectUuid,
out IntPtr InquiryContext);
[DllImport("Rpcrt4.dll", CharSet = CharSet.Auto)]
public static extern int RpcMgmtEpEltInqNext(IntPtr InquiryContext,
out RPC_IF_ID IfId,
out IntPtr Binding,
out Guid ObjectUuid,
out IntPtr Annotation);
[DllImport("Rpcrt4.dll", CharSet = CharSet.Auto)]
public static extern int RpcBindingToStringBinding(IntPtr Binding, out IntPtr StringBinding);
public struct RPC_IF_ID
{
public Guid Uuid;
public ushort VersMajor;
public ushort VersMinor;
}
// Returns a dictionary of <Uuid, port>
public static Dictionary<int, string> QueryEPM(string host)
{
Dictionary<int, string> ports_and_uuids = new Dictionary<int, string>();
int retCode = 0; // RPC_S_OK
IntPtr bindingHandle = IntPtr.Zero;
IntPtr inquiryContext = IntPtr.Zero;
IntPtr elementBindingHandle = IntPtr.Zero;
RPC_IF_ID elementIfId;
Guid elementUuid;
IntPtr elementAnnotation;
try
{
retCode = RpcBindingFromStringBinding("ncacn_ip_tcp:" + host, out bindingHandle);
if (retCode != 0)
throw new Exception("RpcBindingFromStringBinding: " + retCode);
retCode = RpcMgmtEpEltInqBegin(bindingHandle, 0, 0, 0, string.Empty, out inquiryContext);
if (retCode != 0)
throw new Exception("RpcMgmtEpEltInqBegin: " + retCode);
do
{
IntPtr bindString = IntPtr.Zero;
retCode = RpcMgmtEpEltInqNext (inquiryContext, out elementIfId, out elementBindingHandle, out elementUuid, out elementAnnotation);
if (retCode != 0)
if (retCode == 1772)
break;
retCode = RpcBindingToStringBinding(elementBindingHandle, out bindString);
if (retCode != 0)
throw new Exception("RpcBindingToStringBinding: " + retCode);
string s = Marshal.PtrToStringAuto(bindString).Trim().ToLower();
if(s.StartsWith("ncacn_ip_tcp:"))
if (ports_and_uuids.ContainsKey(int.Parse(s.Split('[')[1].Split(']')[0])) == false) ports_and_uuids.Add(int.Parse(s.Split('[')[1].Split(']')[0]), elementIfId.Uuid.ToString());
RpcBindingFree(ref elementBindingHandle);
}
while (retCode != 1772); // RPC_X_NO_MORE_ENTRIES
}
catch(Exception ex)
{
Console.WriteLine(ex);
return ports_and_uuids;
}
finally
{
RpcBindingFree(ref bindingHandle);
}
return ports_and_uuids;
}
}
'@
[Bool]$EPMTestSucceeded = $False
[Bool]$bolResult = $False
$tcpClient = New-Object System.Net.Sockets.TcpClient
Try {
$tcpClient.Connect($ComputerName, 135)
If ($tcpClient.Connected) {
$EPMTestSucceeded = $True
}
$temp.SourceAddress = ConvertTo-IPv4Address($tcpClient.Client.LocalEndPoint.Address)
$tcpClient.Close()
Write-Verbose "TCP Port 135 TestSucceeded"
}
Catch {
$tcpClient.Dispose()
Write-Verbose "TCP Port 135 closed"
}
If ($EPMTestSucceeded) {
try {
Add-Type $PInvokeCode
# Dictionary <Uuid, Port>
$RPC_ports_and_uuids = [Rpc]::QueryEPM($ComputerName)
}
catch {
$temp.TestSucceeded = $false
$temp.Notes = 'couldn''t receive RPC ports'
}
if ( [boolean]$RPC_ports_and_uuids ) {
try {
$temp.TestSucceeded = $true
$ClosedPorts = @()
$PortDeDup = ($RPC_ports_and_uuids.Keys) | Sort-Object -Unique
Foreach ($Port In $PortDeDup)
{
$tcpClient = New-Object System.Net.Sockets.TcpClient
Try
{
$tcpClient.Connect($ComputerName, $Port)
If ($tcpClient.Connected)
{
Write-Verbose "$Port Reachable"
}
$tcpClient.Close()
}
Catch
{
$temp.TestSucceeded = $false
$ClosedPorts += $Port
Write-Verbose "$Port Unreachable"
$tcpClient.Dispose()
}
}
if ( ! $temp.TestSucceeded ) {
$temp.Notes = "Ports $( $ClosedPorts -join ', ' ) are closed"
}
}
catch {
$temp.TestSucceeded = $false
$temp.Notes = 'couldn''t receive RPC ports'
}
}
}
else {
$temp.TestSucceeded = $false
$temp.Notes = "TCP Port 135 closed"
}
}
'ICMP' {
$temp.SourceAddress = (Test-Connection -ComputerName ($env:ComputerName) -IPv4 -Count 1).Address.IPAddressToString
$t = Test-Connection -ComputerName $ComputerName -ErrorAction SilentlyContinue
if ( [boolean]$t ) {
if ( ! [boolean]( $t | Where-Object { $_.Status -ne 'Success' } ) ) {
$temp.TestSucceeded = $true
}
else {
$temp.TestSucceeded = $false
$failedCount = @( $t | Where-Object { $_.Status -ne 'Success' } ).Count
$totalCount =@($t).Count
$temp.Notes = "${failedCount} of ${totalCount} ICMP pings failed"
}
}
else {
$temp.TestSucceeded = $false
$temp.Notes = $error[0]
}
}
}
#Merge temp array with report
$report += $temp }
End {
#Generate Report
$report
}
}