Skip to content

Commit 6eba58a

Browse files
committed
Performance improved
1 parent 1fea240 commit 6eba58a

File tree

5 files changed

+65
-117793
lines changed

5 files changed

+65
-117793
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Maybe you're also interested in my asynchronus [IPv4 Network Scanner](https://gi
2121
## Syntax
2222

2323
```powershell
24-
.\IPv4PortScan.ps1 [-ComputerName] <String> [[-StartPort] <Int32>] [[-EndPort] <Int32>] [[-Threads] <Int32>] [[-Force]] [[-UpdateList]] [<CommonParameters>]
24+
.\IPv4PortScan.ps1 [-ComputerName] <String> [[-StartPort] <Int32>] [[-EndPort] <Int32>] [[-Threads] <Int32>] [[-Force]] [<CommonParameters>]
2525
```
2626

2727
## Example

Scripts/Create-PortListFromWeb.ps1

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#[xml]$LatestPorts = Get-Content -Path "$PSScriptRoot\Service Name and Transport Protocol Port Number Registry.xml"
2+
[xml]$LatestPorts = (Invoke-WebRequest -Uri "https://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.xml").Content
3+
4+
$Output = ""
5+
6+
foreach($record in $LatestPorts.ChildNodes.record)
7+
{
8+
if([string]::IsNullOrEmpty($record.number) -or ([string]::IsNullOrEmpty($record.protocol)))
9+
{
10+
continue
11+
}
12+
13+
$Description = ($record.description -replace '`n','') -replace '\s+',' '
14+
15+
$Number = $record.number
16+
17+
if($Number -like "*-*")
18+
{
19+
$NumberArr = $Number.Split('-')
20+
21+
foreach($Number1 in $NumberArr[0]..$NumberArr[1])
22+
{
23+
$Output += "$Number1|$($record.protocol)|$($record.name)|$Description`n"
24+
}
25+
}
26+
else
27+
{
28+
$Output += "$Number|$($record.protocol)|$($record.name)|$Description`n"
29+
}
30+
}
31+
32+
Out-File -InputObject $Output -FilePath "$PSScriptRoot\Resources\ports.txt"

Scripts/IPv4PortScan.ps1

Lines changed: 32 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -65,129 +65,45 @@ param(
6565
[Parameter(
6666
Position=4,
6767
HelpMessage='Execute script without user interaction')]
68-
[switch]$Force,
69-
70-
[Parameter(
71-
Position=5,
72-
HelpMessage='Update Service Name and Transport Protocol Port Number Registry from IANA.org')]
73-
[switch]$UpdateList
68+
[switch]$Force
7469
)
7570

