@profullstack/threatcrush 0.12.8 → 0.13.1

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
@@ -3342,6 +3342,7 @@ var NetworkMonitor = class {
3342
3342
 
3343
3343
  // src/modules/dns-monitor/index.ts
3344
3344
  var import_node_fs8 = require("fs");
3345
+ var import_node_child_process3 = require("child_process");
3345
3346
  var import_node_readline2 = require("readline");
3346
3347
  var DNS_LOG_SOURCES = [
3347
3348
  "/var/log/syslog",
@@ -3353,10 +3354,30 @@ var DNS_LOG_SOURCES = [
3353
3354
  "/var/log/pihole.log"
3354
3355
  // Pi-hole
3355
3356
  ];
3357
+ function qtypeName(qtype) {
3358
+ const names = {
3359
+ 1: "A",
3360
+ 2: "NS",
3361
+ 5: "CNAME",
3362
+ 6: "SOA",
3363
+ 12: "PTR",
3364
+ 15: "MX",
3365
+ 16: "TXT",
3366
+ 28: "AAAA",
3367
+ 33: "SRV",
3368
+ 35: "NAPTR",
3369
+ 43: "DS",
3370
+ 48: "DNSKEY",
3371
+ 65: "HTTPS",
3372
+ 255: "ANY"
3373
+ };
3374
+ return names[qtype] ?? `TYPE${qtype}`;
3375
+ }
3356
3376
  var DnsMonitor = class {
3357
- // Shannon entropy threshold for DGA
3358
- constructor(bus2) {
3377
+ constructor(bus2, options = {}) {
3359
3378
  this.bus = bus2;
3379
+ this.extraPaths = options.log_paths ?? [];
3380
+ this.journalUnits = options.journal_units ?? [];
3360
3381
  }
3361
3382
  bus;
3362
3383
  active = false;
@@ -3373,8 +3394,13 @@ var DnsMonitor = class {
3373
3394
  // unique high-entropy domains per window
3374
3395
  dgaWindowMs = 6e4;
3375
3396
  entropyThreshold = 3.5;
3397
+ // Shannon entropy threshold for DGA
3398
+ /** Extra files and journal units from `[modules.dns-monitor]` in the config. */
3399
+ extraPaths = [];
3400
+ journalUnits = [];
3401
+ journalTails = [];
3376
3402
  start() {
3377
- const sources = DNS_LOG_SOURCES.filter((p) => {
3403
+ const sources = [...DNS_LOG_SOURCES, ...this.extraPaths].filter((p) => {
3378
3404
  if (!(0, import_node_fs8.existsSync)(p)) return false;
3379
3405
  try {
3380
3406
  (0, import_node_fs8.accessSync)(p, import_node_fs8.constants.R_OK);
@@ -3383,7 +3409,8 @@ var DnsMonitor = class {
3383
3409
  return false;
3384
3410
  }
3385
3411
  });
3386
- if (sources.length === 0) return false;
3412
+ for (const unit of this.journalUnits) this.tailJournalUnit(unit);
3413
+ if (sources.length === 0 && this.journalTails.length === 0) return false;
3387
3414
  this.active = true;
3388
3415
  for (const src of sources) {
3389
3416
  this.tailLog(src);
@@ -3394,8 +3421,30 @@ var DnsMonitor = class {
3394
3421
  stop() {
3395
3422
  for (const t of this.timers.values()) clearInterval(t);
3396
3423
  this.timers.clear();
3424
+ for (const child of this.journalTails) {
3425
+ try {
3426
+ child.kill("SIGTERM");
3427
+ } catch {
3428
+ }
3429
+ }
3430
+ this.journalTails = [];
3397
3431
  this.active = false;
3398
3432
  }
3433
+ /** Follow one systemd unit's journal, feeding its lines to the same parser. */
3434
+ tailJournalUnit(unit) {
3435
+ try {
3436
+ const child = (0, import_node_child_process3.spawn)("journalctl", ["-u", unit, "-f", "-n", "0", "--output=cat"], {
3437
+ stdio: ["ignore", "pipe", "ignore"]
3438
+ });
3439
+ if (!child.stdout) return;
3440
+ const rl = (0, import_node_readline2.createInterface)({ input: child.stdout });
3441
+ rl.on("line", (line) => this.parseDnsLine(line));
3442
+ child.on("error", () => {
3443
+ });
3444
+ this.journalTails.push(child);
3445
+ } catch {
3446
+ }
3447
+ }
3399
3448
  isActive() {
3400
3449
  return this.active;
3401
3450
  }
@@ -3428,6 +3477,18 @@ var DnsMonitor = class {
3428
3477
  rl.on("close", () => this.positions.set(path, stat.size));
3429
3478
  }
3430
3479
  parseDnsLine(line) {
3480
+ const moshpit = line.match(
3481
+ /\[dns\]\s+(?:udp|tcp|doh)\s+(\S+)\s+(\S+)\s+(\d+)\s+(\w+)/i
3482
+ );
3483
+ if (moshpit) {
3484
+ this.domainBuffer.push({
3485
+ type: qtypeName(Number.parseInt(moshpit[3], 10)),
3486
+ domain: moshpit[2],
3487
+ source_ip: moshpit[1],
3488
+ timestamp: Date.now()
3489
+ });
3490
+ return;
3491
+ }
3431
3492
  const resolvedMatch = line.match(/query\[(\w+)\]\s+(\S+)\s+from\s+(\S+)/i);
3432
3493
  if (resolvedMatch) {
3433
3494
  this.domainBuffer.push({
@@ -3669,7 +3730,11 @@ var ModuleHost = class {
3669
3730
  this.bus.announceModule("network-monitor", "running", nmod.detail);
3670
3731
  }
3671
3732
  }
3672
- this.dnsMonitor = new DnsMonitor(this.bus);
3733
+ const dnsConfig = loadModuleConfigs(PATHS.confD).get("dns-monitor") ?? {};
3734
+ this.dnsMonitor = new DnsMonitor(this.bus, {
3735
+ log_paths: Array.isArray(dnsConfig.log_paths) ? dnsConfig.log_paths : void 0,
3736
+ journal_units: Array.isArray(dnsConfig.journal_units) ? dnsConfig.journal_units : void 0
3737
+ });
3673
3738
  if (this.dnsMonitor.start()) {
3674
3739
  const dmod = this.modules.get("dns-monitor");
3675
3740
  if (dmod) {
@@ -8841,7 +8906,7 @@ function loadAllRules(customDir) {
8841
8906
  }
8842
8907
 
8843
8908
  // src/daemon/firewall/adapters.ts
8844
- var import_node_child_process3 = require("child_process");
8909
+ var import_node_child_process4 = require("child_process");
8845
8910
  var import_node_fs17 = require("fs");
8846
8911
  var import_node_path13 = require("path");
8847
8912
  var import_node_net2 = require("net");
@@ -8877,11 +8942,11 @@ var Fail2banAdapter = class {
8877
8942
  enforces = true;
8878
8943
  jailReady = false;
8879
8944
  isAvailable() {
8880
- const result = (0, import_node_child_process3.spawnSync)("fail2ban-client", ["ping"], { stdio: "pipe" });
8945
+ const result = (0, import_node_child_process4.spawnSync)("fail2ban-client", ["ping"], { stdio: "pipe" });
8881
8946
  return result.status === 0;
8882
8947
  }
8883
8948
  client(args) {
8884
- return (0, import_node_child_process3.execFileSync)("fail2ban-client", args, {
8949
+ return (0, import_node_child_process4.execFileSync)("fail2ban-client", args, {
8885
8950
  encoding: "utf-8",
8886
8951
  stdio: ["ignore", "pipe", "pipe"]
8887
8952
  });
@@ -8973,35 +9038,35 @@ var NftablesAdapter = class {
8973
9038
  table = "threatcrush";
8974
9039
  set = "blocklist";
8975
9040
  isAvailable() {
8976
- const result = (0, import_node_child_process3.spawnSync)("nft", ["--version"], { stdio: "pipe" });
9041
+ const result = (0, import_node_child_process4.spawnSync)("nft", ["--version"], { stdio: "pipe" });
8977
9042
  return result.status === 0;
8978
9043
  }
8979
9044
  ensureSetup() {
8980
9045
  try {
8981
- (0, import_node_child_process3.execSync)(`nft list table inet ${this.table} 2>/dev/null`, { stdio: "pipe" });
9046
+ (0, import_node_child_process4.execSync)(`nft list table inet ${this.table} 2>/dev/null`, { stdio: "pipe" });
8982
9047
  } catch {
8983
- (0, import_node_child_process3.execSync)(`nft add table inet ${this.table}`);
8984
- (0, import_node_child_process3.execSync)(`nft add set inet ${this.table} ${this.set} '{ type ipv4_addr; flags timeout; }'`);
8985
- (0, import_node_child_process3.execSync)(`nft add chain inet ${this.table} input '{ type filter hook input priority -1; policy accept; }'`);
8986
- (0, import_node_child_process3.execSync)(`nft add rule inet ${this.table} input ip saddr @${this.set} drop`);
9048
+ (0, import_node_child_process4.execSync)(`nft add table inet ${this.table}`);
9049
+ (0, import_node_child_process4.execSync)(`nft add set inet ${this.table} ${this.set} '{ type ipv4_addr; flags timeout; }'`);
9050
+ (0, import_node_child_process4.execSync)(`nft add chain inet ${this.table} input '{ type filter hook input priority -1; policy accept; }'`);
9051
+ (0, import_node_child_process4.execSync)(`nft add rule inet ${this.table} input ip saddr @${this.set} drop`);
8987
9052
  }
8988
9053
  }
8989
9054
  async block(ip) {
8990
9055
  assertValidFirewallIp(ip);
8991
9056
  this.ensureSetup();
8992
- (0, import_node_child_process3.execSync)(`nft add element inet ${this.table} ${this.set} '{ ${ip} }'`);
9057
+ (0, import_node_child_process4.execSync)(`nft add element inet ${this.table} ${this.set} '{ ${ip} }'`);
8993
9058
  }
8994
9059
  async unblock(ip) {
8995
9060
  assertValidFirewallIp(ip);
8996
9061
  try {
8997
- (0, import_node_child_process3.execSync)(`nft delete element inet ${this.table} ${this.set} '{ ${ip} }'`);
9062
+ (0, import_node_child_process4.execSync)(`nft delete element inet ${this.table} ${this.set} '{ ${ip} }'`);
8998
9063
  } catch {
8999
9064
  }
9000
9065
  }
9001
9066
  async isBlocked(ip) {
9002
9067
  assertValidFirewallIp(ip);
9003
9068
  try {
9004
- const output = (0, import_node_child_process3.execSync)(`nft list set inet ${this.table} ${this.set}`, { encoding: "utf-8" });
9069
+ const output = (0, import_node_child_process4.execSync)(`nft list set inet ${this.table} ${this.set}`, { encoding: "utf-8" });
9005
9070
  return output.includes(ip);
9006
9071
  } catch {
9007
9072
  return false;
@@ -9009,7 +9074,7 @@ var NftablesAdapter = class {
9009
9074
  }
9010
9075
  async listBlocked() {
9011
9076
  try {
9012
- const output = (0, import_node_child_process3.execSync)(`nft list set inet ${this.table} ${this.set}`, { encoding: "utf-8" });
9077
+ const output = (0, import_node_child_process4.execSync)(`nft list set inet ${this.table} ${this.set}`, { encoding: "utf-8" });
9013
9078
  const match = output.match(/elements\s*=\s*\{([^}]*)\}/);
9014
9079
  if (!match) return [];
9015
9080
  return match[1].split(",").map((s) => s.trim().split(/\s/)[0]).filter(Boolean);
@@ -9022,34 +9087,34 @@ var IptablesAdapter = class {
9022
9087
  name = "iptables";
9023
9088
  chain = "THREATCRUSH";
9024
9089
  isAvailable() {
9025
- const result = (0, import_node_child_process3.spawnSync)("iptables", ["--version"], { stdio: "pipe" });
9090
+ const result = (0, import_node_child_process4.spawnSync)("iptables", ["--version"], { stdio: "pipe" });
9026
9091
  return result.status === 0;
9027
9092
  }
9028
9093
  ensureChain() {
9029
9094
  try {
9030
- (0, import_node_child_process3.execSync)(`iptables -n -L ${this.chain} 2>/dev/null`, { stdio: "pipe" });
9095
+ (0, import_node_child_process4.execSync)(`iptables -n -L ${this.chain} 2>/dev/null`, { stdio: "pipe" });
9031
9096
  } catch {
9032
- (0, import_node_child_process3.execSync)(`iptables -N ${this.chain}`);
9033
- (0, import_node_child_process3.execSync)(`iptables -I INPUT 1 -j ${this.chain}`);
9097
+ (0, import_node_child_process4.execSync)(`iptables -N ${this.chain}`);
9098
+ (0, import_node_child_process4.execSync)(`iptables -I INPUT 1 -j ${this.chain}`);
9034
9099
  }
9035
9100
  }
9036
9101
  async block(ip) {
9037
9102
  assertValidFirewallIp(ip);
9038
9103
  this.ensureChain();
9039
9104
  if (await this.isBlocked(ip)) return;
9040
- (0, import_node_child_process3.execSync)(`iptables -A ${this.chain} -s ${ip} -j DROP`);
9105
+ (0, import_node_child_process4.execSync)(`iptables -A ${this.chain} -s ${ip} -j DROP`);
9041
9106
  }
9042
9107
  async unblock(ip) {
9043
9108
  assertValidFirewallIp(ip);
9044
9109
  try {
9045
- (0, import_node_child_process3.execSync)(`iptables -D ${this.chain} -s ${ip} -j DROP`);
9110
+ (0, import_node_child_process4.execSync)(`iptables -D ${this.chain} -s ${ip} -j DROP`);
9046
9111
  } catch {
9047
9112
  }
9048
9113
  }
9049
9114
  async isBlocked(ip) {
9050
9115
  assertValidFirewallIp(ip);
9051
9116
  try {
9052
- const output = (0, import_node_child_process3.execSync)(`iptables -n -L ${this.chain}`, { encoding: "utf-8" });
9117
+ const output = (0, import_node_child_process4.execSync)(`iptables -n -L ${this.chain}`, { encoding: "utf-8" });
9053
9118
  return output.includes(ip);
9054
9119
  } catch {
9055
9120
  return false;
@@ -9057,7 +9122,7 @@ var IptablesAdapter = class {
9057
9122
  }
9058
9123
  async listBlocked() {
9059
9124
  try {
9060
- const output = (0, import_node_child_process3.execSync)(`iptables -n -L ${this.chain}`, { encoding: "utf-8" });
9125
+ const output = (0, import_node_child_process4.execSync)(`iptables -n -L ${this.chain}`, { encoding: "utf-8" });
9061
9126
  const ips = [];
9062
9127
  for (const line of output.split("\n")) {
9063
9128
  const match = line.match(/DROP\s+all\s+--\s+(\d+\.\d+\.\d+\.\d+)/);
@@ -9117,7 +9182,7 @@ var import_node_fs18 = require("fs");
9117
9182
  var import_node_net4 = require("net");
9118
9183
 
9119
9184
  // src/daemon/firewall/protected.ts
9120
- var import_node_child_process4 = require("child_process");
9185
+ var import_node_child_process5 = require("child_process");
9121
9186
  var import_node_net3 = require("net");
9122
9187
  var DEFAULT_PROTECTED = [
9123
9188
  "127.0.0.0/8",
@@ -9213,7 +9278,7 @@ function currentSshClient(env2 = process.env) {
9213
9278
  function establishedSshPeers() {
9214
9279
  const peers = /* @__PURE__ */ new Set();
9215
9280
  try {
9216
- const out = (0, import_node_child_process4.execFileSync)("ss", ["-tnpH"], {
9281
+ const out = (0, import_node_child_process5.execFileSync)("ss", ["-tnpH"], {
9217
9282
  encoding: "utf-8",
9218
9283
  stdio: ["ignore", "pipe", "ignore"],
9219
9284
  timeout: 3e3
@@ -9234,7 +9299,7 @@ function defaultGateways() {
9234
9299
  const found = /* @__PURE__ */ new Set();
9235
9300
  for (const args of [["-4", "route", "show", "default"], ["-6", "route", "show", "default"]]) {
9236
9301
  try {
9237
- const out = (0, import_node_child_process4.execFileSync)("ip", args, { encoding: "utf-8", stdio: ["ignore", "pipe", "ignore"] });
9302
+ const out = (0, import_node_child_process5.execFileSync)("ip", args, { encoding: "utf-8", stdio: ["ignore", "pipe", "ignore"] });
9238
9303
  for (const line of out.split("\n")) {
9239
9304
  const match = line.match(/\bvia\s+(\S+)/);
9240
9305
  if (match && (0, import_node_net3.isIP)(match[1])) found.add(match[1]);