Port scanning with sockets
We have tools such as Nmap for checking ports that a machine has open. We could implement similar functionality to detect open ports with vulnerabilities on a target machine using the socket
module.
In this section, we’ll review how we can implement port scanning with sockets. We are going to implement a port scanner for checking the ports introduced by the user.
Implementing a 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.
The following Python code lets you search for open ports on a local or remote host. The script scans for selected ports on a given user-entered IP address and reflects the open ports back to the user. If the port is locked, it also reveals the reason for that.
You can find the following code in the socket_port_scanner.py
file inside the port_scanning
folder...