Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python2
- #####################
- # Goodies TCP Proxy #
- #####################
- # TCPiece #
- #####################
- ### This is a TCP Proxy created for the specific
- ### reason of TCP session splicing. Each TCP
- ### packet is broken up and sent to the server in
- ### a group of a set number of bytes which will
- ### attempt to prevent any one individual packet
- ### from being detected by a signature based IDS.
- ###
- ### This is not an all-around bypass method, but
- ### it may help in many cases.
- ###
- ### SIMPLY PROXY ONLY VERSION
- ###
- from sys import *
- import time
- import threading
- from socket import *
- from select import *
- import os
- from optparse import OptionParser
- parser = OptionParser()
- parser.add_option("-l", dest="listenIPPort", default="127.0.0.1:7777", metavar="[IP:PORT]")
- parser.add_option("-d", dest="destinationIPPort", metavar="[IP:PORT]")
- parser.add_option("-s", dest="bufferSize", metavar="Size", default=int(8192))
- parser.add_option("-w", dest="wait", metavar="Wait (s)", default=float(0))
- parser.add_option("-v", dest="verbose", metavar="[0-2]", default=int(1))
- options, args = parser.parse_args()
- if len(options.listenIPPort.split(":"))!=2 or len(options.listenIPPort.split(":")[0].split("."))!=4:
- parser.print_help()
- exit(-1)
- if options.destinationIPPort==None:
- parser.print_help()
- exit(-1)
- if len(options.destinationIPPort.split(":"))!=2 or len(options.destinationIPPort.split(":")[0].split("."))!=4:
- parser.print_help()
- exit(-1)
- class TCPieceServer():
- inputStream = []
- channel = {}
- msg = ""
- def __init__(self, ip, port):
- self.tcpiece_stream = socket(AF_INET, SOCK_STREAM)
- print "\nListening on:\t%s:%i\t%s" % (ip, port, options.destinationIPPort)
- self.tcpiece_server = socket(AF_INET, SOCK_STREAM)
- self.tcpiece_server.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
- self.tcpiece_server.bind((ip, port))
- self.tcpiece_server.listen(1)
- def proxy(self, ip, port):
- try:
- self.tcpiece_stream.connect((ip, port))
- return self.tcpiece_stream
- except Exception, e:
- if int(options.verbose)>0:
- print "\nError"
- if int(options.verbose)==2:
- print (e)
- return False
- def startServer(self):
- self.inputStream.append(self.tcpiece_server)
- while 1:
- if float(options.wait)!=float(0):
- time.sleep(float(options.wait))
- sel = select
- try:
- readReady, _, _ = sel(self.inputStream, [], [])
- except KeyboardInterrupt:
- print "Exiting..."
- exit(0)
- i = 0
- self.msg=""
- for self.s in readReady:
- #print "SSS: " + `self.s`
- if self.s == self.tcpiece_server:
- self.acceptConnection()
- break
- if self.s == self.connSocket:
- size=int(options.bufferSize)
- if int(options.verbose)==1 or int(options.verbose)==2:
- self.msg="Sent: "
- elif self.s == self.forwardStream:
- size=8192
- if int(options.verbose)==1 or int(options.verbose)==2:
- self.msg="Received: "
- try:
- data = self.s.recv(size)
- if int(options.verbose)==1:
- print self.msg + "%i bytes" % len(data)
- elif int(options.verbose)==2:
- if data !="\n" and len(data)>0:
- print self.msg + "%s" % data.replace("\n","")
- self.channel[self.s].send(data)
- except Exception, e:
- self.dropConnection()
- def acceptConnection(self):
- destinationIP = options.destinationIPPort.split(":")[0]
- destinationPort = int(options.destinationIPPort.split(":")[1])
- self.forwardStream = self.proxy(destinationIP, destinationPort)
- self.connSocket, self.connAddress = self.tcpiece_server.accept()
- ip, port = self.connAddress
- #print "FWD: " + `self.forwardStream`
- #print "CNS: " + `self.connSocket`
- if self.forwardStream:
- print "New Connection:\t%s:%s" % (ip, port)
- self.inputStream.append(self.connSocket)
- self.inputStream.append(self.forwardStream)
- self.channel[self.connSocket] = self.forwardStream
- self.channel[self.forwardStream] = self.connSocket
- else:
- print "Server Not Available"
- self.connSocket.close()
- def dropConnection(self):
- print "\t\t%s:%i Disconnected" % (self.s.getpeername()[0], self.s.getpeername()[1])
- self.inputStream.remove(self.s)
- self.inputStream.remove(self.channel[self.s])
- out = self.channel[self.s]
- self.channel[out].close()
- self.channel[self.s].close()
- del self.channel[out]
- del self.channel[self.s]
- if __name__ == '__main__':
- listenIP = options.listenIPPort.split(":")[0]
- listenPort = int(options.listenIPPort.split(":")[1])
- TCPieceServer(listenIP, listenPort).startServer()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement