【问题标题】:How to parse packets in a python library? [closed]如何解析python库中的数据包? [关闭]
【发布时间】:2011-06-24 07:10:27
【问题描述】:

如何使用 python 解析来自 .pcap 文件或接口的数据包?

我正在专门寻找一种使用有据可查的库的解决方案。

【问题讨论】:

  • 嗨 PSS,你是如何完成你的项目的?你跟着哪个走近?
  • 既然可以清楚地编辑,为什么要关闭它?
  • 要获得更新的答案:试试 Kaitai Struct kaitai.io 一个新兴的强大、快速的二进制解析器。 pythonistac.wordpress.com/2017/03/09/…

标签: python tcp pcap


【解决方案1】:

试试scapy。它是一个非常强大的数据包检查、操作和创建程序。

您可以使用它来build your own tools

【讨论】:

  • github.com/phaethon/scapy查看新版本的scapy。它与 python3 兼容并包含新功能。
  • 请注意,对于 python 库而言,scapy 是 GPLv2 的异常情况——注意不是库豁免,因此键入 import scapy 会强制您的代码为 GPLv2 - 如果您对此感到满意,那就去吧。
【解决方案2】:

pycapfile 也可以使用。链接到piphttps://pypi.python.org/pypi/pypcapfile

【讨论】:

    【解决方案3】:

    我推荐你使用 Pyshark。这是 tshark 的包装。它还支持所有的 tshark 过滤器、解码器库……并且易于使用! 这是一个用于解析 .pcap 文件和实时捕获的绝佳软件包

    https://pypi.python.org/pypi/pyshark

    示例代码(来自链接):

    import pyshark
    cap = pyshark.FileCapture('/root/log.cap')
    cap
    >>> <FileCapture /root/log.cap>
    print cap[0]
    Packet (Length: 698)
    Layer ETH:
            Destination: BLANKED
            Source: BLANKED
            Type: IP (0x0800)
    Layer IP:
            Version: 4
            Header Length: 20 bytes
            Differentiated Services Field: 0x00 (DSCP 0x00: Default; ECN: 0x00: Not-ECT (Not ECN-Capable Transport))
            Total Length: 684s
            Identification: 0x254f (9551)
            Flags: 0x00
            Fragment offset: 0
            Time to live: 1
            Protocol: UDP (17)
            Header checksum: 0xe148 [correct]
            Source: BLANKED
            Destination: BLANKED
      ...
    dir(cap[0])
    ['__class__', '__contains__', '__delattr__', '__dict__', '__dir__', '__doc__', '__format__', '__getattr__', '__getattribute__', '__getitem__', '__getstate__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setstate__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_packet_string', 'bssgp', 'captured_length', 'eth', 'frame_info', 'gprs-ns', 'highest_layer', 'interface_captured', 'ip', 'layers', 'length', 'number', 'pretty_print', 'sniff_time', 'sniff_timestamp', 'transport_layer', 'udp']
    cap[0].layers
    [<ETH Layer>, <IP Layer>, <UDP Layer>, <GPRS-NS Layer>, <BSSGP Layer>]
    ....
    

    【讨论】:

      【解决方案4】:

      我试过了,然后又尝试了 pcapy。我选择 pcapy 是因为我的使用类似于我在谷歌上搜索到的示例。

      http://snipplr.com/view/3579/live-packet-capture-in-python-with-pcapy/(或查看下面复制的相同代码)

      import pcapy
      from impacket.ImpactDecoder import *
      
      # list all the network devices
      pcapy.findalldevs()
      
      max_bytes = 1024
      promiscuous = False
      read_timeout = 100 # in milliseconds
      pc = pcapy.open_live("name of network device to capture from", max_bytes, 
          promiscuous, read_timeout)
      
      pc.setfilter('tcp')
      
      # callback for received packets
      def recv_pkts(hdr, data):
          packet = EthDecoder().decode(data)
          print packet
      
      packet_limit = -1 # infinite
      pc.loop(packet_limit, recv_pkts) # capture packets
      

      【讨论】:

        猜你喜欢
        • 2012-03-19
        • 1970-01-01
        • 1970-01-01
        • 2014-04-07
        • 1970-01-01
        • 1970-01-01
        • 2011-01-15
        • 2013-12-31
        • 1970-01-01
        相关资源
        最近更新 更多