git.iko.soy/typeable/nixpkgs
on master help sign in
fail2ban: 0.10.5 -> 0.11.1 (#67931)

fail2ban: 0.10.5 -> 0.11.1

commitc23f10da6a6bdfbfe4950d3c5fd741704cbe7188 authorJörg Thalheim <Mic92@users.noreply.github.com> dateFri 31 Jan 2020, 08:58 (7 years ago) parenta5a93e696e2669
2 files changed+231−81
nixos/modules/services/security/fail2ban.nix+228−78 pkgs/tools/security/fail2ban/default.nix+3−3
nixos/modules/services/security/fail2ban.nix+228−78
@@ -6,15 +6,32 @@ let
66
77 cfg = config.services.fail2ban;
88
9− fail2banConf = pkgs.writeText "fail2ban.conf" cfg.daemonConfig;
9+ fail2banConf = pkgs.writeText "fail2ban.local" cfg.daemonConfig;
1010
11− jailConf = pkgs.writeText "jail.conf"
12− (concatStringsSep "\n" (attrValues (flip mapAttrs cfg.jails (name: def:
11+ jailConf = pkgs.writeText "jail.local" ''
12+ [INCLUDES]
13+
14+ before = paths-nixos.conf
15+
16+ ${concatStringsSep "\n" (attrValues (flip mapAttrs cfg.jails (name: def:
1317 optionalString (def != "")
1418 ''
1519 [${name}]
1620 ${def}
17− ''))));
21+ '')))}
22+ '';
23+
24+ pathsConf = pkgs.writeText "paths-nixos.conf" ''
25+ # NixOS
26+
27+ [INCLUDES]
28+
29+ before = paths-common.conf
30+
31+ after = paths-overrides.local
32+
33+ [DEFAULT]
34+ '';
1835
1936in
2037
@@ -31,21 +48,135 @@ in
3148 description = "Whether to enable the fail2ban service.";
3249 };
3350
51+ package = mkOption {
52+ default = pkgs.fail2ban;
53+ type = types.package;
54+ example = "pkgs.fail2ban_0_11";
55+ description = "The fail2ban package to use for running the fail2ban service.";
56+ };
57+
58+ packageFirewall = mkOption {
59+ default = pkgs.iptables;
60+ type = types.package;
61+ example = "pkgs.nftables";
62+ description = "The firewall package used by fail2ban service.";
63+ };
64+
65+ banaction = mkOption {
66+ default = "iptables-multiport";
67+ type = types.str;
68+ example = "nftables-multiport";
69+ description = ''
70+ Default banning action (e.g. iptables, iptables-new, iptables-multiport,
71+ shorewall, etc) It is used to define action_* variables. Can be overridden
72+ globally or per section within jail.local file
73+ '';
74+ };
75+
76+ banaction-allports = mkOption {
77+ default = "iptables-allport";
78+ type = types.str;
79+ example = "nftables-allport";
80+ description = ''
81+ Default banning action (e.g. iptables, iptables-new, iptables-multiport,
82+ shorewall, etc) It is used to define action_* variables. Can be overridden
83+ globally or per section within jail.local file
84+ '';
85+ };
86+
87+ bantime-increment.enable = mkOption {
88+ default = false;
89+ type = types.bool;
90+ description = ''
91+ Allows to use database for searching of previously banned ip's to increase
92+ a default ban time using special formula, default it is banTime * 1, 2, 4, 8, 16, 32...
93+ '';
94+ };
95+
96+ bantime-increment.rndtime = mkOption {
97+ default = "4m";
98+ type = types.str;
99+ example = "8m";
100+ description = ''
101+ "bantime-increment.rndtime" is the max number of seconds using for mixing with random time
102+ to prevent "clever" botnets calculate exact time IP can be unbanned again
103+ '';
104+ };
105+
106+ bantime-increment.maxtime = mkOption {
107+ default = "10h";
108+ type = types.str;
109+ example = "48h";
110+ description = ''
111+ "bantime-increment.maxtime" is the max number of seconds using the ban time can reach (don't grows further)
112+ '';
113+ };
114+
115+ bantime-increment.factor = mkOption {
116+ default = "1";
117+ type = types.str;
118+ example = "4";
119+ description = ''
120+ "bantime-increment.factor" is a coefficient to calculate exponent growing of the formula or common multiplier,
121+ default value of factor is 1 and with default value of formula, the ban time grows by 1, 2, 4, 8, 16 ...
122+ '';
123+ };
124+
125+ bantime-increment.formula = mkOption {
126+ default = "ban.Time * (1<<(ban.Count if ban.Count<20 else 20)) * banFactor";
127+ type = types.str;
128+ example = "ban.Time * math.exp(float(ban.Count+1)*banFactor)/math.exp(1*banFactor)";
129+ description = ''
130+ "bantime-increment.formula" used by default to calculate next value of ban time, default value bellow,
131+ the same ban time growing will be reached by multipliers 1, 2, 4, 8, 16, 32...
132+ '';
133+ };
134+
135+ bantime-increment.multipliers = mkOption {
136+ default = "1 2 4 8 16 32 64";
137+ type = types.str;
138+ example = "2 4 16 128";
139+ description = ''
140+ "bantime-increment.multipliers" used to calculate next value of ban time instead of formula, coresponding
141+ previously ban count and given "bantime.factor" (for multipliers default is 1);
142+ following example grows ban time by 1, 2, 4, 8, 16 ... and if last ban count greater as multipliers count,
143+ always used last multiplier (64 in example), for factor '1' and original ban time 600 - 10.6 hours
144+ '';
145+ };
146+
147+ bantime-increment.overalljails = mkOption {
148+ default = false;
149+ type = types.bool;
150+ example = true;
151+ description = ''
152+ "bantime-increment.overalljails" (if true) specifies the search of IP in the database will be executed
153+ cross over all jails, if false (dafault), only current jail of the ban IP will be searched
154+ '';
155+ };
156+
157+ ignoreIP = mkOption {
158+ default = [ ];
159+ type = types.listOf types.str;
160+ example = [ "192.168.0.0/16" "2001:DB8::42" ];
161+ description = ''
162+ "ignoreIP" can be a list of IP addresses, CIDR masks or DNS hosts. Fail2ban will not ban a host which
163+ matches an address in this list. Several addresses can be defined using space (and/or comma) separator.
164+ '';
165+ };
166+
34167 daemonConfig = mkOption {
35− default =
36− ''
37− [Definition]
38− loglevel = INFO
39− logtarget = SYSLOG
40− socket = /run/fail2ban/fail2ban.sock
41− pidfile = /run/fail2ban/fail2ban.pid
42− '';
168+ default = ''
169+ [Definition]
170+ logtarget = SYSLOG
171+ socket = /run/fail2ban/fail2ban.sock
172+ pidfile = /run/fail2ban/fail2ban.pid
173+ dbfile = /var/lib/fail2ban/fail2ban.sqlite3
174+ '';
43175 type = types.lines;
44− description =
45− ''
46− The contents of Fail2ban's main configuration file. It's
47− generally not necessary to change it.
48− '';
176+ description = ''
177+ The contents of Fail2ban's main configuration file. It's
178+ generally not necessary to change it.
179+ '';
49180 };
50181
51182 jails = mkOption {
@@ -65,88 +196,107 @@ in
65196 }
66197 '';
67198 type = types.attrsOf types.lines;
68− description =
69− ''
70− The configuration of each Fail2ban “jail”. A jail
71− consists of an action (such as blocking a port using
72− <command>iptables</command>) that is triggered when a
73− filter applied to a log file triggers more than a certain
74− number of times in a certain time period. Actions are
75− defined in <filename>/etc/fail2ban/action.d</filename>,
76− while filters are defined in
77− <filename>/etc/fail2ban/filter.d</filename>.
78− '';
199+ description = ''
200+ The configuration of each Fail2ban “jail”. A jail
201+ consists of an action (such as blocking a port using
202+ <command>iptables</command>) that is triggered when a
203+ filter applied to a log file triggers more than a certain
204+ number of times in a certain time period. Actions are
205+ defined in <filename>/etc/fail2ban/action.d</filename>,
206+ while filters are defined in
207+ <filename>/etc/fail2ban/filter.d</filename>.
208+ '';
79209 };
80210
81211 };
82212
83213 };
84214
85−
86215 ###### implementation
87216
88217 config = mkIf cfg.enable {
89218
90− environment.systemPackages = [ pkgs.fail2ban ];
219+ environment.systemPackages = [ cfg.package ];
91220
92− environment.etc."fail2ban/fail2ban.conf".source = fail2banConf;
93− environment.etc."fail2ban/jail.conf".source = jailConf;
94− environment.etc."fail2ban/action.d".source = "${pkgs.fail2ban}/etc/fail2ban/action.d/*.conf";
95− environment.etc."fail2ban/filter.d".source = "${pkgs.fail2ban}/etc/fail2ban/filter.d/*.conf";
96−
97− systemd.services.fail2ban =
98− { description = "Fail2ban Intrusion Prevention System";
221+ environment.etc = {
222+ "fail2ban/fail2ban.local".source = fail2banConf;
223+ "fail2ban/jail.local".source = jailConf;
224+ "fail2ban/fail2ban.conf".source = "${cfg.package}/etc/fail2ban/fail2ban.conf";
225+ "fail2ban/jail.conf".source = "${cfg.package}/etc/fail2ban/jail.conf";
226+ "fail2ban/paths-common.conf".source = "${cfg.package}/etc/fail2ban/paths-common.conf";
227+ "fail2ban/paths-nixos.conf".source = pathsConf;
228+ "fail2ban/action.d".source = "${cfg.package}/etc/fail2ban/action.d/*.conf";
229+ "fail2ban/filter.d".source = "${cfg.package}/etc/fail2ban/filter.d/*.conf";
230+ };
99231
100− wantedBy = [ "multi-user.target" ];
101− after = [ "network.target" ];
102− partOf = optional config.networking.firewall.enable "firewall.service";
232+ systemd.services.fail2ban = {
233+ description = "Fail2ban Intrusion Prevention System";
103234
104− restartTriggers = [ fail2banConf jailConf ];
105− path = [ pkgs.fail2ban pkgs.iptables pkgs.iproute ];
235+ wantedBy = [ "multi-user.target" ];
236+ after = [ "network.target" ];
237+ partOf = optional config.networking.firewall.enable "firewall.service";
106238
107− preStart =
108− ''
109− mkdir -p /var/lib/fail2ban
110− '';
239+ restartTriggers = [ fail2banConf jailConf pathsConf ];
240+ reloadIfChanged = true;
111241
112− unitConfig.Documentation = "man:fail2ban(1)";
242+ path = [ cfg.package cfg.packageFirewall pkgs.iproute ];
113243
114− serviceConfig =
115− { Type = "forking";
116− ExecStart = "${pkgs.fail2ban}/bin/fail2ban-client -x start";
117− ExecStop = "${pkgs.fail2ban}/bin/fail2ban-client stop";
118− ExecReload = "${pkgs.fail2ban}/bin/fail2ban-client reload";
119− PIDFile = "/run/fail2ban/fail2ban.pid";
120− Restart = "always";
244+ unitConfig.Documentation = "man:fail2ban(1)";
121245
122− ReadOnlyDirectories = "/";
123− ReadWriteDirectories = "/run/fail2ban /var/tmp /var/lib";
124− PrivateTmp = "true";
125− RuntimeDirectory = "fail2ban";
126− CapabilityBoundingSet = "CAP_DAC_READ_SEARCH CAP_NET_ADMIN CAP_NET_RAW";
127− };
246+ serviceConfig = {
247+ ExecStart = "${cfg.package}/bin/fail2ban-server -xf start";
248+ ExecStop = "${cfg.package}/bin/fail2ban-server stop";
249+ ExecReload = "${cfg.package}/bin/fail2ban-server reload";
250+ Type = "simple";
251+ Restart = "on-failure";
252+ PIDFile = "/run/fail2ban/fail2ban.pid";
253+ # Capabilities
254+ CapabilityBoundingSet = [ "CAP_AUDIT_READ" "CAP_DAC_READ_SEARCH" "CAP_NET_ADMIN" "CAP_NET_RAW" ];
255+ # Security
256+ NoNewPrivileges = true;
257+ # Directory
258+ RuntimeDirectory = "fail2ban";
259+ RuntimeDirectoryMode = "0750";
260+ StateDirectory = "fail2ban";
261+ StateDirectoryMode = "0750";
262+ LogsDirectory = "fail2ban";
263+ LogsDirectoryMode = "0750";
264+ # Sandboxing
265+ ProtectSystem = "strict";
266+ ProtectHome = true;
267+ PrivateTmp = true;
268+ PrivateDevices = true;
269+ ProtectHostname = true;
270+ ProtectKernelTunables = true;
271+ ProtectKernelModules = true;
272+ ProtectControlGroups = true;
128273 };
274+ };
129275
130276 # Add some reasonable default jails. The special "DEFAULT" jail
131277 # sets default values for all other jails.
132− services.fail2ban.jails.DEFAULT =
133− ''
134− ignoreip = 127.0.0.1/8
135− bantime = 600
136− findtime = 600
137− maxretry = 3
138− backend = systemd
139− enabled = true
140− '';
141−
278+ services.fail2ban.jails.DEFAULT = ''
279+ ${optionalString cfg.bantime-increment.enable ''
280+ # Bantime incremental
281+ bantime.increment = ${if cfg.bantime-increment.enable then "true" else "false"}
282+ bantime.maxtime = ${cfg.bantime-increment.maxtime}
283+ bantime.factor = ${cfg.bantime-increment.factor}
284+ bantime.formula = ${cfg.bantime-increment.formula}
285+ bantime.multipliers = ${cfg.bantime-increment.multipliers}
286+ bantime.overalljails = ${if cfg.bantime-increment.overalljails then "true" else "false"}
287+ ''}
288+ # Miscellaneous options
289+ ignoreip = 127.0.0.1/8 ${optionalString config.networking.enableIPv6 "::1"} ${concatStringsSep " " cfg.ignoreIP}
290+ maxretry = 3
291+ backend = systemd
292+ # Actions
293+ banaction = ${cfg.banaction}
294+ banaction_allports = ${cfg.banaction-allports}
295+ '';
142296 # Block SSH if there are too many failing connection attempts.
143− services.fail2ban.jails.ssh-iptables =
144− ''
145− filter = sshd
146− action = iptables-multiport[name=SSH, port="${concatMapStringsSep "," (p: toString p) config.services.openssh.ports}", protocol=tcp]
147− maxretry = 5
148− '';
149−
297+ services.fail2ban.jails.sshd = mkDefault ''
298+ enabled = true
299+ port = ${concatMapStringsSep "," (p: toString p) config.services.openssh.ports}
300+ '';
150301 };
151−
152302}
pkgs/tools/security/fail2ban/default.nix+3−3
@@ -1,6 +1,6 @@
11{ stdenv, fetchFromGitHub, python3, gamin }:
22
3−let version = "0.10.5"; in
3+let version = "0.11.1"; in
44
55python3.pkgs.buildPythonApplication {
66 pname = "fail2ban";
@@ -10,7 +10,7 @@ python3.pkgs.buildPythonApplication {
1010 owner = "fail2ban";
1111 repo = "fail2ban";
1212 rev = version;
13− sha256 = "1s8g46vkwhqnagj69v4wvcasypzkmq7awhfbxahffrypcpad5ach";
13+ sha256 = "0kqvkxpb72y3kgmxf6g36w67499c6gcd2a9yyblagwx12y05f1sh";
1414 };
1515
1616 pythonPath = with python3.pkgs;
@@ -50,7 +50,7 @@ python3.pkgs.buildPythonApplication {
5050 '';
5151
5252 meta = with stdenv.lib; {
53− homepage = http://www.fail2ban.org/;
53+ homepage = https://www.fail2ban.org/;
5454 description = "A program that scans log files for repeated failing login attempts and bans IP addresses";
5555 license = licenses.gpl2Plus;
5656 maintainers = with maintainers; [ eelco lovek323 fpletz ];