AleksaMCode / Preferred-Network-List-Sniffer

A reconnaissance tool for capturing and displaying SSIDs from device's Preferred Network List.
MIT License
136 stars 7 forks source link

[BUG] Unhandled `Exception` occurs when trying to capture packets on a non-existent interface #30

Open AleksaMCode opened 4 months ago

AleksaMCode commented 4 months ago

Is there an existing issue for this?

Description

When sniffing out packets before creating a Monitor interface, an unhandled Exception will occur in a AsyncSniffer thread, after which the sniffer will stay in an idle state without knowing that the sniffing process has failed.

PNLS version

0.2.0 with d7a9886

Python version

3.11.4

Operating system

Linux 5.15.44-Re4son-v8l+

Additional environment/system information

  1. Raspberry Pi 4

Actual Behavior

Sniffer fails to capture packets and the program continues unaware of the error where it waits for socket connection thread, meaning it stays in an idle state until the socket connection is closed/fails.

Expected Behavior

The unexpected Exception should be handled in some way. One solution could be done using a custom exception hook.

Steps To Reproduce

Follow README instructions in this section.

Anything else?

Screenshot_2024-03-16_15-39-24

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
AleksaMCode commented 4 months ago

As of PR #28, this issue is no longer possible. However, it would be nice to handle this error anyway.