From f5633927ff56c057679347258931e4aa67a3e0bb Mon Sep 17 00:00:00 2001 From: Michel Oosterhof Date: Sun, 16 Sep 2018 16:46:49 +0400 Subject: [PATCH] Fix #883, add ',' to wordchars (#884) * Fix #883, add ',' to wordchars --- src/cowrie/shell/honeypot.py | 17 +++++++++-------- src/cowrie/shell/shlex.py | 3 --- src/cowrie/test/test_echo.py | 7 +++++++ 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/cowrie/shell/honeypot.py b/src/cowrie/shell/honeypot.py index c76238f1..3de09e60 100644 --- a/src/cowrie/shell/honeypot.py +++ b/src/cowrie/shell/honeypot.py @@ -32,15 +32,21 @@ class HoneyPotShell(object): 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) + self.lexer = shlex.shlex(instream=line, punctuation_chars=True, posix=True) # Add these special characters that are not in the default lexer - self.lexer.wordchars += '@%{}=$:+^' + self.lexer.wordchars += '@%{}=$:+^,' tokens = [] while True: try: tok = self.lexer.get_token() # log.msg("tok: %s" % (repr(tok))) + if tok == self.lexer.eof: + if tokens: + self.cmdpending.append((tokens)) + tokens = [] + break + # Ignore parentheses tok_len = len(tok) tok = tok.strip('(') @@ -48,13 +54,8 @@ class HoneyPotShell(object): if len(tok) != tok_len and tok == '': continue - if tok == self.lexer.eof: - if tokens: - self.cmdpending.append((tokens)) - tokens = [] - break # For now, treat && and || same as ;, just execute without checking return code - elif tok == '&&' or tok == '||': + if tok == '&&' or tok == '||': if tokens: self.cmdpending.append((tokens)) tokens = [] diff --git a/src/cowrie/shell/shlex.py b/src/cowrie/shell/shlex.py index 06b25edf..45053a6b 100644 --- a/src/cowrie/shell/shlex.py +++ b/src/cowrie/shell/shlex.py @@ -43,9 +43,6 @@ class shlex: self.commenters = '#' self.wordchars = ('abcdfeghijklmnopqrstuvwxyz' 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_') - if self.posix: - self.wordchars += ('ßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ' - 'ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞ') self.whitespace = ' \t\r\n' self.whitespace_split = False self.quotes = '\'"' diff --git a/src/cowrie/test/test_echo.py b/src/cowrie/test/test_echo.py index 31fd34b0..2919048c 100644 --- a/src/cowrie/test/test_echo.py +++ b/src/cowrie/test/test_echo.py @@ -115,5 +115,12 @@ class ShellEchoCommandTests(unittest.TestCase): self.proto.lineReceived(b'echo test > test012; grep test test012') self.assertEquals(self.tr.value(), b'test\n' + PROMPT) + def test_echo_command_013(self): + """ + echo "ls""ls" + """ + self.proto.lineReceived(b'echo "ls""ls"') + self.assertEquals(self.tr.value(), b'lsls\n' + PROMPT) + def tearDown(self): self.proto.connectionLost("tearDown From Unit Test")