async def connect(self, is_kerberos = False): """ """ #hiding the import, so you'll only need to install multiplexor only when actually using it from multiplexor.operator import MultiplexorOperator con_str = self.target.proxy.target.get_server_url() #creating operator and connecting to multiplexor server self.operator = MultiplexorOperator(con_str, logging_sink = logger) await self.operator.connect() #creating socks5 proxy server_info = await self.operator.start_socks5(self.target.proxy.target.agent_id) await self.operator.terminate() #print(server_info) if is_kerberos is False: #copying the original target, then feeding it to socks5proxy object. it will hold the actual socks5 proxy server address we created before tp = MSLDAPProxy() tp.target = SocksTarget() tp.target.version = SocksServerVersion.SOCKS5 tp.target.server_ip = server_info['listen_ip'] tp.target.server_port = server_info['listen_port'] tp.target.is_bind = False tp.target.proto = SocksProtocol.TCP tp.target.timeout = self.target.timeout tp.target.buffer_size = 4096 tp.target.endpoint_ip = self.target.host tp.target.endpoint_port = self.target.port tp.target.endpoint_timeout = None # TODO: maybe implement endpoint timeout in the msldap target? tp.type = MSLDAPProxyType.SOCKS5 newtarget = copy.deepcopy(self.target) newtarget.proxy = tp return SocksProxyConnection(target = newtarget) else: kt = copy.deepcopy(self.target) kt.proxy = KerberosProxy() kt.proxy.target = SocksTarget() kt.proxy.target.version = SocksServerVersion.SOCKS5 kt.proxy.target.server_ip = server_info['listen_ip'] kt.proxy.target.server_port = server_info['listen_port'] kt.proxy.target.is_bind = False kt.proxy.target.proto = SocksProtocol.TCP kt.proxy.target.timeout = 10 kt.proxy.target.buffer_size = 4096 kt.proxy.target.endpoint_ip = self.target.ip kt.proxy.target.endpoint_port = self.target.port #kt.proxy.creds = copy.deepcopy(self.target.proxy.auth) return kt
def get_target(self): target = SocksTarget() target.version = self.version target.server_ip = self.server_ip target.server_port = self.server_port target.is_bind = self.is_bind target.proto = self.proto target.timeout = self.timeout target.buffer_size = self.buffer_size target.endpoint_ip = self.endpoint_ip target.endpoint_port = self.endpoint_port target.endpoint_timeout = self.endpoint_timeout target.ssl_ctx = self.ssl_ctx return target
async def main(): in_queue = asyncio.Queue() out_queue = asyncio.Queue() await out_queue.put(b'GET / HTTP/1.1\r\nHost: google.com\r\n\r\n') target = SocksTarget() #target.version = SocksServerVersion.SOCKS4 target.version = SocksServerVersion.SOCKS5 target.server_ip = '127.0.0.1' target.server_port = 9050 target.is_bind = False target.proto = SocksProtocol.TCP target.timeout = 10 target.buffer_size = 4096 target.endpoint_ip = ipaddress.ip_address('216.239.32.117') target.endpoint_port = 80 comms = SocksQueueComms(out_queue, in_queue) t = asyncio.create_task(read_q(in_queue)) client = SOCKSClient(comms, target) await client.run() print('DONE!')
async def connect(self, is_kerberos = False): """ """ try: #hiding the import, so you'll only need to install multiplexor when actually using it from multiplexor.operator import MultiplexorOperator #creating connection string #if self.target.proxy.type == SMBProxyType.MULTIPLEXOR: # con_str = 'ws://%s:%s' % (self.target.proxy.ip, self.target.proxy.port) #else: # con_str = 'wss://%s:%s' % (self.target.proxy.ip, self.target.proxy.port) con_str = self.target.proxy.target.get_server_url() #creating operator and connecting to multiplexor server self.operator = MultiplexorOperator(con_str, logging_sink = logger) await self.operator.connect() #creating socks5 proxy server_info = await self.operator.start_socks5(self.target.proxy.target.agent_id) await self.operator.terminate() #print(server_info) if is_kerberos is False: #copying the original target, then feeding it to socks5proxy object. it will hold the actual socks5 proxy server address we created before tp = SMBProxy() tp.target = SocksTarget() tp.target.version = SocksServerVersion.SOCKS5 tp.target.server_ip = server_info['listen_ip'] tp.target.server_port = server_info['listen_port'] tp.target.is_bind = False tp.target.proto = SocksProtocol.TCP tp.target.timeout = 10 tp.target.buffer_size = 4096 tp.target.endpoint_ip = self.target.ip tp.target.endpoint_port = self.target.port tp.target.endpoint_timeout = self.target.timeout tp.type = SMBProxyType.SOCKS5 newtarget = copy.deepcopy(self.target) newtarget.proxy = tp return SocksProxyConnection(target = newtarget), None else: kt = copy.deepcopy(self.target) kt.proxy = KerberosProxy() kt.proxy.target = SocksTarget() kt.proxy.target.version = SocksServerVersion.SOCKS5 kt.proxy.target.server_ip = server_info['listen_ip'] kt.proxy.target.server_port = server_info['listen_port'] kt.proxy.target.is_bind = False kt.proxy.target.proto = SocksProtocol.TCP kt.proxy.target.timeout = 10 kt.proxy.target.buffer_size = 4096 kt.proxy.target.endpoint_ip = self.target.ip kt.proxy.target.endpoint_port = self.target.port kt.proxy.creds = copy.deepcopy(self.target.proxy.auth) return kt, None except Exception as e: return None, e
async def socks5_test_port(self, username = None, password = None, timeout = None, retries = -1): """ Tests if port binding is supported by the socks server """ try: while retries != 0: retries -= 1 target = SocksTarget() target.version = SocksServerVersion.SOCKS5 target.server_ip = self.server_ip target.server_port = self.server_port target.is_bind = False target.timeout = 10 target.buffer_size = 4096 target.ssl_ctx = self.server_sslctx target.endpoint_ip = self.verify_host target.endpoint_port = self.verify_port #random.randint(35000, 50000) target.endpoint_timeout = None target.only_open = True credential = SocksCredential() credential.username = username credential.password = password in_q = asyncio.Queue() out_q = asyncio.Queue() comms = SocksQueueComms(in_q, out_q) channel_open_evt = asyncio.Event() client = SOCKSClient(comms, target, credential, bind_evt = channel_open_evt) #client_task = asyncio.create_task(client.run()) res, err = await client.handle_queue() if err is not None: if isinstance(err, SOCKS5ServerErrorReply): if err.reply in [SOCKS5ReplyType.TTL_EXPIRED, SOCKS5ReplyType.CONN_REFUSED, SOCKS5ReplyType.HOST_UNREACHABLE, SOCKS5ReplyType.CONN_NOT_ALLOWED]: return False, err elif err.reply in [SOCKS5ReplyType.ADDRESS_TYPE_NOT_SUPPORTED]: return None, err continue return True, None return None, Exception('Max retries reached!') except Exception as e: return None, e
async def socks5_local(self, username = None, password = None, timeout = None): """ Tests if port binding is supported by the socks server """ try: target = SocksTarget() target.version = SocksServerVersion.SOCKS5 target.server_ip = self.server_ip target.server_port = self.server_port target.is_bind = False target.timeout = 10 target.buffer_size = 4096 target.ssl_ctx = self.server_sslctx target.endpoint_ip = self.server_ip target.endpoint_port = self.verify_port if self.verify_port is not None else 22 target.endpoint_timeout = None credential = SocksCredential() credential.username = username credential.password = password in_q = asyncio.Queue() out_q = asyncio.Queue() comms = SocksQueueComms(in_q, out_q) client = SOCKSClient(comms, target, credential) res, err = await client.handle_queue() if err is None: return True, None if isinstance(err, SOCKS5ServerErrorReply): if err.reply in [SOCKS5ReplyType.FAILURE, SOCKS5ReplyType.CONN_NOT_ALLOWED]: return False, None return res, err except Exception as e: return None, e
async def socks5_bind(self, username = None, password = None, timeout = None): """ Tests if port binding is supported by the socks server """ try: target = SocksTarget() target.version = SocksServerVersion.SOCKS5 target.server_ip = self.server_ip target.server_port = self.server_port target.is_bind = True target.timeout = 10 target.buffer_size = 4096 target.ssl_ctx = self.server_sslctx target.endpoint_ip = '0.0.0.0' target.endpoint_port = random.randint(35000, 50000) target.endpoint_timeout = None target.only_bind = True credential = SocksCredential() credential.username = username credential.password = password in_q = asyncio.Queue() out_q = asyncio.Queue() comms = SocksQueueComms(in_q, out_q) client = SOCKSClient(comms, target, credential) res, err = await client.handle_queue() if err is None: return True, None if isinstance(err, SOCKS5ServerErrorReply): if err.reply == SOCKS5ReplyType.COMMAND_NOT_SUPPORTED: return False, None return res, err except Exception as e: return None, e