简体   繁体   English

Python Scapy wrpcap - 如何将数据包附加到 pcap 文件?

[英]Python Scapy wrpcap - How do you append packets to a pcap file?

I have some software that can emulate things like BER and delays on the network.我有一些软件可以模拟网络上的 BER 和延迟之类的东西。 I need a way to test the BER module of the software to make sure it actually works correctly.我需要一种方法来测试软件的 BER 模块,以确保它确实正常工作。 My solution is to create a program that sends out raw Ethernet frames with the type field set to an unused type.我的解决方案是创建一个程序,该程序发送原始以太网帧,并将类型字段设置为未使用的类型。 Inside the Ethernet frame is just random bits.以太网帧内部只是随机位。 For each frame sent out I need to log the frame to a pcap file.对于发送的每个帧,我需要将帧记录到pcap文件中。 On the other side of the network link will be a receiving application that simply writes every packet it sees to its own pcap log.在网络链接的另一端将是一个接收应用程序,它只是将它看到的每个数据包写入自己的pcap日志。 After the test is done running the two pcap logs will be compared to get the BER.测试完成后,将比较两个 pcap 日志以获取 BER。

I'm using the python module Scapy and so far its done everything that I need.我正在使用 python 模块Scapy ,到目前为止它已经完成了我需要的一切。 I can send out raw Ethernet frames with random data and see them in Wireshark.我可以发送带有随机数据的原始以太网帧,并在 Wireshark 中查看它们。 However, I don't know how to get the wrpcap() method to append to the pcap file, instead of overwriting.但是,我不知道如何让wrpcap()方法附加到 pcap 文件,而不是覆盖。 I know I can write a list of packets to wrpcap , but this application needs to be able to run for an indefinite amount of time and I don't want to have to wait until the application quits to write all of packets sent to the hard drive.我知道我可以将数据包列表写入wrpcap ,但是此应用程序需要能够无限期地运行,而且我不想等到应用程序退出才能将所有发送到硬盘的数据包写入驾驶。 As that would be a lot to store in memory, and if something happened I would have to start the test all over from scratch.因为这会在内存中存储很多东西,如果发生了什么事情,我将不得不从头开始测试。

My question is: How do I append to a pcap file using scapy instead of overwriting the pcap file?我的问题是:如何使用scapy附加到pcap文件而不是覆盖pcap文件? Is it even possible?甚至有可能吗? If not then what module can do what I need?如果没有,那么什么模块可以做我需要的?

While looking for something with Scapy 's capabilities I ran into dpkt , but I didn't find a lot of documentation for it.在寻找具有Scapy功能的东西时,我遇到了dpkt ,但我没有找到很多文档。 Can dpkt do what I'm asking and if so where can I get some good documentation for it? dpkt可以做我要问的事情吗?如果可以,我可以从哪里获得一些好的文档?

For posterity, PcapWriter or RawPcapWriter looks to be the easier way to deal with this in scapy 2.2.0.对于后代,PcapWriter 或 RawPcapWriter 看起来是在 scapy 2.2.0 中处理此问题的更简单方法。 Couldn't find much documentation other than browsing the source though.除了浏览源代码之外,找不到太多文档。 A brief example:一个简单的例子:

from scapy.utils import PcapWriter

pktdump = PcapWriter("banana.pcap", append=True, sync=True)

...
pktdump.write(pkt)
...

There is a way to do what you want, but it means either:有一种方法可以做你想做的事,但这意味着:

  • [Memory hog with one big pcap ]: Read the existing pcap from disk with rdpcap() into a scapy PacketList() and then writing frames to the PacketList as they are received. [使用一个大pcap rdpcap()内存]:使用rdpcap()从磁盘读取现有的pcap到一个scapy PacketList() ,然后在收到帧时将它们写入PacketList You can selectively save intermediate PacketList to the pcap at will, but I don't think there is anything like an append capability in scapy 's wrpcap() .您可以随意有选择地将中间PacketList保存到pcap ,但我认为scapywrpcap()没有类似附加功能的功能。 As you mentioned, this technique also means that you are keeping the entire PacketList in memory until completion.正如您所提到的,这种技术还意味着您将整个PacketList保留在内存中直到完成。

  • [Glue individual pcap files together]: Only keep small snapshots of packets in memory... you should save pcap snapshots on a per-X-minute basis to disk, and then aggregate those individual files together when the script finishes. [将单个pcap文件粘合在一起]:仅将数据包的小快照保留在内存中……您应该每 X 分钟将pcap快照保存到磁盘,然后在脚本完成时将这些单个文件聚合在一起。

