将 Scapy L3socket 与 WSL 结合使用时,协议不支持地址族

vta*_*ble 2 sockets scapy python-3.x windows-subsystem-for-linux wsl-2

我尝试使用 Scapy 配置 L3 套接字,如下所示:

from scapy.all import *
soc = conf.L3socket(iface="eth1")
Run Code Online (Sandbox Code Playgroud)

当我在 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
Run Code Online (Sandbox Code Playgroud)

我该如何解决这个问题?

Not*_*1ds 5

首先,我有一种强烈的预感,您实际上正在使用 WSL1 实例。首先使用 进行双重检查wsl.exe -l -v

安装/启用 WSL2 但仍有从未转换为 WSL2 的 WSL1 实例是很常见的情况。

有几点表明这是 WSL1:

  • 您有一个eth1接口 - WSL2 通常没有,因为它只设置一个虚拟以太网设备供 Linux 内核使用。

    另一方面,WSL1 使用 Windows API(通过系统调用转换层)来枚举 Windows 可用的实际网络接口。

  • 该错误消息OSError: [Errno 97] Address family not supported by protocol仅在 WSL1 上可见。系统调用转换层不提供所有 Linux API 的完整实现,网络接口配置是未完全实现的领域之一。

因此,至少您需要转换为 WSL2 才能正常工作。假设 WSL2 已实际安装并在您的系统上可用,您可以通过以下方式执行此操作:

  • 退出 WSL
  • 来自 PowerShell:
    • 确认实例/发行版的名称wsl -l -v
    • (可选)备份现有实例wsl --export distro_name path/to/backup.tar
    • wsl --set-version <distro_name> 2

您仍然应该了解,在 WSL2 下,网络接口确实在具有所有功能的普通 Linux 内核下运行。然而,它是一个运行在 NAT 虚拟交换机后面的虚拟 NIC。目前对该开关没有太多控制,尽管这种情况在某种程度上正在发生变化。也就是说,第 3 层在 WSL2 中应该可以正常工作,当然,尽管您需要确保拥有适当的权限。例如,您问题中的示例代码将需要sudo/root。

但无论如何,您将在真正的虚拟机中更好地控制网络。如果您在 WSL2 中使用 scapy 遇到太多障碍,那么我会考虑切换到这个特定的用例。