SHARE
TWEET

Untitled

a guest Aug 13th, 2017 50 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #! /usr/bin/env python
  2.  
  3. import os, sys
  4. from socket import *
  5. from fcntl import ioctl
  6. from select import select
  7. import getopt, struct
  8.  
  9. MAGIC_WORD = "EknathVenkataramani"
  10.  
  11. TUNSETIFF = 0x400454ca
  12. IFF_TUN   = 0x0001
  13. IFF_TAP   = 0x0002
  14.  
  15. TUNMODE = IFF_TUN
  16. MODE = 0
  17. DEBUG = 0
  18.  
  19. def usage(status=0):
  20.     print "Usage: tunproxy [-s port|-c targetip:port] [-e]"
  21.     sys.exit(status)
  22.  
  23. opts = getopt.getopt(sys.argv[1:],"s:c:ehd")
  24.  
  25. for opt,optarg in opts[0]:
  26.     if opt == "-h":
  27.         usage()
  28.     elif opt == "-d":
  29.         DEBUG += 1
  30.     elif opt == "-s":
  31.         '''Server Mode> So get pinged by client and then send data'''
  32.         MODE = 1
  33.         PORT = int(optarg)
  34.     elif opt == "-c":
  35.         '''CLient Mode. So ping the server and receive the data'''
  36.         MODE = 2
  37.         IP,PORT = optarg.split(":")
  38.         PORT = int(PORT)
  39.         peer = (IP,PORT)
  40.     elif opt == "-e":
  41.         TUNMODE = IFF_TAP
  42.        
  43. if MODE == 0:
  44.     usage(1)
  45.  
  46.  
  47. f = os.open("/dev/net/tun", os.O_RDWR)
  48. '''tun is the interface which processes IP packets'''
  49.  
  50. ifs = ioctl(f, TUNSETIFF, struct.pack("16sH", "eknathtun%d", TUNMODE))
  51. '''Create the TUN interface'''
  52.  
  53. ifname = ifs[:16].strip("\x00")
  54. '''Get the interface name'''
  55. print "Allocated interface %s!" % ifname
  56.  
  57. s = socket(AF_INET, SOCK_DGRAM)
  58. '''Open socket for communication'''
  59. try:
  60.     if MODE == 1:'''Used while receiving data (ping packet)'''
  61.        
  62.         s.bind(("", PORT))
  63.         while 1:
  64.             word,peer = s.recvfrom(1500)
  65.             '''_1500_ is the buffer size, indicating the maxmum size of data to be received at once
  66.             _word_ is a string representing the data received and _peer_ is the address of the socket sending the data'''
  67.             if word == MAGIC_WORD:
  68.                 break
  69.             print "Bad magic word for %s:%i" % peer
  70.         s.sendto(MAGIC_WORD, peer)
  71.         '''peer is the name of the destination '''
  72.    
  73.     else:'''Used while sending data (ping packet)'''
  74.         s.sendto(MAGIC_WORD, peer)
  75.         '''peer is the destination to send the data. In this case this comes from the command line arg and is a tuple containing the (destinationIP,port#)'''
  76.         word,peer = s.recvfrom(1500)
  77.         '''_1500_ is the buffer size, indicating the maxmum size of data to be received at once'''
  78.         '''_word_ is a string representing the data received and _peer_ is the address of the socket sending the data'''
  79.         if word != MAGIC_WORD:
  80.             print "Bad magic word for %s:%i" % peer
  81.             sys.exit(2)
  82.     print "Connection with %s:%i established" % peer
  83.    
  84.     while 1:
  85.         r = select([f,s],[],[])[0][0]
  86.         '''wait until _f_ or _s_ is ready for reading '''
  87.         if r == f:
  88.             if DEBUG: os.write(1,">")
  89.             s.sendto(os.read(f,1500),peer)
  90.         else:
  91.             buf,p = s.recvfrom(1500)
  92.             if p != peer:
  93.                 print "Got packet from %s:%i instead of %s:%i" % (p+peer)
  94.                 continue
  95.             if DEBUG: os.write(1,"<")
  96.             os.write(f, buf)
  97. except KeyboardInterrupt:
  98.     print "Exitting....!"
RAW Paste Data
Top