def stop(self): Session.remove() self.ssh_socket.close() if self.chan: self.chan.close() try: _thread.exit() except SystemExit: pass
class HTTPHandler(socketserver.BaseRequestHandler): def handle(self): connection = tables.Connections(sourceIP=self.client_address[0], sourcePort=self.client_address[1], destIP=self.server.server_address[0], destPort=self.server.server_address[1], proto=tables.TCP) self.session = Session() self.session.add(connection) self.session.commit() self.request.settimeout(30) try: data = self.request.recv(4096).decode("utf-8") except: Session.remove() return http = tables.HTTPCommands(request=data, connection=connection) self.session.add(http) self.session.commit() Session.remove() self.request.sendall(Header.encode('utf-8'))
def __init__(self, source, dest, table=None, limit=0): super().__init__() self.source = source self.dest = dest self.limit = limit self.table = table if self.table: self.session = Session() self.connection = tables.Connections( sourceIP=self.source.getsockname()[0], sourcePort=self.source.getsockname()[1], destIP=self.dest.getsockname()[0], destPort=self.dest.getsockname()[1], proto=tables.TCP) self.session.add(self.connection)
class OneWayThread(threading.Thread): def __init__(self, source, dest, table=None, limit=0): super().__init__() self.source = source self.dest = dest self.limit = limit self.table = table if self.table: self.session = Session() self.connection = tables.Connections( sourceIP=self.source.getsockname()[0], sourcePort=self.source.getsockname()[1], destIP=self.dest.getsockname()[0], destPort=self.dest.getsockname()[1], proto=tables.TCP) self.session.add(self.connection) def run(self): logger.debug('Starting timer') timer = threading.Timer(120, self.shutdown) timer.start() total = b'' while 1: try: data = wrap_socket(lambda: self.source.recv(4096)) except Exception: break if data == b'' or not data: break if self.table or self.limit > 0: total += data try: wrap_socket(lambda: self.dest.sendall(data)) except Exception: break if self.limit > 0 and len(total) >= self.limit: break if self.table: http = self.table(request=str(total), connection=self.connection) self.session.add(http) self.session.commit() logger.debug('Canceling timer') timer.cancel() self.shutdown() def shutdown(self): if self.table: Session.remove() self.source.close() self.dest.close()
def run(self): while True: try: client, addr = self.ssh_socket.accept() except ConnectionAbortedError: break session = Session() connection = tables.Connections( sourceIP=addr[0], sourcePort=addr[1], destIP=self.ssh_socket.getsockname()[0], destPort=self.ssh_socket.getsockname()[1], proto=tables.TCP) session.add(connection) session.commit() transport = paramiko.Transport(client) transport.load_server_moduli() # Experiment with different key sizes at: # http://travistidwell.com/jsencrypt/demo/ host_key = paramiko.RSAKey(filename="RSAKey.cfg") transport.add_server_key(host_key) server = SSHServer(session, connection) transport.start_server(server=server) self.chan = transport.accept() if not self.chan: logger.info('no chan') continue fake_shell(self.chan, session, connection, '# ') self.chan.close() Session.remove()
def handle(self): self.request.settimeout(30) self.session = Session() connection = tables.Connections( sourceIP=self.client_address[0], sourcePort=self.client_address[1], destIP=self.server.socket.getsockname()[0], destPort=self.server.socket.getsockname()[1], proto=tables.TCP) self.session.add(connection) self.session.commit() logger.debug('telnet submitted connection') try: username = self.creds(b'Username: '******'Password: '******'After creds') creds = tables.Credentials(username=username, password=password, \ connection=connection) self.session.add(creds) self.session.commit() logger.debug('telnet submitted creds') self.request.sendall( b'Last login: Mon Nov 20 12:41:05 2017 from 8.8.8.8\n') prompt = b'\n$: ' if username in ('root', 'admin') else b'\n#: ' try: fake_shell(self.request, self.session, connection, prompt, \ telnet=True) except Exception as exc: logger.debug(type(exc)) logger.debug(exc) logger.debug('telnet fake_shell threw exception') Session.remove() self.request.close() logger.debug('telnet handle finished')
def shutdown(self): if self.table: Session.remove() self.source.close() self.dest.close()
class TelnetHandler(socketserver.BaseRequestHandler): def creds(self, prompt): logger.debug('Getting creds') tries = 0 response = '' while response == '': self.request.sendall(prompt) logger.debug('Before creds get_string') response = get_string(self.request, limit=256, telnet=True) tries += 1 if tries > 2: logger.debug('Creds no response') raise IOError('no response') logger.debug('Creds returning %s', response) return response def handle(self): self.request.settimeout(30) self.session = Session() connection = tables.Connections( sourceIP=self.client_address[0], sourcePort=self.client_address[1], destIP=self.server.socket.getsockname()[0], destPort=self.server.socket.getsockname()[1], proto=tables.TCP) self.session.add(connection) self.session.commit() logger.debug('telnet submitted connection') try: username = self.creds(b'Username: '******'Password: '******'After creds') creds = tables.Credentials(username=username, password=password, \ connection=connection) self.session.add(creds) self.session.commit() logger.debug('telnet submitted creds') self.request.sendall( b'Last login: Mon Nov 20 12:41:05 2017 from 8.8.8.8\n') prompt = b'\n$: ' if username in ('root', 'admin') else b'\n#: ' try: fake_shell(self.request, self.session, connection, prompt, \ telnet=True) except Exception as exc: logger.debug(type(exc)) logger.debug(exc) logger.debug('telnet fake_shell threw exception') Session.remove() self.request.close() logger.debug('telnet handle finished')