scapy icon indicating copy to clipboard operation
scapy copied to clipboard

No /dev/bpf handle is available !

Open IdoBn opened this issue 8 years ago • 12 comments

I'm running on mac high sierra 10.13.4 after downloading from https://github.com/secdev/scapy/archive/v2.4.0.zip I unzipping and ran each of the following as root: run_scapy, run_scapy2 and run_scapy_py3 within each repl I ran: send(IP(dst="2.2.2.2", src="1.1.1.1")) and the traceback was the same:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/Users/idobn/dev/research/something/playground/tmp/scapy-2.4.0/scapy/sendrecv.py", line 302, in send
    realtime=realtime, return_packets=return_packets)
  File "/Users/idobn/dev/research/something/playground/tmp/scapy-2.4.0/scapy/sendrecv.py", line 276, in __gen_send
    s.send(p)
  File "/Users/idobn/dev/research/something/playground/tmp/scapy-2.4.0/scapy/arch/bpf/supersocket.py", line 345, in send
    frame = raw(self.guessed_cls()/pkt)
  File "/Users/idobn/dev/research/something/playground/tmp/scapy-2.4.0/scapy/compat.py", line 96, in raw
    return bytes(x)
  File "/Users/idobn/dev/research/something/playground/tmp/scapy-2.4.0/scapy/packet.py", line 345, in __bytes__
    return self.build()
  File "/Users/idobn/dev/research/something/playground/tmp/scapy-2.4.0/scapy/packet.py", line 444, in build
    p = self.do_build()
  File "/Users/idobn/dev/research/something/playground/tmp/scapy-2.4.0/scapy/packet.py", line 426, in do_build
    pkt = self.self_build()
  File "/Users/idobn/dev/research/something/playground/tmp/scapy-2.4.0/scapy/packet.py", line 407, in self_build
    p = f.addfield(self, p, val)
  File "/Users/idobn/dev/research/something/playground/tmp/scapy-2.4.0/scapy/fields.py", line 80, in addfield
    return s+struct.pack(self.fmt, self.i2m(pkt,val))
  File "/Users/idobn/dev/research/something/playground/tmp/scapy-2.4.0/scapy/layers/l2.py", line 109, in i2m
    return MACField.i2m(self, pkt, self.i2h(pkt, x))
  File "/Users/idobn/dev/research/something/playground/tmp/scapy-2.4.0/scapy/layers/l2.py", line 101, in i2h
    x = conf.neighbor.resolve(pkt,pkt.payload)
  File "/Users/idobn/dev/research/something/playground/tmp/scapy-2.4.0/scapy/layers/l2.py", line 49, in resolve
    return self.resolvers[k](l2inst,l3inst)
  File "/Users/idobn/dev/research/something/playground/tmp/scapy-2.4.0/scapy/layers/inet.py", line 821, in inet_register_l3
    return getmacbyip(l3.dst)
  File "/Users/idobn/dev/research/something/playground/tmp/scapy-2.4.0/scapy/layers/l2.py", line 84, in getmacbyip
    nofilter=1)
  File "/Users/idobn/dev/research/something/playground/tmp/scapy-2.4.0/scapy/sendrecv.py", line 434, in srp1
    ans, _ = srp(*args, **kargs)
  File "/Users/idobn/dev/research/something/playground/tmp/scapy-2.4.0/scapy/sendrecv.py", line 416, in srp
    s = conf.L2socket(promisc=promisc, iface=iface, filter=filter, nofilter=nofilter, type=type)
  File "/Users/idobn/dev/research/something/playground/tmp/scapy-2.4.0/scapy/arch/bpf/supersocket.py", line 58, in __init__
    (self.ins, self.dev_bpf) = get_dev_bpf()
  File "/Users/idobn/dev/research/something/playground/tmp/scapy-2.4.0/scapy/arch/bpf/core.py", line 98, in get_dev_bpf
    raise Scapy_Exception("No /dev/bpf handle is available !")
scapy.error.Scapy_Exception: No /dev/bpf handle is available !

after looking at some of the past issues it appears similar to this one: #1015 however it was solved some time ago...

Update: The above was ran while I had wireshark running, after quitting wireshark the error stopped.

IdoBn avatar Apr 14 '18 19:04 IdoBn

