# Option: failregex
# Notes.: regex to match the password failures messages in the logfile.
# The host must be matched by a group named "host". The tag "<HOST>" can
# be used for standard IP/hostname matching and is only an alias for
# (?:::f{4,6}:)?(?P<host>\S+)
# Values: TEXT
failregex = ^\w{3,5} \w{1,2} \w{1,2}:\w{1,2}:\w{1,2} \w{1,6} vpopmail.\d{1,6}.: vchkpw-pop3: vpopmail user not found \w{1,30}@\w{1,30}-\w{1,30}.\w{1,6}:<HOST>
# Option: ignoreregex
# Notes.: regex to ignore. If this regex matches, the line is ignored.
# Values: TEXT
ignoreregex =

What I'd like to do is create a jail and rule that would block the IP address for 24 hours.

I tried several things to get it to match. It should be fairly simple.... Look for a new line in the log file - scan down it to find "NOQUEUE: reject: RCPT from" get the <HOST> from inside the [] and ban them for 24 hours.

Python regex just ain't right! Nothing works, including wildcards for all but the pertinent parts.

So what I'd like to see, and you all can keep your heads, is an interface in which I can paste a line of log file, point out the bits I want it to match on and it will write the rule correctly for me.

Having the ability to do the same for the ignore bits would be highly useful as well.

ANSWER Newer versions of fail2ban have fail2ban-regex -D that uses http://debuggex.com to graphically assist in developing filters. Replace <HOST> with (?&.ipv4) for debuggex. If your stuck and its going to be useful for wider community log a bug or use our mailing list. Also reread the python regex documentation

Mulitple Servers in DMZ writing IPTABLE rules to primary firewall

(by Lynn Dobbs)
All Internet traffic enters my internal network through a single firewall/router and is then handed to appropriate servers that are all running fail2ban. That means an ip address I wish to ban (attacking port 22, for instance), must be individually banned on each server. I would much rather ban the attacker at the first firewall. That way none of my servers receive packets from the banned IP. Currently, fail2ban server/client communication is via a unix socket. It would be great to be able to select, via a conf file entry, a tcp/ip socket option so that my DMZed servers could add rules to the IPTABLES of the primary server.

EDIT to ADD: Uff-Dah. After looking even more closely, I see that I can add an "action" set that would handle communication between the individual servers and the main firewall that would then duplicate the iptable commands over there.

Bruteforce from Botnets

(by CorvusCorax)

Hi.
I was recently experimenting with a simple perl script that does roughly the same as fail2ban, to deal with bruteforce attacks on my server. I run into memory issues doe to number of attacks, and searched for alternatives, but fail2ban doesn't meet the requirements due to the following characteristics of nowadays attacks:

Attack characteristics

Connections related to the same attack series (same or similar usernames are tested) come from hundreds of different IPs.

Each attacker IP makes a number of simultaneous connections and login attempts at the same time (usually between 5 and 8)

Each attacker IP does only one such attack, and then will not try again for a timespan reaching from 30 seconds to several hours.

Requirements for Countermeasures

Superb reaction speed: An IP must be blocked fast enough to prevent completing at least some of the several simultaneous login attempts from the same IP.

Low threshold: Even with high reaction time, the server must take countermeasures at the first breakin attempt of an IP, in order to have an impact on the attack success.

User friendlyness: Despite this harsh methods, the server must react in a way not penalizing users that accidently mistype passwords beyond reasonable means.

My approach for implementation:

I used the | (pipe) syntax in /etc/syslogd.conf, to write relevant log information into a fifo, read on the fly by the response script, that way increasing reaction time.

The response script has a whitelist of IPs that have succesfully logged in on the same day - from which failures will be tolerated.

Any login failure from a non whitelisted IP will result in immediate blocking. additionally firewall rules that mimic the old MIRROR iptables target will be put in place, making any further attacks by that IP target the attacking system itself

To not hit the accidental mistyping legitimate user too hard, the first login failure of an IP will only result in a relatively short BAN, however the IP is remembered, and each additional authentication failure (including simultaneous failures occuring before the block - usually of the 5 to 8 I was blocking all but 2 or 3) increases the length of the BAN - increasing in timejumps of something like 30 seconds, 5 minutes, one hour, one day, ...

