Scapy:如何在现有数据包中插入新层(802.1q)? [英] Scapy: How to insert a new layer (802.1q) into existing packet?

查看:77
本文介绍了Scapy:如何在现有数据包中插入新层(802.1q)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a packet dump and want to inject a vlan tag (802.1q header) to the packets.
How to do that?

推荐答案

为了找到答案,我查看了 Scapy: Inserting一个新的层和日志问题,这真的很有帮助,但包含一些细节.

To find the answer, I had a look at Scapy: Inserting a new layer and logging issues, which is really helpful, but contains some cruft.

我添加图层的解决方案,基于引用的问题 (add-dot1q_pcap.py):

My solution for adding a layer, based on the referenced question (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 标签.

The script adds a vlan tag with vlan id 42.

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

这篇关于Scapy:如何在现有数据包中插入新层(802.1q)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