Skip to content

Instantly share code, notes, and snippets.

@sorz
Created September 28, 2013 18:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sorz/6744884 to your computer and use it in GitHub Desktop.
Save sorz/6744884 to your computer and use it in GitHub Desktop.
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