当前位置:   article > 正文

Python解析pcap包——UDP数据包_python pcap

python pcap

一、协议组成:pcap协议、IP协议、MAC协议、UDP协议

1、头文件引入

  1. #!/usr/bin/env python
  2. # -*- coding:UTF-8 -*-
  3. from __future__ import division
  4. import sys
  5. from collections import OrderedDict
  6. import struct

2、pcap报文头:24字节

  1. pcap_header = OrderedDict([
  2. # 4字节 pcap文件的magic num 目前为0xD4C3B2A1
  3. ('magic', ['unsigned int', 1]),
  4. # 2字节 主版本号 #define PCAP_VERSION_MAJOR 2
  5. ('version_major', ['unsigned short', 1]),
  6. # 2字节 次版本号 #define PCAP_VERSION_MINOR 4
  7. ('version_minor', ['unsigned short', 1]),
  8. # 4字节 时区修正 未使用,目前全为0
  9. ('this_zone', ['unsigned int', 1]),
  10. # 4字节 精确时间戳 未使用,目前全为0
  11. ('sig_figs', ['unsigned int', 1]),
  12. # 4字节 抓包最大长度 如果要抓全,设为0x0000ffff(65535),tcpdump -s 0就是设置这个参数,缺省为68字节
  13. ('snap_len', ['unsigned int', 1]),
  14. # 4字节 链路类型 一般都是1:ethernet
  15. ('link_type', ['unsigned int', 1])
  16. ])

3、数据包头:16字节

  1. # 数据包头 16字节
  2. packet_header = OrderedDict([
  3. # struct timeval ts 8字节 抓包时间 4字节表示秒数,4字节表示微秒数
  4. ('time_ms', ['unsigned int', 1]),
  5. ('time_ns', ['unsigned int', 1]),
  6. # 4字节 保存下来的包长度(最多是snap_len,比如68字节)
  7. ('cap_len', ['unsigned int', 1]),
  8. # 4字节 数据包的真实长度,如果文件中保存的不是完整数据包,可能比cap_len大
  9. ('len', ['unsigned int', 1]),
  10. ])

 4、mac报文头:14字节

  1. # 14字节
  2. mac_header = OrderedDict([
  3. ('dst_mac', ['char[]', 6]),
  4. ('src_mac', ['char[]', 6]),
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/200815
推荐阅读
相关标签
  

闽ICP备14008679号