Python grab packet intercepts HTTP log

Source: Internet
Author: User
Tags ord

#!/usr/bin/python

Import Pcap

Import DPKT

Import re

def main ():

Pc=pcap.pcap (name= "eth1") # crawl Eth1

Pc.setfilter (' TCP port 80 ') # Filter expression TCP port 80

For P_time, p_data in PC: #

ret = Main_pcap (P_time, P_data)

IF RET:

Print ret

def main_pcap (P_time, P_data): # decoding

Out_format = "%s\t%s\t%s\t%s\t%s\thttp/%s"

p = dpkt.ethernet.Ethernet (p_data) #

RET = None

if p.data.__class__.__name__ = = ' IP ':

Ip_data = P.data

src_ip = '%d.%d.%d.%d '% tuple (map (ord,list (IP_DATA.SRC)))

dst_ip = '%d.%d.%d.%d '% tuple (map (ord,list (IP_DATA.DST)))

If p.data.data.__class__.__name__== ' TCP ':

Tcp_data = P.data.data

If tcp_data.dport==80:

If Tcp_data.data:

h = dpkt.http.Request (tcp_data.data) # HTTP decoding

Pre = "^/.*$"

If match (pre, H.uri): # URL Rewrite

Http_headers = H.headers

Host = h.headers[' host ']

url = "http +/" + Host + H.uri

Else

URL = H.uri

# datetime SRCIP dstip get/index.htm http/1.1 # output Log format

ret = out_format% (p_time, src_ip, dst_ip, H.method, URL, h.version)

return ret

Def match (pre, line):

p = re.compile (PRE)

m = P.match (line)

return m

# script run also reached the Xiaoming等 expected, OK.

# Follow-up record referer is still necessary.

======================================================================================

Installation

1. Python 2.5

2. Pypcap python's grab function library

Http://code.google.com/p/pypcap/downloads/list

3. Dpkt python's unpacking function library

Http://code.google.com/p/dpkt/downloads/list

4. WinPcap Python supports PACP driver

If you have Wireshark, just install Wireshark, inside with WinPcap

In Linux there is a library called Libpcap can be competent. Libpcap is a simple and powerful library of packet capture functions that can be run on a variety of operating systems.

There are a few good tutorials on Libpcap:

Http://www.tcpdump.org/pcap.htm (Official guide, English, well-written for beginners)

Http://blog.csdn.net/bat603/archive/2006/09/04/1175729.aspx (Chinese description of main function)

Http://blog.csdn.net/bat603/archive/2006/09/04/1176251.aspx (Entry Source)

Python grab packet intercepts HTTP log

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.