@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/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
  }
@@ -8499,8 +8530,12 @@ var DEFAULT_RULES = [
8499
8530
  },
8500
8531
  {
8501
8532
  id: "ssh-success-after-failures",
8502
- title: "SSH Login After Failed Attempts",
8503
- description: "Successful SSH login from an IP that had recent failures",
8533
+ title: "SSH Login Accepted",
8534
+ // Honest description: this rule does NOT correlate with earlier failures —
8535
+ // it matches any accepted login. It is an alert, and it declares no
8536
+ // remediation precisely because banning the person who just logged in is
8537
+ // never the right response.
8538
+ description: "Successful SSH login (alert only \u2014 never grounds for a ban)",
8504
8539
  version: "1.0.0",
8505
8540
  category: "auth",
8506
8541
  severity: "critical",
@@ -8527,7 +8562,9 @@ var DEFAULT_RULES = [
8527
8562
  match: {
8528
8563
  field: "message",
8529
8564
  operator: "regex",
8530
- value: "(failed|accepted).*\\broot\\b"
8565
+ // Attempts only. Matching `accepted` here meant a successful root login
8566
+ // banned the administrator who had just made it.
8567
+ value: "failed.*\\broot\\b"
8531
8568
  },
8532
8569
  threshold: 1,
8533
8570
  window_seconds: 60,
@@ -9150,6 +9187,26 @@ function currentSshClient(env2 = process.env) {
9150
9187
  const ip = source.trim().split(/\s+/)[0];
9151
9188
  return ip && (0, import_node_net3.isIP)(ip) ? ip : null;
9152
9189
  }
9190
+ function establishedSshPeers() {
9191
+ const peers = /* @__PURE__ */ new Set();
9192
+ try {
9193
+ const out = (0, import_node_child_process4.execFileSync)("ss", ["-tnpH"], {
9194
+ encoding: "utf-8",
9195
+ stdio: ["ignore", "pipe", "ignore"],
9196
+ timeout: 3e3
9197
+ });
9198
+ for (const line of out.split("\n")) {
9199
+ if (!/users:\(\("sshd/.test(line)) continue;
9200
+ const parts = line.trim().split(/\s+/);
9201
+ const peer = parts[4];
9202
+ if (!peer) continue;
9203
+ const ip = peer.slice(0, peer.lastIndexOf(":")).replace(/^\[|\]$/g, "");
9204
+ if (ip && (0, import_node_net3.isIP)(ip)) peers.add(ip);
9205
+ }
9206
+ } catch {
9207
+ }
9208
+ return [...peers];
9209
+ }
9153
9210
  function defaultGateways() {
9154
9211
  const found = /* @__PURE__ */ new Set();
9155
9212
  for (const args of [["-4", "route", "show", "default"], ["-6", "route", "show", "default"]]) {
@@ -9182,6 +9239,9 @@ var SEVERITY_RANK4 = {
9182
9239
  high: 3,
9183
9240
  critical: 4
9184
9241
  };
9242
+ function isSuccessfulAuth(message) {
9243
+ return /login accepted|accepted (?:publickey|password|keyboard-interactive)|session opened/i.test(message);
9244
+ }
9185
9245
  function explainBlockFailure(err, backend) {
9186
9246
  const raw = err.message || String(err);
9187
9247
  const denied = /not permitted|EACCES|EPERM|permission denied|must be root/i.test(raw);
@@ -9195,11 +9255,8 @@ var RemediationManager = class {
9195
9255
  this.bus = bus2;
9196
9256
  this.config = { ...DEFAULT_CONFIG2, ...config };
9197
9257
  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
9258
  for (const gateway of defaultGateways()) this.protectedList.push(gateway);
9259
+ this.refreshSshProtection();
9203
9260
  if (this.adapter.enforces === false) this.config.dry_run = true;
9204
9261
  this.loadState();
9205
9262
  void this.reconcile();
@@ -9216,6 +9273,21 @@ var RemediationManager = class {
9216
9273
  getProtected() {
9217
9274
  return [...this.protectedList];
9218
9275
  }
9276
+ /**
9277
+ * Add whoever is connected over SSH to the never-block set.
9278
+ *
9279
+ * Re-run on every sweep, not just at startup: a session opened after the
9280
+ * daemon started is just as much a lockout risk as one that predates it, and
9281
+ * an operator who ssh's in to investigate an incident must not be banned
9282
+ * while doing so.
9283
+ */
9284
+ refreshSshProtection() {
9285
+ if (!this.config.protect_current_ssh_client) return;
9286
+ const candidates = [currentSshClient(), ...establishedSshPeers()];
9287
+ for (const ip of candidates) {
9288
+ if (ip && !this.protectedList.includes(ip)) this.protectedList.push(ip);
9289
+ }
9290
+ }
9219
9291
  isAllowlisted(ip) {
9220
9292
  return isProtected(ip, this.protectedList);
9221
9293
  }
@@ -9224,6 +9296,11 @@ var RemediationManager = class {
9224
9296
  const eventRank = SEVERITY_RANK4[event.severity] ?? 0;
9225
9297
  const minRank = SEVERITY_RANK4[this.config.min_severity] ?? 3;
9226
9298
  if (eventRank < minRank) return;
9299
+ if (isSuccessfulAuth(event.message)) return;
9300
+ if (event.details?.rule_id) {
9301
+ const remediation = event.details?.remediation;
9302
+ if (remediation?.action !== "block") return;
9303
+ }
9227
9304
  const ip = event.source_ip;
9228
9305
  if (!ip || (0, import_node_net4.isIP)(ip) === 0) return;
9229
9306
  if (this.isAllowlisted(ip)) return;
@@ -9425,6 +9502,7 @@ var RemediationManager = class {
9425
9502
  }
9426
9503
  async processExpiries() {
9427
9504
  const now = Date.now();
9505
+ this.refreshSshProtection();
9428
9506
  for (const entry of this.blocklist.filter((b) => b.expires_at <= now)) {
9429
9507
  await this.unban(entry.ip);
9430
9508
  }