Banner grabbing with the socket module

Banners expose information related to the name of the web server and the version that is running on the server. Some expose the backend technology (PHP, Java, Python) that's  used and its version. With the socket module, we can get information related to the version server for a specific domain.

The simplest way to obtain the banner of a server is by using the socket module. We can send a get request and get the response through the recvfrom() method, which would return a tuple with the result.

You can find the following code in the socket_BannerGrabbing.py file:

#!/usr/bin/python3

import socket
import re

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(("www.packtpub.com", 80))

http_get = b"GET / HTTP/1.1 Host: www.packtpub.com "
data = ''
try:
sock.sendall(http_get)
data = sock.recvfrom(1024)
strdata = data[0]
headers = strdata.splitlines()
for header in headers:
print(header.decode())
except socket.error:
print ("Socket error", socket.errno)
finally:
print("closing connection")
sock.close()

This is the output of the socket_BannerGrabbing.py script over the packtpub.com domain for getting information about the server:

HTTP/1.1 301 https://www.packtpub.com/                   Location: https://www.packtpub.com/
Accept-Ranges: bytes
Date: Fri, 15 Feb 2019 14:17:02 GMT
Age: 0
Via: 1.1 varnish
Connection: close
X-Country-Code: NL
Server: packt

In the next section, we are going to study a specific use case for port scanning in a specific IP address or domain.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset