监听指定网卡中的数据(UDP) 数据转发 具体实现代码如下:
from scapy. all import *
from scapy. layers. inet import UDP, IP
from scapy. layers. l2 import Etherdef Callback ( packet) : dst_ip = packet[ IP] . dstsrc_ip = packet[ IP] . srcdst_port = packet[ UDP] . dportvalue = packet[ 'Raw' ] . loaddst_mac = packet[ 'Ether' ] . dstsrc_mac = packet[ 'Ether' ] . srcprint ( f"收: { packet} " ) forward_udp( dst_mac, src_mac, dst_ip, src_ip, dst_port, value) def forward_udp ( dst_mac, src_mac, dst_ip, src_ip, dst_port, value) : """转发数据至指定目标:param dst_mac: 目的MAC:param src_mac: 源MAC:param dst_ip: 目的IP:param src_ip: 源IP:param dst_port: 目的端口:param value: 数据:return:""" eth = Ether( dst= dst_mac, src= src_mac) ip = IP( src= src_ip, dst= dst_ip) udp = UDP( dport= dst_port + 1 ) packet = eth / ip / udp / Raw( value) print ( f"发: { packet} " ) sendp( packet, iface= "WLAN" ) if __name__ == '__main__' : IP = '192.168.3.1' Port = '9999' sniff( filter = f"udp and host { IP} and port { Port} " , iface= "WLAN" , count= - 1 , prn= Callback)