Skip to content

Commit

Permalink
Performance improved
Browse files Browse the repository at this point in the history
  • Loading branch information
BornToBeRoot committed Aug 5, 2017
1 parent 1fea240 commit 6eba58a
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 117,793 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Maybe you're also interested in my asynchronus [IPv4 Network Scanner](https://gi
## Syntax

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

## Example
Expand Down
32 changes: 32 additions & 0 deletions Scripts/Create-PortListFromWeb.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#[xml]$LatestPorts = Get-Content -Path "$PSScriptRoot\Service Name and Transport Protocol Port Number Registry.xml"
[xml]$LatestPorts = (Invoke-WebRequest -Uri "https://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.xml").Content

$Output = ""

foreach($record in $LatestPorts.ChildNodes.record)
{
if([string]::IsNullOrEmpty($record.number) -or ([string]::IsNullOrEmpty($record.protocol)))
{
continue
}

$Description = ($record.description -replace '`n','') -replace '\s+',' '

$Number = $record.number

if($Number -like "*-*")
{
$NumberArr = $Number.Split('-')

foreach($Number1 in $NumberArr[0]..$NumberArr[1])
{
$Output += "$Number1|$($record.protocol)|$($record.name)|$Description`n"
}
}
else
{
$Output += "$Number|$($record.protocol)|$($record.name)|$Description`n"
}
}

Out-File -InputObject $Output -FilePath "$PSScriptRoot\Resources\ports.txt"
138 changes: 32 additions & 106 deletions Scripts/IPv4PortScan.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -65,129 +65,45 @@ param(
[Parameter(
Position=4,
HelpMessage='Execute script without user interaction')]
[switch]$Force,

[Parameter(
Position=5,
HelpMessage='Update Service Name and Transport Protocol Port Number Registry from IANA.org')]
[switch]$UpdateList
[switch]$Force
)

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

# IANA --> Service Name and Transport Protocol Port Number Registry -> xml-file
$IANA_PortList_WebUri = "https://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.xml"

# Port list path
$XML_PortList_Path = "$PSScriptRoot\Resources\IANA_ServiceName_and_TransportProtocolPortNumber_Registry.xml"
$XML_PortList_BackupPath = "$PSScriptRoot\Resources\IANA_ServiceName_and_TransportProtocolPortNumber_Registry.xml.bak"

# Function to update the list from IANA (Port list)
function UpdateListFromIANA
{
try{
Write-Verbose -Message "Create backup of the IANA Service Name and Transport Protocol Port Number Registry..."

# Backup file, before download a new version
if(Test-Path -Path $XML_PortList_Path -PathType Leaf)
{
Rename-Item -Path $XML_PortList_Path -NewName $XML_PortList_BackupPath
}

Write-Verbose -Message "Updating Service Name and Transport Protocol Port Number Registry from IANA.org..."

# Download xml-file from IANA and save it
[xml]$New_XML_PortList = Invoke-WebRequest -Uri $IANA_PortList_WebUri -ErrorAction Stop

$New_XML_PortList.Save($XML_PortList_Path)
$PortList_Path = "$PSScriptRoot\Resources\ports.txt"
}

# Remove backup, if no error
if(Test-Path -Path $XML_PortList_BackupPath -PathType Leaf)
{
Remove-Item -Path $XML_PortList_BackupPath
}
}
catch{
Write-Verbose -Message "Cleanup downloaded file and restore backup..."
Process{
if(Test-Path -Path $PortList_Path -PathType Leaf)
{
$PortsHashTable = @{ }

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

if(Test-Path -Path $XML_PortList_BackupPath -PathType Leaf)
foreach($Line in Get-Content -Path $PortList_Path)
{
if(-not([String]::IsNullOrEmpty($Line)))
{
Rename-Item -Path $XML_PortList_BackupPath -NewName $XML_PortList_Path
}

$_.Exception.Message
}
}

# Function to assign service with port
function AssignServiceWithPort
{
param(
$Result
)

Begin{

}

Process{
$Service = [String]::Empty
$Description = [String]::Empty

foreach($XML_Node in $XML_PortList.Registry.Record)
{
if(($Result.Protocol -eq $XML_Node.protocol) -and ($Result.Port -eq $XML_Node.number))
{
$Service = $XML_Node.name
$Description = $XML_Node.description
break
try{
$HashTableData = $Line.Split('|')

if($HashTableData[1] -eq "tcp")
{
$PortsHashTable.Add([int]$HashTableData[0], [String]::Format("{0}|{1}",$HashTableData[2],$HashTableData[3]))
}
}
catch [System.ArgumentException] { } # Catch if port is already added to hash table
}

[pscustomobject] @{
Port = $Result.Port
Protocol = $Result.Protocol
ServiceName = $Service
ServiceDescription = $Description
Status = $Result.Status
}
}

End{

}
}
}

Process{
$Xml_PortList_Available = Test-Path -Path $XML_PortList_Path -PathType Leaf

if($UpdateList)
{
UpdateListFromIANA
}
elseif($Xml_PortList_Available -eq $false)
{
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."
}

# Check if it is possible to assign service with port --> import xml-file
if($Xml_PortList_Available)
{
$AssignServiceWithPort = $true

$XML_PortList = [xml](Get-Content -Path $XML_PortList_Path)
}
else
{
$AssignServiceWithPort = $false

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."
}

# Check if host is reachable
Expand Down Expand Up @@ -373,7 +289,17 @@ Process{
{
if($AssignServiceWithPort)
{
AssignServiceWithPort -Result $Job_Result
$Service = [String]::Empty

$Service = $PortsHashTable.Get_Item($Job_Result.Port).Split('|')

[pscustomobject] @{
Port = $Job_Result.Port
Protocol = $Job_Result.Protocol
ServiceName = $Service[0]
ServiceDescription = $Service[1]
Status = $Job_Result.Status
}
}
else
{
Expand Down
Loading

0 comments on commit 6eba58a

Please sign in to comment.