HOWTO use geoiplookup

From Fail2ban
Revision as of 03:02, 22 May 2010 by Jensck (Talk | contribs) (Requirements)

Jump to: navigation, search

Geolocalization of banned IPs

You may be interested in a quick summary of the countries where the attacks come from. This document explains how to find these information.


  • Geoiplookup - (Homepage: [1] -- this probably isn't what you want though -- see below for info on distro packages)
  • Python (you should already have that...)

In Gentoo, the needed package is the following :

     Latest version available: 1.3.14
     Latest version installed: [ Not Installed ]
     Size of downloaded files: 1,984 kB
     Description: easily lookup countries by IP addresses, even when Reverse DNS entries don't exist
     License:     GPL-2

This will install "geoiplookup" and "geoipupdate" to update the database (you need a license id to get a new db)

In Debian or Ubuntu, one can simple do apt-get install geoip-bin

In Fedora, you can install with this command: pkcon install python-GeoIP GeoIP (You'll need the python-GeoIP package for the script below, so don't omit it. I don't know why our gentoo and debian/ubuntu friends didn't mention that in their instructions.)


This small script will extract the banned IPs from fail2ban.log. It looks for lines such as "..... Ban", extracts the IP and runs geoiplookup. You may have to change the hardcoded paths in the script depending on your configuration.

import os
import re
f = open('/var/log/fail2ban.log', 'r')    
pattern = r".*?Ban\s*?((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))$"
p = re.compile(pattern)
for i in f:
        m = p.match(i)
        if m:
                ip =
                file = os.popen('geoiplookup %s' % ip)

Note that there is a Geo-Ip binding for Python available.


myserver # python
GeoIP Country Edition: CI, Cote D'Ivoire
GeoIP Country Edition: FR, France
GeoIP Country Edition: CN, China
GeoIP Country Edition: KO, South Korea
GeoIP Country Edition: VN, Vietnam


You can also change the fail2ban script to write the country code to the log file whenever a ban occurs. Make sure you install geoiplookup, then edit the file /usr/share/fail2ban/server/ and change line 31 to read

import time, logging, commands

and change line 139 in the __checkBan(self) function from

logSys.warn("[%s] Ban %s" % (self.jail.getName(), aInfo["ip"])

to (changes are in bold)

logSys.warn("[%s] Ban %s %s" % (self.jail.getName(), aInfo["ip"], commands.getstatusoutput('geoiplookup ' + aInfo["ip"])[1][23:]))

This will log output such as

2008-04-27 20:18:03,109 fail2ban.actions: WARNING [ssh] Ban US, United States

If you only want the two character country code, change the line to

logSys.warn("[%s] Ban %s %s" % (self.jail.getName(), aInfo["ip"], commands.getstatusoutput('geoiplookup ' + aInfo["ip"])[1][23:25]))

Other interesting links

For advanced results, you may be interested in :

Updated GeoIP database

As the geoiplookup database will be pretty outdated (the current included version are from 20060501 in stable debian) you might want to update it regularly as IP assignment changes. One way of doing that is to use a crontab entry that downloads the updated version from maxmind and untar's it to the correct position. I use something like this:

wget -q -O - |gunzip > /usr/share/GeoIP/ && mv /usr/share/GeoIP/ /usr/share/GeoIP/GeoIP.dat

And in /etc/crontab it could look like this:

@monthly root sleep $[$RANDOM/1024]; wget -q -O - |gunzip > /usr/share/GeoIP/ && mv /usr/share/GeoIP/ /usr/share/GeoIP/GeoIP.dat

The above command means:

@montly = do it every month (duh)
root = as user root
sleep $[$RANDOM/1024]; = sleep for a random time (so all you guys don't DDoS maxminds server every month at the same time)
wget ... | gunzip ... && ... = wget and pipe to gunzip, if that succeeds, move the file to the correct place