@profullstack/threatcrush 0.13.79 → 0.13.81

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
@@ -2027,7 +2027,7 @@ var require_toml = __commonJS({
2027
2027
 
2028
2028
  // src/daemon/index.ts
2029
2029
  var import_node_fs39 = require("fs");
2030
- var import_node_child_process14 = require("child_process");
2030
+ var import_node_child_process15 = require("child_process");
2031
2031
  var import_node_os6 = require("os");
2032
2032
  var import_node_path30 = require("path");
2033
2033
  var import_node_util = require("util");
@@ -20660,6 +20660,10 @@ var TorExitList = class _TorExitList {
20660
20660
  get size() {
20661
20661
  return this.exits.size;
20662
20662
  }
20663
+ /** Every exit address currently in memory, for the proactive firewall block. */
20664
+ values() {
20665
+ return [...this.exits];
20666
+ }
20663
20667
  /** When the list in memory was downloaded (ms), 0 if never. */
20664
20668
  get updated() {
20665
20669
  return this.updatedAt;
@@ -20717,6 +20721,96 @@ var TorExitList = class _TorExitList {
20717
20721
  }
20718
20722
  };
20719
20723
 
20724
+ // src/daemon/firewall/tor-block.ts
20725
+ var import_node_child_process11 = require("child_process");
20726
+ var import_node_net14 = require("net");
20727
+ var TOR_NFT_TABLE = "threatcrush-tor";
20728
+ function defaultNftRunner() {
20729
+ return (args, stdin) => new Promise((resolve4, reject) => {
20730
+ const child = (0, import_node_child_process11.execFile)(
20731
+ "nft",
20732
+ args,
20733
+ { maxBuffer: 16 * 1024 * 1024 },
20734
+ (err) => err ? reject(err) : resolve4()
20735
+ );
20736
+ if (stdin !== void 0) child.stdin?.end(stdin);
20737
+ });
20738
+ }
20739
+ var TorExitBlockEnforcer = class {
20740
+ constructor(opts) {
20741
+ this.opts = opts;
20742
+ this.runNft = opts.runNft ?? defaultNftRunner();
20743
+ this.log = opts.log ?? (() => {
20744
+ });
20745
+ this.intervalMs = Math.max(6e4, opts.intervalMs ?? 36e5);
20746
+ this.sshPort = opts.sshPort ?? 22;
20747
+ }
20748
+ opts;
20749
+ timer = null;
20750
+ applied = false;
20751
+ /** Set once if nft is not installed here; stops retrying and logging. */
20752
+ unavailable = false;
20753
+ runNft;
20754
+ log;
20755
+ intervalMs;
20756
+ sshPort;
20757
+ start() {
20758
+ void this.sync();
20759
+ this.timer = setInterval(() => void this.sync(), this.intervalMs);
20760
+ this.timer.unref?.();
20761
+ }
20762
+ stop() {
20763
+ if (this.timer) clearInterval(this.timer);
20764
+ this.timer = null;
20765
+ }
20766
+ /** Apply (or tear down) the block to match the current setting and list. */
20767
+ async sync() {
20768
+ if (this.unavailable) return;
20769
+ if (!this.opts.enabled()) {
20770
+ await this.teardown();
20771
+ return;
20772
+ }
20773
+ const ipv4 = this.opts.exits.values().filter((ip) => (0, import_node_net14.isIP)(ip) === 4);
20774
+ if (ipv4.length === 0) return;
20775
+ try {
20776
+ await this.apply(ipv4);
20777
+ if (!this.applied) {
20778
+ this.log(`[tor] proactively blocking ${ipv4.length} Tor exits on tcp/${this.sshPort} (table inet ${TOR_NFT_TABLE})`);
20779
+ }
20780
+ this.applied = true;
20781
+ } catch (err) {
20782
+ if (err.code === "ENOENT") {
20783
+ this.unavailable = true;
20784
+ this.log("[tor] nft is not installed; the proactive Tor exit block is unavailable on this host");
20785
+ return;
20786
+ }
20787
+ this.log(`[tor] could not apply the proactive Tor exit block: ${err.message}`);
20788
+ }
20789
+ }
20790
+ /** One atomic `nft -f -` transaction: declare the table, then load the set. */
20791
+ async apply(ipv4) {
20792
+ const t = TOR_NFT_TABLE;
20793
+ const ruleset = `add table inet ${t}
20794
+ add set inet ${t} exits { type ipv4_addr; }
20795
+ add chain inet ${t} input { type filter hook input priority -100 ; policy accept ; }
20796
+ flush chain inet ${t} input
20797
+ add rule inet ${t} input tcp dport ${this.sshPort} ip saddr @exits counter drop
20798
+ flush set inet ${t} exits
20799
+ add element inet ${t} exits { ${ipv4.join(", ")} }
20800
+ `;
20801
+ await this.runNft(["-f", "-"], ruleset);
20802
+ }
20803
+ async teardown() {
20804
+ if (!this.applied) return;
20805
+ try {
20806
+ await this.runNft(["delete", "table", "inet", TOR_NFT_TABLE]);
20807
+ this.log("[tor] removed the proactive Tor exit block (block_tor_ssh off)");
20808
+ } catch {
20809
+ }
20810
+ this.applied = false;
20811
+ }
20812
+ };
20813
+
20720
20814
  // src/daemon/rules/engine.ts
20721
20815
  var MAX_WINDOW_ENTRIES = 1e3;
20722
20816
  function live(window) {
@@ -21642,17 +21736,17 @@ function loadAllRules(customDir) {
21642
21736
  }
21643
21737
 
21644
21738
  // src/daemon/firewall/adapters.ts
21645
- var import_node_child_process11 = require("child_process");
21739
+ var import_node_child_process12 = require("child_process");
21646
21740
  var import_node_fs35 = require("fs");
21647
21741
  var import_node_path27 = require("path");
21648
- var import_node_net14 = require("net");
21742
+ var import_node_net15 = require("net");
21649
21743
  function assertValidFirewallIp(ip) {
21650
- if ((0, import_node_net14.isIP)(ip) !== 4) {
21744
+ if ((0, import_node_net15.isIP)(ip) !== 4) {
21651
21745
  throw new Error(`Invalid IPv4 address: ${ip}`);
21652
21746
  }
21653
21747
  }
21654
21748
  function assertBannableIp(ip) {
21655
- if ((0, import_node_net14.isIP)(ip) === 0) {
21749
+ if ((0, import_node_net15.isIP)(ip) === 0) {
21656
21750
  throw new Error(`Invalid IP address: ${ip}`);
21657
21751
  }
21658
21752
  }
@@ -21678,11 +21772,11 @@ var Fail2banAdapter = class {
21678
21772
  enforces = true;
21679
21773
  jailReady = false;
21680
21774
  isAvailable() {
21681
- const result = (0, import_node_child_process11.spawnSync)("fail2ban-client", ["ping"], { stdio: "pipe" });
21775
+ const result = (0, import_node_child_process12.spawnSync)("fail2ban-client", ["ping"], { stdio: "pipe" });
21682
21776
  return result.status === 0;
21683
21777
  }
21684
21778
  client(args) {
21685
- return (0, import_node_child_process11.execFileSync)("fail2ban-client", args, {
21779
+ return (0, import_node_child_process12.execFileSync)("fail2ban-client", args, {
21686
21780
  encoding: "utf-8",
21687
21781
  stdio: ["ignore", "pipe", "pipe"]
21688
21782
  });
@@ -21758,7 +21852,7 @@ var Fail2banAdapter = class {
21758
21852
  const status = this.client(["status", this.jail]);
21759
21853
  const line = status.split("\n").find((l) => /Banned IP list:/i.test(l));
21760
21854
  if (!line) return [];
21761
- return line.split(":").slice(1).join(":").trim().split(/\s+/).filter((s) => (0, import_node_net14.isIP)(s) !== 0);
21855
+ return line.split(":").slice(1).join(":").trim().split(/\s+/).filter((s) => (0, import_node_net15.isIP)(s) !== 0);
21762
21856
  } catch {
21763
21857
  return [];
21764
21858
  }
@@ -21768,45 +21862,45 @@ var Fail2banAdapter = class {
21768
21862
  function parseBannedList(output) {
21769
21863
  const found = [];
21770
21864
  for (const match of output.matchAll(/['"]([^'"]+)['"]/g)) {
21771
- if ((0, import_node_net14.isIP)(match[1]) !== 0) found.push(match[1]);
21865
+ if ((0, import_node_net15.isIP)(match[1]) !== 0) found.push(match[1]);
21772
21866
  }
21773
21867
  if (found.length > 0) return found;
21774
- return output.replace(/[[\],]/g, " ").split(/\s+/).filter((s) => (0, import_node_net14.isIP)(s) !== 0);
21868
+ return output.replace(/[[\],]/g, " ").split(/\s+/).filter((s) => (0, import_node_net15.isIP)(s) !== 0);
21775
21869
  }
21776
21870
  var NftablesAdapter = class {
21777
21871
  name = "nftables";
21778
21872
  table = "threatcrush";
21779
21873
  set = "blocklist";
21780
21874
  isAvailable() {
21781
- const result = (0, import_node_child_process11.spawnSync)("nft", ["--version"], { stdio: "pipe" });
21875
+ const result = (0, import_node_child_process12.spawnSync)("nft", ["--version"], { stdio: "pipe" });
21782
21876
  return result.status === 0;
21783
21877
  }
21784
21878
  ensureSetup() {
21785
21879
  try {
21786
- (0, import_node_child_process11.execSync)(`nft list table inet ${this.table} 2>/dev/null`, { stdio: "pipe" });
21880
+ (0, import_node_child_process12.execSync)(`nft list table inet ${this.table} 2>/dev/null`, { stdio: "pipe" });
21787
21881
  } catch {
21788
- (0, import_node_child_process11.execSync)(`nft add table inet ${this.table}`);
21789
- (0, import_node_child_process11.execSync)(`nft add set inet ${this.table} ${this.set} '{ type ipv4_addr; flags timeout; }'`);
21790
- (0, import_node_child_process11.execSync)(`nft add chain inet ${this.table} input '{ type filter hook input priority -1; policy accept; }'`);
21791
- (0, import_node_child_process11.execSync)(`nft add rule inet ${this.table} input ip saddr @${this.set} drop`);
21882
+ (0, import_node_child_process12.execSync)(`nft add table inet ${this.table}`);
21883
+ (0, import_node_child_process12.execSync)(`nft add set inet ${this.table} ${this.set} '{ type ipv4_addr; flags timeout; }'`);
21884
+ (0, import_node_child_process12.execSync)(`nft add chain inet ${this.table} input '{ type filter hook input priority -1; policy accept; }'`);
21885
+ (0, import_node_child_process12.execSync)(`nft add rule inet ${this.table} input ip saddr @${this.set} drop`);
21792
21886
  }
21793
21887
  }
21794
21888
  async block(ip) {
21795
21889
  assertValidFirewallIp(ip);
21796
21890
  this.ensureSetup();
21797
- (0, import_node_child_process11.execSync)(`nft add element inet ${this.table} ${this.set} '{ ${ip} }'`);
21891
+ (0, import_node_child_process12.execSync)(`nft add element inet ${this.table} ${this.set} '{ ${ip} }'`);
21798
21892
  }
21799
21893
  async unblock(ip) {
21800
21894
  assertValidFirewallIp(ip);
21801
21895
  try {
21802
- (0, import_node_child_process11.execSync)(`nft delete element inet ${this.table} ${this.set} '{ ${ip} }'`);
21896
+ (0, import_node_child_process12.execSync)(`nft delete element inet ${this.table} ${this.set} '{ ${ip} }'`);
21803
21897
  } catch {
21804
21898
  }
21805
21899
  }
21806
21900
  async isBlocked(ip) {
21807
21901
  assertValidFirewallIp(ip);
21808
21902
  try {
21809
- const output = (0, import_node_child_process11.execSync)(`nft list set inet ${this.table} ${this.set}`, { encoding: "utf-8" });
21903
+ const output = (0, import_node_child_process12.execSync)(`nft list set inet ${this.table} ${this.set}`, { encoding: "utf-8" });
21810
21904
  return output.includes(ip);
21811
21905
  } catch {
21812
21906
  return false;
@@ -21814,7 +21908,7 @@ var NftablesAdapter = class {
21814
21908
  }
21815
21909
  async listBlocked() {
21816
21910
  try {
21817
- const output = (0, import_node_child_process11.execSync)(`nft list set inet ${this.table} ${this.set}`, { encoding: "utf-8" });
21911
+ const output = (0, import_node_child_process12.execSync)(`nft list set inet ${this.table} ${this.set}`, { encoding: "utf-8" });
21818
21912
  const match = output.match(/elements\s*=\s*\{([^}]*)\}/);
21819
21913
  if (!match) return [];
21820
21914
  return match[1].split(",").map((s) => s.trim().split(/\s/)[0]).filter(Boolean);
@@ -21827,34 +21921,34 @@ var IptablesAdapter = class {
21827
21921
  name = "iptables";
21828
21922
  chain = "THREATCRUSH";
21829
21923
  isAvailable() {
21830
- const result = (0, import_node_child_process11.spawnSync)("iptables", ["--version"], { stdio: "pipe" });
21924
+ const result = (0, import_node_child_process12.spawnSync)("iptables", ["--version"], { stdio: "pipe" });
21831
21925
  return result.status === 0;
21832
21926
  }
21833
21927
  ensureChain() {
21834
21928
  try {
21835
- (0, import_node_child_process11.execSync)(`iptables -n -L ${this.chain} 2>/dev/null`, { stdio: "pipe" });
21929
+ (0, import_node_child_process12.execSync)(`iptables -n -L ${this.chain} 2>/dev/null`, { stdio: "pipe" });
21836
21930
  } catch {
21837
- (0, import_node_child_process11.execSync)(`iptables -N ${this.chain}`);
21838
- (0, import_node_child_process11.execSync)(`iptables -I INPUT 1 -j ${this.chain}`);
21931
+ (0, import_node_child_process12.execSync)(`iptables -N ${this.chain}`);
21932
+ (0, import_node_child_process12.execSync)(`iptables -I INPUT 1 -j ${this.chain}`);
21839
21933
  }
21840
21934
  }
21841
21935
  async block(ip) {
21842
21936
  assertValidFirewallIp(ip);
21843
21937
  this.ensureChain();
21844
21938
  if (await this.isBlocked(ip)) return;
21845
- (0, import_node_child_process11.execSync)(`iptables -A ${this.chain} -s ${ip} -j DROP`);
21939
+ (0, import_node_child_process12.execSync)(`iptables -A ${this.chain} -s ${ip} -j DROP`);
21846
21940
  }
21847
21941
  async unblock(ip) {
21848
21942
  assertValidFirewallIp(ip);
21849
21943
  try {
21850
- (0, import_node_child_process11.execSync)(`iptables -D ${this.chain} -s ${ip} -j DROP`);
21944
+ (0, import_node_child_process12.execSync)(`iptables -D ${this.chain} -s ${ip} -j DROP`);
21851
21945
  } catch {
21852
21946
  }
21853
21947
  }
21854
21948
  async isBlocked(ip) {
21855
21949
  assertValidFirewallIp(ip);
21856
21950
  try {
21857
- const output = (0, import_node_child_process11.execSync)(`iptables -n -L ${this.chain}`, { encoding: "utf-8" });
21951
+ const output = (0, import_node_child_process12.execSync)(`iptables -n -L ${this.chain}`, { encoding: "utf-8" });
21858
21952
  return output.includes(ip);
21859
21953
  } catch {
21860
21954
  return false;
@@ -21862,7 +21956,7 @@ var IptablesAdapter = class {
21862
21956
  }
21863
21957
  async listBlocked() {
21864
21958
  try {
21865
- const output = (0, import_node_child_process11.execSync)(`iptables -n -L ${this.chain}`, { encoding: "utf-8" });
21959
+ const output = (0, import_node_child_process12.execSync)(`iptables -n -L ${this.chain}`, { encoding: "utf-8" });
21866
21960
  const ips = [];
21867
21961
  for (const line of output.split("\n")) {
21868
21962
  const match = line.match(/DROP\s+all\s+--\s+(\d+\.\d+\.\d+\.\d+)/);
@@ -21919,8 +22013,8 @@ function detectFirewallAdapter(backend = "auto") {
21919
22013
 
21920
22014
  // src/daemon/firewall/remediation.ts
21921
22015
  var import_node_fs36 = require("fs");
21922
- var import_node_child_process12 = require("child_process");
21923
- var import_node_net15 = require("net");
22016
+ var import_node_child_process13 = require("child_process");
22017
+ var import_node_net16 = require("net");
21924
22018
 
21925
22019
  // src/daemon/firewall/crawlers.ts
21926
22020
  var import_node_dns2 = require("dns");
@@ -22009,7 +22103,7 @@ var defaultDiscovery = {
22009
22103
  return out;
22010
22104
  },
22011
22105
  keyLoginJournal: (sinceSeconds) => new Promise((resolve4) => {
22012
- (0, import_node_child_process12.execFile)(
22106
+ (0, import_node_child_process13.execFile)(
22013
22107
  "journalctl",
22014
22108
  ["_COMM=sshd", "_COMM=sshd-session", `--since=-${Math.ceil(sinceSeconds / 86400)}d`, "-o", "short-unix", "--no-pager", "--grep", "Accepted publickey"],
22015
22109
  { maxBuffer: 32 * 1024 * 1024, timeout: 6e4 },
@@ -22089,7 +22183,7 @@ var RemediationManager = class {
22089
22183
  setTorExitCheck(check) {
22090
22184
  this.isTorExit = check;
22091
22185
  let dropped = 0;
22092
- this.protectedList = this.protectedList.filter((a) => !(0, import_node_net15.isIP)(a) || !check(a) || this.pinned.has(a) || this.config.allowlist.includes(a));
22186
+ this.protectedList = this.protectedList.filter((a) => !(0, import_node_net16.isIP)(a) || !check(a) || this.pinned.has(a) || this.config.allowlist.includes(a));
22093
22187
  for (const ip of Object.keys(this.operators)) {
22094
22188
  if (!check(ip)) continue;
22095
22189
  delete this.operators[ip];
@@ -22182,7 +22276,7 @@ var RemediationManager = class {
22182
22276
  if (remediation?.action !== "block") return;
22183
22277
  }
22184
22278
  const ip = event.source_ip;
22185
- if (!ip || (0, import_node_net15.isIP)(ip) === 0) return;
22279
+ if (!ip || (0, import_node_net16.isIP)(ip) === 0) return;
22186
22280
  const strict = remediation?.override_operator_protection === true;
22187
22281
  if (strict ? this.isFleetAddress(ip) : this.isAllowlisted(ip)) {
22188
22282
  if (strict) this.logLine(`[remediation] not banning ${ip}: it is one of our own addresses (${event.message})`);
@@ -22211,7 +22305,7 @@ var RemediationManager = class {
22211
22305
  * 4th offence and so on, clamped to `max_ban_seconds`.
22212
22306
  */
22213
22307
  async ban(ip, reason, opts = {}) {
22214
- if ((0, import_node_net15.isIP)(ip) === 0) return { ok: false, error: `not an IP address: ${ip}` };
22308
+ if ((0, import_node_net16.isIP)(ip) === 0) return { ok: false, error: `not an IP address: ${ip}` };
22215
22309
  if (opts.strict ? this.isFleetAddress(ip) : this.isAllowlisted(ip)) {
22216
22310
  return { ok: false, error: `${ip} is protected and can never be blocked` };
22217
22311
  }
@@ -22488,7 +22582,7 @@ var RemediationManager = class {
22488
22582
  if (saved && typeof saved === "object" && this.config.protect_recent_operators) {
22489
22583
  const cutoff = Date.now() - this.config.operator_memory_seconds * 1e3;
22490
22584
  for (const [ip, at] of Object.entries(saved)) {
22491
- if (typeof at !== "number" || at < cutoff || !(0, import_node_net15.isIP)(ip)) continue;
22585
+ if (typeof at !== "number" || at < cutoff || !(0, import_node_net16.isIP)(ip)) continue;
22492
22586
  if (this.blocklist.some((b) => b.ip === ip && b.strict)) continue;
22493
22587
  this.operators[ip] = at;
22494
22588
  if (!this.protectedList.includes(ip)) this.protectedList.push(ip);
@@ -22832,7 +22926,7 @@ async function flushTelemetry(timeoutMs = 2e3) {
22832
22926
  }
22833
22927
 
22834
22928
  // src/core/node-runtime.ts
22835
- var import_node_child_process13 = require("child_process");
22929
+ var import_node_child_process14 = require("child_process");
22836
22930
  var MIN_DAEMON_NODE_MAJOR = 22;
22837
22931
  function nodeMajor(version) {
22838
22932
  const m = String(version ?? "").trim().replace(/^v/, "").match(/^(\d+)\./);
@@ -22855,7 +22949,7 @@ function readVersion2() {
22855
22949
  }
22856
22950
  }
22857
22951
  async function runHardeningChecks() {
22858
- const { stdout } = await (0, import_node_util.promisify)(import_node_child_process14.execFile)(
22952
+ const { stdout } = await (0, import_node_util.promisify)(import_node_child_process15.execFile)(
22859
22953
  process.execPath,
22860
22954
  [(0, import_node_path30.join)(__dirname, "index.js"), "harden", "--json", "--no-upload"],
22861
22955
  { timeout: 12e4, maxBuffer: 4 * 1024 * 1024 }
@@ -22869,7 +22963,7 @@ async function runHardeningChecks() {
22869
22963
  return patches ? [...findings, patches] : findings;
22870
22964
  }
22871
22965
  async function runPatchCheck() {
22872
- const { stdout } = await (0, import_node_util.promisify)(import_node_child_process14.execFile)(
22966
+ const { stdout } = await (0, import_node_util.promisify)(import_node_child_process15.execFile)(
22873
22967
  process.execPath,
22874
22968
  [(0, import_node_path30.join)(__dirname, "index.js"), "patches", "--json", "--no-upload"],
22875
22969
  { timeout: 3e5, maxBuffer: 16 * 1024 * 1024 }
@@ -22904,9 +22998,9 @@ async function runDaemon() {
22904
22998
  logLine(`[daemon] starting threatcrushd v${version} mode=${PATHS.mode}`);
22905
22999
  const restartDaemon = () => {
22906
23000
  if (PATHS.mode === "system") {
22907
- (0, import_node_child_process14.spawn)("systemctl", ["restart", "threatcrushd"], { detached: true, stdio: "ignore" }).unref();
23001
+ (0, import_node_child_process15.spawn)("systemctl", ["restart", "threatcrushd"], { detached: true, stdio: "ignore" }).unref();
22908
23002
  } else {
22909
- (0, import_node_child_process14.spawn)(process.execPath, process.argv.slice(1), { detached: true, stdio: "ignore" }).unref();
23003
+ (0, import_node_child_process15.spawn)(process.execPath, process.argv.slice(1), { detached: true, stdio: "ignore" }).unref();
22910
23004
  setTimeout(() => process.exit(0), 500).unref();
22911
23005
  }
22912
23006
  };
@@ -23042,6 +23136,12 @@ async function runDaemon() {
23042
23136
  log: logLine
23043
23137
  });
23044
23138
  sshIntrusion.start();
23139
+ const torBlock = new TorExitBlockEnforcer({
23140
+ exits: torExits,
23141
+ enabled: () => remediation.blockTorSsh,
23142
+ log: logLine
23143
+ });
23144
+ torBlock.start();
23045
23145
  const lightningWatch = new LightningWatch({
23046
23146
  bus,
23047
23147
  enabled: config.lightning?.enabled !== false,
@@ -23112,6 +23212,7 @@ async function runDaemon() {
23112
23212
  if (torPinned || typeof block_tor_ssh !== "boolean" || block_tor_ssh === remediation.blockTorSsh) return;
23113
23213
  remediation.setBlockTorSsh(block_tor_ssh);
23114
23214
  logLine(`[ssh-intrusion] Tor SSH ban ${block_tor_ssh ? "ON" : "OFF"} (org setting from the dashboard)`);
23215
+ void torBlock.sync();
23115
23216
  }
23116
23217
  });
23117
23218
  allowlistSync.start();