代码之家  ›  专栏  ›  技术社区  ›  Bhakta Raghavan

分析数据包捕获:什么是正确的方法[关闭]

  •  0
  • Bhakta Raghavan  · 技术社区  · 7 年前

    我的目标是构建一个数据包捕获分析器:

    输入: pcap文件(或任何捕获文件)。该文件可能包含数百/数千个数据包。

    输出 :关于流量流的大量信息

    - How many TCP streams?
    - How many UDP streams?
    - For a given client (source IP):
         o How many TCP connections were opened
         o How many concurrent TCP connections were opened.
         o What was the longest and shortest session
         o What is the re-transmission ratio for a given stream?
    - Given a protocol (say HTTP) identify how many streams had this protocol.
    - etc
    

    解决这个问题的一个明显方法是:

    1. 阅读捕获;将捕获的数据存储在所选的数据结构中。
    2. 转储输出。

    我正计划为此使用scapy(一个python库)。

    1. 有没有其他框架/库可以让这项工作更容易?

    2. 是否有一种完全不同的方法可以利用基于AI/ML的框架。[我之前没有人工智能/人工智能方面的经验]

    3. 实现一个框架的最佳方式是什么?在这个框架中,我可以问一些关于数据集的问题,并实现一些函数来回答这些问题?

    [我非常精通Python和C,但对其他可能的选择持开放态度]

    更新:11月10日:我发现了这个: https://github.com/vichargrave/espcap 成为我想做的事情的一个非常有用的开始。。

    1 回复  |  直到 7 年前
        1
  •  1
  •   Argus Malware    7 年前

    我建议你使用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>]