> Source: https://docs.cyberelements.io/saas/pt/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 | Edge Gateway |
|---|---|---|
| [**R57**](#r57) | Installation and operating principle | ✅ |
| [**R58**](#r58) | SSH jail | ✅ |
| [**R61**](#r61) | Interaction with the local firewall | ✅ |
| [**R62**](#r62) | Operation and monitoring | ✅ |

## R57. Installation and operating principle

**Applies to**: 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`…). 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**: 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** ([R29](https://docs.cyberelements.io/saas/pt/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.

## R61. Interaction with the local firewall

**Applies to**: 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/saas/pt/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 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**: 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** 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.
