Skip to content

Instantly share code, notes, and snippets.

@sorz
Created Sep 28, 2013
Embed
What would you like to do?
Send UDP packets from any custom port (using libnet). Used for UDP hole punching on linux servers (when the local UDP port is being used).
#!/usr/bin/env python
#encoding: utf-8
import libnet
from libnet.constants import RAW4, RESOLVE, IPV4_H, UDP_H, IPPROTO_UDP
IFACE = 'wlan2' # Sending via the interface.
def sendto(sport, address):
l = libnet.context(RAW4, IFACE)
dest_ip = l.name2addr4(address[0], RESOLVE)
l.build_udp(sp=sport, dp=address[1],
payload='\x00hehe'
l.autobuild_ipv4(len=(IPV4_H + UDP_H),
prot=IPPROTO_UDP, dst=dest_ip)
l.write()
if __name__ == '__main__':
sendto(6000, ('vpn.sorz.org', 6001))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment