Build a Python Banner Grabber for Your Own Lab (Full Code)


Connect to an open port and most services will introduce themselves. A web server says “Apache 2.4.41”. An SSH server says “OpenSSH 8.9”. A mail server says its name and version. This announcement is called a banner — and reading it is called banner grabbing.

Attackers grab banners to find outdated software worth attacking. Defenders grab banners to find outdated software worth patching. Same technique, opposite purpose. Today you’ll build the tool — and use it on your own lab only.

The Code

import socket
def grab_banner(host, port, timeout=3):
"""Connect to a port and read whatever the service sends first."""
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(timeout)
sock.connect((host, port))
# Some services speak first; others need a nudge
try:
banner = sock.recv(1024)
except socket.timeout:
banner = b""
if not banner:
# Send a newline — many services reply to any input
sock.sendall(b"\r\n")
banner = sock.recv(1024)
sock.close()
return banner.decode(errors="ignore").strip()
except (socket.timeout, ConnectionRefusedError, OSError) as e:
return f"[no banner: {type(e).__name__}]"
def scan_host(host, ports):
print(f"--- {host} ---")
for port in ports:
banner = grab_banner(host, port)
print(f"Port {port}: {banner if banner else '[open, silent]'}")
if __name__ == "__main__":
# YOUR OWN machines only — never scan targets you don't own
target = "192.168.1.1" # your router, your lab VM, your own server
common_ports = [21, 22, 23, 25, 53, 80, 110, 143, 443, 3306, 8080]
scan_host(target, common_ports)

How It Works

  1. Connect — socket.connect() opens a TCP connection to the target port, exactly like a browser or SSH client would.
  2. Listen first — many services (SSH, FTP, SMTP) send their banner the moment you connect, before you say anything.
  3. Nudge if silent — web servers and some others wait for input, so we send a newline and read the reply.
  4. Decode safely — banners can contain weird bytes, so errors=”ignore” keeps the script from crashing.

Try It on Your Lab

Point it at your own router, your home lab VM, or a virtual machine you built for practice. You’ll see real banners like:

Port 22: SSH-2.0-OpenSSH_8.9p1 Ubuntu-3ubuntu0.6
Port 80: [open, silent]
Port 443: [open, silent]

That SSH banner tells you the exact software version — which is exactly what an attacker checks against vulnerability databases. If your version is old, that’s your patch list.

What Banners Reveal (and Why It Matters)

  • Outdated software — a banner saying Apache 2.4.41 tells an attacker which CVEs to try. Keep software updated and banners become boring.
  • Unnecessary services — a banner on port 23 (Telnet) means an ancient, unencrypted service is running. Close it.
  • Information leakage — some banners include OS details and internal hostnames. Configure your services to send minimal banners where possible.

Level Up

  • Add a –help flag and accept the target from the command line with argparse.
  • Save results to a file with timestamps, so you can diff your network week to week and spot new services.
  • Combine it with the port scanner from an earlier post — scan first, grab banners second.
  • Learn nmap -sV — the professional version of what you just built. Now you understand what it does under the hood.

The Rule, Again

Only scan systems you own or have written permission to test. Banner grabbing a stranger’s server is unauthorized access in many countries — and it’s exactly the kind of reconnaissance attackers do before an attack. Build the skill on your own lab, then use it to defend your own network.

You just built a reconnaissance tool. Use it like a defender.

Comments

Popular Posts