@profullstack/threatcrush 0.12.3 → 0.12.5

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
@@ -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
  }
@@ -24743,8 +24774,12 @@ var DEFAULT_RULES = [
24743
24774
  },
24744
24775
  {
24745
24776
  id: "ssh-success-after-failures",
24746
- title: "SSH Login After Failed Attempts",
24747
- description: "Successful SSH login from an IP that had recent failures",
24777
+ title: "SSH Login Accepted",
24778
+ // Honest description: this rule does NOT correlate with earlier failures —
24779
+ // it matches any accepted login. It is an alert, and it declares no
24780
+ // remediation precisely because banning the person who just logged in is
24781
+ // never the right response.
24782
+ description: "Successful SSH login (alert only \u2014 never grounds for a ban)",
24748
24783
  version: "1.0.0",
24749
24784
  category: "auth",
24750
24785
  severity: "critical",
@@ -24771,7 +24806,9 @@ var DEFAULT_RULES = [
24771
24806
  match: {
24772
24807
  field: "message",
24773
24808
  operator: "regex",
24774
- value: "(failed|accepted).*\\broot\\b"
24809
+ // Attempts only. Matching `accepted` here meant a successful root login
24810
+ // banned the administrator who had just made it.
24811
+ value: "failed.*\\broot\\b"
24775
24812
  },
24776
24813
  threshold: 1,
24777
24814
  window_seconds: 60,
@@ -25396,6 +25433,26 @@ function currentSshClient(env2 = process.env) {
25396
25433
  const ip = source.trim().split(/\s+/)[0];
25397
25434
  return ip && (0, import_node_net4.isIP)(ip) ? ip : null;
25398
25435
  }
25436
+ function establishedSshPeers() {
25437
+ const peers = /* @__PURE__ */ new Set();
25438
+ try {
25439
+ const out = (0, import_node_child_process7.execFileSync)("ss", ["-tnpH"], {
25440
+ encoding: "utf-8",
25441
+ stdio: ["ignore", "pipe", "ignore"],
25442
+ timeout: 3e3
25443
+ });
25444
+ for (const line of out.split("\n")) {
25445
+ if (!/users:\(\("sshd/.test(line)) continue;
25446
+ const parts = line.trim().split(/\s+/);
25447
+ const peer = parts[4];
25448
+ if (!peer) continue;
25449
+ const ip = peer.slice(0, peer.lastIndexOf(":")).replace(/^\[|\]$/g, "");
25450
+ if (ip && (0, import_node_net4.isIP)(ip)) peers.add(ip);
25451
+ }
25452
+ } catch {
25453
+ }
25454
+ return [...peers];
25455
+ }
25399
25456
  function defaultGateways() {
25400
25457
  const found = /* @__PURE__ */ new Set();
25401
25458
  for (const args of [["-4", "route", "show", "default"], ["-6", "route", "show", "default"]]) {
@@ -25428,6 +25485,9 @@ var SEVERITY_RANK4 = {
25428
25485
  high: 3,
25429
25486
  critical: 4
25430
25487
  };
25488
+ function isSuccessfulAuth(message) {
25489
+ return /login accepted|accepted (?:publickey|password|keyboard-interactive)|session opened/i.test(message);
25490
+ }
25431
25491
  function explainBlockFailure(err, backend) {
25432
25492
  const raw = err.message || String(err);
25433
25493
  const denied = /not permitted|EACCES|EPERM|permission denied|must be root/i.test(raw);
@@ -25441,11 +25501,8 @@ var RemediationManager = class {
25441
25501
  this.bus = bus2;
25442
25502
  this.config = { ...DEFAULT_CONFIG2, ...config };
25443
25503
  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
25504
  for (const gateway of defaultGateways()) this.protectedList.push(gateway);
25505
+ this.refreshSshProtection();
25449
25506
  if (this.adapter.enforces === false) this.config.dry_run = true;
25450
25507
  this.loadState();
25451
25508
  void this.reconcile();
@@ -25462,6 +25519,21 @@ var RemediationManager = class {
25462
25519
  getProtected() {
25463
25520
  return [...this.protectedList];
25464
25521
  }
25522
+ /**
25523
+ * Add whoever is connected over SSH to the never-block set.
25524
+ *
25525
+ * Re-run on every sweep, not just at startup: a session opened after the
25526
+ * daemon started is just as much a lockout risk as one that predates it, and
25527
+ * an operator who ssh's in to investigate an incident must not be banned
25528
+ * while doing so.
25529
+ */
25530
+ refreshSshProtection() {
25531
+ if (!this.config.protect_current_ssh_client) return;
25532
+ const candidates = [currentSshClient(), ...establishedSshPeers()];
25533
+ for (const ip of candidates) {
25534
+ if (ip && !this.protectedList.includes(ip)) this.protectedList.push(ip);
25535
+ }
25536
+ }
25465
25537
  isAllowlisted(ip) {
25466
25538
  return isProtected(ip, this.protectedList);
25467
25539
  }
@@ -25470,6 +25542,11 @@ var RemediationManager = class {
25470
25542
  const eventRank = SEVERITY_RANK4[event.severity] ?? 0;
25471
25543
  const minRank = SEVERITY_RANK4[this.config.min_severity] ?? 3;
25472
25544
  if (eventRank < minRank) return;
25545
+ if (isSuccessfulAuth(event.message)) return;
25546
+ if (event.details?.rule_id) {
25547
+ const remediation = event.details?.remediation;
25548
+ if (remediation?.action !== "block") return;
25549
+ }
25473
25550
  const ip = event.source_ip;
25474
25551
  if (!ip || (0, import_node_net5.isIP)(ip) === 0) return;
25475
25552
  if (this.isAllowlisted(ip)) return;
@@ -25671,6 +25748,7 @@ var RemediationManager = class {
25671
25748
  }
25672
25749
  async processExpiries() {
25673
25750
  const now = Date.now();
25751
+ this.refreshSshProtection();
25674
25752
  for (const entry of this.blocklist.filter((b) => b.expires_at <= now)) {
25675
25753
  await this.unban(entry.ip);
25676
25754
  }