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

时间:2017-11-09 08:27:50

标签: python machine-learning scapy packet-capture

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

输入: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. 对于上述每个查询,编写专门的函数来收集数据(即解析所有流,捕获统计数据)
  3. 转储输出。
  4. 我计划使用scapy(python库)。

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

    1. 是否有其他框架工作/库可以让工作更轻松?

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

    3. 实施框架的最佳方式是什么?我可以就数据集问题提出几个问题,并实施函数来回答问题?

    4. [我非常精通Python和C,但对其他可能的选项开放]

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

1 个答案:

答案 0 :(得分:0)

我建议你使用Pyshark。这是tshark的包装器。它还支持所有tshark过滤器,解码器库,...并且易于使用!这是一个很好的解析.pcap文件和livecapturing

的包

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>]
相关问题