Installer et configurer fail2ban

Nos tutoriels complets pour vous aider
Répondre
chatonhub
    unknown unknown

Installer et configurer fail2ban

Message par chatonhub »

Fail2ban bloque les adresses IP appartenant à des hôtes qui tentent de casser la sécurité du système.

Il cherche ces adresses en parcourant les journaux système (par ex. : /var/log/pwdfail, /var/log/auth.log, etc.).

Fail2ban est aussi capable de bloquer les attaques distribuées.

Installer et configurer fail2ban


On va d'abord l'installer
apt-get install fail2ban
Quelques modifications, dans :
nano /etc/fail2ban/jail.conf
pour recevoir un mail en cas de ban modifier le cas échéant
mta = mail

en

mta = sendmail

et

action = %(action_)s

en

action = %(action_mw)s


destemail = votre_email@votre_domaine.tld
On ajoute notre IP (si elle est fixe) afin de ne pas se faire bannir (mettre un espace entre chaque IP)
ignoreip = 127.0.0.1/8 votre_ip_fixe
On active les jails pour les services utilisés sur le serveur (mettre true pour activer ou false pour désactiver)

A l'installation on a celles-là de disponibles, on peut en rajouter pour d'autres services :

cd /etc/fail2ban/filter.d
/etc/fail2ban/filter.d# ls -l
-rw-r--r-- 1 root root 767 juin 12 2013 apache-auth.conf
-rw-r--r-- 1 root root 2375 nov. 29 2011 apache-badbots.conf
-rw-r--r-- 1 root root 352 juin 12 2013 apache-common.conf
-rw-r--r-- 1 root root 758 juin 12 2013 apache-nohome.conf
-rw-r--r-- 1 root root 904 juin 12 2013 apache-noscript.conf
-rw-r--r-- 1 root root 580 juin 12 2013 apache-overflows.conf
-rw-r--r-- 1 root root 1217 nov. 29 2011 common.conf
-rw-r--r-- 1 root root 610 nov. 29 2011 courierlogin.conf
-rw-r--r-- 1 root root 585 nov. 29 2011 couriersmtp.conf
-rw-r--r-- 1 root root 1006 nov. 29 2011 cyrus-imap.conf
-rw-r--r-- 1 root root 735 nov. 29 2011 dovecot.conf
-rw-r--r-- 1 root root 1651 nov. 29 2011 dropbear.conf
-rw-r--r-- 1 root root 607 nov. 29 2011 exim.conf
-rw-r--r-- 1 root root 441 nov. 29 2011 gssftpd.conf
-rw-r--r-- 1 root root 397 nov. 29 2011 lighttpd-fastcgi.conf
-rw-r--r-- 1 root root 1007 nov. 29 2011 named-refused.conf
-rw-r--r-- 1 root root 867 nov. 29 2011 pam-generic.conf
-rw-r--r-- 1 root root 867 nov. 29 2011 php-url-fopen.conf
-rw-r--r-- 1 root root 585 nov. 29 2011 postfix.conf
-rw-r--r-- 1 root root 860 nov. 29 2011 proftpd.conf
-rw-r--r-- 1 root root 803 nov. 29 2011 pure-ftpd.conf
-rw-r--r-- 1 root root 600 nov. 29 2011 qmail.conf
-rw-r--r-- 1 root root 677 nov. 29 2011 sasl.conf
-rw-r--r-- 1 root root 575 nov. 29 2011 sieve.conf
-rw-r--r-- 1 root root 1643 nov. 29 2011 sshd.conf
-rw-r--r-- 1 root root 621 nov. 29 2011 sshd-ddos.conf
-rw-r--r-- 1 root root 694 nov. 29 2011 vsftpd.conf
-rw-r--r-- 1 root root 821 nov. 29 2011 webmin-auth.conf
-rw-r--r-- 1 root root 431 nov. 29 2011 wuftpd.conf
-rw-r--r-- 1 root root 842 nov. 29 2011 xinetd-fail.conf
Les principales à activer :
[ssh], [ssh-ddos],
[apache-multiport],
[proftpd] ou [pure-ftpd] ou [wuftpd],
[postfix], [couriersmtp], [courierauth], [dovecot],
[apache-w00tw00t], [apache-bloquescan],
[apache-phpmyadmin]... et autres selon vos besoins



