Skip to main content

Code a Simple HTTP Header Sniffer Using python

Namaste hackers,


                                  Today, In This Tutorial, I'm Gonna to Show You How We Can Capture HTTP Header Packet Using Python and Socket Module.



Introduction


Information Gathering is a very important concept in Profession Of Ethical Hacking and Penetration Testing. Basically, Web Sniffing is a very useful concept for testing any web application. So, Today We are going to learn a simple script that will help us to grab HTTP response header. Actually, This Simple script can help us in various ways like this Script can help us to examine any website cookies techniques, server information, information gathering, website scraping and much more.

Ok So, Let's Start Here,

How it's Going To Works


Basically, Friends With the help of this script, we only have to Sniff established HTTP connection between our client browser and online server.  Whenever Our client browser will send a request to the HTTP server. Of course after receiving a client request, the Online server will definitely response back and at the time, our script will sniff that response data and print it on the terminal. I hope now you got the basic structure of our method. now, it's time to implement it in code.

Example Code

first of all, we have to Capture packets from local sockets using python socket module and after capturing packets. we have to extract packets for filtering out our useful data.
Hmm, Not getting how it's going to work?

Let me explain you step by step.


  • 1st line is for importing socket module
  • 3rd line is creating socket handler for packet sniffing. this simple configuration will help us to sniff data from the local socket.
  • 5th line for loop
  • 6th line for reading packets from local socket
  • 8th line for verifying that extracted packet is HTTP Header
  • 10th line is for extracting the header from raw data.



I know This script looks very easy. So let's try it.

HTTP Header Sniffing Python Codes

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
import socket

s=socket.socket(socket.PF_PACKET, socket.SOCK_RAW, socket.ntohs(0x0800))

while True:
 data=s.recvfrom(65565)
 try:
  if "HTTP" in data[0][54:]:
    print "[","="*30,']'
    raw=data[0][54:]
    if "\r\n\r\n" in raw:
     line=raw.split('\r\n\r\n')[0]
     print "[*] Header Captured "
     print line[line.find('HTTP'):]
    else:
     print raw
  else:
   #print '[{}]'.format(data)
   pass
 except:
  pass

For Downloading This Script Click Here
I Hope You Enjoyed It.

For Any Question And Suggestion
Comment Below.

Comments

  1. Hello
    Please do a new tutorial for application layer sniffing in Python

    thanks

    ReplyDelete
  2. Ok..I will do it.


    By the way, I will encourage you to take a look here : http://www.bitforestinfo.com/2017/01/how-to-write-simple-packet-sniffer.html

    ReplyDelete
  3. I have seen that tutorial, its awesome, but, if i want to just get the information about a specific ip,then what is the process?

    ReplyDelete
    Replies
    1. Ok.. I will do it in Starting Feburary day... So, keep in touch.

      Thanks

      Delete
  4. No funciona ningĂșn script sniffing compae... Gracias

    ReplyDelete

Post a Comment

Related Post

Top Visited

Create Simple Packet Sniffer Using Python

how to install burp suite in Linux/Ubuntu 16.04

Big List Of Google Dorks For Sqli Injection

List of Keyboard Shortcuts Keys for GNOME Desktop (Kali linux / Linux / Ubuntu/*nix )

How to create Phishing Page Using Kali Linux | Webpage Page Cloning Using Kali Linux Social Engineering Toolkit

Latest Google Dorks List

Best 1000 User-agents List For Web Scraping

How To Install GDB Peda?

What is the use of Pseudo header in TCP/UDP packets?

How To Create Snake Game Using Python And Tkinter - Simple python games