Search code examples
network-programmingscapypacketpacket-sniffers

How to create a custom top-level layer 2 header in scapy?


Say I have the following:

class Delimiter(Packet):
    name = "Delimiter"
    fields_desc = [
        ByteField("val", 0)
    ]

class OutHeader(Packet):
    name = "Out Header"
    fields_desc = [
        ShortField("index", 0)
    ]

bind_layers(Delimiter, OutHeader, val=0)
bind_layers(OutHeader, Ether)

And I want to make Delimeter a layer 2 header, such that there is no Ether header at the top of the packet. How can I do this? With just this code, Scapy reads this instead:

###[ 802.3 ]###
  dst       = 00:51:99:ff:ff:ff
  src       = ff:ff:ff:00:00:00
  len       = 0
###[ Padding ]###

Solution

  • I eventually solved the problem by manually slicing off the headers, without custom Packet objects:

    s = str(pkt).encode("hex")
    delim = int(s[:2])
    index = int(s[2:6], 16)
    rest = Ether(s[6:].decode("hex"))