Blocking IPs for more than one day usually isn't very effective, since most botnet participants are on dynamic/dialin IPs anyway which tend to change on a 24h interval - thus the ban would become useless and worse hit other, innocent users. So the maximum ban time is 24 hours from the last seen breakin attempt.

Squid filter

squid.conf is now included. Don't block on 407 as you'll catch everyone initially needing to authenticate. 403 Forbidden is the response that will happen if authentication is wrong.

Possible extensions

Use iptables "log" feature to react to attempted access during the ban-time and increase the ban time in this case - only unbanning if an IP stays silent long enough

Answer I don't know this actually makes sense. How can you tell if the attempts during the ban time aren't actually using the correct authentication?

It would be cool to see this implemented in fail2ban, especially a configurable escalation strategy with dynamically extending ban times

Answer There is an escalation strategy in the recidive filter.

, and support for piped log fifos for better reaction speed would be a must.

Answer Is this speed actually useful. No reason it couldn't be done. Journals in systemd are providing a better response in 0.9 releases.

Unsorted comments

New Features

Hello, having to manage more than 20 servers a good feature to introduce in the next releases, it could be to allow to modify the email notification's header, setting one common parameter (i.e.<emailheader>=[Machine_name || IP xyz.xyz.yxz.xyz - Fail2ban]) in fail2ban.conf, once for all actions.

This will allow to the webadmins to understand immediately which server is under attack even without having to open the mail message. It is extremely useful if you are receiving alarms on your mobile phone ;)

cheers ^_^

James

ANSWER New mail filters use `uname -n` in the subject line to report hosts.

since 3 years I was noticing unhelpfully, what other little guys was trying to do with my servers. to try to break in. silly usernames, allmostly silly passwords. NOW we can stop this. I'm very keen on intelligently breaking attemps, lets see what "other little guys" can do in future. back to fail2ban. GREAT piece of software. thanx_very_much.

Fail2ban is one of the best projects I've encountered - I love it! One suggestion: in 0.7 , the iptables.conf action uses pre-ban command"

iptables -L....

Is there a reason for this? Maybe ip spoofing? At any rate, this can cause fail2ban to take forever in implementing its actions if the iptables chains are big, because it causes DNS lookups for each entry. I suggest adding the "n" flag to the command, to speed things up, like this:

Seems that upon power failure, Fail2Ban fails to start when the computer reboots because the /tmp/fail2ban.sock file still exists. Perhaps this file could be removed automatically upon boot?

Fail2Ban output (like error messages) is sent to /dev/null when Fail2Ban is started during boot (at least on my Gentoo system), so it would be nice to at least be notified of the issue during bootup. Otherwise, it just says "Failed" during bootup, and it's hard to tell why. Just a thought.

Answer A force option has been added to fail2ban for use in initscripts for exactly this case

=== Additional Actions for large ban tables (spam attacks)

When huge ban tables are formed something a little more efficient than iptables is needed.

Answer This doesn't block it. There are a couple of actions that report failures to various consolidation places.

=== Regexp for vsftp

IP Addresses in Documentation

Hello, reading thru the docs and I just noticed that there are places where one should use 123.123.123.123 as an IP Address for documentation.

Refering to http://tools.ietf.org/html/rfc3330 Section 2, Paragraph 12 please do use the TEST-NET assigned numbers. I think it would save quite a few users from misconfiguring their stuff (Just search for "TEST-NET" on the page and you'll be taken directly to the corresponding paragraph.

192.0.2.0/24 - This block is assigned as "TEST-NET" for use in
documentation and example code. It is often used in conjunction with
domain names example.com or example.net in vendor and protocol
documentation. Addresses within this block should not appear on the
public Internet.

I know this is a bit picky but personally I found that it eases use of documentation (also if you use example.com and example.net domains in documentation rather than some probably not so bogus hostname)

Answer Thank you for the suggestion. I will adapt the documentation. --Lostcontrol 23:12, 18 April 2007 (CEST)

