def __run_install_update(self, fast_mode, update=False): """ Run install or update command. :param fast_mode: Set to true to disable prompts :param update: Mode selector, True for update | False for install (default) :return: Install/Update status :rtype: bool """ if update : cmd = self.update_command.get_cmdline(self.tool_dir) else : cmd = self.install_command.get_cmdline(self.tool_dir) mode = 'update' if update else 'install' logger.info('Description: {descr}'.format(descr=self.description)) #Output.print('{mode} command : {cmd}'.format( # mode=mode.capitalize(), cmd=cmd_short)) if fast_mode \ or Output.prompt_confirm('Confirm {mode} ?'.format(mode=mode), default=True): Output.begin_cmd(cmd) ProcessLauncher(cmd).start() Output.delimiter() logger.success('Tool {mode} has finished'.format(mode=mode)) return True else: logger.warning('Tool {mode} aborted'.format(mode=mode)) return False
def __run_check_command(self): """ Run the check command. The goal is to quickly check if the tool is not buggy or missing some dependencies :return: Boolean indicating if tool is correctly installed """ logger.info('Running the check command for the tool {tool}...'.format( tool=self.name_display)) cmd = self.check_command.get_cmdline(self.tool_dir) Output.begin_cmd(cmd) ProcessLauncher(cmd).start() Output.delimiter() return Output.prompt_confirm( 'Does the tool {tool} seem to be running correctly ?'.format( tool=self.name_display), default=True)
def __run_check_command(self): """ Run the check command. The goal is to quickly check if the tool is not buggy or missing some dependencies. The user must analyze the output and gives an answer. :return: Response from user :rtype: bool """ logger.info('Running the check command for the tool {tool}...'.format( tool=self.name)) cmd = self.check_command.get_cmdline(self.tool_dir) Output.begin_cmd(cmd) ProcessLauncher(cmd).start() Output.delimiter() return Output.prompt_confirm('Does the tool {tool} seem to be running ' \ 'correctly ?'.format(tool=self.name), default=True)
def run_check_command(self, fast_mode=False): """ Run the check command. The goal is to quickly check if the tool is not buggy or missing some dependencies. The user must analyze the output and gives an answer. :param bool fast_mode: Set to true to disable prompts :return: Response from user in interactive mode, otherwise status based on exit code (True if exit code is 0) :rtype: bool """ if not self.check_command: logger.info('No check_command defined in settings for the tool ' \ '{tool}'.format(tool=self.name)) return True logger.info('Running the check command for the tool {tool}...'.format( tool=self.name)) cmd = self.check_command.get_cmdline(self) Output.begin_cmd(cmd) returncode, _ = ProcessLauncher(cmd).start() Output.delimiter() if returncode != 0: logger.warning('Check command has finished with an error ' \ 'exit code: {code}'.format(code=returncode)) else: logger.success('Check command has finished with success exit code') if fast_mode: return (returncode == 0) else: return Output.prompt_confirm('Does the tool {tool} seem to be running ' \ 'correctly ?'.format(tool=self.name), default=True)
def run(self, target, arguments, sqlsession, fast_mode=False): """ Run the security check. It consists in running commands with context requirements matching with the target's context. :param Target target: Target :param ArgumentsParser arguments: Arguments from command-line :param Session sqlsession: SQLAlchemy session :param SmartModulesLoader smartmodules_loader: Loader of SmartModules :param bool fast_mode: Set to true to disable prompts :return: Status :rtype: bool """ if not self.tool.installed: return False i = 1 command_outputs = list() for command in self.commands: if command.context_requirements.check_target_compliance(target): if not command.context_requirements.is_empty: logger.info('Command #{num:02} matches requirements: ' \ '{context}'.format(num=i, context=command.context_requirements)) cmdline = command.get_cmdline(self.tool.tool_dir, target, arguments) if fast_mode: logger.info('Run command #{num:02}'.format(num=i)) mode = 'y' else: mode = Output.prompt_choice( 'Run command {num}? [Y/n/f/q] '.format( num='' if len(self.commands) == 1 else \ '#{num:02} '.format(num=i)), choices={ 'y': 'Yes', 'n': 'No', #'t': 'New tab', #'w': 'New window', 'f': 'Switch to fast mode (do not prompt anymore)', 'q': 'Quit the program', }, default='y') if mode == 'q': logger.warning('Exit !') sys.exit(0) elif mode == 'n': logger.info('Skipping this command') continue else: if mode == 'f': logger.info('Switch to fast mode') arguments.args.fast_mode = True Output.begin_cmd(cmdline) process = ProcessLauncher(cmdline) if mode == 'y' or mode == 'f': output = process.start() # elif mode == 't': # output = process.start_in_new_tab() # logger.info('Command started in new tab') # else: # output = process.start_in_new_window(self.name) # logger.info('Command started in new window') Output.delimiter() print() output = StringUtils.interpret_ansi_escape_clear_lines( output) outputraw = StringUtils.remove_ansi_escape(output) command_outputs.append( CommandOutput(cmdline=cmdline, output=output, outputraw=outputraw)) # Run smartmodule method on output postcheck = SmartPostcheck( target.service, sqlsession, self.tool.name, '{0}\n{1}'.format(cmdline, outputraw)) postcheck.run() else: logger.info('Command #{num:02} does not match requirements: ' \ '{context}'.format(num=i, context=command.context_requirements)) logger.debug('Context string: {rawstr}'.format( rawstr=command.context_requirements)) i += 1 # Add outputs in database if command_outputs: results_requester = ResultsRequester(sqlsession) results_requester.add_result(target.service.id, self.name, self.category, command_outputs) return True
def run(self, target, smartmodules_loader, results_requester, fast_mode=False): """ Run the check, i.e. run the commands for which Target's specific options and authentication level are matching the required context. :param target : Target object :param smartmodules_loader: :param results_requester: ResultsRequester object :param fast_mode: Boolean indicating whether prompts must be displayed or not :return: """ if not self.tool.installed: return False i = 1 command_outputs = list() for command in self.commands: if target.is_matching_context(command.context): if command.context: logger.info( 'Command #{num:02} is matching current target\'s context: {context}' .format(num=i, context=command.context)) cmdline = command.get_cmdline(self.tool.tool_dir, target) #if i == 1: logger.info('Check: {descr}'.format(descr=self.description)) #logger.info('Command #{num:02}: {cmd}'.format(num=i, cmd=cmd_short)) if fast_mode: logger.info('Run command #{num:02}'.format(num=i)) mode = 'y' else: mode = Output.prompt_choice( 'Run command #{num:02} ? [Y/n/t/w/q] '.format(num=i), choices={ 'y': 'Yes', 'n': 'No', 't': 'New tab', 'w': 'New window', 'q': 'Quit the program' }, default='y') if mode == 'q': logger.warning('Exit !') sys.exit(0) elif mode == 'n': logger.info('Skipping this command') continue else: Output.begin_cmd(cmdline) process = ProcessLauncher(cmdline) if mode == 'y': output = process.start() elif mode == 't': output = process.start_in_new_tab() logger.info('Command started in new tab') else: output = process.start_in_new_window(self.name) logger.info('Command started in new window') Output.delimiter() print() command_outputs.append( CommandOutput(cmdline=cmdline, output=output)) if self.postrun: smartmodules_loader.call_postcheck_method( self.postrun, target.service, output) else: logger.info( 'Command #{num:02} is not matching current target\'s context: {context}' .format(num=i, context=command.context)) i += 1 if i == 1: logger.warning('This check is skipped') else: # Add output(s) in db results_requester.add_result(target.service.id, self.name, self.category, command_outputs)