def dataReceived(self, data): """ Input received from user """ self.bytesReceived += len(data) if self.bytesReceivedLimit \ and self.bytesReceived > self.bytesReceivedLimit: log.msg(format='Data upload limit reached') #self.loseConnection() self.eofReceived() return if self.stdinlogOpen: with open(self.stdinlogFile, 'ab') as f: f.write(data) elif self.ttylogEnabled and self.ttylogOpen: ttylog.ttylog_write(self.ttylogFile, len(data), ttylog.TYPE_INPUT, time.time(), data) # TODO: this may need to happen inside the shell rather than here to preserve bytes for redirection #if isinstance(data, bytes): # data = data.decode("utf-8") # prevent crash if something like this was passed: # echo cmd ; exit; \n\n if self.terminalProtocol: insults.ServerProtocol.dataReceived(self, data)
def write(self, data): """ Output sent back to user """ if not isinstance(data, bytes): data = data.encode("utf-8") if self.ttylogEnabled and self.ttylogOpen: ttylog.ttylog_write(self.ttylogFile, len(data), ttylog.TYPE_OUTPUT, time.time(), data) self.ttylogSize += len(data) insults.ServerProtocol.write(self, data)
def write(self, data): """ Called when we send data to the user @type data: L{bytes} @param data: Data sent to the client from the server """ if self.ttylogEnabled: ttylog.ttylog_write(self.ttylogFile, len(data), ttylog.TYPE_OUTPUT, time.time(), data) self.bytesWritten += len(data) channel.SSHChannel.write(self, data)
def dataReceived(self, data): """ Called when we receive data from the user @type data: L{bytes} @param data: Data sent to the server from the client """ self.bytesReceived += len(data) if self.bytesReceivedLimit \ and self.bytesReceived > self.bytesReceivedLimit: log.msg('Data upload limit reached for channel {}'.format(self.id)) self.eofReceived() return if self.ttylogEnabled: ttylog.ttylog_write(self.ttylogFile, len(data), ttylog.TYPE_INPUT, time.time(), data) channel.SSHChannel.dataReceived(self, data)
def runCommand(self): """ """ pp = None def runOrPrompt(): if len(self.cmdpending): self.runCommand() else: self.showPrompt() def parsed_arguments(arguments): parsed_arguments = [] for arg in arguments: parsed_arguments.append(arg) return parsed_arguments def parse_file_arguments(arguments): parsed_arguments = [] for arg in arguments: matches = self.protocol.fs.resolve_path_wc( arg, self.protocol.cwd) if matches: parsed_arguments.extend(matches) else: parsed_arguments.append(arg) return parsed_arguments if not len(self.cmdpending): if self.interactive: self.showPrompt() else: # when commands passed to a shell via PIPE, we spawn a HoneyPotShell in none interactive mode # if there are another shells on stack (cmdstack), let's just exit our new shell # else close connection if len(self.protocol.cmdstack) == 1: ret = failure.Failure(error.ProcessDone(status="")) self.protocol.terminal.transport.processEnded(ret) else: return return cmdAndArgs = self.cmdpending.pop(0) cmd2 = copy.copy(cmdAndArgs) # Probably no reason to be this comprehensive for just PATH... environ = copy.copy(self.environ) cmd_array = [] cmd = {} while len(cmdAndArgs): piece = cmdAndArgs.pop(0) if piece.count('='): key, value = piece.split('=', 1) environ[key] = value continue cmd['command'] = piece cmd['rargs'] = [] break if 'command' not in cmd or not cmd['command']: runOrPrompt() return pipe_indices = [i for i, x in enumerate(cmdAndArgs) if x == "|"] multipleCmdArgs = [] pipe_indices.append(len(cmdAndArgs)) start = 0 # Gather all arguments with pipes for index, pipe_indice in enumerate(pipe_indices): multipleCmdArgs.append(cmdAndArgs[start:pipe_indice]) start = pipe_indice + 1 cmd['rargs'] = parse_file_arguments(multipleCmdArgs.pop(0)) cmd_array.append(cmd) cmd = {} for index, value in enumerate(multipleCmdArgs): cmd['command'] = value.pop(0) cmd['rargs'] = parsed_arguments(value) cmd_array.append(cmd) cmd = {} lastpp = None actionValid = None for index, cmd in reversed(list(enumerate(cmd_array))): cmdclass = self.protocol.getCommand(cmd['command'], environ['PATH'].split(':')) if cmdclass: # log command to database raw_cmd = cmd['command'] rl_state.current_command = raw_cmd proxy.ActionPersister().save(self.actionState, raw_cmd) # generate action actionListener = proxy.ActionListener(self.actionState) generator = proxy.RandomActionGenerator() actionFactory = proxy.ActionFactory( self.protocol.terminal.write, actionListener, generator) validator = proxy.ActionValidator(actionFactory) actionValid = validator.validate(raw_cmd, self.protocol.clientIP) actionName = validator.getActionName() actionColor = validator.getActionColor() log.msg(eventid='irassh.command.action.success', action=actionName, input=cmd['command'] + " " + ' '.join(cmd['rargs']), format='Command found: %(input)s') if index == len(cmd_array) - 1: lastpp = StdOutStdErrEmulationProtocol( self.protocol, cmdclass, cmd['rargs'], None, None) pp = lastpp else: pp = StdOutStdErrEmulationProtocol(self.protocol, cmdclass, cmd['rargs'], None, lastpp) lastpp = pp if self.ttylogEnabled: ttyAction = '\033[1;' + actionColor + 'm' + actionName.upper( ) + '\033[1;m\n' ttylog.ttylog_write(self.protocol.terminal.ttylogFile, len(ttyAction), ttylog.TYPE_OUTPUT, time.time(), ttyAction) else: log.msg(eventid='irassh.command.failed', input=' '.join(cmd2), format='Command not found: %(input)s') self.protocol.terminal.write( 'bash: {}: command not found\n'.format(cmd['command'])) runOrPrompt() if pp and actionValid: self.protocol.call_command(pp, cmdclass, *cmd_array[0]['rargs'])