Merge branch 'enh/fakegooglebot' of https://github.com/yarikoptic/fail2ban into yarikoptic-enh/fakegooglebot

Conflicts:
	config/filter.d/ignorecommands/apache-fakegooglebot
pull/940/head
Lee Clemens 2015-02-02 13:01:23 -05:00
commit 841c476045
2 changed files with 39 additions and 20 deletions

View File

@ -1,21 +1,32 @@
#!/bin/sh #!/usr/bin/python
# Based on: https://isc.sans.edu/forums/diary/When+Google+isnt+Google/15968/ # Inspired by https://isc.sans.edu/forums/diary/When+Google+isnt+Google/15968/
#
# Written in Python to reuse built-in Python batteries and not depend on
# presence of host and cut commands
#
import sys
if [ "$#" -ne 1 ]; then def process_args(argv):
echo "Unexpected number of arguments: $#" if len(argv) != 2:
exit 1 sys.stderr.write("Please provide a single IP as an argument. Got: %s\n"
else % (argv[1:]))
b="$1" sys.exit(2)
h=$(host ${b})
if echo ${h} | grep -e ' crawl-.*\.googlebot\.com\.$'; then ip = argv[1]
h=$(echo ${h} | cut -f5 -d' ')
n=$(host ${h} | cut -f4 -d' ') from fail2ban.server.filter import DNSUtils
if [ "${n}" = "${b}" ] ; then if not DNSUtils.isValidIP(ip):
exit 0 sys.stderr.write("Argument must be a single valid IP. Got: %s\n"
else % ip)
exit 1 sys.exit(3)
fi return ip
else
exit 1 def is_googlebot(ip):
fi import re
fi from fail2ban.server.filter import DNSUtils
host = DNSUtils.ipToName(ip)
sys.exit(0 if (host and re.match('crawl-.*\.googlebot\.com', host)) else 1)
if __name__ == '__main__':
is_googlebot(process_args(sys.argv))

View File

@ -852,6 +852,14 @@ class DNSUtils:
% (dns, e)) % (dns, e))
return list() return list()
@staticmethod
def ipToName(ip):
try:
return socket.gethostbyaddr(ip)[0]
except socket.error, e:
logSys.debug("Unable to find a name for the IP %s: %s" % (ip, e))
return None
@staticmethod @staticmethod
def searchIP(text): def searchIP(text):
""" Search if an IP address if directly available and return """ Search if an IP address if directly available and return