It is likely that you are running other softwares that opened BPF handles, and Scapy isn't trying hard enough to open new handles. Is the following fix works for you?

diff --git a/scapy/arch/bpf/core.py b/scapy/arch/bpf/core.py
index 5753fcd..35aff23 100644
--- a/scapy/arch/bpf/core.py
+++ b/scapy/arch/bpf/core.py
@@ -88,7 +88,7 @@ def get_dev_bpf():
     """Returns an opened BPF file object"""
 
     # Get the first available BPF handle
-    for bpf in range(0, 8):
+    for bpf in range(0, 256):
         try:
             fd = os.open("/dev/bpf%i" % bpf, os.O_RDWR)
             return (fd, bpf)

guedou avatar Apr 14 '18 20:04 guedou

@IdoBn is the proposed fix works?

guedou avatar Apr 27 '18 08:04 guedou

Hi @guedou,

I think I am having the same issue:

$ ./run_scapy
INFO: Can't import PyX. Won't be able to use psdump() or pdfdump().
WARNING: No route found for IPv6 destination :: (no default route?)

                     aSPY//YASa
             apyyyyCY//////////YCa       |
            sY//////YSpcs  scpCY//Pp     | Welcome to Scapy
 ayp ayyyyyyySCP//Pp           syY//C    | Version 2.4.0.dev149
 AYAsAYYYYYYYY///Ps              cY//S   |
         pCCCCY//p          cSSps y//Y   | https://github.com/secdev/scapy
         SPPPP///a          pP///AC//Y   |
              A//A            cyP////C   | Have fun!
              p///Ac            sC///a   |
              P////YCpc           A//A   | Wanna support scapy? Rate it on
       scccccp///pSP///p          p//Y   | sectools!
      sY/////////y  caa           S//P   | http://sectools.org/tool/scapy/
       cayCyayP//Ya              pY/Ya   |             -- Satoshi Nakamoto
        sY/PsY////YCc          aC//Yp    |
         sc  sccaCY//PCypaapyCP//YSs
                  spCPY//////YPSps
                       ccaacs
                                       using IPython 5.3.0
>>> a = sniff(iface="en0", filter="tcp and port 80", count=10)
---------------------------------------------------------------------------
Scapy_Exception                           Traceback (most recent call last)
<ipython-input-1-42ea8830eddd> in <module>()
----> 1 a = sniff(iface="en0", filter="tcp and port 80", count=10)

/Users/keng/playground/scapy/scapy/sendrecv.pyc in sniff(count, store, offline, prn, lfilter, L2socket, timeout, opened_socket, stop_filter, iface, started_callback, *arg, **karg)
    780         else:
    781             sniff_sockets[L2socket(type=ETH_P_ALL, iface=iface,
--> 782                                    *arg, **karg)] = iface
    783     lst = []
    784     if timeout is not None:

/Users/keng/playground/scapy/scapy/arch/bpf/supersocket.pyc in __init__(self, iface, type, promisc, filter, nofilter, monitor)
     56
     57         # Get the BPF handle
---> 58         (self.ins, self.dev_bpf) = get_dev_bpf()
     59         self.outs = self.ins
     60

/Users/keng/playground/scapy/scapy/arch/bpf/core.pyc in get_dev_bpf()
     96             continue
     97
---> 98     raise Scapy_Exception("No /dev/bpf handle is available !")
     99
    100

Scapy_Exception: No /dev/bpf handle is available !
>>>

I just cloned the repo from the library's github page (https://github.com/secdev/scapy), I first had this error, then read a related issue and tried python setup.py install, but still the same error. I'm running python 2.7.13, so I also tried ./run_scapy_py2 but got the same error. I'm on macOS High Sierra (10.13.4). Thanks in advance for the assistance!

simonkeng avatar May 10 '18 03:05 simonkeng

Update: running ./run_scapy with sudo appears to have eliminated the No /dev/bpf handle is available ! error.

Although I seem to have found a different, interesting (aesthetic) problem. The output does not appear to be generated in the REPL:

>>> a = sniff(iface="en0",prn=lambda x:x.sprintf("{Dot11Beacon:%Dot11.addr3%\t%Dot11Beacon.info%\t%PrismHeader.channel%\t%Dot11Beacon.cap%}"
...:), count=10)










>>> a
<Sniffed: TCP:5 UDP:2 ICMP:0 Other:3>
>>>

