Linuxaria

Everything about Linux
  • Home
  • Articoli
    • News
  • Guide
  • Interviste
  • Pillole
  • Recensioni
  • Information
    • Collabora
    • Contattami
    • Info
    • Privacy Policy
  • Links
    • GMStyle
    • Il Bloggatore
    • Linux
    • Linux Today
    • Linuxfeed
    • LinuxInsight
    • Lxer
    • ZioBudda

Sponsor

Come proteggere Apache con Fail2ban

 Guide  Add comments
Apr 232013
 

Circa 2 anni fa ho scritto un articolo su fail2ban .

Fail2ban è un software per la prevenzione delle intrusioni scritto in Python. E’ in grado di girare su sistemi POSIX che hanno un interfaccia ad un sistema di controllo dei pacchetti o firewall installato localmente (per esempio, iptables o TCP Wrapper).
La funzione principale di Fail2ban è quella di bloccare gli indirizzi IP selezionati che potrebbero appartenere a dei computers che stanno tentando di violare la sicurezza del sistema. Determina i server che devono essere bloccati da un controllo dei file di log (per esempio /var/log/pwdfail, /var/log/auth.log, ecc) e proibisce l’accesso dagli IP degli host che hanno troppi tentativi di accesso indesiderato o esegue altre azioni in un arco di tempo definito dall’amministratore.

Oggi voglio mostrarvi alcune configurazioni che è possibile utilizzare per migliorare la sicurezza del tuo Apache.

L’idea di base è che Fail2ban è in grado di monitorare i file di log di Apache e rilevare che un certo IP sta facendo troppe richieste al server web. Fail2ban può anche rilevare le url richieste, per cui è in grado di dire che un certo client ha tentato di autenticarsi diverse volte e non ci è riuscito, il che probabilmente significa che qualcuno sta cercando di fare un attacco di tipo “brute force” sulle vostre password.

Quindi creeremo qualche regola nel file Jail (il file che contiene la configurazione fail2ban) per trovare questi IP “canaglia” e metterlo in una prigione, il che significa bandire tutte le loro richieste, per qualche tempo, dal vostro server web.

NOTA: ho fatto i miei test su Debian dove il file di configurazione principale è /etc/fail2ban/jail.conf e nella parte superiore di esso vi è una nota importante:

# 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

Quindi, per garantire la compatibilità con qualsiasi aggiornamento lavorerò sul file /etc/fail2ban/jail.local

Prima cosa da fare: abilitare la configurazione standard

Quando viene installato fail2ban ha alcune regole che possono essere utilizzate per mitigare alcuni tipi di attacchi su apache, ho attivato queste 3 “jail” scrivendo queste direttive nel file /etc/fail2ban/jail.local:

[apache]
enabled = true

[apache-noscript]
enabled = true

[apache-overflows]
enabled = true

Jail Apache

Questo Jail utilizza una regex per cercare i messaggi di errore nell’inserimento delle password nel file di log /var/log/apache*/*error.log .
Lo scopo di questa regola è quello di non far più accedere tutti gli IP che corrispondono una di queste regole:

failregex = [[]client []] user .* authentication failure
            [[]client []] user .* not found
            [[]client []] user .* password mismatch

Jail Apache-noscript

Questo Jail utilizza una regex per cercare i messaggi di errore di script non trovati nel file di log /var/log/apache*/*error.log .
Lo scopo di questa regola è quello di non far più accedere tutti gli IP che stanno cercando degli script (php, perl, asp) che non esistono, e questi sono i filtri utilizzati :

failregex = [[]client []] (File does not exist|script not found or unable to stat): /\S*(\.php|\.asp|\.exe|\.pl)
            [[]client []] script '/\S*(\.php|\.asp|\.exe|\.pl)\S*' not found or unable to stat *$

Jail Apache-overflows

Questo Jail utilizza una regexp per cercare tutti i tentativi di overflow verso Apache:

failregex = [[]client []] (Invalid method in request|request failed: URI too long|erroneous characters after protocol string)

Seconda cosa da fare: abilitare qualche configurazione aggiuntiva

Questi sono alcuni esempi di Jail aggiuntivi e filtri che possono essere utilizzati per mitigare alcuni tipi di attacchi:
Jail Apache-phpmyadmin

L’obiettivo di questa configurazione è di vietare tutte le richieste che sono alla ricerca di una installazione phpmyadmin, non usatelo se avete installato phpMyAdmin con un URL che corrisponde al seguente elenco “badadmin” di url da controllare.

Aggiungere nel file /etc/fail2ban/jail.local il seguente blocco:

