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

Analyzing packet captures: What is the right approach

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

输入: pcap 文件(或任何捕获文件)。该文件可能有 hundreds/thousands 个数据包。

输出:关于流量流的一堆信息

- 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. 对于上述每个查询,编写专门的函数来收集数据(即解析所有流,捕获统计信息)
  3. 转储输出。

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

在开始实施之前,我很想了解解决该问题的其他可能方法:

  1. 还有其他 frame-works/libraries 可以让工作更轻松吗?

  2. 是否有完全不同的方法可以利用基于 AI/ML 的框架。 [我之前没有使用 AI/ML]

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

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

更新:11 月 10 日:我发现这个:https://github.com/vichargrave/espcap 对我想做的事情来说是一个非常有用的开始..

我推荐你使用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>]