@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 +70 -6
- package/dist/daemon.js.map +1 -1
- package/dist/index.js +70 -6
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
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
|
-
|
|
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"]]) {
|
|
@@ -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
|
}
|