Fail2Ban
From KevinWiki
m |
|||
Line 8: | Line 8: | ||
Check if it is already installed. If it is not installed yet, install it first. | Check if it is already installed. If it is not installed yet, install it first. | ||
<pre> | <pre> | ||
- | $ sudo apt-get install iptables | + | $ sudo apt-get install iptables |
</pre> | </pre> | ||
Line 14: | Line 14: | ||
Fail2ban also uses <code>sendmail</code> mail transfer agent (MTA) to send an email in order to report the failure of login. This can be optional, yet it is better to have the report thus installing <code>sendmail</code> is recommended. | Fail2ban also uses <code>sendmail</code> mail transfer agent (MTA) to send an email in order to report the failure of login. This can be optional, yet it is better to have the report thus installing <code>sendmail</code> is recommended. | ||
<pre> | <pre> | ||
- | $ sudo apt-get install sendmail | + | $ sudo apt-get install sendmail |
</pre> | </pre> | ||
Line 20: | Line 20: | ||
== Installation == | == Installation == | ||
<pre> | <pre> | ||
- | $ sudo apt-get install fail2ban | + | $ sudo apt-get install fail2ban |
</pre> | </pre> | ||
Line 27: | Line 27: | ||
It has a default configuration file which is <code>jail.conf</code> in the <code>/etc/fail2ban</code> directory. However, using <code>/etc/fail2ban/jail.local</code> file is encouraged. If there is no such a file, create one and copy the contents of the <code>jail.conf</code> file. Or just simply copy <code>/etc/fail2ban/jail.conf</code> file to <code>/etc/fail2ban/jail.local</code>. | It has a default configuration file which is <code>jail.conf</code> in the <code>/etc/fail2ban</code> directory. However, using <code>/etc/fail2ban/jail.local</code> file is encouraged. If there is no such a file, create one and copy the contents of the <code>jail.conf</code> file. Or just simply copy <code>/etc/fail2ban/jail.conf</code> file to <code>/etc/fail2ban/jail.local</code>. | ||
<pre> | <pre> | ||
- | $ sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local | + | $ sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local |
</pre> | </pre> | ||
Line 74: | Line 74: | ||
=== Restart Fail2Ban === | === Restart Fail2Ban === | ||
<pre> | <pre> | ||
- | $ sudo /etc/init.d/fail2ban restart | + | $ sudo /etc/init.d/fail2ban restart |
</pre> | </pre> | ||
Latest revision as of 06:27, 3 April 2009
Contents |
Fail2Ban
Fail2Ban is an intrusion prevention framework written in Python (programming language). It checks log files and uses firewall such as iptables and TCP Wrapper to ban IP which makes too many login failure.
Prerequisite
Iptables
As mentioned, it uses iptables
so iptables
(or other applicable firewall applications) should be installed beforehand.
Check if it is already installed. If it is not installed yet, install it first.
$ sudo apt-get install iptables
Sendmail
Fail2ban also uses sendmail
mail transfer agent (MTA) to send an email in order to report the failure of login. This can be optional, yet it is better to have the report thus installing sendmail
is recommended.
$ sudo apt-get install sendmail
Installation
$ sudo apt-get install fail2ban
Usage
Create Configuration File (jail.local
)
It has a default configuration file which is jail.conf
in the /etc/fail2ban
directory. However, using /etc/fail2ban/jail.local
file is encouraged. If there is no such a file, create one and copy the contents of the jail.conf
file. Or just simply copy /etc/fail2ban/jail.conf
file to /etc/fail2ban/jail.local
.
$ sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
Change jail.local
File
Find [ssh]
section and change like:
[ssh] enabled = true # port = ssh port = ssh,sftp filter = sshd logpath = /var/log/auth.log maxretry = 3 action = iptables[name=SSH, port=ssh, protocol=tcp] sendmail-whois[name=SSH, dest=youremail@mail.com]
*port = ssh
is comment out yet ssh
and sftp
are set instead.
- It will email you when it detects the failure of the login attempt which happened more than
maxretry
, 3 in this example, after banning the IP, from which the attempt of the access is. - If you changed the port number used for sshd, then change
action = iptables[name=SSH, port=ssh, protocol=tcp]
to
action = iptables[name=SSH, port=<port number>, protocol=tcp]
e.g.) action = iptables[name=SSH, port=1234, protocol=tcp]
How long the IP is banned can be found from the [DEFAULT]
section
[DEFAULT] # "ignoreip" can be an IP address, a CIDR mask or a DNS host ignoreip = 127.0.0.1 bantime = 600 maxretry = 3
Change it as you wish (it is seconds).
Restart Fail2Ban
$ sudo /etc/init.d/fail2ban restart
Bug
There is a known bug in fail2ban from Ubuntu (8.04) repository. The bug is that fail2ban is not started after rebooting. The bug was reported here.
This can be solved by adding the line below to /etc/init.d/fail2ban
file.
[ -d /var/run/fail2ban ] || mkdir -p /var/run/fail2ban
So it should be like this.
DAEMON_ARGS="$DAEMON_ARGS -x" fi # Assure that /var/run/fail2ban exists. This line is added. [ -d /var/run/fail2ban ] || mkdir -p /var/run/fail2ban start-stop-daemon --start --quiet --chuid root --exec $DAEMON -- \