@profullstack/threatcrush 0.13.11 → 0.13.13

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 CHANGED
@@ -2023,7 +2023,7 @@ var require_toml = __commonJS({
2023
2023
 
2024
2024
  // src/daemon/index.ts
2025
2025
  var import_node_fs21 = require("fs");
2026
- var import_node_child_process6 = require("child_process");
2026
+ var import_node_child_process7 = require("child_process");
2027
2027
  var import_node_os5 = require("os");
2028
2028
  var import_node_path16 = require("path");
2029
2029
  var import_node_util = require("util");
@@ -2320,20 +2320,14 @@ function closeDB() {
2320
2320
  }
2321
2321
 
2322
2322
  // src/daemon/firewall/backoff.ts
2323
- function fibonacciMinutes(strike) {
2323
+ var DEFAULT_FIRST_BAN_SECONDS = 5 * 60;
2324
+ function backoffFactor(strike) {
2324
2325
  const n = Math.max(1, Math.floor(strike));
2325
- let prev = 1;
2326
- let curr = 1;
2327
- for (let i = 1; i < n; i++) {
2328
- const next = prev + curr;
2329
- prev = curr;
2330
- curr = next;
2331
- }
2332
- return curr;
2326
+ return 2 ** Math.min(n - 1, 52);
2333
2327
  }
2334
- function banSeconds(strike, maxSeconds) {
2335
- const seconds = fibonacciMinutes(strike) * 60;
2336
- return Math.min(seconds, Math.max(60, maxSeconds));
2328
+ function banSeconds(strike, maxSeconds, firstSeconds = DEFAULT_FIRST_BAN_SECONDS) {
2329
+ const first = Math.max(60, firstSeconds);
2330
+ return Math.min(first * backoffFactor(strike), Math.max(first, maxSeconds));
2337
2331
  }
2338
2332
  function recordStrike(strikes, ip, now, memorySeconds) {
2339
2333
  const existing = strikes[ip];
@@ -9953,9 +9947,9 @@ var NetworkMonitor = class {
9953
9947
  );
9954
9948
  this.scanTrackers.delete(key);
9955
9949
  } else if (
9956
- // A smaller spread across our listening ports is worth surfacing, but
9957
- // at `medium` so it is logged and never banned (auto-defence bans at
9958
- // `high`). These are inbound-to-listening only, so the ephemeral
9950
+ // A smaller spread across our listening ports. The event itself is
9951
+ // `medium`, but the `port-scan-indicator` rule matches it and bans on
9952
+ // first sight. These are inbound-to-listening only, so the ephemeral
9959
9953
  // outbound-port bug that once banned our upstreams cannot recur here.
9960
9954
  tracker.ports.size >= this.portScanNoticeThreshold && withinWindow && !this.noticed.has(key)
9961
9955
  ) {
@@ -16369,12 +16363,15 @@ var DEFAULT_RULES = [
16369
16363
  enabled: true
16370
16364
  },
16371
16365
  {
16366
+ // A rule that declares `block` must be at the severity auto-defence bans
16367
+ // at, or it fires and is ignored: this one was `medium`, and on dev1 it
16368
+ // detected scanners 25 times and banned none of them.
16372
16369
  id: "web-scanner-detection",
16373
16370
  title: "Web Vulnerability Scanner Detected",
16374
16371
  description: "High volume of 4xx errors suggesting automated scanning",
16375
16372
  version: "1.0.0",
16376
16373
  category: "web",
16377
- severity: "medium",
16374
+ severity: "high",
16378
16375
  source_types: ["log-watcher", "web"],
16379
16376
  match: {
16380
16377
  field: "message",
@@ -16398,7 +16395,7 @@ var DEFAULT_RULES = [
16398
16395
  // and keeps walking URLs: GoogleOther did ~40/min per IP against r4ck's
16399
16396
  // /api/v1/search on dev2, 2026-09-25. `web-scanner-detection` saw it too,
16400
16397
  // but it is `medium`, and auto-defence bans at `high` and above, so it fired
16401
- // every minute and never banned. This one is `high` on purpose.
16398
+ // every minute and never banned (it is `high` now too). This one is `high` on purpose.
16402
16399
  //
16403
16400
  // It cannot see a proxy swarm that asks once per address; nothing per-IP can.
16404
16401
  id: "paywall-hammering",
@@ -16458,12 +16455,16 @@ var DEFAULT_RULES = [
16458
16455
  enabled: true
16459
16456
  },
16460
16457
  {
16458
+ // `high` for the same reason as web-scanner-detection: as `medium` it fired
16459
+ // 21 times on dev1 without a single ban. It also matches network-monitor's
16460
+ // "Possible port scan" notice (5+ of our LISTENING ports from one address),
16461
+ // so that is now banned on first sight as well.
16461
16462
  id: "port-scan-indicator",
16462
16463
  title: "Port Scan Indicators",
16463
16464
  description: "Connection attempts to many ports from a single source",
16464
16465
  version: "1.0.0",
16465
16466
  category: "network",
16466
- severity: "medium",
16467
+ severity: "high",
16467
16468
  source_types: ["network-monitor", "network"],
16468
16469
  match: {
16469
16470
  field: "message",
@@ -16889,8 +16890,11 @@ var DEFAULT_CONFIG2 = {
16889
16890
  enabled: true,
16890
16891
  dry_run: false,
16891
16892
  min_severity: "high",
16892
- max_ban_seconds: 86400,
16893
- strike_memory_seconds: 86400,
16893
+ first_ban_seconds: DEFAULT_FIRST_BAN_SECONDS,
16894
+ max_ban_seconds: 7 * 86400,
16895
+ // Longer than the ceiling, or an address that reaches it is forgotten while
16896
+ // banned and comes back at the first rung.
16897
+ strike_memory_seconds: 30 * 86400,
16894
16898
  allowlist: [],
16895
16899
  protect_current_ssh_client: true,
16896
16900
  spare_verified_crawlers: true
@@ -16982,9 +16986,9 @@ var RemediationManager = class {
16982
16986
  await this.ban(ip, event.message, { ruleId, source: "auto" });
16983
16987
  }
16984
16988
  /**
16985
- * Ban `ip`. With no explicit TTL the length comes from the Fibonacci ladder:
16986
- * 1m, 2m, 3m, 5m, 8m, 13m … for the 1st, 2nd, 3rd offence and so on, clamped
16987
- * to `max_ban_seconds`.
16989
+ * Ban `ip`. With no explicit TTL the length comes from the doubling ladder:
16990
+ * `first_ban_seconds` (5m by default), then 10m, 20m, 40m … for the 2nd, 3rd,
16991
+ * 4th offence and so on, clamped to `max_ban_seconds`.
16988
16992
  */
16989
16993
  async ban(ip, reason, opts = {}) {
16990
16994
  if ((0, import_node_net7.isIP)(ip) === 0) return { ok: false, error: `not an IP address: ${ip}` };
@@ -17022,7 +17026,7 @@ var RemediationManager = class {
17022
17026
  }
17023
17027
  }
17024
17028
  const strike = recordStrike(this.strikes, ip, now, this.config.strike_memory_seconds);
17025
- const ttl = opts.ttlSeconds ?? banSeconds(strike, this.config.max_ban_seconds);
17029
+ const ttl = opts.ttlSeconds ?? banSeconds(strike, this.config.max_ban_seconds, this.config.first_ban_seconds);
17026
17030
  const entry = {
17027
17031
  ip,
17028
17032
  reason,
@@ -17122,6 +17126,7 @@ var RemediationManager = class {
17122
17126
  backend: this.adapter.name,
17123
17127
  min_severity: this.config.min_severity,
17124
17128
  banned: this.blocklist.length,
17129
+ first_ban_seconds: this.config.first_ban_seconds,
17125
17130
  max_ban_seconds: this.config.max_ban_seconds,
17126
17131
  warning: this.privilegeWarning()
17127
17132
  };
@@ -17259,6 +17264,8 @@ function remediationSettings(section) {
17259
17264
  else if (section.mode === "enforce") settings.dry_run = false;
17260
17265
  else if (typeof section.dry_run === "boolean") settings.dry_run = section.dry_run;
17261
17266
  if (section.min_severity) settings.min_severity = section.min_severity;
17267
+ const firstBan = parseDuration(section.first_ban);
17268
+ if (firstBan) settings.first_ban_seconds = firstBan;
17262
17269
  const maxBan = parseDuration(section.max_ban);
17263
17270
  if (maxBan) settings.max_ban_seconds = maxBan;
17264
17271
  const memory = parseDuration(section.strike_memory);
@@ -17320,6 +17327,20 @@ async function flushTelemetry(timeoutMs = 2e3) {
17320
17327
  }
17321
17328
  }
17322
17329
 
17330
+ // src/core/node-runtime.ts
17331
+ var import_node_child_process6 = require("child_process");
17332
+ var MIN_DAEMON_NODE_MAJOR = 22;
17333
+ function nodeMajor(version) {
17334
+ const m = String(version ?? "").trim().replace(/^v/, "").match(/^(\d+)\./);
17335
+ return m ? Number(m[1]) : null;
17336
+ }
17337
+ function daemonNodeProblem(version) {
17338
+ const major = nodeMajor(version);
17339
+ if (major === null) return `cannot tell which Node version this is (${version ?? "unknown"})`;
17340
+ if (major >= MIN_DAEMON_NODE_MAJOR) return null;
17341
+ return `Node ${version} is too old for threatcrushd (needs ${MIN_DAEMON_NODE_MAJOR}+): its SQLite driver crashes the process with SIGSEGV. Install threatcrush with Node ${MIN_DAEMON_NODE_MAJOR} or newer and re-run threatcrush install-service.`;
17342
+ }
17343
+
17323
17344
  // src/daemon/index.ts
17324
17345
  function readVersion2() {
17325
17346
  try {
@@ -17330,7 +17351,7 @@ function readVersion2() {
17330
17351
  }
17331
17352
  }
17332
17353
  async function runHardeningChecks() {
17333
- const { stdout } = await (0, import_node_util.promisify)(import_node_child_process6.execFile)(
17354
+ const { stdout } = await (0, import_node_util.promisify)(import_node_child_process7.execFile)(
17334
17355
  process.execPath,
17335
17356
  [(0, import_node_path16.join)(__dirname, "index.js"), "harden", "--json", "--no-upload"],
17336
17357
  { timeout: 12e4, maxBuffer: 4 * 1024 * 1024 }
@@ -17363,6 +17384,12 @@ async function runDaemon() {
17363
17384
  });
17364
17385
  const version = readVersion2();
17365
17386
  logLine(`[daemon] starting threatcrushd v${version} mode=${PATHS.mode}`);
17387
+ const nodeProblem = daemonNodeProblem(process.versions.node);
17388
+ if (nodeProblem) {
17389
+ logLine(`[daemon] ${nodeProblem}`);
17390
+ console.error(nodeProblem);
17391
+ process.exit(1);
17392
+ }
17366
17393
  try {
17367
17394
  initStateDB(PATHS.stateDb);
17368
17395
  } catch (err) {
@@ -17408,7 +17435,7 @@ async function runDaemon() {
17408
17435
  });
17409
17436
  const remediationStatus = remediation.status();
17410
17437
  logLine(
17411
- `[daemon] auto-defence ${remediationStatus.enabled ? "ON" : "OFF"} (backend=${remediationStatus.backend}, mode=${remediationStatus.dry_run ? "dry_run" : "enforce"}, min_severity=${remediationStatus.min_severity}, bans escalate 1m\u21922m\u21923m\u21925m\u21928m\u2026)`
17438
+ `[daemon] auto-defence ${remediationStatus.enabled ? "ON" : "OFF"} (backend=${remediationStatus.backend}, mode=${remediationStatus.dry_run ? "dry_run" : "enforce"}, min_severity=${remediationStatus.min_severity}, bans double from ${formatDuration(remediationStatus.first_ban_seconds)} up to ${formatDuration(remediationStatus.max_ban_seconds)})`
17412
17439
  );
17413
17440
  if (remediationStatus.dry_run && firewallAdapter.enforces !== false) {
17414
17441
  logLine("[daemon] dry_run is set in [remediation] \u2014 detections will be logged, not blocked");