@profullstack/threatcrush 0.13.19 → 0.13.21
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 +293 -48
- package/dist/daemon.js.map +1 -1
- package/dist/index.js +1001 -414
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/daemon.js
CHANGED
|
@@ -63,9 +63,9 @@ var require_parser = __commonJS({
|
|
|
63
63
|
this.ii = 0;
|
|
64
64
|
this.state = new State(this.parseStart);
|
|
65
65
|
}
|
|
66
|
-
parse(
|
|
67
|
-
if (
|
|
68
|
-
this._buf = String(
|
|
66
|
+
parse(str3) {
|
|
67
|
+
if (str3.length === 0 || str3.length == null) return;
|
|
68
|
+
this._buf = String(str3);
|
|
69
69
|
this.ii = -1;
|
|
70
70
|
this.char = -1;
|
|
71
71
|
let getNext;
|
|
@@ -1612,16 +1612,16 @@ var require_parse_string = __commonJS({
|
|
|
1612
1612
|
module2.exports = parseString;
|
|
1613
1613
|
var TOMLParser = require_toml_parser();
|
|
1614
1614
|
var prettyError = require_parse_pretty_error();
|
|
1615
|
-
function parseString(
|
|
1616
|
-
if (global.Buffer && global.Buffer.isBuffer(
|
|
1617
|
-
|
|
1615
|
+
function parseString(str3) {
|
|
1616
|
+
if (global.Buffer && global.Buffer.isBuffer(str3)) {
|
|
1617
|
+
str3 = str3.toString("utf8");
|
|
1618
1618
|
}
|
|
1619
1619
|
const parser = new TOMLParser();
|
|
1620
1620
|
try {
|
|
1621
|
-
parser.parse(
|
|
1621
|
+
parser.parse(str3);
|
|
1622
1622
|
return parser.finish();
|
|
1623
1623
|
} catch (err) {
|
|
1624
|
-
throw prettyError(err,
|
|
1624
|
+
throw prettyError(err, str3);
|
|
1625
1625
|
}
|
|
1626
1626
|
}
|
|
1627
1627
|
}
|
|
@@ -1634,7 +1634,7 @@ var require_parse_async = __commonJS({
|
|
|
1634
1634
|
module2.exports = parseAsync;
|
|
1635
1635
|
var TOMLParser = require_toml_parser();
|
|
1636
1636
|
var prettyError = require_parse_pretty_error();
|
|
1637
|
-
function parseAsync(
|
|
1637
|
+
function parseAsync(str3, opts) {
|
|
1638
1638
|
if (!opts) opts = {};
|
|
1639
1639
|
const index = 0;
|
|
1640
1640
|
const blocksize = opts.blocksize || 40960;
|
|
@@ -1643,18 +1643,18 @@ var require_parse_async = __commonJS({
|
|
|
1643
1643
|
setImmediate(parseAsyncNext, index, blocksize, resolve3, reject);
|
|
1644
1644
|
});
|
|
1645
1645
|
function parseAsyncNext(index2, blocksize2, resolve3, reject) {
|
|
1646
|
-
if (index2 >=
|
|
1646
|
+
if (index2 >= str3.length) {
|
|
1647
1647
|
try {
|
|
1648
1648
|
return resolve3(parser.finish());
|
|
1649
1649
|
} catch (err) {
|
|
1650
|
-
return reject(prettyError(err,
|
|
1650
|
+
return reject(prettyError(err, str3));
|
|
1651
1651
|
}
|
|
1652
1652
|
}
|
|
1653
1653
|
try {
|
|
1654
|
-
parser.parse(
|
|
1654
|
+
parser.parse(str3.slice(index2, index2 + blocksize2));
|
|
1655
1655
|
setImmediate(parseAsyncNext, index2 + blocksize2, blocksize2, resolve3, reject);
|
|
1656
1656
|
} catch (err) {
|
|
1657
|
-
reject(prettyError(err,
|
|
1657
|
+
reject(prettyError(err, str3));
|
|
1658
1658
|
}
|
|
1659
1659
|
}
|
|
1660
1660
|
}
|
|
@@ -1860,22 +1860,22 @@ var require_stringify = __commonJS({
|
|
|
1860
1860
|
return stringifyBasicString(keyStr);
|
|
1861
1861
|
}
|
|
1862
1862
|
}
|
|
1863
|
-
function stringifyBasicString(
|
|
1864
|
-
return '"' + escapeString(
|
|
1863
|
+
function stringifyBasicString(str3) {
|
|
1864
|
+
return '"' + escapeString(str3).replace(/"/g, '\\"') + '"';
|
|
1865
1865
|
}
|
|
1866
|
-
function stringifyLiteralString(
|
|
1867
|
-
return "'" +
|
|
1866
|
+
function stringifyLiteralString(str3) {
|
|
1867
|
+
return "'" + str3 + "'";
|
|
1868
1868
|
}
|
|
1869
|
-
function numpad(num,
|
|
1870
|
-
while (
|
|
1871
|
-
return
|
|
1869
|
+
function numpad(num, str3) {
|
|
1870
|
+
while (str3.length < num) str3 = "0" + str3;
|
|
1871
|
+
return str3;
|
|
1872
1872
|
}
|
|
1873
|
-
function escapeString(
|
|
1874
|
-
return
|
|
1873
|
+
function escapeString(str3) {
|
|
1874
|
+
return str3.replace(/\\/g, "\\\\").replace(/[\b]/g, "\\b").replace(/\t/g, "\\t").replace(/\n/g, "\\n").replace(/\f/g, "\\f").replace(/\r/g, "\\r").replace(/([\u0000-\u001f\u007f])/, (c) => "\\u" + numpad(4, c.codePointAt(0).toString(16)));
|
|
1875
1875
|
}
|
|
1876
|
-
function stringifyMultilineString(
|
|
1877
|
-
let escaped =
|
|
1878
|
-
return escapeString(
|
|
1876
|
+
function stringifyMultilineString(str3) {
|
|
1877
|
+
let escaped = str3.split(/\n/).map((str4) => {
|
|
1878
|
+
return escapeString(str4).replace(/"(?="")/g, '\\"');
|
|
1879
1879
|
}).join("\n");
|
|
1880
1880
|
if (escaped.slice(-1) === '"') escaped += "\\\n";
|
|
1881
1881
|
return '"""\n' + escaped + '"""';
|
|
@@ -2022,10 +2022,10 @@ var require_toml = __commonJS({
|
|
|
2022
2022
|
});
|
|
2023
2023
|
|
|
2024
2024
|
// src/daemon/index.ts
|
|
2025
|
-
var
|
|
2025
|
+
var import_node_fs23 = require("fs");
|
|
2026
2026
|
var import_node_child_process7 = require("child_process");
|
|
2027
2027
|
var import_node_os5 = require("os");
|
|
2028
|
-
var
|
|
2028
|
+
var import_node_path18 = require("path");
|
|
2029
2029
|
var import_node_util = require("util");
|
|
2030
2030
|
|
|
2031
2031
|
// src/daemon/paths.ts
|
|
@@ -2365,10 +2365,11 @@ function formatDuration(seconds) {
|
|
|
2365
2365
|
|
|
2366
2366
|
// src/daemon/ipc-server.ts
|
|
2367
2367
|
var IpcServer = class {
|
|
2368
|
-
constructor(version, moduleHost, remediation) {
|
|
2368
|
+
constructor(version, moduleHost, remediation, watchdog) {
|
|
2369
2369
|
this.version = version;
|
|
2370
2370
|
this.moduleHost = moduleHost;
|
|
2371
2371
|
this.remediation = remediation;
|
|
2372
|
+
this.watchdog = watchdog;
|
|
2372
2373
|
bus.on("event", (event) => {
|
|
2373
2374
|
this.counters.events++;
|
|
2374
2375
|
if (event.severity === "medium" || event.severity === "high" || event.severity === "critical") {
|
|
@@ -2386,6 +2387,7 @@ var IpcServer = class {
|
|
|
2386
2387
|
version;
|
|
2387
2388
|
moduleHost;
|
|
2388
2389
|
remediation;
|
|
2390
|
+
watchdog;
|
|
2389
2391
|
server = null;
|
|
2390
2392
|
clients = /* @__PURE__ */ new Map();
|
|
2391
2393
|
nextClientId = 1;
|
|
@@ -2510,7 +2512,8 @@ var IpcServer = class {
|
|
|
2510
2512
|
},
|
|
2511
2513
|
modules: this.moduleHost.summary(),
|
|
2512
2514
|
counters: { ...this.counters },
|
|
2513
|
-
stateDb: isStateDbAvailable()
|
|
2515
|
+
stateDb: isStateDbAvailable(),
|
|
2516
|
+
...this.watchdog ? { watchdog: this.watchdog() } : {}
|
|
2514
2517
|
};
|
|
2515
2518
|
return this.send(client, { id: req.id, ok: true, result: status });
|
|
2516
2519
|
}
|
|
@@ -9546,6 +9549,9 @@ var LogWatcher = class {
|
|
|
9546
9549
|
const reqPath = (entry.fields.path || "").split("?")[0];
|
|
9547
9550
|
if (reqPath) details.path = reqPath;
|
|
9548
9551
|
if (entry.fields.user_agent) details.ua = entry.fields.user_agent;
|
|
9552
|
+
if (Number.isFinite(status)) details.status = status;
|
|
9553
|
+
if (entry.fields.method) details.method = entry.fields.method;
|
|
9554
|
+
if (entry.fields.path) details.url = entry.fields.path.slice(0, 2048);
|
|
9549
9555
|
if (crs.score > 0) {
|
|
9550
9556
|
Object.assign(details, {
|
|
9551
9557
|
attack_type: crs.attackType,
|
|
@@ -10312,14 +10318,14 @@ var DnsMonitor = class {
|
|
|
10312
10318
|
);
|
|
10313
10319
|
}
|
|
10314
10320
|
}
|
|
10315
|
-
shannonEntropy(
|
|
10321
|
+
shannonEntropy(str3) {
|
|
10316
10322
|
const freq = /* @__PURE__ */ new Map();
|
|
10317
|
-
for (const ch of
|
|
10323
|
+
for (const ch of str3) {
|
|
10318
10324
|
freq.set(ch, (freq.get(ch) || 0) + 1);
|
|
10319
10325
|
}
|
|
10320
10326
|
let entropy = 0;
|
|
10321
10327
|
for (const count of freq.values()) {
|
|
10322
|
-
const p = count /
|
|
10328
|
+
const p = count / str3.length;
|
|
10323
10329
|
if (p > 0) entropy -= p * Math.log2(p);
|
|
10324
10330
|
}
|
|
10325
10331
|
return entropy;
|
|
@@ -10389,6 +10395,7 @@ function loadConfig(configPath) {
|
|
|
10389
10395
|
remediation: parsed.remediation,
|
|
10390
10396
|
detection: parsed.detection,
|
|
10391
10397
|
cloud: parsed.cloud,
|
|
10398
|
+
watchdog: parsed.watchdog,
|
|
10392
10399
|
license: parsed.license
|
|
10393
10400
|
};
|
|
10394
10401
|
} catch {
|
|
@@ -14223,7 +14230,7 @@ function isTestPath(relativePath) {
|
|
|
14223
14230
|
function rustCodeLines(lines) {
|
|
14224
14231
|
const out = [];
|
|
14225
14232
|
let commentDepth = 0;
|
|
14226
|
-
let
|
|
14233
|
+
let str3 = null;
|
|
14227
14234
|
for (const line of lines) {
|
|
14228
14235
|
let code = "";
|
|
14229
14236
|
let i = 0;
|
|
@@ -14247,15 +14254,15 @@ function rustCodeLines(lines) {
|
|
|
14247
14254
|
i += 1;
|
|
14248
14255
|
continue;
|
|
14249
14256
|
}
|
|
14250
|
-
if (
|
|
14251
|
-
if (
|
|
14257
|
+
if (str3) {
|
|
14258
|
+
if (str3.raw) {
|
|
14252
14259
|
if (c === '"') {
|
|
14253
14260
|
let hashes = 0;
|
|
14254
14261
|
while (line[i + 1 + hashes] === "#") hashes += 1;
|
|
14255
|
-
if (hashes >=
|
|
14256
|
-
code += " ".repeat(1 +
|
|
14257
|
-
i += 1 +
|
|
14258
|
-
|
|
14262
|
+
if (hashes >= str3.hashes) {
|
|
14263
|
+
code += " ".repeat(1 + str3.hashes);
|
|
14264
|
+
i += 1 + str3.hashes;
|
|
14265
|
+
str3 = null;
|
|
14259
14266
|
continue;
|
|
14260
14267
|
}
|
|
14261
14268
|
}
|
|
@@ -14269,7 +14276,7 @@ function rustCodeLines(lines) {
|
|
|
14269
14276
|
continue;
|
|
14270
14277
|
}
|
|
14271
14278
|
if (c === '"') {
|
|
14272
|
-
|
|
14279
|
+
str3 = null;
|
|
14273
14280
|
code += " ";
|
|
14274
14281
|
i += 1;
|
|
14275
14282
|
continue;
|
|
@@ -14291,14 +14298,14 @@ function rustCodeLines(lines) {
|
|
|
14291
14298
|
if (i === 0 || !/[A-Za-z0-9_]/.test(line[i - 1])) {
|
|
14292
14299
|
const raw = /^b?r(#*)"/.exec(line.slice(i));
|
|
14293
14300
|
if (raw) {
|
|
14294
|
-
|
|
14301
|
+
str3 = { raw: true, hashes: raw[1].length };
|
|
14295
14302
|
code += " ".repeat(raw[0].length);
|
|
14296
14303
|
i += raw[0].length;
|
|
14297
14304
|
continue;
|
|
14298
14305
|
}
|
|
14299
14306
|
}
|
|
14300
14307
|
if (c === '"') {
|
|
14301
|
-
|
|
14308
|
+
str3 = { raw: false, hashes: 0 };
|
|
14302
14309
|
code += " ";
|
|
14303
14310
|
i += 1;
|
|
14304
14311
|
continue;
|
|
@@ -17326,6 +17333,239 @@ function remediationSettings(section) {
|
|
|
17326
17333
|
return settings;
|
|
17327
17334
|
}
|
|
17328
17335
|
|
|
17336
|
+
// src/daemon/watchdog-service.ts
|
|
17337
|
+
var import_node_fs22 = require("fs");
|
|
17338
|
+
var import_node_path17 = require("path");
|
|
17339
|
+
|
|
17340
|
+
// src/core/watchdog.ts
|
|
17341
|
+
var import_node_fs21 = require("fs");
|
|
17342
|
+
var import_node_crypto5 = require("crypto");
|
|
17343
|
+
var import_node_path16 = require("path");
|
|
17344
|
+
var WATCHDOG_DIRNAME = "watchdog";
|
|
17345
|
+
var LOG_FILE = "attacks.jsonl";
|
|
17346
|
+
var ROTATED_FILE = "attacks.1.jsonl";
|
|
17347
|
+
var ROTATE_BYTES = 50 * 1024 * 1024;
|
|
17348
|
+
var SEEN_LIMIT = 5e3;
|
|
17349
|
+
var SEVERITY_RANK6 = { info: 0, low: 1, medium: 2, high: 3, critical: 4 };
|
|
17350
|
+
function str2(v) {
|
|
17351
|
+
return typeof v === "string" && v !== "" && v !== "-" ? v : void 0;
|
|
17352
|
+
}
|
|
17353
|
+
function isAttack(event) {
|
|
17354
|
+
if (event.module === "firewall-rules") return false;
|
|
17355
|
+
const d = event.details ?? {};
|
|
17356
|
+
if (d.heartbeat) return false;
|
|
17357
|
+
const crs = typeof d.crs_score === "number" ? d.crs_score : 0;
|
|
17358
|
+
if (crs > 0 || str2(d.attack_type)) return true;
|
|
17359
|
+
if (event.category === "web" && typeof d.status === "number" && !str2(d.rule_id)) return false;
|
|
17360
|
+
return SEVERITY_RANK6[event.severity] >= SEVERITY_RANK6.medium;
|
|
17361
|
+
}
|
|
17362
|
+
function outcomeFor(status) {
|
|
17363
|
+
if (typeof status !== "number" || !Number.isFinite(status)) return "unknown";
|
|
17364
|
+
if (status >= 500) return "errored";
|
|
17365
|
+
if (status >= 400) return "refused";
|
|
17366
|
+
if (status >= 300) return "redirected";
|
|
17367
|
+
if (status >= 100) return "answered";
|
|
17368
|
+
return "unknown";
|
|
17369
|
+
}
|
|
17370
|
+
function requestFromMessage(message) {
|
|
17371
|
+
const m = /\b(GET|POST|PUT|PATCH|DELETE|HEAD|OPTIONS|CONNECT|TRACE|PROPFIND)\s+(\S+)/.exec(message);
|
|
17372
|
+
return m ? { method: m[1], url: m[2] } : {};
|
|
17373
|
+
}
|
|
17374
|
+
function signatureOf(r) {
|
|
17375
|
+
if (r.path) {
|
|
17376
|
+
return ["web", r.host ?? "", r.method ?? "", r.path, r.attack_type ?? r.rule_id ?? ""].join("|");
|
|
17377
|
+
}
|
|
17378
|
+
if (r.rule_id) return ["rule", r.module, r.rule_id].join("|");
|
|
17379
|
+
const shape = r.message.replace(/\b\d{1,3}(?:\.\d{1,3}){3}(?:\/\d+)?\b/g, "<ip>").replace(/\b[0-9a-f]*:[0-9a-f:]+\b/gi, "<ip>").replace(/\d+/g, "#");
|
|
17380
|
+
return ["msg", r.module, shape].join("|");
|
|
17381
|
+
}
|
|
17382
|
+
function findingId(signature) {
|
|
17383
|
+
return (0, import_node_crypto5.createHash)("sha1").update(signature).digest("hex").slice(0, 10);
|
|
17384
|
+
}
|
|
17385
|
+
function toRecord(event) {
|
|
17386
|
+
const d = event.details ?? {};
|
|
17387
|
+
const fromMsg = requestFromMessage(event.message);
|
|
17388
|
+
const url = str2(d.url) ?? fromMsg.url;
|
|
17389
|
+
const path = str2(d.path) ?? (url ? url.split("?")[0] : void 0);
|
|
17390
|
+
const status = typeof d.status === "number" ? d.status : void 0;
|
|
17391
|
+
const base = {
|
|
17392
|
+
module: event.module,
|
|
17393
|
+
host: str2(d.host),
|
|
17394
|
+
method: str2(d.method) ?? fromMsg.method,
|
|
17395
|
+
path,
|
|
17396
|
+
attack_type: str2(d.attack_type),
|
|
17397
|
+
rule_id: str2(d.rule_id),
|
|
17398
|
+
message: event.message
|
|
17399
|
+
};
|
|
17400
|
+
const timestamp = event.timestamp instanceof Date ? event.timestamp : new Date(event.timestamp);
|
|
17401
|
+
const record = {
|
|
17402
|
+
v: 1,
|
|
17403
|
+
id: findingId(signatureOf(base)),
|
|
17404
|
+
at: (Number.isNaN(timestamp.getTime()) ? /* @__PURE__ */ new Date() : timestamp).toISOString(),
|
|
17405
|
+
category: event.category,
|
|
17406
|
+
severity: event.severity,
|
|
17407
|
+
...base,
|
|
17408
|
+
ip: event.source_ip,
|
|
17409
|
+
url,
|
|
17410
|
+
status,
|
|
17411
|
+
outcome: outcomeFor(status),
|
|
17412
|
+
crs_score: typeof d.crs_score === "number" ? d.crs_score : void 0,
|
|
17413
|
+
ua: str2(d.ua)
|
|
17414
|
+
};
|
|
17415
|
+
for (const k of Object.keys(record)) {
|
|
17416
|
+
if (record[k] === void 0) delete record[k];
|
|
17417
|
+
}
|
|
17418
|
+
return record;
|
|
17419
|
+
}
|
|
17420
|
+
function dedupeKey(r) {
|
|
17421
|
+
return `${r.at}|${r.ip ?? ""}|${r.message}`;
|
|
17422
|
+
}
|
|
17423
|
+
var WatchdogLog = class {
|
|
17424
|
+
dir;
|
|
17425
|
+
file;
|
|
17426
|
+
buffer = [];
|
|
17427
|
+
seen = /* @__PURE__ */ new Set();
|
|
17428
|
+
shareGid;
|
|
17429
|
+
stats = { logged: 0, answered: 0, errored: 0 };
|
|
17430
|
+
constructor(dir, options = {}) {
|
|
17431
|
+
this.dir = dir;
|
|
17432
|
+
this.file = (0, import_node_path16.join)(dir, LOG_FILE);
|
|
17433
|
+
this.shareGid = options.shareGid;
|
|
17434
|
+
(0, import_node_fs21.mkdirSync)(dir, { recursive: true, mode: this.shareGid === void 0 ? 448 : 1528 });
|
|
17435
|
+
if (this.shareGid !== void 0) {
|
|
17436
|
+
(0, import_node_fs21.chownSync)(dir, process.getuid?.() ?? 0, this.shareGid);
|
|
17437
|
+
(0, import_node_fs21.chmodSync)(dir, 1528);
|
|
17438
|
+
}
|
|
17439
|
+
for (const r of readTail(this.file, 512 * 1024)) this.remember(dedupeKey(r));
|
|
17440
|
+
}
|
|
17441
|
+
remember(key) {
|
|
17442
|
+
this.seen.add(key);
|
|
17443
|
+
if (this.seen.size > SEEN_LIMIT) {
|
|
17444
|
+
const oldest = this.seen.values().next().value;
|
|
17445
|
+
if (oldest !== void 0) this.seen.delete(oldest);
|
|
17446
|
+
}
|
|
17447
|
+
}
|
|
17448
|
+
/** Logs the event if it is an attack not already logged. Returns the record when it was. */
|
|
17449
|
+
record(event) {
|
|
17450
|
+
if (!isAttack(event)) return null;
|
|
17451
|
+
const rec = toRecord(event);
|
|
17452
|
+
const key = dedupeKey(rec);
|
|
17453
|
+
if (this.seen.has(key)) return null;
|
|
17454
|
+
this.remember(key);
|
|
17455
|
+
this.buffer.push(JSON.stringify(rec));
|
|
17456
|
+
this.stats.logged++;
|
|
17457
|
+
if (rec.outcome === "answered") this.stats.answered++;
|
|
17458
|
+
if (rec.outcome === "errored") this.stats.errored++;
|
|
17459
|
+
return rec;
|
|
17460
|
+
}
|
|
17461
|
+
flush() {
|
|
17462
|
+
if (this.buffer.length === 0) return;
|
|
17463
|
+
const chunk = this.buffer.join("\n") + "\n";
|
|
17464
|
+
this.buffer = [];
|
|
17465
|
+
try {
|
|
17466
|
+
if ((0, import_node_fs21.statSync)(this.file).size > ROTATE_BYTES) {
|
|
17467
|
+
(0, import_node_fs21.renameSync)(this.file, (0, import_node_path16.join)(this.dir, ROTATED_FILE));
|
|
17468
|
+
}
|
|
17469
|
+
} catch {
|
|
17470
|
+
}
|
|
17471
|
+
(0, import_node_fs21.appendFileSync)(this.file, chunk, { mode: this.shareGid === void 0 ? 384 : 432 });
|
|
17472
|
+
if (this.shareGid !== void 0) {
|
|
17473
|
+
try {
|
|
17474
|
+
(0, import_node_fs21.chownSync)(this.file, process.getuid?.() ?? 0, this.shareGid);
|
|
17475
|
+
(0, import_node_fs21.chmodSync)(this.file, 432);
|
|
17476
|
+
} catch {
|
|
17477
|
+
}
|
|
17478
|
+
}
|
|
17479
|
+
}
|
|
17480
|
+
};
|
|
17481
|
+
function parseLines(text) {
|
|
17482
|
+
const out = [];
|
|
17483
|
+
for (const line of text.split("\n")) {
|
|
17484
|
+
if (!line) continue;
|
|
17485
|
+
try {
|
|
17486
|
+
const r = JSON.parse(line);
|
|
17487
|
+
if (r && r.v === 1 && typeof r.id === "string") out.push(r);
|
|
17488
|
+
} catch {
|
|
17489
|
+
}
|
|
17490
|
+
}
|
|
17491
|
+
return out;
|
|
17492
|
+
}
|
|
17493
|
+
function readTail(file, bytes) {
|
|
17494
|
+
let fd;
|
|
17495
|
+
try {
|
|
17496
|
+
fd = (0, import_node_fs21.openSync)(file, "r");
|
|
17497
|
+
} catch (err) {
|
|
17498
|
+
if (err.code === "ENOENT") return [];
|
|
17499
|
+
throw err;
|
|
17500
|
+
}
|
|
17501
|
+
try {
|
|
17502
|
+
const size = (0, import_node_fs21.fstatSync)(fd).size;
|
|
17503
|
+
const start = Math.max(0, size - bytes);
|
|
17504
|
+
const buf = Buffer.alloc(size - start);
|
|
17505
|
+
(0, import_node_fs21.readSync)(fd, buf, 0, buf.length, start);
|
|
17506
|
+
let text = buf.toString("utf8");
|
|
17507
|
+
if (start > 0) text = text.slice(text.indexOf("\n") + 1);
|
|
17508
|
+
return parseLines(text);
|
|
17509
|
+
} finally {
|
|
17510
|
+
(0, import_node_fs21.closeSync)(fd);
|
|
17511
|
+
}
|
|
17512
|
+
}
|
|
17513
|
+
|
|
17514
|
+
// src/daemon/watchdog-service.ts
|
|
17515
|
+
function defaultShareGid() {
|
|
17516
|
+
const isRoot2 = process.platform === "linux" && typeof process.getuid === "function" && process.getuid() === 0;
|
|
17517
|
+
if (!isRoot2) return void 0;
|
|
17518
|
+
try {
|
|
17519
|
+
return (0, import_node_fs22.statSync)("/var/log/auth.log").gid;
|
|
17520
|
+
} catch {
|
|
17521
|
+
return void 0;
|
|
17522
|
+
}
|
|
17523
|
+
}
|
|
17524
|
+
function startWatchdog(opts) {
|
|
17525
|
+
const dir = opts.config?.dir ?? (0, import_node_path17.join)(opts.stateDir, WATCHDOG_DIRNAME);
|
|
17526
|
+
const base = { dir, logged: 0, answered: 0, errored: 0 };
|
|
17527
|
+
if (opts.config?.enabled === false) {
|
|
17528
|
+
opts.log("[watchdog] off ([watchdog] enabled = false)");
|
|
17529
|
+
return { status: () => ({ enabled: false, ...base }), stop: () => {
|
|
17530
|
+
} };
|
|
17531
|
+
}
|
|
17532
|
+
let log;
|
|
17533
|
+
try {
|
|
17534
|
+
log = new WatchdogLog(dir, { shareGid: opts.shareGid ?? defaultShareGid() });
|
|
17535
|
+
} catch (err) {
|
|
17536
|
+
const error2 = err.message;
|
|
17537
|
+
opts.log(`[watchdog] cannot open ${dir}: ${error2}`);
|
|
17538
|
+
return { status: () => ({ enabled: false, ...base, error: error2 }), stop: () => {
|
|
17539
|
+
} };
|
|
17540
|
+
}
|
|
17541
|
+
let error;
|
|
17542
|
+
const onEvent = (event) => {
|
|
17543
|
+
log.record(event);
|
|
17544
|
+
};
|
|
17545
|
+
const flush = () => {
|
|
17546
|
+
try {
|
|
17547
|
+
log.flush();
|
|
17548
|
+
error = void 0;
|
|
17549
|
+
} catch (err) {
|
|
17550
|
+
const message = err.message;
|
|
17551
|
+
if (message !== error) opts.log(`[watchdog] write failed: ${message}`);
|
|
17552
|
+
error = message;
|
|
17553
|
+
}
|
|
17554
|
+
};
|
|
17555
|
+
opts.bus.on("event", onEvent);
|
|
17556
|
+
const timer = setInterval(flush, opts.flushMs ?? 1e3);
|
|
17557
|
+
timer.unref?.();
|
|
17558
|
+
opts.log(`[watchdog] logging attacks to ${log.file}`);
|
|
17559
|
+
return {
|
|
17560
|
+
status: () => ({ enabled: true, dir, ...log.stats, ...error ? { error } : {} }),
|
|
17561
|
+
stop: () => {
|
|
17562
|
+
clearInterval(timer);
|
|
17563
|
+
opts.bus.off("event", onEvent);
|
|
17564
|
+
flush();
|
|
17565
|
+
}
|
|
17566
|
+
};
|
|
17567
|
+
}
|
|
17568
|
+
|
|
17329
17569
|
// src/core/telemetry.ts
|
|
17330
17570
|
var ready = false;
|
|
17331
17571
|
var sentry = null;
|
|
@@ -17389,7 +17629,7 @@ function daemonNodeProblem(version) {
|
|
|
17389
17629
|
// src/daemon/index.ts
|
|
17390
17630
|
function readVersion2() {
|
|
17391
17631
|
try {
|
|
17392
|
-
const pkg = JSON.parse((0,
|
|
17632
|
+
const pkg = JSON.parse((0, import_node_fs23.readFileSync)((0, import_node_path18.join)(__dirname, "..", "package.json"), "utf-8"));
|
|
17393
17633
|
return pkg.version || "0.0.0";
|
|
17394
17634
|
} catch {
|
|
17395
17635
|
return "0.0.0";
|
|
@@ -17398,7 +17638,7 @@ function readVersion2() {
|
|
|
17398
17638
|
async function runHardeningChecks() {
|
|
17399
17639
|
const { stdout } = await (0, import_node_util.promisify)(import_node_child_process7.execFile)(
|
|
17400
17640
|
process.execPath,
|
|
17401
|
-
[(0,
|
|
17641
|
+
[(0, import_node_path18.join)(__dirname, "index.js"), "harden", "--json", "--no-upload"],
|
|
17402
17642
|
{ timeout: 12e4, maxBuffer: 4 * 1024 * 1024 }
|
|
17403
17643
|
);
|
|
17404
17644
|
const parsed = JSON.parse(stdout.slice(stdout.indexOf("{")));
|
|
@@ -17406,7 +17646,7 @@ async function runHardeningChecks() {
|
|
|
17406
17646
|
}
|
|
17407
17647
|
function logLine(line) {
|
|
17408
17648
|
try {
|
|
17409
|
-
(0,
|
|
17649
|
+
(0, import_node_fs23.appendFileSync)(PATHS.logFile, `${(/* @__PURE__ */ new Date()).toISOString()} ${line}
|
|
17410
17650
|
`);
|
|
17411
17651
|
} catch {
|
|
17412
17652
|
}
|
|
@@ -17448,7 +17688,7 @@ async function runDaemon() {
|
|
|
17448
17688
|
} catch (err) {
|
|
17449
17689
|
logLine(`[daemon] state db unavailable: ${err.message}`);
|
|
17450
17690
|
}
|
|
17451
|
-
const config = loadConfig((0,
|
|
17691
|
+
const config = loadConfig((0, import_node_fs23.existsSync)(PATHS.configFile) ? PATHS.configFile : void 0);
|
|
17452
17692
|
configureAttackDetection(config.detection);
|
|
17453
17693
|
bus.on("event", (event) => {
|
|
17454
17694
|
logLine(`[event] ${event.severity} ${event.module} ${event.message}`);
|
|
@@ -17515,7 +17755,7 @@ async function runDaemon() {
|
|
|
17515
17755
|
version,
|
|
17516
17756
|
hostname: (0, import_node_os5.hostname)(),
|
|
17517
17757
|
minSeverity,
|
|
17518
|
-
spoolPath: (0,
|
|
17758
|
+
spoolPath: (0, import_node_path18.join)(PATHS.stateDir, "cloud-spool.jsonl"),
|
|
17519
17759
|
log: logLine,
|
|
17520
17760
|
ruleModules: new Set(ruleEngine.getRules().flatMap((rule) => rule.source_types)),
|
|
17521
17761
|
runHardening: runHardeningChecks
|
|
@@ -17528,7 +17768,8 @@ async function runDaemon() {
|
|
|
17528
17768
|
const link = currentLink();
|
|
17529
17769
|
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");
|
|
17530
17770
|
}
|
|
17531
|
-
const
|
|
17771
|
+
const watchdog = startWatchdog({ bus, config: config.watchdog, stateDir: PATHS.stateDir, log: logLine });
|
|
17772
|
+
const ipc = new IpcServer(version, moduleHost, remediation, () => watchdog.status());
|
|
17532
17773
|
await ipc.start();
|
|
17533
17774
|
logLine(`[daemon] ipc listening on ${PATHS.socket}`);
|
|
17534
17775
|
const shutdown = async (signal) => {
|
|
@@ -17557,6 +17798,10 @@ async function runDaemon() {
|
|
|
17557
17798
|
await moduleHost.stop();
|
|
17558
17799
|
} catch {
|
|
17559
17800
|
}
|
|
17801
|
+
try {
|
|
17802
|
+
watchdog.stop();
|
|
17803
|
+
} catch {
|
|
17804
|
+
}
|
|
17560
17805
|
try {
|
|
17561
17806
|
await ipc.stop();
|
|
17562
17807
|
} catch {
|