我尝试使用 Scapy 配置 L3 套接字,如下所示:
from scapy.all import *
soc = conf.L3socket(iface="eth1")
当我在 Ubuntu VM 中使用此代码时,一切正常,但是当我在 WSL 中使用此代码时,出现了错误:
/usr/local/lib/python3.10/dist-packages/scapy/layers/ipsec.py:471: CryptographyDeprecationWarning: Blowfish has been deprecated
cipher=algorithms.Blowfish,
/usr/local/lib/python3.10/dist-packages/scapy/layers/ipsec.py:485: CryptographyDeprecationWarning: CAST5 has been deprecated
cipher=algorithms.CAST5,
Traceback (most recent call last):
File "........./python/test.py", line 2, in <module>
soc = conf.L3socket(iface="eth1")
File "/usr/local/lib/python3.10/dist-packages/scapy/arch/linux.py", line 486, in __init__
self.ins = socket.socket(
File "/usr/lib/python3.10/socket.py", line 232, in __init__
_socket.socket.__init__(self, family, type, proto, fileno)
OSError: [Errno 97] Address family not supported by protocol
我该如何解决这个问题?
最佳答案
首先,我有一种强烈的预感,您实际上正在使用 WSL1 实例。首先使用 wsl.exe -l -v
仔细检查.
安装/启用 WSL2 但仍有 WSL1 实例从未转换为 WSL2 的情况很常见。
有几件事表明这是 WSL1:
您有一个
eth1
接口(interface) - WSL2 通常不会,因为它只设置一个虚拟以太网设备供 Linux 内核使用。另一方面,WSL1 使用 Windows API(通过系统调用转换层)来枚举 Windows 可用的实际网络接口(interface)。
错误消息
OSError: [Errno 97] Address family not supported by protocol
仅在 WSL1 上可见。系统调用转换层不提供所有 Linux API 的完整实现,网络接口(interface)配置是未完全实现的领域之一。
因此,至少您需要转换为 WSL2 才能正常工作。假设 WSL2 已实际安装并在您的系统上可用,您可以通过以下方式执行此操作:
- 退出 WSL
- 来自 PowerShell:
- 使用
wsl -l -v
确认实例/发行版的名称. - (可选)使用
wsl --export distro_name path/to/backup.tar
备份现有实例 -
wsl --set-version <distro_name> 2
- 使用
您仍然应该了解,在 WSL2 下,网络接口(interface)确实在具有所有功能的普通 Linux 内核下运行。然而,它是一个运行在 NAT 虚拟交换机后面的虚拟 NIC。尽管 that's changing to some degree 目前对该开关没有太多控制权。 。也就是说,第 3 层在 WSL2 中应该可以正常工作,当然,尽管您需要确保拥有适当的权限。例如,您问题中的示例代码将需要 sudo
/根。
无论如何,您将在真正的虚拟机中更好地控制网络。如果您在 WSL2 中使用 scapy 遇到太多障碍,那么我会考虑切换到这个特定的用例。
关于python-3.x - 将 Scapy L3socket 与 WSL 结合使用时,协议(protocol)不支持地址族,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72622772/