Friday, April 23, 2021

Published on Saturday, September 05, 2015

Simple Banner-Grabbing Python Script

This script came about from me playing with similar examples in two books that I own: Black Hat Python (by Justin Seitz) and Violent Python (by TJ O'Connor). It's sort of a combination of ideas from both, with some other tweaks from looking about the web and trying things myself.

import socket
host = "" #any site name or IP address
port = 80
addr = (host, port)
s = socket.socket()
s.connect (addr)
s.send ("GET / HTTP/1.0\r\nHost: " + host + "\r\n\r\n")
print s.recv (1024)

1024 should be enough to grab the banner, which is where you'll get useful stuff like server type, etc. You can increase it to 2048 or 4096, or just repeat the last line to get more from the connection, which is still open. Ideally, when you are finished, you should really do a s.close() rather than let the connection time out.

Note that the line s = socket.socket() is relying on the default settings for this, which is:

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

This is the default for an IPv4 TCP connection. As sites increasingly move to IPv6, this default may not give the results you expect.

Comments (0)Number of views (4561)

Author: Exweeto

Categories: Programming, Python



Please login or register to post comments.

“If I had eight hours to chop down a tree, I'd spend the first six of them sharpening my axe.”

Abraham Lincoln

Copyright 2021 by Exweeto Terms Of Use Privacy Statement