DPKT Packet Manipulation Library

1.9.8 · active · verified Sat Apr 11

DPKT is a fast, simple Python library for creating and parsing network packets, providing definitions for many common TCP/IP protocols like Ethernet, IP, TCP, and UDP. Its current stable version is 1.9.8, with releases occurring semi-regularly, focusing on bug fixes, protocol updates, and performance improvements.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a simple Ethernet/IP/ICMP packet, write it to a .pcap file using `dpkt.pcap.Writer`, and then read and parse that file using `dpkt.pcap.Reader`.

import dpkt
import datetime
import os

# Helper functions for printing (usually from dpkt.utils)
def mac_to_str(buf):
    return ':'.join('%02x' % b for b in buf)
def ip_to_str(buf):
    return '.'.join('%d' % b for b in buf)

# 1. Create a dummy pcap file for demonstration
output_pcap_file = 'test.pcap'
with open(output_pcap_file, 'wb') as f:
    writer = dpkt.pcap.Writer(f, linktype=dpkt.pcap.DLT_EN10MB)

    # Create a simple Ethernet frame with an IP packet and ICMP payload
    eth = dpkt.ethernet.Ethernet()
    eth.src = b'\x00\x11\x22\x33\x44\x55'
    eth.dst = b'\xAA\xBB\xCC\xDD\xEE\xFF'
    eth.type = dpkt.ethernet.ETH_TYPE_IP

    ip = dpkt.ip.IP()
    ip.src = b'\x7f\x00\x00\x01' # 127.0.0.1
    ip.dst = b'\x7f\x00\x00\x02' # 127.0.0.2
    ip.p = dpkt.ip.IP_PROTO_ICMP # Example protocol
    ip.data = dpkt.icmp.ICMP(type=dpkt.icmp.ICMP_ECHO, data=dpkt.icmp.ICMP.Echo(id=1, seq=1, data=b'Hello DPKT!'))
    
    # dpkt handles length calculation usually, but sometimes explicit setting helps
    ip.len = len(ip.data) + ip.__hdr_len__ 

    eth.data = ip

    # Write the packet to the pcap file with current timestamp
    writer.writepkt(eth.pack(), ts=datetime.datetime.now().timestamp())

print(f"Created '{output_pcap_file}' with a dummy packet.")

# 2. Now, read and parse the pcap file
try:
    with open(output_pcap_file, 'rb') as f:
        # Use dpkt.pcap.UniversalReader(f) for auto-detection of PCAP/PCAPNG
        pcap_reader = dpkt.pcap.Reader(f)

        print(f"\nParsing packets from '{output_pcap_file}':")
        for timestamp, buf in pcap_reader:
            print(f'Timestamp: {str(datetime.datetime.fromtimestamp(timestamp))}')
            eth = dpkt.ethernet.Ethernet(buf)
            print(f'  Ethernet Frame: {mac_to_str(eth.src)} -> {mac_to_str(eth.dst)}')

            if eth.type == dpkt.ethernet.ETH_TYPE_IP:
                ip_packet = eth.data
                print(f'  IP Packet: {ip_to_str(ip_packet.src)} -> {ip_to_str(ip_packet.dst)}, Proto: {ip_packet.p}')
                
                if ip_packet.p == dpkt.ip.IP_PROTO_ICMP:
                    icmp_packet = ip_packet.data
                    if isinstance(icmp_packet, dpkt.icmp.ICMP) and isinstance(icmp_packet.data, dpkt.icmp.ICMP.Echo):
                        print(f'    ICMP Echo Request: ID={icmp_packet.data.id}, Seq={icmp_packet.data.seq}, Data={repr(icmp_packet.data.data)}')

            else:
                print(f'  Non-IP Packet (Type: {hex(eth.type)})')

finally:
    # Clean up the dummy file
    if os.path.exists(output_pcap_file):
        os.remove(output_pcap_file)
        print(f"\nCleaned up '{output_pcap_file}'.")

view raw JSON →