System: Monitoring the fail2ban log

1. Format of the Logfile

At the simplest logging level, entries will appear in /var/log/fail2ban.log as follows (fail2ban version 0.8.3):

... 2006-02-13 15:52:30,388 fail2ban.actions: WARNING [sendmail] Ban XXX.66.82.116 2006-02-13 15:59:29,295 fail2ban.actions: WARNING [sendmail] Ban XXX.27.118.100 2006-02-13 16:07:31,183 fail2ban.actions: WARNING [sendmail] Unban XXX.66.82.116 2006-02-13 16:14:29,530 fail2ban.actions: WARNING [sendmail] Unban XXX.27.118.100 2006-02-13 16:56:27,086 fail2ban.actions: WARNING [ssh] Ban XXX.136.60.164 2006-02-13 17:11:27,833 fail2ban.actions: WARNING [ssh] Unban XXX.136.60.164

This is all very interesting, but what if you want to see a summary report so that you can try to identify IP addresses that regularly trigger Fail2Ban – so that you can send a report to their ISP or block them using a firewall script for example?

2. Generating Simple Reports

All of the following commands can be run at the command-line or via a script. They are written for Linux/UNIX systems but may work on other platforms.

Grouping by IP address:

awk '($(NF-1) = /Ban/){print $NF}' /var/log/fail2ban.log | sort | uniq -c | sort -n

Note: the variable NF equals the number of fields in each row of the logfile. So $NF is the value of the last field.

Sample output:

... 4 XXX.124.81.130 5 XXX.248.175.246 8 XXX.29.45.142

Remember that each time an IP address gets banned it’s because they’ve been caught at least maxfailure times, so a total of 8 represents maybe 30 matches in the relevant logfile. Once they reach 10-20 you might consider them as candidates for reporting, or a more permanent solution (see below).

To run this report for all logfiles only a slight change is needed:

zgrep -h "Ban " /var/log/fail2ban.log* | awk '{print $NF}' | sort | uniq -c

or, even better, we can truncate the IP addresses to identify the most problematic subnets:

zgrep -h "Ban " /var/log/fail2ban.log* | awk '{print $NF}' | awk -F\. '{print $1"."$2"."}' | sort | uniq -c | sort -n | tail

This is the best report for identifying problem subnets. The output will be the first two bytes of the most ‘caught’ subnets:

... 75 83.110. 90 219.95. 154 210.213.

Let’s take the last one on the list (highlighted) and see what it’s been up to:

zgrep -c 210.213. /var/log/fail2ban.log*

The output shows how many times those numbers appear in each logfile:

fail2ban.log:39 fail2ban.log.1.gz:129 fail2ban.log.2.gz:55 fail2ban.log.3.gz:78 fail2ban.log.4.gz:22

and which specific IP addresses are involved:

zcat /var/log/fail2ban.log* | awk '(NF == 6 && $NF ~ /^210\.213\./){print $NF}' | sort | uniq -c

The output of this will be a list of the IP addresses starting with 210.213. If they look like they’re part of a subnet (or multiple subnets) you can copy the lowest and highest numbers in our Subnet Calculator to give you the subnet code which you can then add to your firewall rules (see below for details).

Grouping by IP address and Hostname:

The command for including hostnames in the list is a bit more complicated. You also need to insert the correct path for the logresolve program which converts IP addresses to hostnames (the path may be something like /usr/sbin/logresolve but it varies between systems):

awk '($(NF-1) = /Ban/){print $NF,"("$NF")"}' /var/log/fail2ban.log | sort | logresolve | uniq -c | sort -n

Note: The logresolve command can take some time, especially if there are a lot of IP addresses to be processed.

The output is similar to what we’ve seen previously, but also includes the hostname which makes it easier to identify the ISP and/or country of origin and to see which entries might be related:

... 4 (XXX.83.169.221) 5 XXX.248.175.246 (XXX.248.175.246) 8 (XXX.29.45.142)

You can of course just run host, dig, nslookup or logresolve manually on the addresses that you want to identify.

Group by IP address and Fail2Ban section:

grep "Ban " /var/log/fail2ban.log | awk -F[\ \:] '{print $10,$8}' | sort | uniq -c | sort -n

This shows us which services each IP address has been trying to access/exploit:

... 4 XXX.124.81.130 [sendmail] 5 XXX.248.175.246 [sendmail] 8 XXX.29.45.142 [sendmail]

Now you know which logfiles to look in to see what they were doing to get banned. In this case it’s most likely passing forged mail headers to sendmail which you can see in /var/log/mail/mail.log (or the relevant file on your system).

Reporting on ‘today’s activity:

Here’s a report I find useful to run before midnight each day to generate a summary of the day’s activity:

grep "Ban " /var/log/fail2ban.log | grep `date +%Y-%m-%d` | awk '{print $NF}' | sort | awk '{print $1,"("$1")"}' | logresolve | uniq -c | sort -n

The output will be the same as the second report above, but limited to just today’s activity rather than the whole logfile.

Grouping by Date and Fail2Ban section

This report scans all fail2ban logfiles and gives you a summary of how many ban events there were for each section on each day:

zgrep -h "Ban " /var/log/fail2ban.log* | awk '{print $5,$1}' | sort | uniq -c

This can give you an idea of longer-term trends and the effectiveness of your firewall rules. This method of examining all logfiles rather than just the current one can also be applied to most of the reports above.

3. Banning an IP block or subnet

If it turns out that a significant portion of ‘unwanted’ traffic comes from a single ISP then you should try sending an email to their abuse address, but don’t be too hopeful of getting a response. If the abuse continues then it’s time to get strict.

First have a look at the different IP addresses that are being caught. See if you can identify which ones come from the same subnet. The whois reports often include this information, otherwise you can use our Subnet Calculator to help you along – just paste the lowest and highest addresses into the form and it will give you the smallest subnet that covers them both.

Once you have this value (in the form XXX.XXX.XXX.XXX/XX) you can add a firewall rule using iptables to block them from the server completely, or just from the port they’re abusing. For a single address you don’t need to worry about subnets and the address can be used directly.

Block a subnet from accessing SSH:

iptables -I INPUT -p tcp -s XXX.XXX.XXX.XXX/XX --dport ssh -j REJECT --reject-with tcp-reset

Block a subnet from accessing SMTP (mail):

iptables -I INPUT -p tcp -s XXX.XXX.XXX.XXX/XX --dport smtp -j REJECT --reject-with tcp-reset

Block an IP address from HTTP:

iptables -I INPUT -p tcp -s XXX.XXX.XXX.XXX --dport http -j REJECT

Block an IP address from FTP (using DROP):

iptables -I INPUT -p tcp -s XXX.XXX.XXX.XXX --dport ftp -j DROP

and so on for other services.

In the FTP example we’ve used the DROP policy instead of REJECT as that causes the connection to hang for a longer time rather than giving an instant notification that they’ve been rejected.

These rules will be added to the start of your firewall. You can also use -A (append) instead of -I (insert) to specify the end of the chain, or include a rule number to insert them into the middle of a chain. The command for removing a rule is identical, just with -D in place of -I, or again, you can specify the chain and line number.

To see what effect these rules are having – the number of packets and bytes being blocked by each rule – use the following command and look at the values in the first two columns.

iptables -vnL INPUT --line-numbers

At some point (hopefully) the source computer will be ‘fixed’ or in any case stop abusing your server. You should then remove the firewall rules.

4. Monitoring the fail2ban log with fail2ban 0.8

This is something I’ve been meaning to investigate for some time now, and there have been a number of request for this ability. Can we use fail2ban to block for a longer time (even permanently) addresses when they’ve been blocked a number of times by the normal fail2ban filter.

It seems that it is possible, though you may have to set up different jails for different ports. For example, for repeat offenders according to the sendmail filter, add the following to /etc/fail2ban/jail.local:

[fail2ban-smtp] enabled = true port = smtp filter = fail2ban-smtp logpath = /var/log/fail2ban.log maxretry = 3 findtime = 21600 bantime = 86400

And then create a file /etc/fail2ban/filter.d/fail2ban-smtp.conf with the following:

failregex = \[sendmail\] Ban <HOST> ignoreregex =

Finally start the new jail:

# fail2ban-client add fail2ban-smtp # fail2ban-client start fail2ban-smtp

With these settings, fail2ban will monitor it’s own logfile and if a HOST is banned three times (maxretry) in six hours (findtime) they will incur a new ban lasting a full 24 hours (bantime). If you set the bantime value as negative then the HOST in question will never be unbanned.

Similar rules can be set up for other existing jails, and they can be combined if they share the same port. Let us know though the Feedback form below if you have any questions or comments about using it on your server.

5. Test new filters using fail2ban-regex

Whenever you add or change a filter you will want to test that the regular expressions are correct by running it over an existing logfile.

The tool for doing this is fail2ban-regex which is used as follows:

fail2ban-regex /var/log/fail2ban.log /etc/fail2ban/filter.d/fail2ban-smtp.conf

The first argument is the logfile to be scanned and the second argument the jail configuration file containing failregex.

The output lists first all the regular expressions that are being used followed by a tally of how many matches there are for each one. This should match what you can find manually in the logfile using grep or awk. Finally, a list of the ‘caught’ IP addresses is displayed.

Results ======= Failregex |- Regular expressions: | [1] \[sendmail\] Ban | `- Number of matches: [1] 46 match(es) ...

If nothing is being matched, or everything is being matched that may suggest a problem with the regexp. Otherwise, if everything looks ok, you can start the new jail as described above.