Beispiel #1
0
    def runSingleTool(self):
        """
		Only run one tool
		"""
        service = self.arguments.args.service
        self.output.printInfo(
            'Selected mode: Run single tool against service {0}'.format(
                service))

        tool = self.settings.toolbox.searchInToolboxForService(
            self.arguments.args.single_tool, service)
        if not tool:
            sys.exit(0)

        output_file = FileUtils.absolute_path(
            FileUtils.concat_path(self.arguments.args.output_dir,
                                  tool.name + '.txt'))
        output_dir = FileUtils.absolute_path(
            FileUtils.concat_path(self.arguments.args.output_dir, tool.name))
        print
        self.output.printTitle1('   ' + tool.name)
        try:
            tool.runTool(self.settings,
                         self.output,
                         self.arguments.args.output_dir,
                         self.arguments.target,
                         self.arguments.specific,
                         auto_yes=self.arguments.args.auto_yes)
        except KeyboardInterrupt, SystemExit:
            print
            self.output.printError('Tool execution aborted')
Beispiel #2
0
    def __init__(self, directory, raw_cmdline, target, toolbox_dir,
                 output_file, output_dir, service_name, specific_args):

        self.directory = FileUtils.absolute_path(
            directory)  # directory where the command should be launched
        self.cmdline = raw_cmdline
        self.target = target
        self.toolbox_dir = FileUtils.absolute_path(toolbox_dir)
        self.output_file = output_file
        self.output_dir = output_dir
        self.service = service_name
        self.specific_args = specific_args
Beispiel #3
0
    def __init__(self, 
                 name,
                 description,
                 target_service,
                 installed,
                 last_update='',
                 install_command=None,
                 update_command=None,
                 check_command=None):
        """
        Construct the Tool object.

        :param str name: Name of the tool ([a-zA-Z0-9-_])
        :param str description: Short description of the tool
        :param str target_service: Name of service targeted by this tool
            (might be "multi" for tools that could be used against various services)
        :param bool installed: Install status
        :param str last_update: Datetime of the last updated ('' if not installed)
        :param Command install_command: Install command (optional)
        :param Command update_command: Update command (optional)
        :param Command check_command: Command to check install (optional)
        """
        self.name            = name
        self.description     = description
        self.target_service  = target_service
        self.installed       = installed if isinstance(installed, bool) else False
        self.last_update     = last_update
        self.install_command = install_command
        self.update_command  = update_command
        self.check_command   = check_command
        self.tool_dir        = FileUtils.absolute_path(
            '{toolbox}/{service}/{name}'.format(
                toolbox  = TOOLBOX_DIR,
                service  = self.target_service,
                name     = self.name)) if self.install_command else ''
Beispiel #4
0
 def __init__(self, name_clean, name_display, description, target_service,
              installed, last_update, install_command, update_command,
              check_command):
     """
     :param name_clean: Name of the tool without special characters (mandatory)
     :param name_display: Name of the tool to display (mandatory)
     :param description: Description of the tool (mandatory)
     :param target_service: Service targeted by the tool, "multi" when supporting various services (mandatory)
     :param installed: Boolean indicating if tool is installed (mandatory)
     :param last_update: Datetime of last update, empty if not installed (mandatory)
     :param install_command: Instance of Command embedding tool installation command-line (optional)
     :param update_command: Instance of Command embedding tool update command-line (optional)
     :param check_command: Instance of Command embedding command-line for checking install (optional)
     """
     self.name_clean = name_clean
     self.name_display = name_display
     self.description = description
     self.target_service = target_service
     self.installed = installed if isinstance(installed, bool) else False
     self.last_update = last_update
     self.install_command = install_command
     self.update_command = update_command
     self.check_command = check_command
     self.tool_dir = FileUtils.absolute_path(
         '{toolbox}/{service}/{name}'.format(
             toolbox=TOOLBOX_DIR,
             service=self.target_service,
             name=self.name_clean)) if self.install_command else ''
Beispiel #5
0
    def __init__(self, cmdtype, cmdline, current_dir, toolbox_dir):
        """
		Initilialize Command object 
		From a raw command line and information used to replace tags correctly

		@Args 	cmdtype:		Command type	
				cmdline:		Raw command-line (may contain tags)
				current_dir:	Directory where the command should be started
				toolbox_dir:	Toolbox directory

		"""
        self.cmdtype = cmdtype
        self.cmdline = cmdline  # Keep the original command line
        self.current_dir = FileUtils.absolute_path(current_dir)
        self.toolbox_dir = FileUtils.absolute_path(toolbox_dir)
        self.parsed_cmdline = ''
