@profullstack/threatcrush 0.13.31 → 0.13.33
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 +330 -65
- package/dist/daemon.js.map +1 -1
- package/dist/index.js +833 -157
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/daemon.js
CHANGED
|
@@ -2022,10 +2022,10 @@ var require_toml = __commonJS({
|
|
|
2022
2022
|
});
|
|
2023
2023
|
|
|
2024
2024
|
// src/daemon/index.ts
|
|
2025
|
-
var
|
|
2026
|
-
var
|
|
2025
|
+
var import_node_fs25 = require("fs");
|
|
2026
|
+
var import_node_child_process8 = require("child_process");
|
|
2027
2027
|
var import_node_os5 = require("os");
|
|
2028
|
-
var
|
|
2028
|
+
var import_node_path19 = require("path");
|
|
2029
2029
|
var import_node_util = require("util");
|
|
2030
2030
|
|
|
2031
2031
|
// src/daemon/paths.ts
|
|
@@ -10495,6 +10495,7 @@ function loadConfig(configPath) {
|
|
|
10495
10495
|
detection: parsed.detection,
|
|
10496
10496
|
cloud: parsed.cloud,
|
|
10497
10497
|
watchdog: parsed.watchdog,
|
|
10498
|
+
update: parsed.update,
|
|
10498
10499
|
storage: parsed.storage,
|
|
10499
10500
|
license: parsed.license
|
|
10500
10501
|
};
|
|
@@ -15518,7 +15519,8 @@ function findingEvents(serverId, results, observedAt = /* @__PURE__ */ new Date(
|
|
|
15518
15519
|
severity: r.severity,
|
|
15519
15520
|
title: r.title.slice(0, MAX_TITLE_CHARS),
|
|
15520
15521
|
recommendation: r.recommendation,
|
|
15521
|
-
observed_at: observedAt.toISOString()
|
|
15522
|
+
observed_at: observedAt.toISOString(),
|
|
15523
|
+
...r.details ? { details: trimMetadata(r.details) } : {}
|
|
15522
15524
|
}));
|
|
15523
15525
|
}
|
|
15524
15526
|
|
|
@@ -16185,6 +16187,241 @@ var RemediationConsumer = class {
|
|
|
16185
16187
|
}
|
|
16186
16188
|
};
|
|
16187
16189
|
|
|
16190
|
+
// src/daemon/workers/auto-update.ts
|
|
16191
|
+
var import_node_child_process5 = require("child_process");
|
|
16192
|
+
var import_node_fs19 = require("fs");
|
|
16193
|
+
var import_node_path14 = require("path");
|
|
16194
|
+
var PKG_NAME = "@profullstack/threatcrush";
|
|
16195
|
+
var REGISTRY = `https://registry.npmjs.org/${PKG_NAME}`;
|
|
16196
|
+
var DEFAULT_INTERVAL_MS = 6 * 36e5;
|
|
16197
|
+
var DEFAULT_MIN_AGE_MS = 2 * 36e5;
|
|
16198
|
+
var RETRY_AFTER_MS = 24 * 36e5;
|
|
16199
|
+
function isNewer(a, b) {
|
|
16200
|
+
const parse = (v) => {
|
|
16201
|
+
const [core, pre] = v.replace(/^v/, "").split("-", 2);
|
|
16202
|
+
return { nums: core.split(".").map((n) => Number.parseInt(n, 10) || 0), pre: pre ?? null };
|
|
16203
|
+
};
|
|
16204
|
+
const x = parse(a);
|
|
16205
|
+
const y = parse(b);
|
|
16206
|
+
for (let i = 0; i < 3; i++) {
|
|
16207
|
+
if ((x.nums[i] ?? 0) !== (y.nums[i] ?? 0)) return (x.nums[i] ?? 0) > (y.nums[i] ?? 0);
|
|
16208
|
+
}
|
|
16209
|
+
if (x.pre === y.pre) return false;
|
|
16210
|
+
if (x.pre === null) return true;
|
|
16211
|
+
if (y.pre === null) return false;
|
|
16212
|
+
return x.pre > y.pre;
|
|
16213
|
+
}
|
|
16214
|
+
function pickUpdate(doc, current, opts) {
|
|
16215
|
+
const latest = doc["dist-tags"]?.latest;
|
|
16216
|
+
if (!latest) return { version: null, reason: "registry has no latest tag" };
|
|
16217
|
+
if (!isNewer(latest, current)) return { version: null, reason: `up to date (${current})` };
|
|
16218
|
+
const major = (v) => Number.parseInt(v.replace(/^v/, ""), 10);
|
|
16219
|
+
if (!opts.allowMajor && major(latest) !== major(current)) {
|
|
16220
|
+
return { version: null, reason: `${latest} is a new major; set [update] allow_major = true to take it` };
|
|
16221
|
+
}
|
|
16222
|
+
const published = Date.parse(doc.time?.[latest] ?? "");
|
|
16223
|
+
if (!Number.isFinite(published)) return { version: null, reason: `no publish time for ${latest}` };
|
|
16224
|
+
if (opts.now - published < opts.minAgeMs) {
|
|
16225
|
+
return { version: null, reason: `${latest} is younger than min_age; waiting` };
|
|
16226
|
+
}
|
|
16227
|
+
return { version: latest };
|
|
16228
|
+
}
|
|
16229
|
+
function installPlan(opts) {
|
|
16230
|
+
const expected = `${import_node_path14.sep}lib${import_node_path14.sep}node_modules${import_node_path14.sep}@profullstack${import_node_path14.sep}threatcrush`;
|
|
16231
|
+
if (!opts.pkgRoot.endsWith(expected)) {
|
|
16232
|
+
return { kind: "unsupported", reason: `not an npm global install (${opts.pkgRoot}); run \`threatcrush update\`` };
|
|
16233
|
+
}
|
|
16234
|
+
const nodeBin = (0, import_node_path14.dirname)(opts.execPath);
|
|
16235
|
+
const npm = (0, import_node_path14.join)(nodeBin, "npm");
|
|
16236
|
+
if (!opts.npmExists(npm)) return { kind: "unsupported", reason: `no npm beside ${opts.execPath}` };
|
|
16237
|
+
if (!opts.owner) return { kind: "unsupported", reason: `cannot tell who owns ${opts.pkgRoot}` };
|
|
16238
|
+
const npmArgs = [npm, "i", "-g", `${PKG_NAME}@${opts.version}`, "--no-fund", "--no-audit"];
|
|
16239
|
+
const path = `${nodeBin}:/usr/local/bin:/usr/bin:/bin`;
|
|
16240
|
+
if (opts.underSystemd && opts.uid === 0) {
|
|
16241
|
+
return {
|
|
16242
|
+
kind: "run",
|
|
16243
|
+
argv: [
|
|
16244
|
+
"systemd-run",
|
|
16245
|
+
"--wait",
|
|
16246
|
+
"--collect",
|
|
16247
|
+
"--quiet",
|
|
16248
|
+
`--unit=threatcrush-autoupdate-${opts.version.replace(/[^0-9A-Za-z]/g, "-")}`,
|
|
16249
|
+
`--uid=${opts.owner.uid}`,
|
|
16250
|
+
`--gid=${opts.owner.gid}`,
|
|
16251
|
+
`--setenv=HOME=${opts.owner.home}`,
|
|
16252
|
+
`--setenv=PATH=${path}`,
|
|
16253
|
+
...npmArgs
|
|
16254
|
+
]
|
|
16255
|
+
};
|
|
16256
|
+
}
|
|
16257
|
+
if (opts.uid === opts.owner.uid) {
|
|
16258
|
+
return { kind: "run", argv: npmArgs, env: { PATH: path } };
|
|
16259
|
+
}
|
|
16260
|
+
return {
|
|
16261
|
+
kind: "unsupported",
|
|
16262
|
+
reason: `install is owned by uid ${opts.owner.uid}, daemon runs as ${opts.uid}; run \`threatcrush update\` as that user`
|
|
16263
|
+
};
|
|
16264
|
+
}
|
|
16265
|
+
function ownerOf(path, passwd = "/etc/passwd") {
|
|
16266
|
+
try {
|
|
16267
|
+
const st = (0, import_node_fs19.statSync)(path);
|
|
16268
|
+
let home = st.uid === 0 ? "/root" : "";
|
|
16269
|
+
try {
|
|
16270
|
+
for (const line of (0, import_node_fs19.readFileSync)(passwd, "utf8").split("\n")) {
|
|
16271
|
+
const f = line.split(":");
|
|
16272
|
+
if (Number(f[2]) === st.uid && f[5]) home = f[5];
|
|
16273
|
+
}
|
|
16274
|
+
} catch {
|
|
16275
|
+
}
|
|
16276
|
+
return home ? { uid: st.uid, gid: st.gid, home } : null;
|
|
16277
|
+
} catch {
|
|
16278
|
+
return null;
|
|
16279
|
+
}
|
|
16280
|
+
}
|
|
16281
|
+
function updateSettings(section, env2 = process.env) {
|
|
16282
|
+
const interval = parseDuration(section?.interval);
|
|
16283
|
+
const minAge = parseDuration(section?.min_age);
|
|
16284
|
+
return {
|
|
16285
|
+
enabled: section?.auto !== false && env2.THREATCRUSH_AUTO_UPDATE !== "0",
|
|
16286
|
+
intervalMs: interval ? Math.max(15 * 60, interval) * 1e3 : DEFAULT_INTERVAL_MS,
|
|
16287
|
+
minAgeMs: minAge !== null ? minAge * 1e3 : DEFAULT_MIN_AGE_MS,
|
|
16288
|
+
allowMajor: section?.allow_major === true
|
|
16289
|
+
};
|
|
16290
|
+
}
|
|
16291
|
+
function defaultRun(argv, env2) {
|
|
16292
|
+
return new Promise((resolve3) => {
|
|
16293
|
+
const child = (0, import_node_child_process5.spawn)(argv[0], argv.slice(1), {
|
|
16294
|
+
env: { ...process.env, ...env2 },
|
|
16295
|
+
stdio: ["ignore", "pipe", "pipe"]
|
|
16296
|
+
});
|
|
16297
|
+
let output = "";
|
|
16298
|
+
const keep = (b) => {
|
|
16299
|
+
output = (output + b.toString()).slice(-4e3);
|
|
16300
|
+
};
|
|
16301
|
+
child.stdout?.on("data", keep);
|
|
16302
|
+
child.stderr?.on("data", keep);
|
|
16303
|
+
child.on("error", (err) => resolve3({ code: -1, output: err.message }));
|
|
16304
|
+
child.on("close", (code) => resolve3({ code: code ?? -1, output }));
|
|
16305
|
+
});
|
|
16306
|
+
}
|
|
16307
|
+
var AutoUpdate = class {
|
|
16308
|
+
constructor(opts) {
|
|
16309
|
+
this.opts = opts;
|
|
16310
|
+
this.fetchImpl = opts.fetchImpl ?? fetch;
|
|
16311
|
+
this.run = opts.run ?? defaultRun;
|
|
16312
|
+
this.now = opts.now ?? Date.now;
|
|
16313
|
+
this.random = opts.random ?? Math.random;
|
|
16314
|
+
}
|
|
16315
|
+
opts;
|
|
16316
|
+
timer = null;
|
|
16317
|
+
busy = false;
|
|
16318
|
+
fetchImpl;
|
|
16319
|
+
run;
|
|
16320
|
+
now;
|
|
16321
|
+
random;
|
|
16322
|
+
start() {
|
|
16323
|
+
if (!this.opts.settings.enabled) {
|
|
16324
|
+
this.opts.log("[update] auto-update off ([update] auto = false or THREATCRUSH_AUTO_UPDATE=0)");
|
|
16325
|
+
return;
|
|
16326
|
+
}
|
|
16327
|
+
this.schedule(10 * 6e4 + this.random() * 30 * 6e4);
|
|
16328
|
+
}
|
|
16329
|
+
stop() {
|
|
16330
|
+
if (this.timer) clearTimeout(this.timer);
|
|
16331
|
+
this.timer = null;
|
|
16332
|
+
}
|
|
16333
|
+
schedule(ms) {
|
|
16334
|
+
this.timer = setTimeout(() => {
|
|
16335
|
+
void this.check().finally(() => {
|
|
16336
|
+
if (this.timer !== null) this.schedule(this.opts.settings.intervalMs + this.random() * 36e5);
|
|
16337
|
+
});
|
|
16338
|
+
}, ms);
|
|
16339
|
+
this.timer.unref?.();
|
|
16340
|
+
}
|
|
16341
|
+
/** One check; returns what it did, for logs and tests. */
|
|
16342
|
+
async check() {
|
|
16343
|
+
if (this.busy) return "busy";
|
|
16344
|
+
this.busy = true;
|
|
16345
|
+
try {
|
|
16346
|
+
let doc;
|
|
16347
|
+
try {
|
|
16348
|
+
const res = await this.fetchImpl(REGISTRY, {
|
|
16349
|
+
headers: { accept: "application/json" },
|
|
16350
|
+
signal: AbortSignal.timeout(2e4)
|
|
16351
|
+
});
|
|
16352
|
+
if (!res.ok) return this.note(`registry answered ${res.status}`);
|
|
16353
|
+
doc = await res.json();
|
|
16354
|
+
} catch (err) {
|
|
16355
|
+
return this.note(`registry unreachable: ${err.message}`);
|
|
16356
|
+
}
|
|
16357
|
+
const pick = pickUpdate(doc, this.opts.version, {
|
|
16358
|
+
allowMajor: this.opts.settings.allowMajor,
|
|
16359
|
+
minAgeMs: this.opts.settings.minAgeMs,
|
|
16360
|
+
now: this.now()
|
|
16361
|
+
});
|
|
16362
|
+
if (pick.version === null) return this.note(pick.reason, false);
|
|
16363
|
+
const target = pick.version;
|
|
16364
|
+
const last = this.lastAttempt();
|
|
16365
|
+
if (last?.version === target && this.now() - last.at < RETRY_AFTER_MS) {
|
|
16366
|
+
return this.note(`${target} was tried at ${new Date(last.at).toISOString()} and did not take; retrying after a day`);
|
|
16367
|
+
}
|
|
16368
|
+
const plan = installPlan({
|
|
16369
|
+
version: target,
|
|
16370
|
+
pkgRoot: this.opts.pkgRoot,
|
|
16371
|
+
execPath: process.execPath,
|
|
16372
|
+
uid: process.getuid?.() ?? -1,
|
|
16373
|
+
owner: ownerOf(this.opts.pkgRoot),
|
|
16374
|
+
underSystemd: this.opts.underSystemd ?? Boolean(process.env.INVOCATION_ID),
|
|
16375
|
+
npmExists: import_node_fs19.existsSync
|
|
16376
|
+
});
|
|
16377
|
+
if (plan.kind === "unsupported") return this.note(`${target} is available but cannot self-install: ${plan.reason}`);
|
|
16378
|
+
this.recordAttempt(target);
|
|
16379
|
+
this.event(`updating ThreatCrush ${this.opts.version} \u2192 ${target}`, { from: this.opts.version, to: target });
|
|
16380
|
+
this.opts.log(`[update] installing ${target}: ${plan.argv.join(" ")}`);
|
|
16381
|
+
const { code, output } = await this.run(plan.argv, plan.env);
|
|
16382
|
+
if (code !== 0) {
|
|
16383
|
+
const tail = output.trim().split("\n").slice(-3).join(" | ");
|
|
16384
|
+
this.event(`auto-update to ${target} failed (exit ${code})`, { to: target, code, output: tail });
|
|
16385
|
+
return this.note(`install of ${target} failed (exit ${code}): ${tail}`);
|
|
16386
|
+
}
|
|
16387
|
+
this.opts.log(`[update] ${target} installed; restarting onto it`);
|
|
16388
|
+
this.opts.restartDaemon();
|
|
16389
|
+
return `updated to ${target}`;
|
|
16390
|
+
} finally {
|
|
16391
|
+
this.busy = false;
|
|
16392
|
+
}
|
|
16393
|
+
}
|
|
16394
|
+
note(line, log = true) {
|
|
16395
|
+
if (log) this.opts.log(`[update] ${line}`);
|
|
16396
|
+
return line;
|
|
16397
|
+
}
|
|
16398
|
+
event(message, details) {
|
|
16399
|
+
this.opts.bus.publish({
|
|
16400
|
+
timestamp: new Date(this.now()),
|
|
16401
|
+
module: "auto-update",
|
|
16402
|
+
category: "system",
|
|
16403
|
+
severity: "info",
|
|
16404
|
+
message,
|
|
16405
|
+
details
|
|
16406
|
+
});
|
|
16407
|
+
}
|
|
16408
|
+
lastAttempt() {
|
|
16409
|
+
try {
|
|
16410
|
+
const v = JSON.parse((0, import_node_fs19.readFileSync)(this.opts.statePath, "utf8"));
|
|
16411
|
+
return v.version && typeof v.at === "number" ? { version: v.version, at: v.at } : null;
|
|
16412
|
+
} catch {
|
|
16413
|
+
return null;
|
|
16414
|
+
}
|
|
16415
|
+
}
|
|
16416
|
+
recordAttempt(version) {
|
|
16417
|
+
try {
|
|
16418
|
+
(0, import_node_fs19.writeFileSync)(this.opts.statePath, JSON.stringify({ version, at: this.now() }));
|
|
16419
|
+
} catch (err) {
|
|
16420
|
+
this.opts.log(`[update] could not record attempt: ${err.message}`);
|
|
16421
|
+
}
|
|
16422
|
+
}
|
|
16423
|
+
};
|
|
16424
|
+
|
|
16188
16425
|
// src/daemon/rules/engine.ts
|
|
16189
16426
|
var MAX_WINDOW_ENTRIES = 1e3;
|
|
16190
16427
|
function live(window) {
|
|
@@ -16346,8 +16583,8 @@ var RuleEngine = class {
|
|
|
16346
16583
|
};
|
|
16347
16584
|
|
|
16348
16585
|
// src/daemon/rules/loader.ts
|
|
16349
|
-
var
|
|
16350
|
-
var
|
|
16586
|
+
var import_node_fs20 = require("fs");
|
|
16587
|
+
var import_node_path15 = require("path");
|
|
16351
16588
|
|
|
16352
16589
|
// src/daemon/rules/default-rules.ts
|
|
16353
16590
|
var AT_CRS_THRESHOLD = { field: "severity", operator: "regex", value: "^(high|critical)$" };
|
|
@@ -16748,11 +16985,11 @@ var RULES_DIR = "/etc/threatcrush/rules.d";
|
|
|
16748
16985
|
function loadAllRules(customDir) {
|
|
16749
16986
|
const rules = [...DEFAULT_RULES];
|
|
16750
16987
|
const dir = customDir || RULES_DIR;
|
|
16751
|
-
if ((0,
|
|
16752
|
-
const files = (0,
|
|
16988
|
+
if ((0, import_node_fs20.existsSync)(dir)) {
|
|
16989
|
+
const files = (0, import_node_fs20.readdirSync)(dir).filter((f) => f.endsWith(".json"));
|
|
16753
16990
|
for (const file of files) {
|
|
16754
16991
|
try {
|
|
16755
|
-
const raw = (0,
|
|
16992
|
+
const raw = (0, import_node_fs20.readFileSync)((0, import_node_path15.join)(dir, file), "utf-8");
|
|
16756
16993
|
const parsed = JSON.parse(raw);
|
|
16757
16994
|
const customRules = Array.isArray(parsed) ? parsed : [parsed];
|
|
16758
16995
|
for (const rule of customRules) {
|
|
@@ -16783,9 +17020,9 @@ function loadAllRules(customDir) {
|
|
|
16783
17020
|
}
|
|
16784
17021
|
|
|
16785
17022
|
// src/daemon/firewall/adapters.ts
|
|
16786
|
-
var
|
|
16787
|
-
var
|
|
16788
|
-
var
|
|
17023
|
+
var import_node_child_process6 = require("child_process");
|
|
17024
|
+
var import_node_fs21 = require("fs");
|
|
17025
|
+
var import_node_path16 = require("path");
|
|
16789
17026
|
var import_node_net6 = require("net");
|
|
16790
17027
|
function assertValidFirewallIp(ip) {
|
|
16791
17028
|
if ((0, import_node_net6.isIP)(ip) !== 4) {
|
|
@@ -16798,9 +17035,9 @@ function assertBannableIp(ip) {
|
|
|
16798
17035
|
}
|
|
16799
17036
|
}
|
|
16800
17037
|
function writeIfAbsent(path, contents) {
|
|
16801
|
-
(0,
|
|
17038
|
+
(0, import_node_fs21.mkdirSync)((0, import_node_path16.dirname)(path), { recursive: true });
|
|
16802
17039
|
try {
|
|
16803
|
-
(0,
|
|
17040
|
+
(0, import_node_fs21.writeFileSync)(path, contents, { flag: "wx" });
|
|
16804
17041
|
} catch (err) {
|
|
16805
17042
|
if (err.code !== "EEXIST") throw err;
|
|
16806
17043
|
}
|
|
@@ -16819,11 +17056,11 @@ var Fail2banAdapter = class {
|
|
|
16819
17056
|
enforces = true;
|
|
16820
17057
|
jailReady = false;
|
|
16821
17058
|
isAvailable() {
|
|
16822
|
-
const result = (0,
|
|
17059
|
+
const result = (0, import_node_child_process6.spawnSync)("fail2ban-client", ["ping"], { stdio: "pipe" });
|
|
16823
17060
|
return result.status === 0;
|
|
16824
17061
|
}
|
|
16825
17062
|
client(args) {
|
|
16826
|
-
return (0,
|
|
17063
|
+
return (0, import_node_child_process6.execFileSync)("fail2ban-client", args, {
|
|
16827
17064
|
encoding: "utf-8",
|
|
16828
17065
|
stdio: ["ignore", "pipe", "pipe"]
|
|
16829
17066
|
});
|
|
@@ -16919,35 +17156,35 @@ var NftablesAdapter = class {
|
|
|
16919
17156
|
table = "threatcrush";
|
|
16920
17157
|
set = "blocklist";
|
|
16921
17158
|
isAvailable() {
|
|
16922
|
-
const result = (0,
|
|
17159
|
+
const result = (0, import_node_child_process6.spawnSync)("nft", ["--version"], { stdio: "pipe" });
|
|
16923
17160
|
return result.status === 0;
|
|
16924
17161
|
}
|
|
16925
17162
|
ensureSetup() {
|
|
16926
17163
|
try {
|
|
16927
|
-
(0,
|
|
17164
|
+
(0, import_node_child_process6.execSync)(`nft list table inet ${this.table} 2>/dev/null`, { stdio: "pipe" });
|
|
16928
17165
|
} catch {
|
|
16929
|
-
(0,
|
|
16930
|
-
(0,
|
|
16931
|
-
(0,
|
|
16932
|
-
(0,
|
|
17166
|
+
(0, import_node_child_process6.execSync)(`nft add table inet ${this.table}`);
|
|
17167
|
+
(0, import_node_child_process6.execSync)(`nft add set inet ${this.table} ${this.set} '{ type ipv4_addr; flags timeout; }'`);
|
|
17168
|
+
(0, import_node_child_process6.execSync)(`nft add chain inet ${this.table} input '{ type filter hook input priority -1; policy accept; }'`);
|
|
17169
|
+
(0, import_node_child_process6.execSync)(`nft add rule inet ${this.table} input ip saddr @${this.set} drop`);
|
|
16933
17170
|
}
|
|
16934
17171
|
}
|
|
16935
17172
|
async block(ip) {
|
|
16936
17173
|
assertValidFirewallIp(ip);
|
|
16937
17174
|
this.ensureSetup();
|
|
16938
|
-
(0,
|
|
17175
|
+
(0, import_node_child_process6.execSync)(`nft add element inet ${this.table} ${this.set} '{ ${ip} }'`);
|
|
16939
17176
|
}
|
|
16940
17177
|
async unblock(ip) {
|
|
16941
17178
|
assertValidFirewallIp(ip);
|
|
16942
17179
|
try {
|
|
16943
|
-
(0,
|
|
17180
|
+
(0, import_node_child_process6.execSync)(`nft delete element inet ${this.table} ${this.set} '{ ${ip} }'`);
|
|
16944
17181
|
} catch {
|
|
16945
17182
|
}
|
|
16946
17183
|
}
|
|
16947
17184
|
async isBlocked(ip) {
|
|
16948
17185
|
assertValidFirewallIp(ip);
|
|
16949
17186
|
try {
|
|
16950
|
-
const output = (0,
|
|
17187
|
+
const output = (0, import_node_child_process6.execSync)(`nft list set inet ${this.table} ${this.set}`, { encoding: "utf-8" });
|
|
16951
17188
|
return output.includes(ip);
|
|
16952
17189
|
} catch {
|
|
16953
17190
|
return false;
|
|
@@ -16955,7 +17192,7 @@ var NftablesAdapter = class {
|
|
|
16955
17192
|
}
|
|
16956
17193
|
async listBlocked() {
|
|
16957
17194
|
try {
|
|
16958
|
-
const output = (0,
|
|
17195
|
+
const output = (0, import_node_child_process6.execSync)(`nft list set inet ${this.table} ${this.set}`, { encoding: "utf-8" });
|
|
16959
17196
|
const match = output.match(/elements\s*=\s*\{([^}]*)\}/);
|
|
16960
17197
|
if (!match) return [];
|
|
16961
17198
|
return match[1].split(",").map((s) => s.trim().split(/\s/)[0]).filter(Boolean);
|
|
@@ -16968,34 +17205,34 @@ var IptablesAdapter = class {
|
|
|
16968
17205
|
name = "iptables";
|
|
16969
17206
|
chain = "THREATCRUSH";
|
|
16970
17207
|
isAvailable() {
|
|
16971
|
-
const result = (0,
|
|
17208
|
+
const result = (0, import_node_child_process6.spawnSync)("iptables", ["--version"], { stdio: "pipe" });
|
|
16972
17209
|
return result.status === 0;
|
|
16973
17210
|
}
|
|
16974
17211
|
ensureChain() {
|
|
16975
17212
|
try {
|
|
16976
|
-
(0,
|
|
17213
|
+
(0, import_node_child_process6.execSync)(`iptables -n -L ${this.chain} 2>/dev/null`, { stdio: "pipe" });
|
|
16977
17214
|
} catch {
|
|
16978
|
-
(0,
|
|
16979
|
-
(0,
|
|
17215
|
+
(0, import_node_child_process6.execSync)(`iptables -N ${this.chain}`);
|
|
17216
|
+
(0, import_node_child_process6.execSync)(`iptables -I INPUT 1 -j ${this.chain}`);
|
|
16980
17217
|
}
|
|
16981
17218
|
}
|
|
16982
17219
|
async block(ip) {
|
|
16983
17220
|
assertValidFirewallIp(ip);
|
|
16984
17221
|
this.ensureChain();
|
|
16985
17222
|
if (await this.isBlocked(ip)) return;
|
|
16986
|
-
(0,
|
|
17223
|
+
(0, import_node_child_process6.execSync)(`iptables -A ${this.chain} -s ${ip} -j DROP`);
|
|
16987
17224
|
}
|
|
16988
17225
|
async unblock(ip) {
|
|
16989
17226
|
assertValidFirewallIp(ip);
|
|
16990
17227
|
try {
|
|
16991
|
-
(0,
|
|
17228
|
+
(0, import_node_child_process6.execSync)(`iptables -D ${this.chain} -s ${ip} -j DROP`);
|
|
16992
17229
|
} catch {
|
|
16993
17230
|
}
|
|
16994
17231
|
}
|
|
16995
17232
|
async isBlocked(ip) {
|
|
16996
17233
|
assertValidFirewallIp(ip);
|
|
16997
17234
|
try {
|
|
16998
|
-
const output = (0,
|
|
17235
|
+
const output = (0, import_node_child_process6.execSync)(`iptables -n -L ${this.chain}`, { encoding: "utf-8" });
|
|
16999
17236
|
return output.includes(ip);
|
|
17000
17237
|
} catch {
|
|
17001
17238
|
return false;
|
|
@@ -17003,7 +17240,7 @@ var IptablesAdapter = class {
|
|
|
17003
17240
|
}
|
|
17004
17241
|
async listBlocked() {
|
|
17005
17242
|
try {
|
|
17006
|
-
const output = (0,
|
|
17243
|
+
const output = (0, import_node_child_process6.execSync)(`iptables -n -L ${this.chain}`, { encoding: "utf-8" });
|
|
17007
17244
|
const ips = [];
|
|
17008
17245
|
for (const line of output.split("\n")) {
|
|
17009
17246
|
const match = line.match(/DROP\s+all\s+--\s+(\d+\.\d+\.\d+\.\d+)/);
|
|
@@ -17059,7 +17296,7 @@ function detectFirewallAdapter(backend = "auto") {
|
|
|
17059
17296
|
}
|
|
17060
17297
|
|
|
17061
17298
|
// src/daemon/firewall/remediation.ts
|
|
17062
|
-
var
|
|
17299
|
+
var import_node_fs22 = require("fs");
|
|
17063
17300
|
var import_node_net7 = require("net");
|
|
17064
17301
|
|
|
17065
17302
|
// src/daemon/firewall/crawlers.ts
|
|
@@ -17471,7 +17708,7 @@ var RemediationManager = class {
|
|
|
17471
17708
|
}
|
|
17472
17709
|
logLine(line) {
|
|
17473
17710
|
try {
|
|
17474
|
-
(0,
|
|
17711
|
+
(0, import_node_fs22.appendFileSync)(PATHS.logFile, `${(/* @__PURE__ */ new Date()).toISOString()} ${line}
|
|
17475
17712
|
`);
|
|
17476
17713
|
} catch {
|
|
17477
17714
|
}
|
|
@@ -17505,13 +17742,13 @@ function remediationSettings(section) {
|
|
|
17505
17742
|
}
|
|
17506
17743
|
|
|
17507
17744
|
// src/daemon/watchdog-service.ts
|
|
17508
|
-
var
|
|
17509
|
-
var
|
|
17745
|
+
var import_node_fs24 = require("fs");
|
|
17746
|
+
var import_node_path18 = require("path");
|
|
17510
17747
|
|
|
17511
17748
|
// src/core/watchdog.ts
|
|
17512
|
-
var
|
|
17749
|
+
var import_node_fs23 = require("fs");
|
|
17513
17750
|
var import_node_crypto5 = require("crypto");
|
|
17514
|
-
var
|
|
17751
|
+
var import_node_path17 = require("path");
|
|
17515
17752
|
var WATCHDOG_DIRNAME = "watchdog";
|
|
17516
17753
|
var LOG_FILE = "attacks.jsonl";
|
|
17517
17754
|
var ROTATED_FILE = "attacks.1.jsonl";
|
|
@@ -17600,12 +17837,12 @@ var WatchdogLog = class {
|
|
|
17600
17837
|
stats = { logged: 0, answered: 0, errored: 0 };
|
|
17601
17838
|
constructor(dir, options = {}) {
|
|
17602
17839
|
this.dir = dir;
|
|
17603
|
-
this.file = (0,
|
|
17840
|
+
this.file = (0, import_node_path17.join)(dir, LOG_FILE);
|
|
17604
17841
|
this.shareGid = options.shareGid;
|
|
17605
|
-
(0,
|
|
17842
|
+
(0, import_node_fs23.mkdirSync)(dir, { recursive: true, mode: this.shareGid === void 0 ? 448 : 1528 });
|
|
17606
17843
|
if (this.shareGid !== void 0) {
|
|
17607
|
-
(0,
|
|
17608
|
-
(0,
|
|
17844
|
+
(0, import_node_fs23.chownSync)(dir, process.getuid?.() ?? 0, this.shareGid);
|
|
17845
|
+
(0, import_node_fs23.chmodSync)(dir, 1528);
|
|
17609
17846
|
}
|
|
17610
17847
|
for (const r of readTail(this.file, 512 * 1024)) this.remember(dedupeKey(r));
|
|
17611
17848
|
}
|
|
@@ -17634,16 +17871,16 @@ var WatchdogLog = class {
|
|
|
17634
17871
|
const chunk = this.buffer.join("\n") + "\n";
|
|
17635
17872
|
this.buffer = [];
|
|
17636
17873
|
try {
|
|
17637
|
-
if ((0,
|
|
17638
|
-
(0,
|
|
17874
|
+
if ((0, import_node_fs23.statSync)(this.file).size > ROTATE_BYTES) {
|
|
17875
|
+
(0, import_node_fs23.renameSync)(this.file, (0, import_node_path17.join)(this.dir, ROTATED_FILE));
|
|
17639
17876
|
}
|
|
17640
17877
|
} catch {
|
|
17641
17878
|
}
|
|
17642
|
-
(0,
|
|
17879
|
+
(0, import_node_fs23.appendFileSync)(this.file, chunk, { mode: this.shareGid === void 0 ? 384 : 432 });
|
|
17643
17880
|
if (this.shareGid !== void 0) {
|
|
17644
17881
|
try {
|
|
17645
|
-
(0,
|
|
17646
|
-
(0,
|
|
17882
|
+
(0, import_node_fs23.chownSync)(this.file, process.getuid?.() ?? 0, this.shareGid);
|
|
17883
|
+
(0, import_node_fs23.chmodSync)(this.file, 432);
|
|
17647
17884
|
} catch {
|
|
17648
17885
|
}
|
|
17649
17886
|
}
|
|
@@ -17664,21 +17901,21 @@ function parseLines(text) {
|
|
|
17664
17901
|
function readTail(file, bytes) {
|
|
17665
17902
|
let fd;
|
|
17666
17903
|
try {
|
|
17667
|
-
fd = (0,
|
|
17904
|
+
fd = (0, import_node_fs23.openSync)(file, "r");
|
|
17668
17905
|
} catch (err) {
|
|
17669
17906
|
if (err.code === "ENOENT") return [];
|
|
17670
17907
|
throw err;
|
|
17671
17908
|
}
|
|
17672
17909
|
try {
|
|
17673
|
-
const size = (0,
|
|
17910
|
+
const size = (0, import_node_fs23.fstatSync)(fd).size;
|
|
17674
17911
|
const start = Math.max(0, size - bytes);
|
|
17675
17912
|
const buf = Buffer.alloc(size - start);
|
|
17676
|
-
(0,
|
|
17913
|
+
(0, import_node_fs23.readSync)(fd, buf, 0, buf.length, start);
|
|
17677
17914
|
let text = buf.toString("utf8");
|
|
17678
17915
|
if (start > 0) text = text.slice(text.indexOf("\n") + 1);
|
|
17679
17916
|
return parseLines(text);
|
|
17680
17917
|
} finally {
|
|
17681
|
-
(0,
|
|
17918
|
+
(0, import_node_fs23.closeSync)(fd);
|
|
17682
17919
|
}
|
|
17683
17920
|
}
|
|
17684
17921
|
|
|
@@ -17687,13 +17924,13 @@ function defaultShareGid() {
|
|
|
17687
17924
|
const isRoot2 = process.platform === "linux" && typeof process.getuid === "function" && process.getuid() === 0;
|
|
17688
17925
|
if (!isRoot2) return void 0;
|
|
17689
17926
|
try {
|
|
17690
|
-
return (0,
|
|
17927
|
+
return (0, import_node_fs24.statSync)("/var/log/auth.log").gid;
|
|
17691
17928
|
} catch {
|
|
17692
17929
|
return void 0;
|
|
17693
17930
|
}
|
|
17694
17931
|
}
|
|
17695
17932
|
function startWatchdog(opts) {
|
|
17696
|
-
const dir = opts.config?.dir ?? (0,
|
|
17933
|
+
const dir = opts.config?.dir ?? (0, import_node_path18.join)(opts.stateDir, WATCHDOG_DIRNAME);
|
|
17697
17934
|
const base = { dir, logged: 0, answered: 0, errored: 0 };
|
|
17698
17935
|
if (opts.config?.enabled === false) {
|
|
17699
17936
|
opts.log("[watchdog] off ([watchdog] enabled = false)");
|
|
@@ -17784,7 +18021,7 @@ async function flushTelemetry(timeoutMs = 2e3) {
|
|
|
17784
18021
|
}
|
|
17785
18022
|
|
|
17786
18023
|
// src/core/node-runtime.ts
|
|
17787
|
-
var
|
|
18024
|
+
var import_node_child_process7 = require("child_process");
|
|
17788
18025
|
var MIN_DAEMON_NODE_MAJOR = 22;
|
|
17789
18026
|
function nodeMajor(version) {
|
|
17790
18027
|
const m = String(version ?? "").trim().replace(/^v/, "").match(/^(\d+)\./);
|
|
@@ -17800,24 +18037,38 @@ function daemonNodeProblem(version) {
|
|
|
17800
18037
|
// src/daemon/index.ts
|
|
17801
18038
|
function readVersion2() {
|
|
17802
18039
|
try {
|
|
17803
|
-
const pkg = JSON.parse((0,
|
|
18040
|
+
const pkg = JSON.parse((0, import_node_fs25.readFileSync)((0, import_node_path19.join)(__dirname, "..", "package.json"), "utf-8"));
|
|
17804
18041
|
return pkg.version || "0.0.0";
|
|
17805
18042
|
} catch {
|
|
17806
18043
|
return "0.0.0";
|
|
17807
18044
|
}
|
|
17808
18045
|
}
|
|
17809
18046
|
async function runHardeningChecks() {
|
|
17810
|
-
const { stdout } = await (0, import_node_util.promisify)(
|
|
18047
|
+
const { stdout } = await (0, import_node_util.promisify)(import_node_child_process8.execFile)(
|
|
17811
18048
|
process.execPath,
|
|
17812
|
-
[(0,
|
|
18049
|
+
[(0, import_node_path19.join)(__dirname, "index.js"), "harden", "--json", "--no-upload"],
|
|
17813
18050
|
{ timeout: 12e4, maxBuffer: 4 * 1024 * 1024 }
|
|
17814
18051
|
);
|
|
17815
18052
|
const parsed = JSON.parse(stdout.slice(stdout.indexOf("{")));
|
|
17816
|
-
|
|
18053
|
+
const findings = Array.isArray(parsed.findings) ? parsed.findings : [];
|
|
18054
|
+
const patches = await runPatchCheck().catch((err) => {
|
|
18055
|
+
logLine(`[cloud] patch check failed: ${err.message.split("\n")[0]}`);
|
|
18056
|
+
return null;
|
|
18057
|
+
});
|
|
18058
|
+
return patches ? [...findings, patches] : findings;
|
|
18059
|
+
}
|
|
18060
|
+
async function runPatchCheck() {
|
|
18061
|
+
const { stdout } = await (0, import_node_util.promisify)(import_node_child_process8.execFile)(
|
|
18062
|
+
process.execPath,
|
|
18063
|
+
[(0, import_node_path19.join)(__dirname, "index.js"), "patches", "--json", "--no-upload"],
|
|
18064
|
+
{ timeout: 3e5, maxBuffer: 16 * 1024 * 1024 }
|
|
18065
|
+
);
|
|
18066
|
+
const parsed = JSON.parse(stdout.slice(stdout.indexOf("{")));
|
|
18067
|
+
return parsed.finding ?? null;
|
|
17817
18068
|
}
|
|
17818
18069
|
function logLine(line) {
|
|
17819
18070
|
try {
|
|
17820
|
-
(0,
|
|
18071
|
+
(0, import_node_fs25.appendFileSync)(PATHS.logFile, `${(/* @__PURE__ */ new Date()).toISOString()} ${line}
|
|
17821
18072
|
`);
|
|
17822
18073
|
} catch {
|
|
17823
18074
|
}
|
|
@@ -17842,9 +18093,9 @@ async function runDaemon() {
|
|
|
17842
18093
|
logLine(`[daemon] starting threatcrushd v${version} mode=${PATHS.mode}`);
|
|
17843
18094
|
const restartDaemon = () => {
|
|
17844
18095
|
if (PATHS.mode === "system") {
|
|
17845
|
-
(0,
|
|
18096
|
+
(0, import_node_child_process8.spawn)("systemctl", ["restart", "threatcrushd"], { detached: true, stdio: "ignore" }).unref();
|
|
17846
18097
|
} else {
|
|
17847
|
-
(0,
|
|
18098
|
+
(0, import_node_child_process8.spawn)(process.execPath, process.argv.slice(1), { detached: true, stdio: "ignore" }).unref();
|
|
17848
18099
|
setTimeout(() => process.exit(0), 500).unref();
|
|
17849
18100
|
}
|
|
17850
18101
|
};
|
|
@@ -17859,7 +18110,7 @@ async function runDaemon() {
|
|
|
17859
18110
|
} catch (err) {
|
|
17860
18111
|
logLine(`[daemon] state db unavailable: ${err.message}`);
|
|
17861
18112
|
}
|
|
17862
|
-
const config = loadConfig((0,
|
|
18113
|
+
const config = loadConfig((0, import_node_fs25.existsSync)(PATHS.configFile) ? PATHS.configFile : void 0);
|
|
17863
18114
|
configureAttackDetection(config.detection);
|
|
17864
18115
|
const retentionSeconds = parseDuration(config.storage?.event_retention) ?? DEFAULT_EVENT_RETENTION_SECONDS;
|
|
17865
18116
|
const maxEvents = config.storage?.max_events ?? DEFAULT_MAX_EVENTS;
|
|
@@ -17938,7 +18189,7 @@ async function runDaemon() {
|
|
|
17938
18189
|
version,
|
|
17939
18190
|
hostname: (0, import_node_os5.hostname)(),
|
|
17940
18191
|
minSeverity,
|
|
17941
|
-
spoolPath: (0,
|
|
18192
|
+
spoolPath: (0, import_node_path19.join)(PATHS.stateDir, "cloud-spool.jsonl"),
|
|
17942
18193
|
log: logLine,
|
|
17943
18194
|
ruleModules: new Set(ruleEngine.getRules().flatMap((rule) => rule.source_types)),
|
|
17944
18195
|
runHardening: runHardeningChecks
|
|
@@ -17952,6 +18203,16 @@ async function runDaemon() {
|
|
|
17952
18203
|
logLine(link ? `[cloud] reporting as server ${link.serverId} (org ${link.orgId}), detections >= ${minSeverity}` : "[cloud] not linked; run `threatcrush login` and `threatcrush servers link` to report to the dashboard");
|
|
17953
18204
|
}
|
|
17954
18205
|
const watchdog = startWatchdog({ bus, config: config.watchdog, stateDir: PATHS.stateDir, log: logLine });
|
|
18206
|
+
const autoUpdate = new AutoUpdate({
|
|
18207
|
+
version,
|
|
18208
|
+
settings: updateSettings(config.update),
|
|
18209
|
+
pkgRoot: (0, import_node_path19.join)(__dirname, ".."),
|
|
18210
|
+
statePath: (0, import_node_path19.join)(PATHS.stateDir, "auto-update.json"),
|
|
18211
|
+
bus,
|
|
18212
|
+
log: logLine,
|
|
18213
|
+
restartDaemon
|
|
18214
|
+
});
|
|
18215
|
+
autoUpdate.start();
|
|
17955
18216
|
const ipc = new IpcServer(version, moduleHost, remediation, () => watchdog.status());
|
|
17956
18217
|
await ipc.start();
|
|
17957
18218
|
logLine(`[daemon] ipc listening on ${PATHS.socket}`);
|
|
@@ -17985,6 +18246,10 @@ async function runDaemon() {
|
|
|
17985
18246
|
watchdog.stop();
|
|
17986
18247
|
} catch {
|
|
17987
18248
|
}
|
|
18249
|
+
try {
|
|
18250
|
+
autoUpdate.stop();
|
|
18251
|
+
} catch {
|
|
18252
|
+
}
|
|
17988
18253
|
try {
|
|
17989
18254
|
await ipc.stop();
|
|
17990
18255
|
} catch {
|