Beispiel #1
0
    def lineReceived(self, line):
        """
        """
        log.msg('CMD: {}'.format(line))
        self.lexer = shlex.shlex(instream=line, punctuation_chars=True)
        tokens = []
        while True:
            try:
                tok = self.lexer.get_token()
                # log.msg( "tok: %s" % (repr(tok)) )
                if tok == self.lexer.eof:
                    if len(tokens):
                        self.cmdpending.append((tokens))
                        tokens = []
                    break
                # For now, execute all after &&
                elif tok == ';' or tok == '&&' or tok == '||':
                    if len(tokens):
                        self.cmdpending.append((tokens))
                        tokens = []
                        continue
                    else:
                        self.protocol.terminal.write(
                            '-bash: syntax error near unexpected token `{}\'\n'
                            .format(tok))
                        break
                elif tok == '$?':
                    tok = "0"
                elif tok[0] == '$':
                    envRex = re.compile(r'^\$([_a-zA-Z0-9]+)$')
                    envSearch = envRex.search(tok)
                    if envSearch != None:
                        envMatch = envSearch.group(1)
                        if envMatch in list(self.environ.keys()):
                            tok = self.environ[envMatch]
                        else:
                            continue
                    envRex = re.compile(r'^\${([_a-zA-Z0-9]+)}$')
                    envSearch = envRex.search(tok)
                    if envSearch != None:
                        envMatch = envSearch.group(1)
                        if envMatch in list(self.environ.keys()):
                            tok = self.environ[envMatch]
                        else:
                            continue
                tokens.append(tok)
            except Exception as e:
                self.protocol.terminal.write(
                    'bash: syntax error: unexpected end of file\n')
                # Could run runCommand here, but i'll just clear the list instead
                log.msg("exception: {}".format(e))
                self.cmdpending = []
                self.showPrompt()
                return

        if len(self.cmdpending):
            self.runCommand()
        else:
            self.showPrompt()
Beispiel #2
0
    def lineReceived(self, line):
        """
        """
        log.msg('CMD: %s' % (line,))
        self.lexer = shlex.shlex(punctuation_chars=True);
        self.lexer.push_source(line)
        tokens = []
        while True:
            try:
                tok = self.lexer.get_token()
                #log.msg( "tok: %s" % (repr(tok)) )
                # for now, execute all after &&
                if tok == self.lexer.eof:
                    if len(tokens):
                        self.cmdpending.append((tokens))
                        tokens = []
                    break
                if tok == ';' or tok == '&&' or tok == '||':
                    self.cmdpending.append((tokens))
                    tokens = []
                if tok == ';':
                    continue
                if tok == '&&':
                    continue
                if tok == '||':
                    continue
                if tok == '$?':
                    tok = "0"
                elif tok[0] == '$':
                    env_rex = re.compile('^\$([_a-zA-Z0-9]+)$')
                    env_search = env_rex.search(tok)
                    if env_search != None:
                        env_match = env_search.group(1)
                        if env_match in list(self.environ.keys()):
                            tok = self.environ[env_match]
                        else:
                            continue
                    env_rex = re.compile('^\${([_a-zA-Z0-9]+)}$')
                    env_search = env_rex.search(tok)
                    if env_search != None:
                        env_match = env_search.group(1)
                        if env_match in list(self.environ.keys()):
                            tok = self.environ[env_match]
                        else:
                            continue
                tokens.append(tok)
            except Exception as e:
                self.protocol.terminal.write(
                    'bash: syntax error: unexpected end of file\n')
                # Could run runCommand here, but i'll just clear the list instead
                log.msg( "exception: {}".format(e) )
                self.cmdpending = []
                self.showPrompt()
                return

        if len(self.cmdpending):
            self.runCommand()
        else:
            self.showPrompt()
