{ environment.etc."fail2ban/filter.d/sshd-normal.local".text = '' [Definition] failregex = ^.*: Failed password for .* from port \d+.*$ ^.*: pam_unix\(sshd:auth\): authentication failure;.* rhost=\b.*$ ignoreregex = ^.*: Failed password for root from port \d+.*$ ^.*: pam_unix\(sshd:auth\): authentication failure;.* rhost=\b.* user=root\b.*$ ''; # The pam_unix lines look like: # pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=10.243.13.42 user=root environment.etc."fail2ban/filter.d/sshd-instaban.local".text = '' [Definition] failregex = ^.*: (?:Failed password for invalid user|Invalid user|Failed password for root)\b.* from port \d+.*$ ^.*: pam_unix\(sshd:auth\): authentication failure;.* rhost=\b.* user=root\b.*$ ''; services.fail2ban = { enable = true; ignoreIP = [ "10.0.0.0/8" "172.16.0.0/12" "192.168.0.0/16" "42::/16" ]; bantime-increment = { enable = true; multipliers = "1 2 4 8 16 32 64"; maxtime = "168h"; overalljails = true; }; jails = { # Disable NixOS' default sshd jail. sshd.enabled = false; sshd-instaban.settings = { filter = "sshd-instaban"; port = "ssh"; findtime = 600; maxretry = 1; bantime = "24h"; # nft verdicts are lowercase (iptables' would be DROP). action = "%(action_)s[blocktype=drop]"; journalmatch = '' SYSLOG_IDENTIFIER=sshd-session ''; }; sshd-normal.settings = { filter = "sshd-normal"; port = "ssh"; journalmatch = '' SYSLOG_IDENTIFIER=sshd-session ''; findtime = 600; # 10 minutes maxretry = 5; bantime = "24h"; }; }; }; # services.fail2ban sets this to "VERBOSE". services.openssh.settings.LogLevel = "INFO"; }