UDP Packets send via Scapy are not received by a plain Python script

48 Views Asked by At

I need help with sending UDP packets via Scapy. For some reason, I cannot receive the packets that I'm sending - I see these packets in Wireshark, but not in the UDP client.

I'm running a simplest UDP client for testing:

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}")

And I'm sending the packet like that:

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")

I can see the packet I sent in Wireshark, but the client does not print its content - it looks like client did not receive the packet. For comparison, sending the packet via plain Python works:

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!")

I need to use Scapy for my task, because in the future I will be sending packets parsed from a .pcap file, so I cannot just use plain Python. Please, help me find a way to receive the UDP packet sent from Scapy!

1

There are 1 best solutions below

0
Cukic0d On

The loopback interface has special limitations on Linux, see the troubleshooting page: 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

Most likely what you need is to do

conf.L3socket = L3RawSocket