需要一种简单的方法来本地修改 wireshark .pcap 数据包的数据
Need an easy way to locally modify a wireshark .pcap packet's data
我一直在尝试使用 Scapy,但是文档太少了,我无法很好地进行简单编辑。
本质上,我正在 Python 中寻找一个简单的解决方案,从 .pcap 中获取每个数据包,read/modify 一些 data/delete 数据包并将其保存回.pcap.
例如:
给定一个 sACN 数据包,我需要 read/modify 优先级八位位组 (108) 和宇宙八位位组 (113-114) 并再次保存。
谢谢!
要使用 scapy
处理“.pcap”文件,您需要从 'scapy.utils[=34 导入 'PcapWriter' =]'。以下示例演示如何使用 scapy
:
处理“.pcap”文件
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from scapy.all import *
# to process .pcap files we need to
# import 'PcapWriter' from 'scapy.utils'
from scapy.utils import PcapWriter
# initialize a 'net_old.pcap' file
old_cap = PcapWriter("net_old.pcap", append=True, sync=True)
# create a simple packet
packet = IP(dst = "www.google.com")/ICMP()/"hi"
# create a pcap with 5 such packets
for _ in range(5): old_cap.write(packet)
# now read the packets from 'net.pcap'
packets = rdpcap('net_old.pcap')
new_cap = PcapWriter("net_new.pcap", append=True)
# and modify each packet
for p in packets:
# modify any packet field, e.g. IP's dst
p[IP].dst = '8.8.8.8'
# write new packets in the new pcap file
new_cap.write(p)
现在,如果您查看带有 wireshark
的“.pcap”文件,您将看到数据包已成功修改:
- 旧 pcap 文件:
- 新 pcap 文件:
我一直在尝试使用 Scapy,但是文档太少了,我无法很好地进行简单编辑。
本质上,我正在 Python 中寻找一个简单的解决方案,从 .pcap 中获取每个数据包,read/modify 一些 data/delete 数据包并将其保存回.pcap.
例如:
给定一个 sACN 数据包,我需要 read/modify 优先级八位位组 (108) 和宇宙八位位组 (113-114) 并再次保存。
谢谢!
要使用 scapy
处理“.pcap”文件,您需要从 'scapy.utils[=34 导入 'PcapWriter' =]'。以下示例演示如何使用 scapy
:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from scapy.all import *
# to process .pcap files we need to
# import 'PcapWriter' from 'scapy.utils'
from scapy.utils import PcapWriter
# initialize a 'net_old.pcap' file
old_cap = PcapWriter("net_old.pcap", append=True, sync=True)
# create a simple packet
packet = IP(dst = "www.google.com")/ICMP()/"hi"
# create a pcap with 5 such packets
for _ in range(5): old_cap.write(packet)
# now read the packets from 'net.pcap'
packets = rdpcap('net_old.pcap')
new_cap = PcapWriter("net_new.pcap", append=True)
# and modify each packet
for p in packets:
# modify any packet field, e.g. IP's dst
p[IP].dst = '8.8.8.8'
# write new packets in the new pcap file
new_cap.write(p)
现在,如果您查看带有 wireshark
的“.pcap”文件,您将看到数据包已成功修改:
- 旧 pcap 文件:
- 新 pcap 文件: