Guest User

Untitled

a guest
Feb 18th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. #!/usr/bin/python
  2. import os
  3. import socket
  4.  
  5. bannedHosts = []
  6. failedHosts = []
  7. ignoredIPs = []
  8. failLimit = 0 # Better change this
  9. bannedChain = "Hackor"
  10.  
  11. os.system("iptables -N %s &>/dev/null" % (bannedChain))
  12. iptables = os.popen("iptables --list %s -n | egrep -v \"target.+prot.+opt.+source.+destination\" | egrep -v \"Chain %s .+ references\" | awk '{print $4}'" % (bannedChain, bannedChain))
  13. for bannedIP in iptables.readlines():
  14. bannedHosts.append(bannedIP.rstrip())
  15.  
  16. loginFailures = os.popen("grep failure /var/log/secure | grep pam | awk -F \"rhost=\" '{print $2}' | awk '{print $1}' | uniq --count")
  17. for line in loginFailures.readlines():
  18. (number, host) = line.split()
  19. try:
  20. ip = socket.gethostbyaddr(host)[2][0]
  21. except socket.error:
  22. ip = host
  23.  
  24. if int(number) > failLimit:
  25. print "%s is currently over fail limit, processing" % (ip)
  26.  
  27. if ip in bannedHosts:
  28. print "%s is allready banned, ignoring" % (ip)
  29. continue
  30.  
  31. if ip in ignoredIPs:
  32. print "%s is an ingored ip, ignoring" % (ip)
  33. continue
  34.  
  35. print "%s not allready banned, banning for %s failed attempts" % (host, number)
  36. os.system("iptables -A %s -s %s -j DROP" % (bannedChain, ip))
  37. else:
  38. print "%s is currently under fail limit, ignoring" % (host)
Add Comment
Please, Sign In to add comment