I installed the .80 branch on my fedora clarkconnect box. Unfortunatley clark uses python 2.3, so I had to rpm it to 2.4 I had two python libraries so once i downloaded and untarred the fail2ban source I ran "/usr/bin/python2.4 setup.py install" and everything ran fine, no complaints about @staticmod.
I also note my main problem is errors in my proftpd as I'm being hacked by user "administrator" unknown. proftpd logs to /var/log/secure not /var/log/ftp/proftpd as set in the default configs.
Once I set my email I allready nabbed a china hacker and got an email.
Thanks, I run snort,snortsam, and fail2ban and feel pretty secure.

Could you try a nightly build from trunk? It should work with Python 2.3 which is the minimal requirement for the new development branch. I haven't done a lot of work on this branch yet (CHANGELOG) and your current configuration will work without any modifications. If you try it, please, could you give me feedback (mailing-list or e-mail)? Thank you.
--Lostcontrol 09:52, 29 June 2007 (CEST)

Thanks for this - it works perfectly on RHEL4, which is using Python 2.3. Just a suggestion: you may want to update the README that comes with the package so it doesn't talk about Python >= 2.4 any more.

Created script to check fail2ban configuration

Created this script after I had several misconfigurations across many servers - this will
use values from the config files for testing - a wrapper for fail2ban-regex and eliminating
late-night, low-on-caffeine human errors in testing your config. Could also be used: after an
update to verify the configuration, run weekly cron, create a fail2ban report..

This was written on Redhat+fail2ban 0.8 - with mild changes ought to work for and *nix system

