Passive DNS mining from PCAP with dpkt & Python

Update 04/14: A friend pointed me to dnssnarf, a project that looks like it was written at a DojoSec meeting by Christopher McBee and then updated a bit later on by Grant Stavely. It uses Scapy (which I hear is really neat if you haven’t played with it). Check Grant’s blog post about dnssnarf out.

So, here is another quickie in case anyone needs it out there in the Intertubes. Say you have a .pcap file, or many .pcap files, and you want to mine the DNS responses out of them so you can build up a passive DNS database and track malicious resolutions to build a list of ban-able IP addresses. This script aims to parse a given .pcap file (tcpdump/wireshark libpcap format) and returns the results of the query types you have interest in.

This script is built around dpkt, a tool by Dug Song, and the contents are heavily inspired by the tutorials present at Jon Oberheide’s site (also a developer of dpkt). Honestly, most of the time writing this was spent understanding how dpkt handled its internal data structures and how to get to the data. The documentation on dpkt is not the most mature, but the source is pretty readable, if you keep the references I mention in the comments at hand. Also, this script was only tested with Python 2.6 and dpkt 1.7 on Linux, it was confirmed to not work on Windows as dpkt appears to have some serious problems with Windows at the moment.

#!/usr/bin/env python

import dpkt, socket, sys

if len(sys.argv) < 2 or len(sys.argv) > 2:
 print "Usage:\n", sys.argv[0], "filename.pcap"

f = open(sys.argv[1])
pcap = dpkt.pcap.Reader(f)

for ts, buf in pcap:
 # make sure we are dealing with IP traffic
 # ref:
 try: eth = dpkt.ethernet.Ethernet(buf)
 except: continue
 if eth.type != 2048: continue
 # make sure we are dealing with UDP
 # ref:
 try: ip =
 except: continue
 if ip.p != 17: continue
 # filter on UDP assigned ports for DNS
 # ref:
 try: udp =
 except: continue
 if != 53 and udp.dport != 53: continue
 # make the dns object out of the udp data and check for it being a RR (answer)
 # and for opcode QUERY (I know, counter-intuitive)
 try: dns = dpkt.dns.DNS(
 except: continue
 if dns.qr != dpkt.dns.DNS_R: continue
 if dns.opcode != dpkt.dns.DNS_QUERY: continue
 if dns.rcode != dpkt.dns.DNS_RCODE_NOERR: continue
 if len( < 1: continue
 # now we're going to process and spit out responses based on record type
 # ref:
 for answer in
   if answer.type == 5:
     print "CNAME request",, "\tresponse", answer.cname
   elif answer.type == 1:
     print "A request",, "\tresponse", socket.inet_ntoa(answer.rdata)
   elif answer.type == 12:
     print "PTR request",, "\tresponse", answer.ptrname

10 thoughts on “Passive DNS mining from PCAP with dpkt & Python

  1. Without doing too much investigating, it seems that not all A responses come back with 4 bytes, making socket.inet_ntoa throw an error on some pcaps.


    if len(answer.rdata) == 4:

    before attempting to print the output appears to fix it.

    • Lou,

      First, sorry for the delay in response.

      Thanks for the tip. I’m trying to figure out why an A record wouldn’t be 4-bytes. Do you have a sample you can show me? I haven’t found this on our network at work, which is pretty large, but that’s not saying it couldn’t happen.

      There is mention on the Wikipedia page here of some RFC’s that might explain the issue, Notably, they talk about “Returns a 32-bit IPv4 address, most commonly used to map hostnames to an IP address of the host, but also used for DNSBLs, storing subnet masks in RFC 1101, etc.”. A subnet mask should still be 4 bytes. But in reading the RFC 1035, I came across this in section 3.4.1:

      Hosts that have multiple Internet addresses will have multiple A

      Maybe you’re seeing packets with multiple A record addresses in one response? I haven’t tested that, I might have to iterate over answer.rdata, maybe it comes back as an array if there are multiple responses or something. Will test it and report back here.

      An example of this:

      8 0.658817 DNS Standard query response CNAME A A A A


      31 242.110444 DNS Standard query response A A A A A A


      21 106.408197 DNS Standard query response A

      Thanks again!

  2. Traceback (most recent call last):
    File “./”, line 12, in
    for ts, buf in pcap:
    File “build/bdist.linux-i686/egg/dpkt/”, line 141, in __iter__
    File “build/bdist.linux-i686/egg/dpkt/”, line 75, in __init__

  3. Thanks! you really helped me in a project I’m working on!

    Here is a question for you – I need to generate a list of all domain-names and their corresponding IP-addresses from a pcap file, so from your example I used this code:
    if answer.type == 1:
    print “A request”,, “\tresponse”, socket.inet_ntoa(answer.rdata)

    The problem is that is not good enough for me, because I need the original domain name requested, and not its’ CNAME representation. For example, one of the original DNS requests was for, but the CNAME representation of it is

    I looked closely at your code and on dpkt’s, and realized we are extracting the information from the DNS Response (and not the query).
    I looked at the response packet in wireshark and saw that the original domain is there, under ‘queries’ and under ‘answers’, so my question is how can I extract it?


    • Matan,

      I’m glad you found it helpful. If you are still working with my code directly, you could acquire the name from the Questions section of the DNS response via the dns.qd object provided by dpkt.dns. So if you wanted to get the question name(s) you would simply do this:

      for qname in dns.qd:

      Or whatever else you’d like to do with it. Thanks for reading and commenting.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s