-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfail2ban.cpp
More file actions
350 lines (300 loc) · 11.9 KB
/
fail2ban.cpp
File metadata and controls
350 lines (300 loc) · 11.9 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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/*
* Copyright (C) 2004-2026 ZNC, see the NOTICE file for details.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <znc/znc.h>
#include <znc/User.h>
class CFailToBanMod : public CModule {
public:
MODCONSTRUCTOR(CFailToBanMod) {
AddHelpCommand();
AddCommand(
"Timeout", t_d("[minutes]"),
t_d("The number of minutes IPs are blocked after a failed login."),
[=](const CString& sLine) { OnTimeoutCommand(sLine); });
AddCommand("Attempts", t_d("[count]"),
t_d("The number of allowed failed login attempts."),
[=](const CString& sLine) { OnAttemptsCommand(sLine); });
AddCommand("Ban", t_d("<hosts>"), t_d("Ban the specified hosts."),
[=](const CString& sLine) { OnBanCommand(sLine); });
AddCommand("Unban", t_d("<ID>|<hosts>"),
t_d("Unban the specified ID or host."),
[=](const CString& sLine) { OnUnbanCommand(sLine); });
AddCommand("List", "", t_d("List banned hosts."),
[=](const CString& sLine) { OnListCommand(sLine); });
}
~CFailToBanMod() override {}
bool OnLoad(const CString& sArgs, CString& sMessage) override {
CString sTimeout = sArgs.Token(0);
CString sAttempts = sArgs.Token(1);
unsigned int timeout = sTimeout.ToUInt();
if (sAttempts.empty())
m_uiAllowedFailed = 2;
else
m_uiAllowedFailed = sAttempts.ToUInt();
if (sArgs.empty()) {
timeout = 1;
} else if (timeout == 0 || m_uiAllowedFailed == 0 ||
!sArgs.Token(2, true).empty()) {
sMessage =
t_s("Invalid argument, must be the number of minutes IPs are "
"blocked after a failed login and can be followed by "
"number of allowed failed login attempts");
return false;
}
// SetTTL() wants milliseconds
m_Cache.SetTTL(timeout * 60 * 1000);
return true;
}
void OnPostRehash() override { m_Cache.Clear(); }
void Add(const CString& sHost, unsigned int count) {
time_t now = time(nullptr);
BanInfo* pInfo = m_Cache.GetItem(sHost);
if (pInfo) {
pInfo->attempts = count;
pInfo->last_attempt = now;
// Keep the original first_attempt time
m_Cache.AddItem(sHost, *pInfo, m_Cache.GetTTL());
} else {
BanInfo info = {count, now, now};
m_Cache.AddItem(sHost, info, m_Cache.GetTTL());
}
}
bool Remove(const CString& sHost) { return m_Cache.RemItem(sHost); }
void OnTimeoutCommand(const CString& sCommand) {
if (!GetUser()->IsAdmin()) {
PutModule(t_s("Access denied"));
return;
}
CString sArg = sCommand.Token(1);
if (!sArg.empty()) {
unsigned int uTimeout = sArg.ToUInt();
if (uTimeout == 0) {
PutModule(t_s("Usage: Timeout [minutes]"));
} else {
m_Cache.SetTTL(uTimeout * 60 * 1000);
SetArgs(CString(m_Cache.GetTTL() / 60 / 1000) + " " +
CString(m_uiAllowedFailed));
PutModule(t_f("Timeout: {1} min")(uTimeout));
}
} else {
PutModule(t_f("Timeout: {1} min")(m_Cache.GetTTL() / 60 / 1000));
}
}
void OnAttemptsCommand(const CString& sCommand) {
if (!GetUser()->IsAdmin()) {
PutModule(t_s("Access denied"));
return;
}
CString sArg = sCommand.Token(1);
if (!sArg.empty()) {
unsigned int uiAttempts = sArg.ToUInt();
if (uiAttempts == 0) {
PutModule(t_s("Usage: Attempts [count]"));
} else {
m_uiAllowedFailed = uiAttempts;
SetArgs(CString(m_Cache.GetTTL() / 60 / 1000) + " " +
CString(m_uiAllowedFailed));
PutModule(t_f("Attempts: {1}")(uiAttempts));
}
} else {
PutModule(t_f("Attempts: {1}")(m_uiAllowedFailed));
}
}
void OnBanCommand(const CString& sCommand) {
if (!GetUser()->IsAdmin()) {
PutModule(t_s("Access denied"));
return;
}
CString sHosts = sCommand.Token(1, true);
if (sHosts.empty()) {
PutStatus(t_s("Usage: Ban <hosts>"));
return;
}
VCString vsHosts;
sHosts.Replace(",", " ");
sHosts.Split(" ", vsHosts, false, "", "", true, true);
for (const CString& sHost : vsHosts) {
// Ban with max attempts to ensure they're blocked
Add(sHost, m_uiAllowedFailed);
PutModule(t_f("Banned: {1}")(sHost));
}
}
void OnUnbanCommand(const CString& sCommand) {
if (!GetUser()->IsAdmin()) {
PutModule(t_s("Access denied"));
return;
}
CString sTargets = sCommand.Token(1, true);
if (sTargets.empty()) {
PutModule(t_s("Usage: Unban <hosts|IDs>"));
return;
}
VCString vsTargets;
sTargets.Replace(",", " ");
sTargets.Split(" ", vsTargets, false, "", "", true, true);
for (const CString& sTarget : vsTargets) {
bool bUnbanned = false;
// Check if it's a numeric ID by trying to convert and validating
unsigned int targetId = sTarget.ToUInt();
if (targetId > 0 && CString(targetId) == sTarget) {
// Find host by ID
unsigned int currentId = 1;
for (const auto& it : m_Cache.GetItems()) {
if (currentId == targetId) {
if (Remove(it.first)) {
PutModule(t_f("Unbanned ID {1} ({2})")(targetId,
it.first));
bUnbanned = true;
}
break;
}
currentId++;
}
} else {
// Treat as hostname/IP
if (Remove(sTarget)) {
PutModule(t_f("Unbanned: {1}")(sTarget));
bUnbanned = true;
}
}
if (!bUnbanned) {
PutModule(t_f("{1} is not banned.")(sTarget));
}
}
}
void OnListCommand(const CString& sCommand) {
if (!GetUser()->IsAdmin()) {
PutModule(t_s("Access denied"));
return;
}
time_t now = time(nullptr);
CTable Table;
Table.AddColumn(t_s("ID", "list"));
Table.AddColumn(t_s("Username", "list"));
Table.AddColumn(t_s("Host", "list"));
Table.AddColumn(t_s("Attempts", "list"));
Table.AddColumn(t_s("First Seen", "list"));
Table.AddColumn(t_s("Last Seen", "list"));
Table.AddColumn(t_s("Expires In", "list"));
Table.SetStyle(CTable::ListStyle);
unsigned int id = 1;
for (const auto& it : m_Cache.GetItems()) {
Table.AddRow();
Table.SetCell(t_s("ID", "list"), CString(id++));
Table.SetCell(t_s("Username", "list"), it.second.username);
Table.SetCell(t_s("Host", "list"), it.first);
Table.SetCell(t_s("Attempts", "list"), CString(it.second.attempts));
// Format timestamps
CString sFirstSeen =
CUtils::FormatTime(it.second.first_attempt, "%Y-%m-%d %H:%M:%S",
GetUser()->GetTimezone());
CString sLastSeen =
CUtils::FormatTime(it.second.last_attempt, "%Y-%m-%d %H:%M:%S",
GetUser()->GetTimezone());
Table.SetCell(t_s("First Seen", "list"), sFirstSeen);
Table.SetCell(t_s("Last Seen", "list"), sLastSeen);
// Calculate remaining time
time_t ttl_seconds =
m_Cache.GetTTL() / 1000; // Convert ms to seconds
time_t elapsed = now - it.second.last_attempt;
time_t remaining = ttl_seconds - elapsed;
CString sCountdown;
if (remaining <= 0) {
sCountdown = "Expired";
} else {
sCountdown = FormatDuration(remaining);
}
Table.SetCell(t_s("Expires In", "list"), sCountdown);
}
if (Table.empty()) {
PutModule(t_s("No bans", "list"));
} else {
PutModule(Table);
}
}
void OnClientConnect(CZNCSock* pClient, const CString& sHost,
unsigned short uPort) override {
BanInfo* pInfo = m_Cache.GetItem(sHost);
if (sHost.empty() || pInfo == nullptr ||
pInfo->attempts < m_uiAllowedFailed) {
return;
}
// Refresh their ban with updated timestamp
time_t now = time(nullptr);
pInfo->last_attempt = now;
m_Cache.AddItem(sHost, *pInfo, m_Cache.GetTTL());
pClient->Write(
"ERROR :Closing link [Please try again later - reconnecting too "
"fast]\r\n");
pClient->Close(Csock::CLT_AFTERWRITE);
}
void OnFailedLogin(const CString& sUsername,
const CString& sRemoteIP) override {
time_t now = time(nullptr);
BanInfo* pInfo = m_Cache.GetItem(sRemoteIP);
if (pInfo) {
pInfo->attempts++;
pInfo->last_attempt = now;
pInfo->username = sUsername; // Update username
m_Cache.AddItem(sRemoteIP, *pInfo, m_Cache.GetTTL());
} else {
BanInfo info = {1, now, now, sUsername}; // Include username
m_Cache.AddItem(sRemoteIP, info, m_Cache.GetTTL());
}
}
void OnClientLogin() override { Remove(GetClient()->GetRemoteIP()); }
EModRet OnLoginAttempt(std::shared_ptr<CAuthBase> Auth) override {
const CString& sRemoteIP = Auth->GetRemoteIP();
if (sRemoteIP.empty()) return CONTINUE;
BanInfo* pInfo = m_Cache.GetItem(sRemoteIP);
if (pInfo && pInfo->attempts >= m_uiAllowedFailed) {
// OnFailedLogin() will refresh their ban
Auth->RefuseLogin("Please try again later - reconnecting too fast");
return HALT;
}
return CONTINUE;
}
CString FormatDuration(time_t seconds) {
if (seconds <= 0) return "0s";
time_t hours = seconds / 3600;
seconds %= 3600;
time_t minutes = seconds / 60;
seconds %= 60;
CString result;
if (hours > 0) result += CString(hours) + "h";
if (minutes > 0) result += CString(minutes) + "m";
if (seconds > 0 || result.empty()) result += CString(seconds) + "s";
return result;
}
private:
struct BanInfo {
unsigned int attempts;
time_t first_attempt;
time_t last_attempt;
CString username;
};
TCacheMap<CString, BanInfo> m_Cache;
unsigned int m_uiAllowedFailed{};
};
template <>
void TModInfo<CFailToBanMod>(CModInfo& Info) {
Info.SetWikiPage("fail2ban");
Info.SetHasArgs(true);
Info.SetArgsHelpText(
Info.t_s("You might enter the time in minutes for the IP banning and "
"the number of failed logins before any action is taken."));
}
GLOBALMODULEDEFS(CFailToBanMod,
t_s("Block IPs for some time after a failed login."))