DEVELOPING A PORTSCANNER IN PYTHON
Developing a port scanner in python
Port scanning is a scanning method for determining which ports on a network device are open, whether it's a server, a router, or a regular machine. A port scanner is just a script or a program that is designed to probe a host for open ports.
In this tutorial, you will be able to make your own port scanner in Python using the socket
library. The basic idea behind this simple port scanner is to try to
connect to a specific host (website, server, or any device connected to
the Internet/network) through a list of ports. If a successful
connection has been established, that means the port is open.
For instance, when you loaded this web page, you made a connection to this website on port 80. Similarly, this script will try to connect to a host but on multiple ports. These kinds of tools are useful for hackers and penetration testers, so don't use this tool on a host that you don't have permission to test!
Optionally, you need to install colorama
module for printing in colors:
pip3 install colorama
Simple Port Scanner
-
First, let's start by making a simple port scanner. Let's import the
socket
module:Note:
socket
module is already installed on your machine, it is a built-in module in the python standard library, so you don't have to install anything.The socket module provides us with socket operations, functions for network-related tasks, etc. They are widely used on the Internet, as they are behind any connection to any network. Any network communication goes through a socket. More details are in the official Python documentation.
We will use
colorama
here just for printing in green colors whenever a port is open, and gray when it is closed.Let's define the function that is responsible for determining whether a port is open:
s.connect((host, port))
function tries to connect the socket to a remote address using the(host, port)
tuple, it will raise an exception when it fails to connect to that host, that is why we have wrapped that line of code into a try-except block, so whenever an exception is raised, that's an indication for us that the port is actually closed, otherwise it is open.Now let's use the above function and iterate over a range of ports:
The above code will scan ports ranging from 1 all the way to 1024, you can change the range to 65535 if you want, but that will take longer to finish.
When you try to run it, you'll immediately notice that the script is quite slow. Well, we can get away with that if we set a timeout of 200 milliseconds or so (using
settimeout(0.2)
method). However, this actually can reduce the accuracy of the reconnaissance, especially when your latency is quite high. As a result, we need a better way to accelerate this.Fast (Threaded) Port Scanner
Now let's take our simple port scanner to a higher level. In this section, we'll write a threaded port scanner that can scan 200 or more ports simultaneously.
The code below is actually the same function we saw previously, which is responsible for scanning a single port. Since we're using threading, we need to use a lock so only one thread can print at a time. Otherwise, the output will be messed up, and we won't read anything useful:
So this time, the function doesn't return anything; we just want to print whether the port is open (feel free to change it, though).
We used
Queue()
class from the built-in queue module that will help us with consuming ports, the two below functions are for producing and filling up the queue with port numbers and using threads to consume them:
Comments
Post a Comment