【问题标题】:Scapy: How to insert a new layer (802.1q) into existing packet?Scapy:如何在现有数据包中插入新层(802.1q)?
【发布时间】:2015-05-21 21:52:46
【问题描述】:

我有一个数据包转储,并想将一个 vlan 标记(802.1q 标头)注入数据包。
该怎么做?

【问题讨论】:

    标签: python scapy vlan


    【解决方案1】:

    为了找到答案,我查看了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>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-29
      • 2014-04-01
      • 2020-12-25
      • 2015-03-25
      • 2018-04-08
      • 2018-01-29
      • 2014-10-28
      相关资源
      最近更新 更多