dpkt tutorial summary

本文介绍使用dpkt库解析以太网数据包的方法。dpkt是一个用Python编写的便携式包解析库,支持多种网络协议。文章详细解释了dpkt.ethernet模块中各个属性的作用,如data、dst、get_type等,并提供了读取pcap文件的示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原文:http://www.commercialventvac.com/dpkt.html#mozTocId305148

 

dpkt.ethernet.Ethernet

dpkt.ethernet.Ethernet has attributes 'data', 'dst', 'get_type', 'ip', 'pack', 'pack_hdr', 'set_type', 'src', 'type', 'unpack']

data

Contains the data payload of the ethernet packet.

dst

Contains the destination address of the ethernet packet as a 6 byte strings.

6 Byte Ethernet addresses can be converted to strings in format nn:nn:nn:nn:nn:nn with the function jeffs_dpkt.eth_addr_to_str()

get_type

Returns a class which is something from the Ethernet Type field

(Pdb) print eth._typesw.keys()
[2048, 8192, 34916, 2054, 34827, 33079, 8196, 34525]
(Pdb) print eth._typesw.values()
[<class 'dpkt.ip.IP'>, <class 'dpkt.cdp.CDP'>, <class 'dpkt.pppoe.PPPoE'>, <class 'dpkt.arp.ARP'>, <class 'dpkt.ppp.PPP'>, <class 'dpkt.ipx.IPX'>, <class 'dpkt.dtp.DTP'>, <class 'dpkt.ip6.IP6'>]
(Pdb) print eth.get_type(2048)
<class 'dpkt.ip.IP'>
(Pdb) print eth.get_type(34525)
<class 'dpkt.ip6.IP6'>
(Pdb)

src

Contains the source address of the ethernet packet as a 6 byte string.

type

Returns the Ethernet type.  For example, type 2048 (0x0800) is IPv4 and 34525 (0x86DD) is IPv6.  For a complete list of Ethernet types, refer to http://www.iana.org/assignments/ethernet-numbers

dpkt.ethernet.dpkt

['Error', 'NeedData', 'PackError', 'Packet', 'UnpackError', '_MetaPacket', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '__vis_filter', 'array', 'copy', 'hexdump', 'in_cksum', 'in_cksum_add', 'in_cksum_done', 'itertools', 'socket', 'struct']

dpkt.ethernet.stp
dpkt.ethernet.struct
dpkt.ip
dpkt.pcap.Reader(f)

dpkt.pcap.Reader(f) implements an iterator.  Each iteration returns a tuple which is a timestamp and a buffer.  The timestamp contains a time as a floating point number.  The buffer is a complete packet.  For example:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import dpkt
import sys


f = open(sys.argv[1])
pcap = dpkt.pcap.Reader(f)
frame_counter = 0
for ts, buf in pcap:
frame_counter += 1
if frame_counter > 1 :
print "%d: %f %f" % ( frame_counter, ts, ts - last_time )
last_time = ts

f.close()

 

转载于:https://www.cnblogs.com/lovemo1314/archive/2011/05/05/2038192.html

08-02
`dpkt` 是一个用于快速解析和构建网络数据包的 Python 库,其设计目标是轻量、高效且易于使用。以下是对 `dpkt` 的使用指南、安装方法和示例教程的详细说明。 ### 安装方法 `dpkt` 可以通过 Python 的包管理工具 `pip` 进行安装。确保你的系统中已安装 Python 和 pip,然后执行以下命令: ```bash pip install dpkt ``` 如果需要特定版本的 `dpkt`,可以使用如下命令指定版本号: ```bash pip install dpkt==1.9.7 ``` ### 使用指南 `dpkt` 的核心功能是解析和构建网络协议数据包。它支持多种常见的网络协议,如以太网帧、IP 数据包、TCP 和 UDP 报文等。以下是一些基本的使用方法。 #### 解析以太网帧 ```python import dpkt # 假设 eth_data 是一个包含以太网帧的字节流 eth = dpkt.ethernet.Ethernet(eth_data) # 获取以太网类型 eth_type = eth.type # 获取源 MAC 地址和目标 MAC 地址 src_mac = eth.src dst_mac = eth.dst ``` #### 解析 IP 数据包 ```python ip_packet = eth.data # 从以太网帧中提取 IP 数据包 # 获取 IP 地址 src_ip = ip_packet.src dst_ip = ip_packet.dst # 获取 IP 协议类型(如 TCP、UDP) protocol = ip_packet.p ``` #### 解析 TCP 报文段 ```python tcp_segment = ip_packet.data # 从 IP 数据包中提取 TCP 报文段 # 获取源端口和目标端口 src_port = tcp_segment.sport dst_port = tcp_segment.dport # 获取 TCP 标志位 flags = tcp_segment.flags ``` ### 示例教程 下面是一个完整的示例,展示如何读取一个 pcap 文件并解析其中的数据包: ```python import dpkt import socket def mac_addr(address): return ':'.join('%02x' % ord(b) for b in address) def analyze_pcap(file_path): with open(file_path, 'rb') as f: pcap = dpkt.pcap.Reader(f) for timestamp, buf in pcap: eth = dpkt.ethernet.Ethernet(buf) ip = eth.data tcp = ip.data # 获取源 IP 和目标 IP src_ip = socket.inet_ntoa(ip.src) dst_ip = socket.inet_ntoa(ip.dst) # 获取源端口和目标端口 src_port = tcp.sport dst_port = tcp.dport print(f"Timestamp: {timestamp}") print(f"Source MAC: {mac_addr(eth.src)}") print(f"Destination MAC: {mac_addr(eth.dst)}") print(f"Source IP: {src_ip}") print(f"Destination IP: {dst_ip}") print(f"Source Port: {src_port}") print(f"Destination Port: {dst_port}") print("-" * 40) # 调用函数分析 pcap 文件 analyze_pcap('example.pcap') ``` ### 注意事项 - `dpkt` 不提供发送或接收数据包的功能,这些操作通常由 `socket` 等标准库处理。 - 在处理二进制数据时,确保数据格式与 `dpkt` 的解析结构匹配。 - 如果需要更复杂的网络功能(如流量捕获或注入),可以结合 `scapy` 或 `pcapy` 等库使用。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值