CheckExcludedPorts PowerShell script

This commit is contained in:
Xevion
2022-08-26 18:26:15 -05:00
parent 942246265d
commit 4ef7675668
2 changed files with 49 additions and 0 deletions

View File

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

View File

@@ -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" %}}
```