Skip to content

Commit 069b791

Browse files
committed
Upload Version 2.0 - Now with service name for each port number
1 parent 68c15ba commit 069b791

File tree

3 files changed

+117841
-40
lines changed

3 files changed

+117841
-40
lines changed

README.md

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,67 @@
11
# PowerShell Async PortScanner
22

3-
Powerful asynchronus Port-Scanner which returns a custom PowerShell-Object with basic informations about the scanned Port-Range include Port and Status.
3+
Powerful asynchronus Port-Scanner which returns a custom PowerShell-Object with basic informations about the scanned Port-Range include port number, protocol, service name, service description and status.
44

55
## Description
66

7-
This is a powerful asynchronus Port-Scanner working with the PowerShell RunspacePool. You can scan any Port-Range you want.
7+
This is a powerful asynchronus Port-Scanner working with the PowerShell RunspacePool. You can scan any Port-Range you want. The Result will show you all open ports port number, protocol, service name, service description and status.
88

99
This script also work fine along with my asychronus IP-Scanner published on GitHub too. You can easily pipe the output of the IP-Scanner result in this script.
1010

1111
## Syntax
1212

1313
```powershell
14-
.\ScanPortsAsync.ps1 [-IPv4Address] <IPAddress> [[-StartPort] <Int32>] [[-EndPort] <Int32>] [[-Threads] <Int32>] [[-IncludeClosed]] [<CommonParameters>]
14+
.\ScanPortsAsync.ps1 [-IPv4Address] <IPAddress> [[-StartPort] <Int32>] [[-EndPort] <Int32>] [[-Threads] <Int32>] [[-UpdateListFromIANA]] [<CommonParameters>]
1515
```
1616

1717
## Example
1818

19-
Simple Port Scan
19+
Scan a specific Port-Range (1-500)
20+
2021
```powershell
21-
.\ScanPortsAsync.ps1 -IPv4Address 192.168.1.100 -StartPort 1 -EndPort 5000
22+
.\ScanPortsAsync.ps1 -IPv4Address 192.168.1.100 -StartPort 1 -EndPort 500 | Format-Table
2223
```
2324

24-
Show closed Ports in result
25+
You may want to update the official Service Name and Transport Protocol Port Number Registry from IANA... Just add the parameter "-UpdateListFromIANA".
26+
2527
```powershell
26-
.\ScanPortsAsync.ps1 -IPv4Address 172.16.2.5 -Threads 200 -IncludeClosed
28+
.\ScanPortsAsync.ps1 -IPv4Address 172.16.2.5 -UpdateListFromIANA
2729
```
30+
If your PC has enough power, you can use more threads at the same time
2831

32+
```powershell
33+
.\ScanPortsAsync.ps1 -IPv4Address 172.16.2.5 -Threads 250
34+
```
2935

3036
## Output
3137

3238
```powershell
33-
Port Status
34-
---- ------
35-
21 Open
36-
80 Open
37-
139 Open
38-
443 Open
39-
445 Open
39+
Port Protocol Service Name Service Description Status
40+
---- -------- ------------ ------------------- ------
41+
21 tcp ftp File Transfer Protocol [Control] open
42+
53 tcp domain Domain Name Server open
43+
80 tcp http World Wide Web HTTP open
44+
80 tcp www World Wide Web HTTP open
45+
80 tcp www-http World Wide Web HTTP open
46+
139 tcp netbios-ssn NETBIOS Session Service open
47+
445 tcp microsoft-ds Microsoft-DS open
48+
```
49+
50+
and if no port list is available (should never happend, because it's uploaded on Github)
51+
52+
```powershell
53+
Port Protocol Status
54+
---- -------- ------
55+
21 tcp open
56+
53 tcp open
57+
80 tcp open
58+
139 tcp open
59+
445 tcp open
4060
```
41-
61+
62+
## Offical Port List
63+
64+
* [Service Name and Transport Protocol Port Number Registry - IANA.org](https://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.xml)
65+
4266
## ToDo
43-
- Integrate Port-List
44-
like: 80 (http), 443 (https) ...
67+
[x] Integrate Port-List like: 80 (http), 443 (https), etc.

Scripts/ScanPortsAsync.ps1

Lines changed: 115 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@
99
<#
1010
.SYNOPSIS
1111
Powerful asynchronus Port-Scanner which returns a custom PowerShell-Object with basic informations about the
12-
scanned Port-Range include Port and Status.
12+
scanned Port-Range include port number, protocol, service name, service description and status.
1313
1414
.DESCRIPTION
1515
This is a powerful asynchronus Port-Scanner working with the PowerShell RunspacePool. You can scan any
16-
Port-Range you want.
16+
Port-Range you want. The Result will show you all open ports port number, protocol, service name,
17+
service description and status.
1718
1819
This script also work fine along with my asychronus IP-Scanner published on GitHub too. You can easily
1920
pipe the output of the IP-Scanner result in this script.
@@ -24,7 +25,10 @@
2425
.\ScanPortsAsync.ps1 -IPv4Address 172.16.0.1 -StartPort 1 -EndPort 1000
2526
2627
.EXAMPLE
27-
.\ScanPortsAsync.ps1 -IPv4Address 192.168.1.100 -IncludeClosed
28+
.\ScanPortsAsync.ps1 -IPv4Address 192.168.1.100 -UpdateListFromIANA
29+
30+
.EXAMPLE
31+
.\ScanPortsAsync.ps1 -IPv4Address 192.168.1.100 -Threads 250
2832
2933
.LINK
3034
Github Profil: https://github.com/BornToBeRoot
@@ -41,36 +45,39 @@ Param(
4145

4246
[Parameter(
4347
Position=1,
44-
Mandatory=$false,
4548
HelpMessage='Enter the Start-Port (Default=1)')]
4649
[Int32]$StartPort=1,
4750

4851
[Parameter(
4952
Position=2,
50-
Mandatory=$false,
5153
HelpMessage='Enter the End-Port (Default=65535)')]
5254
[Int32]$EndPort=65535,
5355

5456
[Parameter(
5557
Position=3,
56-
Mandatory=$false,
5758
HelpMessage='Set the maximum number of threads at the same time (Default=100)')]
5859
[Int32]$Threads=100,
5960

6061
[Parameter(
6162
Position=4,
62-
Mandatory=$false,
63-
HelpMessage='Show closed Ports in result')]
64-
[Switch]$IncludeClosed
63+
HelpMessage='Update Service Name and Transport Protocol Port Number Registry from IANA.org (https://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.xml)')]
64+
[Switch]$UpdateListFromIANA
6565
)
6666

