Gå til innhold

fail2ban

fail2ban is a log analysis tool that temporarily bans, at local firewall level, the IP addresses behind an abnormal number of authentication failures. It relies on a system of jails (one jail per monitored service), each associated with a filter that determines, among the log lines analysed, which occurrences are counted.

Summary

# Recommendation Mediation Controller Edge Gateway
R57 Installation and operating principle
R58 SSH jail
R59 Apache jails
R60 Jail specific to CyberElements Bastion authentication
R61 Interaction with the local firewall
R62 Operation and monitoring

R57. Installation and operating principle

Applies to: Mediation Controller · Edge Gateway

1
apt install fail2ban

Jails are defined in /etc/fail2ban/jail.local, a file to be created, which takes precedence over jail.conf without modifying it directly. The associated filters live in /etc/fail2ban/filter.d/: some ship as standard with the package (sshd, apache-overflows…), others must be created specifically for CyberElements Bastion (R60). The actions triggered when a jail's threshold is reached are configured in /etc/fail2ban/action.d/.

DEFAULT block common to all jails, in /etc/fail2ban/jail.local:

1
2
[DEFAULT]
ignoreip = 127.0.0.1/8

Warning!

The address ranges of the dedicated administration network should be added to ignoreip, in addition to the loopback. Without that precaution, an administrator who strings together several failed logins during a testing phase can get banned by the local firewall, including for their own SSH access.

R58. SSH jail

Applies to: Mediation Controller · Edge Gateway

Compatible with all server roles. To be added to /etc/fail2ban/jail.local:

1
2
3
4
5
6
7
[sshd]
enabled = true
port = ssh
filter = sshd
logpath = /var/log/auth.log
maxretry = 3
bantime = 600
  • bantime: duration in seconds for which the IP address remains banned;
  • maxretry: number of failures tolerated before banning.

Warning!

If the SSH listening port has been customised in standalone mode (R29), this jail's port directive must be updated to reflect the port actually in use, rather than keeping the generic ssh alias, which resolves to port 22 by default. Conversely, in cluster mode, where customising the SSH port is not supported, the ssh alias remains valid without change.

R59. Apache jails

Applies to: Mediation Controller · Edge Gateway

These jails rely on filters shipped as standard with the fail2ban package (no custom filter to create) and concern only the Mediation Controller, the sole server running Apache (see Apache).

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[apache-overflows]
enabled = true
port = http,https
filter = apache-overflows
logpath = /var/log/apache2/*error.log
maxretry = 3
bantime = 600

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

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

apache-overflows targets abnormally long or malformed requests, apache-noscript attempts to find executable scripts on the portal, apache-badbots robots identified as malicious by their User-Agent header.

Warning!

The logpath directives of these jails assume that Apache error logs are written to files under /var/log/apache2/. If the redirection of Apache logs to syslog is put in place (R64, ErrorLog syslog:daemon), those files are no longer written to and the jails silently become inoperative: the logpath entries must then be adapted to the file actually written by rsyslog, and the triggering of the jails revalidated.

R60. Jail specific to CyberElements Bastion authentication

Applies to: Mediation Controller · Edge Gateway

Unlike the jails in R59, this one requires a filter created specifically for CyberElements Bastion, since it analyses the authentication failure messages specific to the product rather than Apache's generic error logs.

1
2
3
4
5
6
7
8
[systancia-auth]
enabled = true
port = http,https
filter = systancia
logpath = /var/log/syslog
bantime = 3600
findtime = 400
maxretry = 3

File /etc/fail2ban/filter.d/systancia.conf to be created:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
[Definition]
failregex = ^.*ERROR Authentication error for an unknown user.*\(<HOST>\).*
^.*ERROR Authentication error for a registered user from .*\(<HOST>\).*
^.*ERROR Authentication error for the super user from \w+\@.*\(<HOST>\).*
^.*Failure when connecting to the web portal: user authentication error org:.*, user:.*, ip:<HOST>$
^.*Echec de la connexion au portail web : erreur d'authentification d'un utilisateur org:.*, user:.*, ip:<HOST>$
^.*Échec de la connexion à la console système ip:<HOST>$
^.*Failure when attempting to connect to system console ip:<HOST>$
^.*ActiveSync ActiveSync access FORBIDDEN for user: \w+\@\w* \(ip=<HOST>\) during 10 sec\.\.\.$
^.*Echec de modification d'un mot de passe utilisateur org:.*, user:.*, ip:<HOST>$
^.*Echec de la connexion à la console d'administration org:.*, user:.*, ip:<HOST>$
^.*Failure when connecting to the administration console org:.*, user:.*, ip:<HOST>$
^.*Echec de la connexion au portail utilisateur org:.*, user:.*, ip:<HOST>$
^.*Echec de connexion à l'API org:.*, user:.*, ip:<HOST>$
^.*API connexion failure org:.*, user:.*, ip:<HOST>$
ignoreregex =

Information

The patterns above are reproduced exactly as the security hardening guide carries them, French messages included: the product emits its authentication events in the language configured for the organisation, and a filter that only matched the English messages would silently miss the failures of a French-speaking instance.

This jail reads the authentication failures that the product writes to the local syslog of the Mediation Controller. Two boxes decide it, and no remote syslog server is involved:

  1. in the system console, Logger settings module, tick Allow the organizations to send their events via syslog;
  2. in the administration console of each organisation, General options module, Syslog frame, tick Send platform events via syslog.

The events of the system console itself are written only if Send platform events via syslog is also ticked in the Logger settings module. The product writes all these events to /dev/log, with the syslog facility and the identifier Cleanroom-<organization> (Cleanroom for the system console): no rsyslog reception has to be added.

1
systemctl restart fail2ban

Information

The file in which rsyslog writes these events depends on its configuration, and must be the one read by the jail (logpath = /var/log/syslog): check it on a mock-up with a test authentication failure and, if needed, adapt the logpath to the file actually written, or add an rsyslog rule writing these events to a file covered by the logpath.

R61. Interaction with the local firewall

Applies to: Mediation Controller · Edge Gateway

fail2ban works by dynamically inserting and removing banning rules in the local firewall, in a rule set separate from the static base described in Local firewall.

Information

The two mechanisms do not conflict by design, but must be checked together after any change to either: an OUTPUT/INPUT policy defaulting to DROP that is too restrictive at the level of the static base (R36) can prevent fail2ban itself from working properly depending on the configured action; and conversely, a misunderstood fail2ban rule can give the impression that the static firewall is malfunctioning when it is in fact a legitimate temporary ban.

The rules inserted by fail2ban can be told apart from the static ones with:

1
iptables -L -v -n | grep -i f2b

R62. Operation and monitoring

Applies to: Mediation Controller · Edge Gateway

1
2
3
4
fail2ban-client status
fail2ban-client status <jail_name>
fail2ban-client set <jail_name> banip <ip>
fail2ban-client set <jail_name> unbanip <ip>

The number of active bans and how it evolves over time should be monitored: a sudden rise may signal an attack campaign under way, whereas a nil and constant volume on a jail that is supposed to be active may reveal an inoperative filter (see R60) rather than a genuine absence of attempts.

Any IP address unbanned manually must be documented together with the justification for the intervention, in the same way as the other administrative actions covered by this guide.