After executing the command, it appears to be printing in real time, but printing nothing. Seems odd, any ideas for this one? Thanks!

simonkeng avatar May 10 '18 03:05 simonkeng

Scapy don’t sniff 802.11 frames by default, use the sniff() monitor argument.

guedou avatar May 10 '18 07:05 guedou

I'm running on mac high seirra 10.13.6 My scapy version is scapy-2.4.0 my python version is 3.6

I got error message like 'scapy.error.Scapy_Exception: No /dev/bpf handle is available !'

What i run

syn = IP(dst='www.example.com') / TCP(dport=80, flags='S') syn_ack = sr1(syn)

This is the error message what i got

% python example.py 1 ↵ Traceback (most recent call last): File "example.py", line 27, in <module> syn_ack = sr1(syn) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/scapy/sendrecv.py", line 393, in sr1 s=conf.L3socket(promisc=promisc, filter=filter, nofilter=nofilter, iface=iface) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/scapy/arch/bpf/supersocket.py", line 58, in __init__ (self.ins, self.dev_bpf) = get_dev_bpf() File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/scapy/arch/bpf/core.py", line 98, in get_dev_bpf raise Scapy_Exception("No /dev/bpf handle is available !") scapy.error.Scapy_Exception: No /dev/bpf handle is available !

RayKimEX avatar Aug 14 '18 13:08 RayKimEX

@RayKimEX please retry using the github développement version

gpotter2 avatar Aug 14 '18 13:08 gpotter2

@gpotter2 It works now thanks

RayKimEX avatar Aug 14 '18 14:08 RayKimEX

@gpotter2 Getting the same error as @RayKimEX.

OS — macOS 10.13.6 Python — 3.6.5 (installed w/pyenv)

However, I'm running scapy through Jupyter (it's for a school project) and, even after cloning and installing scapy from source (latest commit is 17afebb), I'm getting the No /dev/bpf handle is available ! exception. Is there a reason for this? Is Jupyter not giving scapy the permissions it needs to run properly?

pizzimathy avatar Sep 08 '18 03:09 pizzimathy

Scapy needs to be run as root to send packets.

guedou avatar Sep 08 '18 05:09 guedou

@guedou Hello. I got the same issue, could you please help me to solve it? My environment:

Mac OS High Sierra 10.13.6 Python 3.7.2 Scapy 2.4.2

This is my code:

from scapy.all import * pkt = IP(dst='8.8.8.8')/ICMP() ping = sr1(pkt,timeout=2)

When I wanted to send icmp packets, I got this error.

Traceback (most recent call last): File "/Users/zy-mac/Desktop/pYTHON/1.py", line 7, in ping = sr1(pkt,timeout=2) File "/Users/zy-mac/Library/Python/3.7/lib/python/site-packages/scapy/sendrecv.py", line 469, in sr1 s = conf.L3socket(promisc=promisc, filter=filter, nofilter=nofilter, iface=iface) # noqa: E501 File "/Users/zy-mac/Library/Python/3.7/lib/python/site-packages/scapy/arch/bpf/supersocket.py", line 58, in init (self.ins, self.dev_bpf) = get_dev_bpf() File "/Users/zy-mac/Library/Python/3.7/lib/python/site-packages/scapy/arch/bpf/core.py", line 98, in get_dev_bpf raise Scapy_Exception("No /dev/bpf handle is available !") scapy.error.Scapy_Exception: No /dev/bpf handle is available !

sdtazhangyi avatar Feb 15 '19 21:02 sdtazhangyi

Update: running ./run_scapy with sudo appears to have eliminated the No /dev/bpf handle is available ! error.

Although I seem to have found a different, interesting (aesthetic) problem. The output does not appear to be generated in the REPL:

>>> a = sniff(iface="en0",prn=lambda x:x.sprintf("{Dot11Beacon:%Dot11.addr3%\t%Dot11Beacon.info%\t%PrismHeader.channel%\t%Dot11Beacon.cap%}"
...:), count=10)










>>> a
<Sniffed: TCP:5 UDP:2 ICMP:0 Other:3>
>>>

After executing the command, it appears to be printing in real time, but printing nothing. Seems odd, any ideas for this one? Thanks!

Running it with sudo on Mac solved it for me!

jainsamyak avatar Apr 20 '19 10:04 jainsamyak