7671
Begin{
7772
Write-Verbose -Message "Script started at $(Get-Date)"
7873

79-
# IANA --> Service Name and Transport Protocol Port Number Registry -> xml-file
80-
$IANA_PortList_WebUri = "https://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.xml"
81-
82-
# Port list path
83-
$XML_PortList_Path = "$PSScriptRoot\Resources\IANA_ServiceName_and_TransportProtocolPortNumber_Registry.xml"
84-
$XML_PortList_BackupPath = "$PSScriptRoot\Resources\IANA_ServiceName_and_TransportProtocolPortNumber_Registry.xml.bak"
85-
86-
# Function to update the list from IANA (Port list)
87-
function UpdateListFromIANA
88-
{
89-
try{
90-
Write-Verbose -Message "Create backup of the IANA Service Name and Transport Protocol Port Number Registry..."
91-
92-
# Backup file, before download a new version
93-
if(Test-Path -Path $XML_PortList_Path -PathType Leaf)
94-
{
95-
Rename-Item -Path $XML_PortList_Path -NewName $XML_PortList_BackupPath
96-
}
97-
98-
Write-Verbose -Message "Updating Service Name and Transport Protocol Port Number Registry from IANA.org..."
99-
100-
# Download xml-file from IANA and save it
101-
[xml]$New_XML_PortList = Invoke-WebRequest -Uri $IANA_PortList_WebUri -ErrorAction Stop
102-
103-
$New_XML_PortList.Save($XML_PortList_Path)
74+
$PortList_Path = "$PSScriptRoot\Resources\ports.txt"
75+
}
10476

105-
# Remove backup, if no error
106-
if(Test-Path -Path $XML_PortList_BackupPath -PathType Leaf)
107-
{
108-
Remove-Item -Path $XML_PortList_BackupPath
109-
}
110-
}
111-
catch{
112-
Write-Verbose -Message "Cleanup downloaded file and restore backup..."
77+
Process{
78+
if(Test-Path -Path $PortList_Path -PathType Leaf)
79+
{
80+
$PortsHashTable = @{ }
11381

114-
# On error: cleanup downloaded file and restore backup
115-
if(Test-Path -Path $XML_PortList_Path -PathType Leaf)
116-
{
117-
Remove-Item -Path $XML_PortList_Path -Force
118-
}
82+
Write-Verbose -Message "Read ports.txt and fill hash table..."
11983

120-
if(Test-Path -Path $XML_PortList_BackupPath -PathType Leaf)
84+
foreach($Line in Get-Content -Path $PortList_Path)
85+
{
86+
if(-not([String]::IsNullOrEmpty($Line)))
12187
{
122-
Rename-Item -Path $XML_PortList_BackupPath -NewName $XML_PortList_Path
123-
}
124-
125-
$_.Exception.Message
126-
}
127-
}
128-
129-
# Function to assign service with port
130-
function AssignServiceWithPort
131-
{
132-
param(
133-
$Result
134-
)
135-
136-
Begin{
137-
138-
}
139-
140-
Process{
141-
$Service = [String]::Empty
142-
$Description = [String]::Empty
143-
144-
foreach($XML_Node in $XML_PortList.Registry.Record)
145-
{
146-
if(($Result.Protocol -eq $XML_Node.protocol) -and ($Result.Port -eq $XML_Node.number))
147-
{
148-
$Service = $XML_Node.name
149-
$Description = $XML_Node.description
150-
break
88+
try{
89+
$HashTableData = $Line.Split('|')
90+
91+
if($HashTableData[1] -eq "tcp")
92+
{
93+
$PortsHashTable.Add([int]$HashTableData[0], [String]::Format("{0}|{1}",$HashTableData[2],$HashTableData[3]))
94+
}
15195
}
96+
catch [System.ArgumentException] { } # Catch if port is already added to hash table
15297
}
153-
154-
[pscustomobject] @{
155-
Port = $Result.Port
156-
Protocol = $Result.Protocol
157-
ServiceName = $Service
158-
ServiceDescription = $Description
159-
Status = $Result.Status
160-
}
161-
}
162-
163-
End{
164-
16598
}
166-
}
167-
}
16899

169-
Process{
170-
$Xml_PortList_Available = Test-Path -Path $XML_PortList_Path -PathType Leaf
171-
172-
if($UpdateList)
173-
{
174-
UpdateListFromIANA
175-
}
176-
elseif($Xml_PortList_Available -eq $false)
177-
{
178-
Write-Warning -Message "No xml-file to assign service with port found! Use the parameter ""-UpdateList"" to download the latest version from IANA.org. This warning doesn`t affect the scanning procedure."
179-
}
180-
181-
# Check if it is possible to assign service with port --> import xml-file
182-
if($Xml_PortList_Available)
183-
{
184100
$AssignServiceWithPort = $true
185-
186-
$XML_PortList = [xml](Get-Content -Path $XML_PortList_Path)
187101
}
188102
else
189103
{
190104
$AssignServiceWithPort = $false
105+
106+
Write-Warning -Message "No port-file to assign service with port found! Execute the script ""Create-PortListFromWeb.ps1"" to download the latest version.. This warning doesn`t affect the scanning procedure."
191107
}
192108

193109
# Check if host is reachable
@@ -373,7 +289,17 @@ Process{
373289
{
374290
if($AssignServiceWithPort)
375291
{
376-
AssignServiceWithPort -Result $Job_Result
292+
$Service = [String]::Empty
293+
294+
$Service = $PortsHashTable.Get_Item($Job_Result.Port).Split('|')
295+
296+
[pscustomobject] @{
297+
Port = $Job_Result.Port
298+
Protocol = $Job_Result.Protocol
299+
ServiceName = $Service[0]
300+
ServiceDescription = $Service[1]
301+
Status = $Job_Result.Status
302+
}
377303
}
378304
else
379305
{

0 commit comments

Comments
 (0)