Automatischer Schutz vor Script-Kids
Die meisten Angriffe auf Server werden nicht von „echten“ Hackern ausgeführt, sonder meist sind Scripte dafür verantwortlich. Diese Progrämmchen suchen sich wahllos IP-Adressen und befeuern diese mit bekannten Hacks oder versuchen sich via Brut-Force anzumelden.
Mit den richtigen Tools bleiben diese Angriffe nicht unentdeckt.
fail2ban
Was ist Fail2Ban?
Fail2Ban durchsucht Logdateien wie /var/log/pwdfail oder /var/log/apache/error_log und blockt IP-Adressen, die zu viele fehlgeschlagene Loginversuche haben. Es aktualisiert Firewallregeln, um diese IP-Adressen zu sperren. Fail2Ban kann mehrere Logfiles lesen - beispielsweise die von sshd oder apache. Die IP-Adressen werden nach einer vorher festgelegten Zeitspanne wieder aktiviert.
Die Installation ist mit den richtigen Repositories einfach:
yum install fail2ban
Die Konfiguration ist ein wenig umfangreicher, aber zum Glück in weiten Bereichen schon sauber vorkonfiguriert. Die Grundkonfiguration wird in der Datei /etc/fail2ban/fail2ban.conf vorgenommen. Hier ein Beispiel:
# Fail2Ban main configuration file # # Comments: use '#' for comment lines and ';' (following a space) for inline comments # # Changes: in most of the cases you should not modify this # file, but provide customizations in fail2ban.local file, e.g.: # # [Definition] # loglevel = 4 # [Definition] # Option: loglevel # Notes.: Set the log level output. # 1 = ERROR # 2 = WARN # 3 = INFO # 4 = DEBUG # Values: [ NUM ] Default: 1 # loglevel = 2 # Option: logtarget # Notes.: Set the log target. This could be a file, SYSLOG, STDERR or STDOUT. # Only one log target can be specified. # If you change logtarget from the default value and you are # using logrotate -- also adjust or disable rotation in the # corresponding configuration file # (e.g. /etc/logrotate.d/fail2ban on Debian systems) # Values: [ STDOUT | STDERR | SYSLOG | FILE ] Default: STDERR # logtarget = /var/log/fail2ban.log # Option: socket # Notes.: Set the socket file. This is used to communicate with the daemon. Do # not remove this file when Fail2ban runs. It will not be possible to # communicate with the server afterwards. # Values: [ FILE ] Default: /var/run/fail2ban/fail2ban.sock # socket = /var/run/fail2ban/fail2ban.sock # Option: pidfile # Notes.: Set the PID file. This is used to store the process ID of the # fail2ban server. # Values: [ FILE ] Default: /var/run/fail2ban/fail2ban.pid # pidfile = /var/run/fail2ban/fail2ban.pidEtwas aufwändiger ist die /etc/fail2ban/jail.conf. Hier werden die Überwachungsregeln fest gelegt. Glücklicherweise müssen wir uns nur um den Teil [DEFAULT] kümmern:
[DEFAULT] # "ignoreip" can be an IP address, a CIDR mask or a DNS host. Fail2ban will not # ban a host which matches an address in this list. Several addresses can be # defined using space separator. ignoreip = 127.0.0.1/8 192.168.1.0/24 # 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_mwl)s destmail = your@mail.address # "bantime" is the number of seconds that a host is banned. bantime = 600 # A host is banned if it has generated "maxretry" during the last "findtime" # seconds. findtime = 600 # "maxretry" is the number of failures before a host get banned. maxretry = 4 # "backend" specifies the backend used to get files modification. # Available options are "pyinotify", "gamin", "polling" and "auto". # This option can be overridden in each jail as well. # # pyinotify: requires pyinotify (a file alteration monitor) to be installed. # If pyinotify is not installed, Fail2ban will use auto. # gamin: requires Gamin (a file alteration monitor) to be installed. # If Gamin is not installed, Fail2ban will use auto. # polling: uses a polling algorithm which does not require external libraries. # auto: will try to use the following backends, in order: # pyinotify, gamin, polling. backend = auto # "usedns" specifies if jails should trust hostnames in logs, # warn when DNS lookups are performed, or ignore all hostnames in logs # # yes: if a hostname is encountered, a DNS lookup will be performed. # warn: if a hostname is encountered, a DNS lookup will be performed, # but it will be logged as a warning. # no: if a hostname is encountered, will not be used for banning, # but it will be logged as info. usedns = yes
- ignoreip: Alle zu ignorierende IP-Adressen mit Leerzeichen als Trenner aufzählen
- action: Welchen Inhalt soll eine Benachrichtigungsmail haben. m=Sperrmeldung, w=Freigabemeldung, l=Logfile
- destmail: Die Ziel-Mailadresse für Benachrichtigungen
Standort der geblockten Adressen mitteilen
Das Tool GeoIP kann den Standort (Land) einer IP-Adresse ausgeben. Diese Funktion kann in fail2ban verwendet werden.
Zuerst muss GeoIP installiert werden:
yum install GeoIP
Jetzt kann dei Ausgabe von fail2ban durch die GeoIP-Ausgabe erweitert werden. Dazu sind zwei Änderungen an der Datei /usr/share/fail2ban/server/actions.py nötig:
import time, logging, commands logSys.warn("[%s] Ban %s %s" % (self.jail.getName(), aInfo["ip"], commands.getstatusoutput('geoiplookup ' + aInfo["ip"])[1][23:]))Siehe: http://www.fail2ban.org/wiki/index.php/HOWTO_use_geoiplookup