@profullstack/threatcrush 0.12.7 → 0.13.0

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) {
@@ -5359,7 +5424,8 @@ var CODE_RULES = [
5359
5424
  severity: "high",
5360
5425
  languages: ["python"],
5361
5426
  pattern: /\brequests\.(?:get|request|head)\s*\(\s*[a-zA-Z_]\w*\s*[,)]|\burlopen\s*\(\s*[a-zA-Z_]\w*\s*[,)]|\bhttpx\.get\s*\(\s*[a-zA-Z_]\w*\s*[,)]/,
5362
- needsContext: true
5427
+ needsContext: true,
5428
+ constantRequestHostGuard: true
5363
5429
  },
5364
5430
  {
5365
5431
  id: "go-ssrf-outbound-request",
@@ -6274,6 +6340,21 @@ function identifierAfter(text, start) {
6274
6340
  while (isIdentifierPart(text[index])) index += 1;
6275
6341
  return text.slice(from, index);
6276
6342
  }
6343
+ function urlAuthority(text) {
6344
+ const found = /['"]\s*https?:\/\/([^'"/?#\s]*)/.exec(text);
6345
+ return found ? found[1] : null;
6346
+ }
6347
+ function requestHostIsConstant(line, fileText) {
6348
+ const call = /(?:urlopen|requests\.(?:get|request|head)|httpx\.get)\s*\(\s*([A-Za-z_]\w*)\s*[,)]/.exec(line);
6349
+ if (!call) return false;
6350
+ const assignment = new RegExp(String.raw`^[ \t]*${call[1]}[ \t]*=[ \t]*(.+)$`, "gm");
6351
+ const values = [...fileText.matchAll(assignment)].map((m) => m[1]);
6352
+ if (!values.length) return false;
6353
+ return values.every((value) => {
6354
+ const authority = urlAuthority(value);
6355
+ return authority !== null && /^[A-Za-z0-9.-]+(?::\d+)?$/.test(authority);
6356
+ });
6357
+ }
6277
6358
  function hasSafeRedirectValue(line, fileText) {
6278
6359
  const location = line.indexOf("window.location");
6279
6360
  const redirect = line.indexOf(".redirect");
@@ -6383,6 +6464,9 @@ function evaluateRule(rule, ctx) {
6383
6464
  if (rule.safeRedirectGuard && hasSafeRedirectValue(line, fileTextOf(ctx.lines))) {
6384
6465
  return null;
6385
6466
  }
6467
+ if (rule.constantRequestHostGuard && requestHostIsConstant(line, fileTextOf(ctx.lines))) {
6468
+ return null;
6469
+ }
6386
6470
  if (rule.filledBeforeUseGuard && bufferFilledBeforeUse(ctx)) return null;
6387
6471
  const guard = rule.guard === void 0 ? GENERIC_GUARD : rule.guard;
6388
6472
  if (guard && (guard.test(line) || guard.test(context))) return null;
@@ -8822,7 +8906,7 @@ function loadAllRules(customDir) {
8822
8906
  }
8823
8907
 
8824
8908
  // src/daemon/firewall/adapters.ts
8825
- var import_node_child_process3 = require("child_process");
8909
+ var import_node_child_process4 = require("child_process");
8826
8910
  var import_node_fs17 = require("fs");
8827
8911
  var import_node_path13 = require("path");
8828
8912
  var import_node_net2 = require("net");
@@ -8858,11 +8942,11 @@ var Fail2banAdapter = class {
8858
8942
  enforces = true;
8859
8943
  jailReady = false;
8860
8944
  isAvailable() {
8861
- 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" });
8862
8946
  return result.status === 0;
8863
8947
  }
8864
8948
  client(args) {
8865
- return (0, import_node_child_process3.execFileSync)("fail2ban-client", args, {
8949
+ return (0, import_node_child_process4.execFileSync)("fail2ban-client", args, {
8866
8950
  encoding: "utf-8",
8867
8951
  stdio: ["ignore", "pipe", "pipe"]
8868
8952
  });
@@ -8954,35 +9038,35 @@ var NftablesAdapter = class {
8954
9038
  table = "threatcrush";
8955
9039
  set = "blocklist";
8956
9040
  isAvailable() {
8957
- 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" });
8958
9042
  return result.status === 0;
8959
9043
  }
8960
9044
  ensureSetup() {
8961
9045
  try {
8962
- (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" });
8963
9047
  } catch {
8964
- (0, import_node_child_process3.execSync)(`nft add table inet ${this.table}`);
8965
- (0, import_node_child_process3.execSync)(`nft add set inet ${this.table} ${this.set} '{ type ipv4_addr; flags timeout; }'`);
8966
- (0, import_node_child_process3.execSync)(`nft add chain inet ${this.table} input '{ type filter hook input priority -1; policy accept; }'`);
8967
- (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`);
8968
9052
  }
8969
9053
  }
8970
9054
  async block(ip) {
8971
9055
  assertValidFirewallIp(ip);
8972
9056
  this.ensureSetup();
8973
- (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} }'`);
8974
9058
  }
8975
9059
  async unblock(ip) {
8976
9060
  assertValidFirewallIp(ip);
8977
9061
  try {
8978
- (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} }'`);
8979
9063
  } catch {
8980
9064
  }
8981
9065
  }
8982
9066
  async isBlocked(ip) {
8983
9067
  assertValidFirewallIp(ip);
8984
9068
  try {
8985
- 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" });
8986
9070
  return output.includes(ip);
8987
9071
  } catch {
8988
9072
  return false;
@@ -8990,7 +9074,7 @@ var NftablesAdapter = class {
8990
9074
  }
8991
9075
  async listBlocked() {
8992
9076
  try {
8993
- 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" });
8994
9078
  const match = output.match(/elements\s*=\s*\{([^}]*)\}/);
8995
9079
  if (!match) return [];
8996
9080
  return match[1].split(",").map((s) => s.trim().split(/\s/)[0]).filter(Boolean);
@@ -9003,34 +9087,34 @@ var IptablesAdapter = class {
9003
9087
  name = "iptables";
9004
9088
  chain = "THREATCRUSH";
9005
9089
  isAvailable() {
9006
- 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" });
9007
9091
  return result.status === 0;
9008
9092
  }
9009
9093
  ensureChain() {
9010
9094
  try {
9011
- (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" });
9012
9096
  } catch {
9013
- (0, import_node_child_process3.execSync)(`iptables -N ${this.chain}`);
9014
- (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}`);
9015
9099
  }
9016
9100
  }
9017
9101
  async block(ip) {
9018
9102
  assertValidFirewallIp(ip);
9019
9103
  this.ensureChain();
9020
9104
  if (await this.isBlocked(ip)) return;
9021
- (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`);
9022
9106
  }
9023
9107
  async unblock(ip) {
9024
9108
  assertValidFirewallIp(ip);
9025
9109
  try {
9026
- (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`);
9027
9111
  } catch {
9028
9112
  }
9029
9113
  }
9030
9114
  async isBlocked(ip) {
9031
9115
  assertValidFirewallIp(ip);
9032
9116
  try {
9033
- 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" });
9034
9118
  return output.includes(ip);
9035
9119
  } catch {
9036
9120
  return false;
@@ -9038,7 +9122,7 @@ var IptablesAdapter = class {
9038
9122
  }
9039
9123
  async listBlocked() {
9040
9124
  try {
9041
- 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" });
9042
9126
  const ips = [];
9043
9127
  for (const line of output.split("\n")) {
9044
9128
  const match = line.match(/DROP\s+all\s+--\s+(\d+\.\d+\.\d+\.\d+)/);
@@ -9098,7 +9182,7 @@ var import_node_fs18 = require("fs");
9098
9182
  var import_node_net4 = require("net");
9099
9183
 
9100
9184
  // src/daemon/firewall/protected.ts
9101
- var import_node_child_process4 = require("child_process");
9185
+ var import_node_child_process5 = require("child_process");
9102
9186
  var import_node_net3 = require("net");
9103
9187
  var DEFAULT_PROTECTED = [
9104
9188
  "127.0.0.0/8",
@@ -9194,7 +9278,7 @@ function currentSshClient(env2 = process.env) {
9194
9278
  function establishedSshPeers() {
9195
9279
  const peers = /* @__PURE__ */ new Set();
9196
9280
  try {
9197
- const out = (0, import_node_child_process4.execFileSync)("ss", ["-tnpH"], {
9281
+ const out = (0, import_node_child_process5.execFileSync)("ss", ["-tnpH"], {
9198
9282
  encoding: "utf-8",
9199
9283
  stdio: ["ignore", "pipe", "ignore"],
9200
9284
  timeout: 3e3
@@ -9215,7 +9299,7 @@ function defaultGateways() {
9215
9299
  const found = /* @__PURE__ */ new Set();
9216
9300
  for (const args of [["-4", "route", "show", "default"], ["-6", "route", "show", "default"]]) {
9217
9301
  try {
9218
- 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"] });
9219
9303
  for (const line of out.split("\n")) {
9220
9304
  const match = line.match(/\bvia\s+(\S+)/);
9221
9305
  if (match && (0, import_node_net3.isIP)(match[1])) found.add(match[1]);