Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用Scapy嗅探并发送UDP流量_Python_Sockets_Udp_Scapy - Fatal编程技术网

Python 使用Scapy嗅探并发送UDP流量

Python 使用Scapy嗅探并发送UDP流量,python,sockets,udp,scapy,Python,Sockets,Udp,Scapy,我按照下面的教程在Python中实现了数据包嗅探器: 在收到每个UDP数据包时,我想发送一个已经保存的pcap文件(test.pcap)。以下代码段显示了我的实现: # receive a packet while True: packet = s.recvfrom(65565) #packet string from tuple packet = packet[0] #parse ethernet header eth_length = 14 eth_heade

我按照下面的教程在Python中实现了数据包嗅探器:

在收到每个UDP数据包时,我想发送一个已经保存的pcap文件(test.pcap)。以下代码段显示了我的实现:

# receive a packet
while True:
  packet = s.recvfrom(65565)

  #packet string from tuple
  packet = packet[0]

  #parse ethernet header
  eth_length = 14

  eth_header = packet[:eth_length]
  eth = unpack('!6s6sH' , eth_header)
  eth_protocol = socket.ntohs(eth[2])
  print 'Destination MAC : ' + eth_addr(packet[0:6]) + ' Source MAC : ' + 
    eth_addr(packet[6:12]) + ' Protocol : ' + str(eth_protocol)

  if eth_addr(packet[6:12]) != my_MAC_address:

      #Parse IP packets, IP Protocol number = 8
      if eth_protocol == 8 :
      #Parse IP header
      #take first 20 characters for the ip header
      ip_header = packet[eth_length:20+eth_length]

      #now unpack them :)
      iph = unpack('!BBHHHBBH4s4s' , ip_header)

      version_ihl = iph[0]
      version = version_ihl >> 4
      ihl = version_ihl & 0xF

      iph_length = ihl * 4

      ttl = iph[5]
      protocol = iph[6]
      s_addr = socket.inet_ntoa(iph[8]);
      d_addr = socket.inet_ntoa(iph[9]);

      print 'Version : ' + str(version) + ' IP Header Length : ' + str(ihl) + ' TTL : ' + str(ttl) + ' Protocol : ' + str(protocol) + ' Source Address : ' + str(s_addr) + ' Destination Address : ' + str(d_addr)


      #UDP packets
      if protocol == 17 :
         u = iph_length + eth_length
         udph_length = 8
         udp_header = packet[u:u+8]

         #now unpack them :)
         udph = unpack('!HHHH' , udp_header)

         source_port = udph[0]
         dest_port = udph[1]
         length = udph[2]
         checksum = udph[3]

         print 'Source Port : ' + str(source_port) + ' Dest Port : ' + str(dest_port) + ' Length : ' + str(length) + ' Checksum : ' + str(checksum)

         h_size = eth_length + iph_length + udph_length
         data_size = len(packet) - h_size

         #get data from the packet
         data = packet[h_size:]

         print 'Data : ' + data
         my_pkt = rdpcap("test.pcap")
         sendp(my_pkt)
Test.pcap包含UDP_src=7777和UDP_dest=9999的UDP数据包

使用netcat生成的流量如下所示:

nc -u -p 7777 ip_dst_addr 9999
嗅探器只能接收第一条netcat消息并发送test.pcap作为响应。但随后的netcat MSG根本没有收到。但是,在netcat中使用任何其他UDP端口组合,嗅探器都可以正常工作。例如:以以下方式运行netcat:

nc -u -p 8888 ip_dst_addr 9999
没有问题,我能够发送test.pcap以响应每个UDP数据包/msg


任何帮助都将不胜感激

Scapy有几个内置的嗅探器,非常容易使用

这意味着你可以简单地做:

packets = rdpcap("test.pcap")
sniff(lfilter=lambda x: x.haslayer(UDP) and x[Ether].src==sending_mac and x[UDP].sport==port, prn=lambda x: send(packets))

这将把所有UDP数据包附加到test.pcap文件中

谢谢,您的实现要简单得多。但问题仍然存在,当我们使用与客户端消息中相同的udp端口发送test.pcap时,嗅探器无法嗅探数据包/是否要嗅探您发送的数据包?否则,这应该是可行的:/Don’我不想在嗅探器中嗅探传出的PKT。这就是为什么会出现以下if条件:if eth_addr(packet[6:12])!=我的MAC地址有两种数据包:1)我想嗅探的请求。它可以使用netcat生成。让我们假设UDP_src=7777和UDP_dst=9999 2)响应,在我们的例子中是test.pcap。我不想嗅探响应包。test.pcap包含一个UDP数据包,其中UDP_src=7777和UDP_dst=9999在收到第一个请求时,我可以发送test.pcap。但之后它无法嗅探UDP_src=7777和UDP_dst=9999的PKT在上述情况下,请求和响应使用相同的UDP端口。但是,通过使用不同的端口组合,一切正常。例如,对于请求=(UDP_src=8888,UDP_dst=9999)。对于响应=(UDP_src=7777,UDP_dst=9999)。谢谢,问题已经解决。我的人际网络中存在一个潜在问题
packets = rdpcap("test.pcap")
sniff(lfilter=lambda x: x.haslayer(UDP) and x[Ether].src==sending_mac and x[UDP].sport==port, prn=lambda x: send(packets))