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

Python 无法接收发送的Scapy数据包,python,sockets,networking,packet,scapy,Python,Sockets,Networking,Packet,Scapy,我正在尝试使用以下命令使用scapy发送UDP数据包: >> send(IP(dst="127.0.0.1",src="111.111.111.111")/UDP(dport=5005)/"Hello") . Sent 1 packets. 从tcpdump我可以看到: 22:02:58.384730 IP 111.111.111.111.domain > localhost.5005: [|domain] 我正在尝试使用以下代码接收此数据包: import socket

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

>> send(IP(dst="127.0.0.1",src="111.111.111.111")/UDP(dport=5005)/"Hello")
.
Sent 1 packets.
tcpdump
我可以看到:

22:02:58.384730 IP 111.111.111.111.domain > localhost.5005: [|domain]
我正在尝试使用以下代码接收此数据包:

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
但无法接收消息

我已经通过正常发送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)
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
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
任何帮助都将不胜感激

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

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

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)
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
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
13:22:52.984862 IP(tos 0x0,ttl 64,id 1,偏移量0,标志[DF],协议UDP(17),长度33)
127.0.0.1.5555>127.0.0.1.12345:[udp总和确定]udp,长度5
0x0000:4500 0021 0001 4000 4011 3cc9 7f00 0001 E.。@。127.0.0.1.12345:[错误的udp校验和0xfe20->0xde2e!]udp,长度5

0x0000:4500 0021 0000 4000 4011 3cca 7f00 0001 E.。@ 看起来您正在使用Scapy将UDP通信发送到本地主机接口。在
send()
函数中,指定适当的出站接口以发送通信量

例如:

send((IP(dst="127.0.0.1",src="111.111.111.111")/UDP(dport=5005)/"Hello"),iface="lo0")

在我的计算机上,lo0是我的本地环回接口。要查看或设置scapy的默认界面,请查看本文的下半部分:

您可以使用nfqueue和iptables: 您可以定义一个规则,将数据包定向到队列,然后使用脚本截获它们

以下是一个基本示例:

import nfqueue, socket
from scapy.all import *
import os

#add iptables rule
os.system('iptables -A OUTPUT -j NFQUEUE --queue-num 0')
#since you are sending packets from your machine you can get them in the OUPUT hook or even in the POSTROUTING hook.

#Set the callback for received packets. The callback should expect the payload:
def cb(payload):
    data = payload.get_data()
    p = IP(data)
    #your manipulation


q = nfqueue.queue()
q.open()
q.unbind(socket.AF_INET)
q.bind(socket.AF_INET)
q.set_callback(cb)
q.create_queue(0) #Same queue number of the rule

try:
    q.try_run()
except KeyboardInterrupt, e:
    os.system('iptables -t -F') #remove iptables rule
    print "interruption"
    q.unbind(socket.AF_INET)
    q.close()

谢谢你,马特!我找到了
ifconfig
,找到了
eth0
eth1
lo
。我尝试了每一种,但仍然不起作用…因为你可以用tcpdump嗅探Scapy包,我认为这不是Scapy的问题。我发现这个问题有一个相似的线索:我有同样的问题。我创建了一个非scapy程序,可以接收数据包。比较wireshark中可用的数据包和不可用的数据包,可以看出唯一的区别是scapy数据包使用的是第2层广播地址。如果我在数据包中指定MAC地址,则会用广播地址覆盖我指定的MAC地址。这些输出是否在同一接口上?两个数据包中的源MAC地址和目标MAC地址不同。在某些情况下,由于错误的目的地MAC地址,数据包无法到达目的地。