A port scanner is a tool that allows you to scan a target host to see which ports are open or closed. It’s a useful tool for security professionals to identify vulnerabilities in a network or for anyone looking to see what services are running on a particular host. In this blog post, we’ll go through the process of creating a simple port scanner in Python.
# Import pyfiglet for ASCII banner
import pyfiglet
# Import sys for using sys.exit
import sys
# Import the socket module
import socket
# Import date and time
from datetime import datetime
We first need to import the necessary modules that will be used in our port scanner. The pyfiglet
module is used to generate ASCII art banners, the sys
module is used to exit the program if needed, the socket
module is used for socket programming and the datetime
module is used to display the current date and time.
# ASCII Banner when starting program
ascii_banner = pyfiglet.figlet_format("SIMPLE PORT SCANNER")
print(ascii_banner)
print("DISCLAIMER: Only use this tool for targets you are allowed to scan!\n")
Next, we create an ASCII banner using the pyfiglet
module. This banner will be displayed when the program starts.
It’s important to note that port scanners can be used for malicious purposes, so it’s important to only use them on targets that you have permission to scan. In this case, we display a disclaimer reminding the user of this.
# User input for target address
target = input("Target host: ")
Next, we ask the user to input the target host that they want to scan. This can be either an IP address or a hostname.
# Banner when starting scan
# Line
print("-" * 50)
print("Scanning target: " + target)
print("Scanning started at: " + str(datetime.now()))
# Line
print("-" * 50)
We then display a banner indicating that the scan has started, along with the current date and time.
try:
# Scan ports between 1 to 500
for port in range(1,500):
# New socket using the default family socket (AF_INET)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# If connection successful to target port
result = s.connect_ex((target,port))
if result ==0:
print("Port {} is open".format(port))
s.close()
Now we come to the main part of the port scanner. We use a for
loop to iterate through the range of ports we want to scan (in this case, from 1 to 500). For each port, we create a new socket using the socket
module and try to establish a connection to the target host on that port. If the connection is successful, we print a message indicating that the port is open. If the connection is not successful, the port is assumed to be closed.
# Exit if keyboard interrupt
except KeyboardInterrupt:
print("\n Keyboard interrupt detected, terminating.")
sys.exit()
# Exit if invalid hostname
except socket.gaierror:
print("\n Hostname could not be resolved.")
sys.exit()
# Exit if host not responding
except socket.error:
print("\n Server not responding.")
sys.exit()
There are a few exceptions that we need to handle in our port scanner. If the user interrupts the scan with a keyboard interrupt, we exit the program. If the hostname could not be resolved, we also exit the program. And if the host is not responding, we exit the program as well.
Here is the entire code:
# Import pyfiglet for ASCII banner
import pyfiglet
# Import sys for using sys.exit
import sys
# Import the socket module
import socket
# Import date and time
from datetime import datetime
# ASCII Banner when starting program
ascii_banner = pyfiglet.figlet_format("SIMPLE PORT SCANNER")
print(ascii_banner)
print("DISCLAIMER: Only use this tool for targets you are allowed to scan!\n")
# User input for target address
target = input("Target host: ")
# Banner when starting scan
# Line
print("-" * 50)
print("Scanning target: " + target)
print("Scanning started at: " + str(datetime.now()))
# Line
print("-" * 50)
try:
# Scan ports between 1 to 500
for port in range(1,500):
# New socket using the default family socket (AF_INET)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# If connection successful to target port
result = s.connect_ex((target,port))
if result ==0:
print("Port {} is open".format(port))
s.close()
# Exit if keyboard interrupt
except KeyboardInterrupt:
print("\n Keyboard interrupt detected, terminating.")
sys.exit()
# Exit if invalid hostname
except socket.gaierror:
print("\n Hostname could not be resolved.")
sys.exit()
# Exit if host not responding
except socket.error:
print("\n Server not responding.")
sys.exit()
The output of the port scanner when running against Hack the Box machine Trick.
That’s it! With just a few lines of code, we’ve created a simple port scanner in Python. Of course, there are many more features that could be added to a port scanner, such as the ability to scan a range of IP addresses or to specify which ports to scan. But this basic version should give you a good idea of how a port scanner works and how to create one using Python.
The Simple Port Scanner can be downloaded from my GitHub repository: https://github.com/teemuhak/simple_portscanner.py