发送的Scapy数据包无法接收

eth*_*jyx 6 python sockets networking scapy packet

我正在尝试使用以下命令发送带有scapy的UDP数据包:

>> send(IP(dst="127.0.0.1",src="111.111.111.111")/UDP(dport=5005)/"Hello")
.
Sent 1 packets.
Run Code Online (Sandbox Code Playgroud)

tcpdump我可以看出:

22:02:58.384730 IP 111.111.111.111.domain > localhost.5005: [|domain]
Run Code Online (Sandbox Code Playgroud)

我正在尝试使用以下代码接收此数据包:

import socket

UDP_IP = "127.0.0.1"
UDP_PORT = 5005

sock = socket.socket(socket.AF_INET, # Internet
                     socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, UDP_PORT))

while True:
    data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
    print "received message:", data
Run Code Online (Sandbox Code Playgroud)

但无法收到消息.

我通过使用以下代码正常发送udp数据包来测试网络,并且可以接收数据包:

import socket
import time

UDP_IP = "127.0.0.1"
UDP_PORT = 5005

print "UDP target IP:", UDP_IP
print "UDP target port:", UDP_PORT

sock = socket.socket(socket.AF_INET, # Internet
                     socket.SOCK_DGRAM) # UDP
num = 0
while True:
  sock.sendto(str(num), (UDP_IP, UDP_PORT))
  print "Message sent: " + str(num)
  num += 1
  time.sleep(1)
Run Code Online (Sandbox Code Playgroud)

任何帮助将不胜感激.

- - - - - - - - 更新 - - - - - - - - - - - -

Scapy发送的无法接收的数据包:

13:22:52.984862 IP (tos 0x0, ttl 64, id 1, offset 0, flags [DF], proto UDP (17), length 33)
    127.0.0.1.5555 > 127.0.0.1.12345: [udp sum ok] UDP, length 5
    0x0000:  4500 0021 0001 4000 4011 3cc9 7f00 0001  E..!..@.@.<.....
    0x0010:  7f00 0001 15b3 3039 000d 9813 4865 6c6c  ......09....Hell
    0x0020:  6f     

                              o
Run Code Online (Sandbox Code Playgroud)

通过普通python脚本发送的数据包可以接收:

13:20:02.374481 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto UDP (17), length 33)
    127.0.0.1.53143 > 127.0.0.1.12345: [bad udp cksum 0xfe20 -> 0xde2e!] UDP, length 5
    0x0000:  4500 0021 0000 4000 4011 3cca 7f00 0001  E..!..@.@.<.....
    0x0010:  7f00 0001 cf97 3039 000d fe20 4865 6c6c  ......09....Hell
    0x0020:  6f
Run Code Online (Sandbox Code Playgroud)

小智 2

看起来您正在使用 Scapy 将 UDP 流量发送到本地主机接口。在该send()函数中,指定适当的出站接口以将流量发送出去。

例子:

send((IP(dst="127.0.0.1",src="111.111.111.111")/UDP(dport=5005)/"Hello"),iface="lo0")
Run Code Online (Sandbox Code Playgroud)

在我的计算机上,lo0 是我的本地环回接口。要查看或设置 scapy 的默认界面,请查看这篇文章的下半部分:http ://thepacketgeek.com/scapy-p-02-installing-python-and-scapy/

  • 谢谢马特!我找到了“ifconfig”并找到了“eth0”、“eth1”和“lo”。我尝试了每一个,但仍然不起作用...... (2认同)