-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsuricata.nix
More file actions
308 lines (284 loc) · 7.33 KB
/
Copy pathsuricata.nix
File metadata and controls
308 lines (284 loc) · 7.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
{ config, pkgs, lib, ... }:
let
suricataUpdatePython = pkgs.python313.withPackages (ps: [
ps.pyyaml
ps.requests
ps.pygments
]);
in
{
users.users.suricata = {
isSystemUser = true;
group = "suricata";
description = "Suricata IDS/IPS";
};
users.groups.suricata = { };
systemd.tmpfiles.rules = [
"d /var/log/suricata 0750 suricata suricata -"
"d /var/lib/suricata 0750 suricata suricata -"
"d /var/lib/suricata/rules 0750 suricata suricata -"
];
environment.etc."suricata/suricata.yaml" = {
user = "suricata";
group = "suricata";
mode = "0640";
text = ''
%YAML 1.1
---
vars:
address-groups:
HOME_NET: "[192.168.0.0/16,10.0.0.0/24,172.16.0.0/24,100.64.0.0/24]"
EXTERNAL_NET: "!$HOME_NET"
HTTP_SERVERS: "$HOME_NET"
SQL_SERVERS: "$HOME_NET"
DNS_SERVERS: "$HOME_NET"
TELNET_SERVERS: "$HOME_NET"
AIM_SERVERS: "$EXTERNAL_NET"
DC_SERVERS: "$HOME_NET"
DNP3_SERVER: "$HOME_NET"
DNP3_CLIENT: "$HOME_NET"
MODBUS_CLIENT: "$HOME_NET"
MODBUS_SERVER: "$HOME_NET"
ENIP_CLIENT: "$HOME_NET"
ENIP_SERVER: "$HOME_NET"
port-groups:
HTTP_PORTS: "80"
SHELLCODE_PORTS: "!80"
SSH_PORTS: "22"
ORACLE_PORTS: "1521"
FTP_PORTS: "21"
DNP3_PORTS: "20000"
MODBUS_PORTS: "502"
FILE_DATA_PORTS: "[80,110,143]"
GENEVE_PORTS: "6081"
VXLAN_PORTS: "4789"
TEREDO_PORTS: "3544"
VAULTWARDEN_PORTS: "8222"
default-log-dir: /var/log/suricata
stats:
enabled: yes
interval: 8
outputs:
- eve-log:
enabled: yes
filetype: regular
filename: /var/log/suricata/eve.json
community-id: yes
community-id-seed: 0
types:
- alert:
payload: yes
payload-buffer-size: 4kb
payload-printable: yes
packet: yes
metadata: yes
tagged-packets: yes
- anomaly:
enabled: yes
types:
decode: yes
stream: yes
applayer: yes
- http:
extended: yes
- dns:
version: 2
- tls:
extended: yes
- files:
force-magic: no
- smtp: {}
- ftp: {}
- rdp: {}
- nfs: {}
- smb: {}
- ssh: {}
- flow: {}
- netflow: {}
- drop: {}
- fast:
enabled: yes
filename: /var/log/suricata/fast.log
append: yes
- stats:
enabled: yes
filename: /var/log/suricata/stats.log
append: yes
totals: yes
threads: no
logging:
default-log-level: notice
outputs:
- console:
enabled: no
- file:
enabled: yes
filename: /var/log/suricata/suricata.log
level: info
- syslog:
enabled: no
nfq:
- id: 0
- id: 1
- id: 2
- id: 3
stream:
memcap: 256mb
checksum-validation: yes
inline: yes
reassembly:
memcap: 256mb
depth: 1mb
toserver-chunk-size: 2560
toclient-chunk-size: 2560
randomize-chunk-size: yes
app-layer:
protocols:
tls:
enabled: yes
http:
enabled: yes
http2:
enabled: yes
ftp:
enabled: yes
smtp:
enabled: yes
ssh:
enabled: yes
dns:
enabled: yes
rdp:
enabled: yes
nfs:
enabled: yes
smb:
enabled: yes
krb5:
enabled: yes
dhcp:
enabled: yes
ike:
enabled: yes
modbus:
enabled: no
dnp3:
enabled: no
detect:
profile: medium
custom-values:
toclient-groups: 3
toserver-groups: 25
sgh-mpm-context: auto
inspection-recursion-limit: 3000
threading:
set-cpu-affinity: no
detect-thread-ratio: 1.0
default-rule-path: /var/lib/suricata/rules
rule-files:
- suricata.rules
classification-file: ${pkgs.suricata}/etc/suricata/classification.config
reference-config-file: ${pkgs.suricata}/etc/suricata/reference.config
unix-command:
enabled: yes
filename: /run/suricata/suricata-command.socket
'';
};
environment.etc."suricata/disable.conf" = {
user = "suricata";
group = "suricata";
mode = "0640";
text = ''
re:modbus
re:dnp3
'';
};
systemd.services.suricata = {
description = "Suricata IDS/IPS (NFQ mode)";
after = [ "network-online.target" "nftables.service" "suricata-update.service" ];
wants = [ "network-online.target" ];
wantedBy = [ "multi-user.target" ];
serviceConfig = {
Type = "simple";
PIDFile = "/run/suricata/suricata.pid";
RuntimeDirectory = "suricata";
RuntimeDirectoryMode = "0750";
User = "suricata";
Group = "suricata";
AmbientCapabilities = [ "CAP_NET_ADMIN" "CAP_NET_RAW" "CAP_SYS_NICE" ];
CapabilityBoundingSet = [ "CAP_NET_ADMIN" "CAP_NET_RAW" "CAP_SYS_NICE" ];
ExecStartPre = "${pkgs.suricata}/bin/suricata -c /etc/suricata/suricata.yaml -T";
ExecStart = "${pkgs.suricata}/bin/suricata -c /etc/suricata/suricata.yaml --pidfile /run/suricata/suricata.pid -q 0 -q 1 -q 2 -q 3";
ExecReload = "${pkgs.suricata}/bin/suricatasc -c reload-rules";
Restart = "on-failure";
RestartSec = "5s";
};
};
systemd.services.suricata-update = {
description = "Update Suricata Rules";
after = [ "network-online.target" ];
wants = [ "network-online.target" ];
before = [ "suricata.service" ];
wantedBy = [ "suricata.service" ];
serviceConfig = {
Type = "oneshot";
User = "suricata";
ExecStart = "${suricataUpdatePython}/bin/python3 ${pkgs.suricata}/bin/suricata-update --suricata-conf /etc/suricata/suricata.yaml --disable-conf /etc/suricata/disable.conf --no-reload";
StateDirectory = "suricata";
};
};
systemd.timers.suricata-update = {
description = "Daily Suricata rule update";
wantedBy = [ "timers.target" ];
timerConfig = {
OnCalendar = "03:00:00";
RandomizedDelaySec = "30m";
Persistent = true;
};
};
# NFQ nftables hooks — intercept all traffic except trusted interfaces
networking.nftables.tables.suricata-ips = {
family = "inet";
content = ''
chain suricata-forward {
type filter hook forward priority -1; policy accept;
iifname "wg0" accept
oifname "wg0" accept
iifname "tailscale0" accept
oifname "tailscale0" accept
counter queue num 0-3 bypass
}
chain suricata-input {
type filter hook input priority -1; policy accept;
iifname "wg0" accept
iifname "tailscale0" accept
counter queue num 0-3 bypass
}
chain suricata-output {
type filter hook output priority -1; policy accept;
oifname "wg0" accept
oifname "tailscale0" accept
counter queue num 0-3 bypass
}
'';
};
# fail2ban jail for Suricata alerts
services.fail2ban.jails.suricata = {
settings = {
enabled = true;
filter = "suricata";
logpath = "/var/log/suricata/fast.log";
action = "nftables-multiport[name=suricata, port=\"1:65535\", protocol=tcp]";
maxretry = 3;
findtime = 300;
bantime = 3600;
};
};
environment.etc."fail2ban/filter.d/suricata.conf".text = ''
[Definition]
failregex = ^\d+/\d+/\d+-\d+:\d+:\d+\.\d+ \[\*\*\] .+ \[\*\*\] .+ \{TCP\} <HOST>:\d+ -> .+$
^\d+/\d+/\d+-\d+:\d+:\d+\.\d+ \[\*\*\] .+ \[\*\*\] .+ \{UDP\} <HOST>:\d+ -> .+$
ignoreregex =
datepattern = %%m/%%d/%%Y-%%H:%%M:%%S.%%f
'';
}