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.

Share this

Related Posts

Previous
Next Post »

4 comments

Write comments
Anonymous
21 January 2018 at 12:59 delete

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

thanks

Reply
avatar
27 January 2018 at 12:25 delete

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

Reply
avatar
Anonymous
28 January 2018 at 09:23 delete

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?

Reply
avatar
28 January 2018 at 22:56 delete

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

Thanks

Reply
avatar