Beispiel #6
0
    def remove_for_service(self, service):
        """
        Remove the tools for a given service.

        :param str service: Name of the service targeted by the tools to remove
            (may be "multi")
        """
        if service not in self.services: return
        Output.title1(
            'Remove tools for service: {service}'.format(service=service))

        if not self.tools[service]:
            logger.info('No tool specific to this service in the toolbox')
        else:
            i = 1
            status = True
            for tool in self.tools[service]:
                if i > 1: print()
                Output.title2(
                    '[{svc}][{i:02}/{max:02}] Remove {tool_name}:'.format(
                        svc=service,
                        i=i,
                        max=len(self.tools[service]),
                        tool_name=tool.name))

                status &= tool.remove(self.settings)
                i += 1

            # Remove the service directory if all tools successfully removed
            if status:
                short_svc_path = '{toolbox}/{service}'.format(
                    toolbox=TOOLBOX_DIR, service=service)

                full_svc_path = FileUtils.absolute_path(short_svc_path)

                if FileUtils.remove_directory(full_svc_path):
                    logger.success(
                        'Toolbox service directory "{path}" deleted'.format(
                            path=short_svc_path))
                else:
                    logger.warning('Toolbox service directory "{path}" cannot be ' \
                        'deleted because it still stores some files'.format(
                            path=short_svc_path))
Beispiel #7
0
    def __init__(self, script_path, arguments, settings, output):
        self.script_path = script_path
        self.settings = settings
        self.arguments = arguments
        self.output = output

        # Toolbox management
        print
        if self.arguments.args.show_toolbox:
            service = self.arguments.args.show_toolbox
            self.output.printInfo(
                'Selected mode: Show toolbox content for service {0}'.format(
                    service))
            self.settings.toolbox.printToolboxForService(self.output, service)
            sys.exit(0)

        if self.arguments.args.show_toolbox_brief:
            service = self.arguments.args.show_toolbox_brief
            self.output.printInfo(
                'Selected mode: Show toolbox content (brief) for service {0}'.
                format(service))
            self.settings.toolbox.printToolboxBriefForService(
                self.output, service)
            sys.exit(0)

        if self.arguments.args.install_toolbox:
            service = self.arguments.args.install_toolbox
            self.output.printInfo(
                'Selected mode: Toolbox install for service {0}'.format(
                    service))
            self.settings.toolbox.installToolboxForService(
                self.output, service)
            sys.exit(0)

        if self.arguments.args.install_all:
            self.output.printInfo(
                'Selected mode: Toolbox install for all services')
            self.settings.toolbox.installToolbox(self.output)
            sys.exit(0)

        if self.arguments.args.update_toolbox:
            service = self.arguments.args.update_toolbox
            self.output.printInfo(
                'Selected mode: Toolbox update for service {0}'.format(
                    service))
            self.settings.toolbox.updateToolboxForService(self.output, service)
            sys.exit(0)

        if self.arguments.args.update_all:
            self.output.printInfo(
                'Selected mode: Toolbox update for all services')
            self.settings.toolbox.updateToolbox(self.output)
            sys.exit(0)

        if self.arguments.args.uninstall_tool:
            tool_name = self.arguments.args.uninstall_tool
            self.output.printInfo(
                'Selected mode: Uninstall tool named "{0}"'.format(tool_name))
            self.settings.toolbox.removeTool(self.output, tool_name)
            sys.exit(0)

        if self.arguments.args.uninstall_toolbox:
            service = self.arguments.args.uninstall_toolbox
            self.output.printInfo(
                'Selected mode: Uninstall toolbox for service {0}'.format(
                    service))
            self.settings.toolbox.removeToolboxForService(self.output, service)
            sys.exit(0)

        if self.arguments.args.uninstall_all:
            self.output.printInfo('Selected mode: Uninstall the whole toolbox')
            self.settings.toolbox.removeToolbox(self.output)
            sys.exit(0)

        if self.arguments.args.list_services:
            self.output.printInfo('Selected mode: List supported services')
            self.settings.toolbox.printListSupportedServices(self.output)
            sys.exit(0)

        if self.arguments.args.list_categories:
            service = self.arguments.args.list_categories
            self.output.printInfo(
                'Selected mode: List tools categories for service {0}'.format(
                    service))
            self.settings.toolbox.printListCategories(self.output, service)
            sys.exit(0)

        if self.arguments.args.list_specific:
            service = self.arguments.args.list_specific
            self.output.printInfo(
                'Selected mode: List context specific options for service {0}'.
                format(service))
            SpecificOptions.listAvailableSpecificOptions(
                self.settings, service, self.output)
            sys.exit(0)

        service = self.arguments.args.service
        output.printInfo(
            'Selected mode: Run tools againt target - Service {0}'.format(
                service))
        print

        # Print target info
        output.printTitle0('Target Summary:')
        self.arguments.target.printSummary(output)

        begin = time.time()

        # Single tool mode
        if self.arguments.args.single_tool:
            tool = self.settings.toolbox.isInToolboxForService(
                self.arguments.args.single_tool, service)
            if not tool:
                sys.exit(0)

            output_file = FileUtils.absolute_path(
                FileUtils.concat_path(self.arguments.args.output_dir,
                                      tool.name + '.txt'))
            output_dir = FileUtils.absolute_path(
                FileUtils.concat_path(self.arguments.args.output_dir,
                                      tool.name))
            print
            output.printTitle1('   ' + tool.name)
            try:
                tool.runTool(self.settings, output, output_file, output_dir,
                             self.arguments.target, self.arguments.specific,
                             False, False)
            except KeyboardInterrupt, SystemExit:
                print
                self.output.printError('Tool execution aborted')

            print
