@profullstack/threatcrush 0.13.22 → 0.13.23
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 +62 -22
- package/dist/daemon.js.map +1 -1
- package/dist/index.js +78 -38
- package/dist/index.js.map +1 -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
|
|
|
@@ -17282,7 +17322,7 @@ var RemediationManager = class {
|
|
|
17282
17322
|
*/
|
|
17283
17323
|
async reconcile() {
|
|
17284
17324
|
const now = Date.now();
|
|
17285
|
-
const
|
|
17325
|
+
const live2 = this.blocklist.filter((b) => b.expires_at > now);
|
|
17286
17326
|
const stale = this.blocklist.filter((b) => b.expires_at <= now);
|
|
17287
17327
|
for (const entry of stale) {
|
|
17288
17328
|
if (!entry.dry_run) {
|
|
@@ -17292,7 +17332,7 @@ var RemediationManager = class {
|
|
|
17292
17332
|
}
|
|
17293
17333
|
}
|
|
17294
17334
|
}
|
|
17295
|
-
this.blocklist =
|
|
17335
|
+
this.blocklist = live2;
|
|
17296
17336
|
this.saveState();
|
|
17297
17337
|
let applied = null;
|
|
17298
17338
|
try {
|
|
@@ -17300,7 +17340,7 @@ var RemediationManager = class {
|
|
|
17300
17340
|
} catch {
|
|
17301
17341
|
}
|
|
17302
17342
|
if (applied) {
|
|
17303
|
-
const known = new Set(
|
|
17343
|
+
const known = new Set(live2.filter((b) => !b.dry_run).map((b) => b.ip));
|
|
17304
17344
|
for (const ip of applied) {
|
|
17305
17345
|
if (known.has(ip)) continue;
|
|
17306
17346
|
this.logLine(`[firewall] reconcile: dropping stale rule for ${ip} (no record of why)`);
|
|
@@ -17311,7 +17351,7 @@ var RemediationManager = class {
|
|
|
17311
17351
|
}
|
|
17312
17352
|
}
|
|
17313
17353
|
if (this.config.dry_run) return;
|
|
17314
|
-
for (const entry of
|
|
17354
|
+
for (const entry of live2) {
|
|
17315
17355
|
if (applied?.includes(entry.ip)) continue;
|
|
17316
17356
|
try {
|
|
17317
17357
|
await this.adapter.block(entry.ip);
|
|
@@ -17795,7 +17835,7 @@ async function runDaemon() {
|
|
|
17795
17835
|
if (event.module !== "rule-engine") ruleEngine.evaluate(event);
|
|
17796
17836
|
});
|
|
17797
17837
|
logLine(`[daemon] rule engine loaded ${ruleEngine.getRules().length} rules`);
|
|
17798
|
-
setInterval(() => ruleEngine.cleanup(),
|
|
17838
|
+
setInterval(() => ruleEngine.cleanup(), 6e4);
|
|
17799
17839
|
const settings = remediationSettings(config.remediation);
|
|
17800
17840
|
const firewallAdapter = detectFirewallAdapter(config.remediation?.backend ?? "auto");
|
|
17801
17841
|
const remediation = new RemediationManager(firewallAdapter, bus, settings);
|