[apache-phpmyadmin]
enabled  = true
port     = http,https
filter   = apache-phpmyadmin
logpath  = /var/log/apache*/*error.log
maxretry = 3

Ed ora create il file /etc/fail2ban/filter.d/apache-phpmyadmin.conf con il seguente contenuto:

# Fail2Ban configuration file
#
# Bans bots scanning for non-existing phpMyAdmin installations on your webhost.
#
# Author: Gina Haeussge
#
 
[Definition]
 
docroot = /var/www
badadmin = PMA|phpmyadmin|myadmin|mysql|mysqladmin|sqladmin|mypma|admin|xampp|mysqldb|mydb|db|pmadb|phpmyadmin1|phpmyadmin2
 
# Option:  failregex
# Notes.:  Regexp to match often probed and not available phpmyadmin paths.
# Values:  TEXT
#
failregex = [[]client []] File does not exist: %(docroot)s/(?:%(badadmin)s)
 
# Option:  ignoreregex
# Notes.:  regex to ignore. If this regex matches, the line is ignored.
# Values:  TEXT
#
ignoreregex =

Jail Apache-postflood

L'obiettivo di questo Jail è quello di fermare gli attacchi di tipo flood POST verso apache. In questo esempio si blocca qualsiasi client che supera le 10 richieste di POST in 10 secondi.
Aggiungere nel file
/etc/fail2ban/jail.local il seguente blocco:

    [apache-postflood]

    enabled = true
    filter = apache-postflood
    logpath = /var/log/httpd/access_log
    findtime = 10
    maxretry = 10

Ed ora create il file /etc/fail2ban/filter.d/apache-postflood.conf con il seguente contenuto:

    # Fail2Ban configuration file
    #
    #
    # $Revision: 1 $
    #

    [Definition]
    # Option: failregex
    # Notes.: Regexp to catch known spambots and software alike. Please verify
    # that it is your intent to block IPs which were driven by
    # abovementioned bots.
    # Values: TEXT
    #
    failregex = ^ -.*”POST.*

    # Option: ignoreregex
    # Notes.: regex to ignore. If this regex matches, the line is ignored.
    # Values: TEXT
    #
    ignoreregex =

Conclusioni

Questi sono alcuni esempi di configurazioni che si possono fare con fail2ban utilizzando alcuni dei Jail/filtri inclusi nel pacchetto e 2 esempi di nuovi Jail che è possibile utilizzare per mitigare gli attacchi contro il vostro server Web Apache. Non fare affidamento solo su questo livello per la sicurezza del server Web ma consideratelo un buon modo per limitare il numero di richieste "cattive" che arrivano al server, e quindi un modo per evitare situazioni in cui un attacco di forza bruta potrebbe causare un DOS o qualcosa di peggio.

Riferimenti:

Banning phpMyAdmin bots using fail2ban

fail2ban – add apache POST flood rule



Popular Posts:

    None Found

Flattr this!

 Posted by Riccardo at 23:21  Tagged with: apache, apache log files, fail2ban, host ip, intrusion prevention, iptables, login attempts, security, web server

  12 Responses to “Come proteggere Apache con Fail2ban”

  1. Swen says:
    7 September 2013 at 15:51

    Hi, postflood.conf doesnt work for me. It just ignores the rule. Log sais in restart:
    “2013-09-07 16:50:41,262 fail2ban.filter : ERROR No ‘host’ group in ‘^ -.*”POST.*'”

    Rispondi
    • Grega says:
      2 November 2013 at 20:11

      Hi,
      Are you sure that in your /etc/fail2ban/filter.d/apache-post.conf file, failregex line is

      failregex = ^ -.*”POST.*

      (seems like you are missing parameter)

      Rispondi
  2. json says:
    15 December 2013 at 07:51

    Should be this.

    failregex = [[]client []] ^ -.”POST.

    Rispondi
  3. Marc Isaacson says:
    20 December 2013 at 05:33

    The [apache-postflood] was giving me a problem. I needed to add port = http,https.
    Also, I need to change the logpath to /var/log/apache*/access_log

    Rispondi
  4. Marc Isaacson says:
    20 December 2013 at 05:37

    Sorry, I should have said the log file was /var/log/apache*/access.log

    Rispondi
  5. Marc Isaacson says:
    20 December 2013 at 14:58

    It seems that the example filters should be taking advantage of something like

    [INCLUDES]
    # Retrieve the _apache_error_client pattern for our failregex expression
    before = apache-common.conf

    Then, their failregex could be written like

    failregex = ^%(_apache_error_client)s File does not exist: %(docroot)s/(?:%(badadmin)s)

    and

    failregex = ^%(_apache_error_client)s -.*”POST.*

    As things stand, both of them are missing the required host information and neither one will actually do anything.

    Rispondi
  6. Marc Isaacson says:
    21 December 2013 at 14:01

    If you look at the original sources (linked in the References section) you will see that they include a HOST parameter (I have left off the “brackets”). Part of the issue here is that the HOST parameter is not appearing on account of the less than sign and greater than sign. The page is trying to interpret the whole string as some sort of HTML tag.

    Rispondi
  7. Andrey says:
    7 February 2014 at 11:21

    compiltation error with this postflood

    Rispondi
  8. Lars says:
    6 June 2014 at 08:34

    I get an error message from the apache-phpmyadmin.conf with the latest Fail2ban version. I replaced

    failregex = [[]client []] File does not exist: %(docroot)s/(?:%(badadmin)s)

    with

    failregex = [[]client []] File does not exist: .*(PMA|phpmyadmin|phpMyAdmin|myadmin|mysql|mysqladmin|sqladmin|mypma|xampp|mysqldb|mydb|db|pmadb|phpmyadmin1|myadmin2|Joomla*)

    and it works again.

    Rispondi
  9. Efstathios says:
    29 September 2015 at 08:06

    I wonder if fail2ban can do all this.

    Rispondi
  10. Ionut J says:
    26 April 2016 at 14:32

    i have troubleshooted on ubuntu 14 with xampp my fail2ban with kodos -> http://kodos.sourceforge.net/-
    ——

    so, in jail.conf
    some lines
    some lines

    [apache-overflows]

    enabled = true
    port = http,https
    filter = apache-client-denied
    logpath = /opt/lampp/logs/your_file1-error_log
    /opt/lampp/logs/your_file2-error_log
    /opt/lampp/logs/your_file3-error_log

    maxretry = 2
    —————————————-
    in filter.d directory

    file apache-client-denied.conf
    # Fail2Ban apache-auth filter
    #

    [INCLUDES]

    # Read common prefixes. If any customizations available — read them from
    # apache-common.local
    before = apache-common.conf

    [Definition]

    failregex = ^%(_apache_error_client)s AH01630:\sclient denied by server configuration: (uri )?\S*\s*$
    ^%(_apache_error_client)s AH00126:\sInvalid URI in request (uri )?\S*\s*$

    ignoreregex =

    # DEV Notes:
    “apache-client-denied.conf” 43L, 1736C

    ———————————-

    and apache-common.conf ( i have deleted ” : ” from script)

    # Generic configuration items (to be used as interpolations) in other
    # apache filters.

    [INCLUDES]

    # Load customizations if any available
    after = apache-common.local

    [DEFAULT]

    #_apache_error_client = \[[^]]*\] \[(error|\S+:\S+)\]( \[pid \d+:\S+ \d+\])? \[client (:\d{1,5})?\]

    _apache_error_client =\[[^]]*\] \[(error|\S+:\S+)\]( \[pid \d+\S+\d+\])? \[client (:\d{1,5})?\]

    #_apache_error_client = \[\] \[(:?error|\S+:\S+)\] \[client (:\d{1,5})?\]

    # Common prefix for [error] apache messages which also would include
    # Depending on the version it could be

    ———————–

    service fail2ban restart

    Rispondi
  11. Chris JayZ says:
    14 August 2016 at 21:17

    We all know fail2ban.
    If You need something different take a look on https://aipa.elineo.eu

    Rispondi

 Leave a Reply Cancel reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)

CAPTCHA
Refresh

*

  (English) What are the desktop environments that use less resources on Linux ?   (English) Linux shell: Dfc – Check your disk space with style

Language:

  • English
  • Italiano
Facebook

Sponsor

buy tablet pc
ssd vps apk monk

Follow Me

RSS Twitter Facebook Google+

Popular posts

    None Found

Subscribe by Email

Subscribe to RSS» English by mail

Iscriviti agli RSS» Italiano via Email

Recent Comments

  • Ashwin su (English) What You Don’t Know About Linux Open Source Could Be Costing to More Than You Think
  • frann su gEdit, un editor di testo facile da usare con molti funzionalità avanzate
  • greg125 su Il miglior modo per spostare dati
  • Ashwin su (English) Top Five Mobile Devices That Run Linux
  • Ashwin su (English) Paas and continuos integration

RSS My scoop.it feed

© 2013 Linuxaria All site content, except where otherwise noted, is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. Suffusion theme by Sayontan Sinha
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish.Accept Read More
Privacy & Cookies Policy