Beispiel #8
0
                        continue

                subdir = FileUtils.concat_path(self.arguments.args.output_dir,
                                               cat)
                if not FileUtils.create_directory(subdir):
                    output.printFail(
                        'Impossible to create output subdir "{0}"'.format(
                            subdir))
                    sys.exit(0)
                output.printInfo('Output subdir "{0}" created'.format(subdir))
                for tool in self.settings.toolbox.tools[service][cat]:
                    print
                    output.printTitle1('   ' + tool.name)

                    # Output for each tool is stored into a file
                    output_file = FileUtils.absolute_path(
                        FileUtils.concat_path(subdir, tool.name + '.txt'))
                    # Some tools (e.g. skipfish) required an output dir too
                    output_dir = FileUtils.absolute_path(
                        FileUtils.concat_path(subdir, tool.name))
                    try:
                        tool.runTool(self.settings, output, output_file,
                                     output_dir, self.arguments.target,
                                     self.arguments.specific,
                                     self.arguments.args.ignore_specific,
                                     self.arguments.args.auto_yes)
                    except KeyboardInterrupt, SystemExit:
                        print
                        self.output.printError('Tool execution aborted')

                    print
        print
Beispiel #9
0
    def getParsedCmdline(self,
                         output_dir=None,
                         output_filename=None,
                         target=None,
                         specific_args=None,
                         remove_args=False):
        """
		Return the parsed command line, i.e. with the tags replaced by their correct values
		according to the context

		@Args 		output_dir:			Directory where outputs are saved (for RUN commands)
					output_filename:	Filename for output (for RUN commands)
					target: 			Target object (for RUN commands)
					specific_args: 		Specific arguments (for RUN commands)
					remove_args:		Boolean indicating if arguments from cmd must be deleted (for RUN commands)
										Used for check install commands

		@Returns 	Tuple	(full parsed cmdline, shortened parsed cmdline)
		"""

        self.parsed_cmdline = self.cmdline
        if self.cmdtype == CommandType.RUN:
            if remove_args:
                self.parsed_cmdline = CmdUtils.removeArgsFromCmd(
                    self.parsed_cmdline)
            else:
                if not output_dir or not output_filename or not target:
                    raise ValueError('Missing required arguments')

                output_dir = FileUtils.absolute_path(output_dir)
                output_file = FileUtils.concat_path(output_dir,
                                                    output_filename)
                output_subdir = FileUtils.concat_path(
                    output_dir, FileUtils.remove_ext(output_filename))

                self.replaceIP(target.ip)
                self.replaceURL(target.url)
                self.replaceHOST(target.host)
                self.replacePORT(target.port)
                self.replacePROTOCOL(target.protocol)
                self.replaceSERVICE(target.service)
                self.replaceOUTPUT(output_file)
                self.replaceOUTPUTDIR(output_subdir)
                self.replaceTOOLBOXDIR(self.toolbox_dir)
                self.replaceWORDLISTSDIR(Constants.WORDLISTS_DIR)
                self.replaceSpecificTags(target.service, specific_args)

        elif self.cmdtype in (CommandType.INSTALL, CommandType.UPDATE):
            self.replaceTOOLBOXDIR(self.toolbox_dir)

        else:
            raise ValueError('Invalid command type')

        # Shortened parsed command line:
        # 	- without "cd [...]" prefix
        # 	- without "2>&1 | tee [...]" suffix
        short_cmdline = self.parsed_cmdline
        endcmd_index = short_cmdline.rfind('2>&1 | tee')
        if endcmd_index > 0:
            short_cmdline = short_cmdline[:endcmd_index].strip()

        # Full parsed command line:
        self.parsed_cmdline = 'cd {0}; '.format(
            self.current_dir) + self.parsed_cmdline

        return self.parsed_cmdline, short_cmdline