通过 Scapy 发送的 UDP 数据包不会被普通的 Python 脚本接收到

问题描述 投票:0回答:1

我需要通过 Scapy 发送 UDP 数据包的帮助。由于某种原因,我无法接收我发送的数据包 - 我在 Wireshark 中看到这些数据包,但在 UDP 客户端中看不到。

我正在运行一个最简单的 UDP 客户端进行测试:

import socket

UDP_IP = "127.0.0.1"
UDP_PORT = 25516

print("Waiting for packet...")
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
    s.bind((UDP_IP, UDP_PORT))
    data, addr = s.recvfrom(1024)
    print(f"Received message from {addr}: {data}")

我就这样发送数据包:

from scapy.layers.inet import *
from scapy.sendrecv import send
# The interface is the correct one, but I tried without it as well
send(IP(dst="127.0.0.1") / UDP(sport=25515, dport=25516) / "Test123", iface="enp5s0")

我可以在 Wireshark 中看到我发送的数据包,但客户端没有打印其内容 - 看起来客户端没有收到该数据包。作为比较,通过普通 Python 发送数据包是可行的:

import socket

UDP_IP = "127.0.0.1"
UDP_PORT = 25516

with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
    s.sendto("Test123".encode(), (UDP_IP, UDP_PORT))
    print("Message sent!")

我需要使用 Scapy 来完成我的任务,因为将来我将发送从

.pcap
文件解析的数据包,所以我不能只使用普通的 Python。请帮我想办法接收Scapy发送的UDP数据包!

python sockets networking udp scapy
1个回答
0
投票

环回接口在 Linux 上有特殊限制,请参阅故障排除页面:https://scapy.readthedocs.io/en/latest/troubleshooting.html#i-can-t-ping-127-0-0-1- or-1-scapy-does-not-work-with-127-0-0-1-or-1-on-the-loopback-interface

很可能您需要做的是

conf.L3socket = L3RawSocket
© www.soinside.com 2019 - 2024. All rights reserved.