(self)
o  X'00' NO AUTHENTICATION REQUIRED
		o  X'01' GSSAPI
		o  X'02' USERNAME/PASSWORD
		o  X'03' to X'7F' IANA ASSIGNED
		o  X'80' to X'FE' RESERVED FOR PRIVATE METHODS
		o  X'FF' NO ACCEPTABLE METHODS
o  X'00' NO AUTHENTICATION REQUIRED
		o  X'01' GSSAPI
		o  X'02' USERNAME/PASSWORD
		o  X'03' to X'7F' IANA ASSIGNED
		o  X'80' to X'FE' RESERVED FOR PRIVATE METHODS
		o  X'FF' NO ACCEPTABLE METHODS
o X 00 NO AUTHENTICATION REQUIRED o X 01 GSSAPI o X 02 USERNAME / PASSWORD o X 03 to X 7F IANA ASSIGNED o X 80 to X FE RESERVED FOR PRIVATE METHODS o X FF NO ACCEPTABLE METHODS
def handle(self):
		self.request.settimeout(5)
		VER=self.request.recv(1)
		NMETHODS=self.request.recv(1)
		METHODS=self.request.recv(int(struct.unpack("!B",NMETHODS)[0]))
		"""
		o  X'00' NO AUTHENTICATION REQUIRED
		o  X'01' GSSAPI
		o  X'02' USERNAME/PASSWORD
		o  X'03' to X'7F' IANA ASSIGNED
		o  X'80' to X'FE' RESERVED FOR PRIVATE METHODS
		o  X'FF' NO ACCEPTABLE METHODS
		"""
		#for now only no authentication is supported :
		self.request.sendall("\x05\x00")
		VER=self.request.recv(1)
		if VER!="\x05":
			self.server.module.error("receiving unsuported socks version: %s"%VER.encode('hex'))
			self._socks_response(CODE_GENERAL_SRV_FAILURE, terminate=True)
			return

		CMD=self.request.recv(1)
		if CMD!="\x01": # we only support CONNECT for now
			self.server.module.error("receiving unsuported socks CMD: %s"%CMD.encode('hex'))
			self._socks_response(CODE_COMMAND_NOT_SUPPORTED, terminate=True)
			return

		RSV=self.request.recv(1)

		DST_ADDR=None
		DST_PORT=None
		ATYP=self.request.recv(1)
		if ATYP=="\x01":
			DST_ADDR=".".join([str(ord(x)) for x in self.request.recv(4)])
			DST_PORT=struct.unpack("!H",self.request.recv(2))[0]
		elif ATYP=="\x03":
			DOMAIN_LEN=int(struct.unpack("!B",self.request.recv(1))[0])
			DST_ADDR=self.request.recv(DOMAIN_LEN)
			DST_PORT=struct.unpack("!H",self.request.recv(2))[0]
		else: #TODO: ipv6
			self.server.module.error("atyp not supported: %s"%ATYP.encode('hex'))
			self._socks_response(CODE_ADDRESS_TYPE_NOT_SUPPORTED, terminate=True)
			return

		#now we have all we need, we can open the socket proxyfied through rpyc :)
		self.server.module.info("connecting to %s:%s ..."%(DST_ADDR,DST_PORT))
		rsocket_mod=self.server.rpyc_client.conn.modules.socket
		rsocket=rsocket_mod.socket(rsocket_mod.AF_INET,rsocket_mod.SOCK_STREAM)
		rsocket.settimeout(5)
		try:
			rsocket.connect((DST_ADDR, DST_PORT))
		except Exception as e:
			self.server.module.error("error %s connecting to %s:%s ..."%(str(e),DST_ADDR,DST_PORT))
			if e[0]==10060:
				self._socks_response(CODE_HOST_UNREACHABLE, terminate=True)
			else:
				self._socks_response(CODE_NET_NOT_REACHABLE, terminate=True)
			return
		self._socks_response(CODE_SUCCEEDED)
		self.server.module.success("connection to %s:%s succeed !"%(DST_ADDR,DST_PORT))

		#self.request.settimeout(30)
		#rsocket.settimeout(30)

		sp1=SocketPiper(self.request, rsocket)
		sp2=SocketPiper(rsocket, self.request)
		sp1.start()
		sp2.start()
		sp1.join()
		sp2.join()
		self.server.module.info("conn to %s:%s closed"%(DST_ADDR,DST_PORT))
def handle ( self ) : self . request . settimeout ( 5 ) VER = self . request . recv ( 1 ) NMETHODS = self . request . recv ( 1 ) METHODS = self . request . recv ( int ( struct . unpack ( "!B" , NMETHODS ) [ 0 ] ) ) #for now only no authentication is supported : self . request . sendall ( "\x05\x00" ) VER = self . request . recv ( 1 ) if VER != "\x05" : self . server . module . error ( "receiving unsuported socks version: %s" % VER . encode ( 'hex' ) ) self . _socks_response ( CODE_GENERAL_SRV_FAILURE , terminate = True ) return CMD = self . request . recv ( 1 ) if CMD != "\x01" : # we only support CONNECT for now self . server . module . error ( "receiving unsuported socks CMD: %s" % CMD . encode ( 'hex' ) ) self . _socks_response ( CODE_COMMAND_NOT_SUPPORTED , terminate = True ) return RSV = self . request . recv ( 1 ) DST_ADDR = None DST_PORT = None ATYP = self . request . recv ( 1 ) if ATYP == "\x01" : DST_ADDR = "." . join ( [ str ( ord ( x ) ) for x in self . request . recv ( 4 ) ] ) DST_PORT = struct . unpack ( "!H" , self . request . recv ( 2 ) ) [ 0 ] elif ATYP == "\x03" : DOMAIN_LEN = int ( struct . unpack ( "!B" , self . request . recv ( 1 ) ) [ 0 ] ) DST_ADDR = self . request . recv ( DOMAIN_LEN ) DST_PORT = struct . unpack ( "!H" , self . request . recv ( 2 ) ) [ 0 ] else : #TODO: ipv6 self . server . module . error ( "atyp not supported: %s" % ATYP . encode ( 'hex' ) ) self . _socks_response ( CODE_ADDRESS_TYPE_NOT_SUPPORTED , terminate = True ) return #now we have all we need, we can open the socket proxyfied through rpyc :) self . server . module . info ( "connecting to %s:%s ..." % ( DST_ADDR , DST_PORT ) ) rsocket_mod = self . server . rpyc_client . conn . modules . socket rsocket = rsocket_mod . socket ( rsocket_mod . AF_INET , rsocket_mod . SOCK_STREAM ) rsocket . settimeout ( 5 ) try : rsocket . connect ( ( DST_ADDR , DST_PORT ) ) except Exception as e : self . server . module . error ( "error %s connecting to %s:%s ..." % ( str ( e ) , DST_ADDR , DST_PORT ) ) if e [ 0 ] == 10060 : self . _socks_response ( CODE_HOST_UNREACHABLE , terminate = True ) else : self . _socks_response ( CODE_NET_NOT_REACHABLE , terminate = True ) return self . _socks_response ( CODE_SUCCEEDED ) self . server . module . success ( "connection to %s:%s succeed !" % ( DST_ADDR , DST_PORT ) ) #self.request.settimeout(30) #rsocket.settimeout(30) sp1 = SocketPiper ( self . request , rsocket ) sp2 = SocketPiper ( rsocket , self . request ) sp1 . start ( ) sp2 . start ( ) sp1 . join ( ) sp2 . join ( ) self . server . module . info ( "conn to %s:%s closed" % ( DST_ADDR , DST_PORT ) )