Advertisement
GoodiesHQ

TCPiece ~ TCP Session Splicing Proxy by Goodies

Mar 2nd, 2014
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.51 KB | None | 0 0
  1. #!/usr/bin/env python2
  2.         #####################
  3.         # Goodies TCP Proxy #
  4.         #####################
  5.         #      TCPiece      #
  6.         #####################
  7. ### This is a TCP Proxy created for the specific
  8. ### reason of TCP session splicing. Each TCP
  9. ### packet is broken up and sent to the server in
  10. ### a group of a set number of bytes which will
  11. ### attempt to prevent any one individual packet
  12. ### from being detected by a signature based IDS.
  13. ###
  14. ### This is not an all-around bypass method, but
  15. ### it may help in many cases.
  16. ###
  17. ### SIMPLY PROXY ONLY VERSION
  18. ###
  19.  
  20. from sys import *
  21. import time
  22. import threading
  23. from socket import *
  24. from select import *
  25. import os
  26. from optparse import OptionParser
  27.    
  28. parser = OptionParser()
  29. parser.add_option("-l", dest="listenIPPort", default="127.0.0.1:7777", metavar="[IP:PORT]")
  30. parser.add_option("-d", dest="destinationIPPort", metavar="[IP:PORT]")
  31. parser.add_option("-s", dest="bufferSize", metavar="Size", default=int(8192))
  32. parser.add_option("-w", dest="wait", metavar="Wait (s)", default=float(0))
  33. parser.add_option("-v", dest="verbose", metavar="[0-2]", default=int(1))
  34.  
  35. options, args = parser.parse_args()
  36. if len(options.listenIPPort.split(":"))!=2 or len(options.listenIPPort.split(":")[0].split("."))!=4:
  37.     parser.print_help()
  38.     exit(-1)
  39. if options.destinationIPPort==None:
  40.     parser.print_help()
  41.     exit(-1)
  42. if len(options.destinationIPPort.split(":"))!=2 or len(options.destinationIPPort.split(":")[0].split("."))!=4:
  43.     parser.print_help()
  44.     exit(-1)
  45.            
  46. class TCPieceServer():
  47.     inputStream = []
  48.     channel = {}
  49.     msg = ""
  50.     def __init__(self, ip, port):
  51.         self.tcpiece_stream = socket(AF_INET, SOCK_STREAM)
  52.         print "\nListening on:\t%s:%i\t%s" % (ip, port, options.destinationIPPort)
  53.         self.tcpiece_server = socket(AF_INET, SOCK_STREAM)
  54.         self.tcpiece_server.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
  55.         self.tcpiece_server.bind((ip, port))
  56.         self.tcpiece_server.listen(1)
  57.        
  58.     def proxy(self, ip, port):
  59.         try:
  60.             self.tcpiece_stream.connect((ip, port))
  61.             return self.tcpiece_stream
  62.         except Exception, e:
  63.             if int(options.verbose)>0:
  64.                 print "\nError"
  65.             if int(options.verbose)==2:
  66.                 print (e)
  67.             return False
  68.            
  69.     def startServer(self)
  70.         self.inputStream.append(self.tcpiece_server)
  71.         while 1:
  72.             if float(options.wait)!=float(0):
  73.                 time.sleep(float(options.wait))
  74.             sel = select
  75.             try:
  76.                 readReady, _, _ = sel(self.inputStream, [], [])
  77.             except KeyboardInterrupt:
  78.                 print "Exiting..."
  79.                 exit(0)
  80.             i = 0
  81.             self.msg=""
  82.             for self.s in readReady:
  83.                 #print "SSS: " + `self.s`
  84.                 if self.s == self.tcpiece_server:
  85.                     self.acceptConnection()
  86.                     break
  87.                 if self.s == self.connSocket:
  88.                     size=int(options.bufferSize)
  89.                     if int(options.verbose)==1 or int(options.verbose)==2:
  90.                         self.msg="Sent: "
  91.                 elif self.s == self.forwardStream:
  92.                     size=8192
  93.                     if int(options.verbose)==1 or int(options.verbose)==2:
  94.                         self.msg="Received: "
  95.                 try:
  96.                     data = self.s.recv(size)
  97.                     if int(options.verbose)==1:
  98.                         print self.msg + "%i bytes" % len(data)
  99.                     elif int(options.verbose)==2:
  100.                         if data !="\n" and len(data)>0:
  101.                             print self.msg + "%s" % data.replace("\n","")
  102.                     self.channel[self.s].send(data)
  103.                 except Exception, e:
  104.                     self.dropConnection()
  105.                
  106.     def acceptConnection(self):
  107.         destinationIP = options.destinationIPPort.split(":")[0]
  108.         destinationPort = int(options.destinationIPPort.split(":")[1])
  109.         self.forwardStream = self.proxy(destinationIP, destinationPort)
  110.         self.connSocket, self.connAddress = self.tcpiece_server.accept()
  111.         ip, port = self.connAddress
  112.         #print "FWD: " + `self.forwardStream`
  113.         #print "CNS: " + `self.connSocket`
  114.         if self.forwardStream:
  115.             print "New Connection:\t%s:%s" % (ip, port)
  116.             self.inputStream.append(self.connSocket)
  117.             self.inputStream.append(self.forwardStream)
  118.             self.channel[self.connSocket] = self.forwardStream
  119.             self.channel[self.forwardStream] = self.connSocket
  120.         else:
  121.             print "Server Not Available"
  122.             self.connSocket.close()
  123.  
  124.     def dropConnection(self):
  125.         print "\t\t%s:%i Disconnected" % (self.s.getpeername()[0], self.s.getpeername()[1])
  126.         self.inputStream.remove(self.s)
  127.         self.inputStream.remove(self.channel[self.s])
  128.         out = self.channel[self.s]
  129.         self.channel[out].close()
  130.         self.channel[self.s].close()
  131.         del self.channel[out]
  132.         del self.channel[self.s]
  133.  
  134. if __name__ == '__main__':
  135.     listenIP = options.listenIPPort.split(":")[0]
  136.     listenPort = int(options.listenIPPort.split(":")[1])
  137.     TCPieceServer(listenIP, listenPort).startServer()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement