2024-08-02 04:59:00 +02:00
|
|
|
{ pkgs, ... }: {
|
2024-08-02 04:58:26 +02:00
|
|
|
|
|
|
|
# References:
|
|
|
|
# https://blog.lrvt.de/configuring-fail2ban-with-traefik/
|
|
|
|
# https://nixos.wiki/wiki/Fail2ban#Extending_Fail2ban
|
|
|
|
|
2024-08-12 02:48:40 +02:00
|
|
|
services.logrotate = {
|
|
|
|
enable = true;
|
|
|
|
checkConfig = true;
|
|
|
|
settings = {
|
|
|
|
"/var/log/traefik/access.log" = {
|
|
|
|
frequency = "daily";
|
2024-08-13 05:03:34 +02:00
|
|
|
rotate = 30;
|
2024-08-12 02:48:40 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2024-08-02 04:58:26 +02:00
|
|
|
services.fail2ban.jails = {
|
|
|
|
traefik-general-forceful-browsing = {
|
|
|
|
settings = {
|
|
|
|
enabled = true;
|
2024-08-05 04:54:10 +02:00
|
|
|
filter = "traefik-general-forceful-browsing";
|
2024-08-05 05:20:46 +02:00
|
|
|
action = "action-ban-docker-forceful-browsing";
|
2024-08-02 04:58:26 +02:00
|
|
|
logpath = "/var/log/traefik/access.log";
|
|
|
|
backend = "auto";
|
|
|
|
findtime = 600;
|
|
|
|
bantime = 600;
|
|
|
|
maxretry = 5;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
environment.etc= {
|
2024-08-05 04:49:12 +02:00
|
|
|
"fail2ban/filter.d/traefik-general-forceful-browsing.conf".text = pkgs.lib.mkDefault (pkgs.lib.mkAfter ''
|
2024-08-02 04:58:26 +02:00
|
|
|
[INCLUDES]
|
|
|
|
|
|
|
|
[Definition]
|
|
|
|
|
|
|
|
# fail regex based on traefik JSON access logs with enabled user agent logging
|
|
|
|
failregex = ^{"ClientAddr":"<F-CLIENTADDR>.*</F-CLIENTADDR>","ClientHost":"<HOST>","ClientPort":"<F-CLIENTPORT>.*</F-CLIENTPORT>","ClientUsername":"<F-CLIENTUSERNAME>.*</F-CLIENTUSERNAME>","DownstreamContentSize":<F-DOWNSTREAMCONTENTSIZE>.*</F-DOWNSTREAMCONTENTSIZE>,"DownstreamStatus":<F-DOWNSTREAMSTATUS>.*</F-DOWNSTREAMSTATUS>,"Duration":<F-DURATION>.*</F-DURATION>,"OriginContentSize":<F-ORIGINCONTENTSIZE>.*</F-ORIGINCONTENTSIZE>,"OriginDuration":<F-ORIGINDURATION>.*</F-ORIGINDURATION>,"OriginStatus":(405|404|403|402|401),"Overhead":<F-OVERHEAD>.*</F-OVERHEAD>,"RequestAddr":"<F-REQUESTADDR>.*</F-REQUESTADDR>","RequestContentSize":<F-REQUESTCONTENTSIZE>.*</F-REQUESTCONTENTSIZE>,"RequestCount":<F-REQUESTCOUNT>.*</F-REQUESTCOUNT>,"RequestHost":"<F-CONTAINER>.*</F-CONTAINER>","RequestMethod":"<F-REQUESTMETHOD>.*</F-REQUESTMETHOD>","RequestPath":"<F-REQUESTPATH>.*</F-REQUESTPATH>","RequestPort":"<F-REQUESTPORT>.*</F-REQUESTPORT>","RequestProtocol":"<F-REQUESTPROTOCOL>.*</F-REQUESTPROTOCOL>","RequestScheme":"<F-REQUESTSCHEME>.*</F-REQUESTSCHEME>","RetryAttempts":<F-RETRYATTEMPTS>.*</F-RETRYATTEMPTS>,.*"StartLocal":"<F-STARTLOCAL>.*</F-STARTLOCAL>","StartUTC":"<F-STARTUTC>.*</F-STARTUTC>","TLSCipher":"<F-TLSCIPHER>.*</F-TLSCIPHER>","TLSVersion":"<F-TLSVERSION>.*</F-TLSVERSION>","entryPointName":"<F-ENTRYPOINTNAME>.*</F-ENTRYPOINTNAME>","level":"<F-LEVEL>.*</F-LEVEL>","msg":"<F-MSG>.*</F-MSG>",("request_User-Agent":"<F-USERAGENT>.*</F-USERAGENT>",){0,1}?"time":"<F-TIME>.*</F-TIME>"}$
|
|
|
|
|
|
|
|
# custom date pattern for traefik JSON access logs
|
|
|
|
# based on https://github.com/fail2ban/fail2ban/issues/2558#issuecomment-546738270
|
|
|
|
datepattern = "StartLocal"\s*:\s*"%%Y-%%m-%%d[T]%%H:%%M:%%S\.%%f\d*(%%z)?",
|
|
|
|
|
|
|
|
# ignore common errors like missing media files or JS/CSS/TXT/ICO stuff
|
|
|
|
ignoreregex = ^{"ClientAddr":"<F-CLIENTADDR>.*</F-CLIENTADDR>","ClientHost":"<HOST>","ClientPort":"<F-CLIENTPORT>.*</F-CLIENTPORT>","ClientUsername":"<F-CLIENTUSERNAME>.*</F-CLIENTUSERNAME>","DownstreamContentSize":<F-DOWNSTREAMCONTENTSIZE>.*</F-DOWNSTREAMCONTENTSIZE>,"DownstreamStatus":<F-DOWNSTREAMSTATUS>.*</F-DOWNSTREAMSTATUS>,"Duration":<F-DURATION>.*</F-DURATION>,"OriginContentSize":<F-ORIGINCONTENTSIZE>.*</F-ORIGINCONTENTSIZE>,"OriginDuration":<F-ORIGINDURATION>.*</F-ORIGINDURATION>,"OriginStatus":(405|404|403|402|401),"Overhead":<F-OVERHEAD>.*</F-OVERHEAD>,"RequestAddr":"<F-REQUESTADDR>.*</F-REQUESTADDR>","RequestContentSize":<F-REQUESTCONTENTSIZE>.*</F-REQUESTCONTENTSIZE>,"RequestCount":<F-REQUESTCOUNT>.*</F-REQUESTCOUNT>,"RequestHost":"<F-REQUESTHOST>.*</F-REQUESTHOST>","RequestMethod":"<F-REQUESTMETHOD>.*</F-REQUESTMETHOD>","RequestPath":"<F-REQUESTPATH>.*(\.png|\.txt|\.jpg|\.ico|\.js|\.css|\.ttf|\.woff|\.woff2)(/)*?</F-REQUESTPATH>","RequestPort":"<F-REQUESTPORT>.*</F-REQUESTPORT>","RequestProtocol":"<F-REQUESTPROTOCOL>.*</F-REQUESTPROTOCOL>","RequestScheme":"<F-REQUESTSCHEME>.*</F-REQUESTSCHEME>","RetryAttempts":<F-RETRYATTEMPTS>.*</F-RETRYATTEMPTS>,.*"StartLocal":"<F-STARTLOCAL>.*</F-STARTLOCAL>","StartUTC":"<F-STARTUTC>.*</F-STARTUTC>","TLSCipher":"<F-TLSCIPHER>.*</F-TLSCIPHER>","TLSVersion":"<F-TLSVERSION>.*</F-TLSVERSION>","entryPointName":"<F-ENTRYPOINTNAME>.*</F-ENTRYPOINTNAME>","level":"<F-LEVEL>.*</F-LEVEL>","msg":"<F-MSG>.*</F-MSG>",("request_User-Agent":"<F-USERAGENT>.*</F-USERAGENT>",){0,1}?"time":"<F-TIME>.*</F-TIME>"}$
|
|
|
|
'');
|
2024-08-05 04:54:10 +02:00
|
|
|
|
|
|
|
"fail2ban/action.d/action-ban-docker-forceful-browsing.conf".text = pkgs.lib.mkDefault (pkgs.lib.mkAfter ''
|
|
|
|
[Definition]
|
|
|
|
|
2024-08-05 14:16:28 +02:00
|
|
|
actionban = ${pkgs.iptables}/bin/iptables -I DOCKER-USER -m string --algo bm --string 'X-Forwarded-For: <ip>' -j DROP
|
|
|
|
${pkgs.iptables}/bin/iptables -A INPUT -s <ip> -j DROP
|
2024-08-05 04:54:10 +02:00
|
|
|
|
2024-08-05 14:16:28 +02:00
|
|
|
actionunban = ${pkgs.iptables}/bin/iptables -D DOCKER-USER -m string --algo bm --string 'X-Forwarded-For: <ip>' -j DROP
|
|
|
|
${pkgs.iptables}/bin/iptables -D INPUT -s <ip> -j DROP
|
2024-08-05 04:54:10 +02:00
|
|
|
'');
|
2024-08-02 04:58:26 +02:00
|
|
|
};
|
|
|
|
}
|