[英]Scapy: How to insert a new layer (802.1q) into existing packet?
我有一個數據包轉儲,並希望向數據包注入vlan標記(802.1q標頭)。
怎么做?
為了找到答案,我看了一下Scapy:插入一個新圖層和記錄問題 ,這確實很有用,但包含了一些內容。
我根據引用的問題(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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.