@profullstack/threatcrush 0.12.2 → 0.12.4

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/index.js CHANGED
@@ -11246,7 +11246,7 @@ function bansPanel(parent, state, theme, options) {
11246
11246
  if (fw.warning) {
11247
11247
  panel.text("NOT PRIVILEGED", { fg: severityColors.critical, bold: true });
11248
11248
  panel.label(fw.warning, { fg: severityColors.high });
11249
- panel.label("sudo threatcrush install-service", { fg: theme.muted });
11249
+ panel.label("threatcrush install-service", { fg: theme.muted });
11250
11250
  panel.spacer(1);
11251
11251
  }
11252
11252
  if (state.bans.length === 0) {
@@ -23657,6 +23657,33 @@ var NetworkMonitor = class {
23657
23657
  if (ss.status === 0) return true;
23658
23658
  return (0, import_node_fs21.existsSync)("/proc/net/tcp");
23659
23659
  }
23660
+ /**
23661
+ * The TCP ports this host actually listens on.
23662
+ *
23663
+ * Everything else in a socket table is an *outbound* connection, whose local
23664
+ * port is an ephemeral one picked per connection. Counting those as "ports
23665
+ * probed" is what made every busy upstream look like a port scanner: open ten
23666
+ * connections to a registry, a database or an API inside thirty seconds and
23667
+ * the peer had "probed 10 ports". With auto-defence enforcing, that banned
23668
+ * our own dependencies — and any host we ssh out to.
23669
+ */
23670
+ listeningPorts() {
23671
+ const ports = /* @__PURE__ */ new Set();
23672
+ try {
23673
+ const out = (0, import_node_child_process5.spawnSync)("ss", ["-ltnH"], { encoding: "utf-8", stdio: ["pipe", "pipe", "pipe"], timeout: 3e3 });
23674
+ if (out.status === 0 && out.stdout) {
23675
+ for (const line of out.stdout.split("\n")) {
23676
+ const parts = line.trim().split(/\s+/);
23677
+ const local = parts[3];
23678
+ if (!local) continue;
23679
+ const port = Number.parseInt(local.slice(local.lastIndexOf(":") + 1), 10);
23680
+ if (Number.isFinite(port)) ports.add(port);
23681
+ }
23682
+ }
23683
+ } catch {
23684
+ }
23685
+ return ports;
23686
+ }
23660
23687
  poll() {
23661
23688
  try {
23662
23689
  const connections = this.getConnections();
@@ -23669,6 +23696,8 @@ var NetworkMonitor = class {
23669
23696
  getConnections() {
23670
23697
  const records = [];
23671
23698
  const now = Date.now();
23699
+ const listening = this.listeningPorts();
23700
+ if (listening.size === 0) return records;
23672
23701
  try {
23673
23702
  const ct = (0, import_node_child_process5.spawnSync)("conntrack", ["-L", "-p", "tcp", "-o", "extended"], {
23674
23703
  encoding: "utf-8",
@@ -23680,7 +23709,9 @@ var NetworkMonitor = class {
23680
23709
  const srcMatch = line.match(/src=(\d+\.\d+\.\d+\.\d+)/);
23681
23710
  const dportMatch = line.match(/dport=(\d+)/);
23682
23711
  if (srcMatch && dportMatch) {
23683
- records.push({ source_ip: srcMatch[1], dest_port: parseInt(dportMatch[1]), timestamp: now });
23712
+ const port = parseInt(dportMatch[1]);
23713
+ if (!listening.has(port)) continue;
23714
+ records.push({ source_ip: srcMatch[1], dest_port: port, timestamp: now });
23684
23715
  }
23685
23716
  }
23686
23717
  if (records.length > 0) return records;
@@ -23702,7 +23733,7 @@ var NetworkMonitor = class {
23702
23733
  if (peerParts.length >= 2 && localParts.length >= 2) {
23703
23734
  const sourceIp = peerParts.slice(0, -1).join(":");
23704
23735
  const destPort = parseInt(localParts[localParts.length - 1]);
23705
- if (sourceIp && !isNaN(destPort) && !this.isLocalIp(sourceIp)) {
23736
+ if (sourceIp && !isNaN(destPort) && listening.has(destPort) && !this.isLocalIp(sourceIp)) {
23706
23737
  records.push({ source_ip: sourceIp, dest_port: destPort, timestamp: now });
23707
23738
  }
23708
23739
  }
@@ -25396,6 +25427,26 @@ function currentSshClient(env2 = process.env) {
25396
25427
  const ip = source.trim().split(/\s+/)[0];
25397
25428
  return ip && (0, import_node_net4.isIP)(ip) ? ip : null;
25398
25429
  }
25430
+ function establishedSshPeers() {
25431
+ const peers = /* @__PURE__ */ new Set();
25432
+ try {
25433
+ const out = (0, import_node_child_process7.execFileSync)("ss", ["-tnpH"], {
25434
+ encoding: "utf-8",
25435
+ stdio: ["ignore", "pipe", "ignore"],
25436
+ timeout: 3e3
25437
+ });
25438
+ for (const line of out.split("\n")) {
25439
+ if (!/users:\(\("sshd/.test(line)) continue;
25440
+ const parts = line.trim().split(/\s+/);
25441
+ const peer = parts[4];
25442
+ if (!peer) continue;
25443
+ const ip = peer.slice(0, peer.lastIndexOf(":")).replace(/^\[|\]$/g, "");
25444
+ if (ip && (0, import_node_net4.isIP)(ip)) peers.add(ip);
25445
+ }
25446
+ } catch {
25447
+ }
25448
+ return [...peers];
25449
+ }
25399
25450
  function defaultGateways() {
25400
25451
  const found = /* @__PURE__ */ new Set();
25401
25452
  for (const args of [["-4", "route", "show", "default"], ["-6", "route", "show", "default"]]) {
@@ -25433,7 +25484,7 @@ function explainBlockFailure(err, backend) {
25433
25484
  const denied = /not permitted|EACCES|EPERM|permission denied|must be root/i.test(raw);
25434
25485
  if (!denied) return `${raw} (backend: ${backend})`;
25435
25486
  const who = typeof process.getuid === "function" && process.getuid() === 0 ? "as root" : `as uid ${typeof process.getuid === "function" ? process.getuid() : "?"}`;
25436
- return `threatcrushd is running ${who} and cannot manage ${backend}. A ban is written by the daemon, not by whoever asked for it \u2014 run the daemon as root: sudo threatcrush install-service && sudo systemctl enable --now threatcrushd`;
25487
+ return `threatcrushd is running ${who} and cannot manage ${backend}. A ban is written by the daemon, not by whoever asked for it. Fix it with one command, which asks for root itself: threatcrush install-service`;
25437
25488
  }
25438
25489
  var RemediationManager = class {
25439
25490
  constructor(adapter, bus2, config) {
@@ -25441,11 +25492,8 @@ var RemediationManager = class {
25441
25492
  this.bus = bus2;
25442
25493
  this.config = { ...DEFAULT_CONFIG2, ...config };
25443
25494
  this.protectedList = [...DEFAULT_PROTECTED, ...this.config.allowlist];
25444
- if (this.config.protect_current_ssh_client) {
25445
- const ssh = currentSshClient();
25446
- if (ssh) this.protectedList.push(ssh);
25447
- }
25448
25495
  for (const gateway of defaultGateways()) this.protectedList.push(gateway);
25496
+ this.refreshSshProtection();
25449
25497
  if (this.adapter.enforces === false) this.config.dry_run = true;
25450
25498
  this.loadState();
25451
25499
  void this.reconcile();
@@ -25462,6 +25510,21 @@ var RemediationManager = class {
25462
25510
  getProtected() {
25463
25511
  return [...this.protectedList];
25464
25512
  }
25513
+ /**
25514
+ * Add whoever is connected over SSH to the never-block set.
25515
+ *
25516
+ * Re-run on every sweep, not just at startup: a session opened after the
25517
+ * daemon started is just as much a lockout risk as one that predates it, and
25518
+ * an operator who ssh's in to investigate an incident must not be banned
25519
+ * while doing so.
25520
+ */
25521
+ refreshSshProtection() {
25522
+ if (!this.config.protect_current_ssh_client) return;
25523
+ const candidates = [currentSshClient(), ...establishedSshPeers()];
25524
+ for (const ip of candidates) {
25525
+ if (ip && !this.protectedList.includes(ip)) this.protectedList.push(ip);
25526
+ }
25527
+ }
25465
25528
  isAllowlisted(ip) {
25466
25529
  return isProtected(ip, this.protectedList);
25467
25530
  }
@@ -25671,6 +25734,7 @@ var RemediationManager = class {
25671
25734
  }
25672
25735
  async processExpiries() {
25673
25736
  const now = Date.now();
25737
+ this.refreshSshProtection();
25674
25738
  for (const entry of this.blocklist.filter((b) => b.expires_at <= now)) {
25675
25739
  await this.unban(entry.ip);
25676
25740
  }
@@ -26070,14 +26134,31 @@ function resolveBinPath() {
26070
26134
  return "threatcrush";
26071
26135
  }
26072
26136
  }
26137
+ function stableNodePath(execPath) {
26138
+ const stable = execPath.replace(/(\/installs\/node\/)[^/]+\//, "$1latest/");
26139
+ if (stable === execPath) return execPath;
26140
+ return (0, import_node_fs30.existsSync)(stable) ? stable : execPath;
26141
+ }
26142
+ function looksLikeNodeScript(path) {
26143
+ try {
26144
+ const head = (0, import_node_fs30.readFileSync)(path, "utf-8").slice(0, 120);
26145
+ return /^#!.*\bnode\b/.test(head);
26146
+ } catch {
26147
+ return false;
26148
+ }
26149
+ }
26150
+ function execStartCommand(binPath, nodePath, isNodeScript) {
26151
+ return isNodeScript ? `${nodePath} ${binPath} daemon` : `${binPath} daemon`;
26152
+ }
26073
26153
  function isRoot2() {
26074
26154
  return typeof process.getuid === "function" && process.getuid() === 0;
26075
26155
  }
26076
26156
  function reexecWithSudo(args) {
26077
26157
  if ((0, import_node_child_process9.spawnSync)("sudo", ["--version"], { stdio: "pipe" }).status !== 0) return false;
26078
- console.log(source_default.dim(" This needs root. Re-running under sudo...\n"));
26158
+ console.log(source_default.dim(" This needs root \u2014 sudo will ask for your password.\n"));
26079
26159
  const bin = resolveBinPath();
26080
- const result = (0, import_node_child_process9.spawnSync)("sudo", [bin, ...args], { stdio: "inherit" });
26160
+ const argv = looksLikeNodeScript(bin) ? [stableNodePath(process.execPath), bin, ...args] : [bin, ...args];
26161
+ const result = (0, import_node_child_process9.spawnSync)("sudo", argv, { stdio: "inherit" });
26081
26162
  return result.status === 0;
26082
26163
  }
26083
26164
  function callerHome() {
@@ -26134,9 +26215,16 @@ async function installServiceCommand() {
26134
26215
  console.log(source_default.dim(" Run this as root: threatcrush install-service"));
26135
26216
  return;
26136
26217
  }
26137
- const unit = resolveTemplate().replace("{{BIN_PATH}}", resolveBinPath());
26218
+ const binPath = resolveBinPath();
26219
+ const exec = execStartCommand(
26220
+ binPath,
26221
+ stableNodePath(process.execPath),
26222
+ looksLikeNodeScript(binPath)
26223
+ );
26224
+ const unit = resolveTemplate().replace("{{BIN_PATH}} daemon", exec).replace("{{BIN_PATH}}", binPath);
26138
26225
  (0, import_node_fs30.writeFileSync)(UNIT_PATH, unit, { mode: 420 });
26139
26226
  console.log(source_default.green(` \u2713 Installed unit file: ${UNIT_PATH}`));
26227
+ console.log(source_default.dim(` ExecStart=${exec}`));
26140
26228
  ensureSystemDirs();
26141
26229
  stopUserModeDaemon();
26142
26230
  try {