You can combine pcap files in linux with mergecap from the wireshark package... The following command will combine pak1.pcap and pak2.pcap into all_paks.pcap :您可以将 linux 中的pcap文件与来自wireshark包的mergecap组合...以下命令将pak1.pcappak2.pcap组合成all_paks.pcap

mergecap -w all_paks.pcap pak1.pcap pak2.pcap

As for dpkt , I looked through their source, and it might be able to incrementally write packets, but I can't speak for how stable or maintained their code base is... it looks a bit neglected from the commit logs (last commit was January 9th 2011).至于dpkt ,我查看了他们的源代码,它可能能够增量写入数据包,但我不能说他们的代码库有多稳定或维护得有多好......从提交日志(最后一次提交)中它看起来有点被忽视2011 年 1 月 9 日)。

The wrpcap() function can be used to append if you include the keyword argument append=True .如果包含关键字参数append=True则可使用wrpcap()函数进行追加。 For example:例如:

pkt = IP()
wrpcap('/path/to/filename.pcap', pkt, append=True)
pkt2 = IP()
wrpcap('/path/to/filename.pcap', pkt2, append=True)

rdpcap('/path/to/filename.pcap')

<filename.pcap: TCP:0 UDP:0 ICMP:0 Other:2>

Side note: wrpcap opens and closes the file handle with each call.旁注:wrpcap 在每次调用时打开和关闭文件句柄。 If you have an open file handle to the pcap file, it will be closed after a call to wrpcap() .如果您有一个打开的 pcap 文件句柄,它将在调用wrpcap()后关闭。

I think I am following you here as packets are sniffed you would like to have them all written to a single pcap file?我想我在这里跟踪你,因为数据包被嗅探你想让它们全部写入单个 pcap 文件吗? While you cannot append to a pcap you can append the packets to list and then write them all at once to the pcap.虽然您不能附加到 pcap,但您可以将数据包附加到列表中,然后将它们一次全部写入 pcap。

I'm not sure if this answers your question or helps at all, if not let me know and I can tweek it to meet your needs.我不确定这是否回答了您的问题或是否有帮助,如果没有,请告诉我,我可以调整它以满足您的需求。 In this example I set the threshold to create a new pcap for ever 500 packets sniffed.在这个例子中,我设置了阈值来为每 500 个嗅探的数据包创建一个新的 pcap。 Be careful if you run this twice as your pcaps may get over written on the second go.如果您运行两次,请小心,因为您的 pcap 可能会在第二次运行时被覆盖。

#!/usr/bin/python -tt

from scapy.all import *

pkts = []
iter = 0
pcapnum = 0

def makecap(x):
    global pkts
    global iter
    global pcapnum
    pkts.append(x)
    iter += 1
    if iter == 500:
        pcapnum += 1
        pname = "pcap%d.pcap" % pcapnum
        wrpcap(pname, pkts)
        pkts = []
        iter = 0

while 1:
    sniff(prn=makecap)

This should give you a little bit of leverage however the last few packets may get lost (lower the value in the if statement to mitigate this.) Suggest using it on both sides at the same time so each pcap should line up, Later on you can use mergepcap as Mike suggests to if you like.这应该会给你一点影响,但是最后几个数据包可能会丢失(降低 if 语句中的值以减轻这种情况。)建议在两侧同时使用它,这样每个 pcap 应该排队,稍后你如果您愿意,可以按照 Mike 的建议使用 mergepcap。 Let me know if this works for you.让我知道这是否适合您。

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

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