[Python] Why does not scapy recognize my custom layer?

Discussão em 'Python' iniciado por Stack, Dezembro 17, 2024.

  1. Stack

    Stack Membro Participativo

    In scapy I've added a layer with "bind_layers". But it seems that I did something wrong and a packet is not recognized correctly.

    I try to add a layer to scipy and don't understand where am I wrong.

    from scapy.data import ETHER_TYPES
    from scapy.fields import XBitField, BitFieldLenField, ShortField, XShortEnumField
    from scapy.layers.l2 import Ether, Dot1Q, LLC
    from scapy.packet import Packet, bind_layers


    def remove_layer(pkt, pos):
    print('remove_layer N = ' + str(pos))
    payload = pkt[pos].payload
    pkt[pos - 1].remove_payload()
    return pkt / payload


    def remove_hsr(pkt):
    print('remove_hsr')
    print('layers:')
    for layer in pkt:
    print(layer.name)
    if HSR in pkt:
    print('HSR is here')
    j = 0
    for layer in pkt:
    if layer is HSR:
    print('HSR layer index: ' + str(j))
    return remove_layer(pkt, j)
    j += 1
    print('remove_hsr : HSR not found')
    return pkt


    class HSR(Packet):
    name = "HSR"
    fields_desc = [
    XBitField("path_id", 0, 4),
    BitFieldLenField("lsdu_size", 0, 12),
    ShortField("sequence_number", 0),
    XShortEnumField("type", 0x9000, ETHER_TYPES),
    ]


    bind_layers(Ether, HSR, type=0x892f)
    bind_layers(Dot1Q, HSR, type=0x892f)


    pkt_nohsr = '0060089fb1f300400540ef24000e810000200800450000a43b3b40004006b7548397208183972015048a17704e14e0754d3d609980187c70b13900000101080a0004f0c90199a3f3420007001d00c0000800c00000005e00d9025e00d9020000d9025e00420007001d00c0000900c00001005d00d8025d00d8020100d8025d00420007001d00c0000700c00001000100d80201000100010001005d00420007001d00c0000a00c00000000000d90200000000000000005e00568034cb'
    pkt_nohsr = bytearray.fromhex(pkt_nohsr)
    pkt_nohsr = Ether(pkt_nohsr)
    print(pkt_nohsr)
    pkt = '0060089fb1f300400540ef24892f10b60000000e810000200800450000a43b3b40004006b7548397208183972015048a17704e14e0754d3d609980187c70b13900000101080a0004f0c90199a3f3420007001d00c0000800c00000005e00d9025e00d9020000d9025e00420007001d00c0000900c00001005d00d8025d00d8020100d8025d00420007001d00c0000700c00001000100d80201000100010001005d00420007001d00c0000a00c00000000000d90200000000000000005e00568034cb'
    pkt = bytearray.fromhex(pkt)
    pkt = Ether(pkt)
    print(pkt)
    print(remove_hsr(pkt))


    My header here is '892f10b60000' where '892f' is an indicator of my type. I connected my layer with Ether and Dot1Q because those are possible lower layers. This code produces the output:

    802.3 00:40:05:40:ef:24 > 00:60:08:9f:b1:f3 / LLC / Raw / Padding
    00:40:05:40:ef:24 > 00:60:08:9f:b1:f3 (0x892f) / HSR / Raw
    remove_hsr
    layers:
    Ethernet
    HSR is here
    remove_hsr : HSR not found
    00:40:05:40:ef:24 > 00:60:08:9f:b1:f3 (0x892f) / HSR / Raw


    print(pkt) does not recognize inner layers with HSR added. HSR in pkt is true, but for layer in pkt has only 1 iteration with layer == Ether. What did i do wrong?

    Continue reading...

Compartilhe esta Página