def openShell(self, transport): """ Write 60 lines of data to the transport, then exit. """ proto = protocol.Protocol() proto.makeConnection(transport) transport.makeConnection(wrapProtocol(proto)) # Send enough bytes to the connection so that a rekey is triggered in # the client. def write(counter): i = counter() if i == 60: call.stop() transport.session.conn.sendRequest( transport.session, 'exit-status', '\x00\x00\x00\x00') transport.loseConnection() else: transport.write("line #%02d\n" % (i,)) # The timing for this loop is an educated guess (and/or the result of # experimentation) to exercise the case where a packet is generated # mid-rekey. Since the other side of the connection is (so far) the # OpenSSH command line client, there's no easy way to determine when the # rekey has been initiated. If there were, then generating a packet # immediately at that time would be a better way to test the # functionality being tested here. call = LoopingCall(write, count().next) call.start(0.01)
def request_shell(self, data): protocol = EchoProtocol() transport = SSHSessionProcessProtocol(self) protocol.makeConnection(transport) transport.makeConnection(wrapProtocol(protocol)) self.client = transport return True
def openShell(self, processprotocol): """ """ self.protocol = insults.LoggingServerProtocol( protocol.HoneyPotInteractiveProtocol, self) self.protocol.makeConnection(processprotocol) processprotocol.makeConnection(session.wrapProtocol(self.protocol))
def execCommand(self, proto, cmd): serverProtocol = protocol.LoggingServerProtocol( protocol.HoneyPotExecProtocol, self, self.env, cmd) self.protocol = serverProtocol serverProtocol.makeConnection(proto) proto.makeConnection(session.wrapProtocol(serverProtocol)) self.protocol = serverProtocol
def openShell(self, protocol): serverProtocol = insults.ServerProtocol(SSHDemoProtocol, self) serverProtocol.makeConnection(protocol) protocol.makeConnection(session.wrapProtocol(serverProtocol))
def openShell(self, transport): """ Use the custom protocol """ protocol = EchoProtocol() protocol.makeConnection(transport) transport.makeConnection(session.wrapProtocol(protocol))
def execCommand(self, proto, cmd): """ """ self.protocol = protocol.LoggingServerProtocol( protocol.HoneyPotExecProtocol, self, cmd) self.protocol.makeConnection(proto) proto.makeConnection(session.wrapProtocol(self.protocol))
def execCommand(self, processprotocol, cmd): """ """ self.protocol = insults.LoggingServerProtocol( protocol.HoneyPotExecProtocol, self, cmd) self.protocol.makeConnection(processprotocol) processprotocol.makeConnection(session.wrapProtocol(self.protocol))
def openShell(self, transport): """ Write 60 lines of data to the transport, then exit. """ proto = protocol.Protocol() proto.makeConnection(transport) transport.makeConnection(wrapProtocol(proto)) # Send enough bytes to the connection so that a rekey is triggered in # the client. def write(counter): i = counter() if i == 60: call.stop() transport.session.conn.sendRequest(transport.session, 'exit-status', '\x00\x00\x00\x00') transport.loseConnection() else: transport.write("line #%02d\n" % (i, )) # The timing for this loop is an educated guess (and/or the result of # experimentation) to exercise the case where a packet is generated # mid-rekey. Since the other side of the connection is (so far) the # OpenSSH command line client, there's no easy way to determine when the # rekey has been initiated. If there were, then generating a packet # immediately at that time would be a better way to test the # functionality being tested here. call = LoopingCall(write, count().next) call.start(0.01)
def request_shell(self, data): protocol = Protocol() transport = SSHSessionProcessProtocol(self) protocol.makeConnection(transport) transport.makeConnection(wrapProtocol(protocol)) self.client = transport return True
def openShell(self, proto): """ """ self.protocol = protocol.LoggingServerProtocol( protocol.HoneyPotInteractiveProtocol, self) self.protocol.makeConnection(proto) proto.makeConnection(session.wrapProtocol(self.protocol))
def openShell(self, trans): """ Called when a shell is opened by a user logging in via SSH or similar. """ # Obtain a protocol instance. This is our custom Network.SSHServerProtocol. # The protocol controls the way that data is sent and received down the connection. # In our case, it presents a TTY-based user interface to the user, while all we care # about is sending lines to the user and receiving lines from them. from Network import SSHServerProtocol # Get the protocol instance. The protocol is also our transport. # Note that the Twisted networking model is a stack of protocols, # where lower level protocols transport higher level ones. self.transport = proto = SSHServerProtocol(self, *self.savedSize) # Connect the protocol and the transport together (I don't really understand why # it needs to be connected both ways like this, or what the wrapper does) proto.makeConnection(trans) trans.makeConnection(session.wrapProtocol(proto)) #self.send_message("Hi there!") # Obtain the Player object from the database player_id = self.world.db.get_player_id(self.username, self._charname) log.debug("Username: {0}, character: {2}, id: {1}".format(self.username, player_id, self._charname)) self.player = self.world.get_thing(player_id) # Finish login (what does this call do?) self.complete_login()
def execCommand(self, proto, cmd): p = session_wrapper_protocol(self, False) p.makeConnection(proto) proto.makeConnection(session.wrapProtocol(p)) p.dataReceived(cmd) if (not cmd.endswith("\n")): p.dataReceived("\n") p.stransport.terminate()
def openShell(self, protocol): serverProtocol = insults.ServerProtocol(SSHProtocol, self, self.prompt, self.commands) serverProtocol.makeConnection(protocol) protocol.makeConnection(session.wrapProtocol(serverProtocol))
def openShell(self, proto): serverProtocol = protocol.LoggingServerProtocol( protocol.HoneyPotInteractiveProtocol, self, self.env) self.protocol = serverProtocol serverProtocol.makeConnection(proto) proto.makeConnection(session.wrapProtocol(serverProtocol)) #self.protocol = serverProtocol self.protocol = proto
def openShell(self, trans): log.msg("Your terminal name is %r. " "Your terminal is %d columns wide and %d rows tall." % (self.terminalName, self.windowSize[0], self.windowSize[1])) log.msg("ExampleSession: open shell!") ep = SSHServerConsoleProtocol(self.terminalName) ep.makeConnection(trans) trans.makeConnection(session.wrapProtocol(ep))
def request_exec(self, data): print 'request_exec', data protocol = SCPProtocol() transport = SSHSessionProcessProtocol(self) protocol.makeConnection(transport) transport.makeConnection(wrapProtocol(protocol)) self.client = transport return True
def openShell(self, protocol): shell_protocol = insults.ServerProtocol( ShellProtocol, self, self.executables, ) shell_protocol.makeConnection(protocol) protocol.makeConnection(session.wrapProtocol(shell_protocol))
def openShell(self, protocol): """ Open a shell and connect it to proto. """ peer_address = protocol.getPeer().address # IAddress (host, port) = (peer_address.host, peer_address.port) log.msg("Open shell from %s:%d." % (host, port)) serverProtocol = ServerProtocol(GatewayTerminalProtocol, self.avatar) serverProtocol.makeConnection(protocol) protocol.makeConnection(wrapProtocol(serverProtocol))
def openShell(self, transport): """ Use our protocol as shell session. """ protocol = EchoProtocol() # Connect the new protocol to the transport and the transport # to the new protocol so they can communicate in both directions. protocol.makeConnection(transport) transport.makeConnection(session.wrapProtocol(protocol))
def openShell(self, protocol): serverProto = ServerProtocol2( makeSSHApplicationProtocol, self.reactor, self.user_id) serverProto.makeConnection(protocol) protocol.makeConnection(wrapProtocol(serverProto)) self.ssh_protocol = serverProto app_protocol = serverProto.terminalProtocol.app_protocol app_protocol.term_size = self.term_size app_protocol.update_display()
def openShell(self, trans): log.msg( "Your terminal name is %r. " "Your terminal is %d columns wide and %d rows tall." % ( self.terminalName, self.windowSize[0], self.windowSize[1])) log.msg("ExampleSession: open shell!") ep = SSHServerConsoleProtocol(self.terminalName) ep.makeConnection(trans) trans.makeConnection(session.wrapProtocol(ep))
def openShell(self, protocol): #serverProtocol = insults.ServerProtocol(remoteCLI, self) control = self.service.root.getServiceNamed('control').handle_command serverProtocol = insults.ServerProtocol(manhole.ColoredManhole, { 'app': self.service.root, 'stop': reactor.stop, 'c': control }) serverProtocol.makeConnection(protocol) protocol.makeConnection(session.wrapProtocol(serverProtocol))
def openShell(self, protocol): _log.debug("openShell %s", protocol.getHost().address.port) # protocol is an SSHSessionProcessProtocol object # protocol.getHost().address.port # protocol.factory # protocol.transport # TODO if port is global sshport create CLI ts_protocol = TSProtocol(self.avatar) ts_protocol.makeConnection(protocol) protocol.makeConnection(session.wrapProtocol(ts_protocol))
def attach_protocol_to_channel(protocol, channel): # These are from # http://as.ynchrono.us/2011/08/twisted-conch-in-60-seconds-protocols.html transport = SSHSessionProcessProtocol(channel) protocol.makeConnection(transport) transport.makeConnection(wrapProtocol(protocol)) channel.client = transport # And this one's from me :3 channel.dataReceived = protocol.dataReceived
def openShell(self, protocol): serverProto = ServerProtocol2( make_terminal_adapter, self.reactor, self.user_id) serverProto.makeConnection(protocol) protocol.makeConnection(wrapProtocol(serverProto)) self.ssh_protocol = serverProto self.set_term_adapter_term_size() app_protocol = serverProto.terminalProtocol.app_protocol app_protocol.term_size = self.term_size app_protocol.update_display()
def makeProtocol(self): env = HoneyPotEnvironment() user = HoneyPotAvatar("root", env) serverProtocol = insults.ServerProtocol( HoneyPotInteractiveProtocol, user, env) serverProtocol.makeConnection(protocol) protocol.makeConnection(session.wrapProtocol(serverProtocol)) #honeypot = HoneyPotInteractiveProtocol(user, env) return serverProtocol
def openShell(self, transport): """ Use our protocol as shell session. """ protocol = CLIProtocol(self.datastore) # Connect the new protocol to the transport and the transport # to the new protocol so they can communicate in both directions. protocol.makeConnection(transport) transport.makeConnection(session.wrapProtocol(protocol)) protocol.transport.write( b'Welcome to Digital Sky\r\nType "help" for help.\r\n$ ')
def execCommand(self, protocol, cmd): cfg = config() if cfg.has_option('honeypot', 'exec_enabled'): if ( cfg.get('honeypot', 'exec_enabled') != "true" ): print 'exec disabled not executing command: "%s"' % cmd raise os.OSError print 'Executing command: "%s"' % cmd serverProtocol = LoggingServerProtocol(HoneyPotProtocol, self, self.env, cmd) serverProtocol.makeConnection(protocol) protocol.makeConnection(session.wrapProtocol(serverProtocol))
def connectionMade(self): processprotocol = TelnetSessionProcessProtocol(self) # If we are dealing with a proper Telnet client: enable server echo if self.transport.options: self.transport.willChain(SGA) self.transport.willChain(ECHO) self.protocol = insults.LoggingTelnetServerProtocol( cproto.HoneyPotInteractiveTelnetProtocol, self) self.protocol.makeConnection(processprotocol) processprotocol.makeConnection(session.wrapProtocol(self.protocol))
def connectionMade(self): processprotocol = TelnetSessionProcessProtocol(self) # If we are dealing with a proper Telnet client: enable server echo if self.transport.options: self.transport.will(SGA) self.transport.will(ECHO) self.protocol = insults.LoggingTelnetServerProtocol( cproto.HoneyPotInteractiveTelnetProtocol, self) self.protocol.makeConnection(processprotocol) processprotocol.makeConnection(session.wrapProtocol(self.protocol))
def execCommand(self, processprotocol, cmd): cfg = config() # default is enabled if cfg.has_option('honeypot', 'exec_enabled'): if ( cfg.get('honeypot', 'exec_enabled') != "true" ): print 'exec disabled not executing command: "%s"' % cmd raise os.OSError print 'Executing command' self.protocol = cowrie.core.protocol.LoggingServerProtocol( cowrie.core.protocol.HoneyPotExecProtocol, self, cmd) self.protocol.makeConnection(processprotocol) processprotocol.makeConnection(session.wrapProtocol(self.protocol))
def openShell(self, transport): """ Use our protocol as shell session. """ converter = transport.session.conn.transport.factory protocol = SideProtocol() # Connect the new protocol to the transport and the transport # to the new protocol so they can communicate in both directions. protocol.makeConnection(transport) transport.makeConnection(session.wrapProtocol(protocol)) converter.push_a_side(protocol) protocol.other_side = converter.get_b_side(protocol)
def openShell(self, transport): """ wire the protocol to the transport channel :param transport: """ serverProtocol = insults.insults.ServerProtocol( SSHProtocol) # neues ServerProtocol mit SSHProtocol als Terminal serverProtocol.makeConnection(transport) transport.makeConnection(session.wrapProtocol(serverProtocol)) remote = transport.session.avatar.conn.transport.transport.client log.request("SSH", remote[0], remote[1], self.local_ip, self.local_port, "<open shell>", transport.session.avatar.username)
def execCommand(self, processprotocol, cmd): cfg = config() # default is enabled if cfg.has_option('honeypot', 'exec_enabled'): if (cfg.get('honeypot', 'exec_enabled') != "true"): print 'exec disabled not executing command: "%s"' % cmd raise os.OSError print 'Executing command' self.protocol = cowrie.core.protocol.LoggingServerProtocol( cowrie.core.protocol.HoneyPotExecProtocol, self, cmd) self.protocol.makeConnection(processprotocol) processprotocol.makeConnection(session.wrapProtocol(self.protocol))
def openShell(self, transport): """ wire the protocol to the transport channel :param transport: """ serverProtocol = insults.insults.ServerProtocol( SSHProtocol) # neues ServerProtocol mit SSHProtocol als Terminal serverProtocol.makeConnection(transport) transport.makeConnection(session.wrapProtocol(serverProtocol)) ip = transport.session.avatar.conn.transport.transport.client[0] port = transport.session.avatar.conn.transport.transport.server._realPortNumber log.request("SSH", ip, port, "Request shell", transport.session.avatar.username.decode())
def openShell(self, transport): protocol = SSHProtocol() protocol.makeConnection(transport) transport.makeConnection(session.wrapProtocol(protocol)) command_processor = self.avatar.cli( self.avatar.model, protocol, transport, (), template_root=self.avatar.template_root, daemon=True) command_processor.skipLogin = True thread = threading.Thread(target=command_processor.loop, args=()) thread.start()
def request_subsystem(self, data): subsystem, junk = common.getNS(data) if subsystem == 'amp': #import pdb; pdb.set_trace() protocol = EchoDateProtocol(self.avatar) transport = SSHSessionProcessProtocol(self) protocol.makeConnection(transport) transport.makeConnection(wrapProtocol(protocol)) self.client = transport return True # subsystem request OK else: log.msg('Unknown subsystem requested: %r' % (subsystem, )) return False # Fail subsystem request.
def openShell(self, trans): """ Called when a shell is opened by a user logging in via SSH or similar. """ # Create an SSHProtocol object and connect it from Network import SSHServerProtocol # self.transport = t = insults.ServerProtocol(SSHProtocol, self, *self.savedSize) self.transport = t = SSHServerProtocol(self, *self.savedSize) t.makeConnection(trans) trans.makeConnection(session.wrapProtocol(t)) # self.send_message("Hi there!") self.player = self.world.get_thing(self.world.db.get_player_id(self.username)) self.complete_login()
def request_subsystem(self, data): subsystem, junk = common.getNS(data) if subsystem == 'amp': #import pdb; pdb.set_trace() protocol = EchoDateProtocol(self.avatar) transport = SSHSessionProcessProtocol(self) protocol.makeConnection(transport) transport.makeConnection(wrapProtocol(protocol)) self.client = transport return True # subsystem request OK else: log.msg('Unknown subsystem requested: %r' % (subsystem,)) return False # Fail subsystem request.
def execCommand(self, protocol, cmd): cfg = config() if not cfg.has_option('honeypot', 'exec_enabled') or \ cfg.get('honeypot', 'exec_enabled').lower() not in \ ('yes', 'true', 'on'): print 'Exec disabled. Not executing command: "%s"' % cmd raise core.exceptions.NotEnabledException, \ 'exec_enabled not enabled in configuration file!' return print 'exec command: "%s"' % cmd serverProtocol = kippo.core.protocol.LoggingServerProtocol( kippo.core.protocol.HoneyPotExecProtocol, self, self.env, cmd) serverProtocol.makeConnection(protocol) protocol.makeConnection(session.wrapProtocol(serverProtocol))
def execCommand(self, proto, cmd): cfg = config() if not cfg.has_option('honeypot', 'exec_enabled') or \ cfg.get('honeypot', 'exec_enabled').lower() not in \ ('yes', 'true', 'on'): log.msg( 'Exec disabled. Not executing command: "%s"' % cmd ) raise exceptions.NotEnabledException( 'exec_enabled not enabled in configuration file!') return serverProtocol = protocol.LoggingServerProtocol( protocol.HoneyPotExecProtocol, self, self.env, cmd) self.protocol = serverProtocol serverProtocol.makeConnection(proto) proto.makeConnection(session.wrapProtocol(serverProtocol)) self.protocol = serverProtocol
def execCommand(self, proto, cmd): cfg = config() if not cfg.has_option('honeypot', 'exec_enabled') or \ cfg.get('honeypot', 'exec_enabled').lower() not in \ ('yes', 'true', 'on'): log.msg('Exec disabled. Not executing command: "%s"' % cmd) raise exceptions.NotEnabledException( 'exec_enabled not enabled in configuration file!') return serverProtocol = protocol.LoggingServerProtocol( protocol.HoneyPotExecProtocol, self, self.env, cmd) self.protocol = serverProtocol serverProtocol.makeConnection(proto) proto.makeConnection(session.wrapProtocol(serverProtocol)) self.protocol = serverProtocol
def openShell(self, trans): """ called back on a successful connection """ # Instantiate the protocol. self.ep = self.WRAPPROTOCOL() self.avatar.con = self.ep self.ep.user = self.avatar self.ep.cb = self.cb # the peer information self.ep.peer = self.avatar.conn.transport.transport.getPeer() # . method from the actual transport. #self.ep.loseRealConnection = self.avatar.conn.transport.transport.loseConnection # Twisted black magic self.ep.makeConnection(trans) trans.makeConnection(session.wrapProtocol(self.ep))
def test_wrapProtocol(self): """ L{wrapProtocol}, when passed a L{Protocol} should return something that has write(), writeSequence(), loseConnection() methods which call the Protocol's dataReceived() and connectionLost() methods, respectively. """ protocol = MockProtocol() protocol.transport = StubTransport() protocol.connectionMade() wrapped = session.wrapProtocol(protocol) wrapped.dataReceived(b"dataReceived") self.assertEqual(protocol.transport.buf, b"dataReceived") wrapped.write(b"data") wrapped.writeSequence([b"1", b"2"]) wrapped.loseConnection() self.assertEqual(protocol.data, b"data12") protocol.reason.trap(error.ConnectionDone)
def connectionMade(self): processprotocol = TelnetSessionProcessProtocol(self) # If we are dealing with a proper Telnet client: enable server echo if self.transport.options: self.transport.willChain(SGA) self.transport.willChain(ECHO) self.protocol = insults.LoggingTelnetServerProtocol( cproto.HoneyPotInteractiveTelnetProtocol, self) # somewhere in Twisted this exception gets lost. Log explicitly here try: self.protocol.makeConnection(processprotocol) processprotocol.makeConnection(session.wrapProtocol(self.protocol)) except Exception: log.msg(traceback.format_exc())
def connectionMade(self): processprotocol = TelnetSessionProcessProtocol(self) # If we are dealing with a proper Telnet client: enable server echo if self.transport.options: self.transport.willChain(SGA) self.transport.willChain(ECHO) self.protocol = insults.LoggingTelnetServerProtocol( cproto.HoneyPotInteractiveTelnetProtocol, self) # somewhere in Twisted this exception gets lost. Log explicitly here try: self.protocol.makeConnection(processprotocol) processprotocol.makeConnection(session.wrapProtocol(self.protocol)) except Exception as e: log.msg(traceback.format_exc())
def test_wrapProtocol(self): """ L{wrapProtocol}, when passed a L{Protocol} should return something that has write(), writeSequence(), loseConnection() methods which call the Protocol's dataReceived() and connectionLost() methods, respectively. """ protocol = MockProtocol() protocol.transport = StubTransport() protocol.connectionMade() wrapped = session.wrapProtocol(protocol) wrapped.dataReceived('dataReceived') self.assertEquals(protocol.transport.buf, 'dataReceived') wrapped.write('data') wrapped.writeSequence(['1', '2']) wrapped.loseConnection() self.assertEquals(protocol.data, 'data12') protocol.reason.trap(error.ConnectionDone)
def execCommand(self, proto, cmd): """ Support command execution sessions. Arguments: proto: is an instance of SSHSessionProcessProtocol """ self.avatar.addr = proto.getPeer().address self.remote_ip = self.avatar.addr.host self.protocol = self.protocol_class(self.avatar) # Connect the new protocol to the transport and the transport # to the new protocol so they can communicate in both directions. self.protocol.makeConnection(proto) proto.makeConnection(session.wrapProtocol(self.protocol)) # execute command might also requires get_pty #assert not self.is_pty # in case, there is no new line at the file end end = '' if cmd[-1] in ('\r', '\n') else '\n' self.protocol.rawDataReceived(cmd + end + 'EOF\n')
def openShell(self, proto): """ Use our protocol as shell session. Arguments: proto: is a twisted.conch.ssh.session.SSHSessionProcessProtocol object * SSHAutoTester in autotest.py goes this way. * self.protocol is a objshshell.ObjshProtocol object """ if self.is_pty: log.msg('shell opened by pty') else: log.msg('shell not opened by pty') self.avatar.addr = proto.getPeer().address self.remote_ip = self.avatar.addr.host self.protocol = self.protocol_class(self.avatar) # Connect the new protocol to the transport and the transport # to the new protocol so they can communicate in both directions. self.protocol.makeConnection(proto) proto.makeConnection(session.wrapProtocol(self.protocol))
def openShell(self, protocol): serverProtocol = insults.ServerProtocol(SSHServiceProtocol, self.avatar) serverProtocol.makeConnection(protocol) protocol.makeConnection(session.wrapProtocol(serverProtocol))
def openShell(self, transport): protocol = EchoProtocol() protocol.makeConnection(transport) transport.makeConnection(session.wrapProtocol(protocol))
def openShell(self, processprotocol): log.msg("openshell: %s" % (repr(processprotocol), )) self.protocol = cowrie.core.protocol.LoggingServerProtocol( cowrie.core.protocol.HoneyPotInteractiveProtocol, self) self.protocol.makeConnection(processprotocol) processprotocol.makeConnection(session.wrapProtocol(self.protocol))