@profullstack/threatcrush 0.13.34 → 0.13.36
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 +525 -74
- package/dist/daemon.js.map +1 -1
- package/dist/index.js +701 -172
- 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_fs27 = require("fs");
|
|
2026
|
+
var import_node_child_process10 = require("child_process");
|
|
2027
2027
|
var import_node_os5 = require("os");
|
|
2028
|
-
var
|
|
2028
|
+
var import_node_path21 = require("path");
|
|
2029
2029
|
var import_node_util = require("util");
|
|
2030
2030
|
|
|
2031
2031
|
// src/daemon/paths.ts
|
|
@@ -10861,6 +10861,7 @@ function loadConfig(configPath) {
|
|
|
10861
10861
|
watchdog: parsed.watchdog,
|
|
10862
10862
|
update: parsed.update,
|
|
10863
10863
|
threat_feeds: parsed.threat_feeds,
|
|
10864
|
+
privileged: parsed.privileged,
|
|
10864
10865
|
storage: parsed.storage,
|
|
10865
10866
|
license: parsed.license
|
|
10866
10867
|
};
|
|
@@ -16255,13 +16256,13 @@ var AllowlistSync = class {
|
|
|
16255
16256
|
const value = String(entry.value ?? "").trim();
|
|
16256
16257
|
if (isAddressOrCidr(value)) wanted.add(value);
|
|
16257
16258
|
}
|
|
16258
|
-
const
|
|
16259
|
+
const added2 = [];
|
|
16259
16260
|
const protectedNow = new Set(this.manager.getProtected());
|
|
16260
16261
|
for (const value of wanted) {
|
|
16261
16262
|
if (this.owned.has(value) || protectedNow.has(value)) continue;
|
|
16262
16263
|
this.manager.addToAllowlist(value);
|
|
16263
16264
|
this.owned.add(value);
|
|
16264
|
-
|
|
16265
|
+
added2.push(value);
|
|
16265
16266
|
}
|
|
16266
16267
|
const removed = [];
|
|
16267
16268
|
for (const value of this.owned) {
|
|
@@ -16270,9 +16271,9 @@ var AllowlistSync = class {
|
|
|
16270
16271
|
this.owned.delete(value);
|
|
16271
16272
|
removed.push(value);
|
|
16272
16273
|
}
|
|
16273
|
-
if (
|
|
16274
|
+
if (added2.length > 0 || removed.length > 0) {
|
|
16274
16275
|
this.opts.log(
|
|
16275
|
-
`[cloud] org allowlist: +${
|
|
16276
|
+
`[cloud] org allowlist: +${added2.length} -${removed.length} (${[...added2.map((v) => `+${v}`), ...removed.map((v) => `-${v}`)].slice(0, 10).join(" ")})`
|
|
16276
16277
|
);
|
|
16277
16278
|
}
|
|
16278
16279
|
}
|
|
@@ -16864,6 +16865,443 @@ var ThreatFeeds = class {
|
|
|
16864
16865
|
}
|
|
16865
16866
|
};
|
|
16866
16867
|
|
|
16868
|
+
// src/daemon/workers/privilege-watch.ts
|
|
16869
|
+
var import_node_fs21 = require("fs");
|
|
16870
|
+
var import_node_path16 = require("path");
|
|
16871
|
+
var import_node_child_process7 = require("child_process");
|
|
16872
|
+
|
|
16873
|
+
// src/core/privileges/index.ts
|
|
16874
|
+
var import_node_crypto5 = require("crypto");
|
|
16875
|
+
var ADMIN_GROUPS = ["sudo", "wheel", "admin"];
|
|
16876
|
+
function parseUsers(text) {
|
|
16877
|
+
const out = [];
|
|
16878
|
+
for (const line of text.split("\n")) {
|
|
16879
|
+
const f = line.split(":");
|
|
16880
|
+
if (f.length < 7 || line.startsWith("#")) continue;
|
|
16881
|
+
const uid = Number(f[2]);
|
|
16882
|
+
if (!f[0] || !Number.isInteger(uid)) continue;
|
|
16883
|
+
out.push({ name: f[0], uid, home: f[5], shell: f[6].trim() });
|
|
16884
|
+
}
|
|
16885
|
+
return out;
|
|
16886
|
+
}
|
|
16887
|
+
function parseAdminGroups(text) {
|
|
16888
|
+
const out = {};
|
|
16889
|
+
for (const line of text.split("\n")) {
|
|
16890
|
+
const f = line.split(":");
|
|
16891
|
+
if (f.length < 4 || !ADMIN_GROUPS.includes(f[0])) continue;
|
|
16892
|
+
out[f[0]] = f[3].trim().split(",").map((m) => m.trim()).filter(Boolean).sort();
|
|
16893
|
+
}
|
|
16894
|
+
return out;
|
|
16895
|
+
}
|
|
16896
|
+
function canLogIn(user) {
|
|
16897
|
+
return !/(nologin|\/false|\/sync|\/shutdown|\/halt)$/.test(user.shell);
|
|
16898
|
+
}
|
|
16899
|
+
function sudoersRules(text) {
|
|
16900
|
+
const joined = text.replace(/\\\n/g, " ");
|
|
16901
|
+
const rules = [];
|
|
16902
|
+
for (const raw of joined.split("\n")) {
|
|
16903
|
+
const line = raw.trim();
|
|
16904
|
+
if (!line) continue;
|
|
16905
|
+
if (line.startsWith("#") && !/^#include(dir)?\s/.test(line)) continue;
|
|
16906
|
+
rules.push(line.replace(/\s+/g, " "));
|
|
16907
|
+
}
|
|
16908
|
+
return rules;
|
|
16909
|
+
}
|
|
16910
|
+
function sudoReadsIncluded(name) {
|
|
16911
|
+
return !name.endsWith("~") && !name.includes(".");
|
|
16912
|
+
}
|
|
16913
|
+
function keyFingerprint(blobBase64) {
|
|
16914
|
+
if (!/^[A-Za-z0-9+/]+=*$/.test(blobBase64)) return null;
|
|
16915
|
+
const digest = (0, import_node_crypto5.createHash)("sha256").update(Buffer.from(blobBase64, "base64")).digest("base64");
|
|
16916
|
+
return `SHA256:${digest.replace(/=+$/, "")}`;
|
|
16917
|
+
}
|
|
16918
|
+
function parseAuthorizedKeys(text) {
|
|
16919
|
+
const out = [];
|
|
16920
|
+
for (const raw of text.split("\n")) {
|
|
16921
|
+
const line = raw.trim();
|
|
16922
|
+
if (!line || line.startsWith("#")) continue;
|
|
16923
|
+
const m = /(?:^|\s)((?:ssh|ecdsa|sk)-[\w@.-]+)\s+([A-Za-z0-9+/]+=*)(?:\s+(.*))?$/.exec(line);
|
|
16924
|
+
if (!m) continue;
|
|
16925
|
+
const fingerprint = keyFingerprint(m[2]);
|
|
16926
|
+
if (fingerprint) out.push({ fingerprint, type: m[1], comment: (m[3] ?? "").trim() });
|
|
16927
|
+
}
|
|
16928
|
+
return out;
|
|
16929
|
+
}
|
|
16930
|
+
function takeSnapshot(io, now = /* @__PURE__ */ new Date()) {
|
|
16931
|
+
const unreadable = [];
|
|
16932
|
+
const read = (path) => {
|
|
16933
|
+
const text = io.readFile(path);
|
|
16934
|
+
if (text === null && io.exists(path)) unreadable.push(path);
|
|
16935
|
+
return text;
|
|
16936
|
+
};
|
|
16937
|
+
const users = parseUsers(read("/etc/passwd") ?? "");
|
|
16938
|
+
const snapshot = {
|
|
16939
|
+
taken_at: now.toISOString(),
|
|
16940
|
+
uid0: users.filter((a) => a.uid === 0).map((a) => a.name).sort(),
|
|
16941
|
+
admin_groups: parseAdminGroups(read("/etc/group") ?? ""),
|
|
16942
|
+
sudoers: {},
|
|
16943
|
+
keys: {},
|
|
16944
|
+
unreadable
|
|
16945
|
+
};
|
|
16946
|
+
const sudoersFiles = ["/etc/sudoers"];
|
|
16947
|
+
for (const name of (io.readDir("/etc/sudoers.d") ?? []).sort()) {
|
|
16948
|
+
if (sudoReadsIncluded(name)) sudoersFiles.push(`/etc/sudoers.d/${name}`);
|
|
16949
|
+
}
|
|
16950
|
+
for (const path of sudoersFiles) {
|
|
16951
|
+
const text = read(path);
|
|
16952
|
+
if (text !== null) snapshot.sudoers[path] = sudoersRules(text);
|
|
16953
|
+
}
|
|
16954
|
+
for (const user of users) {
|
|
16955
|
+
if (user.uid !== 0 && (!canLogIn(user) || !user.home || user.home === "/")) continue;
|
|
16956
|
+
const keys = {};
|
|
16957
|
+
for (const file of ["authorized_keys", "authorized_keys2"]) {
|
|
16958
|
+
const text = read(`${user.home.replace(/\/$/, "")}/.ssh/${file}`);
|
|
16959
|
+
for (const k of parseAuthorizedKeys(text ?? "")) keys[k.fingerprint] = { type: k.type, comment: k.comment };
|
|
16960
|
+
}
|
|
16961
|
+
if (Object.keys(keys).length > 0) snapshot.keys[user.name] = keys;
|
|
16962
|
+
}
|
|
16963
|
+
return snapshot;
|
|
16964
|
+
}
|
|
16965
|
+
var added = (before, after) => after.filter((x) => !before.includes(x));
|
|
16966
|
+
function diffSnapshots(prev, next) {
|
|
16967
|
+
const changes = [];
|
|
16968
|
+
for (const name of added(prev.uid0, next.uid0)) {
|
|
16969
|
+
changes.push({ severity: "critical", message: `New uid 0 account: ${name}`, details: { rule_id: "priv-uid0-added", account: name } });
|
|
16970
|
+
}
|
|
16971
|
+
for (const name of added(next.uid0, prev.uid0)) {
|
|
16972
|
+
changes.push({ severity: "low", message: `uid 0 account removed: ${name}`, details: { rule_id: "priv-uid0-removed", account: name } });
|
|
16973
|
+
}
|
|
16974
|
+
for (const group of /* @__PURE__ */ new Set([...Object.keys(prev.admin_groups), ...Object.keys(next.admin_groups)])) {
|
|
16975
|
+
const before = prev.admin_groups[group] ?? [];
|
|
16976
|
+
const after = next.admin_groups[group] ?? [];
|
|
16977
|
+
for (const m of added(before, after)) {
|
|
16978
|
+
changes.push({ severity: "high", message: `${m} added to the ${group} group (can run anything as root)`, details: { rule_id: "priv-admin-group-added", group, account: m } });
|
|
16979
|
+
}
|
|
16980
|
+
for (const m of added(after, before)) {
|
|
16981
|
+
changes.push({ severity: "low", message: `${m} removed from the ${group} group`, details: { rule_id: "priv-admin-group-removed", group, account: m } });
|
|
16982
|
+
}
|
|
16983
|
+
}
|
|
16984
|
+
for (const path of /* @__PURE__ */ new Set([...Object.keys(prev.sudoers), ...Object.keys(next.sudoers)])) {
|
|
16985
|
+
if (next.unreadable.includes(path) || prev.unreadable.includes(path)) continue;
|
|
16986
|
+
const before = prev.sudoers[path];
|
|
16987
|
+
const after = next.sudoers[path];
|
|
16988
|
+
if (!before && after) {
|
|
16989
|
+
changes.push({ severity: "high", message: `New sudoers file ${path}${after.length ? `: ${after.slice(0, 3).join(" | ")}` : ""}`, details: { rule_id: "priv-sudoers-added", path, rules: after.slice(0, 20) } });
|
|
16990
|
+
continue;
|
|
16991
|
+
}
|
|
16992
|
+
if (before && !after) {
|
|
16993
|
+
changes.push({ severity: "low", message: `Sudoers file removed: ${path}`, details: { rule_id: "priv-sudoers-removed", path } });
|
|
16994
|
+
continue;
|
|
16995
|
+
}
|
|
16996
|
+
const plus = added(before ?? [], after ?? []);
|
|
16997
|
+
const minus = added(after ?? [], before ?? []);
|
|
16998
|
+
if (plus.length) {
|
|
16999
|
+
changes.push({ severity: "high", message: `Sudoers rule added in ${path}: ${plus.slice(0, 3).join(" | ")}`, details: { rule_id: "priv-sudoers-changed", path, added: plus.slice(0, 20), removed: minus.slice(0, 20) } });
|
|
17000
|
+
} else if (minus.length) {
|
|
17001
|
+
changes.push({ severity: "low", message: `Sudoers rule removed in ${path}: ${minus.slice(0, 3).join(" | ")}`, details: { rule_id: "priv-sudoers-changed", path, removed: minus.slice(0, 20) } });
|
|
17002
|
+
}
|
|
17003
|
+
}
|
|
17004
|
+
for (const user of /* @__PURE__ */ new Set([...Object.keys(prev.keys), ...Object.keys(next.keys)])) {
|
|
17005
|
+
const before = prev.keys[user] ?? {};
|
|
17006
|
+
const after = next.keys[user] ?? {};
|
|
17007
|
+
for (const fp of Object.keys(after)) {
|
|
17008
|
+
if (before[fp]) continue;
|
|
17009
|
+
const { type, comment } = after[fp];
|
|
17010
|
+
changes.push({
|
|
17011
|
+
severity: "high",
|
|
17012
|
+
message: `New SSH key for ${user}: ${type} ${fp}${comment ? ` (${comment})` : ""}`,
|
|
17013
|
+
details: { rule_id: "priv-authorized-key-added", account: user, fingerprint: fp, key_type: type, comment }
|
|
17014
|
+
});
|
|
17015
|
+
}
|
|
17016
|
+
for (const fp of Object.keys(before)) {
|
|
17017
|
+
if (after[fp]) continue;
|
|
17018
|
+
changes.push({ severity: "low", message: `SSH key removed for ${user}: ${fp}`, details: { rule_id: "priv-authorized-key-removed", account: user, fingerprint: fp } });
|
|
17019
|
+
}
|
|
17020
|
+
}
|
|
17021
|
+
const rank = { info: 0, low: 1, medium: 2, high: 3, critical: 4 };
|
|
17022
|
+
return changes.sort((a, b) => rank[b.severity] - rank[a.severity]);
|
|
17023
|
+
}
|
|
17024
|
+
function parseSudoMessage(message) {
|
|
17025
|
+
const m = /^\s*(\S+)\s+:\s+(.*)$/.exec(message);
|
|
17026
|
+
if (!m) return null;
|
|
17027
|
+
const fields = {};
|
|
17028
|
+
let reason = "";
|
|
17029
|
+
for (const part of m[2].split(" ; ")) {
|
|
17030
|
+
const kv = /^([A-Z]+)=(.*)$/.exec(part.trim());
|
|
17031
|
+
if (kv) fields[kv[1]] = kv[2];
|
|
17032
|
+
else if (!reason) reason = part.trim();
|
|
17033
|
+
}
|
|
17034
|
+
if (!fields.COMMAND) return null;
|
|
17035
|
+
if (/incorrect password|authentication failure/i.test(reason)) return null;
|
|
17036
|
+
return {
|
|
17037
|
+
user: m[1],
|
|
17038
|
+
runas: fields.USER ?? "root",
|
|
17039
|
+
command: fields.COMMAND,
|
|
17040
|
+
tty: fields.TTY,
|
|
17041
|
+
pwd: fields.PWD,
|
|
17042
|
+
denied: /not allowed|not in the sudoers|NOT in sudoers|command not allowed/i.test(reason)
|
|
17043
|
+
};
|
|
17044
|
+
}
|
|
17045
|
+
|
|
17046
|
+
// src/daemon/workers/privilege-watch.ts
|
|
17047
|
+
function privilegeSettings(section) {
|
|
17048
|
+
return { enabled: section?.enabled !== false, geo: section?.geo !== false, pollMs: 6e4 };
|
|
17049
|
+
}
|
|
17050
|
+
var COUNTRY_NAMES = new Intl.DisplayNames(["en"], { type: "region" });
|
|
17051
|
+
function countryName(code) {
|
|
17052
|
+
try {
|
|
17053
|
+
return COUNTRY_NAMES.of(code) ?? code;
|
|
17054
|
+
} catch {
|
|
17055
|
+
return code;
|
|
17056
|
+
}
|
|
17057
|
+
}
|
|
17058
|
+
var fsIO = {
|
|
17059
|
+
readFile: (p) => {
|
|
17060
|
+
try {
|
|
17061
|
+
return (0, import_node_fs21.readFileSync)(p, "utf-8");
|
|
17062
|
+
} catch {
|
|
17063
|
+
return null;
|
|
17064
|
+
}
|
|
17065
|
+
},
|
|
17066
|
+
readDir: (p) => {
|
|
17067
|
+
try {
|
|
17068
|
+
return (0, import_node_fs21.readdirSync)(p);
|
|
17069
|
+
} catch {
|
|
17070
|
+
return null;
|
|
17071
|
+
}
|
|
17072
|
+
},
|
|
17073
|
+
exists: (p) => (0, import_node_fs21.existsSync)(p)
|
|
17074
|
+
};
|
|
17075
|
+
function readJson(path) {
|
|
17076
|
+
try {
|
|
17077
|
+
return JSON.parse((0, import_node_fs21.readFileSync)(path, "utf-8"));
|
|
17078
|
+
} catch {
|
|
17079
|
+
return null;
|
|
17080
|
+
}
|
|
17081
|
+
}
|
|
17082
|
+
function writeJson(path, value) {
|
|
17083
|
+
(0, import_node_fs21.mkdirSync)((0, import_node_path16.dirname)(path), { recursive: true });
|
|
17084
|
+
(0, import_node_fs21.writeFileSync)(`${path}.tmp`, JSON.stringify(value));
|
|
17085
|
+
(0, import_node_fs21.renameSync)(`${path}.tmp`, path);
|
|
17086
|
+
}
|
|
17087
|
+
function defaultRun3(cmd, args) {
|
|
17088
|
+
const res = (0, import_node_child_process7.spawnSync)(cmd, args, { encoding: "utf-8", timeout: 2e4, maxBuffer: 16 * 1024 * 1024, stdio: ["ignore", "pipe", "pipe"] });
|
|
17089
|
+
return res.status === 0 ? res.stdout : null;
|
|
17090
|
+
}
|
|
17091
|
+
async function geolocateViaThreatcrush(ip, fetchImpl = fetch) {
|
|
17092
|
+
const res = await fetchImpl("https://threatcrush.com/api/geo", {
|
|
17093
|
+
method: "POST",
|
|
17094
|
+
headers: { "Content-Type": "application/json" },
|
|
17095
|
+
body: JSON.stringify({ ips: [ip] }),
|
|
17096
|
+
signal: AbortSignal.timeout(8e3)
|
|
17097
|
+
});
|
|
17098
|
+
if (!res.ok) return null;
|
|
17099
|
+
const body = await res.json();
|
|
17100
|
+
const code = body.results?.[ip]?.countryCode;
|
|
17101
|
+
return typeof code === "string" && /^[A-Z]{2}$/.test(code) ? { countryCode: code } : null;
|
|
17102
|
+
}
|
|
17103
|
+
async function geolocateManyViaThreatcrush(ips, fetchImpl = fetch) {
|
|
17104
|
+
const out = /* @__PURE__ */ new Map();
|
|
17105
|
+
for (let i = 0; i < ips.length; i += 200) {
|
|
17106
|
+
const chunk = ips.slice(i, i + 200);
|
|
17107
|
+
const res = await fetchImpl("https://threatcrush.com/api/geo", {
|
|
17108
|
+
method: "POST",
|
|
17109
|
+
headers: { "Content-Type": "application/json" },
|
|
17110
|
+
body: JSON.stringify({ ips: chunk }),
|
|
17111
|
+
signal: AbortSignal.timeout(15e3)
|
|
17112
|
+
});
|
|
17113
|
+
if (!res.ok) throw new Error(`geo lookup failed: HTTP ${res.status}`);
|
|
17114
|
+
const body = await res.json();
|
|
17115
|
+
for (const ip of chunk) {
|
|
17116
|
+
const code = body.results?.[ip]?.countryCode;
|
|
17117
|
+
if (typeof code === "string" && /^[A-Z]{2}$/.test(code)) out.set(ip, code);
|
|
17118
|
+
}
|
|
17119
|
+
}
|
|
17120
|
+
return out;
|
|
17121
|
+
}
|
|
17122
|
+
function parseAcceptedLogins(text) {
|
|
17123
|
+
const out = /* @__PURE__ */ new Map();
|
|
17124
|
+
for (const m of text.matchAll(/Accepted \S+ for (\S+) from (\S+) port/g)) {
|
|
17125
|
+
const [, user, ip] = m;
|
|
17126
|
+
if (isProtected(ip, DEFAULT_PROTECTED)) continue;
|
|
17127
|
+
const ips = out.get(user) ?? /* @__PURE__ */ new Set();
|
|
17128
|
+
ips.add(ip);
|
|
17129
|
+
out.set(user, ips);
|
|
17130
|
+
}
|
|
17131
|
+
return out;
|
|
17132
|
+
}
|
|
17133
|
+
var PrivilegeWatch = class {
|
|
17134
|
+
constructor(opts) {
|
|
17135
|
+
this.opts = opts;
|
|
17136
|
+
this.io = opts.io ?? fsIO;
|
|
17137
|
+
this.run = opts.run ?? defaultRun3;
|
|
17138
|
+
this.now = opts.now ?? (() => /* @__PURE__ */ new Date());
|
|
17139
|
+
}
|
|
17140
|
+
opts;
|
|
17141
|
+
timers = [];
|
|
17142
|
+
io;
|
|
17143
|
+
run;
|
|
17144
|
+
now;
|
|
17145
|
+
warnedUnreadable = false;
|
|
17146
|
+
onEvent = (event) => {
|
|
17147
|
+
void this.checkLogin(event);
|
|
17148
|
+
};
|
|
17149
|
+
start() {
|
|
17150
|
+
if (!this.opts.settings.enabled) {
|
|
17151
|
+
this.opts.log("[privileged] disabled by [privileged] enabled = false");
|
|
17152
|
+
return;
|
|
17153
|
+
}
|
|
17154
|
+
this.checkSnapshot();
|
|
17155
|
+
this.readSudoLedger();
|
|
17156
|
+
if (this.opts.settings.geo) {
|
|
17157
|
+
this.opts.bus.on("event", this.onEvent);
|
|
17158
|
+
void this.backfillLogins();
|
|
17159
|
+
}
|
|
17160
|
+
const timer = setInterval(() => {
|
|
17161
|
+
this.checkSnapshot();
|
|
17162
|
+
this.readSudoLedger();
|
|
17163
|
+
}, this.opts.settings.pollMs);
|
|
17164
|
+
timer.unref?.();
|
|
17165
|
+
this.timers.push(timer);
|
|
17166
|
+
}
|
|
17167
|
+
stop() {
|
|
17168
|
+
for (const t of this.timers) clearInterval(t);
|
|
17169
|
+
this.timers = [];
|
|
17170
|
+
this.opts.bus.off("event", this.onEvent);
|
|
17171
|
+
}
|
|
17172
|
+
emit(severity, message, details) {
|
|
17173
|
+
const event = { timestamp: this.now(), module: "privileged-access", category: "auth", severity, message, details };
|
|
17174
|
+
try {
|
|
17175
|
+
insertEvent(event);
|
|
17176
|
+
} catch {
|
|
17177
|
+
}
|
|
17178
|
+
this.opts.bus.publish(event);
|
|
17179
|
+
}
|
|
17180
|
+
checkSnapshot() {
|
|
17181
|
+
const next = takeSnapshot(this.io, this.now());
|
|
17182
|
+
if (next.unreadable.length && !this.warnedUnreadable) {
|
|
17183
|
+
this.warnedUnreadable = true;
|
|
17184
|
+
this.opts.log(`[privileged] cannot read ${next.unreadable.join(", ")}; run the daemon as root to watch them`);
|
|
17185
|
+
}
|
|
17186
|
+
const prev = readJson(this.opts.snapshotPath);
|
|
17187
|
+
if (!prev) {
|
|
17188
|
+
const keys = Object.values(next.keys).reduce((n, k) => n + Object.keys(k).length, 0);
|
|
17189
|
+
this.opts.log(`[privileged] baseline recorded: uid0 [${next.uid0.join(", ")}], ${Object.values(next.admin_groups).flat().length} admin-group member(s), ${Object.keys(next.sudoers).length} sudoers file(s), ${keys} authorized key(s)`);
|
|
17190
|
+
} else {
|
|
17191
|
+
for (const change of diffSnapshots(prev, next)) this.emit(change.severity, change.message, change.details);
|
|
17192
|
+
}
|
|
17193
|
+
try {
|
|
17194
|
+
writeJson(this.opts.snapshotPath, next);
|
|
17195
|
+
} catch (err) {
|
|
17196
|
+
this.opts.log(`[privileged] could not save snapshot: ${err.message}`);
|
|
17197
|
+
}
|
|
17198
|
+
}
|
|
17199
|
+
/**
|
|
17200
|
+
* New sudo records since the saved journal cursor. The first run only finds
|
|
17201
|
+
* the current end of the journal, so history from before the daemon is not
|
|
17202
|
+
* replayed as if it had just happened.
|
|
17203
|
+
*/
|
|
17204
|
+
readSudoLedger() {
|
|
17205
|
+
const cursor = readJson(this.opts.cursorPath)?.cursor;
|
|
17206
|
+
const args = cursor ? ["-t", "sudo", "-o", "json", "--no-pager", `--after-cursor=${cursor}`] : ["-o", "json", "--no-pager", "-n", "0", "--show-cursor"];
|
|
17207
|
+
const out = this.run("journalctl", args);
|
|
17208
|
+
if (out === null) return;
|
|
17209
|
+
let last = cursor ?? /^-- cursor: (\S+)/m.exec(out)?.[1];
|
|
17210
|
+
for (const line of cursor ? out.split("\n") : []) {
|
|
17211
|
+
if (!line.trim()) continue;
|
|
17212
|
+
let rec;
|
|
17213
|
+
try {
|
|
17214
|
+
rec = JSON.parse(line);
|
|
17215
|
+
} catch {
|
|
17216
|
+
continue;
|
|
17217
|
+
}
|
|
17218
|
+
if (typeof rec.__CURSOR === "string") last = rec.__CURSOR;
|
|
17219
|
+
const entry = typeof rec.MESSAGE === "string" ? parseSudoMessage(rec.MESSAGE) : null;
|
|
17220
|
+
if (!entry) continue;
|
|
17221
|
+
this.emit(
|
|
17222
|
+
entry.denied ? "medium" : "info",
|
|
17223
|
+
entry.denied ? `sudo denied: ${entry.user} tried ${entry.command} as ${entry.runas}` : `sudo: ${entry.user} ran ${entry.command} as ${entry.runas}`,
|
|
17224
|
+
{ rule_id: entry.denied ? "priv-sudo-denied" : "priv-sudo-command", account: entry.user, runas: entry.runas, command: entry.command, tty: entry.tty, pwd: entry.pwd }
|
|
17225
|
+
);
|
|
17226
|
+
}
|
|
17227
|
+
if (last && last !== cursor) {
|
|
17228
|
+
try {
|
|
17229
|
+
writeJson(this.opts.cursorPath, { cursor: last });
|
|
17230
|
+
} catch {
|
|
17231
|
+
}
|
|
17232
|
+
}
|
|
17233
|
+
}
|
|
17234
|
+
/**
|
|
17235
|
+
* Seed the login history from the last 30 days of sshd logins, once, when
|
|
17236
|
+
* there is none. Learning only from logins after install meant regular
|
|
17237
|
+
* automation from another country (a backup box pulling every four hours)
|
|
17238
|
+
* raised a "new country" alert on its first run; on dev2 it did, at once.
|
|
17239
|
+
*/
|
|
17240
|
+
async backfillLogins() {
|
|
17241
|
+
if ((0, import_node_fs21.existsSync)(this.opts.loginsPath)) return;
|
|
17242
|
+
const out = this.run("journalctl", ["_COMM=sshd", "_COMM=sshd-session", "--since=-30d", "-o", "cat", "--no-pager"]);
|
|
17243
|
+
const byUser = parseAcceptedLogins(out ?? "");
|
|
17244
|
+
const ips = [...new Set([...byUser.values()].flatMap((s) => [...s]))];
|
|
17245
|
+
let codes = /* @__PURE__ */ new Map();
|
|
17246
|
+
if (ips.length > 0) {
|
|
17247
|
+
try {
|
|
17248
|
+
codes = await (this.opts.geolocateMany ?? geolocateManyViaThreatcrush)(ips);
|
|
17249
|
+
} catch (err) {
|
|
17250
|
+
this.opts.log(`[privileged] login backfill geolocation failed: ${err.message}`);
|
|
17251
|
+
return;
|
|
17252
|
+
}
|
|
17253
|
+
}
|
|
17254
|
+
if ((0, import_node_fs21.existsSync)(this.opts.loginsPath)) return;
|
|
17255
|
+
const at = this.now().toISOString();
|
|
17256
|
+
const history = {};
|
|
17257
|
+
for (const [user, userIps] of byUser) {
|
|
17258
|
+
for (const ip of userIps) {
|
|
17259
|
+
const code = codes.get(ip);
|
|
17260
|
+
if (code) (history[user] ??= {})[code] = { first: at, last: at };
|
|
17261
|
+
}
|
|
17262
|
+
}
|
|
17263
|
+
try {
|
|
17264
|
+
writeJson(this.opts.loginsPath, history);
|
|
17265
|
+
} catch {
|
|
17266
|
+
}
|
|
17267
|
+
const summary = Object.entries(history).map(([u, c]) => `${u}: ${Object.keys(c).join("/")}`).join(", ");
|
|
17268
|
+
this.opts.log(`[privileged] login baseline from the last 30 days: ${summary || "no public SSH logins"}`);
|
|
17269
|
+
}
|
|
17270
|
+
/** An accepted SSH login from a country new for that user. */
|
|
17271
|
+
async checkLogin(event) {
|
|
17272
|
+
if (event.module !== "ssh-guard" || !event.source_ip) return;
|
|
17273
|
+
const user = /SSH login accepted for (\S+)/.exec(event.message)?.[1];
|
|
17274
|
+
if (!user || user === "unknown" || isProtected(event.source_ip, DEFAULT_PROTECTED)) return;
|
|
17275
|
+
let geo = null;
|
|
17276
|
+
try {
|
|
17277
|
+
geo = await (this.opts.geolocate ?? geolocateViaThreatcrush)(event.source_ip);
|
|
17278
|
+
} catch (err) {
|
|
17279
|
+
this.opts.log(`[privileged] geolocation failed for ${event.source_ip}: ${err.message}`);
|
|
17280
|
+
}
|
|
17281
|
+
const country = geo?.countryCode;
|
|
17282
|
+
if (!country || !/^[A-Z]{2}$/.test(country)) return;
|
|
17283
|
+
const history = readJson(this.opts.loginsPath) ?? {};
|
|
17284
|
+
const seen = history[user] ?? {};
|
|
17285
|
+
const at = this.now().toISOString();
|
|
17286
|
+
const known = Object.keys(seen);
|
|
17287
|
+
if (known.length > 0 && !seen[country]) {
|
|
17288
|
+
this.emit("high", `SSH login for ${user} from a new country: ${countryName(country)}, ${event.source_ip}; seen before from ${known.join(", ")}`, {
|
|
17289
|
+
rule_id: "priv-login-new-country",
|
|
17290
|
+
account: user,
|
|
17291
|
+
login_ip: event.source_ip,
|
|
17292
|
+
country,
|
|
17293
|
+
known_countries: known
|
|
17294
|
+
});
|
|
17295
|
+
}
|
|
17296
|
+
seen[country] = { first: seen[country]?.first ?? at, last: at };
|
|
17297
|
+
history[user] = seen;
|
|
17298
|
+
try {
|
|
17299
|
+
writeJson(this.opts.loginsPath, history);
|
|
17300
|
+
} catch {
|
|
17301
|
+
}
|
|
17302
|
+
}
|
|
17303
|
+
};
|
|
17304
|
+
|
|
16867
17305
|
// src/daemon/rules/engine.ts
|
|
16868
17306
|
var MAX_WINDOW_ENTRIES = 1e3;
|
|
16869
17307
|
function live(window) {
|
|
@@ -17025,8 +17463,8 @@ var RuleEngine = class {
|
|
|
17025
17463
|
};
|
|
17026
17464
|
|
|
17027
17465
|
// src/daemon/rules/loader.ts
|
|
17028
|
-
var
|
|
17029
|
-
var
|
|
17466
|
+
var import_node_fs22 = require("fs");
|
|
17467
|
+
var import_node_path17 = require("path");
|
|
17030
17468
|
|
|
17031
17469
|
// src/daemon/rules/default-rules.ts
|
|
17032
17470
|
var AT_CRS_THRESHOLD = { field: "severity", operator: "regex", value: "^(high|critical)$" };
|
|
@@ -17427,11 +17865,11 @@ var RULES_DIR = "/etc/threatcrush/rules.d";
|
|
|
17427
17865
|
function loadAllRules(customDir) {
|
|
17428
17866
|
const rules = [...DEFAULT_RULES];
|
|
17429
17867
|
const dir = customDir || RULES_DIR;
|
|
17430
|
-
if ((0,
|
|
17431
|
-
const files = (0,
|
|
17868
|
+
if ((0, import_node_fs22.existsSync)(dir)) {
|
|
17869
|
+
const files = (0, import_node_fs22.readdirSync)(dir).filter((f) => f.endsWith(".json"));
|
|
17432
17870
|
for (const file of files) {
|
|
17433
17871
|
try {
|
|
17434
|
-
const raw = (0,
|
|
17872
|
+
const raw = (0, import_node_fs22.readFileSync)((0, import_node_path17.join)(dir, file), "utf-8");
|
|
17435
17873
|
const parsed = JSON.parse(raw);
|
|
17436
17874
|
const customRules = Array.isArray(parsed) ? parsed : [parsed];
|
|
17437
17875
|
for (const rule of customRules) {
|
|
@@ -17462,9 +17900,9 @@ function loadAllRules(customDir) {
|
|
|
17462
17900
|
}
|
|
17463
17901
|
|
|
17464
17902
|
// src/daemon/firewall/adapters.ts
|
|
17465
|
-
var
|
|
17466
|
-
var
|
|
17467
|
-
var
|
|
17903
|
+
var import_node_child_process8 = require("child_process");
|
|
17904
|
+
var import_node_fs23 = require("fs");
|
|
17905
|
+
var import_node_path18 = require("path");
|
|
17468
17906
|
var import_node_net8 = require("net");
|
|
17469
17907
|
function assertValidFirewallIp(ip) {
|
|
17470
17908
|
if ((0, import_node_net8.isIP)(ip) !== 4) {
|
|
@@ -17477,9 +17915,9 @@ function assertBannableIp(ip) {
|
|
|
17477
17915
|
}
|
|
17478
17916
|
}
|
|
17479
17917
|
function writeIfAbsent(path, contents) {
|
|
17480
|
-
(0,
|
|
17918
|
+
(0, import_node_fs23.mkdirSync)((0, import_node_path18.dirname)(path), { recursive: true });
|
|
17481
17919
|
try {
|
|
17482
|
-
(0,
|
|
17920
|
+
(0, import_node_fs23.writeFileSync)(path, contents, { flag: "wx" });
|
|
17483
17921
|
} catch (err) {
|
|
17484
17922
|
if (err.code !== "EEXIST") throw err;
|
|
17485
17923
|
}
|
|
@@ -17498,11 +17936,11 @@ var Fail2banAdapter = class {
|
|
|
17498
17936
|
enforces = true;
|
|
17499
17937
|
jailReady = false;
|
|
17500
17938
|
isAvailable() {
|
|
17501
|
-
const result = (0,
|
|
17939
|
+
const result = (0, import_node_child_process8.spawnSync)("fail2ban-client", ["ping"], { stdio: "pipe" });
|
|
17502
17940
|
return result.status === 0;
|
|
17503
17941
|
}
|
|
17504
17942
|
client(args) {
|
|
17505
|
-
return (0,
|
|
17943
|
+
return (0, import_node_child_process8.execFileSync)("fail2ban-client", args, {
|
|
17506
17944
|
encoding: "utf-8",
|
|
17507
17945
|
stdio: ["ignore", "pipe", "pipe"]
|
|
17508
17946
|
});
|
|
@@ -17598,35 +18036,35 @@ var NftablesAdapter = class {
|
|
|
17598
18036
|
table = "threatcrush";
|
|
17599
18037
|
set = "blocklist";
|
|
17600
18038
|
isAvailable() {
|
|
17601
|
-
const result = (0,
|
|
18039
|
+
const result = (0, import_node_child_process8.spawnSync)("nft", ["--version"], { stdio: "pipe" });
|
|
17602
18040
|
return result.status === 0;
|
|
17603
18041
|
}
|
|
17604
18042
|
ensureSetup() {
|
|
17605
18043
|
try {
|
|
17606
|
-
(0,
|
|
18044
|
+
(0, import_node_child_process8.execSync)(`nft list table inet ${this.table} 2>/dev/null`, { stdio: "pipe" });
|
|
17607
18045
|
} catch {
|
|
17608
|
-
(0,
|
|
17609
|
-
(0,
|
|
17610
|
-
(0,
|
|
17611
|
-
(0,
|
|
18046
|
+
(0, import_node_child_process8.execSync)(`nft add table inet ${this.table}`);
|
|
18047
|
+
(0, import_node_child_process8.execSync)(`nft add set inet ${this.table} ${this.set} '{ type ipv4_addr; flags timeout; }'`);
|
|
18048
|
+
(0, import_node_child_process8.execSync)(`nft add chain inet ${this.table} input '{ type filter hook input priority -1; policy accept; }'`);
|
|
18049
|
+
(0, import_node_child_process8.execSync)(`nft add rule inet ${this.table} input ip saddr @${this.set} drop`);
|
|
17612
18050
|
}
|
|
17613
18051
|
}
|
|
17614
18052
|
async block(ip) {
|
|
17615
18053
|
assertValidFirewallIp(ip);
|
|
17616
18054
|
this.ensureSetup();
|
|
17617
|
-
(0,
|
|
18055
|
+
(0, import_node_child_process8.execSync)(`nft add element inet ${this.table} ${this.set} '{ ${ip} }'`);
|
|
17618
18056
|
}
|
|
17619
18057
|
async unblock(ip) {
|
|
17620
18058
|
assertValidFirewallIp(ip);
|
|
17621
18059
|
try {
|
|
17622
|
-
(0,
|
|
18060
|
+
(0, import_node_child_process8.execSync)(`nft delete element inet ${this.table} ${this.set} '{ ${ip} }'`);
|
|
17623
18061
|
} catch {
|
|
17624
18062
|
}
|
|
17625
18063
|
}
|
|
17626
18064
|
async isBlocked(ip) {
|
|
17627
18065
|
assertValidFirewallIp(ip);
|
|
17628
18066
|
try {
|
|
17629
|
-
const output = (0,
|
|
18067
|
+
const output = (0, import_node_child_process8.execSync)(`nft list set inet ${this.table} ${this.set}`, { encoding: "utf-8" });
|
|
17630
18068
|
return output.includes(ip);
|
|
17631
18069
|
} catch {
|
|
17632
18070
|
return false;
|
|
@@ -17634,7 +18072,7 @@ var NftablesAdapter = class {
|
|
|
17634
18072
|
}
|
|
17635
18073
|
async listBlocked() {
|
|
17636
18074
|
try {
|
|
17637
|
-
const output = (0,
|
|
18075
|
+
const output = (0, import_node_child_process8.execSync)(`nft list set inet ${this.table} ${this.set}`, { encoding: "utf-8" });
|
|
17638
18076
|
const match = output.match(/elements\s*=\s*\{([^}]*)\}/);
|
|
17639
18077
|
if (!match) return [];
|
|
17640
18078
|
return match[1].split(",").map((s) => s.trim().split(/\s/)[0]).filter(Boolean);
|
|
@@ -17647,34 +18085,34 @@ var IptablesAdapter = class {
|
|
|
17647
18085
|
name = "iptables";
|
|
17648
18086
|
chain = "THREATCRUSH";
|
|
17649
18087
|
isAvailable() {
|
|
17650
|
-
const result = (0,
|
|
18088
|
+
const result = (0, import_node_child_process8.spawnSync)("iptables", ["--version"], { stdio: "pipe" });
|
|
17651
18089
|
return result.status === 0;
|
|
17652
18090
|
}
|
|
17653
18091
|
ensureChain() {
|
|
17654
18092
|
try {
|
|
17655
|
-
(0,
|
|
18093
|
+
(0, import_node_child_process8.execSync)(`iptables -n -L ${this.chain} 2>/dev/null`, { stdio: "pipe" });
|
|
17656
18094
|
} catch {
|
|
17657
|
-
(0,
|
|
17658
|
-
(0,
|
|
18095
|
+
(0, import_node_child_process8.execSync)(`iptables -N ${this.chain}`);
|
|
18096
|
+
(0, import_node_child_process8.execSync)(`iptables -I INPUT 1 -j ${this.chain}`);
|
|
17659
18097
|
}
|
|
17660
18098
|
}
|
|
17661
18099
|
async block(ip) {
|
|
17662
18100
|
assertValidFirewallIp(ip);
|
|
17663
18101
|
this.ensureChain();
|
|
17664
18102
|
if (await this.isBlocked(ip)) return;
|
|
17665
|
-
(0,
|
|
18103
|
+
(0, import_node_child_process8.execSync)(`iptables -A ${this.chain} -s ${ip} -j DROP`);
|
|
17666
18104
|
}
|
|
17667
18105
|
async unblock(ip) {
|
|
17668
18106
|
assertValidFirewallIp(ip);
|
|
17669
18107
|
try {
|
|
17670
|
-
(0,
|
|
18108
|
+
(0, import_node_child_process8.execSync)(`iptables -D ${this.chain} -s ${ip} -j DROP`);
|
|
17671
18109
|
} catch {
|
|
17672
18110
|
}
|
|
17673
18111
|
}
|
|
17674
18112
|
async isBlocked(ip) {
|
|
17675
18113
|
assertValidFirewallIp(ip);
|
|
17676
18114
|
try {
|
|
17677
|
-
const output = (0,
|
|
18115
|
+
const output = (0, import_node_child_process8.execSync)(`iptables -n -L ${this.chain}`, { encoding: "utf-8" });
|
|
17678
18116
|
return output.includes(ip);
|
|
17679
18117
|
} catch {
|
|
17680
18118
|
return false;
|
|
@@ -17682,7 +18120,7 @@ var IptablesAdapter = class {
|
|
|
17682
18120
|
}
|
|
17683
18121
|
async listBlocked() {
|
|
17684
18122
|
try {
|
|
17685
|
-
const output = (0,
|
|
18123
|
+
const output = (0, import_node_child_process8.execSync)(`iptables -n -L ${this.chain}`, { encoding: "utf-8" });
|
|
17686
18124
|
const ips = [];
|
|
17687
18125
|
for (const line of output.split("\n")) {
|
|
17688
18126
|
const match = line.match(/DROP\s+all\s+--\s+(\d+\.\d+\.\d+\.\d+)/);
|
|
@@ -17738,7 +18176,7 @@ function detectFirewallAdapter(backend = "auto") {
|
|
|
17738
18176
|
}
|
|
17739
18177
|
|
|
17740
18178
|
// src/daemon/firewall/remediation.ts
|
|
17741
|
-
var
|
|
18179
|
+
var import_node_fs24 = require("fs");
|
|
17742
18180
|
var import_node_net9 = require("net");
|
|
17743
18181
|
|
|
17744
18182
|
// src/daemon/firewall/crawlers.ts
|
|
@@ -18150,7 +18588,7 @@ var RemediationManager = class {
|
|
|
18150
18588
|
}
|
|
18151
18589
|
logLine(line) {
|
|
18152
18590
|
try {
|
|
18153
|
-
(0,
|
|
18591
|
+
(0, import_node_fs24.appendFileSync)(PATHS.logFile, `${(/* @__PURE__ */ new Date()).toISOString()} ${line}
|
|
18154
18592
|
`);
|
|
18155
18593
|
} catch {
|
|
18156
18594
|
}
|
|
@@ -18184,13 +18622,13 @@ function remediationSettings(section) {
|
|
|
18184
18622
|
}
|
|
18185
18623
|
|
|
18186
18624
|
// src/daemon/watchdog-service.ts
|
|
18187
|
-
var
|
|
18188
|
-
var
|
|
18625
|
+
var import_node_fs26 = require("fs");
|
|
18626
|
+
var import_node_path20 = require("path");
|
|
18189
18627
|
|
|
18190
18628
|
// src/core/watchdog.ts
|
|
18191
|
-
var
|
|
18192
|
-
var
|
|
18193
|
-
var
|
|
18629
|
+
var import_node_fs25 = require("fs");
|
|
18630
|
+
var import_node_crypto6 = require("crypto");
|
|
18631
|
+
var import_node_path19 = require("path");
|
|
18194
18632
|
var WATCHDOG_DIRNAME = "watchdog";
|
|
18195
18633
|
var LOG_FILE = "attacks.jsonl";
|
|
18196
18634
|
var ROTATED_FILE = "attacks.1.jsonl";
|
|
@@ -18230,7 +18668,7 @@ function signatureOf(r) {
|
|
|
18230
18668
|
return ["msg", r.module, shape].join("|");
|
|
18231
18669
|
}
|
|
18232
18670
|
function findingId(signature) {
|
|
18233
|
-
return (0,
|
|
18671
|
+
return (0, import_node_crypto6.createHash)("sha1").update(signature).digest("hex").slice(0, 10);
|
|
18234
18672
|
}
|
|
18235
18673
|
function toRecord(event) {
|
|
18236
18674
|
const d = event.details ?? {};
|
|
@@ -18279,12 +18717,12 @@ var WatchdogLog = class {
|
|
|
18279
18717
|
stats = { logged: 0, answered: 0, errored: 0 };
|
|
18280
18718
|
constructor(dir, options = {}) {
|
|
18281
18719
|
this.dir = dir;
|
|
18282
|
-
this.file = (0,
|
|
18720
|
+
this.file = (0, import_node_path19.join)(dir, LOG_FILE);
|
|
18283
18721
|
this.shareGid = options.shareGid;
|
|
18284
|
-
(0,
|
|
18722
|
+
(0, import_node_fs25.mkdirSync)(dir, { recursive: true, mode: this.shareGid === void 0 ? 448 : 1528 });
|
|
18285
18723
|
if (this.shareGid !== void 0) {
|
|
18286
|
-
(0,
|
|
18287
|
-
(0,
|
|
18724
|
+
(0, import_node_fs25.chownSync)(dir, process.getuid?.() ?? 0, this.shareGid);
|
|
18725
|
+
(0, import_node_fs25.chmodSync)(dir, 1528);
|
|
18288
18726
|
}
|
|
18289
18727
|
for (const r of readTail(this.file, 512 * 1024)) this.remember(dedupeKey(r));
|
|
18290
18728
|
}
|
|
@@ -18313,16 +18751,16 @@ var WatchdogLog = class {
|
|
|
18313
18751
|
const chunk = this.buffer.join("\n") + "\n";
|
|
18314
18752
|
this.buffer = [];
|
|
18315
18753
|
try {
|
|
18316
|
-
if ((0,
|
|
18317
|
-
(0,
|
|
18754
|
+
if ((0, import_node_fs25.statSync)(this.file).size > ROTATE_BYTES) {
|
|
18755
|
+
(0, import_node_fs25.renameSync)(this.file, (0, import_node_path19.join)(this.dir, ROTATED_FILE));
|
|
18318
18756
|
}
|
|
18319
18757
|
} catch {
|
|
18320
18758
|
}
|
|
18321
|
-
(0,
|
|
18759
|
+
(0, import_node_fs25.appendFileSync)(this.file, chunk, { mode: this.shareGid === void 0 ? 384 : 432 });
|
|
18322
18760
|
if (this.shareGid !== void 0) {
|
|
18323
18761
|
try {
|
|
18324
|
-
(0,
|
|
18325
|
-
(0,
|
|
18762
|
+
(0, import_node_fs25.chownSync)(this.file, process.getuid?.() ?? 0, this.shareGid);
|
|
18763
|
+
(0, import_node_fs25.chmodSync)(this.file, 432);
|
|
18326
18764
|
} catch {
|
|
18327
18765
|
}
|
|
18328
18766
|
}
|
|
@@ -18343,21 +18781,21 @@ function parseLines(text) {
|
|
|
18343
18781
|
function readTail(file, bytes) {
|
|
18344
18782
|
let fd;
|
|
18345
18783
|
try {
|
|
18346
|
-
fd = (0,
|
|
18784
|
+
fd = (0, import_node_fs25.openSync)(file, "r");
|
|
18347
18785
|
} catch (err) {
|
|
18348
18786
|
if (err.code === "ENOENT") return [];
|
|
18349
18787
|
throw err;
|
|
18350
18788
|
}
|
|
18351
18789
|
try {
|
|
18352
|
-
const size = (0,
|
|
18790
|
+
const size = (0, import_node_fs25.fstatSync)(fd).size;
|
|
18353
18791
|
const start = Math.max(0, size - bytes);
|
|
18354
18792
|
const buf = Buffer.alloc(size - start);
|
|
18355
|
-
(0,
|
|
18793
|
+
(0, import_node_fs25.readSync)(fd, buf, 0, buf.length, start);
|
|
18356
18794
|
let text = buf.toString("utf8");
|
|
18357
18795
|
if (start > 0) text = text.slice(text.indexOf("\n") + 1);
|
|
18358
18796
|
return parseLines(text);
|
|
18359
18797
|
} finally {
|
|
18360
|
-
(0,
|
|
18798
|
+
(0, import_node_fs25.closeSync)(fd);
|
|
18361
18799
|
}
|
|
18362
18800
|
}
|
|
18363
18801
|
|
|
@@ -18366,13 +18804,13 @@ function defaultShareGid() {
|
|
|
18366
18804
|
const isRoot2 = process.platform === "linux" && typeof process.getuid === "function" && process.getuid() === 0;
|
|
18367
18805
|
if (!isRoot2) return void 0;
|
|
18368
18806
|
try {
|
|
18369
|
-
return (0,
|
|
18807
|
+
return (0, import_node_fs26.statSync)("/var/log/auth.log").gid;
|
|
18370
18808
|
} catch {
|
|
18371
18809
|
return void 0;
|
|
18372
18810
|
}
|
|
18373
18811
|
}
|
|
18374
18812
|
function startWatchdog(opts) {
|
|
18375
|
-
const dir = opts.config?.dir ?? (0,
|
|
18813
|
+
const dir = opts.config?.dir ?? (0, import_node_path20.join)(opts.stateDir, WATCHDOG_DIRNAME);
|
|
18376
18814
|
const base = { dir, logged: 0, answered: 0, errored: 0 };
|
|
18377
18815
|
if (opts.config?.enabled === false) {
|
|
18378
18816
|
opts.log("[watchdog] off ([watchdog] enabled = false)");
|
|
@@ -18463,7 +18901,7 @@ async function flushTelemetry(timeoutMs = 2e3) {
|
|
|
18463
18901
|
}
|
|
18464
18902
|
|
|
18465
18903
|
// src/core/node-runtime.ts
|
|
18466
|
-
var
|
|
18904
|
+
var import_node_child_process9 = require("child_process");
|
|
18467
18905
|
var MIN_DAEMON_NODE_MAJOR = 22;
|
|
18468
18906
|
function nodeMajor(version) {
|
|
18469
18907
|
const m = String(version ?? "").trim().replace(/^v/, "").match(/^(\d+)\./);
|
|
@@ -18479,16 +18917,16 @@ function daemonNodeProblem(version) {
|
|
|
18479
18917
|
// src/daemon/index.ts
|
|
18480
18918
|
function readVersion2() {
|
|
18481
18919
|
try {
|
|
18482
|
-
const pkg = JSON.parse((0,
|
|
18920
|
+
const pkg = JSON.parse((0, import_node_fs27.readFileSync)((0, import_node_path21.join)(__dirname, "..", "package.json"), "utf-8"));
|
|
18483
18921
|
return pkg.version || "0.0.0";
|
|
18484
18922
|
} catch {
|
|
18485
18923
|
return "0.0.0";
|
|
18486
18924
|
}
|
|
18487
18925
|
}
|
|
18488
18926
|
async function runHardeningChecks() {
|
|
18489
|
-
const { stdout } = await (0, import_node_util.promisify)(
|
|
18927
|
+
const { stdout } = await (0, import_node_util.promisify)(import_node_child_process10.execFile)(
|
|
18490
18928
|
process.execPath,
|
|
18491
|
-
[(0,
|
|
18929
|
+
[(0, import_node_path21.join)(__dirname, "index.js"), "harden", "--json", "--no-upload"],
|
|
18492
18930
|
{ timeout: 12e4, maxBuffer: 4 * 1024 * 1024 }
|
|
18493
18931
|
);
|
|
18494
18932
|
const parsed = JSON.parse(stdout.slice(stdout.indexOf("{")));
|
|
@@ -18500,9 +18938,9 @@ async function runHardeningChecks() {
|
|
|
18500
18938
|
return patches ? [...findings, patches] : findings;
|
|
18501
18939
|
}
|
|
18502
18940
|
async function runPatchCheck() {
|
|
18503
|
-
const { stdout } = await (0, import_node_util.promisify)(
|
|
18941
|
+
const { stdout } = await (0, import_node_util.promisify)(import_node_child_process10.execFile)(
|
|
18504
18942
|
process.execPath,
|
|
18505
|
-
[(0,
|
|
18943
|
+
[(0, import_node_path21.join)(__dirname, "index.js"), "patches", "--json", "--no-upload"],
|
|
18506
18944
|
{ timeout: 3e5, maxBuffer: 16 * 1024 * 1024 }
|
|
18507
18945
|
);
|
|
18508
18946
|
const parsed = JSON.parse(stdout.slice(stdout.indexOf("{")));
|
|
@@ -18510,7 +18948,7 @@ async function runPatchCheck() {
|
|
|
18510
18948
|
}
|
|
18511
18949
|
function logLine(line) {
|
|
18512
18950
|
try {
|
|
18513
|
-
(0,
|
|
18951
|
+
(0, import_node_fs27.appendFileSync)(PATHS.logFile, `${(/* @__PURE__ */ new Date()).toISOString()} ${line}
|
|
18514
18952
|
`);
|
|
18515
18953
|
} catch {
|
|
18516
18954
|
}
|
|
@@ -18535,9 +18973,9 @@ async function runDaemon() {
|
|
|
18535
18973
|
logLine(`[daemon] starting threatcrushd v${version} mode=${PATHS.mode}`);
|
|
18536
18974
|
const restartDaemon = () => {
|
|
18537
18975
|
if (PATHS.mode === "system") {
|
|
18538
|
-
(0,
|
|
18976
|
+
(0, import_node_child_process10.spawn)("systemctl", ["restart", "threatcrushd"], { detached: true, stdio: "ignore" }).unref();
|
|
18539
18977
|
} else {
|
|
18540
|
-
(0,
|
|
18978
|
+
(0, import_node_child_process10.spawn)(process.execPath, process.argv.slice(1), { detached: true, stdio: "ignore" }).unref();
|
|
18541
18979
|
setTimeout(() => process.exit(0), 500).unref();
|
|
18542
18980
|
}
|
|
18543
18981
|
};
|
|
@@ -18552,7 +18990,7 @@ async function runDaemon() {
|
|
|
18552
18990
|
} catch (err) {
|
|
18553
18991
|
logLine(`[daemon] state db unavailable: ${err.message}`);
|
|
18554
18992
|
}
|
|
18555
|
-
const config = loadConfig((0,
|
|
18993
|
+
const config = loadConfig((0, import_node_fs27.existsSync)(PATHS.configFile) ? PATHS.configFile : void 0);
|
|
18556
18994
|
configureAttackDetection(config.detection);
|
|
18557
18995
|
const retentionSeconds = parseDuration(config.storage?.event_retention) ?? DEFAULT_EVENT_RETENTION_SECONDS;
|
|
18558
18996
|
const maxEvents = config.storage?.max_events ?? DEFAULT_MAX_EVENTS;
|
|
@@ -18616,11 +19054,20 @@ async function runDaemon() {
|
|
|
18616
19054
|
const threatFeeds = new ThreatFeeds({
|
|
18617
19055
|
settings: { ...feedSettings, allow: [...feedSettings.allow, ...remediation.getAllowlist()] },
|
|
18618
19056
|
enforcing: !remediationStatus.dry_run && firewallAdapter.enforces !== false,
|
|
18619
|
-
cachePath: (0,
|
|
19057
|
+
cachePath: (0, import_node_path21.join)(PATHS.stateDir, "threat-feeds.json"),
|
|
18620
19058
|
bus,
|
|
18621
19059
|
log: logLine
|
|
18622
19060
|
});
|
|
18623
19061
|
threatFeeds.start();
|
|
19062
|
+
const privilegeWatch = new PrivilegeWatch({
|
|
19063
|
+
settings: privilegeSettings(config.privileged),
|
|
19064
|
+
bus,
|
|
19065
|
+
snapshotPath: (0, import_node_path21.join)(PATHS.stateDir, "privileges.json"),
|
|
19066
|
+
loginsPath: (0, import_node_path21.join)(PATHS.stateDir, "login-countries.json"),
|
|
19067
|
+
cursorPath: (0, import_node_path21.join)(PATHS.stateDir, "sudo-cursor.json"),
|
|
19068
|
+
log: logLine
|
|
19069
|
+
});
|
|
19070
|
+
privilegeWatch.start();
|
|
18624
19071
|
new AlertDispatcher(bus, config);
|
|
18625
19072
|
const runsWorker = new RunsWorker(bus);
|
|
18626
19073
|
try {
|
|
@@ -18640,7 +19087,7 @@ async function runDaemon() {
|
|
|
18640
19087
|
version,
|
|
18641
19088
|
hostname: (0, import_node_os5.hostname)(),
|
|
18642
19089
|
minSeverity,
|
|
18643
|
-
spoolPath: (0,
|
|
19090
|
+
spoolPath: (0, import_node_path21.join)(PATHS.stateDir, "cloud-spool.jsonl"),
|
|
18644
19091
|
log: logLine,
|
|
18645
19092
|
ruleModules: new Set(ruleEngine.getRules().flatMap((rule) => rule.source_types)),
|
|
18646
19093
|
runHardening: runHardeningChecks
|
|
@@ -18657,8 +19104,8 @@ async function runDaemon() {
|
|
|
18657
19104
|
const autoUpdate = new AutoUpdate({
|
|
18658
19105
|
version,
|
|
18659
19106
|
settings: updateSettings(config.update),
|
|
18660
|
-
pkgRoot: (0,
|
|
18661
|
-
statePath: (0,
|
|
19107
|
+
pkgRoot: (0, import_node_path21.join)(__dirname, ".."),
|
|
19108
|
+
statePath: (0, import_node_path21.join)(PATHS.stateDir, "auto-update.json"),
|
|
18662
19109
|
bus,
|
|
18663
19110
|
log: logLine,
|
|
18664
19111
|
restartDaemon
|
|
@@ -18705,6 +19152,10 @@ async function runDaemon() {
|
|
|
18705
19152
|
threatFeeds.stop();
|
|
18706
19153
|
} catch {
|
|
18707
19154
|
}
|
|
19155
|
+
try {
|
|
19156
|
+
privilegeWatch.stop();
|
|
19157
|
+
} catch {
|
|
19158
|
+
}
|
|
18708
19159
|
try {
|
|
18709
19160
|
await ipc.stop();
|
|
18710
19161
|
} catch {
|