Beispiel #3
0
    def lineReceived(self, line):
        """
        """
        log.msg(eventid='cowrie.command.input', input=line, format='CMD: %(input)s')
        self.lexer = shlex.shlex(instream=line, punctuation_chars=True)
        tokens = []
        while True:
            try:
                tok = self.lexer.get_token()
                # log.msg( "tok: %s" % (repr(tok)) )
                if tok == self.lexer.eof:
                    if len(tokens):
                        self.cmdpending.append((tokens))
                        tokens = []
                    break
                # For now, execute all after &&
                elif tok == ';' or tok == '&&' or tok == '||':
                    if len(tokens):
                        self.cmdpending.append((tokens))
                        tokens = []
                        continue
                    else:
                        self.protocol.terminal.write(
                            '-bash: syntax error near unexpected token `{}\'\n'.format(tok))
                        break
                elif tok == '$?':
                    tok = "0"
                elif tok[0] == '$':
                    envRex = re.compile(r'^\$([_a-zA-Z0-9]+)$')
                    envSearch = envRex.search(tok)
                    if envSearch != None:
                        envMatch = envSearch.group(1)
                        if envMatch in list(self.environ.keys()):
                            tok = self.environ[envMatch]
                        else:
                            continue
                    envRex = re.compile(r'^\${([_a-zA-Z0-9]+)}$')
                    envSearch = envRex.search(tok)
                    if envSearch != None:
                        envMatch = envSearch.group(1)
                        if envMatch in list(self.environ.keys()):
                            tok = self.environ[envMatch]
                        else:
                            continue
                tokens.append(tok)
            except Exception as e:
                self.protocol.terminal.write(
                    'bash: syntax error: unexpected end of file\n')
                # Could run runCommand here, but i'll just clear the list instead
                log.msg( "exception: {}".format(e) )
                self.cmdpending = []
                self.showPrompt()
                return

        if len(self.cmdpending):
            self.runCommand()
        else:
            self.showPrompt()
Beispiel #4
0
    def lineReceived(self, line):
        """
        """
        log.msg('CMD: %s' % (line,))
        self.lexer = shlex.shlex(punctuation_chars=True);
        self.lexer.push_source(line)
        tokens = []
        while True:
            try:
                tok = self.lexer.get_token()
                #log.msg( "tok: %s" % (repr(tok)) )
                # for now, execute all after &&
                if tok == self.lexer.eof:
                    if len(tokens):
                        self.cmdpending.append((tokens))
                        tokens = []
                    break
                if tok == ';' or tok == '&&' or tok == '||':
                    self.cmdpending.append((tokens))
                    tokens = []
                if tok == ';':
                    continue
                if tok == '&&':
                    continue
                if tok == '||':
                    continue
                tokens.append(tok)
            except Exception as e:
                self.protocol.terminal.write(
                    'bash: syntax error: unexpected end of file\n')
                # Could run runCommand here, but i'll just clear the list instead
                self.cmdpending = []
                self.showPrompt()
                return

        if len(self.cmdpending):
            self.runCommand()
        else:
            self.showPrompt()
Beispiel #5
0
    def lineReceived(self, line):
        """
        """
        log.msg('CMD: %s' % (line, ))
        self.lexer = shlex.shlex(punctuation_chars=True)
        self.lexer.push_source(line)
        tokens = []
        while True:
            try:
                tok = self.lexer.get_token()
                #log.msg( "tok: %s" % (repr(tok)) )
                # for now, execute all after &&
                if tok == self.lexer.eof:
                    if len(tokens):
                        self.cmdpending.append((tokens))
                        tokens = []
                    break
                if tok == ';' or tok == '&&' or tok == '||':
                    self.cmdpending.append((tokens))
                    tokens = []
                if tok == ';':
                    continue
                if tok == '&&':
                    continue
                if tok == '||':
                    continue
                tokens.append(tok)
            except Exception as e:
                self.protocol.terminal.write(
                    'bash: syntax error: unexpected end of file\n')
                # Could run runCommand here, but i'll just clear the list instead
                self.cmdpending = []
                self.showPrompt()
                return

        if len(self.cmdpending):
            self.runCommand()
        else:
            self.showPrompt()