DDOS Protection mit Fail2Ban

Ich betreibe einen Lighttpd Webserver der des öfteren unter DDOS Attacken steht. Dazu kann man mit Fail2Ban IP-Adressen sperren die zuviele Zugriffe in einer bestimmten Zeit erzeugen. Meine Accesslog hat folgende Syntax:

Client-IP blog.bastelfreak.de - [31/May/2012:13:17:01 +0200] "GET /bla HTTP/1.1" 200 6221 "-" "Mozilla/5.0 (Windows NT 5.1; rv:12.0) Gecko/20100101 Firefox/12.0"

Um solche Logs zu filtern benötigt man folgende Filterregel:

[Definition]
failregex = ^ *.*\”(GET|POST).*

Dies wird in eine neue Datei unter /etc/fail2ban/actions.d/blub.conf gespeichert. Danach kann man die eigentliche Regel in Fail2Ban erstellen. Diese wird in die jail.local unter /etc/fail2ban geschrieben:

[blub]
enabled = true
port = http,https
filter = blub
logpath = /home/lighttpd/vhosts/bastelfreak/blog.bastelfreak.de/logs/access_log
maxretry = 50
findtime = 300
bantime = 6000

Damit werden IP-Adressen für 6000 Sekunden gesperrt wenn sie innerhalb 300 Sekunden 50 GETs oder POSTs erzeugen. Je nach Webseite muss man dies natürlich anpassen.

Nach einem service fail2ban restart

ist die Regel aktiv.

 

This entry was posted in General, IT-Security, Linux. Bookmark the permalink.

4 Responses to DDOS Protection mit Fail2Ban

  1. Martin Selbmann says:

    Ich habe den Filterausdruck verwendet, aber wenn ich mit LOIC auf meinen Server ziele, passiert nichts. Nicht mal ein Log-File-Eintrag bei fail2ban.

    jail.conf

    [lighttpd-flood]

    enabled = false
    port = http
    filter = lighttpd-flood
    logpath = /var/log/lighttpd/access.log
    findtime = 60
    maxretry = 120
    bantime = 300

    lighttpd-flood.conf

    # Fail2Ban configuration file
    #
    # Author: Martin Selbmann
    #
    # $Revision$
    #

    [INCLUDES]

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

    [Definition]

    # Option: failregex
    # Notes.: regex to match the password failure messages in the logfile. The
    # host must be matched by a group named "host". The tag "" can
    # be used for standard IP/hostname matching and is only an alias for
    # (?:::f{4,6}:)?(?P[\w\-.^_]+)
    # Values: TEXT
    #

    failregex = ^ *.*\”(GET|POST).*

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

    Eine Log-Zeile:

    1.2.3.4 www.beispieldomain.de - [05/Jun/2014:18:11:01 +0200] "GET /xyz" "Useragent"

  2. Martin Selbmann says:

    regex ist natürlich
    failregex = ^ *.*\”(GET|POST).*
    Das war ein Copy&Paste Fehler.

  3. Martin Selbmann says:

    Auch wenn es langsam wie Spam aussieht, der Blog entfernt das .
    Wollte ich nur mal so anmerken. ;)

  4. bastelfreak says:

    Hi Martin, danke für den Kommentar, ich hab den Regex oben bei mir angepasst

Leave a Reply to Martin Selbmann Cancel reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.