gpt4 book ai didi

python - Scapy:如何将新层(802.1q)插入现有数据包?

转载 作者:太空狗 更新时间:2023-10-30 01:14:37 26 4
gpt4 key购买 nike

我有一个数据包转储,想向数据包中注入(inject)一个 vlan 标记(802.1q header )。
如何做到这一点?

最佳答案

为了找到答案,我查看了 Scapy: Inserting a new layer and logging issues ,这确实很有帮助,但包含一些废话。

我根据引用问题 (add-dot1q_pcap.py) 添加层的解决方案:

import sys
from scapy.all import rdpcap, wrpcap, NoPayload, Ether, Dot1Q

# read all packets into buffer
# WARNING works only for small files
packets = []

for packet in rdpcap(sys.argv[1]):
# gets the first layer of the current packet
layer = packet.firstlayer()
# loop over the layers
while not isinstance(layer, NoPayload):
if 'chksum' in layer.default_fields:
del layer.chksum

if (type(layer) is Ether):
# adjust ether type
layer.type = 33024
# add 802.1q layer between Ether and IP
dot1q = Dot1Q(vlan=42)
dot1q.add_payload(layer.payload)
layer.remove_payload()
layer.add_payload(dot1q)
layer = dot1q

# advance to the next layer
layer = layer.payload
packets.append(packet)

wrpcap(sys.argv[2], packets)

脚本添加一个 vlan id 为 42 的 vlan 标签。

用法:./add-dot1q_pcap.py <source_file> <output_file>

关于python - Scapy:如何将新层(802.1q)插入现有数据包?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29133482/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com