Tested with windows and fixed all notes

This commit is contained in:
Vakaris 2018-05-22 18:54:10 +03:00
parent cdb4d459bb
commit a6d2483f7b
2 changed files with 44 additions and 28 deletions

View File

@ -14,24 +14,36 @@ class SSHCollector(object):
SSH keys and known hosts collection module SSH keys and known hosts collection module
""" """
default_dirs = ['/.ssh', '/'] default_dirs = ['/.ssh/', '/']
@staticmethod @staticmethod
def get_info(): def get_info():
LOG.info("Started scanning for ssh keys")
home_dirs = SSHCollector.get_home_dirs() home_dirs = SSHCollector.get_home_dirs()
ssh_info = SSHCollector.get_ssh_files(home_dirs) ssh_info = SSHCollector.get_ssh_files(home_dirs)
LOG.info("Scanned for ssh keys") LOG.info("Scanned for ssh keys")
return ssh_info return ssh_info
@staticmethod
def get_ssh_struct(name, home_dir):
"""
:return: SSH info struct with these fields:
name: username of user, for whom the keys belong
home_dir: users home directory
public_key: contents of *.pub file(public key)
private_key: contents of * file(private key)
known_hosts: contents of known_hosts file(all the servers keys are good for,
possibly hashed)
"""
return {'name': name, 'home_dir': home_dir, 'public_key': None,
'private_key': None, 'known_hosts': None}
@staticmethod @staticmethod
def get_home_dirs(): def get_home_dirs():
home_dirs = [{'name': 'root', 'home_dir': '/root', 'public_key': None, root_dir = SSHCollector.get_ssh_struct('root', '/')
'private_key': None, 'known_hosts': None}] home_dirs = [SSHCollector.get_ssh_struct(x.pw_name,x.pw_dir) for x in pwd.getpwall()
for usr in pwd.getpwall(): if x.pw_dir.startswith('/home')]
if usr[5].startswith('/home'): home_dirs.append(root_dir)
ssh_data = {'name': usr[0], 'home_dir': usr[5], 'public_key': None,
'private_key': None, 'known_hosts': None}
home_dirs.append(ssh_data)
return home_dirs return home_dirs
@staticmethod @staticmethod
@ -41,28 +53,32 @@ class SSHCollector(object):
for directory in SSHCollector.default_dirs: for directory in SSHCollector.default_dirs:
if os.path.isdir(path + directory): if os.path.isdir(path + directory):
try: try:
os.chdir(path + directory) current_path = path + directory
# searching for public key # searching for public key
if glob.glob('*.pub'): if glob.glob(current_path+'*.pub'):
public = '/' + (glob.glob('*.pub')[0]) public = (glob.glob(current_path+'*.pub')[0])
LOG.info("Found public key in %s" % public)
try: try:
with open(path + directory + public) as f: with open(public) as f:
info['public_key'] = f.read() info['public_key'] = f.read()
private = public.split('.')[0] private = public.rsplit('.', 1)[0]
except: if os.path.exists(private):
try:
with open(private) as f:
info['private_key'] = f.read()
LOG.info("Found private key in %s" % private)
except (IOError, OSError):
pass
if os.path.exists(current_path + '/known_hosts'):
try:
with open(current_path + '/known_hosts') as f:
info['known_hosts'] = f.read()
LOG.info("Found known_hosts in %s" % current_path+'/known_hosts')
except (IOError, OSError):
pass
except (IOError, OSError):
pass pass
if os.path.exists(path + directory + private): except OSError:
try:
with open(path + directory + private) as f:
info['private_key'] = f.read()
except:
pass
if os.path.exists(path + directory + '/known_hosts'):
try:
with open(path + directory + '/known_hosts') as f:
info['known_hosts'] = f.read()
except:
pass
except:
pass pass
usr_info = [info for info in usr_info if not info['private_key'] and not info['known_hosts'] and not info['public_key']]
return usr_info return usr_info

View File

@ -27,6 +27,6 @@ class LinuxInfoCollector(InfoCollector):
self.get_process_list() self.get_process_list()
self.get_network_info() self.get_network_info()
self.get_azure_info() self.get_azure_info()
self.info['ssh_info'].update(SSHCollector.get_info()) self.info['ssh_info'] = SSHCollector.get_info()
return self.info return self.info