@profullstack/threatcrush 0.13.22 → 0.13.24
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/daemon.js +66 -22
- package/dist/daemon.js.map +1 -1
- package/dist/index.js +539 -413
- package/dist/index.js.map +1 -1
- package/dist/systemd/threatcrushd.service +7 -1
- package/package.json +1 -1
package/dist/daemon.js
CHANGED
|
@@ -16129,6 +16129,19 @@ var RemediationConsumer = class {
|
|
|
16129
16129
|
};
|
|
16130
16130
|
|
|
16131
16131
|
// src/daemon/rules/engine.ts
|
|
16132
|
+
var MAX_WINDOW_ENTRIES = 1e3;
|
|
16133
|
+
function live(window) {
|
|
16134
|
+
return window.times.length - window.head;
|
|
16135
|
+
}
|
|
16136
|
+
function expire(window, now) {
|
|
16137
|
+
const cutoff = now - window.windowMs;
|
|
16138
|
+
while (window.head < window.times.length && window.times[window.head] < cutoff) window.head++;
|
|
16139
|
+
if (window.head > 64 && window.head * 2 > window.times.length) {
|
|
16140
|
+
window.times = window.times.slice(window.head);
|
|
16141
|
+
window.ips = window.ips.slice(window.head);
|
|
16142
|
+
window.head = 0;
|
|
16143
|
+
}
|
|
16144
|
+
}
|
|
16132
16145
|
var RuleEngine = class {
|
|
16133
16146
|
constructor(onDetection) {
|
|
16134
16147
|
this.onDetection = onDetection;
|
|
@@ -16154,27 +16167,42 @@ var RuleEngine = class {
|
|
|
16154
16167
|
const windowKey = `${rule.id}:${groupBy}:${groupValue}`;
|
|
16155
16168
|
let window = this.windows.get(windowKey);
|
|
16156
16169
|
if (!window) {
|
|
16157
|
-
window = {
|
|
16170
|
+
window = {
|
|
16171
|
+
times: [],
|
|
16172
|
+
ips: [],
|
|
16173
|
+
head: 0,
|
|
16174
|
+
lastAlert: 0,
|
|
16175
|
+
windowMs: rule.window_seconds * 1e3,
|
|
16176
|
+
cooldownMs: rule.cooldown_seconds * 1e3
|
|
16177
|
+
};
|
|
16158
16178
|
this.windows.set(windowKey, window);
|
|
16159
16179
|
}
|
|
16160
|
-
window.
|
|
16161
|
-
|
|
16162
|
-
window
|
|
16163
|
-
|
|
16164
|
-
|
|
16165
|
-
|
|
16166
|
-
|
|
16167
|
-
|
|
16168
|
-
|
|
16180
|
+
window.times.push(now);
|
|
16181
|
+
window.ips.push(event.source_ip);
|
|
16182
|
+
expire(window, now);
|
|
16183
|
+
const cap = Math.max(rule.threshold, MAX_WINDOW_ENTRIES);
|
|
16184
|
+
let capped = false;
|
|
16185
|
+
if (live(window) > cap) {
|
|
16186
|
+
window.head = window.times.length - cap;
|
|
16187
|
+
capped = true;
|
|
16188
|
+
expire(window, now);
|
|
16189
|
+
}
|
|
16190
|
+
if (live(window) < rule.threshold) continue;
|
|
16191
|
+
if (window.lastAlert > 0 && now - window.lastAlert < window.cooldownMs) continue;
|
|
16192
|
+
const hits = live(window);
|
|
16193
|
+
const distinctIps = new Set(window.ips.slice(window.head).filter(Boolean)).size;
|
|
16169
16194
|
window.lastAlert = now;
|
|
16170
|
-
window.
|
|
16195
|
+
window.times = [];
|
|
16196
|
+
window.ips = [];
|
|
16197
|
+
window.head = 0;
|
|
16198
|
+
const hitsLabel = capped ? `${hits}+` : String(hits);
|
|
16171
16199
|
const aggregate = groupBy !== "source_ip";
|
|
16172
16200
|
const spread = aggregate ? ` from ${distinctIps} address${distinctIps === 1 ? "" : "es"}${groupBy === "endpoint" ? ` against ${groupValue}` : ""}` : "";
|
|
16173
16201
|
this.onDetection({
|
|
16174
16202
|
rule_id: rule.id,
|
|
16175
16203
|
severity: rule.severity,
|
|
16176
16204
|
title: rule.title,
|
|
16177
|
-
description: `${rule.description} (${
|
|
16205
|
+
description: `${rule.description} (${hitsLabel} events in ${rule.window_seconds}s${spread})`,
|
|
16178
16206
|
source_ip: aggregate ? void 0 : event.source_ip,
|
|
16179
16207
|
username: event.details?.user || void 0,
|
|
16180
16208
|
raw_metadata: {
|
|
@@ -16237,14 +16265,26 @@ var RuleEngine = class {
|
|
|
16237
16265
|
return event.details?.[field];
|
|
16238
16266
|
}
|
|
16239
16267
|
}
|
|
16240
|
-
|
|
16241
|
-
|
|
16242
|
-
|
|
16243
|
-
|
|
16244
|
-
|
|
16268
|
+
/**
|
|
16269
|
+
* Expire every window by its own rule's length, and forget windows with
|
|
16270
|
+
* nothing left in them once their cooldown is over (a cooldown must outlive
|
|
16271
|
+
* an empty window, or a source could re-trigger the moment it was forgotten).
|
|
16272
|
+
* Without the expiry, a window whose address never came back was kept forever.
|
|
16273
|
+
*/
|
|
16274
|
+
cleanup(now = Date.now()) {
|
|
16275
|
+
let removed = 0;
|
|
16276
|
+
for (const [key, window] of this.windows) {
|
|
16277
|
+
expire(window, now);
|
|
16278
|
+
if (live(window) === 0 && (window.lastAlert === 0 || now - window.lastAlert >= window.cooldownMs)) {
|
|
16245
16279
|
this.windows.delete(key);
|
|
16280
|
+
removed++;
|
|
16246
16281
|
}
|
|
16247
16282
|
}
|
|
16283
|
+
return removed;
|
|
16284
|
+
}
|
|
16285
|
+
/** How many windows are open, for tests and the status report. */
|
|
16286
|
+
windowCount() {
|
|
16287
|
+
return this.windows.size;
|
|
16248
16288
|
}
|
|
16249
16289
|
};
|
|
16250
16290
|
|
|
@@ -17029,6 +17069,10 @@ function isSuccessfulAuth(message) {
|
|
|
17029
17069
|
}
|
|
17030
17070
|
function explainBlockFailure(err, backend) {
|
|
17031
17071
|
const raw = err.message || String(err);
|
|
17072
|
+
if (/EROFS|read-only file system/i.test(raw)) {
|
|
17073
|
+
const path = /open '([^']+)'/.exec(raw)?.[1];
|
|
17074
|
+
return `threatcrushd cannot write ${path ?? "its firewall config"} for ${backend}: the systemd unit mounts it read-only. Reinstall the unit and restart: sudo $(which threatcrush) install-service && sudo systemctl restart threatcrushd`;
|
|
17075
|
+
}
|
|
17032
17076
|
const denied = /not permitted|EACCES|EPERM|permission denied|must be root/i.test(raw);
|
|
17033
17077
|
if (!denied) return `${raw} (backend: ${backend})`;
|
|
17034
17078
|
const who = typeof process.getuid === "function" && process.getuid() === 0 ? "as root" : `as uid ${typeof process.getuid === "function" ? process.getuid() : "?"}`;
|
|
@@ -17282,7 +17326,7 @@ var RemediationManager = class {
|
|
|
17282
17326
|
*/
|
|
17283
17327
|
async reconcile() {
|
|
17284
17328
|
const now = Date.now();
|
|
17285
|
-
const
|
|
17329
|
+
const live2 = this.blocklist.filter((b) => b.expires_at > now);
|
|
17286
17330
|
const stale = this.blocklist.filter((b) => b.expires_at <= now);
|
|
17287
17331
|
for (const entry of stale) {
|
|
17288
17332
|
if (!entry.dry_run) {
|
|
@@ -17292,7 +17336,7 @@ var RemediationManager = class {
|
|
|
17292
17336
|
}
|
|
17293
17337
|
}
|
|
17294
17338
|
}
|
|
17295
|
-
this.blocklist =
|
|
17339
|
+
this.blocklist = live2;
|
|
17296
17340
|
this.saveState();
|
|
17297
17341
|
let applied = null;
|
|
17298
17342
|
try {
|
|
@@ -17300,7 +17344,7 @@ var RemediationManager = class {
|
|
|
17300
17344
|
} catch {
|
|
17301
17345
|
}
|
|
17302
17346
|
if (applied) {
|
|
17303
|
-
const known = new Set(
|
|
17347
|
+
const known = new Set(live2.filter((b) => !b.dry_run).map((b) => b.ip));
|
|
17304
17348
|
for (const ip of applied) {
|
|
17305
17349
|
if (known.has(ip)) continue;
|
|
17306
17350
|
this.logLine(`[firewall] reconcile: dropping stale rule for ${ip} (no record of why)`);
|
|
@@ -17311,7 +17355,7 @@ var RemediationManager = class {
|
|
|
17311
17355
|
}
|
|
17312
17356
|
}
|
|
17313
17357
|
if (this.config.dry_run) return;
|
|
17314
|
-
for (const entry of
|
|
17358
|
+
for (const entry of live2) {
|
|
17315
17359
|
if (applied?.includes(entry.ip)) continue;
|
|
17316
17360
|
try {
|
|
17317
17361
|
await this.adapter.block(entry.ip);
|
|
@@ -17795,7 +17839,7 @@ async function runDaemon() {
|
|
|
17795
17839
|
if (event.module !== "rule-engine") ruleEngine.evaluate(event);
|
|
17796
17840
|
});
|
|
17797
17841
|
logLine(`[daemon] rule engine loaded ${ruleEngine.getRules().length} rules`);
|
|
17798
|
-
setInterval(() => ruleEngine.cleanup(),
|
|
17842
|
+
setInterval(() => ruleEngine.cleanup(), 6e4);
|
|
17799
17843
|
const settings = remediationSettings(config.remediation);
|
|
17800
17844
|
const firewallAdapter = detectFirewallAdapter(config.remediation?.backend ?? "auto");
|
|
17801
17845
|
const remediation = new RemediationManager(firewallAdapter, bus, settings);
|