> Source: https://docs.cyberelements.io/bastion/4.7/no/Security/Fail2ban/

# 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**](#r57) | Installation and operating principle | ✅ | ✅ |
| [**R58**](#r58) | SSH jail | ✅ | ✅ |
| [**R59**](#r59) | Apache jails | ✅ | ❌ |
| [**R60**](#r60) | Jail specific to CyberElements Bastion authentication | ✅ | ❌ |
| [**R61**](#r61) | Interaction with the local firewall | ✅ | ✅ |
| [**R62**](#r62) | Operation and monitoring | ✅ | ✅ |

## R57. Installation and operating principle

**Applies to**: Mediation Controller ✅ · Edge Gateway ✅

```
1
```

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](#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
```

> **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
```

- `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](https://docs.cyberelements.io/bastion/4.7/no/Security/Ssh/#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](https://docs.cyberelements.io/bastion/4.7/no/Security/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` 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](https://docs.cyberelements.io/bastion/4.7/no/Security/CentralizedLogging/#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](#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
```

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
```

> **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](https://docs.cyberelements.io/bastion/4.7/no/Usage/Administration/SystemConsole/LoggerSettings/) module, tick **Allow the organizations to send their events via syslog**;
2. in the administration console of each organisation, [General options](https://docs.cyberelements.io/bastion/4.7/no/Usage/Administration/AdminConsole/ConfigurationWorkPanel/GeneralOptions/) 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
```

> **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](https://docs.cyberelements.io/bastion/4.7/no/Security/LocalFirewall/).

> **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](https://docs.cyberelements.io/bastion/4.7/no/Security/LocalFirewall/#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
```

## R62. Operation and monitoring

**Applies to**: Mediation Controller ✅ · Edge Gateway ✅

```
1
2
3
4
```

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](#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.