il manque apache-phpmyadmin on va le créer
nano /etc/fail2ban/filter.d/apache-phpmyadmin.conf
Contenu du fichier apache-phpmyadmin.conf
# Fail2Ban configuration file
# Bans bots scanning for non-existing phpMyAdmin installations on your webhost.
#
[Definition]
# Option: failregex
# Notes.: Regexp to match often probed and not available phpmyadmin paths.
# Values: TEXT
#
failregex = [[]client <HOST>[]] File does not exist: .*(PMA|phpmyadmin|myadmin|mysql|mysqladmin|sqladmin|mypma|admin|xampp|mysqldb|mydb|db|pmadb|phpmyadmin1|myadmin2)
# Option: ignoreregex
# Notes.: regex to ignore. If this regex matches, the line is ignored.
# Values: TEXT
#
ignoreregex =
et aussi http-get-dos
nano /etc/fail2ban/filter.d/http-get-dos.conf
Contenu du fichier http-get-dos.conf
# Fail2Ban configuration file
#
# Author: http://www.go2linux.org" onclick="window.open(this.href);return false;
#
[Definition]javascript:void(0);
# Option: failregex
# Note: This regex will match any GET entry in your logs, so basically all valid and not valid entries are$
# You should set up in the jail.conf file, the maxretry and findtime carefully in order to avoid false pos$
failregex = ^ -.*GET
# Option: ignoreregex
# Notes.: regex to ignore. If this regex matches, the line is ignored.
# Values: TEXT
#
ignoreregex =
ainsi que apache-w00tw00t
nano /etc/fail2ban/filter.d/apache-w00tw00t.conf
Contenu du fichier apache-w00tw00t.conf
[Definition]
# Option: failregex
# Notes.: regex to match the w00tw00t scan 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.
# Values: TEXT
failregex = ^<HOST> -.*"GET \/w00tw00t\.at\.ISC\.SANS\.DFind\:\).*".*
# Option: ignoreregex
# Notes.: regex to ignore. If this regex matches, the line is ignored.
# Values: TEXT
ignoreregex =
jail pour wordpress

Code : Tout sélectionner

nano /etc/fail2ban/filter.d/apache-wp-login.conf
Contenu du fichier apache-wp-login.conf
[Definition]
failregex = ^<HOST> -.*POST /wp-login.php HTTP.*
ignoreregex =



