@profullstack/threatcrush 0.13.79 → 0.13.80

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,88 @@ 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
+ runNft;
20752
+ log;
20753
+ intervalMs;
20754
+ sshPort;
20755
+ start() {
20756
+ void this.sync();
20757
+ this.timer = setInterval(() => void this.sync(), this.intervalMs);
20758
+ this.timer.unref?.();
20759
+ }
20760
+ stop() {
20761
+ if (this.timer) clearInterval(this.timer);
20762
+ this.timer = null;
20763
+ }
20764
+ /** Apply (or tear down) the block to match the current setting and list. */
20765
+ async sync() {
20766
+ if (!this.opts.enabled()) {
20767
+ await this.teardown();
20768
+ return;
20769
+ }
20770
+ const ipv4 = this.opts.exits.values().filter((ip) => (0, import_node_net14.isIP)(ip) === 4);
20771
+ if (ipv4.length === 0) return;
20772
+ try {
20773
+ await this.apply(ipv4);
20774
+ if (!this.applied) {
20775
+ this.log(`[tor] proactively blocking ${ipv4.length} Tor exits on tcp/${this.sshPort} (table inet ${TOR_NFT_TABLE})`);
20776
+ }
20777
+ this.applied = true;
20778
+ } catch (err) {
20779
+ this.log(`[tor] could not apply the proactive Tor exit block: ${err.message}`);
20780
+ }
20781
+ }
20782
+ /** One atomic `nft -f -` transaction: declare the table, then load the set. */
20783
+ async apply(ipv4) {
20784
+ const t = TOR_NFT_TABLE;
20785
+ const ruleset = `add table inet ${t}
20786
+ add set inet ${t} exits { type ipv4_addr; }
20787
+ add chain inet ${t} input { type filter hook input priority -100 ; policy accept ; }
20788
+ flush chain inet ${t} input
20789
+ add rule inet ${t} input tcp dport ${this.sshPort} ip saddr @exits counter drop
20790
+ flush set inet ${t} exits
20791
+ add element inet ${t} exits { ${ipv4.join(", ")} }
20792
+ `;
20793
+ await this.runNft(["-f", "-"], ruleset);
20794
+ }
20795
+ async teardown() {
20796
+ if (!this.applied) return;
20797
+ try {
20798
+ await this.runNft(["delete", "table", "inet", TOR_NFT_TABLE]);
20799
+ this.log("[tor] removed the proactive Tor exit block (block_tor_ssh off)");
20800
+ } catch {
20801
+ }
20802
+ this.applied = false;
20803
+ }
20804
+ };
20805
+
20720
20806
  // src/daemon/rules/engine.ts
20721
20807
  var MAX_WINDOW_ENTRIES = 1e3;
