2019-02-26 13:44:40 +01:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
# mostly taken from https://github.com/davidak/nixos-config/blob/master/services/fail2ban.nix
|
2019-03-20 02:57:59 +01:00
|
|
|
with lib;
|
|
|
|
|
|
|
|
mkIf (elem "fail2ban" config.machine.services) {
|
2019-02-26 13:44:40 +01:00
|
|
|
services.fail2ban = {
|
|
|
|
enable = true;
|
|
|
|
jails = {
|
|
|
|
DEFAULT = ''
|
|
|
|
bantime = 3600
|
2019-03-25 01:41:26 +01:00
|
|
|
ignoreip = 127.0.0.1
|
2019-02-26 13:44:40 +01:00
|
|
|
logpath = /var/log/auth.log
|
|
|
|
'';
|
|
|
|
|
|
|
|
ssh = ''
|
|
|
|
enabled = true
|
|
|
|
filter = sshd
|
|
|
|
maxretry = 4
|
|
|
|
action = iptables[name=SSH, port=ssh, protocol=tcp]
|
|
|
|
'';
|
|
|
|
sshd-ddos = ''
|
|
|
|
enabled = true
|
|
|
|
filter = sshd-ddos
|
|
|
|
maxretry = 2
|
|
|
|
action = iptables[name=ssh, port=ssh, protocol=tcp]
|
|
|
|
'';
|
|
|
|
|
|
|
|
postfix = ''
|
|
|
|
enabled = true
|
|
|
|
filter = postfix
|
|
|
|
maxretry = 3
|
|
|
|
action = iptables[name=postfix, port=smtp, protocol=tcp]
|
|
|
|
'';
|
|
|
|
postfix-sasl = ''
|
|
|
|
enabled = true
|
|
|
|
filter = postfix-sasl
|
|
|
|
maxretry = 3
|
|
|
|
action = iptables[name=postfix, port=smtp, protocol=tcp]
|
|
|
|
'';
|
|
|
|
postfix-ddos = ''
|
|
|
|
enabled = true
|
|
|
|
filter = postfix-ddos
|
|
|
|
maxretry = 3
|
|
|
|
action = iptables[name=postfix, port=submission, protocol=tcp]
|
|
|
|
bantime = 7200
|
|
|
|
'';
|
|
|
|
|
|
|
|
nginx-req-limit = ''
|
|
|
|
enabled = true
|
|
|
|
filter = nginx-req-limit
|
|
|
|
maxretry = 10
|
|
|
|
action = iptables-multiport[name=ReqLimit, port="http,https", protocol=tcp]
|
|
|
|
findtime = 600
|
|
|
|
bantime = 7200
|
2019-03-20 02:57:59 +01:00
|
|
|
'';
|
2019-02-26 13:44:40 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
environment.etc."fail2ban/filter.d/postfix-ddos.conf".text = ''
|
|
|
|
[Definition]
|
|
|
|
failregex = lost connection after EHLO from \S+\[<HOST>\]
|
|
|
|
'';
|
|
|
|
|
|
|
|
environment.etc."fail2ban/filter.d/nginx-req-limit.conf".text = ''
|
|
|
|
[Definition]
|
|
|
|
failregex = limiting requests, excess:.* by zone.*client: <HOST>
|
|
|
|
'';
|
|
|
|
|
|
|
|
# Limit stack size to reduce memory usage
|
|
|
|
systemd.services.fail2ban.serviceConfig.LimitSTACK = 256 * 1024;
|
|
|
|
}
|