Dpkt tutorial #2: parsing a pcap File

Source: Internet
Author: User
Dpkt tutorial #2: parsing a pcap File

As we showed in the first dpkt tutorial, dpkt makes it simple to construct packets. dpkt is equally useful for parsing packets and files, so in this second tutorial we will demonstrate parsing a pcap file and the packets contained within it.

Dpkt is a sweet framework for creating and parsing packets. while dpkt doesn' t have much documentation, once you get the hang of using one module, the rest fall into place fairly easily. i'll be doing a number of dpkt tutorials with simple tasks in hopes of providing some "documentation by example ". if you have any tasks you 'd like to see done in dpkt, drop me a line.

In this tutorial, we'll not only show how to parse the raw pcap file format, but also how to parse the packets contained in the pcap file. Let's get started!

Let's parse a previusly captured pcap file, test. pcap, that contains some HTTP sessions. if we look at the dpkt/pcap. PY module, we see that it offers a reader class that takes a file object and exposes a similar interface to pypcap for reading records from the file. let's first open test. pcap with the reader class:

f = open('test.pcap')pcap = dpkt.pcap.Reader(f)

We can now interate through the pcap object and access each packet contained in the dump. For example, we can print out the timestamp and packet data length of each record:

>>> for ts, buf in pcap:>>>     print ts, len(buf)1220901348.61 661220901348.68 66...

Of course, it wocould be a lot more useful to parse the packet data into a more friendly, usable form. using dpkt, we can simply pass a raw buffer to the appropriate dpkt class and have its contents automatically parsed and decoded into friendly Python objects:

for ts, buf in pcap:    eth = dpkt.ethernet.Ethernet(buf)

Passing the packet data to dpkt's Ethernet class will parse and decode it into the ETH object. since dpkt's Ethernet class also contains some extra magic to parse higher layer protocols that are recognized, we see that both the IP and TCP layer information has been decoded as well:

>>> print ethEthernet(src='\x00\x1a\xa0kUf', dst='\x00\x13I\xae\x84,', data=IP(src='\xc0\xa8\n\n',off=16384, dst='C\x17\x030', sum=25129, len=52, p=6, id=51105, data=TCP(seq=9632694,off_x2=128, ack=3382015884, win=54, sum=65372, flags=17, dport=80, sport=56145)))

As we can see from the output, Eth is the Ethernet object, Pkt. data is the IP object, and Pkt. data. data is the TCP object. we can assign references to these objects in a more friendly manner:

ip = eth.datatcp = ip.data

We can then examine the attributes of the various objects as usual. For example, we can look at the Source and Destination Ports of the TCP Header:

>>> print tcp.sport56145>>> print tcp.dport80

Of course, since we know that this packet dump contains HTTP sessions, we may also want to parse beyond the TCP layer and decode the HTTP requests. to do so, we'll ensure that our destination port is 80 (indicating a request as opposed to a response) and that there is data beyond the TCP layer available for parsing. we'll use dpkt's HTTP decoder to parse the data:

if tcp.dport == 80 and len(tcp.data) > 0:    http = dpkt.http.Request(tcp.data)

Once the HTTP payload has been parsed, we can examine its varous attributes:

>>> print http.methodGET>>> print http.uri/testurl>>> print http.version1.1>>> print http.headers['user-agent']Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.0.5)

For the purposes of our tutorial program, we'll just output the HTTP. Uri attribute.

And that concludes our tutorial for parsing a pcap file and the packets within it. in just 10 simple lines of Python, we 've created a powerful tool that reads the raw pcap file, parses and decodes the Ethernet, IP, TCP, and HTTP layers, and prints out the URI of the HTTP requests.

The full Python script for this tutorial follows:

#!/usr/bin/env pythonimport dpktf = open('test.pcap')pcap = dpkt.pcap.Reader(f)for ts, buf in pcap:    eth = dpkt.ethernet.Ethernet(buf)    ip = eth.data    tcp = ip.data    if tcp.dport == 80 and len(tcp.data) > 0:        http = dpkt.http.Request(tcp.data)        print http.urif.close()

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.