Subversion Repositories configs

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
192 - 1
# Fail2Ban configuration file for black-listing via nginx
2
#
3
# Author: Serg G. Brester (aka sebres)
4
#
5
# To use 'nginx-block-map' action you should define some special blocks in your nginx configuration,
6
# and use it hereafter in your locations (to notify fail2ban by failure, resp. nginx by ban).
7
#
8
# Example (argument "token_id" resp. cookie "session_id" used here as unique identifier for user):
9
#
10
#   http {
11
#     ...
12
#     # maps to check user is blacklisted (banned in f2b):
13
#     #map $arg_token_id      $blck_lst_tok { include blacklisted-tokens.map; }
14
#     map  $cookie_session_id $blck_lst_ses { include blacklisted-sessions.map; }
15
#     ...
16
#     # special log-format to notify fail2ban about failures:
17
#     log_format f2b_session_errors '$msec failure "$cookie_session_id" - $remote_addr - $remote_user '
18
#      ;#                  '"$request" $status $bytes_sent '
19
#       #                  '"$http_referer" "$http_user_agent"';
20
#
21
#     # location checking blacklisted values:
22
#     location ... {
23
#       # check banned sessionid:
24
#       if ($blck_lst_ses != "") {
25
#         try_files "" @f2b-banned;
26
#       }
27
#       ...
28
#       # notify fail2ban about a failure inside nginx:
29
#       error_page 401 = @notify-f2b;
30
#       ...
31
#     }
32
#     ...
33
#     # location for return with "403 Forbidden" if banned:
34
#     location @f2b-banned {
35
#       default_type text/html;
36
#       return 403 "<br/><center>
37
#         <b style=\"color:red; font-size:18pt; border:2pt solid black; padding:5pt;\">
38
#         You are banned!</b></center>";
39
#     }
40
#     ...
41
#     # location to notify fail2ban about a failure inside nginx:
42
#     location @notify-f2b {
43
#       access_log /var/log/nginx/f2b-auth-errors.log f2b_session_errors;
44
#     }
45
#   }
46
#   ...
47
#
48
# Note that quote-character (and possibly other special characters) are not allowed currently as session-id.
49
# Thus please add any session-id validation rule in your locations (or in the corresponding backend-service),
50
# like in example below:
51
#
52
#   location ... {
53
#     if ($cookie_session_id !~ "^[\w\-]+$") {
54
#       return  403 "Wrong session-id"
55
#     }
56
#     ...
57
#   }
58
#
59
# The parameters for jail corresponding log-format (f2b_session_errors):
60
#
61
#   [nginx-blck-lst]
62
#   filter =
63
#   datepattern = ^Epoch
64
#   failregex = ^ failure "<F-ID>[^"]+</F-ID>" - <ADDR>
65
#   usedns = no
66
#
67
# The same log-file can be used for IP-related jail (additionally to session-related, to ban very bad IPs):
68
#
69
#   [nginx-blck-ip]
70
#   maxretry = 100
71
#   filter =
72
#   datepattern = ^Epoch
73
#   failregex = ^ failure "[^"]+" - <ADDR>
74
#   usedns = no
75
#
76
 
77
[Definition]
78
 
79
# path to configuration of nginx (used to target nginx-instance in multi-instance system,
80
# and as path for the blacklisted map):
81
srv_cfg_path = /etc/nginx/
82
 
83
# cmd-line arguments to supply to test/reload nginx:
84
#srv_cmd = nginx -c %(srv_cfg_path)s/nginx.conf
85
srv_cmd = nginx
86
 
87
# first test configuration is correct, hereafter send reload signal:
88
blck_lst_reload = %(srv_cmd)s -qt; if [ $? -eq 0 ]; then
89
                    %(srv_cmd)s -s reload; if [ $? -ne 0 ]; then echo 'reload failed.'; fi;
90
                  fi;
91
 
92
# map-file for nginx, can be redefined using `action = nginx-block-map[blck_lst_file="/path/file.map"]`:
93
blck_lst_file = %(srv_cfg_path)s/blacklisted-sessions.map
94
 
95
# Action definition:
96
 
97
actionstart_on_demand = false
98
actionstart = touch '%(blck_lst_file)s'
99
 
100
actionflush = truncate -s 0 '%(blck_lst_file)s'; %(blck_lst_reload)s
101
 
102
actionstop = %(actionflush)s
103
 
104
actioncheck =
105
 
106
_echo_blck_row = printf '\%%s 1;\n' "<fid>"
107
 
108
actionban = %(_echo_blck_row)s >> '%(blck_lst_file)s'; %(blck_lst_reload)s
109
 
110
actionunban = id=$(%(_echo_blck_row)s | sed -e 's/[]\/$*.^|[]/\\&/g'); sed -i "/^$id$/d" %(blck_lst_file)s; %(blck_lst_reload)s