コード例 #1
0
class ShellCatCommandTests(unittest.TestCase):
    """Test for cowrie/commands/cat.py."""
    def setUp(self) -> None:
        self.proto = HoneyPotInteractiveProtocol(FakeAvatar(FakeServer()))
        self.tr = FakeTransport("", "31337")
        self.proto.makeConnection(self.tr)
        self.tr.clear()

    def tearDown(self) -> None:
        self.proto.connectionLost("tearDown From Unit Test")

    def test_cat_command_001(self) -> None:
        self.proto.lineReceived(b"cat nonExisting\n")
        self.assertEqual(
            self.tr.value(),
            b"cat: nonExisting: No such file or directory\n" + PROMPT)

    def test_cat_command_002(self) -> None:
        self.proto.lineReceived(b"echo test | cat -\n")
        self.assertEqual(self.tr.value(), b"test\n" + PROMPT)

    def test_cat_command_003(self) -> None:
        self.proto.lineReceived(b"echo 1 | cat\n")
        self.proto.lineReceived(b"echo 2\n")
        self.proto.handle_CTRL_D()
        self.assertEqual(self.tr.value(), b"1\n" + PROMPT + b"2\n" + PROMPT)

    def test_cat_command_004(self) -> None:
        self.proto.lineReceived(b"cat\n")
        self.proto.lineReceived(b"test\n")
        self.proto.handle_CTRL_C()
        self.assertEqual(self.tr.value(), b"test\n^C\n" + PROMPT)
コード例 #2
0
ファイル: test_tftp.py プロジェクト: carbonlayer/cowrie
class ShellTftpCommandTests(unittest.TestCase):
    """Tests for cowrie/commands/tftp.py."""

    def setUp(self) -> None:
        self.proto = HoneyPotInteractiveProtocol(FakeAvatar(FakeServer()))
        self.tr = FakeTransport("", "31337")
        self.proto.makeConnection(self.tr)
        self.tr.clear()

    def tearDown(self) -> None:
        self.proto.connectionLost("tearDown From Unit Test")

    def test_echo_command_001(self) -> None:
        self.proto.lineReceived(b"tftp\n")
        self.assertEqual(
            self.tr.value(), b"usage: tftp [-h] [-c C C] [-l L] [-g G] [-p P] [-r R] [hostname]\n" + PROMPT
        )