Il reste maintenant à configurer les jails, quelques modifications si vous utilisez Virtualmin puisqu'il met les logs des domaines dans un répertoire non standard :
logpath = /var/log/apache*/*error.log
logpath = /var/log/virtualmin/*_error_log


logpath = /var/log/apache*/*access_log
logpath = /var/log/virtualmin/*_access_log
et pour les Release 2 et 3 OVH, les logs Apache sont ici (/var/log a un lien symbolique vers /home/log vous pouvez donc laisser comme ceci) :
logpath = /var/log/httpd/access_log
logpath = /var/log/httpd/*-access_log
logpath = /var/log/httpd/error_log
logpath = /var/log/httpd/*-error_log

j'augmente considérablement le temps de bannissement, et si vous avez désactivé la connexion SSH par root, vous pouvez mettre le nombre de tentatives SSH à 1, idem pour phpmyadmin si vous avez suivi mon conseil et mis dans un répertoire avec un nom personnel
# Fail2Ban configuration file.
# This file was composed for Debian systems from the original one
# provided now under /usr/share/doc/fail2ban/examples/jail.conf
# for additional examples.
# To avoid merges during upgrades DO NOT MODIFY THIS FILE
# and rather provide your changes in /etc/fail2ban/jail.local
# Author: Yaroslav O. Halchenko <debian@onerussian.com>
# $Revision$
# The DEFAULT allows a global definition of the options. They can be overridden
# in each jail afterwards.

[DEFAULT]
# "ignoreip" can be an IP address, a CIDR mask or a DNS host
ignoreip = 127.0.0.1/8
# ajouter votre-ip-fixe si vous en avez une en séparant par un espace : ignoreip = 127.0.0.1/8 votre_ip_fixe
# bannissement d'environ 15 jours par défaut
bantime = 1296369
maxretry = 3

# "backend" specifies the backend used to get files modification. Available
# options are "gamin", "polling" and "auto".
# yoh: For some reason Debian shipped python-gamin didn't work as expected
# This issue left ToDo, so polling is default backend for now
backend = auto

# Destination email address used solely for the interpolations in
# jail.{conf,local} configuration files.
destemail = votre_email

# ACTIONS
#
# Default banning action (e.g. iptables, iptables-new,
# iptables-multiport, shorewall, etc) It is used to define
# action_* variables. Can be overridden globally or per
# section within jail.local file
banaction = iptables-multiport

# email action. Since 0.8.1 upstream fail2ban uses sendmail
# MTA for the mailing. Change mta configuration parameter to mail
# if you want to revert to conventional 'mail'.
mta =sendmail

# Default protocol
protocol = tcp

# Specify chain where jumps would need to be added in iptables-* actions
chain = INPUT

# Action shortcuts. To be used to define action parameter

# The simplest action to take: ban only
action_ = %(banaction)s[name=%(__name__)s, port="%(port)s", protocol="%(protocol)s", chain="%(chain)s"]

# ban & send an e-mail with whois report to the destemail.
action_mw = %(banaction)s[name=%(__name__)s, port="%(port)s", protocol="%(protocol)s", chain="%(chain)s"]
%(mta)s-whois[name=%(__name__)s, dest="%(destemail)s", protocol="%(protocol)s", chain="%(chain)s"]

# ban & send an e-mail with whois report and relevant log lines
# to the destemail.
action_mwl = %(banaction)s[name=%(__name__)s, port="%(port)s", protocol="%(protocol)s", chain="%(chain)s"]
%(mta)s-whois-lines[name=%(__name__)s, dest="%(destemail)s", logpath=%(logpath)s, chain="%(chain)s"]

# Choose default action. To change, just override value of 'action' with the
# interpolation to the chosen action shortcut (e.g. action_mw, action_mwl, etc) in jail.local
# globally (section [DEFAULT]) or per specific section
# action = %(action_)s
action = %(action_mw)s
#
# JAILS
#
# Next jails corresponds to the standard configuration in Fail2ban 0.6 which
# was shipped in Debian. Enable any defined here jail by including
#
# [SECTION_NAME]
# enabled = true
#
# in /etc/fail2ban/jail.local.
#
# Optionally you may override any other parameter (e.g. banaction,
# action, port, logpath, etc) in that section within jail.local

[ssh]
enabled = true
port = ssh
filter = sshd
logpath = /var/log/auth.log
maxretry = 1

[dropbear]
enabled = false
port = ssh
filter = sshd
logpath = /var/log/dropbear
maxretry = 3

# Generic filter for pam. Has to be used with action which bans all ports
# such as iptables-allports, shorewall

[pam-generic]
enabled = false
# pam-generic filter can be customized to monitor specific subset of 'tty's
filter = pam-generic
# port actually must be irrelevant but lets leave it all for some possible uses
port = all
banaction = iptables-allports
port = anyport
logpath = /var/log/auth.log
maxretry = 6

[xinetd-fail]
enabled = true
filter = xinetd-fail
port = all
banaction = iptables-multiport-log
logpath = /var/log/daemon.log
maxretry = 2

[ssh-ddos]
enabled = true
port = ssh
filter = sshd-ddos
logpath = /var/log/auth.log
maxretry = 6

#
# HTTP servers
#
[apache]
enabled = false
port = http,https
filter = apache-auth
logpath = /var/log/apache*/*error.log
logpath = /var/log/virtualmin/*_error_log
maxretry = 6

# default action is now multiport, so apache-multiport jail was left
# for compatibility with previous (<0.7.6-2) releases

[apache-multiport]
enabled = true
port = http,https
filter = apache-auth
logpath = /var/log/apache*/*error.log
logpath = /var/log/virtualmin/*_error_log
maxretry = 6

[apache-noscript]
enabled = false
port = http,https
filter = apache-noscript
logpath = /var/log/apache*/*error.log
logpath = /var/log/virtualmin/*_error_log
maxretry = 6

[apache-overflows]
enabled = false
port = http,https
filter = apache-overflows
logpath = /var/log/apache*/*error.log
logpath = /var/log/virtualmin/*_error_log
maxretry = 2

#
# FTP servers
#
[vsftpd]
enabled = false
port = ftp,ftp-data,ftps,ftps-data
filter = vsftpd
logpath = /var/log/vsftpd.log
# or overwrite it in jails.local to be
# logpath = /var/log/auth.log
# if you want to rely on PAM failed login attempts
# vsftpd's failregex should match both of those formats
maxretry = 6

[proftpd]
enabled = true
port = ftp,ftp-data,ftps,ftps-data
filter = proftpd
logpath = /var/log/proftpd/proftpd.log
maxretry = 6

[pure-ftpd]
enabled = false
port = ftp,ftp-data,ftps,ftps-data
filter = pure-ftpd
logpath = /var/log/auth.log
maxretry = 6

[wuftpd]
enabled = false
port = ftp,ftp-data,ftps,ftps-data
filter = wuftpd
logpath = /var/log/auth.log
maxretry = 6

#
# Mail servers
#

[postfix]
enabled = true
port = smtp,ssmtp
filter = postfix
logpath = /var/log/mail.log

[couriersmtp]
enabled = true
port = smtp,ssmtp
filter = couriersmtp
logpath = /var/log/mail.log

#
# Mail servers authenticators: might be used for smtp,ftp,imap servers, so
# all relevant ports get banned
#

[courierauth]
enabled = true
port = smtp,ssmtp,imap2,imap3,imaps,pop3,pop3s
filter = courierlogin
logpath = /var/log/mail.log

[sasl]
enabled = true
port = smtp,ssmtp,imap2,imap3,imaps,pop3,pop3s
filter = sasl
# You might consider monitoring /var/log/mail.warn instead if you are
# running postfix since it would provide the same log lines at the
# "warn" level but overall at the smaller filesize.
logpath = /var/log/mail.log

[dovecot]
enabled = true
port = smtp,ssmtp,imap2,imap3,imaps,pop3,pop3s
filter = dovecot
logpath = /var/log/mail.log

# DNS Servers
# These jails block attacks against named (bind9). By default, logging is off
# with bind9 installation. You will need something like this:
# logging {
# channel security_file {
# file "/var/log/named/security.log" versions 3 size 30m;
# severity dynamic;
# print-time yes;
# };
# category security {
# security_file;
# };
# };
#
# in your named.conf to provide proper logging
# !!! WARNING !!!
# Since UDP is connection-less protocol, spoofing of IP and imitation
# of illegal actions is way too simple. Thus enabling of this filter
# might provide an easy way for implementing a DoS against a chosen
# victim. See
# http://nion.modprobe.de/blog/archives/6 ... -fail.html" onclick="window.open(this.href);return false;
# Please DO NOT USE this jail unless you know what you are doing.
#[named-refused-udp]
#enabled = false
#port = domain,953
#protocol = udp
#filter = named-refused
#logpath = /var/log/named/security.log

[named-refused-tcp]
enabled = false
port = domain,953
protocol = tcp
filter = named-refused
logpath = /var/log/named/security.log

[http-get-dos]
enabled = true
port = http,https
filter = http-get-dos
logpath = /var/log/apache*/*.*
logpath = /var/log/virtualmin/*.*
maxretry = 360
findtime = 120
ignoreip = 168.192.0.1 # ajouter votre_ip si elle est fixe
bantime = 259200
action = iptables-allports

# Ajouter les definitions si vous ne l'avez pas fait a l'etape precedente
[apache-phpmyadmin]
enabled = true
port = http,https
filter = apache-phpmyadmin
logpath = /var/log/apache*/*error.log
logpath = /var/log/virtualmin/*_error_log
maxretry = 3

[apache-w00tw00t]
enabled = true
filter = apache-w00tw00t
port = all
banaction = iptables-allports
port = anyport
logpath = /var/log/apache*/access.log
logpath = /var/log/virtualmin/*_access_log
maxretry = 1
bantime = 864000

[apache-wp-login]
# activer si vous utilisez wordpress
enabled = false
port = http,https
filter = apache-wp-login
logpath = /var/log/apache*/access.log
/var/log/virtualmin/*_access.log
maxretry = 3

Une fois configuré on le lance :
/etc/init.d/fail2ban start
et si récalcitrant :
fail2ban-client -x start

Quand on est CERTAIN qu'il fonctionne bien on peut l'ajouter au reboot :
update-rc.d fail2ban defaults
pour le retirer du boot :
update-rc.d -f fail2ban remove

source
Avatar du membre
palerider
Administrateur du site
Administrateur du site
Messages : 6029
Enregistré le : lun. 21 mars 2011 09:18
Localisation : Vaucluse
A remercié : 46 fois
A été remercié : 238 fois
Contact :
    unknown unknown

Re: Installer et configurer fail2ban

Message par palerider »

Merci pour le tuto,

Je vais le tester :)
J'aime les gens qui, pour briller, n'éteignent pas les autres.

Image
chatonhub
    unknown unknown

Re: Installer et configurer fail2ban

Message par chatonhub »

bonjour,
fonctionnel chez moi sur deux serveur ^^
Répondre