git.iko.soy/typeable/nixpkgs
on master help sign in
nixos/fail2ban: add custom options
commitf1d7dfe29f091639d935358f2d3a4ee94973d30a authorIzorkin <izorkin@elven.pw> dateMon 2 Dec 2019, 20:19 (7 years ago) parenta55be8d
1 file changed+115−1
nixos/modules/services/security/fail2ban.nix+115−1
nixos/modules/services/security/fail2ban.nix+115−1
@@ -62,6 +62,108 @@ in
6262 description = "The firewall package used by fail2ban service.";
6363 };
6464
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+
65167 daemonConfig = mkOption {
66168 default = ''
67169 [Definition]
@@ -160,10 +262,22 @@ in
160262 # Add some reasonable default jails. The special "DEFAULT" jail
161263 # sets default values for all other jails.
162264 services.fail2ban.jails.DEFAULT = ''
265+ ${optionalString cfg.bantime-increment.enable ''
266+ # Bantime incremental
267+ bantime.increment = ${if cfg.bantime-increment.enable then "true" else "false"}
268+ bantime.maxtime = ${cfg.bantime-increment.maxtime}
269+ bantime.factor = ${cfg.bantime-increment.factor}
270+ bantime.formula = ${cfg.bantime-increment.formula}
271+ bantime.multipliers = ${cfg.bantime-increment.multipliers}
272+ bantime.overalljails = ${if cfg.bantime-increment.overalljails then "true" else "false"}
273+ ''}
163274 # Miscellaneous options
164− ignoreip = 127.0.0.1/8 ${optionalString config.networking.enableIPv6 "::1"}
275+ ignoreip = 127.0.0.1/8 ${optionalString config.networking.enableIPv6 "::1"} ${concatStringsSep " " cfg.ignoreIP}
165276 maxretry = 3
166277 backend = systemd
278+ # Actions
279+ banaction = ${cfg.banaction}
280+ banaction_allports = ${cfg.banaction-allports}
167281 '';
168282 # Block SSH if there are too many failing connection attempts.
169283 services.fail2ban.jails.sshd = mkDefault ''