【问题标题】:scapy sniffed udp packets can not be received after sending to another hostscapy嗅探的udp数据包发送到另一台主机后无法接收
【发布时间】:2021-04-04 13:33:36
【问题描述】:

我正在尝试将scapy 嗅探到的udp 数据包发送到另一台主机。主机地址可通过我的默认网关访问。

在主机中我通过tcpdump监听目标端口10000但是没有收到数据包。

我的代码是这样的:

from scapy.all import *

class Des:
    def __init__(self, port):
        self.port = port

def send_packets(port):
    des = Des(port)
    def get_pack(packet):
        pkt = packet.copy()
        pkt['IP'].dst= "192.168.20.111" # address of the destination host
        pkt['IP'].src = "192.168.12.111" # address of my system
        pkt['UDP'].dport = des.port
        pkt['Ethernet'].dst = dst_mac
        pkt['Ethernet'].src = src_mac
        send(pkt)
    return get_pack

sniff(filter = 'udp and port 50000', prn = send_packets(10000))

如果我将数据包发送到网络中的另一台主机192.168.12.112,问题仍然存在。在这种情况下,如果我将send 行替换为以下行,这些数据包将在目的地收到!

send(IP(dst='192.168.12.112')/UDP(dport=10000))

将其替换为以下行时,会导致主机192.168.20.111中没有接收数据包

send(IP(dst='192.168.20.111')/UDP(dport=10000))

我搜索了问题,但没有找到结果。两边的防火墙都被禁用了,scapy的路由路径如下输出。

>>> conf.route
Network         Netmask         Gateway         Iface           Output IP
0.0.0.0         0.0.0.0         192.168.12.1    vr0             192.168.12.111
192.168.12.0    255.255.255.0   0.0.0.0         vr0             192.168.12.111 

此外,我的输出界面上的tcpdump显示使用时

send(IP(dst='ANY_DST)/UDP(dport=ANY_PORT))

数据包正在发出,但是当我发送嗅探的数据包时,它们没有发出!

我哪里做错了? 问题可能出在更改的数据包上吗?它们是包含有效负载的RTP 数据包。

我对 python 和 scapy 非常陌生。任何帮助都可以成为朝着正确方向前进的光。感谢您的宝贵时间。

我的操作系统是FreeBSD9.2,我正在使用python 2.7scapy (2.2.0)

【问题讨论】:

  • “主机地址可通过我的默认网关访问”:您已使用ping 验证了这一点? tracert 对它有什么奇怪的表现吗?
  • 我觉得很奇怪。在scapy中我使用traceroute('192.168.20.111'),输出显示:第一步是我的default gateway,第二步是192.168.20.111

标签: python sockets networking scapy packet


【解决方案1】:

如果我用以下行替换发送行,这些数据包将 在目的地收到!

发送(IP(dst='192.168.12.112')/UDP(dport=10000))

在调试了代码,查看了scapy的源码后,找到了出现上述问题的原因。当我调用send(pkt) 时,从类L3dnetSocket 调用以下函数。

        def send(self, x):
            iff,a,gw  = x.route()
            # Rest of the code is ignored here

这里的对象x属于class Packet,它的route函数总是为gateway返回None!所以数据包将在第 2 层发送。

    def route(self):
        return (None,None,None)

当我调用send(IP(dst='192.168.12.112')/UDP(dport=10000)) 时,对象类型xclass IP,它是route 函数返回正确的gateway

    def route(self):
        dst = self.dst
        if isinstance(dst,Gen):
            dst = iter(dst).next()
        return conf.route.route(dst)

最后,我将我的代码更改为以下代码,它可以正常工作:)

def send_packet(port):
    des = Des(port)
    def get_pack(packet):
        udp = UDP()
        udp.sport = packet[UDP].sport
        udp.dport = des.port
        udp.len = packet[UDP].len

        ip = IP()
        ip.version = packet[IP].version
        ip.tos = packet[IP].tos
        ip.len = packet[IP].len
        ip.id = packet[IP].id
        ip.flags = packet[IP].flags
        ip.frag = packet[IP].frag
        ip.ttl = packet[IP].ttl
        ip.proto = packet[IP].proto
        ip.dst = '192.168.12.112'

        payload = packet[Raw].load

        pkt = ip/udp/payload
        pkt = pkt.__class__(bytes(pkt))

        send(pkt)

    return get_pack

此外,在我的可达网络中没有收到数据包的原因是来自我的网络:)

【讨论】:

    猜你喜欢
    • 2015-07-28
    • 1970-01-01
    • 2015-04-02
    • 1970-01-01
    • 2023-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-03
    相关资源
    最近更新 更多