6767
Begin{
6868
# Time when the script starts
69-
$StartTime = Get-Date
70-
71-
# Script FileName
69+
$StartTime = Get-Date
70+
71+
# Script Path and FileName
72+
$Script_Startup_Path = Split-Path -Parent $MyInvocation.MyCommand.Path
7273
$ScriptFileName = $MyInvocation.MyCommand.Name
73-
74+
75+
# IANA -> Service Name and Transport Protocol Port Number Registry -> XML-File
76+
$IANA_PortList_WebUri = "https://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.xml"
77+
78+
# Local path to PortList
79+
$XML_PortList_Path = "$Script_Startup_Path\ServiceName_and_TransportProtocolPortNumber_Registry.xml"
80+
7481
# Validate Port-Range
7582
if($StartPort -gt $EndPort)
7683
{
@@ -79,6 +86,42 @@ Begin{
7986
}
8087

8188
$PortRange = ($EndPort - $StartPort)
89+
90+
# Port list can be updated from IANA.org with the parameter "-UpdatePortList
91+
if($UpdateListFromIANA)
92+
{
93+
try
94+
{
95+
Write-Host "Updating Service Name and Transport Protocol Port Number Registry from IANA...`t" -ForegroundColor Gray -NoNewline
96+
97+
[xml]$New_XML_PortList = Invoke-WebRequest -Uri $IANA_PortList_WebUri # Download latest xml-file from IANA
98+
99+
Remove-Item $XML_PortList_Path -Force -ErrorAction SilentlyContinue # Don`t show errors if there is no old file
100+
101+
$New_XML_PortList.Save($XML_PortList_Path) # Save xml-file
102+
103+
Write-Host "OK" -ForegroundColor Green
104+
}
105+
catch
106+
{
107+
$ErrorMsg = $_.Exception.Message
108+
109+
Write-Host "Update Service Name and Transport Protocol Port Number Registry from IANA failed with the follwing error message: $ErrorMsg" -ForegroundColor Red
110+
}
111+
}
112+
elseif(-Not([System.IO.File]::Exists($XML_PortList_Path)))
113+
{
114+
Write-Host 'No XML-File to assign service name with port number found! Use the parameter "-UpdateListFromIANA" to download the latest version from IANA.org. This warning doesn`t affect the scanning procedure.' -ForegroundColor Yellow
115+
}
116+
117+
if([System.IO.File]::Exists($XML_PortList_Path))
118+
{
119+
$AssignServiceWithPorts = $true
120+
}
121+
else
122+
{
123+
$AssignServiceWithPorts = $false
124+
}
82125

83126
if(-not( Test-Connection -ComputerName $IPv4Address -Count 2 -Quiet))
84127
{
@@ -107,18 +150,20 @@ Process{
107150

108151
if($Socket.Connected)
109152
{
110-
$Status = "Open"
153+
$Status = "open"
111154
$Socket.Close()
112155
}
113156
}
114157
catch
115158
{
116-
$Status = "Closed"
159+
$Status = "closed"
117160
}
118161

119162
$Result = New-Object -TypeName PSObject
120-
Add-Member -InputObject $Result -MemberType NoteProperty -Name Port -Value $Port
121-
Add-Member -InputObject $Result -MemberType NoteProperty -Name Status -Value $Status
163+
Add-Member -InputObject $Result -MemberType NoteProperty -Name "Port" -Value $Port
164+
Add-Member -InputObject $Result -MemberType NoteProperty -Name "Protocol" -Value "tcp"
165+
Add-Member -InputObject $Result -MemberType NoteProperty -Name "Status" -Value $Status
166+
122167
return $Result
123168
}
124169

@@ -164,32 +209,79 @@ Process{
164209
Write-Host "Process results...`t`t`t" -ForegroundColor Yellow -NoNewline
165210

166211
# Built global array
167-
$Results = @()
212+
$Jobs_Result = @()
168213

169214
# Get results and fill the array
170215
foreach($Job in $Jobs)
171216
{
172-
$Results += $Job.Pipe.EndInvoke($Job.Result)
217+
$Jobs_Result += $Job.Pipe.EndInvoke($Job.Result)
173218
}
174219

220+
# Only get open ports (others are closed -.- )
221+
$Ports_Open = $Jobs_Result | Where-Object {$_.Status -eq "open"}
222+
175223
Write-Host "[" -ForegroundColor Gray -NoNewline; Write-Host "Done" -ForegroundColor Green -NoNewline; Write-Host "]" -ForegroundColor Gray
224+
225+
# Assign service with ports
226+
if($AssignServiceWithPorts)
227+
{
228+
Write-Host "Assign services to ports...`t`t" -ForegroundColor Yellow -NoNewline
229+
230+
$XML_PortList = [xml](Get-Content -Path $XML_PortList_Path)
231+
232+
$Ports_Open_Assigned = @()
233+
234+
# Go through each port
235+
foreach($Port_Open in $Ports_Open)
236+
{
237+
# go through each service
238+
foreach($XML_Node in $XML_PortList.Registry.Record)
239+
{
240+
# Find the right service (based on protocol and port number)
241+
if(($Port_Open.Protocol -eq $XML_Node.protocol) -and ($Port_Open.Port -eq $XML_Node.number))
242+
{
243+
# Built new custom PSObject
244+
$Port_Open_Assigned = New-Object -TypeName PSObject
245+
Add-Member -InputObject $Port_Open_Assigned -MemberType NoteProperty -Name "Port" -Value $Port_Open.Port
246+
Add-Member -InputObject $Port_Open_Assigned -MemberType NoteProperty -Name "Protocol" -Value $Port_Open.Protocol
247+
Add-Member -InputObject $Port_Open_Assigned -MemberType NoteProperty -Name "Service Name" -Value $XML_Node.name
248+
Add-Member -InputObject $Port_Open_Assigned -MemberType NoteProperty -Name "Service Description" -Value $XML_Node.description
249+
Add-Member -InputObject $Port_Open_Assigned -MemberType NoteProperty -Name "Status" -Value $Port_Open.Status
250+
251+
# Add it to an array
252+
$Ports_Open_Assigned += $Port_Open_Assigned
253+
}
254+
}
255+
}
256+
257+
Write-Host "[" -ForegroundColor Gray -NoNewline; Write-Host "Done" -ForegroundColor Green -NoNewline; Write-Host "]" -ForegroundColor Gray
258+
}
176259
}
177260

261+
End{
262+
# If no XML-File to assign service with port... only show open ports
263+
if($AssignServiceWithPorts)
264+
{
265+
$Results = $Ports_Open_Assigned
266+
}
267+
else
268+
{
269+
$Results = $Ports_Open
270+
}
178271

179-
End{
180272
$EndTime = Get-Date
181273

182274
$ExecutionTimeMinutes = (New-TimeSpan -Start $StartTime -End $EndTime).Minutes
183275
$ExecutionTimeSeconds = (New-TimeSpan -Start $StartTime -End $EndTime).Seconds
184276

185277
# Some User-Output with Device UP/Down and execution time
186278
Write-Host "`n+=-=-=-=-=-=-=-=-=-=-=-= Result =-=-=-=-=-=-=-=-=-=-=-=`n|"
187-
Write-Host "| Ports Open:`t`t$(@($Results | Where-Object {($_.Status -eq "Open")}).Count)"
188-
Write-Host "| Ports Closed:`t$(@($Results | Where-Object {($_.Status -eq "Closed")}).Count)"
279+
Write-Host "| Ports Scanned:`t$($Jobs_Result.Count)"
280+
Write-Host "| Ports Open:`t`t$(@($Results | Where-Object {($_.Status -eq "open")}).Count)"
189281
Write-Host "|`n+========================================================`n"
190282
Write-Host "Script duration:`t$ExecutionTimeMinutes Minutes $ExecutionTimeSeconds Seconds`n" -ForegroundColor Yellow
191283
Write-Host "Script ($ScriptFileName) exit at $EndTime`n" -ForegroundColor Green
192284

193285
# Return custom psobject with Port status
194-
if($IncludeClosed) { return $Results } else { return $Results | Where-Object {$_.Status -eq "Open"} }
286+
return $Results
195287
}

0 commit comments

Comments
 (0)