def get_scan_issues(self, scan_name=None, scan_guid=None, pretty=False): try: if scan_name: api = WebInspectApi(self.url, verify_ssl=False, username=self.username, password=self.password) response = api.get_scan_by_name(scan_name) if response.response_code == 401: Logger.app.critical( "An authentication error occurred, retrieving scan issues!" ) sys.exit(ExitStatus.failure) if response.success: scan_guid = response.data[0]['ID'] else: Logger.app.error(response.message) return None api = WebInspectApi(self.url, verify_ssl=False, username=self.username, password=self.password) response = api.get_scan_issues(scan_guid) if response.response_code == 401: Logger.app.critical("An authentication error occurred!") sys.exit(ExitStatus.failure) if response.success: return response.data_json(pretty=True) else: return None except (ValueError, UnboundLocalError) as e: Logger.app.error( "There was an error getting scan issues: {}".format(e))
def get_scan_log(self, scan_name=None, scan_guid=None): try: if scan_name: api = WebInspectApi(self.url, verify_ssl=False, username=self.username, password=self.password) response = api.get_scan_by_name(scan_name) if response.response_code == 401: Logger.app.critical("An an authentication error occurred while retrieving scan name!") sys.exit(ExitStatus.failure) if response.success: scan_guid = response.data[0]['ID'] else: Logger.app.error(response.message) return None api = WebInspectApi(self.url, verify_ssl=False, username=self.username, password=self.password) response = api.get_scan_log(scan_guid) if response.response_code == 401: Logger.app.critical("An authentication error occurred while reading the scan log!") sys.exit(ExitStatus.failure) if response.success: return response.data_json() else: return None except (ValueError, UnboundLocalError) as e: Logger.app.error("get_scan_log failed: {}".format(e))
def upload_policy(self): # if a policy of the same name already exists, delete it prior to upload try: api = WebInspectApi(self.url, verify_ssl=False, username=self.username, password=self.password) # bit of ugliness here. I'd like to just have the policy name at this point but I don't # so find it in the full path # TODO: Verify split here response = api.get_policy_by_name(ntpath.basename(self.webinspect_upload_policy).split('.')[0]) if response.response_code == 401: Logger.app.critical("An Authorization Error occured.") sys.exit(ExitStatus.failure) if response.success and response.response_code == 200: # the policy exists on the server already api = webinspectapi.WebInspectApi(self.url, verify_ssl=False, username=self.username, password=self.password) response = api.delete_policy(response.data['uniqueId']) if response.success: Logger.app.debug("Deleted policy {} from server".format( ntpath.basename(self.webinspect_upload_policy).split('.')[0])) except (ValueError, UnboundLocalError, TypeError) as e: Logger.app.error("Verify if deletion of existing policy failed: {}".format(e)) try: api = WebInspectApi(self.url, verify_ssl=False, username=self.username, password=self.password) response = api.upload_policy(self.webinspect_upload_policy) if response.response_code == 401: Logger.app.critical("An Authorization Error occured.") sys.exit(ExitStatus.failure) if response.success: Logger.console.debug("Uploaded policy {} to server.".format(self.webinspect_upload_policy)) else: Logger.app.error("Error uploading policy {0}. {1}".format(self.webinspect_upload_policy, response.message)) except (ValueError, UnboundLocalError, TypeError, NameError) as e: logexceptionhelper.LogErrorUploading("policy", e) logexceptionhelper.LogNoWebInspectServerFound(e)
def export_scan_results(self, scan_id, extension): """ Save scan results to file :param scan_id: :param extension: :return: """ # Export scan as a xml for Threadfix or other Vuln Management System Logger.app.info('Exporting scan: {} as {}'.format(scan_id, extension)) detail_type = 'Full' if extension == 'xml' else None api = WebInspectApi(self.url, verify_ssl=False, username=self.username, password=self.password) response = api.export_scan_format(scan_id, extension, detail_type) if response.response_code == 401: Logger.app.critical( "An authentication error occurred, exporting scan results!") sys.exit(ExitStatus.failure) if response.success: try: with open('{0}.{1}'.format(self.scan_name, extension), 'wb') as f: Logger.app.debug( str('Scan results file is available: {0}.{1}\n'.format( self.scan_name, extension))) f.write(response.data) print( str('Scan results file is available: {0}.{1}\n'.format( self.scan_name, extension))) except UnboundLocalError as e: Logger.app.error('Error saving file locally! {}'.format(e)) else: Logger.app.error('Unable to retrieve scan results! {} '.format( response.message))
def stop_scan(self, scan_guid): api = WebInspectApi(self.url, verify_ssl=False, username=self.username, password=self.password) response = api.stop_scan(scan_guid) if response.response_code == 401: Logger.app.critical("An Authorization Error occured.") sys.exit(ExitStatus.failure) return response.success
def _download_proxy(self, webmacro, setting, server): api = WebInspectApi(server, verify_ssl=False, username=self.username, password=self.password) if webmacro: response = api.download_proxy_webmacro(self.proxy_name) extension = 'webmacro' elif setting: response = api.download_proxy_setting(self.proxy_name) extension = 'xml' else: Logger.app.error("Please enter a file type to download.") sys.exit(ExitStatus.failure) APIHelper().check_for_response_errors(response) try: with open('{0}-proxy.{1}'.format(self.proxy_name, extension), 'wb') as f: Logger.app.info( 'Scan results file is available: {0}-proxy.{1}'.format( self.proxy_name, extension)) f.write(response.data) except UnboundLocalError as e: Logger.app.error('Error saving file locally {}'.format(e)) sys.exit(ExitStatus.failure)
def create_scan(self): """ Launches and monitors a scan :return: If scan was able to launch, scan_id. Otherwise none. """ try: Logger.app.debug("Creating Scan in webinspect client") overrides = json.dumps(webinspectjson.formatted_settings_payload(self.settings, self.scan_name, self.runenv, self.scan_mode, self.scan_scope, self.login_macro, self.scan_policy, self.scan_start, self.start_urls, self.workflow_macros, self.allowed_hosts)) api = WebInspectApi(self.url, verify_ssl=False, username=self.username, password=self.password) response = api.create_scan(overrides) if response.response_code == 401: Logger.app.critical("An Authorization Error occured.") sys.exit(ExitStatus.failure) logger_response = json.dumps(response, default=lambda o: o.__dict__, sort_keys=True) Logger.app.debug("Request sent to {0}:\n{1}".format(self.url, overrides)) Logger.app.debug("Response from {0}:\n{1}\n".format(self.url, logger_response)) if response.success: scan_id = response.data['ScanId'] sys.stdout.write(str('WebInspect scan launched on {0} your scan id: {1}\n'.format(self.url, scan_id))) else: Logger.app.error("No scan was launched!\n {}".format(response.message)) sys.exit(ExitStatus.failure) return scan_id except (ValueError, UnboundLocalError) as e: Logger.app.error("Creating the WebInspect scan failed! {}".format(e))
def policy_exists(self, policy_guid): # true if policy exists api = WebInspectApi(self.url, verify_ssl=False, username=self.username, password=self.password) response = api.get_policy_by_guid(policy_guid) if response.response_code == 401: Logger.app.critical("An Authorization Error occured.") sys.exit(ExitStatus.failure) return response.success
def _verify_proxy_server(self, server): api = WebInspectApi(server, verify_ssl=False, username=self.username, password=self.password) response = api.get_proxy_information(self.proxy_name) APIHelper().check_for_response_errors(response) return response.data
def get_policy_by_name(self, policy_name): api = WebInspectApi(self.url, verify_ssl=False, username=self.username, password=self.password) response = api.get_policy_by_name(policy_name) if response.response_code == 401: Logger.app.critical("An authentication error occured, retrieving the policy name!") sys.exit(ExitStatus.failure) if response.success: return response.data else: return None
def _delete_proxy(self, server): api = WebInspectApi(server, verify_ssl=False, username=self.username, password=self.password) response = api.delete_proxy(self.proxy_name) APIHelper().check_for_response_errors(response) Logger.app.info("Proxy: '{0}' deleted from '{1}'".format( self.proxy_name, server))
def __init__(self, url, wiswag_name, username=None, password=None, server=None): auth_config = WebInspectAuth() self.wiswag_name = wiswag_name self.host = server self.username, self.password = auth_config.authenticate(username, password) if self.host is None: self.host = self.get_endpoint() self.api = WebInspectApi(self.host, verify_ssl=False, username=self.username, password=self.password) self.wiswag(url)
def _list_proxy(self, server): if self.proxy_name is None: self._generate_random_proxy_name() api = WebInspectApi(server, verify_ssl=False, username=self.username, password=self.password) response = api.list_proxies() APIHelper().check_for_response_errors(response) return response.data
def get_scan_status(self, scan_guid): api = WebInspectApi(self.url, verify_ssl=False, username=self.username, password=self.password) try: response = api.get_current_status(scan_guid) if response.response_code == 401: Logger.app.critical("An Authorization Error occured.") sys.exit(ExitStatus.failure) status = json.loads(response.data_json())['ScanStatus'] return status except (ValueError, UnboundLocalError, TypeError) as e: Logger.app.error("get_scan_status failed: {}".format(e)) return "Unknown"
def _upload_proxy(self, upload_file, server): try: api = WebInspectApi(server, verify_ssl=False, username=self.username, password=self.password) response = api.upload_webmacro_proxy(self.proxy_name, upload_file) APIHelper().check_for_response_errors(response) Logger.app.info("Uploaded '{0}' to '{1}' on: {2}.".format( upload_file, self.proxy_name, server)) except (ValueError, UnboundLocalError) as e: Logger.app.error("Error uploading policy {}".format(e)) sys.exit(ExitStatus.failure)
def list_webmacros(self): try: api = WebInspectApi(self.url, verify_ssl=False, username=self.username, password=self.password) response = api.list_webmacros() if response.response_code == 401: Logger.app.critical("An Authorization Error occured.") sys.exit(ExitStatus.failure) if response.success: for webmacro in response.data: Logger.console.info("{}".format(webmacro)) else: Logger.app.error("{}".format(response.message)) except (ValueError, UnboundLocalError) as e: Logger.app.error("list_webmacros failed: {}".format(e))
def upload_webmacros(self): try: for webmacro in self.webinspect_upload_webmacros: api = WebInspectApi(self.url, verify_ssl=False, username=self.username, password=self.password) response = api.upload_webmacro(webmacro) if response.response_code == 401: Logger.app.critical("An Authorization Error occured.") sys.exit(ExitStatus.failure) if response.success: Logger.console.debug("Uploaded webmacro {} to server.".format(webmacro)) else: Logger.app.error("Error uploading webmacro {0}. {1}".format(webmacro, response.message)) except (ValueError, UnboundLocalError) as e: logexceptionhelper.LogErrorUploading("webmacro", e) logexceptionhelper.LogNoWebInspectServerFound(e)
def wait_for_scan_status_change(self, scan_id): """ Blocking call, will remain in this method until status of scan changes :param scan_id: :return: """ # WebInspect Scan has started, wait here until it's done api = WebInspectApi(self.url, verify_ssl=False, username=self.username, password=self.password) response = api.wait_for_status_change(scan_id) # this line is the blocker if response.response_code == 401: Logger.app.critical("An Authorization Error occured.") sys.exit(ExitStatus.failure) if response.success: Logger.console.debug('Scan status {}'.format(response.data)) else: Logger.app.debug('Scan status not known because: {}'.format(response.message))
def __settings_exists__(self): try: api = WebInspectApi(self.url, verify_ssl=False, username=self.username, password=self.password) response = api.list_settings() if response.response_code == 401: Logger.app.critical("An Authorization Error occured.") sys.exit(ExitStatus.failure) if response.success: for setting in response.data: if setting in self.settings: return True except (ValueError, UnboundLocalError) as e: Logger.app.error("Unable to determine if setting file exists, scan will continue without setting!" "Error: {}".format(e)) return False
def _get_proxy_certificate(self, server): path = Config().cert api = WebInspectApi(server, verify_ssl=False, username=self.username, password=self.password) response = api.cert_proxy() APIHelper().check_for_response_errors(response) try: with open(path, 'wb') as f: f.write(response.data) Logger.app.info( 'Certificate has downloaded to\t:\t{}'.format(path)) except UnboundLocalError as e: Logger.app.error('Error saving certificate locally {}'.format(e))
def _set_api(self): self.api = WebInspectApi(self.host, verify_ssl=False, username=self.username, password=self.password)