How To Write A Simple Portscanner Using Python 3

How To Write A Simple Portscanner Using Python 3

This is a simple Python script that scans a target host for open ports using the socket module. By calling the portscanner() function with a port number as an argument, the script will check if the port is open or closed and print the result.

import socket

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = "192.168.1.8"
port = 443

def portscanner(port):
    if sock.connect_ex((host, port)):
        print("Port %d is closed" % (port))
    else:
        print("Port %d is opened" % (port))

portscanner(port)

Thesocket module in Python provides low-level networking interfaces for communication over various protocols, including TCP/IP. Thesocket module allows you to create socket objects, which represent endpoints in a network communication.

In the code above, we import the socket module using the import statement. This makes the functions and classes in the socket module available for use in our code.

We then create a socket object using the socket.socket() function, which takes two arguments: the address family and the socket type. The address family specifies the type of network protocol to use, and the socket type specifies the type of connection to establish. In this case, we use AF_INET for the address family, which specifies IPv4, and SOCK_STREAM for the socket type, which specifies a TCP connection.

By importing thesocket module and creating a socket object, we can use the socket object to establish a connection to a remote host and port and send and receive data over the network.

In the code above, we use theconnect_ex() method of the socket object to establish a TCP connection to the target host and port. Theconnect_ex() method takes a tuple of the form(host, port) as an argument, where host is the IP address of the target host and port is the port number to connect to.

If the connection is successful, connect_ex() returns 0. If the connection cannot be established, connect_ex() returns an error code. In the code above, we use an if statement to check the return valueconnect_ex(). If it is non-zero, we print the message "Port X is closed," where X is the port number. If it is zero, we print the message "Port X is opened.".

Finally, we define a functionportscanner() that takes a port number as an argument and prints the result of the port scan. We call this function with the port number to scan as the argument.

Overall, this code is a simple port scanner that checks if a specific port on a target host is open or closed. It can be useful for network administrators to diagnose network issues or for penetration testers to identify potential vulnerabilities in a network. However, it is important to note that port scanning can be illegal in some jurisdictions and should only be performed with the permission of the network owner.

In the next part I will be changing some of the code and improving the port scanner.

# Import the socket module
import socket

# Create a socket object. The first argument is the address family (AF_INET for IPv4)
# The second argument is the socket type (SOCK_STREAM for TCP)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# The IP address of the target host
host = "192.168.1.8"

# The port number to scan
port = 443

# Define a function that takes a port number as an argument
def portscanner(port):
    # Try to establish a TCP connection to the target host and port
    # connect_ex() returns None if the connection is successful, otherwise it returns an error code
    if sock.connect_ex((host, port)):
        # If connect_ex() returns an error code, it means the port is closed
        print("Port %d is closed" % (port))
    else:
        # If connect_ex() returns None, it means the port is opened
        print("Port %d is opened" % (port))

# Call the function with the port number to scan
portscanner(port)

Useful link: https://docs.python.org/3/library/socket.html