def missing_host_key(self, client, hostname, key): host_key_fingerprint = 'sha256:' + base64.b64encode(hashlib.new('sha256', key.asbytes()).digest()).decode('utf-8') host_keys = paramiko.hostkeys.HostKeys() host_keys_modified = False known_hosts_file = self.application.config.get('ssh_known_hosts_file', os.path.join(GLib.get_user_config_dir(), 'king-phisher', 'known_hosts')) if os.access(known_hosts_file, os.R_OK): host_keys.load(known_hosts_file) if host_keys.lookup(hostname): if host_keys.check(hostname, key): self.logger.debug("accepting known ssh host key {0} {1} {2}".format(hostname, key.get_name(), host_key_fingerprint)) return self.logger.warning("ssh host key does not match known value for {0}".format(hostname)) dialog = HostKeyWarnDialog(self.application, hostname, key) if dialog.interact() != Gtk.ResponseType.ACCEPT: raise errors.KingPhisherAbortError('bad ssh host key for ' + hostname) else: dialog = HostKeyAcceptDialog(self.application, hostname, key) if dialog.interact() != Gtk.ResponseType.ACCEPT: raise errors.KingPhisherAbortError('unknown ssh host key for ' + hostname) host_keys.add(hostname, key.get_name(), key) host_keys_modified = True if host_keys_modified: host_keys.save(known_hosts_file) os.chmod(known_hosts_file, 0o600)
def missing_host_key(self, client, hostname, key): host_key_fingerprint = 'sha256:' + base64.b64encode( hashlib.new('sha256', key.asbytes()).digest()).decode('utf-8') host_keys = paramiko.hostkeys.HostKeys() known_hosts_file = self.application.config.get( 'ssh_known_hosts_file', os.path.join(GLib.get_user_config_dir(), 'king-phisher', 'known_hosts')) if os.access(known_hosts_file, os.R_OK): self.logger.debug('loading known ssh host keys from: ' + known_hosts_file) host_keys.load(known_hosts_file) else: self.logger.warning('can not read known ssh host keys from: ' + known_hosts_file) add_host_key = False if host_keys.lookup(hostname): if host_keys.check(hostname, key): self.logger.debug( "accepting known ssh host key {0} {1} {2}".format( hostname, key.get_name(), host_key_fingerprint)) return self.logger.warning( "ssh host key does not match known value for {0}".format( hostname)) dialog = HostKeyWarnDialog(self.application, hostname, key) if dialog.interact() != Gtk.ResponseType.ACCEPT: raise errors.KingPhisherAbortError('bad ssh host key for ' + hostname) add_host_key = dialog.accept_permanently else: dialog = HostKeyAcceptDialog(self.application, hostname, key) if dialog.interact() != Gtk.ResponseType.ACCEPT: raise errors.KingPhisherAbortError( 'unknown ssh host key not accepted by the user for ' + hostname) add_host_key = True if add_host_key: self.logger.debug("setting ssh host key {0} for {1}".format( key.get_name(), hostname)) if hostname in host_keys: host_keys.pop(hostname) host_keys.add(hostname, key.get_name(), key) try: host_keys.save(known_hosts_file) os.chmod(known_hosts_file, 0o600) except IOError if its.py_v2 else PermissionError: self.logger.warning( 'failed to save the known_hosts file and set its permissions' )