@profullstack/threatcrush 0.13.4 → 0.13.5

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/README.md CHANGED
@@ -42,7 +42,7 @@
42
42
 
43
43
  ---
44
44
 
45
- ThreatCrush is a security daemon that runs on your server, **reading your logs and watching inbound connections** for live attacks. It checks every nginx request against 21 attack signatures, runs 12 detection rules over auth, web and network events, auto-bans attackers, scans your codebase, spot-checks your URLs, and alerts you in real-time.
45
+ ThreatCrush is a security daemon that runs on your server, **reading your logs and watching inbound connections** for live attacks. It checks every nginx request against 21 attack signatures, runs 13 detection rules over auth, web and network events, auto-bans attackers, scans your codebase, spot-checks your URLs, and alerts you in real-time.
46
46
 
47
47
  ```
48
48
  $ threatcrush monitor
package/dist/daemon.js CHANGED
@@ -9342,6 +9342,48 @@ function defaultGateways() {
9342
9342
  return [...found];
9343
9343
  }
9344
9344
 
9345
+ // src/daemon/firewall/crawlers.ts
9346
+ var import_node_dns = require("dns");
9347
+ var CRAWLER_DOMAINS = ["googlebot.com", "google.com", "search.msn.com"];
9348
+ var systemResolver = {
9349
+ reverse: (ip) => import_node_dns.promises.reverse(ip),
9350
+ lookup: async (host) => (await import_node_dns.promises.lookup(host, { all: true })).map((a) => a.address)
9351
+ };
9352
+ var TIMEOUT_MS = 2e3;
9353
+ var CACHE_MS = 6 * 36e5;
9354
+ function withTimeout(p) {
9355
+ return Promise.race([
9356
+ p,
9357
+ new Promise((_, reject) => setTimeout(() => reject(new Error("dns timeout")), TIMEOUT_MS).unref())
9358
+ ]);
9359
+ }
9360
+ function inCrawlerDomain(host) {
9361
+ const h = host.toLowerCase().replace(/\.$/, "");
9362
+ return CRAWLER_DOMAINS.some((d) => h === d || h.endsWith(`.${d}`));
9363
+ }
9364
+ function crawlerVerifier(resolver = systemResolver) {
9365
+ const cache = /* @__PURE__ */ new Map();
9366
+ return async (ip) => {
9367
+ const hit = cache.get(ip);
9368
+ if (hit && hit.until > Date.now()) return hit.host;
9369
+ let host = null;
9370
+ try {
9371
+ const names = await withTimeout(resolver.reverse(ip));
9372
+ for (const name of names.filter(inCrawlerDomain)) {
9373
+ const addrs = await withTimeout(resolver.lookup(name));
9374
+ if (addrs.includes(ip)) {
9375
+ host = name.replace(/\.$/, "");
9376
+ break;
9377
+ }
9378
+ }
9379
+ } catch {
9380
+ host = null;
9381
+ }
9382
+ cache.set(ip, { host, until: Date.now() + CACHE_MS });
9383
+ return host;
9384
+ };
9385
+ }
9386
+
9345
9387
  // src/daemon/firewall/remediation.ts
9346
9388
  var DEFAULT_CONFIG2 = {
9347
9389
  enabled: true,
@@ -9350,7 +9392,8 @@ var DEFAULT_CONFIG2 = {
9350
9392
  max_ban_seconds: 86400,
9351
9393
  strike_memory_seconds: 86400,
9352
9394
  allowlist: [],
9353
- protect_current_ssh_client: true
9395
+ protect_current_ssh_client: true,
9396
+ spare_verified_crawlers: true
9354
9397
  };
9355
9398
  var SEVERITY_RANK4 = {
9356
9399
  info: 0,
@@ -9370,9 +9413,10 @@ function explainBlockFailure(err, backend) {
9370
9413
  return `threatcrushd is running ${who} and cannot manage ${backend}. A ban is written by the daemon, not by whoever asked for it. Fix it with one command, which asks for root itself: threatcrush install-service`;
9371
9414
  }
9372
9415
  var RemediationManager = class {
9373
- constructor(adapter, bus2, config) {
9416
+ constructor(adapter, bus2, config, verifyCrawler = crawlerVerifier()) {
9374
9417
  this.adapter = adapter;
9375
9418
  this.bus = bus2;
9419
+ this.verifyCrawler = verifyCrawler;
9376
9420
  this.config = { ...DEFAULT_CONFIG2, ...config };
9377
9421
  this.protectedList = [...DEFAULT_PROTECTED, ...this.config.allowlist];
9378
9422
  for (const gateway of defaultGateways()) this.protectedList.push(gateway);
@@ -9384,6 +9428,7 @@ var RemediationManager = class {
9384
9428
  }
9385
9429
  adapter;
9386
9430
  bus;
9431
+ verifyCrawler;
9387
9432
  config;
9388
9433
  blocklist = [];
9389
9434
  strikes = {};
@@ -9425,6 +9470,13 @@ var RemediationManager = class {
9425
9470
  if (!ip || (0, import_node_net4.isIP)(ip) === 0) return;
9426
9471
  if (this.isAllowlisted(ip)) return;
9427
9472
  if (this.blocklist.some((b) => b.ip === ip)) return;
9473
+ if (this.config.spare_verified_crawlers) {
9474
+ const crawler = await this.verifyCrawler(ip);
9475
+ if (crawler) {
9476
+ this.logLine(`[remediation] not banning ${ip}: verified crawler ${crawler} (${event.message})`);
9477
+ return;
9478
+ }
9479
+ }
9428
9480
  const ruleId = event.details?.rule_id;
9429
9481
  await this.ban(ip, event.message, { ruleId, source: "auto" });
9430
9482
  }
@@ -9699,6 +9751,9 @@ function remediationSettings(section) {
9699
9751
  if (typeof section.protect_current_ssh_client === "boolean") {
9700
9752
  settings.protect_current_ssh_client = section.protect_current_ssh_client;
9701
9753
  }
9754
+ if (typeof section.spare_verified_crawlers === "boolean") {
9755
+ settings.spare_verified_crawlers = section.spare_verified_crawlers;
9756
+ }
9702
9757
  return settings;
9703
9758
  }
9704
9759