@profullstack/threatcrush 0.13.78 → 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/README.md +15 -1
- package/dist/daemon.js +522 -88
- package/dist/daemon.js.map +1 -1
- package/dist/index.js +696 -215
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/daemon.js
CHANGED
|
@@ -2026,8 +2026,8 @@ var require_toml = __commonJS({
|
|
|
2026
2026
|
});
|
|
2027
2027
|
|
|
2028
2028
|
// src/daemon/index.ts
|
|
2029
|
-
var
|
|
2030
|
-
var
|
|
2029
|
+
var import_node_fs39 = require("fs");
|
|
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");
|
|
@@ -9495,13 +9495,13 @@ var CrsEngine = class {
|
|
|
9495
9495
|
}
|
|
9496
9496
|
return hits;
|
|
9497
9497
|
}
|
|
9498
|
-
assess(
|
|
9498
|
+
assess(request2) {
|
|
9499
9499
|
const ctx = new RequestContext({
|
|
9500
|
-
method:
|
|
9501
|
-
uri: asBytes(
|
|
9502
|
-
protocol:
|
|
9503
|
-
userAgent: asBytes(
|
|
9504
|
-
referer: asBytes(
|
|
9500
|
+
method: request2.method,
|
|
9501
|
+
uri: asBytes(request2.uri),
|
|
9502
|
+
protocol: request2.protocol,
|
|
9503
|
+
userAgent: asBytes(request2.userAgent),
|
|
9504
|
+
referer: asBytes(request2.referer)
|
|
9505
9505
|
});
|
|
9506
9506
|
const hit = new Uint8Array(this.rules.length);
|
|
9507
9507
|
for (const [target, rules] of this.byTarget) {
|
|
@@ -9574,9 +9574,9 @@ function parseCaddyAccessLog(line) {
|
|
|
9574
9574
|
return null;
|
|
9575
9575
|
}
|
|
9576
9576
|
if (typeof entry.logger !== "string" || !/^http\.log\.access(?:\.log\d+)?$/.test(entry.logger)) return null;
|
|
9577
|
-
const
|
|
9578
|
-
if (!
|
|
9579
|
-
const req =
|
|
9577
|
+
const request2 = entry.request;
|
|
9578
|
+
if (!request2 || typeof request2 !== "object" || Array.isArray(request2)) return null;
|
|
9579
|
+
const req = request2;
|
|
9580
9580
|
const host = typeof req.host === "string" ? req.host.toLowerCase() : "";
|
|
9581
9581
|
if (host !== "agent.database.pit.world") return null;
|
|
9582
9582
|
const required = [req.method, req.uri, entry.status, entry.size];
|
|
@@ -11256,6 +11256,7 @@ function parseConfigText(raw) {
|
|
|
11256
11256
|
file_integrity: parsed.file_integrity,
|
|
11257
11257
|
edr: parsed.edr,
|
|
11258
11258
|
lightning: parsed.lightning,
|
|
11259
|
+
docker_logs: parsed.docker_logs,
|
|
11259
11260
|
storage: parsed.storage,
|
|
11260
11261
|
// TOML's `[abuse-reporter]` parses to a hyphenated key; expose it under
|
|
11261
11262
|
// the JS-friendly name the rest of the daemon reads.
|
|
@@ -20303,10 +20304,307 @@ var LightningWatch = class {
|
|
|
20303
20304
|
}
|
|
20304
20305
|
};
|
|
20305
20306
|
|
|
20306
|
-
// src/
|
|
20307
|
+
// src/daemon/workers/docker-logs.ts
|
|
20307
20308
|
var import_node_fs32 = require("fs");
|
|
20308
|
-
var
|
|
20309
|
+
var import_node_http = require("http");
|
|
20309
20310
|
var import_node_net12 = require("net");
|
|
20311
|
+
var DEFAULT_DOCKER_SOCKET = "/var/run/docker.sock";
|
|
20312
|
+
var DEFAULT_NAME_PATTERN = "supabase-db$";
|
|
20313
|
+
var DEFAULT_IMAGE_PATTERN = "(^|/)postgres(:|@|$)";
|
|
20314
|
+
function regexOr(source, fallback) {
|
|
20315
|
+
if (typeof source === "string" && source) {
|
|
20316
|
+
try {
|
|
20317
|
+
return new RegExp(source, "i");
|
|
20318
|
+
} catch {
|
|
20319
|
+
}
|
|
20320
|
+
}
|
|
20321
|
+
return new RegExp(fallback, "i");
|
|
20322
|
+
}
|
|
20323
|
+
function dockerLogsSettings(section) {
|
|
20324
|
+
const poll = Number(section?.poll_seconds);
|
|
20325
|
+
return {
|
|
20326
|
+
enabled: section?.enabled !== false,
|
|
20327
|
+
socket: typeof section?.socket === "string" && section.socket ? section.socket : DEFAULT_DOCKER_SOCKET,
|
|
20328
|
+
namePattern: regexOr(section?.name_pattern, DEFAULT_NAME_PATTERN),
|
|
20329
|
+
imagePattern: regexOr(section?.image_pattern, DEFAULT_IMAGE_PATTERN),
|
|
20330
|
+
pollMs: Number.isFinite(poll) && poll >= 2 ? poll * 1e3 : 15e3
|
|
20331
|
+
};
|
|
20332
|
+
}
|
|
20333
|
+
var PASSWORD_FAILED = /FATAL:\s+password authentication failed for user "([^"]*)"/;
|
|
20334
|
+
var HBA_REJECT = /FATAL:\s+no pg_hba\.conf entry for (?:replication connection from )?host "([^"]+)", user "([^"]*)"(?:, database "([^"]*)")?/;
|
|
20335
|
+
function cleanPgHost(raw) {
|
|
20336
|
+
if (!raw) return void 0;
|
|
20337
|
+
let host = raw.trim().replace(/\(\d+\)$/, "").replace(/^\[|\]$/g, "");
|
|
20338
|
+
if (/^::ffff:\d+\.\d+\.\d+\.\d+$/i.test(host)) host = host.slice(7);
|
|
20339
|
+
return (0, import_node_net12.isIP)(host) ? host : void 0;
|
|
20340
|
+
}
|
|
20341
|
+
function prefixHost(line) {
|
|
20342
|
+
const first = cleanPgHost(line.split(/\s/, 1)[0]);
|
|
20343
|
+
if (first) return first;
|
|
20344
|
+
const m = /\b(?:host|client|remote)=(\S+?)[\s,]/.exec(line);
|
|
20345
|
+
return cleanPgHost(m?.[1]);
|
|
20346
|
+
}
|
|
20347
|
+
function parsePostgresAuthFailure(line) {
|
|
20348
|
+
const hba = HBA_REJECT.exec(line);
|
|
20349
|
+
if (hba) {
|
|
20350
|
+
return {
|
|
20351
|
+
kind: "pg_hba",
|
|
20352
|
+
ip: cleanPgHost(hba[1]) ?? prefixHost(line),
|
|
20353
|
+
user: hba[2],
|
|
20354
|
+
...hba[3] !== void 0 ? { database: hba[3] } : {}
|
|
20355
|
+
};
|
|
20356
|
+
}
|
|
20357
|
+
const pw = PASSWORD_FAILED.exec(line);
|
|
20358
|
+
if (pw) {
|
|
20359
|
+
const ip = prefixHost(line);
|
|
20360
|
+
const db2 = /\s([^\s@]+)@([^\s@]+)\s+FATAL:/.exec(line);
|
|
20361
|
+
return { kind: "password", ...ip ? { ip } : {}, user: pw[1], ...db2 ? { database: db2[2] } : {} };
|
|
20362
|
+
}
|
|
20363
|
+
return null;
|
|
20364
|
+
}
|
|
20365
|
+
function isUnbannableSource(ip, protectedList = []) {
|
|
20366
|
+
return isProtected(ip, [...DEFAULT_PROTECTED, ...protectedList]);
|
|
20367
|
+
}
|
|
20368
|
+
function postgresAuthEvent(line, container, protectedList = [], now = /* @__PURE__ */ new Date()) {
|
|
20369
|
+
const failure = parsePostgresAuthFailure(line);
|
|
20370
|
+
if (!failure?.ip) return null;
|
|
20371
|
+
if (isUnbannableSource(failure.ip, protectedList)) return null;
|
|
20372
|
+
const who = `user "${failure.user ?? ""}"${failure.database !== void 0 ? ` database "${failure.database}"` : ""}`;
|
|
20373
|
+
const message = failure.kind === "password" ? `Postgres password authentication failed for ${who} from ${failure.ip} (${container})` : `Postgres connection rejected by pg_hba for ${who} from ${failure.ip} (${container})`;
|
|
20374
|
+
return {
|
|
20375
|
+
timestamp: now,
|
|
20376
|
+
module: "postgres-guard",
|
|
20377
|
+
category: "auth",
|
|
20378
|
+
// One failure is not an attack, and anything at the remediation floor
|
|
20379
|
+
// (`min_severity`, high by default) bans on its own. The rule decides.
|
|
20380
|
+
severity: "low",
|
|
20381
|
+
message,
|
|
20382
|
+
source_ip: failure.ip,
|
|
20383
|
+
details: {
|
|
20384
|
+
service: "postgres",
|
|
20385
|
+
container,
|
|
20386
|
+
kind: failure.kind,
|
|
20387
|
+
...failure.user !== void 0 ? { user: failure.user } : {},
|
|
20388
|
+
...failure.database !== void 0 ? { database: failure.database } : {},
|
|
20389
|
+
line: line.slice(0, 500)
|
|
20390
|
+
}
|
|
20391
|
+
};
|
|
20392
|
+
}
|
|
20393
|
+
var DockerLogDecoder = class {
|
|
20394
|
+
constructor(tty2, onLine) {
|
|
20395
|
+
this.tty = tty2;
|
|
20396
|
+
this.onLine = onLine;
|
|
20397
|
+
}
|
|
20398
|
+
tty;
|
|
20399
|
+
onLine;
|
|
20400
|
+
pending = Buffer.alloc(0);
|
|
20401
|
+
partial = /* @__PURE__ */ new Map();
|
|
20402
|
+
push(chunk) {
|
|
20403
|
+
if (this.tty) {
|
|
20404
|
+
this.text(1, chunk.toString("utf-8"));
|
|
20405
|
+
return;
|
|
20406
|
+
}
|
|
20407
|
+
this.pending = this.pending.length ? Buffer.concat([this.pending, chunk]) : chunk;
|
|
20408
|
+
while (this.pending.length >= 8) {
|
|
20409
|
+
const stream = this.pending[0];
|
|
20410
|
+
const size = this.pending.readUInt32BE(4);
|
|
20411
|
+
if (stream > 2 || this.pending[1] !== 0 || this.pending[2] !== 0 || this.pending[3] !== 0) {
|
|
20412
|
+
this.tty = true;
|
|
20413
|
+
const rest = this.pending;
|
|
20414
|
+
this.pending = Buffer.alloc(0);
|
|
20415
|
+
this.text(1, rest.toString("utf-8"));
|
|
20416
|
+
return;
|
|
20417
|
+
}
|
|
20418
|
+
if (this.pending.length < 8 + size) break;
|
|
20419
|
+
this.text(stream, this.pending.subarray(8, 8 + size).toString("utf-8"));
|
|
20420
|
+
this.pending = this.pending.subarray(8 + size);
|
|
20421
|
+
}
|
|
20422
|
+
}
|
|
20423
|
+
text(stream, text) {
|
|
20424
|
+
let buf = (this.partial.get(stream) ?? "") + text;
|
|
20425
|
+
let idx;
|
|
20426
|
+
while ((idx = buf.indexOf("\n")) >= 0) {
|
|
20427
|
+
const line = buf.slice(0, idx).replace(/\r$/, "");
|
|
20428
|
+
buf = buf.slice(idx + 1);
|
|
20429
|
+
if (line) this.onLine(line);
|
|
20430
|
+
}
|
|
20431
|
+
this.partial.set(stream, buf.length > 65536 ? "" : buf);
|
|
20432
|
+
}
|
|
20433
|
+
};
|
|
20434
|
+
function socketDeps(socketPath) {
|
|
20435
|
+
return {
|
|
20436
|
+
getJson: (path) => new Promise((resolve4, reject) => {
|
|
20437
|
+
const req = (0, import_node_http.request)({ socketPath, path, method: "GET", timeout: 1e4 }, (res) => {
|
|
20438
|
+
const chunks = [];
|
|
20439
|
+
res.on("data", (c) => chunks.push(c));
|
|
20440
|
+
res.on("end", () => {
|
|
20441
|
+
const body = Buffer.concat(chunks).toString("utf-8");
|
|
20442
|
+
if ((res.statusCode ?? 500) >= 300) return reject(new Error(`docker ${path}: HTTP ${res.statusCode} ${body.slice(0, 200)}`));
|
|
20443
|
+
try {
|
|
20444
|
+
resolve4(JSON.parse(body));
|
|
20445
|
+
} catch (err) {
|
|
20446
|
+
reject(err);
|
|
20447
|
+
}
|
|
20448
|
+
});
|
|
20449
|
+
res.on("error", reject);
|
|
20450
|
+
});
|
|
20451
|
+
req.on("timeout", () => req.destroy(new Error(`docker ${path}: timed out`)));
|
|
20452
|
+
req.on("error", reject);
|
|
20453
|
+
req.end();
|
|
20454
|
+
}),
|
|
20455
|
+
stream: (path, onData, onEnd) => {
|
|
20456
|
+
let ended = false;
|
|
20457
|
+
const end = () => {
|
|
20458
|
+
if (!ended) {
|
|
20459
|
+
ended = true;
|
|
20460
|
+
onEnd();
|
|
20461
|
+
}
|
|
20462
|
+
};
|
|
20463
|
+
const req = (0, import_node_http.request)({ socketPath, path, method: "GET" }, (res) => {
|
|
20464
|
+
if ((res.statusCode ?? 500) >= 300) {
|
|
20465
|
+
res.resume();
|
|
20466
|
+
end();
|
|
20467
|
+
return;
|
|
20468
|
+
}
|
|
20469
|
+
res.on("data", onData);
|
|
20470
|
+
res.on("end", end);
|
|
20471
|
+
res.on("close", end);
|
|
20472
|
+
res.on("error", end);
|
|
20473
|
+
});
|
|
20474
|
+
req.on("error", end);
|
|
20475
|
+
req.on("close", end);
|
|
20476
|
+
req.end();
|
|
20477
|
+
return req;
|
|
20478
|
+
}
|
|
20479
|
+
};
|
|
20480
|
+
}
|
|
20481
|
+
var DockerLogsWatch = class {
|
|
20482
|
+
constructor(opts) {
|
|
20483
|
+
this.opts = opts;
|
|
20484
|
+
this.deps = opts.deps ?? socketDeps(opts.settings.socket);
|
|
20485
|
+
}
|
|
20486
|
+
opts;
|
|
20487
|
+
follows = /* @__PURE__ */ new Map();
|
|
20488
|
+
/** When each container's stream last closed: a restart resumes from there. */
|
|
20489
|
+
closedAt = /* @__PURE__ */ new Map();
|
|
20490
|
+
timer = null;
|
|
20491
|
+
lastPoll = 0;
|
|
20492
|
+
stopped = false;
|
|
20493
|
+
deps;
|
|
20494
|
+
warned = false;
|
|
20495
|
+
/** False when off or there is no Docker here. */
|
|
20496
|
+
start() {
|
|
20497
|
+
const { settings } = this.opts;
|
|
20498
|
+
if (!settings.enabled) return false;
|
|
20499
|
+
if (!this.opts.deps && !(0, import_node_fs32.existsSync)(settings.socket)) return false;
|
|
20500
|
+
this.opts.log(`[docker-logs] following containers named /${settings.namePattern.source}/ or with image /${settings.imagePattern.source}/ for Postgres auth failures`);
|
|
20501
|
+
void this.poll();
|
|
20502
|
+
this.timer = setInterval(() => {
|
|
20503
|
+
void this.poll();
|
|
20504
|
+
}, settings.pollMs);
|
|
20505
|
+
this.timer.unref?.();
|
|
20506
|
+
return true;
|
|
20507
|
+
}
|
|
20508
|
+
stop() {
|
|
20509
|
+
this.stopped = true;
|
|
20510
|
+
if (this.timer) clearInterval(this.timer);
|
|
20511
|
+
this.timer = null;
|
|
20512
|
+
for (const f of this.follows.values()) {
|
|
20513
|
+
try {
|
|
20514
|
+
f.req.destroy();
|
|
20515
|
+
} catch {
|
|
20516
|
+
}
|
|
20517
|
+
}
|
|
20518
|
+
this.follows.clear();
|
|
20519
|
+
}
|
|
20520
|
+
/** Names of the containers being followed now. */
|
|
20521
|
+
following() {
|
|
20522
|
+
return [...this.follows.values()].map((f) => f.name).sort();
|
|
20523
|
+
}
|
|
20524
|
+
matches(c) {
|
|
20525
|
+
const name = (c.Names?.[0] ?? "").replace(/^\//, "");
|
|
20526
|
+
return this.opts.settings.namePattern.test(name) || this.opts.settings.imagePattern.test(c.Image ?? "");
|
|
20527
|
+
}
|
|
20528
|
+
/** One discovery pass: follow new or restarted matches, drop gone ones. */
|
|
20529
|
+
async poll() {
|
|
20530
|
+
if (this.stopped) return;
|
|
20531
|
+
const now = Date.now();
|
|
20532
|
+
const previous = this.lastPoll;
|
|
20533
|
+
let list;
|
|
20534
|
+
try {
|
|
20535
|
+
list = await this.deps.getJson("/containers/json");
|
|
20536
|
+
this.warned = false;
|
|
20537
|
+
} catch (err) {
|
|
20538
|
+
if (!this.warned) this.opts.log(`[docker-logs] cannot list containers: ${err.message}`);
|
|
20539
|
+
this.warned = true;
|
|
20540
|
+
return;
|
|
20541
|
+
}
|
|
20542
|
+
this.lastPoll = now;
|
|
20543
|
+
const running = /* @__PURE__ */ new Set();
|
|
20544
|
+
for (const c of Array.isArray(list) ? list : []) {
|
|
20545
|
+
if (!c?.Id || !this.matches(c)) continue;
|
|
20546
|
+
running.add(c.Id);
|
|
20547
|
+
if (this.follows.has(c.Id)) continue;
|
|
20548
|
+
const from = this.closedAt.get(c.Id) ?? (previous || now);
|
|
20549
|
+
await this.follow(c, from);
|
|
20550
|
+
}
|
|
20551
|
+
for (const [id, f] of this.follows) {
|
|
20552
|
+
if (!running.has(id)) {
|
|
20553
|
+
try {
|
|
20554
|
+
f.req.destroy();
|
|
20555
|
+
} catch {
|
|
20556
|
+
}
|
|
20557
|
+
this.follows.delete(id);
|
|
20558
|
+
}
|
|
20559
|
+
}
|
|
20560
|
+
for (const id of this.closedAt.keys()) if (!running.has(id)) this.closedAt.delete(id);
|
|
20561
|
+
}
|
|
20562
|
+
async follow(c, fromMs) {
|
|
20563
|
+
const name = (c.Names?.[0] ?? c.Id.slice(0, 12)).replace(/^\//, "");
|
|
20564
|
+
let tty2 = false;
|
|
20565
|
+
try {
|
|
20566
|
+
const info = await this.deps.getJson(`/containers/${c.Id}/json`);
|
|
20567
|
+
tty2 = info?.Config?.Tty === true;
|
|
20568
|
+
} catch {
|
|
20569
|
+
}
|
|
20570
|
+
if (this.stopped || this.follows.has(c.Id)) return;
|
|
20571
|
+
const since = Math.max(1, Math.floor(fromMs / 1e3));
|
|
20572
|
+
const decoder = new DockerLogDecoder(tty2, (line) => this.line(line, name));
|
|
20573
|
+
const req = this.deps.stream(
|
|
20574
|
+
`/containers/${c.Id}/logs?follow=1&stdout=1&stderr=1&since=${since}`,
|
|
20575
|
+
(chunk) => {
|
|
20576
|
+
try {
|
|
20577
|
+
decoder.push(chunk);
|
|
20578
|
+
} catch {
|
|
20579
|
+
}
|
|
20580
|
+
},
|
|
20581
|
+
() => {
|
|
20582
|
+
if (this.follows.get(c.Id)?.req === req) {
|
|
20583
|
+
this.follows.delete(c.Id);
|
|
20584
|
+
this.closedAt.set(c.Id, Date.now());
|
|
20585
|
+
}
|
|
20586
|
+
}
|
|
20587
|
+
);
|
|
20588
|
+
this.follows.set(c.Id, { req, name });
|
|
20589
|
+
this.opts.log(`[docker-logs] following ${name}`);
|
|
20590
|
+
}
|
|
20591
|
+
/** Exposed for tests and `rules test`: one container log line through the parser. */
|
|
20592
|
+
line(line, container) {
|
|
20593
|
+
const event = postgresAuthEvent(line, container, this.opts.protectedList ?? []);
|
|
20594
|
+
if (!event) return null;
|
|
20595
|
+
try {
|
|
20596
|
+
insertEvent(event);
|
|
20597
|
+
} catch {
|
|
20598
|
+
}
|
|
20599
|
+
this.opts.bus.publish(event);
|
|
20600
|
+
return event;
|
|
20601
|
+
}
|
|
20602
|
+
};
|
|
20603
|
+
|
|
20604
|
+
// src/core/tor-exits.ts
|
|
20605
|
+
var import_node_fs33 = require("fs");
|
|
20606
|
+
var import_node_path25 = require("path");
|
|
20607
|
+
var import_node_net13 = require("net");
|
|
20310
20608
|
var TOR_BULK_EXIT_LIST_URL = "https://check.torproject.org/torbulkexitlist";
|
|
20311
20609
|
function parseTorExitList(text) {
|
|
20312
20610
|
const out = /* @__PURE__ */ new Set();
|
|
@@ -20314,7 +20612,7 @@ function parseTorExitList(text) {
|
|
|
20314
20612
|
const line = raw.trim();
|
|
20315
20613
|
if (!line || line.startsWith("#")) continue;
|
|
20316
20614
|
const ip = line.split(/\s+/)[0];
|
|
20317
|
-
if ((0,
|
|
20615
|
+
if ((0, import_node_net13.isIP)(ip)) out.add(normalizeIp(ip));
|
|
20318
20616
|
}
|
|
20319
20617
|
return out;
|
|
20320
20618
|
}
|
|
@@ -20362,6 +20660,10 @@ var TorExitList = class _TorExitList {
|
|
|
20362
20660
|
get size() {
|
|
20363
20661
|
return this.exits.size;
|
|
20364
20662
|
}
|
|
20663
|
+
/** Every exit address currently in memory, for the proactive firewall block. */
|
|
20664
|
+
values() {
|
|
20665
|
+
return [...this.exits];
|
|
20666
|
+
}
|
|
20365
20667
|
/** When the list in memory was downloaded (ms), 0 if never. */
|
|
20366
20668
|
get updated() {
|
|
20367
20669
|
return this.updatedAt;
|
|
@@ -20373,10 +20675,10 @@ var TorExitList = class _TorExitList {
|
|
|
20373
20675
|
}
|
|
20374
20676
|
loadCache() {
|
|
20375
20677
|
try {
|
|
20376
|
-
if (!(0,
|
|
20377
|
-
const parsed = parseTorExitList((0,
|
|
20678
|
+
if (!(0, import_node_fs33.existsSync)(this.opts.cachePath)) return false;
|
|
20679
|
+
const parsed = parseTorExitList((0, import_node_fs33.readFileSync)(this.opts.cachePath, "utf-8"));
|
|
20378
20680
|
if (parsed.size === 0) return false;
|
|
20379
|
-
this.load(parsed, (0,
|
|
20681
|
+
this.load(parsed, (0, import_node_fs33.statSync)(this.opts.cachePath).mtimeMs);
|
|
20380
20682
|
this.log(`[tor] loaded ${parsed.size} Tor exit addresses from cache (${new Date(this.updatedAt).toISOString()})`);
|
|
20381
20683
|
return true;
|
|
20382
20684
|
} catch (err) {
|
|
@@ -20408,9 +20710,9 @@ var TorExitList = class _TorExitList {
|
|
|
20408
20710
|
const before = this.exits.size;
|
|
20409
20711
|
this.load(parsed);
|
|
20410
20712
|
try {
|
|
20411
|
-
(0,
|
|
20412
|
-
(0,
|
|
20413
|
-
(0,
|
|
20713
|
+
(0, import_node_fs33.mkdirSync)((0, import_node_path25.dirname)(this.opts.cachePath), { recursive: true });
|
|
20714
|
+
(0, import_node_fs33.writeFileSync)(`${this.opts.cachePath}.tmp`, [...parsed].join("\n") + "\n");
|
|
20715
|
+
(0, import_node_fs33.renameSync)(`${this.opts.cachePath}.tmp`, this.opts.cachePath);
|
|
20414
20716
|
} catch (err) {
|
|
20415
20717
|
this.log(`[tor] could not save the exit list cache: ${err.message}`);
|
|
20416
20718
|
}
|
|
@@ -20419,6 +20721,88 @@ var TorExitList = class _TorExitList {
|
|
|
20419
20721
|
}
|
|
20420
20722
|
};
|
|
20421
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
|
+
|
|
20422
20806
|
// src/daemon/rules/engine.ts
|
|
20423
20807
|
var MAX_WINDOW_ENTRIES = 1e3;
|
|
20424
20808
|
function live(window) {
|
|
@@ -20631,7 +21015,7 @@ var RuleEngine = class {
|
|
|
20631
21015
|
};
|
|
20632
21016
|
|
|
20633
21017
|
// src/daemon/rules/loader.ts
|
|
20634
|
-
var
|
|
21018
|
+
var import_node_fs34 = require("fs");
|
|
20635
21019
|
var import_node_path26 = require("path");
|
|
20636
21020
|
|
|
20637
21021
|
// src/daemon/rules/default-rules.ts
|
|
@@ -21269,6 +21653,33 @@ var DEFAULT_RULES = [
|
|
|
21269
21653
|
tags: ["agentdb", "rate-limit", "data-access"],
|
|
21270
21654
|
remediation: { action: "alert", description: "Review AgentDB audit metadata for the token and request pattern" },
|
|
21271
21655
|
enabled: true
|
|
21656
|
+
},
|
|
21657
|
+
{
|
|
21658
|
+
// 2026-11-20 dev2: ~10,150 failed `postgres` logins from ~60 addresses in
|
|
21659
|
+
// 72h against a Supabase Postgres published on 0.0.0.0, none banned. Fed by
|
|
21660
|
+
// docker-logs (`[docker_logs]`), which already drops private, Docker-bridge
|
|
21661
|
+
// and protected sources, so every event here names a public client.
|
|
21662
|
+
id: "postgres-auth-bruteforce",
|
|
21663
|
+
title: "Postgres Brute Force Detected",
|
|
21664
|
+
description: "Repeated Postgres password failures or pg_hba rejects from the same source",
|
|
21665
|
+
version: "1.0.0",
|
|
21666
|
+
category: "auth",
|
|
21667
|
+
severity: "high",
|
|
21668
|
+
source_types: ["postgres-guard"],
|
|
21669
|
+
match: {
|
|
21670
|
+
field: "message",
|
|
21671
|
+
operator: "regex",
|
|
21672
|
+
value: "^Postgres (?:password authentication failed|connection rejected by pg_hba) "
|
|
21673
|
+
},
|
|
21674
|
+
threshold: 5,
|
|
21675
|
+
window_seconds: 600,
|
|
21676
|
+
cooldown_seconds: 600,
|
|
21677
|
+
tags: ["postgres", "database", "brute-force", "credential-stuffing"],
|
|
21678
|
+
remediation: {
|
|
21679
|
+
action: "block",
|
|
21680
|
+
description: "Block source IP brute-forcing Postgres"
|
|
21681
|
+
},
|
|
21682
|
+
enabled: true
|
|
21272
21683
|
}
|
|
21273
21684
|
];
|
|
21274
21685
|
|
|
@@ -21277,11 +21688,16 @@ var RULES_DIR = "/etc/threatcrush/rules.d";
|
|
|
21277
21688
|
function loadAllRules(customDir) {
|
|
21278
21689
|
const rules = [...DEFAULT_RULES];
|
|
21279
21690
|
const dir = customDir || RULES_DIR;
|
|
21280
|
-
if ((0,
|
|
21281
|
-
|
|
21691
|
+
if ((0, import_node_fs34.existsSync)(dir)) {
|
|
21692
|
+
let files = [];
|
|
21693
|
+
try {
|
|
21694
|
+
files = (0, import_node_fs34.readdirSync)(dir).filter((f) => f.endsWith(".json"));
|
|
21695
|
+
} catch (err) {
|
|
21696
|
+
console.warn(`[rules] cannot read ${dir}: ${err.message}; custom rules not loaded`);
|
|
21697
|
+
}
|
|
21282
21698
|
for (const file of files) {
|
|
21283
21699
|
try {
|
|
21284
|
-
const raw = (0,
|
|
21700
|
+
const raw = (0, import_node_fs34.readFileSync)((0, import_node_path26.join)(dir, file), "utf-8");
|
|
21285
21701
|
const parsed = JSON.parse(raw);
|
|
21286
21702
|
const customRules = Array.isArray(parsed) ? parsed : [parsed];
|
|
21287
21703
|
for (const rule of customRules) {
|
|
@@ -21312,24 +21728,24 @@ function loadAllRules(customDir) {
|
|
|
21312
21728
|
}
|
|
21313
21729
|
|
|
21314
21730
|
// src/daemon/firewall/adapters.ts
|
|
21315
|
-
var
|
|
21316
|
-
var
|
|
21731
|
+
var import_node_child_process12 = require("child_process");
|
|
21732
|
+
var import_node_fs35 = require("fs");
|
|
21317
21733
|
var import_node_path27 = require("path");
|
|
21318
|
-
var
|
|
21734
|
+
var import_node_net15 = require("net");
|
|
21319
21735
|
function assertValidFirewallIp(ip) {
|
|
21320
|
-
if ((0,
|
|
21736
|
+
if ((0, import_node_net15.isIP)(ip) !== 4) {
|
|
21321
21737
|
throw new Error(`Invalid IPv4 address: ${ip}`);
|
|
21322
21738
|
}
|
|
21323
21739
|
}
|
|
21324
21740
|
function assertBannableIp(ip) {
|
|
21325
|
-
if ((0,
|
|
21741
|
+
if ((0, import_node_net15.isIP)(ip) === 0) {
|
|
21326
21742
|
throw new Error(`Invalid IP address: ${ip}`);
|
|
21327
21743
|
}
|
|
21328
21744
|
}
|
|
21329
21745
|
function writeIfAbsent(path, contents) {
|
|
21330
|
-
(0,
|
|
21746
|
+
(0, import_node_fs35.mkdirSync)((0, import_node_path27.dirname)(path), { recursive: true });
|
|
21331
21747
|
try {
|
|
21332
|
-
(0,
|
|
21748
|
+
(0, import_node_fs35.writeFileSync)(path, contents, { flag: "wx" });
|
|
21333
21749
|
} catch (err) {
|
|
21334
21750
|
if (err.code !== "EEXIST") throw err;
|
|
21335
21751
|
}
|
|
@@ -21348,11 +21764,11 @@ var Fail2banAdapter = class {
|
|
|
21348
21764
|
enforces = true;
|
|
21349
21765
|
jailReady = false;
|
|
21350
21766
|
isAvailable() {
|
|
21351
|
-
const result = (0,
|
|
21767
|
+
const result = (0, import_node_child_process12.spawnSync)("fail2ban-client", ["ping"], { stdio: "pipe" });
|
|
21352
21768
|
return result.status === 0;
|
|
21353
21769
|
}
|
|
21354
21770
|
client(args) {
|
|
21355
|
-
return (0,
|
|
21771
|
+
return (0, import_node_child_process12.execFileSync)("fail2ban-client", args, {
|
|
21356
21772
|
encoding: "utf-8",
|
|
21357
21773
|
stdio: ["ignore", "pipe", "pipe"]
|
|
21358
21774
|
});
|
|
@@ -21428,7 +21844,7 @@ var Fail2banAdapter = class {
|
|
|
21428
21844
|
const status = this.client(["status", this.jail]);
|
|
21429
21845
|
const line = status.split("\n").find((l) => /Banned IP list:/i.test(l));
|
|
21430
21846
|
if (!line) return [];
|
|
21431
|
-
return line.split(":").slice(1).join(":").trim().split(/\s+/).filter((s) => (0,
|
|
21847
|
+
return line.split(":").slice(1).join(":").trim().split(/\s+/).filter((s) => (0, import_node_net15.isIP)(s) !== 0);
|
|
21432
21848
|
} catch {
|
|
21433
21849
|
return [];
|
|
21434
21850
|
}
|
|
@@ -21438,45 +21854,45 @@ var Fail2banAdapter = class {
|
|
|
21438
21854
|
function parseBannedList(output) {
|
|
21439
21855
|
const found = [];
|
|
21440
21856
|
for (const match of output.matchAll(/['"]([^'"]+)['"]/g)) {
|
|
21441
|
-
if ((0,
|
|
21857
|
+
if ((0, import_node_net15.isIP)(match[1]) !== 0) found.push(match[1]);
|
|
21442
21858
|
}
|
|
21443
21859
|
if (found.length > 0) return found;
|
|
21444
|
-
return output.replace(/[[\],]/g, " ").split(/\s+/).filter((s) => (0,
|
|
21860
|
+
return output.replace(/[[\],]/g, " ").split(/\s+/).filter((s) => (0, import_node_net15.isIP)(s) !== 0);
|
|
21445
21861
|
}
|
|
21446
21862
|
var NftablesAdapter = class {
|
|
21447
21863
|
name = "nftables";
|
|
21448
21864
|
table = "threatcrush";
|
|
21449
21865
|
set = "blocklist";
|
|
21450
21866
|
isAvailable() {
|
|
21451
|
-
const result = (0,
|
|
21867
|
+
const result = (0, import_node_child_process12.spawnSync)("nft", ["--version"], { stdio: "pipe" });
|
|
21452
21868
|
return result.status === 0;
|
|
21453
21869
|
}
|
|
21454
21870
|
ensureSetup() {
|
|
21455
21871
|
try {
|
|
21456
|
-
(0,
|
|
21872
|
+
(0, import_node_child_process12.execSync)(`nft list table inet ${this.table} 2>/dev/null`, { stdio: "pipe" });
|
|
21457
21873
|
} catch {
|
|
21458
|
-
(0,
|
|
21459
|
-
(0,
|
|
21460
|
-
(0,
|
|
21461
|
-
(0,
|
|
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`);
|
|
21462
21878
|
}
|
|
21463
21879
|
}
|
|
21464
21880
|
async block(ip) {
|
|
21465
21881
|
assertValidFirewallIp(ip);
|
|
21466
21882
|
this.ensureSetup();
|
|
21467
|
-
(0,
|
|
21883
|
+
(0, import_node_child_process12.execSync)(`nft add element inet ${this.table} ${this.set} '{ ${ip} }'`);
|
|
21468
21884
|
}
|
|
21469
21885
|
async unblock(ip) {
|
|
21470
21886
|
assertValidFirewallIp(ip);
|
|
21471
21887
|
try {
|
|
21472
|
-
(0,
|
|
21888
|
+
(0, import_node_child_process12.execSync)(`nft delete element inet ${this.table} ${this.set} '{ ${ip} }'`);
|
|
21473
21889
|
} catch {
|
|
21474
21890
|
}
|
|
21475
21891
|
}
|
|
21476
21892
|
async isBlocked(ip) {
|
|
21477
21893
|
assertValidFirewallIp(ip);
|
|
21478
21894
|
try {
|
|
21479
|
-
const output = (0,
|
|
21895
|
+
const output = (0, import_node_child_process12.execSync)(`nft list set inet ${this.table} ${this.set}`, { encoding: "utf-8" });
|
|
21480
21896
|
return output.includes(ip);
|
|
21481
21897
|
} catch {
|
|
21482
21898
|
return false;
|
|
@@ -21484,7 +21900,7 @@ var NftablesAdapter = class {
|
|
|
21484
21900
|
}
|
|
21485
21901
|
async listBlocked() {
|
|
21486
21902
|
try {
|
|
21487
|
-
const output = (0,
|
|
21903
|
+
const output = (0, import_node_child_process12.execSync)(`nft list set inet ${this.table} ${this.set}`, { encoding: "utf-8" });
|
|
21488
21904
|
const match = output.match(/elements\s*=\s*\{([^}]*)\}/);
|
|
21489
21905
|
if (!match) return [];
|
|
21490
21906
|
return match[1].split(",").map((s) => s.trim().split(/\s/)[0]).filter(Boolean);
|
|
@@ -21497,34 +21913,34 @@ var IptablesAdapter = class {
|
|
|
21497
21913
|
name = "iptables";
|
|
21498
21914
|
chain = "THREATCRUSH";
|
|
21499
21915
|
isAvailable() {
|
|
21500
|
-
const result = (0,
|
|
21916
|
+
const result = (0, import_node_child_process12.spawnSync)("iptables", ["--version"], { stdio: "pipe" });
|
|
21501
21917
|
return result.status === 0;
|
|
21502
21918
|
}
|
|
21503
21919
|
ensureChain() {
|
|
21504
21920
|
try {
|
|
21505
|
-
(0,
|
|
21921
|
+
(0, import_node_child_process12.execSync)(`iptables -n -L ${this.chain} 2>/dev/null`, { stdio: "pipe" });
|
|
21506
21922
|
} catch {
|
|
21507
|
-
(0,
|
|
21508
|
-
(0,
|
|
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}`);
|
|
21509
21925
|
}
|
|
21510
21926
|
}
|
|
21511
21927
|
async block(ip) {
|
|
21512
21928
|
assertValidFirewallIp(ip);
|
|
21513
21929
|
this.ensureChain();
|
|
21514
21930
|
if (await this.isBlocked(ip)) return;
|
|
21515
|
-
(0,
|
|
21931
|
+
(0, import_node_child_process12.execSync)(`iptables -A ${this.chain} -s ${ip} -j DROP`);
|
|
21516
21932
|
}
|
|
21517
21933
|
async unblock(ip) {
|
|
21518
21934
|
assertValidFirewallIp(ip);
|
|
21519
21935
|
try {
|
|
21520
|
-
(0,
|
|
21936
|
+
(0, import_node_child_process12.execSync)(`iptables -D ${this.chain} -s ${ip} -j DROP`);
|
|
21521
21937
|
} catch {
|
|
21522
21938
|
}
|
|
21523
21939
|
}
|
|
21524
21940
|
async isBlocked(ip) {
|
|
21525
21941
|
assertValidFirewallIp(ip);
|
|
21526
21942
|
try {
|
|
21527
|
-
const output = (0,
|
|
21943
|
+
const output = (0, import_node_child_process12.execSync)(`iptables -n -L ${this.chain}`, { encoding: "utf-8" });
|
|
21528
21944
|
return output.includes(ip);
|
|
21529
21945
|
} catch {
|
|
21530
21946
|
return false;
|
|
@@ -21532,7 +21948,7 @@ var IptablesAdapter = class {
|
|
|
21532
21948
|
}
|
|
21533
21949
|
async listBlocked() {
|
|
21534
21950
|
try {
|
|
21535
|
-
const output = (0,
|
|
21951
|
+
const output = (0, import_node_child_process12.execSync)(`iptables -n -L ${this.chain}`, { encoding: "utf-8" });
|
|
21536
21952
|
const ips = [];
|
|
21537
21953
|
for (const line of output.split("\n")) {
|
|
21538
21954
|
const match = line.match(/DROP\s+all\s+--\s+(\d+\.\d+\.\d+\.\d+)/);
|
|
@@ -21588,9 +22004,9 @@ function detectFirewallAdapter(backend = "auto") {
|
|
|
21588
22004
|
}
|
|
21589
22005
|
|
|
21590
22006
|
// src/daemon/firewall/remediation.ts
|
|
21591
|
-
var
|
|
21592
|
-
var
|
|
21593
|
-
var
|
|
22007
|
+
var import_node_fs36 = require("fs");
|
|
22008
|
+
var import_node_child_process13 = require("child_process");
|
|
22009
|
+
var import_node_net16 = require("net");
|
|
21594
22010
|
|
|
21595
22011
|
// src/daemon/firewall/crawlers.ts
|
|
21596
22012
|
var import_node_dns2 = require("dns");
|
|
@@ -21658,19 +22074,19 @@ var defaultDiscovery = {
|
|
|
21658
22074
|
const snapshot = takeSnapshot({
|
|
21659
22075
|
readFile: (p) => {
|
|
21660
22076
|
try {
|
|
21661
|
-
return (0,
|
|
22077
|
+
return (0, import_node_fs36.readFileSync)(p, "utf-8");
|
|
21662
22078
|
} catch {
|
|
21663
22079
|
return null;
|
|
21664
22080
|
}
|
|
21665
22081
|
},
|
|
21666
22082
|
readDir: (p) => {
|
|
21667
22083
|
try {
|
|
21668
|
-
return (0,
|
|
22084
|
+
return (0, import_node_fs36.readdirSync)(p);
|
|
21669
22085
|
} catch {
|
|
21670
22086
|
return null;
|
|
21671
22087
|
}
|
|
21672
22088
|
},
|
|
21673
|
-
exists: (p) => (0,
|
|
22089
|
+
exists: (p) => (0, import_node_fs36.existsSync)(p)
|
|
21674
22090
|
});
|
|
21675
22091
|
const out = /* @__PURE__ */ new Set();
|
|
21676
22092
|
for (const keys of Object.values(snapshot.keys)) {
|
|
@@ -21679,7 +22095,7 @@ var defaultDiscovery = {
|
|
|
21679
22095
|
return out;
|
|
21680
22096
|
},
|
|
21681
22097
|
keyLoginJournal: (sinceSeconds) => new Promise((resolve4) => {
|
|
21682
|
-
(0,
|
|
22098
|
+
(0, import_node_child_process13.execFile)(
|
|
21683
22099
|
"journalctl",
|
|
21684
22100
|
["_COMM=sshd", "_COMM=sshd-session", `--since=-${Math.ceil(sinceSeconds / 86400)}d`, "-o", "short-unix", "--no-pager", "--grep", "Accepted publickey"],
|
|
21685
22101
|
{ maxBuffer: 32 * 1024 * 1024, timeout: 6e4 },
|
|
@@ -21759,7 +22175,7 @@ var RemediationManager = class {
|
|
|
21759
22175
|
setTorExitCheck(check) {
|
|
21760
22176
|
this.isTorExit = check;
|
|
21761
22177
|
let dropped = 0;
|
|
21762
|
-
this.protectedList = this.protectedList.filter((a) => !(0,
|
|
22178
|
+
this.protectedList = this.protectedList.filter((a) => !(0, import_node_net16.isIP)(a) || !check(a) || this.pinned.has(a) || this.config.allowlist.includes(a));
|
|
21763
22179
|
for (const ip of Object.keys(this.operators)) {
|
|
21764
22180
|
if (!check(ip)) continue;
|
|
21765
22181
|
delete this.operators[ip];
|
|
@@ -21852,7 +22268,7 @@ var RemediationManager = class {
|
|
|
21852
22268
|
if (remediation?.action !== "block") return;
|
|
21853
22269
|
}
|
|
21854
22270
|
const ip = event.source_ip;
|
|
21855
|
-
if (!ip || (0,
|
|
22271
|
+
if (!ip || (0, import_node_net16.isIP)(ip) === 0) return;
|
|
21856
22272
|
const strict = remediation?.override_operator_protection === true;
|
|
21857
22273
|
if (strict ? this.isFleetAddress(ip) : this.isAllowlisted(ip)) {
|
|
21858
22274
|
if (strict) this.logLine(`[remediation] not banning ${ip}: it is one of our own addresses (${event.message})`);
|
|
@@ -21881,7 +22297,7 @@ var RemediationManager = class {
|
|
|
21881
22297
|
* 4th offence and so on, clamped to `max_ban_seconds`.
|
|
21882
22298
|
*/
|
|
21883
22299
|
async ban(ip, reason, opts = {}) {
|
|
21884
|
-
if ((0,
|
|
22300
|
+
if ((0, import_node_net16.isIP)(ip) === 0) return { ok: false, error: `not an IP address: ${ip}` };
|
|
21885
22301
|
if (opts.strict ? this.isFleetAddress(ip) : this.isAllowlisted(ip)) {
|
|
21886
22302
|
return { ok: false, error: `${ip} is protected and can never be blocked` };
|
|
21887
22303
|
}
|
|
@@ -22158,7 +22574,7 @@ var RemediationManager = class {
|
|
|
22158
22574
|
if (saved && typeof saved === "object" && this.config.protect_recent_operators) {
|
|
22159
22575
|
const cutoff = Date.now() - this.config.operator_memory_seconds * 1e3;
|
|
22160
22576
|
for (const [ip, at] of Object.entries(saved)) {
|
|
22161
|
-
if (typeof at !== "number" || at < cutoff || !(0,
|
|
22577
|
+
if (typeof at !== "number" || at < cutoff || !(0, import_node_net16.isIP)(ip)) continue;
|
|
22162
22578
|
if (this.blocklist.some((b) => b.ip === ip && b.strict)) continue;
|
|
22163
22579
|
this.operators[ip] = at;
|
|
22164
22580
|
if (!this.protectedList.includes(ip)) this.protectedList.push(ip);
|
|
@@ -22183,7 +22599,7 @@ var RemediationManager = class {
|
|
|
22183
22599
|
}
|
|
22184
22600
|
logLine(line) {
|
|
22185
22601
|
try {
|
|
22186
|
-
(0,
|
|
22602
|
+
(0, import_node_fs36.appendFileSync)(PATHS.logFile, `${(/* @__PURE__ */ new Date()).toISOString()} ${line}
|
|
22187
22603
|
`);
|
|
22188
22604
|
} catch {
|
|
22189
22605
|
}
|
|
@@ -22223,11 +22639,11 @@ function remediationSettings(section) {
|
|
|
22223
22639
|
}
|
|
22224
22640
|
|
|
22225
22641
|
// src/daemon/watchdog-service.ts
|
|
22226
|
-
var
|
|
22642
|
+
var import_node_fs38 = require("fs");
|
|
22227
22643
|
var import_node_path29 = require("path");
|
|
22228
22644
|
|
|
22229
22645
|
// src/core/watchdog.ts
|
|
22230
|
-
var
|
|
22646
|
+
var import_node_fs37 = require("fs");
|
|
22231
22647
|
var import_node_crypto9 = require("crypto");
|
|
22232
22648
|
var import_node_path28 = require("path");
|
|
22233
22649
|
var WATCHDOG_DIRNAME = "watchdog";
|
|
@@ -22320,10 +22736,10 @@ var WatchdogLog = class {
|
|
|
22320
22736
|
this.dir = dir;
|
|
22321
22737
|
this.file = (0, import_node_path28.join)(dir, LOG_FILE2);
|
|
22322
22738
|
this.shareGid = options.shareGid;
|
|
22323
|
-
(0,
|
|
22739
|
+
(0, import_node_fs37.mkdirSync)(dir, { recursive: true, mode: this.shareGid === void 0 ? 448 : 1528 });
|
|
22324
22740
|
if (this.shareGid !== void 0) {
|
|
22325
|
-
(0,
|
|
22326
|
-
(0,
|
|
22741
|
+
(0, import_node_fs37.chownSync)(dir, process.getuid?.() ?? 0, this.shareGid);
|
|
22742
|
+
(0, import_node_fs37.chmodSync)(dir, 1528);
|
|
22327
22743
|
}
|
|
22328
22744
|
for (const r of readTail(this.file, 512 * 1024)) this.remember(dedupeKey(r));
|
|
22329
22745
|
}
|
|
@@ -22352,16 +22768,16 @@ var WatchdogLog = class {
|
|
|
22352
22768
|
const chunk = this.buffer.join("\n") + "\n";
|
|
22353
22769
|
this.buffer = [];
|
|
22354
22770
|
try {
|
|
22355
|
-
if ((0,
|
|
22356
|
-
(0,
|
|
22771
|
+
if ((0, import_node_fs37.statSync)(this.file).size > ROTATE_BYTES) {
|
|
22772
|
+
(0, import_node_fs37.renameSync)(this.file, (0, import_node_path28.join)(this.dir, ROTATED_FILE));
|
|
22357
22773
|
}
|
|
22358
22774
|
} catch {
|
|
22359
22775
|
}
|
|
22360
|
-
(0,
|
|
22776
|
+
(0, import_node_fs37.appendFileSync)(this.file, chunk, { mode: this.shareGid === void 0 ? 384 : 432 });
|
|
22361
22777
|
if (this.shareGid !== void 0) {
|
|
22362
22778
|
try {
|
|
22363
|
-
(0,
|
|
22364
|
-
(0,
|
|
22779
|
+
(0, import_node_fs37.chownSync)(this.file, process.getuid?.() ?? 0, this.shareGid);
|
|
22780
|
+
(0, import_node_fs37.chmodSync)(this.file, 432);
|
|
22365
22781
|
} catch {
|
|
22366
22782
|
}
|
|
22367
22783
|
}
|
|
@@ -22382,21 +22798,21 @@ function parseLines(text) {
|
|
|
22382
22798
|
function readTail(file, bytes) {
|
|
22383
22799
|
let fd;
|
|
22384
22800
|
try {
|
|
22385
|
-
fd = (0,
|
|
22801
|
+
fd = (0, import_node_fs37.openSync)(file, "r");
|
|
22386
22802
|
} catch (err) {
|
|
22387
22803
|
if (err.code === "ENOENT") return [];
|
|
22388
22804
|
throw err;
|
|
22389
22805
|
}
|
|
22390
22806
|
try {
|
|
22391
|
-
const size = (0,
|
|
22807
|
+
const size = (0, import_node_fs37.fstatSync)(fd).size;
|
|
22392
22808
|
const start = Math.max(0, size - bytes);
|
|
22393
22809
|
const buf = Buffer.alloc(size - start);
|
|
22394
|
-
(0,
|
|
22810
|
+
(0, import_node_fs37.readSync)(fd, buf, 0, buf.length, start);
|
|
22395
22811
|
let text = buf.toString("utf8");
|
|
22396
22812
|
if (start > 0) text = text.slice(text.indexOf("\n") + 1);
|
|
22397
22813
|
return parseLines(text);
|
|
22398
22814
|
} finally {
|
|
22399
|
-
(0,
|
|
22815
|
+
(0, import_node_fs37.closeSync)(fd);
|
|
22400
22816
|
}
|
|
22401
22817
|
}
|
|
22402
22818
|
|
|
@@ -22405,7 +22821,7 @@ function defaultShareGid() {
|
|
|
22405
22821
|
const isRoot2 = process.platform === "linux" && typeof process.getuid === "function" && process.getuid() === 0;
|
|
22406
22822
|
if (!isRoot2) return void 0;
|
|
22407
22823
|
try {
|
|
22408
|
-
return (0,
|
|
22824
|
+
return (0, import_node_fs38.statSync)("/var/log/auth.log").gid;
|
|
22409
22825
|
} catch {
|
|
22410
22826
|
return void 0;
|
|
22411
22827
|
}
|
|
@@ -22502,7 +22918,7 @@ async function flushTelemetry(timeoutMs = 2e3) {
|
|
|
22502
22918
|
}
|
|
22503
22919
|
|
|
22504
22920
|
// src/core/node-runtime.ts
|
|
22505
|
-
var
|
|
22921
|
+
var import_node_child_process14 = require("child_process");
|
|
22506
22922
|
var MIN_DAEMON_NODE_MAJOR = 22;
|
|
22507
22923
|
function nodeMajor(version) {
|
|
22508
22924
|
const m = String(version ?? "").trim().replace(/^v/, "").match(/^(\d+)\./);
|
|
@@ -22518,14 +22934,14 @@ function daemonNodeProblem(version) {
|
|
|
22518
22934
|
// src/daemon/index.ts
|
|
22519
22935
|
function readVersion2() {
|
|
22520
22936
|
try {
|
|
22521
|
-
const pkg = JSON.parse((0,
|
|
22937
|
+
const pkg = JSON.parse((0, import_node_fs39.readFileSync)((0, import_node_path30.join)(__dirname, "..", "package.json"), "utf-8"));
|
|
22522
22938
|
return pkg.version || "0.0.0";
|
|
22523
22939
|
} catch {
|
|
22524
22940
|
return "0.0.0";
|
|
22525
22941
|
}
|
|
22526
22942
|
}
|
|
22527
22943
|
async function runHardeningChecks() {
|
|
22528
|
-
const { stdout } = await (0, import_node_util.promisify)(
|
|
22944
|
+
const { stdout } = await (0, import_node_util.promisify)(import_node_child_process15.execFile)(
|
|
22529
22945
|
process.execPath,
|
|
22530
22946
|
[(0, import_node_path30.join)(__dirname, "index.js"), "harden", "--json", "--no-upload"],
|
|
22531
22947
|
{ timeout: 12e4, maxBuffer: 4 * 1024 * 1024 }
|
|
@@ -22539,7 +22955,7 @@ async function runHardeningChecks() {
|
|
|
22539
22955
|
return patches ? [...findings, patches] : findings;
|
|
22540
22956
|
}
|
|
22541
22957
|
async function runPatchCheck() {
|
|
22542
|
-
const { stdout } = await (0, import_node_util.promisify)(
|
|
22958
|
+
const { stdout } = await (0, import_node_util.promisify)(import_node_child_process15.execFile)(
|
|
22543
22959
|
process.execPath,
|
|
22544
22960
|
[(0, import_node_path30.join)(__dirname, "index.js"), "patches", "--json", "--no-upload"],
|
|
22545
22961
|
{ timeout: 3e5, maxBuffer: 16 * 1024 * 1024 }
|
|
@@ -22549,7 +22965,7 @@ async function runPatchCheck() {
|
|
|
22549
22965
|
}
|
|
22550
22966
|
function logLine(line) {
|
|
22551
22967
|
try {
|
|
22552
|
-
(0,
|
|
22968
|
+
(0, import_node_fs39.appendFileSync)(PATHS.logFile, `${(/* @__PURE__ */ new Date()).toISOString()} ${line}
|
|
22553
22969
|
`);
|
|
22554
22970
|
} catch {
|
|
22555
22971
|
}
|
|
@@ -22574,9 +22990,9 @@ async function runDaemon() {
|
|
|
22574
22990
|
logLine(`[daemon] starting threatcrushd v${version} mode=${PATHS.mode}`);
|
|
22575
22991
|
const restartDaemon = () => {
|
|
22576
22992
|
if (PATHS.mode === "system") {
|
|
22577
|
-
(0,
|
|
22993
|
+
(0, import_node_child_process15.spawn)("systemctl", ["restart", "threatcrushd"], { detached: true, stdio: "ignore" }).unref();
|
|
22578
22994
|
} else {
|
|
22579
|
-
(0,
|
|
22995
|
+
(0, import_node_child_process15.spawn)(process.execPath, process.argv.slice(1), { detached: true, stdio: "ignore" }).unref();
|
|
22580
22996
|
setTimeout(() => process.exit(0), 500).unref();
|
|
22581
22997
|
}
|
|
22582
22998
|
};
|
|
@@ -22712,6 +23128,12 @@ async function runDaemon() {
|
|
|
22712
23128
|
log: logLine
|
|
22713
23129
|
});
|
|
22714
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();
|
|
22715
23137
|
const lightningWatch = new LightningWatch({
|
|
22716
23138
|
bus,
|
|
22717
23139
|
enabled: config.lightning?.enabled !== false,
|
|
@@ -22719,6 +23141,13 @@ async function runDaemon() {
|
|
|
22719
23141
|
log: logLine
|
|
22720
23142
|
});
|
|
22721
23143
|
lightningWatch.start();
|
|
23144
|
+
const dockerLogs = new DockerLogsWatch({
|
|
23145
|
+
bus,
|
|
23146
|
+
settings: dockerLogsSettings(config.docker_logs),
|
|
23147
|
+
protectedList: remediation.getAllowlist(),
|
|
23148
|
+
log: logLine
|
|
23149
|
+
});
|
|
23150
|
+
dockerLogs.start();
|
|
22722
23151
|
let cloudSyncRef = null;
|
|
22723
23152
|
new AlertDispatcher(bus, config, { cloudHealthy: () => cloudSyncRef?.healthy ?? false, log: logLine });
|
|
22724
23153
|
if (loaded.error) {
|
|
@@ -22775,6 +23204,7 @@ async function runDaemon() {
|
|
|
22775
23204
|
if (torPinned || typeof block_tor_ssh !== "boolean" || block_tor_ssh === remediation.blockTorSsh) return;
|
|
22776
23205
|
remediation.setBlockTorSsh(block_tor_ssh);
|
|
22777
23206
|
logLine(`[ssh-intrusion] Tor SSH ban ${block_tor_ssh ? "ON" : "OFF"} (org setting from the dashboard)`);
|
|
23207
|
+
void torBlock.sync();
|
|
22778
23208
|
}
|
|
22779
23209
|
});
|
|
22780
23210
|
allowlistSync.start();
|
|
@@ -22863,6 +23293,10 @@ async function runDaemon() {
|
|
|
22863
23293
|
lightningWatch.stop();
|
|
22864
23294
|
} catch {
|
|
22865
23295
|
}
|
|
23296
|
+
try {
|
|
23297
|
+
dockerLogs.stop();
|
|
23298
|
+
} catch {
|
|
23299
|
+
}
|
|
22866
23300
|
try {
|
|
22867
23301
|
await ipc.stop();
|
|
22868
23302
|
} catch {
|