Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@clickfreak
Last active August 18, 2016 04:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save clickfreak/f272aee687653996da0b to your computer and use it in GitHub Desktop.
Save clickfreak/f272aee687653996da0b to your computer and use it in GitHub Desktop.
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 = "192.168.0.5"
send_to_port = 2055
bpf_filter="udp and dst port 9996"
iface="eth0"
def resend_action(p):
newpkt = craft_packet(p)
newpkt[IP].dst = send_to_address
newpkt[UDP].dport = send_to_port
send(newpkt[IP])
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)/\
Raw(load=pkt[Raw].load)
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