简体   繁体   English

在 Scapy 中读取 pcap 文件的一部分

[英]Reading part of pcap file in Scapy

I've been trying to figure out a way to read a part of a pcap-file using scapy.我一直在想办法使用 scapy 读取 pcap 文件的一部分。

Ex: I have a pcap-file with 1000 rows.例如:我有一个 1000 行的 pcap 文件。

Question: Is it possible to read this in 10 pieces, ie read 100 rows sequentially?问题:是否可以分 10 段读取,即按顺序读取 100 行?

The scenario is:场景是:

I'm reading multiple pcap files and comparing packets between them.我正在读取多个 pcap 文件并比较它们之间的数据包。 In order to compare packets, I use index and call a specific packet as pkt = packets_from_pcap_file_7[idx] .为了比较数据包,我使用索引并将特定数据包称为pkt = packets_from_pcap_file_7[idx] To use index I need to read the pcap-files with rdpcap(FILENAME.pcap) .要使用索引,我需要使用rdpcap(FILENAME.pcap)读取 pcap 文件。 However, this function will read the entire pcap-file and cause a MemoryError.但是,此函数将读取整个 pcap 文件并导致 MemoryError。

Since I want to compare specific packets from different files, I find it hard to use RawPcapReader(FILENAME.pcap) where I can just iterate through one specific packet at the time as:由于我想比较来自不同文件的特定数据包,我发现很难使用RawPcapReader(FILENAME.pcap) ,我可以在其中迭代一个特定数据包,如下所示:

packets = RawPcapReader(FILENAME.pcap)
for pkt in packets:
    pkt.doThings

I will post a very slow solution to the problem we're I have to iterate through the entire pcap-file for every packet and iterate to a specified index:我将发布一个非常缓慢的解决方案来解决我们必须为每个数据包遍历整个 pcap 文件并迭代到指定索引的问题:

def get_packet(idx):
    packets = RawPcapReader("FILENAME.pcap")
    counter = 0
    for pkt in packets:
        if counter == idx:
            return pkt
        counter += 1

I could split the packets in the linux terminal but since this will be used for others as well, I would like the scripts to do all the job.我可以在 linux 终端中拆分数据包,但由于这也将用于其他人,我希望脚本完成所有工作。 I think there is a much simpler solution to this, which I've been unable to find.我认为对此有一个更简单的解决方案,但我一直找不到。 Thanks in advance.提前致谢。

You could probably open all the files without reading them, then iterate through the packets.您可能无需阅读即可打开所有文件,然后遍历数据包。 Something like就像是

files = ["a.pcap", "b.pcap", ......]
fds = [RawPcapReader(x) for x in files]
while True:
    try:
        # get the next packets of all files 
        pkts = [next(x) for x in fds]
        # do something with them
    except EOFError:
        # file has ended
        break
for f in fds:
    f.close()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM