Local Tech Repair: Simple Port Scan using Powershell

Wednesday, July 8, 2015

Simple Port Scan using Powershell

Ever want to learn how to make a simple port scanner using powershell? Well here is how you can do a very simple tcp port scan. 

This is very basic and doesn't read the socket responses just checks if the response is there. this is using a tcp handshake and is not stealthy at all.

[int[]]$port = 80..83
$net = "131.219.230"
$r = 153..153
#beginning of ip scan
foreach ($range in $r) {
$ip = "{0}.{1}" -F $net,$range
#beginning of port scan
foreach ($p in $port){
try {
$buffer = new-object System.Byte[] 1026;
$socket = New-Object System.Net.Sockets.tcpClient($ip, $p)

if($socket.Connected) {"port $p is open"}

catch { "port $p is Closed"}
} #end of port scan

} #end of ip scan

This will give you results like.

port 80 is open
port 81 is Closed
port 82 is Closed
port 83 is Closed

Or something like that. you can also make the port look like 80,443,etc to specify specific ports to check. and the r is the range. so if you wanted to check from ip addresses 2..255 then you could. though since this is not parallel and doing tcp connections this is extremely slow. though it gives you an idea on how it could be done. 

Thanks for reading,

local tech repair admin