Search code examples
pythonnetworkingnetwork-programmingscapy

Scapy, IndexError: Layer [Dot11] not found


I am trying to send a HTTP response using scapy. I sniff out an request and then attempt to fabricate a response. I am basing this off the code in this question. Here is the code that is causing problems:

import scapy.all as scapy
from scapy.layers.http import HTTPRequest
from scapy.layers.dot11 import RadioTap
from scapy.layers.dot11 import Dot11
packets = scapy.sniff(count=30)
    for i in range(len(packets)):
        if packets[i].haslayer(HTTPRequest):
            pkt = packets[i]
            dot11_frame = RadioTap()/Dot11(
                type = "Data",
                FCfield = "from-DS",
                addr1 = pkt[scapy.Dot11].addr2,
                addr2 = pkt[scapy.Dot11].addr1,
                addr3 = pkt[scapy.Dot11].addr1,
                )

After I sniff the packet and get all the information required I do what the other guy did, and the problem is that I keep on getting this error with the part defining dot11_frame, IndexError: Layer [Dot11] not found, this happens when the line addr1 = pkt[scapy.Dot11].addr2 is run. I have not even done a wildcard import like some answers suggested. So how do I fix this error?


Solution

  • First, you probably mean pkt[Dot11], not [scapy.Dot11], as you imported the name Dot11.

    Second, it seems that the packet you are capturing do not contain a Dot11 layer. This will happen, for example, if you capture packets from a non-Wi-Fi interface, or from a Wi-Fi interface that is not configured as a monitor interface.

    To help you debug your problem, you could add a print(pkt.command()) just after assigning the pkt variable.

    Also, you probably want to learn a bit of Python before using Scapy seriously. For example, rather than:

    for i in range(len(packets)):
        if packets[i].haslayer(HTTPRequest):
            pkt = packets[i]
            [...]
    

    You want to write:

    for pkt in packets:
        if HTTPRequest not in pkt:
            continue
        [...]
    

    Hope this helps!