From 4ef7675668d094b1f0ecc42b54d319be566ddd17 Mon Sep 17 00:00:00 2001 From: Xevion Date: Fri, 26 Aug 2022 18:26:15 -0500 Subject: [PATCH] CheckExcludedPorts PowerShell script --- content/scripts/CheckExcludedPorts.ps1 | 36 ++++++++++++++++++++++++++ content/scripts/excluded-ports.md | 13 ++++++++++ 2 files changed, 49 insertions(+) create mode 100644 content/scripts/CheckExcludedPorts.ps1 create mode 100644 content/scripts/excluded-ports.md diff --git a/content/scripts/CheckExcludedPorts.ps1 b/content/scripts/CheckExcludedPorts.ps1 new file mode 100644 index 0000000..b8e0a7e --- /dev/null +++ b/content/scripts/CheckExcludedPorts.ps1 @@ -0,0 +1,36 @@ +<# + .Description + This script checks if the given port has been placed in a excluded port range. + This script utilizes the netsh command and only observes TCP exclusion ranges. +#> +param ( + [Parameter(Mandatory=$true)] + [Int32] + $targetPort +) + +# Filters excluded port range output to just the ports +$rawPortRanges = netsh interface ipv4 show excludedportrange protocol=tcp | Select-String "\d+\s+\d+" +# Cleans the output to include just start/end port with a single space +$cleanPortRanges = $rawPortRanges -replace "\s+(\d+)\s+(\d+).*", '$1 $2' + +$found = $false +$cleanPortRanges | % { + # Break apart the port, intrepret as int + $start, $end = $_.toString().split(' ') + $start = $start -as [int] + $end = $end -as [int] + + # Complete the check + if ($targetPort -In $start..$end) { + $found = $true + Write-Host "${start} - ${end}" + Write-Host "`tPort ${targetPort} is in an excluded port range." + Break # Assumption: Excluded port ranges do not overlap + } +} + +# Default output +if (!$found) { + Write-Host "Port ${targetPort} is not in an excluded port range." +} \ No newline at end of file diff --git a/content/scripts/excluded-ports.md b/content/scripts/excluded-ports.md new file mode 100644 index 0000000..122e353 --- /dev/null +++ b/content/scripts/excluded-ports.md @@ -0,0 +1,13 @@ ++++ +title = "Check Excluded Port Ranges" +summary = "Check if a specific port is in an excluded port range with this simple script." +date = 2022-08-26 ++++ + +This script was built during an internship (and properly requested) to check excluded port ranges. + +Raw file available at {{% absolute_url "./scripts/CheckExcludedPorts.ps1" %}} + +```powershell +{{% file "/scripts/CheckExcludedPorts.ps1" %}} +``` \ No newline at end of file