Guest User

Untitled

a guest
Jun 25th, 2018
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.55 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import sys
  4. import socket
  5. import re
  6. import smtplib
  7. import string
  8. import time
  9.  
  10. accepted_hosts = ['dsl.pitt.sbcglobal.net', 'dsl.bell.ca']
  11. log_file = '/var/log/secure'
  12. bad_hosts = []
  13. root_email = 'marty@nme-rs.com'
  14.  
  15. def main():
  16.     time_start = time.time()
  17.     print 'Script started'
  18.     for accepted_host in accepted_hosts:
  19.         print 'Allowed host: ' + accepted_host
  20.     with open(log_file) as file:
  21.         file_lines = file.readlines()
  22.     print 'Looping through ' + str(len(file_lines)) + ' lines'
  23.     for line in file_lines:
  24.         if "Accepted password for" in line:
  25.             ip = re.search(r'[0-9]+(?:\.[0-9]+){3}', line).group()
  26.             host = socket.gethostbyaddr(ip)[0]
  27.             host_allowed = False
  28.             for accepted_host in accepted_hosts:
  29.                 if re.search(accepted_host, host):
  30.                     host_allowed = True
  31.             if host_allowed == False:
  32.                 print 'Bad host: ' + host
  33.                 bad_hosts.append(line)
  34.     if len(bad_hosts) != 0:
  35.         print 'Bad hosts found: ' + str(len(bad_hosts))
  36.         print 'Sending email to ' + root_email + '...'
  37.         email_subject = "POSSIBLE BREAK-IN [NON-WHITELISTED HOSTS]"
  38.         email_to = root_email
  39.         email_from = "root@nme-rs.com"
  40.         email_text = string.join(bad_hosts)
  41.         email_body = string.join((
  42.             "From: %s" % email_from,
  43.             "To: %s" % email_to,
  44.             "Subject: %s" % email_subject ,
  45.             "",
  46.             email_text
  47.             ), "\r\n")
  48.         server = smtplib.SMTP('localhost')
  49.         server.sendmail(email_from, [email_to], email_body)
  50.         server.quit()
  51.     print 'Script finished, execution time: ' + str(time.time() - time_start) + ' seconds'
  52.  
  53. if __name__ == '__main__':
  54.     main()
Add Comment
Please, Sign In to add comment