lets create a Port Scanner tool, which will test each port listenning on the destination i.p
usually the most common ports we are checking are on the list below –
http – 80
ftp – 21
ssl – 443
telnet -23
ssh – 22
so ..there are few ways to tell the function to go over each port and test it ,i chose to do it with Hashtable
of-course you can use also with an array –
Hashtable example –
$Ports = @{
http=80
ftp=21
ssl=443
telnet =23
ssh = 22
}
O.k Not that we created a table we should loop through it with a command .
so the command needs to test if the Socket on the listening side is open we’ll use it with –
New-Object System.Net.Sockets.TcpClient
and add it to a variable called $Socket = New-Object System.Net.Sockets.TcpClient
one of the member of this object is ‘Connect‘ which also required with I.P Address and Port ,like –
$Socket.Connect(“192.168.10.252” ,443)
so now we know what required we’ll wrap it in to a function call the I.P we’ll choose and test it with each Port we
added before to the Hashtable we created on the beginning .
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
function PortScanner { param($ip = "localhost") $Socket = New-Object System.Net.Sockets.TcpClient $Ports = [ordered]@{ http=80 ftp=21 ssl=443 telnet =23 ssh = 22 } foreach ($port in $Ports.Values) { try { Write-Host "Trying to Connect to i.p $IP with Port #$port" $Socket.Connect($ip ,$port) Write-Host "$Port Open on $IP" -BackgroundColor Black -ForegroundColor Green Start-Sleep 2 } catch{write-host "Couldn't connect to Port #$port On $IP " -BackgroundColor Black -ForegroundColor Red} } } PortScanner |
you might interest also with post Powershell Test-Socket.