20722
20808
  function live(window) {
@@ -21642,17 +21728,17 @@ function loadAllRules(customDir) {
21642
21728
  }
21643
21729
 
21644
21730
  // src/daemon/firewall/adapters.ts
21645
- var import_node_child_process11 = require("child_process");
21731
+ var import_node_child_process12 = require("child_process");
21646
21732
  var import_node_fs35 = require("fs");
21647
21733
  var import_node_path27 = require("path");
21648
- var import_node_net14 = require("net");
21734
+ var import_node_net15 = require("net");
21649
21735
  function assertValidFirewallIp(ip) {
21650
- if ((0, import_node_net14.isIP)(ip) !== 4) {
21736
+ if ((0, import_node_net15.isIP)(ip) !== 4) {
21651
21737
  throw new Error(`Invalid IPv4 address: ${ip}`);
21652
21738
  }
21653
21739
  }
21654
21740
  function assertBannableIp(ip) {
21655
- if ((0, import_node_net14.isIP)(ip) === 0) {
21741
+ if ((0, import_node_net15.isIP)(ip) === 0) {
21656
21742
  throw new Error(`Invalid IP address: ${ip}`);
21657
21743
  }
21658
21744
  }
@@ -21678,11 +21764,11 @@ var Fail2banAdapter = class {
21678
21764
  enforces = true;
21679
21765
  jailReady = false;
21680
21766
  isAvailable() {
21681
- const result = (0, import_node_child_process11.spawnSync)("fail2ban-client", ["ping"], { stdio: "pipe" });
21767
+ const result = (0, import_node_child_process12.spawnSync)("fail2ban-client", ["ping"], { stdio: "pipe" });
21682
21768
  return result.status === 0;
21683
21769
  }
21684
21770
  client(args) {
21685
- return (0, import_node_child_process11.execFileSync)("fail2ban-client", args, {
21771
+ return (0, import_node_child_process12.execFileSync)("fail2ban-client", args, {
21686
21772
  encoding: "utf-8",
21687
21773
  stdio: ["ignore", "pipe", "pipe"]
21688
21774
  });
@@ -21758,7 +21844,7 @@ var Fail2banAdapter = class {
21758
21844
  const status = this.client(["status", this.jail]);
21759
21845
  const line = status.split("\n").find((l) => /Banned IP list:/i.test(l));
21760
21846
  if (!line) return [];
21761
- return line.split(":").slice(1).join(":").trim().split(/\s+/).filter((s) => (0, import_node_net14.isIP)(s) !== 0);
21847
+ return line.split(":").slice(1).join(":").trim().split(/\s+/).filter((s) => (0, import_node_net15.isIP)(s) !== 0);
21762
21848
  } catch {
21763
21849
  return [];
21764
21850
  }
@@ -21768,45 +21854,45 @@ var Fail2banAdapter = class {
21768
21854
  function parseBannedList(output) {
21769
21855
  const found = [];
21770
21856
  for (const match of output.matchAll(/['"]([^'"]+)['"]/g)) {
21771
- if ((0, import_node_net14.isIP)(match[1]) !== 0) found.push(match[1]);
21857
+ if ((0, import_node_net15.isIP)(match[1]) !== 0) found.push(match[1]);
21772
21858
  }
21773
21859
  if (found.length > 0) return found;
21774
- return output.replace(/[[\],]/g, " ").split(/\s+/).filter((s) => (0, import_node_net14.isIP)(s) !== 0);
21860
+ return output.replace(/[[\],]/g, " ").split(/\s+/).filter((s) => (0, import_node_net15.isIP)(s) !== 0);
21775
21861
  }
21776
21862
  var NftablesAdapter = class {
21777
21863
  name = "nftables";
21778
21864
  table = "threatcrush";
21779
21865
  set = "blocklist";
21780
21866
  isAvailable() {
21781
- const result = (0, import_node_child_process11.spawnSync)("nft", ["--version"], { stdio: "pipe" });
21867
+ const result = (0, import_node_child_process12.spawnSync)("nft", ["--version"], { stdio: "pipe" });
21782
21868
  return result.status === 0;
21783
21869
  }
21784
21870
  ensureSetup() {
21785
21871
  try {
21786
- (0, import_node_child_process11.execSync)(`nft list table inet ${this.table} 2>/dev/null`, { stdio: "pipe" });
21872
+ (0, import_node_child_process12.execSync)(`nft list table inet ${this.table} 2>/dev/null`, { stdio: "pipe" });
21787
21873
  } 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`);
21874
+ (0, import_node_child_process12.execSync)(`nft add table inet ${this.table}`);
21875
+ (0, import_node_child_process12.execSync)(`nft add set inet ${this.table} ${this.set} '{ type ipv4_addr; flags timeout; }'`);
21876
+ (0, import_node_child_process12.execSync)(`nft add chain inet ${this.table} input '{ type filter hook input priority -1; policy accept; }'`);
21877
+ (0, import_node_child_process12.execSync)(`nft add rule inet ${this.table} input ip saddr @${this.set} drop`);
21792
21878
  }
21793
21879
  }
21794
21880
  async block(ip) {
21795
21881
  assertValidFirewallIp(ip);
21796
21882
  this.ensureSetup();
21797
- (0, import_node_child_process11.execSync)(`nft add element inet ${this.table} ${this.set} '{ ${ip} }'`);
21883
+ (0, import_node_child_process12.execSync)(`nft add element inet ${this.table} ${this.set} '{ ${ip} }'`);
21798
21884
  }
21799
21885
  async unblock(ip) {
21800
21886
  assertValidFirewallIp(ip);
21801
21887
  try {
21802
- (0, import_node_child_process11.execSync)(`nft delete element inet ${this.table} ${this.set} '{ ${ip} }'`);
21888
+ (0, import_node_child_process12.execSync)(`nft delete element inet ${this.table} ${this.set} '{ ${ip} }'`);
21803
21889
  } catch {
21804
21890
  }
21805
21891
  }
21806
21892
  async isBlocked(ip) {
21807
21893
  assertValidFirewallIp(ip);
21808
21894
  try {
21809
- const output = (0, import_node_child_process11.execSync)(`nft list set inet ${this.table} ${this.set}`, { encoding: "utf-8" });
21895
+ const output = (0, import_node_child_process12.execSync)(`nft list set inet ${this.table} ${this.set}`, { encoding: "utf-8" });
21810
21896
  return output.includes(ip);
21811
21897
  } catch {
21812
21898
  return false;
@@ -21814,7 +21900,7 @@ var NftablesAdapter = class {
21814
21900
  }
21815
21901
  async listBlocked() {
21816
21902
  try {
21817
- 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" });
21818
21904
  const match = output.match(/elements\s*=\s*\{([^}]*)\}/);
21819
21905
  if (!match) return [];
21820
21906
  return match[1].split(",").map((s) => s.trim().split(/\s/)[0]).filter(Boolean);
@@ -21827,34 +21913,34 @@ var IptablesAdapter = class {
21827
21913
  name = "iptables";
21828
21914
  chain = "THREATCRUSH";
21829
21915
  isAvailable() {
21830
- const result = (0, import_node_child_process11.spawnSync)("iptables", ["--version"], { stdio: "pipe" });
21916
+ const result = (0, import_node_child_process12.spawnSync)("iptables", ["--version"], { stdio: "pipe" });
21831
21917
  return result.status === 0;
21832
21918
  }
21833
21919
  ensureChain() {
21834
21920
  try {
21835
- (0, import_node_child_process11.execSync)(`iptables -n -L ${this.chain} 2>/dev/null`, { stdio: "pipe" });
21921
+ (0, import_node_child_process12.execSync)(`iptables -n -L ${this.chain} 2>/dev/null`, { stdio: "pipe" });
21836
21922
  } 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}`);
21923
+ (0, import_node_child_process12.execSync)(`iptables -N ${this.chain}`);
21924
+ (0, import_node_child_process12.execSync)(`iptables -I INPUT 1 -j ${this.chain}`);
21839
21925
  }
21840
21926
  }
21841
21927
  async block(ip) {
21842
21928
  assertValidFirewallIp(ip);
21843
21929
  this.ensureChain();
21844
21930
  if (await this.isBlocked(ip)) return;
21845
- (0, import_node_child_process11.execSync)(`iptables -A ${this.chain} -s ${ip} -j DROP`);
21931
+ (0, import_node_child_process12.execSync)(`iptables -A ${this.chain} -s ${ip} -j DROP`);
21846
21932
  }
21847
21933
  async unblock(ip) {
21848
21934
  assertValidFirewallIp(ip);
21849
21935
  try {
21850
- (0, import_node_child_process11.execSync)(`iptables -D ${this.chain} -s ${ip} -j DROP`);
21936
+ (0, import_node_child_process12.execSync)(`iptables -D ${this.chain} -s ${ip} -j DROP`);
21851
21937
  } catch {
21852
21938
  }
21853
21939
  }
21854
21940
  async isBlocked(ip) {
21855
21941
  assertValidFirewallIp(ip);
21856
21942
  try {
21857
- const output = (0, import_node_child_process11.execSync)(`iptables -n -L ${this.chain}`, { encoding: "utf-8" });
21943
+ const output = (0, import_node_child_process12.execSync)(`iptables -n -L ${this.chain}`, { encoding: "utf-8" });
21858
21944
  return output.includes(ip);
21859
21945
  } catch {
21860
21946
  return false;
@@ -21862,7 +21948,7 @@ var IptablesAdapter = class {
21862
21948
  }
21863
21949
  async listBlocked() {
21864
21950
  try {
21865
- 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" });
21866
21952
  const ips = [];
21867
21953
  for (const line of output.split("\n")) {
21868
21954
  const match = line.match(/DROP\s+all\s+--\s+(\d+\.\d+\.\d+\.\d+)/);
@@ -21919,8 +22005,8 @@ function detectFirewallAdapter(backend = "auto") {
21919
22005
 
21920
22006
  // src/daemon/firewall/remediation.ts
21921
22007
  var import_node_fs36 = require("fs");
21922
- var import_node_child_process12 = require("child_process");
21923
- var import_node_net15 = require("net");
22008
+ var import_node_child_process13 = require("child_process");
22009
+ var import_node_net16 = require("net");
21924
22010
 
21925
22011
  // src/daemon/firewall/crawlers.ts
21926
22012
  var import_node_dns2 = require("dns");
@@ -22009,7 +22095,7 @@ var defaultDiscovery = {
22009
22095
  return out;
22010
22096
  },
22011
22097
  keyLoginJournal: (sinceSeconds) => new Promise((resolve4) => {
22012
- (0, import_node_child_process12.execFile)(
22098
+ (0, import_node_child_process13.execFile)(
22013
22099
  "journalctl",
22014
22100
  ["_COMM=sshd", "_COMM=sshd-session", `--since=-${Math.ceil(sinceSeconds / 86400)}d`, "-o", "short-unix", "--no-pager", "--grep", "Accepted publickey"],
22015
22101
  { maxBuffer: 32 * 1024 * 1024, timeout: 6e4 },
@@ -22089,7 +22175,7 @@ var RemediationManager = class {
22089
22175
  setTorExitCheck(check) {
22090
22176
  this.isTorExit = check;
22091
22177
  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));
22178
+ this.protectedList = this.protectedList.filter((a) => !(0, import_node_net16.isIP)(a) || !check(a) || this.pinned.has(a) || this.config.allowlist.includes(a));
22093
22179
  for (const ip of Object.keys(this.operators)) {
22094
22180
  if (!check(ip)) continue;
22095
22181
  delete this.operators[ip];
@@ -22182,7 +22268,7 @@ var RemediationManager = class {
22182
22268
  if (remediation?.action !== "block") return;
22183
22269
  }
22184
22270
  const ip = event.source_ip;
22185
- if (!ip || (0, import_node_net15.isIP)(ip) === 0) return;
22271
+ if (!ip || (0, import_node_net16.isIP)(ip) === 0) return;
22186
22272
  const strict = remediation?.override_operator_protection === true;
22187
22273
  if (strict ? this.isFleetAddress(ip) : this.isAllowlisted(ip)) {
22188
22274
  if (strict) this.logLine(`[remediation] not banning ${ip}: it is one of our own addresses (${event.message})`);
@@ -22211,7 +22297,7 @@ var RemediationManager = class {
22211
22297
  * 4th offence and so on, clamped to `max_ban_seconds`.
22212
22298
  */
22213
22299
  async ban(ip, reason, opts = {}) {
22214
- if ((0, import_node_net15.isIP)(ip) === 0) return { ok: false, error: `not an IP address: ${ip}` };
22300
+ if ((0, import_node_net16.isIP)(ip) === 0) return { ok: false, error: `not an IP address: ${ip}` };
22215
22301
  if (opts.strict ? this.isFleetAddress(ip) : this.isAllowlisted(ip)) {
22216
22302
  return { ok: false, error: `${ip} is protected and can never be blocked` };
22217
22303
  }
@@ -22488,7 +22574,7 @@ var RemediationManager = class {
22488
22574
  if (saved && typeof saved === "object" && this.config.protect_recent_operators) {
22489
22575
  const cutoff = Date.now() - this.config.operator_memory_seconds * 1e3;
22490
22576
  for (const [ip, at] of Object.entries(saved)) {
22491
- if (typeof at !== "number" || at < cutoff || !(0, import_node_net15.isIP)(ip)) continue;
22577
+ if (typeof at !== "number" || at < cutoff || !(0, import_node_net16.isIP)(ip)) continue;
22492
22578
  if (this.blocklist.some((b) => b.ip === ip && b.strict)) continue;
22493
22579
  this.operators[ip] = at;
22494
22580
  if (!this.protectedList.includes(ip)) this.protectedList.push(ip);
@@ -22832,7 +22918,7 @@ async function flushTelemetry(timeoutMs = 2e3) {
22832
22918
  }
22833
22919
 
22834
22920
  // src/core/node-runtime.ts
22835
- var import_node_child_process13 = require("child_process");
22921
+ var import_node_child_process14 = require("child_process");
22836
22922
  var MIN_DAEMON_NODE_MAJOR = 22;
22837
22923
  function nodeMajor(version) {
22838
22924
  const m = String(version ?? "").trim().replace(/^v/, "").match(/^(\d+)\./);
@@ -22855,7 +22941,7 @@ function readVersion2() {
22855
22941
  }
22856
22942
  }
22857
22943
  async function runHardeningChecks() {
22858
- const { stdout } = await (0, import_node_util.promisify)(import_node_child_process14.execFile)(
22944
+ const { stdout } = await (0, import_node_util.promisify)(import_node_child_process15.execFile)(
22859
22945
  process.execPath,
22860
22946
  [(0, import_node_path30.join)(__dirname, "index.js"), "harden", "--json", "--no-upload"],
22861
22947
  { timeout: 12e4, maxBuffer: 4 * 1024 * 1024 }
@@ -22869,7 +22955,7 @@ async function runHardeningChecks() {
22869
22955
  return patches ? [...findings, patches] : findings;
22870
22956
  }
22871
22957
  async function runPatchCheck() {
22872
- const { stdout } = await (0, import_node_util.promisify)(import_node_child_process14.execFile)(
22958
+ const { stdout } = await (0, import_node_util.promisify)(import_node_child_process15.execFile)(
22873
22959
  process.execPath,
22874
22960
  [(0, import_node_path30.join)(__dirname, "index.js"), "patches", "--json", "--no-upload"],
22875
22961
  { timeout: 3e5, maxBuffer: 16 * 1024 * 1024 }
@@ -22904,9 +22990,9 @@ async function runDaemon() {
22904
22990
  logLine(`[daemon] starting threatcrushd v${version} mode=${PATHS.mode}`);
22905
22991
  const restartDaemon = () => {
22906
22992
  if (PATHS.mode === "system") {
22907
- (0, import_node_child_process14.spawn)("systemctl", ["restart", "threatcrushd"], { detached: true, stdio: "ignore" }).unref();
22993
+ (0, import_node_child_process15.spawn)("systemctl", ["restart", "threatcrushd"], { detached: true, stdio: "ignore" }).unref();
22908
22994
  } else {
22909
- (0, import_node_child_process14.spawn)(process.execPath, process.argv.slice(1), { detached: true, stdio: "ignore" }).unref();
22995
+ (0, import_node_child_process15.spawn)(process.execPath, process.argv.slice(1), { detached: true, stdio: "ignore" }).unref();
22910
22996
  setTimeout(() => process.exit(0), 500).unref();
22911
22997
  }
22912
22998
  };
@@ -23042,6 +23128,12 @@ async function runDaemon() {
23042
23128
  log: logLine
23043
23129
  });
23044
23130
  sshIntrusion.start();
23131
+ const torBlock = new TorExitBlockEnforcer({
23132
+ exits: torExits,
23133
+ enabled: () => remediation.blockTorSsh && firewallAdapter.name === "nftables",
23134
+ log: logLine
23135
+ });
23136
+ torBlock.start();
23045
23137
  const lightningWatch = new LightningWatch({
23046
23138
  bus,
23047
23139
  enabled: config.lightning?.enabled !== false,
@@ -23112,6 +23204,7 @@ async function runDaemon() {
23112
23204
  if (torPinned || typeof block_tor_ssh !== "boolean" || block_tor_ssh === remediation.blockTorSsh) return;
23113
23205
  remediation.setBlockTorSsh(block_tor_ssh);
23114
23206
  logLine(`[ssh-intrusion] Tor SSH ban ${block_tor_ssh ? "ON" : "OFF"} (org setting from the dashboard)`);
23207
+ void torBlock.sync();
23115
23208
  }
23116
23209
  });
23117
23210
  allowlistSync.start();