|
| 1 | +# ABOUTME: Tests for shell script execution via bash/sh and path-based invocation. |
| 2 | +# ABOUTME: Covers shebang stripping, binary detection, comment stripping, and recursion depth limits. |
| 3 | + |
| 4 | +from __future__ import annotations |
| 5 | + |
| 6 | +import os |
| 7 | +import unittest |
| 8 | + |
| 9 | +from cowrie.shell.protocol import HoneyPotInteractiveProtocol |
| 10 | +from cowrie.test.fake_server import FakeAvatar, FakeServer |
| 11 | +from cowrie.test.fake_transport import FakeTransport |
| 12 | + |
| 13 | +os.environ["COWRIE_HONEYPOT_DATA_PATH"] = "data" |
| 14 | +os.environ["COWRIE_HONEYPOT_DOWNLOAD_PATH"] = "/tmp" |
| 15 | +os.environ["COWRIE_SHELL_FILESYSTEM"] = "src/cowrie/data/fs.pickle" |
| 16 | + |
| 17 | +PROMPT = b"root@unitTest:~# " |
| 18 | + |
| 19 | + |
| 20 | +class ScriptExecutionTests(unittest.TestCase): |
| 21 | + """Tests for executing shell scripts via bash/sh and ./path.""" |
| 22 | + |
| 23 | + def setUp(self) -> None: |
| 24 | + self.proto = HoneyPotInteractiveProtocol(FakeAvatar(FakeServer())) |
| 25 | + self.tr = FakeTransport("", "31337") |
| 26 | + self.proto.makeConnection(self.tr) |
| 27 | + self.tr.clear() |
| 28 | + |
| 29 | + def tearDown(self) -> None: |
| 30 | + self.proto.connectionLost() |
| 31 | + |
| 32 | + def test_bash_executes_script_file(self) -> None: |
| 33 | + """bash script.sh reads and executes file contents.""" |
| 34 | + self.proto.lineReceived(b'echo "echo hello" > /tmp/test.sh') |
| 35 | + self.tr.clear() |
| 36 | + self.proto.lineReceived(b"bash /tmp/test.sh") |
| 37 | + self.assertEqual(self.tr.value(), b"hello\n" + PROMPT) |
| 38 | + |
| 39 | + def test_sh_executes_script_file(self) -> None: |
| 40 | + """sh script.sh reads and executes file contents.""" |
| 41 | + self.proto.lineReceived(b'echo "echo world" > /tmp/test_sh.sh') |
| 42 | + self.tr.clear() |
| 43 | + self.proto.lineReceived(b"sh /tmp/test_sh.sh") |
| 44 | + self.assertEqual(self.tr.value(), b"world\n" + PROMPT) |
| 45 | + |
| 46 | + def test_bash_nonexistent_file(self) -> None: |
| 47 | + """bash nonexistent.sh shows error.""" |
| 48 | + self.proto.lineReceived(b"bash /tmp/nonexistent.sh") |
| 49 | + output = self.tr.value() |
| 50 | + self.assertIn(b"No such file or directory", output) |
| 51 | + |
| 52 | + def test_dotslash_with_shebang_executes(self) -> None: |
| 53 | + """./script.sh with #!/bin/sh shebang executes.""" |
| 54 | + self.proto.lineReceived(b'printf "#!/bin/sh\\necho from_script\\n" > run.sh') |
| 55 | + self.tr.clear() |
| 56 | + self.proto.lineReceived(b"./run.sh") |
| 57 | + self.assertEqual(self.tr.value(), b"from_script\n" + PROMPT) |
| 58 | + |
| 59 | + def test_dotslash_without_shebang_executes(self) -> None: |
| 60 | + """./file without shebang executes as shell script (kernel ENOEXEC fallback).""" |
| 61 | + self.proto.lineReceived(b'echo "echo no_shebang" > noshebang.sh') |
| 62 | + self.tr.clear() |
| 63 | + self.proto.lineReceived(b"./noshebang.sh") |
| 64 | + self.assertEqual(self.tr.value(), b"no_shebang\n" + PROMPT) |
| 65 | + |
| 66 | + def test_dotslash_binary_file_fails(self) -> None: |
| 67 | + """./file with null bytes emits 'cannot execute binary file'.""" |
| 68 | + # Use printf to write a null byte into the file |
| 69 | + self.proto.lineReceived(b'printf "\\x00ELF" > binfile') |
| 70 | + self.tr.clear() |
| 71 | + self.proto.lineReceived(b"./binfile") |
| 72 | + output = self.tr.value() |
| 73 | + self.assertIn(b"cannot execute binary file", output) |
| 74 | + |
| 75 | + def test_shebang_line_stripped(self) -> None: |
| 76 | + """Shebang line is not echoed or executed as a command.""" |
| 77 | + self.proto.lineReceived( |
| 78 | + b'printf "#!/bin/bash\\necho shebang_stripped\\n" > /tmp/shebang.sh' |
| 79 | + ) |
| 80 | + self.tr.clear() |
| 81 | + self.proto.lineReceived(b"bash /tmp/shebang.sh") |
| 82 | + self.assertEqual(self.tr.value(), b"shebang_stripped\n" + PROMPT) |
| 83 | + |
| 84 | + def test_comment_lines_stripped(self) -> None: |
| 85 | + """Comment-only lines are stripped from script execution.""" |
| 86 | + self.proto.lineReceived( |
| 87 | + b'printf "#!/bin/sh\\n# this is a comment\\necho works\\n" > /tmp/comments.sh' |
| 88 | + ) |
| 89 | + self.tr.clear() |
| 90 | + self.proto.lineReceived(b"bash /tmp/comments.sh") |
| 91 | + self.assertEqual(self.tr.value(), b"works\n" + PROMPT) |
| 92 | + |
| 93 | + def test_multiline_script(self) -> None: |
| 94 | + """Script with multiple commands executes all of them.""" |
| 95 | + self.proto.lineReceived( |
| 96 | + b'printf "echo line1\\necho line2\\necho line3\\n" > /tmp/multi.sh' |
| 97 | + ) |
| 98 | + self.tr.clear() |
| 99 | + self.proto.lineReceived(b"bash /tmp/multi.sh") |
| 100 | + self.assertEqual(self.tr.value(), b"line1\nline2\nline3\n" + PROMPT) |
| 101 | + |
| 102 | + def test_absolute_path_with_shebang(self) -> None: |
| 103 | + """Absolute path /tmp/script.sh with shebang executes.""" |
| 104 | + self.proto.lineReceived( |
| 105 | + b'printf "#!/bin/sh\\necho absolute\\n" > /tmp/abs.sh' |
| 106 | + ) |
| 107 | + self.tr.clear() |
| 108 | + self.proto.lineReceived(b"/tmp/abs.sh") |
| 109 | + self.assertEqual(self.tr.value(), b"absolute\n" + PROMPT) |
| 110 | + |
| 111 | + def test_bash_dash_c_still_works(self) -> None: |
| 112 | + """Existing sh -c 'cmd' functionality still works.""" |
| 113 | + self.proto.lineReceived(b"sh -c 'echo still_works'") |
| 114 | + self.assertEqual(self.tr.value(), b"still_works\n" + PROMPT) |
| 115 | + |
| 116 | + def test_bash_piped_input_still_works(self) -> None: |
| 117 | + """Existing piped input functionality still works.""" |
| 118 | + self.proto.lineReceived(b"echo echo piped | bash") |
| 119 | + self.assertEqual(self.tr.value(), b"piped\n" + PROMPT) |
0 commit comments