ZiBaT => Peter Levinsky => Tech => exercise
Introduction
Port Scanner
Updated : 2021-11-18

Port Scanner

Mission

The purpose of this exercise is to be able to scan open ports on a host.
This could be done as either a White-Hat attack, or a Black-Hat attack. What is the difference?
You should only scan for open TCP ports, not UDP.
Why is it more difficult to scan UDP ports?

 

Part 1: Simple Port Scanner (TCP)


Note: If you want to test something different than your own PC, you should be on the MGV-xxxx network.

Create a new console application project.
Either hardcode a destination or ask the user for an IP-address. (First time could just be localhost / 127.0.0.1)
Implement a for loop that goes through all available ports (How many? Hint: 16bit).
For each port, create a TcpClient with the IP-address and the port number.
If it throws an Exception, the port is not in use.
If it doesn’t throw an Exception, remember the port number (in a list?)
Show the open ports in the console.

 

Part 2: Using Threads:

Now the speed of your port scanner is probably not very impressive.
Why is it so slow?
One way to make it faster is to use threads.
How would you implement this? One thread per port, or multiple ports per thread?
How will you show the open ports in the console when using threads?

 

Extra 1: Refactor your Port Scanner

If testing on your local PC, there is a much faster way of testing if a port is already in use.
How would you check this?
Hint: Can the same port be used twice?

 

Extra 2: Port Scanner for UDP

Until now you have scanned for tcp-ports. If you are on localhost you can also scan for UDP Ports.
Make a new project to scan for UDP-ports

 

Extra 3: General Improve the Port Scanner

Can you think of other ways to improve the Port Scanner (tcp)?