Open In App

How to Get Open Port Banner in Python

Last Updated : 03 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will see how to Get Open Port Banner in Python. Here we will discuss 

What is Port?

Port is a terminology used on computer networks. You might know, that whenever you connect to the internet, your system or any device to which you are connecting is assigned an IP address.  So, when you search for something on the internet, your IP address is sent to the server where your page or response resides. After getting a request, the server sends back the desired page to your system. Now, suppose you have opened various tabs and gone to different websites, like YouTube or chrome or Amazon or any website. Then here, how the computer knows which request is for which tab, this is done with the help of a port number.  Ports are nothing but logical entities used for our system to make connections with the network. There are port ranges and each port functions for a particular work. The port ranges from 1 to 65535. The ports range from 49152 to 65535 and are used by client browsers. So, when you request something on the network, a port number between 49152 and 65535 is assigned and that is unique. This port number can be reassigned again, once the session is closed.

What is Banner? 

Banner is the description that the server returns. It contains a description of the host system like software type, system version, etc. This banner must be kept hidden as attackers or hackers can use this banner to attack by exploiting any loop while using the system description. And banner grabbing is nothing but getting the banner on a system on the network and banner grabbing is a crime if practiced without required permission.

What is Open Port?

Ports are open and closed. If a port is not open, we won’t be able to make a connection between two systems. So, when we do any activity on the network, the required ports are open and, as a result, we get the response in our system. Now, after discussing these three keywords, you will be able to understand what we are actually trying to do. To get an open port banner we will use a socket module. The socket is a way of connecting two nodes on a network to communicate with each other. One node listens on a particular port at an IP, while the other socket reaches out to the other to form a connection. The server forms the listener socket while the client reaches out to the server. 

Example 1

First, let’s see how we can see which ports are open for our system for that we are using threading for fast computation. We can get the IP address of the localhost by passing the host variable to the gethostbyname() function of the socket module. Here AF_INET specifies the IP address is IPV4(Internet Protocol version 4) and SOCKET_STREAM  specifies it’s a TCP socket.  Next, we have set the status=False, which will be true whenever we make a connection. connect() function is used to make connections between host_ip and port. 

Python3




import socket
import threading
import time
  
# function to scan ports and see which ports are open
def scan_port(port):
    # we will check port of localhost
    host = "localhost"
    host_ip = socket.gethostbyname(host)
      
    # print("host_ip = {}".format(host_ip))
    status = False
  
    # create instance of socket
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  
    # connecting the host ip address and port
    try:
        s.connect((host_ip, port))
        status = True
    except:
        status = False
  
    if status:
        print("port {} is open".format(port))
  
  
start_time = time.time()
  
for i in range(0, 100000):
    thread = threading.Thread(target=scan_port, args=[i])
    thread.start()
  
end_time = time.time()
print("To all scan all ports it took {} seconds".format(end_time-start_time))


How to Get Open Port Banner in Python

 

Example 2

Here you can see we have added s.recv(1024).decode(). This means the socket will return the banner in 1024 bytes of buffer size and then we decode it to a string. Now to get banners on these open ports, we need to add just one more line after making the connection, banner = s.recv(1024).decode().

Python3




import socket
import threading
import time
  
def scan_port(port):
    try:
        host = "localhost"
        host_ip = socket.gethostbyname(host)
        status = False
  
        # create instance of socket
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  
        # connecting the host ip address and port
        s.connect((host_ip, port))
        try:
            banner = s.recv(1024).decode()
            print("port {} is open with banner {}".format(port, banner))
  
        except:
            print("port {} is open ".format(port))
  
    except:
        pass
  
  
start_time = time.time()
  
for i in range(0, 100000):
    thread = threading.Thread(target=scan_port, args=[i])
    thread.start()
  
end_time = time.time()
print("To scan all ports it took {} seconds".format(end_time-start_time))


Output:

How to Get Open Port Banner in Python

We can see that port 22 is open with its banner information.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads