Port scanning with sockets
In the same way that we have tools such as Nmap to analyze the ports that a machine has open, with the socket
module, we could implement similar functionality to detect open ports in order to later detect vulnerabilities in a service that is open on said server.
In this section, we'll review how we can implement port scanning with sockets. We are going to implement a basic port scanner for checking each port in a hardcoded port list and another where the user enters the port list that he regards as interesting to analyze.
Implementing a basic port scanner
Sockets are the fundamental building block for network communication, and by calling the connect_ex()
method, we can easily test whether a particular port is opened, closed, or filtered.
For example, we could implement a function that accepts as parameters an IP address and a port list, and returns for each port whether it is open or closed.
In the following example, we are implementing...