Search code examples
pythonscapywifi

Scapy - Counting the number of layers in a packet.


I am trying to count the number of Element Layers in a Beacon frame. I want to loop through all the layers until the last layer is reached. Let suppose i've this packet stored in a variable pkt:

>>> pkt.summary()
    "RadioTap / 802.11 Management 8L 34:bf:90:4a:bb:57 > ff:ff:ff:ff:ff:ff / Dot11Beacon / SSID='unknown' / Dot11Elt / Dot11Elt / Dot11Elt / Dot11Elt / Dot11Elt / Dot11Elt / Dot11Elt / Dot11Elt / Dot11Elt / Dot11Elt / Dot11Elt / Dot11Elt / Dot11Elt"

There are 13 Dot11Elt layers in the packet. To extract information from all of these layers i would need to loop through all of them. What i've tried doing to achieve this:

eltlayers = pkt.getlayer(Dot11Elt)
for layer in len(eltlayers):  # problem lies here
    print layer[layer].ID     # This is field in Elt Layer 

But len gives me the length of the Raw payload not the number of layers. The question is how to loop through the packet layers or is there any other way to get the number of layers?


Solution

  • You can extract Dot11Elt layers like this:

    pkt_elt = pkt[Dot11Elt]
    while isinstance(pkt_elt, Dot11Elt):
        print "tag id = %d" % pkt_elt.ID
        pkt_elt = pkt_elt.payload