Open Ports
Linux
netstat works on windows and linux, but all of the flags/switches are not the same.
netstat
See everything:
netstat -tunlp
Filter specific ports:
netstat -tunlp | egrep ":80|:443"
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 9730/apache2
tcp 0 0 0.0.0.0:443 0.0.0.0:* LISTEN 9730/apache2
netstat port watch
while true; do clear; netstat -tuln | grep ':443 '; sleep 2; done
ss port watch
ss -l | grep http
tcp LISTEN 0 128 0.0.0.0:http 0.0.0.0:*
tcp LISTEN 0 128 0.0.0.0:https 0.0.0.0:*
while true; do clear; ss -tuln | grep ':443 '; sleep 2; done
netcat port check
nc -v somesite.com 22
Connection to somesite.com (162.243.23.116) 22 port [tcp/ssh] succeeded!
SSH-2.0-OpenSSH_7.6p1 Ubuntu-4ubuntu0.7
nmap
nmap -sV -p 22,80 192.168.1.201
telnet
telnet 192.168.1.201 22
Windows
Show Matching Binary
as admin if you want to see the binary that is opening the port:
netstat -anotb | findstr :80
or as non-admin if you only need to see the open ports:
netstat -anot | findstr :80
netstat Port Watch
for /L %i in (0,0,0) do @cls && netstat -ano|find ":443" && @ping -n 1 -w 2000 224.0.0.0 >NUL && @cls
Powershell Port Watch
while ($true) { cls; netstat -ano | Select-String ":443"; Start-Sleep -Milliseconds 2000 }
Powershell TNC
Basic check looks like this:
Test-NetConnection -Computername "192.168.1.201" -Port 22
Monitoring looks like this:
while(1) {sleep -sec 2; Test-NetConnection -Computername "192.168.1.201" -Port 22 -Information Quiet}
Powershell 3 way Handshake
$client = new-object System.Net.Sockets.TcpClient
$result = $client.ConnectAsync("192.168.1.201","22").Wait(1500)
write-host -NoNewline "$(Get-Date)`t$result"
Powershell Port Scanner
<#
-----------------
DESCRIPTION
-----------------
Given a host and a list of ports, tells you if they are open/closed
SIMPLER ONELINER:
(cannot specify timeout or multiple ports, but does TCP Connect)
PS> while(1) {sleep -sec 2; Test-NetConnection -Computername 'yourhost.com' -Port 443 -Information Quiet}
THIS VERSION:
Allows 1 host and multiple ports, colorizes output, measures time taken
(based on Chapmans code with a few adjustments because the timewait was annoying)
-----------------
OUTPUT:
-----------------
PS C:\Users\James\Desktop> &".\checkports.ps1" 'yourhost.com',22,80,443,3389
MM/DD/YYY HH:MM:SS STAT PORT HOST TIME TAKEN
12/09/2020 13:09:32 OPEN 22 yourhost.com 0.0491842
12/09/2020 13:09:34 OPEN 80 yourhost.com 0.0401987
12/09/2020 13:09:36 OPEN 443 yourhost.com 0.0426557
12/09/2020 13:09:40 FAIL 3389 yourhost.com 1.5036415
MM/DD/YYY HH:MM:SS STAT PORT HOST TIME TAKEN
12/09/2020 13:09:42 OPEN 22 yourhost.com 0.0436378
12/09/2020 13:09:44 OPEN 80 yourhost.com 0.0413645
12/09/2020 13:09:46 OPEN 443 yourhost.com 0.0415196
12/09/2020 13:09:49 FAIL 3389 yourhost.com 1.5025818
-----------------
INSTRUCTIONS:
-----------------
CHECK PERMISSION:
Get-ExecutionPolicy -List
SET PERMISSION:
Set-ExecutionPolicy Unrestricted
SAVE AND RUN:
Save as "checkports.ps1"
USAGE:
PS> & ".\checkports.ps1" "yourhost.com",21,22,80,443,3389
#>
[CmdletBinding()]
param(
[Parameter(Mandatory=$False)]
[string[]]$in=@()
)
if ( $PSBoundParameters.Values.Count -eq 0 ){
write-host "USAGE:" -ForegroundColor Red
write-host "`tPS> & '.\checkports.ps1' 'yourhost.com',21,22,80,443,3389" -ForegroundColor Red
return
}
$h,$ps = $in
while(1){
write-host "MM/DD/YYYY HH:MM:SS`tSTAT`tPORT`tHOST`t`tTIME TAKEN" -ForegroundColor Yellow
foreach ($p in $ps) {
# measure it
Measure-Command {
# try a connection
try {
$client = new-object System.Net.Sockets.TcpClient
$opened = $client.ConnectAsync($h,$p).Wait(1500)
# if actively closed or fails timeout
if($opened){
$client.Close()
write-host -NoNewline "$(Get-Date)`tOPEN`t$p`t$h`t" -ForegroundColor Green
} else {
write-host -NoNewline "$(Get-Date)`tFAIL`t$p`t$h`t" -ForegroundColor Red
}
}
# default if it fails connect
catch {
write-host -NoNewline "$(Get-Date)`t????`t$p`t$h`t" -ForegroundColor Red
}
} | % TotalSeconds
# wait 2 seconds between each port to prevent blocking
Start-Sleep -Seconds 2
}
write-host "`n"
}