Debian Codegraph @

doc/sources/admin/crowdsec.rst

raw
1 CrowdSec
2 ========
3
4 Presentation
5 ------------
6
7 `CrowdSec <https://crowdsec.net>`__ is a free and open-source security
8 automation tool leveraging local IP behavior detection and a
9 community-powered IP reputation system.
10
11 LL::NG provides two **Crowdsec** plugins:
12
13 * a :ref:`Bouncer` that can reject CrowdSec banned IP requests or just provide
14 an environment variable that can be used inanother plugin rule. For example,
15 a second factor may be required if user'si IP is CrowdSec banned
16 * an :ref:`Agent` that push alerts to Crowdsec server and ban IP addresses if
17 limits are reached:
18
19 * for each authentication failure *(Crowdsec scenario: llng/badcredentials)*
20 * if filters are set, for each filter alert, see :ref:`Filters<Agent>`
21 *(Crowdsec scenario: llng/urlscan)*
22
23 You can also find **CrowdSec** agents to parse webserver logs in
24 `CrowdSec hub <https://hub.crowdsec.net>`__.
25
26 Configuration
27 -------------
28
29 .. important::
30
31 If you need to whitelist something, you can use a rule in "activation" using
32 ``$env->{REMOTE_ADDR}`` for example.
33
34 .. _Bouncer:
35
36 Bouncer
37 ~~~~~~~
38
39 To configure bouncer plugin, go in ``General Parameters`` > ``Advanced Parameters`` > ``Security`` > ``CrowdSec`` > ``Bouncer``.
40
41 You can then configure:
42
43 - **Activation**: enable this plugin *(default: disabled)*
44 - **Ignore CrowdSec failures**: avoid to block user in case of CrowdSec error *(default: disabled)*
45 - **Action**: reject **or** warn and set ``$env->{CROWDSEC_REJECT} = 1``
46 - **Base URL of local API**: base URL of CrowdSec local API
47 *(default: http://localhost:8080)*
48 - **API key**: API key, usually given by ``cscli bouncers add mylemon``
49
50 .. _Agent:
51
52 Agent
53 ~~~~~
54
55 To configure agent plugin, go in ``General Parameters`` > ``Advanced Parameters`` > ``Security`` > ``CrowdSec`` > ``Agent``.
56
57 You can then configure:
58
59 - **Activation**: enable this plugin *(default: disabled)*
60 - **Crowdsec agent ID**: machine_id registered in Crowdsec local server
61 - **Password**: password
62 - **Max failures allowed during delay**: maximum failures allowed in the delay.
63 If reached, the plugin will send a CrowdSec decision *(ban)*. 0 means that
64 no ban decision will be pushed
65 - **Check delay**: number of seconds to consider failures. Default: 180
66 - **Ban duration**: duration of the ban decision sent to CrowdSec
67 *(default: 4h)*. Accepts CrowdSec duration format (e.g. 4h, 1d, 30m)
68 - **Filters directory**: directory where Crowdsec can find its filters. If
69 not defined or empty, filters are disabled. See :ref:`Filters`
70 - **Ban response code**: code for rejections based on filters *(default: 404)*
71 - **Ban response content**: override the content of rejection responses
72
73 Note that:
74
75 - Agent pushes alerts to CrowdSec. When the maximum failures threshold is
76 reached, it also pushes a ban decision that will be immediately available
77 to the **Bouncer**
78 - You must enable **Bouncer** to block banned IPs
79 - **Base URL of local API** has to be configured into ``Bouncer``.
80
81 .. _Filters:
82
83 Filters
84 ^^^^^^^
85
86 When this feature is enabled, the **CrowdsecAgent** plugin parse the directory
87 given in **Filters directory** to find files. A file must refer to:
88
89 * a :ref:`category<Categories>`
90 * a :ref:`type<Formats>`
91
92 The category is the scope of the filter *(example: "url")* and the type is the
93 format of the file *(example: "txt")*.
94 The type is the suffix of the file, there are two ways to define the category:
95 either by setting the category as second suffix in the main directory, either
96 by storing the file into a subdirectory named with the category followed by a
97 separator (``_``, ``-`` or space) and an optional description, or exactly the
98 category name. Examples:
99
100 - ``bad.url.txt`` is a file formatted as "txt" and owned by category "url"
101 - ``url_scanner/bad.re`` is a file formatted as "re" and owned by category "url"
102 - ``urlskip-icons/icons.re`` is a file formatted as "re" and owned by category "urlskip"
103
104 .. _Formats:
105
106 **Formats**
107 ***********
108
109 * **txt**: list of substring
110 * **re**: list of regular expression
111
112 An empty line or a line started by ``#`` is ignored.
113
114 Examples:
115
116 * **txt**
117
118 .. code-block::
119
120 phpinfo.php
121 config.php
122 /.htaccess
123
124 * **re**
125
126 .. code-block::
127
128 /php(my|pg|ldap)admin
129 \.exe(\?.*)?$
130
131 .. _Categories:
132
133 **Categories**
134 **************
135
136 * **url**, if the required URI matches a given expression, then:
137
138 * an alert is sent to local Crowdsec server *(scenario: llng/urlscan)*
139 * the portal sends a ``404 Not found`` response
140
141 * **urlskip**, whitelisted patterns that won't trigger alerts even if they
142 match other categories
143
144 .. _Named Scenarios:
145
146 **Named Scenarios**
147 *******************
148
149 You can define custom CrowdSec scenarios by creating a subdirectory with a
150 ``.scenario`` file. The ``.scenario`` file contains the scenario name that
151 will be sent to CrowdSec.
152
153 Example structure:
154
155 .. code-block::
156
157 crowdsec-filters/
158 ├── url/ # Legacy: uses llng/urlscan
159 │ └── bad.txt
160 ├── urlskip/ # Whitelist (no alerts)
161 │ └── allowed.txt
162 ├── http-sensitive-files/ # Named scenario
163 │ ├── .scenario # Contains: llng/http-sensitive-files
164 │ └── patterns.txt
165 └── custom-scenario/ # User-defined scenario
166 ├── .scenario # Contains: custom/my-scenario
167 └── patterns.txt
168
169 The ``.scenario`` file should contain a single line with the scenario name,
170 for example:
171
172 .. code-block::
173
174 llng/http-sensitive-files
175
176 When a URI matches a pattern in a named scenario directory, the alert sent
177 to CrowdSec will use this scenario name instead of the default ``llng/urlscan``.
178
179 **Per-scenario configuration**
180
181 You can override global settings for a specific scenario by adding special
182 files in the scenario directory:
183
184 * ``.maxfailures`` - Override ``crowdsecMaxFailures`` (integer)
185 * ``.banduration`` - Override ``crowdsecBanDuration`` (duration string: 4h, 1d, 30m, etc.)
186
187 Example:
188
189 .. code-block::
190
191 crowdsec-filters/
192 └── http-admin-probing/
193 ├── .scenario # Contains: llng/http-admin-probing
194 ├── .maxfailures # Contains: 11
195 ├── .banduration # Contains: 24h
196 └── patterns.txt
197
198 In this example, the ``http-admin-probing`` scenario will:
199
200 * Trigger a ban after 11 failures (instead of global ``crowdsecMaxFailures``)
201 * Ban IPs for 24 hours (instead of global ``crowdsecBanDuration``)
202
203 This is useful for tuning detection sensitivity per scenario type.