Add opt-in host firewalling with nftables and Fail2ban #85
No files matched your search
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"version": 1,
|
||||
"profile": "public",
|
||||
"services": ["web", "mail", "dns", "sftp"],
|
||||
"admin_sources": ["192.0.2.10", "2001:db8::10"],
|
||||
"ssh_port": 22,
|
||||
"panel_ports": [80, 443],
|
||||
"rates": {
|
||||
"panel": 40,
|
||||
"web": 120,
|
||||
"mail": 30,
|
||||
"dns": 100,
|
||||
"ssh": 8
|
||||
}
|
||||
}
|
||||
@@ -162,6 +162,134 @@ The addresses for hosted sites, mail, and authoritative DNS can differ from
|
||||
the panel hostname. Configure the corresponding public DNS records for each
|
||||
service using its reachable address.
|
||||
|
||||
## Protect a host with nftables and Fail2ban
|
||||
|
||||
Enable protection independently on a panel, web, mail, or DNS host. Use the
|
||||
distribution packages and install the Coulobre units on each protected host.
|
||||
The firewall changes the host's inbound policy. Keep console access
|
||||
available while testing a new policy.
|
||||
|
||||
```sh
|
||||
sudo apt install nftables fail2ban python3-systemd
|
||||
sudo install -m 0644 deploy/systemd/coulobre-firewall.service \
|
||||
deploy/systemd/coulobre-fail2ban.service /etc/systemd/system/
|
||||
sudo systemctl daemon-reload
|
||||
sudo systemctl enable --now coulobre-firewall.service
|
||||
sudo install -d -m 0750 /etc/coulobre
|
||||
if ! sudo test -e /etc/coulobre/firewall.json; then
|
||||
sudo install -m 0600 deploy/firewall/host.json.example /etc/coulobre/firewall.json
|
||||
fi
|
||||
sudoedit /etc/coulobre/firewall.json
|
||||
```
|
||||
|
||||
Set `admin_sources` to the addresses or CIDR networks from which SSH
|
||||
administrators and Coulobre relay workers reach this host. Set `ssh_port`
|
||||
to the local OpenSSH listener port. Add `sftp` to `services` to admit
|
||||
public SSH and SFTP connections subject to the configured connection limit.
|
||||
The administrator sources have direct access to the SSH listener and are
|
||||
excluded from the managed Fail2ban jails.
|
||||
|
||||
| Profile | Panel listeners |
|
||||
| --- | --- |
|
||||
| `public` | Public access on `panel_ports`, default TCP 80 and 443 |
|
||||
| `lan` | Access from `panel_sources` only, default TCP 80 and 443 |
|
||||
| `proxy` | Access from `panel_sources` only, default TCP 8080 |
|
||||
| `node` | No panel listener |
|
||||
|
||||
Use the LAN subnets for `lan`. For `proxy`, list the immediate proxy peers
|
||||
and set `panel_ports` to the origin listener ports. A restricted panel
|
||||
listener needs its own ports when public websites share the host. A
|
||||
packet filter admits connections by address and port. Use the Apache
|
||||
origin configuration for the trusted-proxy and hostname restrictions.
|
||||
[External reverse proxy configuration](#external-reverse-proxy)
|
||||
|
||||
Select the services to offer per host. The `web` role allows TCP 80 and 443.
|
||||
The `mail` role allows TCP 25, 143, 465, 587, and 993. The `dns` role
|
||||
allows TCP and UDP 53. The `web` rate applies to ports shared by websites
|
||||
and a public panel. Use `extra_tcp_ports` or `extra_udp_ports` to permit
|
||||
additional public listeners. The firewall accepts loopback, established
|
||||
connections, ICMP, and DHCP replies. It drops incoming connections outside
|
||||
the selected policy.
|
||||
|
||||
Set `rates` to limits per source IP and second for `ssh`, `panel`, `web`,
|
||||
`mail`, and `dns`. The defaults are 8, 40, 120, 30, and 100 respectively.
|
||||
Set a rate to `0` to allow that service without its Coulobre rate limit.
|
||||
TCP limits apply to new connections. DNS also has a separate UDP packet
|
||||
limit, including repeated requests on an established UDP flow. A reverse
|
||||
proxy shares one origin-side limit across its clients. Limit login requests
|
||||
per client at the trusted proxy or application layer using the client
|
||||
address verified by that layer. [nftables meters](https://wiki.nftables.org/wiki-nftables/index.php/Meters)
|
||||
|
||||
### Stage and confirm a change
|
||||
|
||||
Preview the generated rules and stage the policy. The command checks the
|
||||
nftables transaction and schedules a rollback before changing the live
|
||||
rules. The trial lasts 120 seconds by default.
|
||||
|
||||
```sh
|
||||
firewall=/opt/coulobre/bin/coulobre-firewall
|
||||
sudo "$firewall" preview /etc/coulobre/firewall.json
|
||||
sudo "$firewall" apply /etc/coulobre/firewall.json
|
||||
```
|
||||
|
||||
Open a new administrator SSH session and verify the services from the
|
||||
intended networks. From that session, confirm using the ID printed by
|
||||
`apply`. The timer restores the previous confirmed policy if the trial
|
||||
expires. Run `sudo "$firewall" rollback` to restore immediately. The first
|
||||
rollback removes the Coulobre table when there is no confirmed policy.
|
||||
|
||||
```sh
|
||||
sudo "$firewall" confirm CONFIRMATION_ID
|
||||
sudo "$firewall" status
|
||||
```
|
||||
|
||||
The service restores the last confirmed policy on reboot. Coulobre owns
|
||||
`inet coulobre_host` and the managed Fail2ban instance uses
|
||||
`inet coulobre_bans`. Other tables keep their rules. Existing firewall
|
||||
chains can still reject traffic allowed by the Coulobre policy.
|
||||
Package upgrades preserve the confirmed policy in
|
||||
`/var/lib/coulobre/firewall/`. If another firewall manager reloads the
|
||||
entire ruleset, restore the Coulobre policy and restart the managed
|
||||
Fail2ban instance after that reload. [nftables ruleset operations](https://wiki.nftables.org/wiki-nftables/index.php/Operations_at_ruleset_level)
|
||||
|
||||
### Enable authentication failure bans
|
||||
|
||||
After confirming the policy, start the managed Fail2ban instance. It uses
|
||||
the distribution's journal-backed SSH, Postfix authentication, and
|
||||
Dovecot filters. The mail jails are enabled with the `mail` role.
|
||||
Each jail issues a ten-minute ban after five failures in ten minutes.
|
||||
The instance has its own jails, control socket, and persistent ban database.
|
||||
Review any separately installed Fail2ban service and its enabled jails.
|
||||
Both services apply their configured policies independently.
|
||||
[Fail2ban jail configuration](https://manpages.debian.org/trixie/fail2ban/jail.conf.5.en.html)
|
||||
|
||||
```sh
|
||||
sudo systemctl enable --now coulobre-fail2ban.service
|
||||
sudo "$firewall" status
|
||||
```
|
||||
|
||||
Run `sudo systemctl reload coulobre-fail2ban.service` after confirming a
|
||||
policy that changes service roles, the SSH port, or administrator sources.
|
||||
Use the `status` output to find the managed jail and banned address.
|
||||
Unblocking records the operator, address, reason, and result on the node.
|
||||
|
||||
```sh
|
||||
sudo "$firewall" unblock coulobre-sshd 192.0.2.25 --reason "Verified administrator address"
|
||||
sudo "$firewall" audit
|
||||
```
|
||||
|
||||
Include `firewall` and `fail2ban` in the host's
|
||||
[service health configuration](#monitor-local-and-remote-services) to
|
||||
monitor the units. Use `status` on the node to inspect rules and bans.
|
||||
To remove protection, disable the managed Fail2ban unit, clear the
|
||||
Coulobre table and confirmed policy, then disable the firewall unit.
|
||||
|
||||
```sh
|
||||
sudo systemctl disable --now coulobre-fail2ban.service
|
||||
sudo "$firewall" clear
|
||||
sudo systemctl disable --now coulobre-firewall.service
|
||||
```
|
||||
|
||||
## Start the services
|
||||
|
||||
Install and enable the three units. The panel's socket directory is available
|
||||
|
||||
Reference in new issue
Block a user