Instantly share code, notes, and snippets.

What would you like to do?
Resend udp packet to another host using pcap
#!/usr/bin/env python
import pcap
import socket
from scapy.all import *
conf.use_pcap=True # because by default scapy try to use raw socket and can't assign bpf filter
conf.verb=0 # it just grab all traffic and get error on processing
import scapy.arch.pcapdnet
send_to_address = ""
send_to_port = 2055
bpf_filter="udp and dst port 9996"
def resend_action(p):
newpkt = craft_packet(p)
newpkt[IP].dst = send_to_address
newpkt[UDP].dport = send_to_port
def craft_packet(pkt):
# make new packet based on original packet
newpkt = Ether()/\
IP(src=pkt[IP].src, dst=pkt[IP].dst, id=pkt[IP].id)/\
UDP(sport=pkt[UDP].sport, dport=pkt[UDP].dport)/\
return newpkt
while 1:
sniff(filter=bpf_filter, iface=iface, prn=resend_action, store=0, promisc=False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment