1# 2# sshd.py 3# 4# Find number of ssh logins and authorised users (uses /var/log/auth.log) 5# 6# NOTE: This file is now deprecated in favour of the newer journald mechanism 7# used in sshd-journald.py. This parser is still functional but is slower and 8# has less features. Please switch over if possible. 9# 10 11import re 12 13from logparse.formatting import * 14from logparse.util import readlog, resolve 15from logparse import config 16from logparse.load_parsers import Parser 17 18class Sshd(Parser): 19 20 def __init__(self): 21 super().__init__() 22 self.name = "sshd" 23 self.info = "Find number of ssh logins and authorised users (uses /var/log/auth.log)" 24 self.deprecated = True 25 26 def parse_log(self): 27 28 logger.warning("NOTE: This sshd parser is now deprecated. Please use sshd-journald if possible.") 29 30 logger.debug("Starting sshd section") 31 section = Section("ssh") 32 logger.debug("Searching for matches in {0}".format(config.prefs.get("logs", "auth"))) 33 matches = re.findall('.*sshd.*Accepted publickey for .* from .*', readlog(config.prefs.get("logs", "auth"))) # get all logins 34 logger.debug("Finished searching for logins") 35 36 logger.debug("Searching for matches in {0}".format(config.prefs.get("logs", "auth"))) 37 authlog = readlog(config.prefs.get("logs", "auth")) 38 39 matches = re.findall('.*sshd.*Accepted publickey for .* from .*', authlog) # get all logins 40 invalid_matches = re.findall(".*sshd.*Invalid user .* from .*", authlog) 41 root_matches = re.findall("Disconnected from authenticating user root", authlog) 42 logger.debug("Finished searching for logins") 43 44 users = [] # list of users with format [username, number of logins] for each item 45 data = [] 46 num = len(matches) # total number of logins 47 for match in matches: 48 entry = re.search('^.*publickey\sfor\s(\w*)\sfrom\s(\S*)', match) # [('user', 'ip')] 49 50 user = entry.group(1) 51 ip = entry.group(2) 52 53 userhost = user + '@' + resolve(ip, fqdn=config.prefs.get("sshd", "sshd-resolve-domains")) 54 users.append(userhost) 55 logger.debug("Parsed list of authorised users") 56 57 auth_data = Data(subtitle=plural('login', num) + ' from', items=users) 58 59 if (len(auth_data.items) == 1): # if only one user, do not display no of logins for this user 60 logger.debug("found " + str(len(matches)) + " ssh logins for user " + users[0]) 61 auth_data.subtitle += ' ' + auth_data.items[0] 62 auth_data.orderbyfreq() 63 auth_data.truncl(config.prefs.getint("logparse", "maxlist")) 64 logger.debug("Found " + str(len(matches)) + " ssh logins for users " + str(data)) 65 section.append_data(auth_data) 66 67 invalid_users = [] 68 for match in invalid_matches: 69 entry = re.search('^.*Invalid user (\S+) from (\S+).*', match) # [('user', 'ip')] 70 71 try: 72 user = entry.group(1) 73 ip = entry.group(2) 74 except: # blank user field 75 continue 76 77 userhost = user + '@' + ip 78 invalid_users.append(userhost) 79 logger.debug("Parsed list of invalid users") 80 invalid_data = Data(subtitle=plural("attempted login", len(invalid_matches)) + " from " + plural("invalid user", len(invalid_users), print_quantity=False), items=invalid_users) 81 if (len(invalid_data.items) == 1): # if only one user, do not display no of logins for this user 82 logger.debug("Found " + str(len(invalid_matches)) + " SSH login attempts for invalid user " + invalid_users[0]) 83 invalid_data.subtitle += ' ' + invalid_data.items[0] 84 invalid_data.orderbyfreq() 85 invalid_data.truncl(config.prefs.get("logparse", "maxlist")) 86 logger.debug("Found " + str(len(invalid_matches)) + " SSH login attempts for invalid users " + str(data)) 87 section.append_data(invalid_data) 88 89 logger.debug("Found {0} attempted logins for root".format(str(len(root_matches)))) 90 91 section.append_data(Data(subtitle=plural("attempted login", str(len(root_matches))) + " for root")) 92 93 logger.info("Finished sshd section") 94 return section