如何使用Scapy(pcapwriter)在FIFO中写入Pcap数据包



我是法国人,很抱歉我的英语不完美
在开始之前,如果您想尝试我的代码,可以在此处下载pcap示例文件:https://wiki.wireshark.org/SampleCaptures?action=AttachFile&do=get&target=ipv4frags.pap

我成功地打开了pcap文件,读取数据包,并用以下代码将它们写入另一个文件:

# Python 3.6
# Scapy 2.4.3
from scapy.utils import PcapReader, PcapWriter
import time
i_pcap_filepath = "inputfile.pcap"  # pcap to read
o_filepath = "outputfile.pcap"  # pcap to write

i_open_file = PcapReader(i_pcap_filepath)  # opened file to read
o_open_file = PcapWriter(o_filepath, append=True)  # opened file to write
while 1:
# I will have EOF exception but anyway
time.sleep(1)  # in order to see packet
packet = i_open_file.read_packet()  # read a packet in file
o_open_file.write(packet)  # write it

所以现在我想在FIFO中写入,并在实时Wireshark窗口中查看结果
要做到这一点,我只需要创建一个FIFO:$ mkfifo /my/project/location/fifo.fifo
并在其上启动Wireshark应用程序:$ wireshark -k -i /my/project/location/fifo.fifo
我在Python脚本中更改文件路径:o_filepath = "fifo.fifo" # fifo to write

但我崩溃了。。。这是回溯:

Traceback (most recent call last):
File "fifo.py", line 25, in <module>
o_open_file = PcapWriter(o_pcap_filepath, append=True)
File "/home/localuser/.local/lib/python3.6/site-packages/scapy/utils.py", line 1264, in __init__
self.f = [open, gzip.open][gz](filename, append and "ab" or "wb", gz and 9 or bufsz)  # noqa: E501
OSError: [Errno 29] Illegal seek

Wireshark还给了我一个错误("打开时管道魔术文件结束"(:Wireshark错误

我不明白为什么,该怎么办。难道不能使用scape.utils库在FIFO中写入吗?那该怎么办?

感谢您的支持,
Nicos44k


晚上很有用,因为我今天早上解决了问题!我并没有忘记昨天的回溯,但它实际上给了我一个很大的提示:我们有一个寻找问题
等待。。。FIFO文件中没有寻道!!!

所以我们不能设置";附加";参数设置为true
我更改为:o_open_file = PcapWriter(o_filepath)
错误消失了。

然而,数据包没有显示在实时
为了解决这个问题,我需要使用:o_open_file.flush()强制FIFO刷新

请记住,您可以在此处下载pcap示例文件:https://wiki.wireshark.org/SampleCaptures?action=AttachFile&do=get&target=ipv4frags.pcap
所以这里是完整的代码:

# Python 3.6
# Scapy 2.4.3
from scapy.utils import PcapReader, PcapWriter
import time
i_pcap_filepath = "inputfile.pcap"  # pcap to read
o_filepath = "fifo.fifo"  # pcap to write
i_open_file = PcapReader(i_pcap_filepath)  # opened file to read
o_open_file = PcapWriter(o_filepath)  # opened file to write
while 1:
# I will have EOF exception but anyway
time.sleep(1)  # in order to see packet
packet = i_open_file.read_packet()  # read a packet in file
o_open_file.write(packet)  # write it
o_open_file.flush()  # force buffered data to be written to the file

祝你今天过得愉快
Nicos4k

相关内容

  • 没有找到相关文章

最新更新