예제 #1
0
    def test_build_monkey_commandline_explicitly(self):
        test1 = " -p 101010 -t 10.10.101.10 -s 127.127.127.127:5000 -d 0 -l C:\\windows\\abc -vp 80"
        result1 = build_monkey_commandline_explicitly(101010, "10.10.101.10",
                                                      "127.127.127.127:5000",
                                                      0, "C:\\windows\\abc",
                                                      80)

        test2 = " -p parent -s 127.127.127.127:5000 -d 0 -vp 80"
        result2 = build_monkey_commandline_explicitly(
            parent="parent",
            server="127.127.127.127:5000",
            depth="0",
            vulnerable_port="80")

        self.assertEqual(test1, result1)
        self.assertEqual(test2, result2)
예제 #2
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:
            LOG.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 = MONKEY_CMDLINE_WINDOWS % {
            'monkey_path': WormConfiguration.dropper_target_path_win_64} + monkey_options

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

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

        time.sleep(WindowsUpgrader.__UPGRADE_WAIT_TIME__)
        if monkey_process.poll() is not None:
            LOG.error("Seems like monkey died too soon")
예제 #3
0
파일: dropper.py 프로젝트: rakhif/monkey
    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_cmdline = MONKEY_CMDLINE_WINDOWS % {
                'monkey_path': self._config['destination_path']
            } + monkey_options
        else:
            dest_path = self._config['destination_path']
            # In linux we have a more complex commandline. There's a general outer one, and the inner one which actually
            # runs the monkey
            inner_monkey_cmdline = MONKEY_CMDLINE_LINUX % {
                'monkey_filename': dest_path.split("/")[-1]
            } + monkey_options
            monkey_cmdline = GENERAL_CMDLINE_LINUX % {
                'monkey_directory': dest_path[0:dest_path.rfind("/")],
                'monkey_commandline': inner_monkey_cmdline
            }

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

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

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