コード例 #3
0
class ShellBaseCommandsTests(unittest.TestCase):  # TODO: ps, history
    """Tests for basic commands from cowrie/commands/base.py."""
    def setUp(self) -> None:
        self.proto = HoneyPotInteractiveProtocol(FakeAvatar(FakeServer()))
        self.tr = FakeTransport("", "31337")
        self.proto.makeConnection(self.tr)
        self.tr.clear()

    def tearDown(self) -> None:
        self.proto.connectionLost("tearDown From Unit Test")

    def test_whoami_command(self) -> None:
        self.proto.lineReceived(b"whoami\n")
        self.assertEqual(self.tr.value(), b"root\n" + PROMPT)

    def test_users_command(self) -> None:
        self.proto.lineReceived(b"users \n")
        self.assertEqual(self.tr.value(), b"root\n" + PROMPT)

    def test_exit_command(self) -> None:
        self.proto.lineReceived(b"exit\n")
        self.assertEqual(self.tr.value(), b"")

    def test_logout_command(self) -> None:
        self.proto.lineReceived(b"logout\n")
        self.assertEqual(self.tr.value(), b"")

    def test_clear_command(self) -> None:
        self.proto.lineReceived(b"clear\n")
        self.assertEqual(self.tr.value(), PROMPT)

    def test_hostname_command(self) -> None:
        self.proto.lineReceived(b"hostname unitChanged\n")
        self.assertEqual(self.tr.value(), b"root@unitChanged:~# ")

    def test_reset_command(self) -> None:
        self.proto.lineReceived(b"reset\n")
        self.assertEqual(self.tr.value(), PROMPT)

    def test_id_command(self) -> None:
        self.proto.lineReceived(b"id\n")
        self.assertEqual(self.tr.value(),
                         b"uid=0(root) gid=0(root) groups=0(root)\n" + PROMPT)

    def test_passwd_command(self) -> None:
        self.proto.lineReceived(b"passwd\n")
        self.proto.lineReceived(b"changeme\n")
        self.proto.lineReceived(b"changeme\n")
        self.assertEqual(
            self.tr.value(),
            b"Enter new UNIX password: Retype new UNIX password: passwd: password updated successfully\n"
            + PROMPT)

    def test_shutdown_command(self) -> None:
        self.proto.lineReceived(b"shutdown\n")
        self.assertEqual(self.tr.value(),
                         b"Try `shutdown --help' for more information.\n" +
                         PROMPT)  # TODO: Is it right?..

    def test_poweroff_command(self) -> None:
        self.proto.lineReceived(b"poweroff\n")
        self.assertEqual(self.tr.value(),
                         b"Try `shutdown --help' for more information.\n" +
                         PROMPT)  # TODO: Is it right?..

    def test_date_command(self) -> None:
        self.proto.lineReceived(b"date\n")
        self.assertRegex(
            self.tr.value(),
            rb"[A-Za-z]{3} [A-Za-z]{3} \d{2} \d{2}:\d{2}:\d{2} UTC \d{4}\n" +
            PROMPT)

    def test_bash_command(self) -> None:
        self.proto.lineReceived(b"bash\n")
        self.assertEqual(self.tr.value(), PROMPT)

    def test_sh_command(self) -> None:
        self.proto.lineReceived(b"sh -c id\n")
        self.assertEqual(self.tr.value(),
                         b"uid=0(root) gid=0(root) groups=0(root)\n" + PROMPT)

    def test_php_help_command(self) -> None:
        self.proto.lineReceived(b"php -h\n")
        self.assertEqual(self.tr.value(), Command_php.HELP.encode() + PROMPT)

    def test_php_version_command(self) -> None:
        self.proto.lineReceived(b"php -v\n")
        self.assertEqual(self.tr.value(),
                         Command_php.VERSION.encode() + PROMPT)

    def test_chattr_command(self) -> None:
        self.proto.lineReceived(b"chattr\n")
        self.assertEqual(self.tr.value(), PROMPT)

    def test_umask_command(self) -> None:
        self.proto.lineReceived(b"umask\n")
        self.assertEqual(self.tr.value(), PROMPT)

    def test_set_command(self) -> None:
        self.proto.lineReceived(b"set\n")
        self.assertEqual(
            self.tr.value(),
            b"COLUMNS=80\nHOME=/root\nLINES=25\nLOGNAME=root\nPATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin\nTMOUT=1800\nUSER=root\n"
            + PROMPT)

    def test_unset_command(self) -> None:
        self.proto.lineReceived(b"unset\n")
        self.assertEqual(self.tr.value(), PROMPT)

    def test_export_command(self) -> None:
        self.proto.lineReceived(b"export\n")
        self.assertEqual(self.tr.value(), PROMPT)

    def test_alias_command(self) -> None:
        self.proto.lineReceived(b"alias\n")
        self.assertEqual(self.tr.value(), PROMPT)

    def test_jobs_command(self) -> None:
        self.proto.lineReceived(b"jobs\n")
        self.assertEqual(self.tr.value(), PROMPT)

    def test_kill_command(self) -> None:
        self.proto.lineReceived(b"/bin/kill\n")
        self.assertEqual(self.tr.value(), PROMPT)

    def test_pkill_command(self) -> None:
        self.proto.lineReceived(b"/bin/pkill\n")
        self.assertEqual(self.tr.value(), PROMPT)

    def test_killall_command(self) -> None:
        self.proto.lineReceived(b"/bin/killall\n")
        self.assertEqual(self.tr.value(), PROMPT)

    def test_killall5_command(self) -> None:
        self.proto.lineReceived(b"/bin/killall5\n")
        self.assertEqual(self.tr.value(), PROMPT)

    def test_su_command(self) -> None:
        self.proto.lineReceived(b"su\n")
        self.assertEqual(self.tr.value(), PROMPT)

    def test_chown_command(self) -> None:
        self.proto.lineReceived(b"chown\n")
        self.assertEqual(self.tr.value(), PROMPT)

    def test_chgrp_command(self) -> None:
        self.proto.lineReceived(b"chgrp\n")
        self.assertEqual(self.tr.value(), PROMPT)

    def test_cd_output(self) -> None:
        path = "/usr/bin"

        self.proto.lineReceived(f"cd {path:s}".encode())
        self.assertEqual(self.tr.value(), PROMPT.replace(b"~", path.encode()))
        self.assertEqual(self.proto.cwd, path)

    def test_cd_error_output(self) -> None:
        self.proto.lineReceived(f"cd {NONEXISTEN_FILE:s}".encode())
        self.assertEqual(
            self.tr.value(),
            f"bash: cd: {NONEXISTEN_FILE:s}: No such file or directory\n".
            encode() + PROMPT)