Fail2ban is an incredibly valuable program. It ought to be included in all Redhat/Debian/FreeBSD/*nix distro's

ban for about xx min, config time between break-in attempts?

I don't know that much about the way fail2ban and breaking into systems works so bear with me. Nevertheless here is an idea that struck me: If you implement an algorithm to "humanize" banning time, i.e. that calculates the time when that IP is not longer droped, would that hide some more information from a potential cracker? I'm thinking: If I can see that my IP is blocked for 1 min, for 3 min, for 8 min, but not 10 min after my last unsuccessful try I might be able to guess (using maybe other info, too) that fail2ban is utilized? Or even better if fail2ban extends the period with every time this ip keeps trying? As I said, just brainstorming here :)

It's not surprising that "hostname.domain)" cannot be resolved due to the ending ")". A simple dns = dns.replace(")","") in filter.py solved the problem, but probably it's just a little typo in the filter regexp for pure-ftpd and apache?
Maybe this problem is solved anyway and just apparent in the FC7 rpm?

Regards and thanks for this useful software!

Eike

The above Error also happens on Ubuntu 8,04 with fail2ban 0.8.2 from the ubuntu repositories. So it's not a FC7 related issue, rather an issue if a hostname or ip address is enclosed in brackets "()". I resolved the problem exactly like you did after playing around two hours with the regex and could not find a solution.

I also had to change the logfile entry to /var/log/messages instead of the supplied logfile.

-Doug

I fixed this problem with my pure-ftpd logs by turning off host name lookups in pure-ftpd so I only got ip addresses in my logs. For some ISPs host name look ups will return a generic host name for multiple ip addresses anyway.

Start pure-ftpd with the -H switch or as I did on Ubuntu 'echo yes > /etc/pure-ftpd/conf/DontResolve'

-Blake

Answer Great suggestion. Added this to the filter documentation for pureftpd

Fail2ban is failing to ban VSFTPD bruteforce

A similar issue to those above, in my case with VSFTPD, with unresolvable DNS names from /var/log/secure:

Scenario: VSFTP configuration is set for PAM authentication, using xferlog in standard format. Fail2ban for vsftpd is watching /var/log/secure

Problem: PAM sends failed login information to /var/log/secure, but the remote server's IP address has been replaced by a DNS name. Resulting DNS name does not resolve or does not resolve correctly, thus fail2ban is unable to ban the IP address.

Answer Added to vsftpd filter documentation. It seems vsftpd.log has been the default in the jail.conf for a while.

A few notes

Hello and thanks for the nice script :)

I'm running Gentoo and fail2ban 0.8.2. I noticed that fail2ban didn't use the "POSSIBLE BREAK-IN ATTEMPT"-regexp at all and I think it's because my sshd puts an exclamation mark after the line, like "POSSIBLE BREAK-IN ATTEMPT!".

Answer This regex is nolonger used.

88.191.80.227 had some good ideas and I thought of something else too: a specific bantime for a certain regexp. At least I'm annoyed with the endless "connection from"-reports from sshd and most of them are caused by a bot. It would be nice to have that rule among the other sshd-rules, but with a lesser 'punishment'.

Answer Currently we try to make filters contain a similar class of messages so the same bantime/findtime apply to all regexes there. It just gets a bit too complicated to do bantime per regex. After all if a number of different regexes where used to create a ban which bantime would be used?

Question about persistent IP address bans over restart

Hi, just started using Fail2ban - love the software.

I have the ban time for postfix spamming IP adresses set to 1 month, but when logrotate does its thing from cron.daily I see that it restarts Fail2ban. This seems to remove all currently banned IPs (for all rulesets) and fire out the usual collection of "Stopped...", "Started..." emails.

That works for logrotate, but not for other restarts (e.g. system reboot). A feature to keep track of active bans in some persistent store would be nice to cover all cases.

- Curby

Here is a solution that seems to save and reload banned ip's across fail2ban service restarts (without creating duplicate fail2ban rules in iptables) by modifying a small portion of /etc/init.d/fail2ban:

Permanently Ban Persistent IP Addresses

I would love to see a way for Fail2Ban to permanently ban IPs that receive a temporary ban x number of times. For instance, if an IP attempts to break into SSH 5 times and fails, it gets a temporarily ban of 10 minutes. If that same IP tries and fails again 5 more times after the temporary ban is lifted, it gets another temporary ban. If that same IP cycles through this process 5 times in a span of 6 hours, create a firewall rule that is either permanent or covers a much longer time frame. I don't want to extend the number of minutes for a single ban in case a legit user just fails to log in too many times, but I don't want to receive the Fail2Ban emails for the same IP 300 times before I manually create a new firewall entry permanently blocking that IP.

Is this possible or can this functionality be easily added to Fail2Ban?

Add regex tag for count

I have a couple services who will sit on failed attempts for a little bit, then release them all with something like "Failed login from <HOST>, X times" where X is of course variable. I don't see an easy way to add a new tag to the parser that could be used in a regex to pull that number in like one does with the <HOST> tag, so if someone could tell me where to do it that would be great. If someone wants to send me a diff, that would be even greater! :>
128.112.24.13 18:24, 10 June 2009 (UTC) (domain astro.princeton.edu, user huston)

Python 2.6 support (relevant only for 0.8.3 and older)

fail2ban does not work with Python 2.6.
See http://www.howtoforge.com/forums/showthread.php?t=34886
As, on OpenSUSE 11.1, there is no Python 2.5 (which is recommended in the above source
and which I can confirm to work)
available, I am looking for a fix for the fail2ban-server.

Would it be possible to work out the solution for fail2ban and could someone help me with regex? Because I haven't had any experience with it and so far a couple hours and many different tries to make it work were worthless.
Everything else, like ssh, ssh-ddos works perfectly.
So far this is a perfect tool for stopping mid-knowledge wannabe-hackers.
Great piece of software!

Prevention of duplicate iptables entries

If you do a default fail2ban setup and enable ssh-iptables (for example), fail2ban will write a user defined chain to iptables in memory. So far so good. Now if you did a "service iptables save" or "iptables-save > /etc/sysconfig/iptables" which is the standard way to save iptables it writes the current rules, including the added fail2ban rules, to the file. The next time you reboot the fail2ban rules are added to iptables twice, once by the default 'iptables' config file and again by fail2ban. This is confirmed by doing a "iptables -L".

If you understand iptables and fail2ban it is an easy mistake to avoid but certainly not idiot proof or when you are working on something at 4am on 10 cups of coffee or whatever as a previous contributor mentioned. Is there any way to perhaps put a check into /etc/fail2ban/action.d/iptables.conf to prevent duplicate entries?

Some help on banning bad guys

reviewing the fail2ban logs I keep seeing hundreds of entries like these:

My only recourse is when I see these attacks is to open a command line and just manually kill them one by one with iptables -A INPUT -s 122.70.146.53 -j DROP. So how can I configure my fail2ban to really get rid of these idiots?
This is my jail2.conf for apache-tcpwrapper:

reply- terry@cnysupport.com: You may be using the wrong jail config file. You're banning using tcpwrappers, but apache isn't using tcpwrappers. You would need to switch to an apache jail that uses iptables or reconfigure apache to use tcpwrappers.

ASSP filter for Fail2ban

You can set up fail2ban to monitor the maillog.txt file of the ASSP Anti-SPAM SMTP proxy: ASSP

Regex Tag to modify bantime

(by Paul Gregg)
I am attempting to use ProjectHoneyPot.org http:BL (HTTP blacklist) to identify suspicious/comment spammer/etc visitors to my webserver. I have written a daemon that watches the Apache HTTP server's access_log (parses it out) and checks http:BL API for the visiting IP. If the IP comes back reported as being on the blacklist (excluding search engines which are reported, but obviously not dangerous), I output a record to a new logfile called httpbl_access_log.
I then have fail2ban watch this log and ban any IPs I find in there with:

(so you can see my regex ignores the whitelisted, but reported, entries).

However, and the reason for my posting, I want to take the number after the <HOST> entry and use that as my bantime for this entry. Currently I am reporting http:BL's "threat" score (see 3rd octet in the 127.x.x.x discounted by how old http:BL's entry is, 2nd octet). I am prepared to change this to output raw seconds that I want the ban to be in force for.

I don't think this can be done today - any chance for a <BANTIME> tag?

Thanks,
Paul Gregg

Answer The plan for 0.9 is to make filters be able to do named captures and make these available for the actions.

Bug: banning when PAM is erroring

(by Nate M)

There is a bug wherein fail2ban will ban an IP when the user is not authenticated due to PAM erroring. In this particular case, our server came under a DoS attack which ate up the RAM memory and thus caused errors with PAM ... when I tried to log-in via SSH in order to respond to the attack, I was banned due to the temporary error. This should ideally not happen. As you can see below, the PAM error and "failed password" error occurred during the same timestamp; fail2ban perhaps should check to see that this type of error did not occur at the same time as a "failed password" in order to avoid banning legitimate users.

IP log suggestion

(by Chad Columbus)

I suggest that you add an IP log, this log will allow 2 things:
1. It will allow IP bans to be reinstated on restart Answer 0.9 has persistent bans
2. It will allow IPs to be blocked exponentially longer with each ban. Answer recidive filter

I think #1 is obvious, but what I want with #2 is that when someone is banned for say 10 min, we add that to the log. If they come back and get banned again, this time it is for 20 min, if they return a 3rd time 40 min, and so on. This will eventually block them for days, weeks or years, if they behave badly enough.

Multi-server suggestion

(by Chad Columbus)

I also support the "Mulitple Servers in DMZ writing IPTABLE rules to primary firewall" suggested at the top of this page, I also use a central firewall/load balancer.
I would like to go one further and find a way to allow multiple servers in the same DMZ share their bad his and bans. I think maybe I can do this with a centralized log server, but I would prefer it was build into fail2ban.
Here is what I mean:
I have (as an example) 3 servers for ssh, a bad guy comes in and the load balancer sends the request to server #1, They come back and the request goes to server #2, and again but to server #3. This will effectively give the bad guy 3x the number of chances. The real problem is that I have 40+ servers behind the load balancer.
What I would like is for server #1 to share with server #2 that it got a bad hit, or at the very least when one of the servers bans the IP, it shares the ban with the others, so that they are not getting 5 tries X 40 servers.

If there is a good way to solve this without modifying fail2ban please let me know and I will try it.

Multi-server answers?

You'd just need a few things to accomplish your goal:

Centralized logging

fail2ban running on the centralized log server

and fail2ban able to control your firewall/load balancer

-Scott Miller

Bug: Fail2ban stacks on no whois response

If a whois server does not return a response the Fail2Ban process will just hang because there is not timeout on some version of whois. Can the action.d/sendmail-whois.conf be modified to include a timeout for the whois command. Something like this works on my version.

IPv6

Is Fail2ban protecting me against attackers with an IPv6 address? Are there some changes to do in the configuration files (such as using ip6tables in addition to iptables)? The switch from IPv4 is coming fast (last v4 ranges allocated in June 2011, last IPs around the beginning of 2012, this is only 1 year from now!), so I'd rather be ready…
- Xavier

Count "Last message repeated N times" correctly

It seems Fail2ban undercounts entries from syslog files such as /var/log/syslog and /var/log/auth.log, since it doesn't seem to be aware that syslog may log "last message repeated N times" instead of the full message. For example, if an ssh attack occurs several times in quick succession, there may be only one entry "Failed password for someuser from 1.2.3.4 port 4307 ssh2" followed by "last message repeated 10 times". It should be possible to set a regex that matches these kinds of log entries, and forces the log filter to treat them as if they were N of the previous log entry.

GNU/Linux distributions rsyslog solution:
Tested in Ubuntu 10.04, should also work Centos/RHEL 5.9 or 6.X if rsyslog is used.

1.open /etc/rsyslog.conf
2.find RepeatedMsgReduction and change on to off
3.After that, restart rsyslog and fail2ban

OSX solution:
1. modify /etc/asl.conf by adding this line to the end of it:
= dup_delay 0
2. reboot the machine or restarting syslog with

Bugs running action.d start actions

I'm seeing some startup actions getting errors during the startup of fail2ban,
but I can enter those same commands by hand and they are accepted. All are setting
up the iptables.

Of the 7 servers I have, all are set up fairly identically. Servers 2 and 3 restarted with
no problems; the 5 jails I define all started with no errors. Server 1 had problems with
APACHE-SETUP; server 4 created the vsftp chain, but didn't add the call or the Return.
Server 5 failed to set up anything in iptables for vsftp; server 6 also completely
lost the setup for vsftp; server 7 missed all the commands for the asterisk iptables setup.

I see this kind of thing from the logfile:

(my apologies, I don't know how to quote or force line breaks in the following!)

Is it some timing issue? Is there any way to better debug why the setup commands
are getting errors? See the stuff iptables responded with? It's the inconsistency
of what commands are failing, for what jail, that makes me wonder.

any help appreciated. If I can demo fail2ban, I'll share the usage of "recent" and it's hash tables for banning
huge numbers of IP's, with just a few fixed rules in iptables. Perhaps it could be added to the actions.d dir.

I'm looking at the logs, and I see the 5 jails started, 4-6 milliseconds apart. The error(s) coming within another
4-6 msec. Could it be, that, if they are separate threads, they are dumping their iptables commands in parallel,
is it even possible that an iptables command would be rejected if it could not immediately get a lock?

--Late breaking: I inserted a sleep 1 in front of the allports start action; a sleep 2 in front of another, and a sleep 3 in the iptables.conf;
that left only the iptables-multiport, because 2 rules use the same thing. So, they got the 0 second delay slot.
I restarted my fail2bans one after the other, and got only 1 ERROR message: from the 2 sharing the same slot.
So, if you are running an older version of netfilter (This is on centos, which seems to prefer ancient versions)
and you are getting what seems to be random ERRORS in starting/stopping your jails, then consider throwing in
some sleeps to spread them out. It might be nice if these start actions could be serialized.

A successful resolution is to modify only the relevant action config (in this case iptables-multiport.conf) and insert a random sleep (0.0000 to 2.9999 seconds) before the iptables action, so actionstart becomes:

# Option: failregex
# Notes.: regex to match the password failures messages in the logfile.
# The host must be matched by a group named "host". The tag "<HOST>" can
# be used for standard IP/hostname matching and is only an alias for
# (?:::f{4,6}:)?(?P<host>\S+)
# Values: TEXT
failregex = ^\w{3,5} \w{1,2} \w{1,2}:\w{1,2}:\w{1,2} \w{1,6} vpopmail.\d{1,6}.: vchkpw-pop3: vpopmail user not found \w{1,30}@\w{1,30}-\w{1,30}.\w{1,6}:<HOST>
# Option: ignoreregex
# Notes.: regex to ignore. If this regex matches, the line is ignored.
# Values: TEXT
ignoreregex =