-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathreaction.nix
More file actions
222 lines (207 loc) · 5.57 KB
/
Copy pathreaction.nix
File metadata and controls
222 lines (207 loc) · 5.57 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
{
lib,
config,
...
}:
let
ignore_ip = [
"127.0.0.1"
"::1"
"90.113.129.48"
];
ban_scan = 24 * 7;
sudo = "/run/wrappers/bin/sudo";
ip4tables = "${config.networking.firewall.package}/bin/iptables";
ip6tables = "${config.networking.firewall.package}/bin/ip6tables";
in
{
# Source: https://framagit.org/ppom/nixos/-/blob/main/modules/common/reaction-custom.nix
config =
let
iptablesBan = cmd: [
sudo
cmd
"-w"
"-A"
"reaction"
"-s"
"<ip>"
"-j"
"DROP"
];
iptablesUnban = cmd: [
sudo
cmd
"-w"
"-D"
"reaction"
"-s"
"<ip>"
"-j"
"DROP"
];
banFor = duration: {
ban4 = {
cmd = iptablesBan ip4tables;
ipv4only = true;
};
ban6 = {
cmd = iptablesBan ip6tables;
ipv6only = true;
};
unban4 = {
cmd = iptablesUnban ip4tables;
ipv4only = true;
after = duration;
};
unban6 = {
cmd = iptablesUnban ip6tables;
ipv6only = true;
after = duration;
};
};
in
{
users.users.reaction = {
extraGroups = [
"systemd-journal"
"vector"
];
};
# Allow reaction to manage iptables rules
security.sudo.execWheelOnly = lib.mkForce false;
security.sudo.extraRules = [
{
users = [ "reaction" ];
commands = [
{
command = "${config.networking.firewall.package}/bin/iptables";
options = [
"NOPASSWD"
];
}
{
command = "${config.networking.firewall.package}/bin/ip6tables";
options = [
"NOPASSWD"
];
}
];
runAs = "root";
}
];
services.reaction = {
enable = true;
runAsRoot = false;
settings = {
patterns = {
ip = {
type = "ip";
ipv6mask = 64;
ignore = ignore_ip;
};
};
streams = {
vector = {
cmd = [
"tail"
"-n0"
"-F"
"/var/lib/vector/logs/reaction.logfmt"
];
filters = {
port_scan = {
regex = [
"ip=<ip>.*risk_type=.?port scan.?.*service=iptables"
];
retry = 3;
retryperiod = "4h";
actions = banFor "${toString ban_scan}h";
};
login_fail = {
regex = [
"ip=<ip>.*risk_type=.?login fail.*service=sshd"
];
retry = 3;
retryperiod = "4h";
actions = banFor "${toString ban_scan}h";
};
ddos = {
regex = [
"ip=<ip>.*risk_type=DDOS.*service=sshd"
];
retry = 3;
retryperiod = "4h";
actions = banFor "${toString ban_scan}h";
};
http_exploit = {
regex = [
"ip=<ip>.*risk_type=.?HTTP exploit.?.*service=nginx_logs"
];
retry = 2;
retryperiod = "4h";
actions = banFor "${toString ban_scan}h";
};
ai_bot = {
regex = [
"ip=<ip>.*risk_type=.?AI bot.?.*service=nginx_logs"
];
retry = 2;
retryperiod = "4h";
actions = banFor "${toString ban_scan}h";
};
};
};
};
};
};
# Create the reaction chain in iptables when the service starts
systemd.services.reaction = {
serviceConfig =
let
mkIptablesCmd = table: args: "+${sudo} ${table} ${args}";
ip46tables = args: [
(mkIptablesCmd ip4tables args)
(mkIptablesCmd ip6tables args)
];
in
{
ExecStartPre = builtins.concatMap ip46tables [
"-w -N reaction"
"-w -I INPUT -p all -j reaction"
"-w -I FORWARD -p all -j reaction"
];
ExecStopPost = builtins.concatMap ip46tables [
"-w -D INPUT -p all -j reaction"
"-w -D FORWARD -p all -j reaction"
"-w -F reaction"
"-w -X reaction"
];
TimeoutStopSec = "3min";
UMask = "0002";
};
};
# Verify that the reaction chain exists in iptables every 10 minutes
systemd.services.reaction-chain =
let
message = chain: "reaction chain has been removed from ${chain} :o";
in
{
enable = true;
startAt = "*:0/10";
unitConfig.Requisite = [ "reaction.service" ];
script = ''
if ! ${sudo} ${ip4tables} -L INPUT | grep -q reaction
then
${sudo} ${ip4tables} -w -I INPUT -p all -j reaction
echo ${message "INPUT"}
fi
if ! ${sudo} ${ip4tables} -L FORWARD | grep -q reaction
then
${sudo} ${ip4tables} -w -I FORWARD -p all -j reaction
echo ${message "FORWARD"}
fi
'';
};
};
}