赞
踩
一、协议组成:pcap协议、IP协议、MAC协议、UDP协议
1、头文件引入
- #!/usr/bin/env python
- # -*- coding:UTF-8 -*-
- from __future__ import division
- import sys
- from collections import OrderedDict
- import struct
2、pcap报文头:24字节
- pcap_header = OrderedDict([
- # 4字节 pcap文件的magic num 目前为0xD4C3B2A1
- ('magic', ['unsigned int', 1]),
- # 2字节 主版本号 #define PCAP_VERSION_MAJOR 2
- ('version_major', ['unsigned short', 1]),
- # 2字节 次版本号 #define PCAP_VERSION_MINOR 4
- ('version_minor', ['unsigned short', 1]),
- # 4字节 时区修正 未使用,目前全为0
- ('this_zone', ['unsigned int', 1]),
- # 4字节 精确时间戳 未使用,目前全为0
- ('sig_figs', ['unsigned int', 1]),
- # 4字节 抓包最大长度 如果要抓全,设为0x0000ffff(65535),tcpdump -s 0就是设置这个参数,缺省为68字节
- ('snap_len', ['unsigned int', 1]),
- # 4字节 链路类型 一般都是1:ethernet
- ('link_type', ['unsigned int', 1])
- ])

3、数据包头:16字节
- # 数据包头 16字节
- packet_header = OrderedDict([
- # struct timeval ts 8字节 抓包时间 4字节表示秒数,4字节表示微秒数
- ('time_ms', ['unsigned int', 1]),
- ('time_ns', ['unsigned int', 1]),
- # 4字节 保存下来的包长度(最多是snap_len,比如68字节)
- ('cap_len', ['unsigned int', 1]),
- # 4字节 数据包的真实长度,如果文件中保存的不是完整数据包,可能比cap_len大
- ('len', ['unsigned int', 1]),
- ])
4、mac报文头:14字节
- # 14字节
- mac_header = OrderedDict([
- ('dst_mac', ['char[]', 6]),
- ('src_mac', ['char[]', 6]),
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。