@profullstack/threatcrush 0.12.3 → 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/daemon.js CHANGED
@@ -3173,6 +3173,33 @@ var NetworkMonitor = class {
3173
3173
  if (ss.status === 0) return true;
3174
3174
  return (0, import_node_fs7.existsSync)("/proc/net/tcp");
3175
3175
  }
3176
+ /**
3177
+ * The TCP ports this host actually listens on.
3178
+ *
3179
+ * Everything else in a socket table is an *outbound* connection, whose local
3180
+ * port is an ephemeral one picked per connection. Counting those as "ports
3181
+ * probed" is what made every busy upstream look like a port scanner: open ten
3182
+ * connections to a registry, a database or an API inside thirty seconds and
3183
+ * the peer had "probed 10 ports". With auto-defence enforcing, that banned
3184
+ * our own dependencies — and any host we ssh out to.
3185
+ */
3186
+ listeningPorts() {
3187
+ const ports = /* @__PURE__ */ new Set();
3188
+ try {
3189
+ const out = (0, import_node_child_process2.spawnSync)("ss", ["-ltnH"], { encoding: "utf-8", stdio: ["pipe", "pipe", "pipe"], timeout: 3e3 });
3190
+ if (out.status === 0 && out.stdout) {
3191
+ for (const line of out.stdout.split("\n")) {
3192
+ const parts = line.trim().split(/\s+/);
3193
+ const local = parts[3];
3194
+ if (!local) continue;
3195
+ const port = Number.parseInt(local.slice(local.lastIndexOf(":") + 1), 10);
3196
+ if (Number.isFinite(port)) ports.add(port);
3197
+ }
3198
+ }
3199
+ } catch {
3200
+ }
3201
+ return ports;
3202
+ }
3176
3203
  poll() {
3177
3204
  try {
3178
3205
  const connections = this.getConnections();
@@ -3185,6 +3212,8 @@ var NetworkMonitor = class {
3185
3212
  getConnections() {
3186
3213
  const records = [];
3187
3214
  const now = Date.now();
3215
+ const listening = this.listeningPorts();
3216
+ if (listening.size === 0) return records;
3188
3217
  try {
3189
3218
  const ct = (0, import_node_child_process2.spawnSync)("conntrack", ["-L", "-p", "tcp", "-o", "extended"], {
3190
3219
  encoding: "utf-8",
@@ -3196,7 +3225,9 @@ var NetworkMonitor = class {
3196
3225
  const srcMatch = line.match(/src=(\d+\.\d+\.\d+\.\d+)/);
3197
3226
  const dportMatch = line.match(/dport=(\d+)/);
3198
3227
  if (srcMatch && dportMatch) {
3199
- records.push({ source_ip: srcMatch[1], dest_port: parseInt(dportMatch[1]), timestamp: now });
3228
+ const port = parseInt(dportMatch[1]);
3229
+ if (!listening.has(port)) continue;
3230
+ records.push({ source_ip: srcMatch[1], dest_port: port, timestamp: now });
3200
3231
  }
3201
3232
  }
3202
3233
  if (records.length > 0) return records;
@@ -3218,7 +3249,7 @@ var NetworkMonitor = class {
3218
3249
  if (peerParts.length >= 2 && localParts.length >= 2) {
3219
3250
  const sourceIp = peerParts.slice(0, -1).join(":");
3220
3251
  const destPort = parseInt(localParts[localParts.length - 1]);
3221
- if (sourceIp && !isNaN(destPort) && !this.isLocalIp(sourceIp)) {
3252
+ if (sourceIp && !isNaN(destPort) && listening.has(destPort) && !this.isLocalIp(sourceIp)) {
3222
3253
  records.push({ source_ip: sourceIp, dest_port: destPort, timestamp: now });
3223
3254
  }
3224
3255
  }
@@ -9150,6 +9181,26 @@ function currentSshClient(env2 = process.env) {
9150
9181
  const ip = source.trim().split(/\s+/)[0];
9151
9182
  return ip && (0, import_node_net3.isIP)(ip) ? ip : null;
9152
9183
  }
9184
+ function establishedSshPeers() {
9185
+ const peers = /* @__PURE__ */ new Set();
9186
+ try {
9187
+ const out = (0, import_node_child_process4.execFileSync)("ss", ["-tnpH"], {
9188
+ encoding: "utf-8",
9189
+ stdio: ["ignore", "pipe", "ignore"],
9190
+ timeout: 3e3
9191
+ });
9192
+ for (const line of out.split("\n")) {
9193
+ if (!/users:\(\("sshd/.test(line)) continue;
9194
+ const parts = line.trim().split(/\s+/);
9195
+ const peer = parts[4];
9196
+ if (!peer) continue;
9197
+ const ip = peer.slice(0, peer.lastIndexOf(":")).replace(/^\[|\]$/g, "");
9198
+ if (ip && (0, import_node_net3.isIP)(ip)) peers.add(ip);
9199
+ }
9200
+ } catch {
9201
+ }
9202
+ return [...peers];
9203
+ }
9153
9204
  function defaultGateways() {
9154
9205
  const found = /* @__PURE__ */ new Set();
9155
9206
  for (const args of [["-4", "route", "show", "default"], ["-6", "route", "show", "default"]]) {
@@ -9195,11 +9246,8 @@ var RemediationManager = class {
9195
9246
  this.bus = bus2;
9196
9247
  this.config = { ...DEFAULT_CONFIG2, ...config };
9197
9248
  this.protectedList = [...DEFAULT_PROTECTED, ...this.config.allowlist];
9198
- if (this.config.protect_current_ssh_client) {
9199
- const ssh = currentSshClient();
9200
- if (ssh) this.protectedList.push(ssh);
9201
- }
9202
9249
  for (const gateway of defaultGateways()) this.protectedList.push(gateway);
9250
+ this.refreshSshProtection();
9203
9251
  if (this.adapter.enforces === false) this.config.dry_run = true;
9204
9252
  this.loadState();
9205
9253
  void this.reconcile();
@@ -9216,6 +9264,21 @@ var RemediationManager = class {
9216
9264
  getProtected() {
9217
9265
  return [...this.protectedList];
9218
9266
  }
9267
+ /**
9268
+ * Add whoever is connected over SSH to the never-block set.
9269
+ *
9270
+ * Re-run on every sweep, not just at startup: a session opened after the
9271
+ * daemon started is just as much a lockout risk as one that predates it, and
9272
+ * an operator who ssh's in to investigate an incident must not be banned
9273
+ * while doing so.
9274
+ */
9275
+ refreshSshProtection() {
9276
+ if (!this.config.protect_current_ssh_client) return;
9277
+ const candidates = [currentSshClient(), ...establishedSshPeers()];
9278
+ for (const ip of candidates) {
9279
+ if (ip && !this.protectedList.includes(ip)) this.protectedList.push(ip);
9280
+ }
9281
+ }
9219
9282
  isAllowlisted(ip) {
9220
9283
  return isProtected(ip, this.protectedList);
9221
9284
  }
@@ -9425,6 +9488,7 @@ var RemediationManager = class {
9425
9488
  }
9426
9489
  async processExpiries() {
9427
9490
  const now = Date.now();
9491
+ this.refreshSshProtection();
9428
9492
  for (const entry of this.blocklist.filter((b) => b.expires_at <= now)) {
9429
9493
  await this.unban(entry.ip);
9430
9494
  }