def output_security_sub(out: OutputBuffer, sub: str, software: Optional[Software], client_audit: bool, padlen: int) -> None: secdb = VersionVulnerabilityDB.CVE if sub == 'cve' else VersionVulnerabilityDB.TXT if software is None or software.product not in secdb: return for line in secdb[software.product]: vfrom: str = '' vtill: str = '' vfrom, vtill = line[0:2] if not software.between_versions(vfrom, vtill): continue target: int = 0 name: str = '' target, name = line[2:4] is_server = target & 1 == 1 is_client = target & 2 == 2 # is_local = target & 4 == 4 # If this security entry applies only to servers, but we're testing a client, then skip it. Similarly, skip entries that apply only to clients, but we're testing a server. if (is_server and not is_client and client_audit) or (is_client and not is_server and not client_audit): continue p = '' if out.batch else ' ' * (padlen - len(name)) if sub == 'cve': cvss: float = 0.0 descr: str = '' cvss, descr = line[4:6] # Critical CVSS scores (>= 8.0) are printed as a fail, otherwise they are printed as a warning. out_func = out.warn if cvss >= 8.0: out_func = out.fail out_func('(cve) {}{} -- (CVSSv2: {}) {}'.format(name, p, cvss, descr)) else: descr = line[4] out.fail('(sec) {}{} -- {}'.format(name, p, descr))
def output_compatibility(out: OutputBuffer, algs: Algorithms, client_audit: bool, for_server: bool = True) -> None: # Don't output any compatibility info if we're doing a client audit. if client_audit: return ssh_timeframe = algs.get_ssh_timeframe(for_server) comp_text = [] for ssh_prod in [Product.OpenSSH, Product.DropbearSSH]: if ssh_prod not in ssh_timeframe: continue v_from = ssh_timeframe.get_from(ssh_prod, for_server) v_till = ssh_timeframe.get_till(ssh_prod, for_server) if v_from is None: continue if v_till is None: comp_text.append('{} {}+'.format(ssh_prod, v_from)) elif v_from == v_till: comp_text.append('{} {}'.format(ssh_prod, v_from)) else: software = Software(None, ssh_prod, v_from, None, None) if software.compare_version(v_till) > 0: tfmt = '{0} {1}+ (some functionality from {2})' else: tfmt = '{0} {1}-{2}' comp_text.append(tfmt.format(ssh_prod, v_from, v_till)) if len(comp_text) > 0: out.good('(gen) compatibility: ' + ', '.join(comp_text))
def output_security(out: OutputBuffer, banner: Optional[Banner], client_audit: bool, padlen: int, is_json_output: bool) -> None: with out: if banner is not None: software = Software.parse(banner) output_security_sub(out, 'cve', software, client_audit, padlen) output_security_sub(out, 'txt', software, client_audit, padlen) if banner.protocol[0] == 1: p = '' if out.batch else ' ' * (padlen - 14) out.fail('(sec) SSH v1 enabled{} -- SSH v1 can be exploited to recover plaintext passwords'.format(p)) if not out.is_section_empty() and not is_json_output: out.head('# security') out.flush_section() out.sep()
def windows_manual(out: OutputBuffer) -> int: '''Prints the man page on Windows. Returns an exitcodes.* flag.''' retval = exitcodes.GOOD if sys.platform != 'win32': out.fail("The '-m' and '--manual' parameters are reserved for use on Windows only.\nUsers of other operating systems should read the man page.") retval = exitcodes.FAILURE return retval # If colors are disabled, strip the ANSI color codes from the man page. windows_man_page = WINDOWS_MAN_PAGE if not out.use_colors: windows_man_page = re.sub(r'\x1b\[\d+?m', '', windows_man_page) out.info(windows_man_page) return retval
def output_security(banner: Optional[Banner], client_audit: bool, padlen: int, is_json_output: bool) -> None: with OutputBuffer() as obuf: if banner is not None: software = Software.parse(banner) output_security_sub('cve', software, client_audit, padlen) output_security_sub('txt', software, client_audit, padlen) if len(obuf) > 0 and not is_json_output: out.head('# security') obuf.flush() out.sep()
def _audit(self, spy, conf=None, exit_expected=False): if conf is None: conf = self._conf() spy.begin() out = OutputBuffer() if exit_expected: with pytest.raises(SystemExit): self.audit(out, conf) else: ret = self.audit(out, conf) assert ret != 0 out.write() lines = spy.flush() # If the last line is empty, delete it. if len(lines) > 1 and lines[-1] == '': del lines[-1] return lines
def output_algorithms(out: OutputBuffer, title: str, alg_db: Dict[str, Dict[str, List[List[Optional[str]]]]], alg_type: str, algorithms: List[str], unknown_algs: List[str], is_json_output: bool, program_retval: int, maxlen: int = 0, alg_sizes: Optional[Dict[str, Tuple[int, int]]] = None) -> int: # pylint: disable=too-many-arguments with out: for algorithm in algorithms: program_retval = output_algorithm(out, alg_db, alg_type, algorithm, unknown_algs, program_retval, maxlen, alg_sizes) if not out.is_section_empty() and not is_json_output: out.head('# ' + title) out.flush_section() out.sep() return program_retval
def output_info(software: Optional['Software'], client_audit: bool, any_problems: bool, is_json_output: bool) -> None: with OutputBuffer() as obuf: # Tell user that PuTTY cannot be hardened at the protocol-level. if client_audit and (software is not None) and (software.product == Product.PuTTY): out.warn( '(nfo) PuTTY does not have the option of restricting any algorithms during the SSH handshake.' ) # If any warnings or failures were given, print a link to the hardening guides. if any_problems: out.warn( '(nfo) For hardening guides on common OSes, please see: <https://www.ssh-audit.com/hardening_guides.html>' ) if len(obuf) > 0 and not is_json_output: out.head('# additional info') obuf.flush() out.sep()
def target_worker_thread(host: str, port: int, shared_aconf: AuditConf) -> Tuple[int, str]: ret = -1 string_output = '' out = OutputBuffer() out.verbose = shared_aconf.verbose my_aconf = copy.deepcopy(shared_aconf) my_aconf.host = host my_aconf.port = port # If we're outputting JSON, turn off colors and ensure 'info' level messages go through. if my_aconf.json: out.json = True out.use_colors = False out.v("Running against: %s:%d..." % (my_aconf.host, my_aconf.port), write_now=True) try: ret = audit(out, my_aconf, print_target=True) string_output = out.get_buffer() except Exception: ret = -1 string_output = "An exception occurred while scanning %s:%d:\n%s" % (host, port, str(traceback.format_exc())) return ret, string_output
def output_fingerprints(algs: Algorithms, is_json_output: bool, sha256: bool = True) -> None: with OutputBuffer() as obuf: fps = [] if algs.ssh1kex is not None: name = 'ssh-rsa1' fp = Fingerprint(algs.ssh1kex.host_key_fingerprint_data) # bits = algs.ssh1kex.host_key_bits fps.append((name, fp)) if algs.ssh2kex is not None: host_keys = algs.ssh2kex.host_keys() for host_key_type in algs.ssh2kex.host_keys(): if host_keys[host_key_type] is None: continue fp = Fingerprint(host_keys[host_key_type]) # Workaround for Python's order-indifference in dicts. We might get a random RSA type (ssh-rsa, rsa-sha2-256, or rsa-sha2-512), so running the tool against the same server three times may give three different host key types here. So if we have any RSA type, we will simply hard-code it to 'ssh-rsa'. if host_key_type in HostKeyTest.RSA_FAMILY: host_key_type = 'ssh-rsa' # Skip over certificate host types (or we would return invalid fingerprints). if '-cert-' not in host_key_type: fps.append((host_key_type, fp)) # Similarly, the host keys can be processed in random order due to Python's order-indifference in dicts. So we sort this list before printing; this makes automated testing possible. fps = sorted(fps) for fpp in fps: name, fp = fpp fpo = fp.sha256 if sha256 else fp.md5 # p = '' if out.batch else ' ' * (padlen - len(name)) # out.good('(fin) {0}{1} -- {2} {3}'.format(name, p, bits, fpo)) out.good('(fin) {}: {}'.format(name, fpo)) if len(obuf) > 0 and not is_json_output: out.head('# fingerprints') obuf.flush() out.sep()
def process_commandline(out: OutputBuffer, args: List[str], usage_cb: Callable[..., None]) -> 'AuditConf': # pylint: disable=too-many-statements # pylint: disable=too-many-branches aconf = AuditConf() try: sopts = 'h1246M:p:P:jbcnvl:t:T:Lmd' lopts = ['help', 'ssh1', 'ssh2', 'ipv4', 'ipv6', 'make-policy=', 'port=', 'policy=', 'json', 'batch', 'client-audit', 'no-colors', 'verbose', 'level=', 'timeout=', 'targets=', 'list-policies', 'lookup=', 'threads=', 'manual', 'debug'] opts, args = getopt.gnu_getopt(args, sopts, lopts) except getopt.GetoptError as err: usage_cb(str(err)) aconf.ssh1, aconf.ssh2 = False, False host: str = '' oport: Optional[str] = None port: int = 0 for o, a in opts: if o in ('-h', '--help'): usage_cb() elif o in ('-1', '--ssh1'): aconf.ssh1 = True elif o in ('-2', '--ssh2'): aconf.ssh2 = True elif o in ('-4', '--ipv4'): aconf.ipv4 = True elif o in ('-6', '--ipv6'): aconf.ipv6 = True elif o in ('-p', '--port'): oport = a elif o in ('-b', '--batch'): aconf.batch = True aconf.verbose = True elif o in ('-c', '--client-audit'): aconf.client_audit = True elif o in ('-n', '--no-colors'): aconf.colors = False out.use_colors = False elif o in ('-j', '--json'): if aconf.json: # If specified twice, enable indent printing. aconf.json_print_indent = True else: aconf.json = True elif o in ('-v', '--verbose'): aconf.verbose = True out.verbose = True elif o in ('-l', '--level'): if a not in ('info', 'warn', 'fail'): usage_cb('level {} is not valid'.format(a)) aconf.level = a elif o in ('-t', '--timeout'): aconf.timeout = float(a) aconf.timeout_set = True elif o in ('-M', '--make-policy'): aconf.make_policy = True aconf.policy_file = a elif o in ('-P', '--policy'): aconf.policy_file = a elif o in ('-T', '--targets'): aconf.target_file = a elif o == '--threads': aconf.threads = int(a) elif o in ('-L', '--list-policies'): aconf.list_policies = True elif o == '--lookup': aconf.lookup = a elif o in ('-m', '--manual'): aconf.manual = True elif o in ('-d', '--debug'): aconf.debug = True out.debug = True if len(args) == 0 and aconf.client_audit is False and aconf.target_file is None and aconf.list_policies is False and aconf.lookup == '' and aconf.manual is False: usage_cb() if aconf.manual: return aconf if aconf.lookup != '': return aconf if aconf.list_policies: list_policies(out) sys.exit(exitcodes.GOOD) if aconf.client_audit is False and aconf.target_file is None: if oport is not None: host = args[0] else: host, port = Utils.parse_host_and_port(args[0]) if not host and aconf.target_file is None: usage_cb('host is empty') if port == 0 and oport is None: if aconf.client_audit: # The default port to listen on during a client audit is 2222. port = 2222 else: port = 22 if oport is not None: port = Utils.parse_int(oport) if port <= 0 or port > 65535: usage_cb('port {} is not valid'.format(oport)) aconf.host = host aconf.port = port if not (aconf.ssh1 or aconf.ssh2): aconf.ssh1, aconf.ssh2 = True, True # If a file containing a list of targets was given, read it. if aconf.target_file is not None: try: with open(aconf.target_file, 'r', encoding='utf-8') as f: aconf.target_list = f.readlines() except PermissionError as e: # If installed as a Snap package, print a more useful message with potential work-arounds. if SNAP_PACKAGE: print(SNAP_PERMISSIONS_ERROR) else: print("Error: insufficient permissions: %s" % str(e)) sys.exit(exitcodes.UNKNOWN_ERROR) # Strip out whitespace from each line in target file, and skip empty lines. aconf.target_list = [target.strip() for target in aconf.target_list if target not in ("", "\n")] # If a policy file was provided, validate it. if (aconf.policy_file is not None) and (aconf.make_policy is False): # First, see if this is a built-in policy name. If not, assume a file path was provided, and try to load it from disk. aconf.policy = Policy.load_builtin_policy(aconf.policy_file) if aconf.policy is None: try: aconf.policy = Policy(policy_file=aconf.policy_file) except Exception as e: out.fail("Error while loading policy file: %s: %s" % (str(e), traceback.format_exc())) out.write() sys.exit(exitcodes.UNKNOWN_ERROR) # If the user wants to do a client audit, but provided a server policy, terminate. if aconf.client_audit and aconf.policy.is_server_policy(): out.fail("Error: client audit selected, but server policy provided.") out.write() sys.exit(exitcodes.UNKNOWN_ERROR) # If the user wants to do a server audit, but provided a client policy, terminate. if aconf.client_audit is False and aconf.policy.is_server_policy() is False: out.fail("Error: server audit selected, but client policy provided.") out.write() sys.exit(exitcodes.UNKNOWN_ERROR) return aconf
def list_policies(out: OutputBuffer) -> None: '''Prints a list of server & client policies.''' server_policy_names, client_policy_names = Policy.list_builtin_policies() if len(server_policy_names) > 0: out.head('\nServer policies:\n') out.info(" * \"%s\"" % "\"\n * \"".join(server_policy_names)) if len(client_policy_names) > 0: out.head('\nClient policies:\n') out.info(" * \"%s\"" % "\"\n * \"".join(client_policy_names)) out.sep() if len(server_policy_names) == 0 and len(client_policy_names) == 0: out.fail("Error: no built-in policies found!") else: out.info("\nHint: Use -P and provide the full name of a policy to run a policy scan with.\n") out.write()
def evaluate_policy(out: OutputBuffer, aconf: AuditConf, banner: Optional['Banner'], client_host: Optional[str], kex: Optional['SSH2_Kex'] = None) -> bool: if aconf.policy is None: raise RuntimeError('Internal error: cannot evaluate against null Policy!') passed, error_struct, error_str = aconf.policy.evaluate(banner, kex) if aconf.json: json_struct = {'host': aconf.host, 'policy': aconf.policy.get_name_and_version(), 'passed': passed, 'errors': error_struct} out.info(json.dumps(json_struct, indent=4 if aconf.json_print_indent else None, sort_keys=True)) else: spacing = '' if aconf.client_audit: out.info("Client IP: %s" % client_host) spacing = " " # So the fields below line up with 'Client IP: '. else: host = aconf.host if aconf.port != 22: # Check if this is an IPv6 address, as that is printed in a different format. if Utils.is_ipv6_address(aconf.host): host = '[%s]:%d' % (aconf.host, aconf.port) else: host = '%s:%d' % (aconf.host, aconf.port) out.info("Host: %s" % host) out.info("Policy: %s%s" % (spacing, aconf.policy.get_name_and_version())) out.info("Result: %s" % spacing, line_ended=False) # Use these nice unicode characters in the result message, unless we're on Windows (the cmd.exe terminal doesn't display them properly). icon_good = "✔ " icon_fail = "❌ " if Utils.is_windows(): icon_good = "" icon_fail = "" if passed: out.good("%sPassed" % icon_good) else: out.fail("%sFailed!" % icon_fail) out.warn("\nErrors:\n%s" % error_str) return passed
def output(out: OutputBuffer, aconf: AuditConf, banner: Optional[Banner], header: List[str], client_host: Optional[str] = None, kex: Optional[SSH2_Kex] = None, pkm: Optional[SSH1_PublicKeyMessage] = None, print_target: bool = False) -> int: program_retval = exitcodes.GOOD client_audit = client_host is not None # If set, this is a client audit. sshv = 1 if pkm is not None else 2 algs = Algorithms(pkm, kex) with out: if print_target: host = aconf.host # Print the port if it's not the default of 22. if aconf.port != 22: # Check if this is an IPv6 address, as that is printed in a different format. if Utils.is_ipv6_address(aconf.host): host = '[%s]:%d' % (aconf.host, aconf.port) else: host = '%s:%d' % (aconf.host, aconf.port) out.good('(gen) target: {}'. format(host)) if client_audit: out.good('(gen) client IP: {}'.format(client_host)) if len(header) > 0: out.info('(gen) header: ' + '\n'.join(header)) if banner is not None: banner_line = '(gen) banner: {}'.format(banner) if sshv == 1 or banner.protocol[0] == 1: out.fail(banner_line) out.fail('(gen) protocol SSH1 enabled') else: out.good(banner_line) if not banner.valid_ascii: # NOTE: RFC 4253, Section 4.2 out.warn('(gen) banner contains non-printable ASCII') software = Software.parse(banner) if software is not None: out.good('(gen) software: {}'.format(software)) else: software = None output_compatibility(out, algs, client_audit) if kex is not None: compressions = [x for x in kex.server.compression if x != 'none'] if len(compressions) > 0: cmptxt = 'enabled ({})'.format(', '.join(compressions)) else: cmptxt = 'disabled' out.good('(gen) compression: {}'.format(cmptxt)) if not out.is_section_empty() and not aconf.json: # Print output when it exists and JSON output isn't requested. out.head('# general') out.flush_section() out.sep() maxlen = algs.maxlen + 1 output_security(out, banner, client_audit, maxlen, aconf.json) # Filled in by output_algorithms() with unidentified algs. unknown_algorithms: List[str] = [] if pkm is not None: adb = SSH1_KexDB.ALGORITHMS ciphers = pkm.supported_ciphers auths = pkm.supported_authentications title, atype = 'SSH1 host-key algorithms', 'key' program_retval = output_algorithms(out, title, adb, atype, ['ssh-rsa1'], unknown_algorithms, aconf.json, program_retval, maxlen) title, atype = 'SSH1 encryption algorithms (ciphers)', 'enc' program_retval = output_algorithms(out, title, adb, atype, ciphers, unknown_algorithms, aconf.json, program_retval, maxlen) title, atype = 'SSH1 authentication types', 'aut' program_retval = output_algorithms(out, title, adb, atype, auths, unknown_algorithms, aconf.json, program_retval, maxlen) if kex is not None: adb = SSH2_KexDB.ALGORITHMS title, atype = 'key exchange algorithms', 'kex' program_retval = output_algorithms(out, title, adb, atype, kex.kex_algorithms, unknown_algorithms, aconf.json, program_retval, maxlen, kex.dh_modulus_sizes()) title, atype = 'host-key algorithms', 'key' program_retval = output_algorithms(out, title, adb, atype, kex.key_algorithms, unknown_algorithms, aconf.json, program_retval, maxlen, kex.rsa_key_sizes()) title, atype = 'encryption algorithms (ciphers)', 'enc' program_retval = output_algorithms(out, title, adb, atype, kex.server.encryption, unknown_algorithms, aconf.json, program_retval, maxlen) title, atype = 'message authentication code algorithms', 'mac' program_retval = output_algorithms(out, title, adb, atype, kex.server.mac, unknown_algorithms, aconf.json, program_retval, maxlen) output_fingerprints(out, algs, aconf.json) perfect_config = output_recommendations(out, algs, software, aconf.json, maxlen) output_info(out, software, client_audit, not perfect_config, aconf.json) if aconf.json: out.reset() # Build & write the JSON struct. out.info(json.dumps(build_struct(aconf.host + ":" + str(aconf.port), banner, kex=kex, client_host=client_host), indent=4 if aconf.json_print_indent else None, sort_keys=True)) elif len(unknown_algorithms) > 0: # If we encountered any unknown algorithms, ask the user to report them. out.warn("\n\n!!! WARNING: unknown algorithm(s) found!: %s. Please email the full output above to the maintainer ([email protected]), or create a Github issue at <https://github.com/jtesta/ssh-audit/issues>.\n" % ','.join(unknown_algorithms)) return program_retval
def output_recommendations(out: OutputBuffer, algs: Algorithms, software: Optional[Software], is_json_output: bool, padlen: int = 0) -> bool: ret = True # PuTTY's algorithms cannot be modified, so there's no point in issuing recommendations. if (software is not None) and (software.product == Product.PuTTY): max_vuln_version = 0.0 max_cvssv2_severity = 0.0 # Search the CVE database for the most recent vulnerable version and the max CVSSv2 score. for cve_list in VersionVulnerabilityDB.CVE['PuTTY']: vuln_version = float(cve_list[1]) cvssv2_severity = cve_list[4] if vuln_version > max_vuln_version: max_vuln_version = vuln_version if cvssv2_severity > max_cvssv2_severity: max_cvssv2_severity = cvssv2_severity fn = out.warn if max_cvssv2_severity > 8.0: fn = out.fail # Assuming that PuTTY versions will always increment by 0.01, we can calculate the first safe version by adding 0.01 to the latest vulnerable version. current_version = float(software.version) upgrade_to_version = max_vuln_version + 0.01 if current_version < upgrade_to_version: out.head('# recommendations') fn('(rec) Upgrade to PuTTY v%.2f' % upgrade_to_version) out.sep() ret = False return ret for_server = True with out: software, alg_rec = algs.get_recommendations(software, for_server) for sshv in range(2, 0, -1): if sshv not in alg_rec: continue for alg_type in ['kex', 'key', 'enc', 'mac']: if alg_type not in alg_rec[sshv]: continue for action in ['del', 'add', 'chg']: if action not in alg_rec[sshv][alg_type]: continue for name in alg_rec[sshv][alg_type][action]: p = '' if out.batch else ' ' * (padlen - len(name)) chg_additional_info = '' if action == 'del': an, sg, fn = 'remove', '-', out.warn ret = False if alg_rec[sshv][alg_type][action][name] >= 10: fn = out.fail elif action == 'add': an, sg, fn = 'append', '+', out.good elif action == 'chg': an, sg, fn = 'change', '!', out.fail ret = False chg_additional_info = ' (increase modulus size to 2048 bits or larger)' b = '(SSH{})'.format(sshv) if sshv == 1 else '' fm = '(rec) {0}{1}{2}-- {3} algorithm to {4}{5} {6}' fn(fm.format(sg, name, p, alg_type, an, chg_additional_info, b)) if not out.is_section_empty() and not is_json_output: if software is not None: title = '(for {})'.format(software.display(False)) else: title = '' out.head('# algorithm recommendations {}'.format(title)) out.flush_section(sort_section=True) # Sort the output so that it is always stable (needed for repeatable testing). out.sep() return ret
def output_fingerprints(out: OutputBuffer, algs: Algorithms, is_json_output: bool) -> None: with out: fps = [] if algs.ssh1kex is not None: name = 'ssh-rsa1' fp = Fingerprint(algs.ssh1kex.host_key_fingerprint_data) # bits = algs.ssh1kex.host_key_bits fps.append((name, fp)) if algs.ssh2kex is not None: host_keys = algs.ssh2kex.host_keys() for host_key_type in algs.ssh2kex.host_keys(): if host_keys[host_key_type] is None: continue fp = Fingerprint(host_keys[host_key_type]) # Workaround for Python's order-indifference in dicts. We might get a random RSA type (ssh-rsa, rsa-sha2-256, or rsa-sha2-512), so running the tool against the same server three times may give three different host key types here. So if we have any RSA type, we will simply hard-code it to 'ssh-rsa'. if host_key_type in HostKeyTest.RSA_FAMILY: host_key_type = 'ssh-rsa' # Skip over certificate host types (or we would return invalid fingerprints). if '-cert-' not in host_key_type: fps.append((host_key_type, fp)) # Similarly, the host keys can be processed in random order due to Python's order-indifference in dicts. So we sort this list before printing; this makes automated testing possible. fps = sorted(fps) for fpp in fps: name, fp = fpp out.good('(fin) {}: {}'.format(name, fp.sha256)) # Output the MD5 hash too if verbose mode is enabled. if out.verbose: out.info('(fin) {}: {} -- [info] do not rely on MD5 fingerprints for server identification; it is insecure for this use case'.format(name, fp.md5)) if not out.is_section_empty() and not is_json_output: out.head('# fingerprints') out.flush_section() out.sep()
def main() -> int: out = OutputBuffer() aconf = process_commandline(out, sys.argv[1:], usage) # If we're on Windows, but the colorama module could not be imported, print a warning if we're in verbose mode. if (sys.platform == 'win32') and ('colorama' not in sys.modules): out.v("WARNING: colorama module not found. Colorized output will be disabled.", write_now=True) # If we're outputting JSON, turn off colors and ensure 'info' level messages go through. if aconf.json: out.json = True out.use_colors = False if aconf.manual: # If the colorama module was not be imported, turn off colors in order # to output a plain text version of the man page. if (sys.platform == 'win32') and ('colorama' not in sys.modules): out.use_colors = False retval = windows_manual(out) out.write() sys.exit(retval) if aconf.lookup != '': retval = algorithm_lookup(out, aconf.lookup) out.write() sys.exit(retval) # If multiple targets were specified... if len(aconf.target_list) > 0: ret = exitcodes.GOOD # If JSON output is desired, each target's results will be reported in its own list entry. if aconf.json: print('[', end='') # Loop through each target in the list. target_servers = [] for _, target in enumerate(aconf.target_list): host, port = Utils.parse_host_and_port(target, default_port=22) target_servers.append((host, port)) # A ranked list of return codes. Those with higher indices will take precedence over lower ones. For example, if three servers are scanned, yielding WARNING, GOOD, and UNKNOWN_ERROR, the overall result will be UNKNOWN_ERROR, since its index is the highest. Errors have highest priority, followed by failures, then warnings. ranked_return_codes = [exitcodes.GOOD, exitcodes.WARNING, exitcodes.FAILURE, exitcodes.CONNECTION_ERROR, exitcodes.UNKNOWN_ERROR] # Queue all worker threads. num_target_servers = len(target_servers) num_processed = 0 out.v("Scanning %u targets with %s%u threads..." % (num_target_servers, '(at most) ' if aconf.threads > num_target_servers else '', aconf.threads), write_now=True) with concurrent.futures.ThreadPoolExecutor(max_workers=aconf.threads) as executor: future_to_server = {executor.submit(target_worker_thread, target_server[0], target_server[1], aconf): target_server for target_server in target_servers} for future in concurrent.futures.as_completed(future_to_server): worker_ret, worker_output = future.result() # If this worker's return code is ranked higher that what we've cached so far, update our cache. if ranked_return_codes.index(worker_ret) > ranked_return_codes.index(ret): ret = worker_ret # print("Worker for %s:%d returned %d: [%s]" % (target_server[0], target_server[1], worker_ret, worker_output)) print(worker_output, end='' if aconf.json else "\n") # Don't print a delimiter after the last target was handled. num_processed += 1 if num_processed < num_target_servers: if aconf.json: print(", ", end='') else: print(("-" * 80) + "\n") if aconf.json: print(']') else: # Just a scan against a single target. ret = audit(out, aconf) out.write() return ret