Day 1: Port Banner Grabber (Python)

hangerio(56)
Published in
Python
Words
161
Reading
1 min
Listen
Play
1y

Banner Grabber collects information about computer system on a network or service running on a its open ports. The python code for this service is given below:


LEARN.png

Customiezed on Canva

To collect information about a system we need a python library **socket**. > import socket

The open ports we want to see.

COMMON_PORTS = [22, 80, 443]

Then a function to do our job:

def grab_banner(ip, port):
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # IPv4 TCP socket
sock.settimeout(5) # Set a 5-second timeout
sock.connect((ip, port))
try:
banner = sock.recv(1024).decode().strip()
print(f"[+] Port {port} Banner: {banner}")
except:
print(f"[+] Port {port} is open but no banner received")
sock.close()
except Exception as e:
print(f"[-] Port {port} is closed or filtered ({e})")

To run the grab_banner function, we have created a main function, because all other languages contain a main function.

if name == "main":
ip = input("Enter target IP or hostname: ").strip()
print(f"\nScanning {ip}...\n")
for port in COMMON_PORTS:
grab_banner(ip, port)

Use command to run program:

python3 program_name.py

image.png

Day 1: Port Banner Grabber (Python) | Ecency