pastebin - collaborative debugging tool
kpaste.net RSS


Untitled
Posted by Anonymous on Sat 8th Dec 2012 00:31
raw | new post

  1. import os, gzip, socket, re, shutil, cPickle, operator, GeoIP
  2. #config stuffs....
  3. path='/home/pronto/logs/'
  4. newfile="ssh_fails.dict.p"
  5. oldfil="ssh_fails.dict.old.p"
  6. gi = GeoIP.open("/home/pronto/scripts/ssh-fail/GeoLiteCity.dat",GeoIP.GEOIP_STANDARD)
  7.  
  8. shutil.copy2('/home/pronto/logs/ssh_fails.dict.p', '/home/pronto/logs/ssh_fails.dict.old.p')
  9. ignore_ip = ['75.101.142.201', '198.101.145.249', '76.21.11.37']
  10. def openfile(logfile):
  11.     if 'gz' in logfile:
  12.         celery = gzip.open(logfile, 'r')
  13.     else:
  14.         celery = open(logfile, 'r')
  15.     return celery
  16.  
  17. def get_index(seq, attribute, value):
  18.     return next(index for (index, d) in enumerate(seq) if d[attribute] == value)
  19.  
  20. def ipcheck(ip,dns):
  21.     dns_ip=socket.gethostbyname_ex(dns)[2]
  22.     if ip not in dns_ip:
  23.         return "IP: "+ dns_ip[0]
  24.     else:
  25.         return "good"
  26. def port_check(address, port):
  27.     s = socket.socket()
  28.     s.settimeout(1)
  29.     try:
  30.         s.connect((address, port))
  31.         return True
  32.     except:
  33.         return False
  34.  
  35. ip_dict = []
  36. for files in os.listdir('/var/log'):
  37.     if 'auth.log' in files:
  38.         logfile = "/var/log/" + files
  39.         celery = openfile(logfile)
  40.         for line in celery:
  41.             if "Failed" in line:
  42.                 if "pronto" not in line:
  43.                     ip = re.findall(r'[0-9]+(?:\.[0-9]+){3}', line)
  44.                     if ip:
  45.                         if ip[0] not in ignore_ip:
  46.                             try:
  47.                                 index = get_index(ip_dict, 'IP', ip[0])
  48.                                 ip_dict[index]['attempts'] += 1
  49.                                 ip_dict[index]['geo'] = gi.record_by_addr(ip[0])['country_name']
  50.                             except:
  51.                                 try:
  52.                                     host = socket.gethostbyaddr(ip[0])[0]
  53.                                     checker = ipcheck(ip[0], host)
  54.                                     ip_dict.append({"IP": ip[0], "attempts": 1, "RDNS": host, "IPCHECK": checker})
  55.                                 except:
  56.                                     ip_dict.append({"IP": ip[0], "attempts": 1, "RDNS": None, "IPCHECK": None})
  57.  
  58.  
  59. ip_dict.sort(key=operator.itemgetter('attempts'), reverse=True)
  60. old_list=cPickle.load(open("/home/pronto/logs/ssh_fails.dict.old.p","rb"))
  61. for a in ip_dict:
  62.     try:
  63.         index_old=get_index(old_list,'IP',a['IP'])
  64.         index_new=get_index(ip_dict,'IP',a['IP'])
  65.         if ip_dict[index_new]['IP'] == old_list[index_old]['IP']:
  66.             diff = ip_dict[index_new]['attempts']-old_list[index_old]['attempts']
  67.             ip_dict[index_new]['new']=diff
  68.         else:
  69.             ip_dict[index_new]['new']=0
  70.     except:
  71.         ip_dict[index_new]['new']=0
  72.  
  73. for a in ip_dict:
  74.     if a['attempts'] > 99:
  75.         index=get_index(ip_dict,'IP',a['IP'])
  76.         ip_dict[index]['Port80']=port_check(a['IP'],80)
  77.         ip_dict[index]['Port443']=port_check(a['IP'],443)
  78.         ip_dict[index]['Port22']=port_check(a['IP'],22)
  79.  
  80.  
  81.  
  82. cPickle.dump(ip_dict,open("/home/pronto/logs/ssh_fails.dict.p", "wb"))

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}




All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at