diff --git a/scapy/arch/windows/native.py b/scapy/arch/windows/native.py index 61cfa8beb8a..b2b457da615 100644 --- a/scapy/arch/windows/native.py +++ b/scapy/arch/windows/native.py @@ -222,7 +222,7 @@ def recv_raw(self, x=MTU): def close(self): # type: () -> None - if not self.closed and self.promisc: + if not self.closed and self.promisc and hasattr(self, 'ins'): self.ins.ioctl(socket.SIO_RCVALL, socket.RCVALL_OFF) super(L3WinSocket, self).close() diff --git a/test/windows.uts b/test/windows.uts index 59202833331..394376be0c2 100644 --- a/test/windows.uts +++ b/test/windows.uts @@ -117,6 +117,27 @@ def _test(): retry_test(_test) += Test L3WinSocket close() with partial initialization +~ windows + +from scapy.arch.windows.native import L3WinSocket +import socket + +# Create partially initialized L3WinSocket +ws = object.__new__(L3WinSocket) +ws.closed = False +ws.promisc = True +# Note: ws.ins is intentionally not set + +# This should not raise AttributeError +try: + ws.close() + test_passed = True +except AttributeError: + test_passed = False + +assert test_passed, "L3WinSocket.close() raised AttributeError on partially initialized object" + = Leave native mode conf.use_pcap = True