diff --git a/src/cowrie/commands/cat.py b/src/cowrie/commands/cat.py index 6cace5b2..a4f7e6fa 100644 --- a/src/cowrie/commands/cat.py +++ b/src/cowrie/commands/cat.py @@ -62,26 +62,33 @@ class command_cat(HoneyPotCommand): raise FileNotFound except FileNotFound: self.errorWrite('cat: {}: No such file or directory\n'.format(arg)) - + self.exit() + elif self.input_data is not None: + self.output(self.input_data) self.exit() def output(self, input): """ This is the cat output, with optional line numbering """ - if 'decode' in dir(input): - input = input.decode('UTF-8') - if not isinstance(input, str): - pass + if input is None: + return - lines = input.split('\n') - if lines[-1] == "": + if isinstance(input, str): + input = input.encode('utf8') + elif isinstance(input, bytes): + pass + else: + log.msg("unusual cat input {}".format(repr(input))) + + lines = input.split(b'\n') + if lines[-1] == b'': lines.pop() for line in lines: if self.number: - self.write("{:>6} ".format(self.linenumber)) + self.write('{:>6} '.format(self.linenumber)) self.linenumber = self.linenumber + 1 - self.write(line + '\n') + self.writeBytes(line + b'\n') def lineReceived(self, line): """ diff --git a/src/cowrie/commands/fs.py b/src/cowrie/commands/fs.py index c67dedda..bf556ef0 100644 --- a/src/cowrie/commands/fs.py +++ b/src/cowrie/commands/fs.py @@ -34,12 +34,11 @@ class command_grep(HoneyPotCommand): self.errorWrite("grep: {}: No such file or directory\n".format(filename)) def grep_application(self, contents, match): - match = os.path.basename(match) - match = match.replace('\"', '') + match = os.path.basename(match).replace('\"', '').encode('utf8') + matches = re.compile(match) contentsplit = contents.split(b'\n') - matches = re.compile('.*' + match + '.*') for line in contentsplit: - if matches.match(line.decode()): + if matches.search(line): self.writeBytes(line + b'\n') def help(self): diff --git a/src/cowrie/test/test_cat.py b/src/cowrie/test/test_cat.py index 2a614cf5..fd9f2829 100644 --- a/src/cowrie/test/test_cat.py +++ b/src/cowrie/test/test_cat.py @@ -47,12 +47,12 @@ class ShellCatCommandTests(unittest.TestCase): def test_cat_command_003(self): """ - test ignore stdin when called without '-' + test without arguments, read stdin only and quit """ - self.proto.lineReceived(b'echo whatever | cat\n') - self.proto.lineReceived(b'test\n') + self.proto.lineReceived(b'echo 1 | cat\n') + self.proto.lineReceived(b'echo 2\n') self.proto.handle_CTRL_D() - self.assertEquals(self.tr.value(), b'test\n' + PROMPT) + self.assertEquals(self.tr.value(), b'1\n' + PROMPT + b'2\n' + PROMPT) def test_cat_command_004(self): """