'scapy.error.Scapy_Exception: Can't attach the BPF filter

I found a WiFi scanner written in python on youtube. https://www.youtube.com/watch?v=DFTwB2nAexs Direct GitHub script link: https://github.com/davidbombal/red-python-scripts/blob/main/lanscan_arp.py

But I'm having an issue with BPF filter as "scapy.error.Scapy_Exception: Can't attach the BPF filter !"

script

#!/usr/bin/env python3
# Import scapy
import scapy.all as scapy

# We need to create regular expressions to ensure that the input is correctly formatted.
import re

# Basic user interface header
print(
    r"""______            _     _  ______                 _           _ 
|  _  \          (_)   | | | ___ \               | |         | |
| | | |__ ___   ___  __| | | |_/ / ___  _ __ ___ | |__   __ _| |
| | | / _` \ \ / / |/ _` | | ___ \/ _ \| '_ ` _ \| '_ \ / _` | |
| |/ / (_| |\ V /| | (_| | | |_/ / (_) | | | | | | |_) | (_| | |
|___/ \__,_| \_/ |_|\__,_| \____/ \___/|_| |_| |_|_.__/ \__,_|_|"""
)
print("\n****************************************************************")
print("\n* Copyright of David Bombal, 2021                              *")
print("\n* https://www.davidbombal.com                                  *")
print("\n* https://www.youtube.com/davidbombal                          *")
print("\n****************************************************************")

# Regular Expression Pattern to recognise IPv4 addresses.
ip_add_range_pattern = re.compile("^(?:[0-9]{1,3}\.){3}[0-9]{1,3}/[0-9]*$")

# Get the address range to ARP
while True:
    ip_add_range_entered = input(
        "\nPlease enter the ip address and range that you want to send the ARP request to (ex 192.168.1.0/24): "
    )
    if ip_add_range_pattern.search(ip_add_range_entered):
        print(f"{ip_add_range_entered} is a valid ip address range")
        break


# Try ARPing the ip address range supplied by the user.
# The arping() method in scapy creates a pakcet with an ARP message
# and sends it to the broadcast mac address ff:ff:ff:ff:ff:ff.
# If a valid ip address range was supplied the program will return
# the list of all results.
arp_result = scapy.arping(ip_add_range_entered)

output

Please enter the ip address and range that you want to send the ARP request to (ex 192.168.1.0/24): 192.168.1.0/24
192.168.1.0/24 is a valid ip address range
Traceback (most recent call last):
  File "/Users/belgra/Development/WiFi Scanner/lan_scan_arp.py", line 41, in <module>
    arp_result = scapy.arping(ip_add_range_entered)
  File "/opt/homebrew/lib/python3.10/site-packages/scapy/layers/l2.py", line 734, in arping
    ans, unans = srp(
  File "/opt/homebrew/lib/python3.10/site-packages/scapy/sendrecv.py", line 675, in srp
    s = iface.l2socket()(promisc=promisc, iface=iface,
  File "/opt/homebrew/lib/python3.10/site-packages/scapy/arch/bpf/supersocket.py", line 254, in __init__
    super(L2bpfListenSocket, self).__init__(*args, **kwargs)
  File "/opt/homebrew/lib/python3.10/site-packages/scapy/arch/bpf/supersocket.py", line 119, in __init__
    attach_filter(self.ins, filter, self.iface)
  File "/opt/homebrew/lib/python3.10/site-packages/scapy/arch/bpf/core.py", line 155, in attach_filter
    raise Scapy_Exception("Can't attach the BPF filter !")
scapy.error.Scapy_Exception: Can't attach the BPF filter !
/Users/belgra/Development/WiFi Scanner ❯                                  

I installed scapy 2.4.5 and am running this code by Python 3.10.1 on M1 Mac.

Any ideas?



Solution 1:[1]

I was able to solve this by installing the optional libpcap library that Scapy mentions in its installation documentation.

  1. Run brew update in your terminal
  2. Run brew install libpcap in your terminal
  3. Run Scapy with scapy in your terminal
  4. Within Scapy run conf.use_pcap = True

Here's the link to the documentation with more info

For reference I am running an M1 MacBook Air (macOS Monterey v12.1) with python 3.8.12.

Solution 2:[2]

I got the same error on a M1 MacBook and I solved this by installing and configuring libpcap as @RBPEDIIIAL suggested.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 RBPEDIIIAL
Solution 2 Salem