scapy icon indicating copy to clipboard operation
scapy copied to clipboard

AttributeError: 'L3WinSocket' object has no attribute 'ins'

Open Dobatymo opened this issue 1 year ago • 1 comments

Brief description

Only a minor issue, but it could be confusing for users.

Running the script under Windows without admin raises an exception (as expected) but also has a different problem from a AttributeError in __del__ which make the real problem harder to see (since it's not at the bottom anymore).

Scapy version

2.6.1

Python version

3.13

Operating system

Windows 11 x64

Additional environment information

No response

How to reproduce

Run on Windows without admin

from scapy.all import IP, ICMP, sr1
s = IP(dst="google.com")/ICMP()/"asd"
p = sr1(s)

Actual result

WARNING: No libpcap provider available ! pcap won't be used
WARNING: Dropping unsupported option: filter
WARNING: Dropping unsupported option: nofilter
Traceback (most recent call last):
  File "...\Python313\Lib\site-packages\scapy\arch\windows\native.py", line 100, in __init__
    self.outs = self.ins = socket.socket(
                           ~~~~~~~~~~~~~^
        af,
        ^^^
        socket.SOCK_RAW,
        ^^^^^^^^^^^^^^^^
        socket.IPPROTO_IP,
        ^^^^^^^^^^^^^^^^^^
    )
    ^
  File "...\Python313\Lib\socket.py", line 233, in __init__
    _socket.socket.__init__(self, family, type, proto, fileno)
    ~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
PermissionError: [WinError 10013] An attempt was made to access a socket in a way forbidden by its access permissions

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "...\scapy-no-admin.py", line 3, in <module>
    p = sr1(s)
  File "...\Python313\Lib\site-packages\scapy\sendrecv.py", line 732, in sr1
    ans, _ = sr(*args, **kargs)
             ~~^^^^^^^^^^^^^^^^
  File "...\Python313\Lib\site-packages\scapy\sendrecv.py", line 706, in sr
    s = iface.l3socket(ipv6)(
        promisc=promisc, filter=filter,
        iface=iface, nofilter=nofilter,
    )
  File "...\Python313\Lib\site-packages\scapy\arch\windows\native.py", line 107, in __init__
    raise OSError("Windows native L3 Raw sockets are only "
                  "usable as administrator ! "
                  "Please install Npcap to workaround !")
OSError: Windows native L3 Raw sockets are only usable as administrator ! Please install Npcap to workaround !
Exception ignored in: <function SuperSocket.__del__ at 0x0000046425AF7760>
Traceback (most recent call last):
  File "...\Python313\Lib\site-packages\scapy\supersocket.py", line 305, in __del__
    self.close()
  File "...\Python313\Lib\site-packages\scapy\arch\windows\native.py", line 226, in close
    self.ins.ioctl(socket.SIO_RCVALL, socket.RCVALL_OFF)
AttributeError: 'L3WinSocket' object has no attribute 'ins'

Expected result

Same traceback but without the lowest Exception ignored in part.

Related resources

Similar older issues:

  • https://github.com/secdev/scapy/issues/3065

Dobatymo avatar Jul 17 '25 07:07 Dobatymo

hi @Dobatymo When running Scapy without Windows administrator privileges, in addition to the expected PermissionError, an additional AttributeError occurs, making the original error message difficult to see.

Root cause
When socket creation fails in the init method of the L3WinSocket class, the self.ins and self.outs attributes are not set. Subsequently, when the object is destroyed, del→close() is called, and an AttributeError occurs when attempting to access the non-existent self.ins attribute.

Modify the L3WinSocket.close() method to check for the existence of the self.ins attribute before accessing it.

This modification should make errors easier to see.

i will create pull request!

T3pp31 avatar Aug 17 '25 23:08 T3pp31