def __parse_all_conf_files(self, files): """ Parse all *.conf files into the settings directory. Initialize ServicesConfig object with list of supported services :param files: List of files in settings directory :return: None """ list_services = list() for f in files: name = FileUtils.remove_ext(f).lower().strip() if name not in (INSTALL_STATUS_CONF_FILE, TOOLBOX_CONF_FILE): list_services.append(name) full_path = FileUtils.concat_path(SETTINGS_DIR, f) self.config_parsers[name] = DefaultConfigParser() self.config_parsers[name].read(full_path, 'utf8') # utf8 to avoid encoding issues list_services.append('multi') # Add support for special "multi" service self.services = ServicesConfig(list_services)
def parseToolsConfFile(self, file): """ Parse a given settings file """ #self.output.printInfo('Parsing configuration file "{0}" ...'.format(file)) full_path = FileUtils.concat_path(self.settings_dir, file) conf_filename = FileUtils.remove_ext(file).lower().strip() self.config_parsers[conf_filename] = DefaultConfigParser() self.config_parsers[conf_filename].read(full_path) # Add the entry into general settings for the service self.general_settings[conf_filename] = {} if conf_filename == MULTI_SERVICES_CONF_FILE: self.general_settings[conf_filename]['tools_categories'] = ['all'] else: # General settings - [general] in .conf file tools_cats = self.config_parsers[conf_filename].safeGetList( 'general', 'tools_categories', ',', []) self.general_settings[conf_filename]['tools_categories'] = [ StringUtils.cleanSpecialChars(e).lower() for e in tools_cats ] # General settings - Optional/Specific settings (depends on the targeted service) if conf_filename in SPECIFIC_TOOL_OPTIONS.keys(): for option in SPECIFIC_TOOL_OPTIONS[conf_filename]: setting_name = SPECIFIC_TOOL_OPTIONS[conf_filename][option] if setting_name: self.general_settings[conf_filename][setting_name] = \ [ e.lower() for e in self.config_parsers[conf_filename].safeGetList('general', setting_name, ',', []) ] # Check general settings for the current service self.checkGeneralSettings(conf_filename) # Add service as new toolbox section self.toolbox.addService(conf_filename) # Add tools in current config file into the toolbox, under the correct service section for section in self.config_parsers[conf_filename].sections(): if section.startswith(PREFIX_TOOL_SECTIONNAME): if conf_filename != MULTI_SERVICES_CONF_FILE: newtool = self.createToolFromConfiguration( section, conf_filename, tooltype=ToolType.STANDARD) else: newtool = self.createToolFromConfiguration( section, conf_filename, tooltype=ToolType.MULTI_SERVICES) elif section.startswith(PREFIX_TOOL_USEMULTI_SECTIONNAME): newtool = self.createToolFromConfiguration( section, conf_filename, tooltype=ToolType.USE_MULTI) else: continue if newtool: if not self.toolbox.addTool(newtool, conf_filename): self.output.printWarning( 'Unable to add tool "{0}" into the toolbox'.format( newtool.name)) else: #self.output.printSettings('Tool "{0}" added into the toolbox (category "{1}")'.format(newtool.name, # newtool.category)) pass
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