Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unhandled Exception after join in AsyncSniffer caused by usage of a non-existent interface #4322

Closed
AleksaMCode opened this issue Mar 16, 2024 · 0 comments · Fixed by #4359
Closed

Comments

@AleksaMCode
Copy link

AleksaMCode commented Mar 16, 2024

Brief description

When invoking AsyncSniffer with an interface that doesn't exist, it causes an Exception in AsyncSniffer thread. I've discovered this by accident, as my project's code was trying to invoke the sniffing process with an incorrect interface name. An easy fix for me was to check the interface name and in doing so preventing this. For example, when trying to sniff packets on a monitoring interface, which hasn't yet been created (e.q. wlan0mon), this error occurs. Also, another solution for this could be to use a custom exception hook.

Running the example I've provided creates the AsyncSniffer thread and starts executing it. The thread blocks for a moment and raises an exception, which then bubbles up and is reported as an error (it's printed out). The failure in the thread does not impact the main thread, but the code after doesn't know that the capture of packets has failed. Ideally, I would like to know that the sniffing has failed, so I can stop my own program instead of having my program in an idle state.

Scapy version

2.5.0

Python version

3.11.4

Operating system

Linux 5.15.44-Re4son-v8l+

Additional environment information

The program was ran on Raspberry Pi 4.

How to reproduce

I can reproduce this by simply running this script:

from scapy.sendrecv import AsyncSniffer

def parsing(pkt):
    print(pkt)

sniffer = AsyncSniffer(iface="fake_int", prn=parsing)
sniffer.start()
sniffer.join()

The Exception occurs after join().

Actual result

Unhandled Exception occurs.

Expected result

It would be preferable if the unexpected and unhandled Exception doesn't occur. You could maybe check if the given interface is available (exists) during the creation of the AsyncSniffer and return some sort of indicator that the provided interface doesn't exist. This could prevent this sort of error.

Related resources

Exception in thread AsyncSniffer:
Traceback (most recent call last):
  File "/usr/lib/python3.11/threading.py", line 1038, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.11/threading.py", line 975, in run
    self._target(*self._args, **self._kwargs)
  File "/usr/lib/python3/dist-packages/scapy/sendrecv.py", line 1171, in _run
    sniff_sockets[_RL2(iface)(type=ETH_P_ALL, iface=iface,
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/scapy/arch/linux.py", line 499, in __init__
    set_promisc(self.ins, self.iface)
  File "/usr/lib/python3/dist-packages/scapy/arch/linux.py", line 179, in set_promisc
    mreq = struct.pack("IHH8s", get_if_index(iff), PACKET_MR_PROMISC, 0, b"")
                                ^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/scapy/arch/linux.py", line 399, in get_if_index
    return int(struct.unpack("I", get_if(iff, SIOCGIFINDEX)[16:20])[0])
                                  ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/scapy/arch/unix.py", line 42, in get_if
    return ioctl(sck, cmd, struct.pack("16s16x", iff.encode("utf8")))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
OSError: [Errno 19] No such device
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant