@profullstack/threatcrush 0.13.21 → 0.13.23
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 +284 -166
- package/dist/daemon.js.map +1 -1
- package/dist/index.js +497 -362
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -12772,6 +12772,7 @@ var init_dist = __esm({
|
|
|
12772
12772
|
"../../node_modules/.pnpm/@profullstack+hqtui@0.7.0/node_modules/@profullstack/hqtui/dist/index.js"() {
|
|
12773
12773
|
"use strict";
|
|
12774
12774
|
init_app();
|
|
12775
|
+
init_markdown();
|
|
12775
12776
|
init_color();
|
|
12776
12777
|
init_theme();
|
|
12777
12778
|
init_widgets();
|
|
@@ -12785,26 +12786,26 @@ function isRoot() {
|
|
|
12785
12786
|
function resolveClientSocket() {
|
|
12786
12787
|
const override = process.env.THREATCRUSH_SOCKET;
|
|
12787
12788
|
if (override) return override;
|
|
12788
|
-
if ((0,
|
|
12789
|
-
if ((0,
|
|
12789
|
+
if ((0, import_node_fs5.existsSync)(SYSTEM_PATHS.socket)) return SYSTEM_PATHS.socket;
|
|
12790
|
+
if ((0, import_node_fs5.existsSync)(USER_PATHS.socket)) return USER_PATHS.socket;
|
|
12790
12791
|
return PATHS.socket;
|
|
12791
12792
|
}
|
|
12792
12793
|
function systemDaemonPresent() {
|
|
12793
|
-
return (0,
|
|
12794
|
+
return (0, import_node_fs5.existsSync)(SYSTEM_PATHS.socket) || (0, import_node_fs5.existsSync)(SYSTEM_PATHS.pidFile);
|
|
12794
12795
|
}
|
|
12795
12796
|
function ensureRuntimeDirs() {
|
|
12796
12797
|
for (const dir of [PATHS.configDir, PATHS.confD, PATHS.moduleDir, PATHS.logDir, PATHS.stateDir, PATHS.runDir]) {
|
|
12797
12798
|
try {
|
|
12798
|
-
(0,
|
|
12799
|
+
(0, import_node_fs5.mkdirSync)(dir, { recursive: true });
|
|
12799
12800
|
} catch {
|
|
12800
12801
|
}
|
|
12801
12802
|
}
|
|
12802
12803
|
}
|
|
12803
|
-
var
|
|
12804
|
+
var import_node_fs5, import_node_os2, import_node_path4, userBase, SYSTEM_PATHS, USER_PATHS, PATHS;
|
|
12804
12805
|
var init_paths = __esm({
|
|
12805
12806
|
"src/daemon/paths.ts"() {
|
|
12806
12807
|
"use strict";
|
|
12807
|
-
|
|
12808
|
+
import_node_fs5 = require("fs");
|
|
12808
12809
|
import_node_os2 = require("os");
|
|
12809
12810
|
import_node_path4 = require("path");
|
|
12810
12811
|
userBase = (0, import_node_path4.join)((0, import_node_os2.homedir)(), ".threatcrush");
|
|
@@ -12843,17 +12844,17 @@ var init_paths = __esm({
|
|
|
12843
12844
|
// src/daemon/control-token.ts
|
|
12844
12845
|
function issueControlToken() {
|
|
12845
12846
|
const token = (0, import_node_crypto.randomBytes)(32).toString("hex");
|
|
12846
|
-
(0,
|
|
12847
|
+
(0, import_node_fs6.writeFileSync)(CONTROL_TOKEN_FILE, token, { mode: 384 });
|
|
12847
12848
|
try {
|
|
12848
|
-
(0,
|
|
12849
|
+
(0, import_node_fs6.chmodSync)(CONTROL_TOKEN_FILE, 384);
|
|
12849
12850
|
} catch {
|
|
12850
12851
|
}
|
|
12851
12852
|
return token;
|
|
12852
12853
|
}
|
|
12853
12854
|
function readControlToken() {
|
|
12854
|
-
if (!(0,
|
|
12855
|
+
if (!(0, import_node_fs6.existsSync)(CONTROL_TOKEN_FILE)) return null;
|
|
12855
12856
|
try {
|
|
12856
|
-
const token = (0,
|
|
12857
|
+
const token = (0, import_node_fs6.readFileSync)(CONTROL_TOKEN_FILE, "utf-8").trim();
|
|
12857
12858
|
return token || null;
|
|
12858
12859
|
} catch {
|
|
12859
12860
|
return null;
|
|
@@ -12865,12 +12866,12 @@ function tokensMatch(expected, provided) {
|
|
|
12865
12866
|
const b = Buffer.from(provided);
|
|
12866
12867
|
return a.length === b.length && (0, import_node_crypto.timingSafeEqual)(a, b);
|
|
12867
12868
|
}
|
|
12868
|
-
var import_node_crypto,
|
|
12869
|
+
var import_node_crypto, import_node_fs6, import_node_path5, CONTROL_TOKEN_FILE;
|
|
12869
12870
|
var init_control_token = __esm({
|
|
12870
12871
|
"src/daemon/control-token.ts"() {
|
|
12871
12872
|
"use strict";
|
|
12872
12873
|
import_node_crypto = require("crypto");
|
|
12873
|
-
|
|
12874
|
+
import_node_fs6 = require("fs");
|
|
12874
12875
|
import_node_path5 = require("path");
|
|
12875
12876
|
init_paths();
|
|
12876
12877
|
CONTROL_TOKEN_FILE = (0, import_node_path5.join)(PATHS.runDir, "control.token");
|
|
@@ -12884,12 +12885,12 @@ function daemonError(error) {
|
|
|
12884
12885
|
}
|
|
12885
12886
|
return new Error(error || "ipc error");
|
|
12886
12887
|
}
|
|
12887
|
-
var import_node_net,
|
|
12888
|
+
var import_node_net, import_node_fs7, REQUEST_TIMEOUT_MS, IpcClient;
|
|
12888
12889
|
var init_ipc_client = __esm({
|
|
12889
12890
|
"src/core/ipc-client.ts"() {
|
|
12890
12891
|
"use strict";
|
|
12891
12892
|
import_node_net = require("net");
|
|
12892
|
-
|
|
12893
|
+
import_node_fs7 = require("fs");
|
|
12893
12894
|
init_paths();
|
|
12894
12895
|
init_control_token();
|
|
12895
12896
|
REQUEST_TIMEOUT_MS = 1e4;
|
|
@@ -12905,10 +12906,10 @@ var init_ipc_client = __esm({
|
|
|
12905
12906
|
pending = /* @__PURE__ */ new Map();
|
|
12906
12907
|
socketPath;
|
|
12907
12908
|
static isDaemonRunning(socketPath = resolveClientSocket()) {
|
|
12908
|
-
return (0,
|
|
12909
|
+
return (0, import_node_fs7.existsSync)(socketPath);
|
|
12909
12910
|
}
|
|
12910
12911
|
async connect(timeoutMs = 2e3) {
|
|
12911
|
-
if (!(0,
|
|
12912
|
+
if (!(0, import_node_fs7.existsSync)(this.socketPath)) {
|
|
12912
12913
|
throw new Error(`threatcrushd socket not found at ${this.socketPath}`);
|
|
12913
12914
|
}
|
|
12914
12915
|
return new Promise((resolve6, reject) => {
|
|
@@ -13058,6 +13059,7 @@ function initialState(now = Date.now()) {
|
|
|
13058
13059
|
threatIndex: 0,
|
|
13059
13060
|
banIndex: 0,
|
|
13060
13061
|
notice: null,
|
|
13062
|
+
lastError: null,
|
|
13061
13063
|
busy: false,
|
|
13062
13064
|
moduleIndex: 0,
|
|
13063
13065
|
moduleFilter: null,
|
|
@@ -13132,7 +13134,14 @@ function reducer(state, action) {
|
|
|
13132
13134
|
}
|
|
13133
13135
|
};
|
|
13134
13136
|
case "connection_lost":
|
|
13135
|
-
return {
|
|
13137
|
+
return {
|
|
13138
|
+
...state,
|
|
13139
|
+
connection: "lost",
|
|
13140
|
+
connectionLabel: action.label,
|
|
13141
|
+
daemon: null,
|
|
13142
|
+
watchdog: OFF_WATCHDOG,
|
|
13143
|
+
lastError: action.label.startsWith("demo mode") ? state.lastError : action.label
|
|
13144
|
+
};
|
|
13136
13145
|
case "event": {
|
|
13137
13146
|
if (state.paused) {
|
|
13138
13147
|
return { ...state, parked: [...state.parked, action.event].slice(-FEED_LIMIT) };
|
|
@@ -13213,8 +13222,9 @@ function reducer(state, action) {
|
|
|
13213
13222
|
notice: {
|
|
13214
13223
|
text: action.text,
|
|
13215
13224
|
tone: action.tone,
|
|
13216
|
-
until: (action.now ?? Date.now()) + NOTICE_MS
|
|
13217
|
-
}
|
|
13225
|
+
until: (action.now ?? Date.now()) + (action.tone === "error" ? ERROR_NOTICE_MS : NOTICE_MS)
|
|
13226
|
+
},
|
|
13227
|
+
lastError: action.tone === "error" ? action.text : state.lastError
|
|
13218
13228
|
};
|
|
13219
13229
|
case "busy":
|
|
13220
13230
|
return { ...state, busy: action.busy };
|
|
@@ -13305,12 +13315,13 @@ function formatCount(n) {
|
|
|
13305
13315
|
if (n < 1e6) return `${(n / 1e3).toFixed(n < 1e4 ? 1 : 0)}k`;
|
|
13306
13316
|
return `${(n / 1e6).toFixed(1)}M`;
|
|
13307
13317
|
}
|
|
13308
|
-
var FOCUS_ORDER, NOTICE_MS, OFF_WATCHDOG, TIMELINE_SLOTS, FEED_LIMIT, SEVERITIES;
|
|
13318
|
+
var FOCUS_ORDER, NOTICE_MS, ERROR_NOTICE_MS, OFF_WATCHDOG, TIMELINE_SLOTS, FEED_LIMIT, SEVERITIES;
|
|
13309
13319
|
var init_state = __esm({
|
|
13310
13320
|
"src/tui/state.ts"() {
|
|
13311
13321
|
"use strict";
|
|
13312
13322
|
FOCUS_ORDER = ["feed", "modules", "threats", "bans"];
|
|
13313
13323
|
NOTICE_MS = 6e3;
|
|
13324
|
+
ERROR_NOTICE_MS = 3e4;
|
|
13314
13325
|
OFF_WATCHDOG = { on: false, logged: 0, answered: 0, errored: 0 };
|
|
13315
13326
|
TIMELINE_SLOTS = 60;
|
|
13316
13327
|
FEED_LIMIT = 500;
|
|
@@ -13812,8 +13823,10 @@ function throughputPanel(parent, state, theme) {
|
|
|
13812
13823
|
}
|
|
13813
13824
|
);
|
|
13814
13825
|
}
|
|
13815
|
-
function footer(ui, state, theme) {
|
|
13826
|
+
function footer(ui, state, theme, options = {}) {
|
|
13816
13827
|
const onFeed = state.focus === "feed";
|
|
13828
|
+
const notice = state.notice;
|
|
13829
|
+
const copyNotice = notice && options.onCopy ? () => options.onCopy?.(notice.text) : void 0;
|
|
13817
13830
|
ui.statusBar({
|
|
13818
13831
|
size: 1,
|
|
13819
13832
|
items: [
|
|
@@ -13826,12 +13839,18 @@ function footer(ui, state, theme) {
|
|
|
13826
13839
|
{ key: "n", label: state.showNoise ? "hide 4xx" : "show 4xx" },
|
|
13827
13840
|
// Only offer the cron toggle once a cron line has actually shown up, or
|
|
13828
13841
|
// while it is muting, so the hint never advertises a key with nothing to act on.
|
|
13829
|
-
...state.muteCron || state.events.some(isCronLine) ? [{ key: "c", label: state.muteCron ? "show cron" : "mute cron" }] : []
|
|
13842
|
+
...state.muteCron || state.events.some(isCronLine) ? [{ key: "c", label: state.muteCron ? "show cron" : "mute cron" }] : [],
|
|
13843
|
+
...state.lastError ? [{ key: "y", label: "copy error" }] : []
|
|
13830
13844
|
],
|
|
13831
13845
|
right: [
|
|
13832
13846
|
// The notice is the answer to "did my keypress do anything", so it takes
|
|
13833
13847
|
// the slot for as long as it lives.
|
|
13834
|
-
|
|
13848
|
+
// Click it to copy the text: an error is something to paste somewhere.
|
|
13849
|
+
{
|
|
13850
|
+
label: notice ? notice.tone === "error" && copyNotice ? `${notice.text} \u29C9` : notice.text : "",
|
|
13851
|
+
color: notice?.tone === "error" ? severityColors.high : theme.success,
|
|
13852
|
+
onPress: copyNotice
|
|
13853
|
+
},
|
|
13835
13854
|
{
|
|
13836
13855
|
label: state.paused && state.parked.length > 0 ? `${state.parked.length} held` : "",
|
|
13837
13856
|
color: severityColors.medium
|
|
@@ -13864,7 +13883,7 @@ function renderDashboard(ui, state, theme, options = {}) {
|
|
|
13864
13883
|
throughputPanel(right, state, theme);
|
|
13865
13884
|
});
|
|
13866
13885
|
});
|
|
13867
|
-
footer(col, state, theme);
|
|
13886
|
+
footer(col, state, theme, options);
|
|
13868
13887
|
});
|
|
13869
13888
|
}
|
|
13870
13889
|
var init_view = __esm({
|
|
@@ -13920,6 +13939,15 @@ async function startDashboard(options = {}) {
|
|
|
13920
13939
|
state = reducer(state, action);
|
|
13921
13940
|
app.invalidate();
|
|
13922
13941
|
};
|
|
13942
|
+
const copyText = (text) => {
|
|
13943
|
+
if (!text) return;
|
|
13944
|
+
try {
|
|
13945
|
+
process.stdout.write(clipboardSequence(text, !!process.env.TMUX));
|
|
13946
|
+
dispatch({ type: "notice", text: `copied to clipboard: ${text.length > 60 ? `${text.slice(0, 57)}...` : text}`, tone: "ok" });
|
|
13947
|
+
} catch (err) {
|
|
13948
|
+
dispatch({ type: "notice", text: `copy failed: ${err.message}`, tone: "error" });
|
|
13949
|
+
}
|
|
13950
|
+
};
|
|
13923
13951
|
let client = null;
|
|
13924
13952
|
let stopped = false;
|
|
13925
13953
|
const timers = [];
|
|
@@ -14098,6 +14126,9 @@ async function startDashboard(options = {}) {
|
|
|
14098
14126
|
case "c":
|
|
14099
14127
|
dispatch({ type: "toggle_cron" });
|
|
14100
14128
|
break;
|
|
14129
|
+
case "y":
|
|
14130
|
+
copyText(state.notice?.tone === "error" ? state.notice.text : state.lastError ?? state.notice?.text ?? "");
|
|
14131
|
+
break;
|
|
14101
14132
|
case "tab":
|
|
14102
14133
|
dispatch({ type: "focus_next" });
|
|
14103
14134
|
break;
|
|
@@ -14134,6 +14165,7 @@ async function startDashboard(options = {}) {
|
|
|
14134
14165
|
app.render(({ ui, theme }) => {
|
|
14135
14166
|
renderDashboard(ui, state, theme, {
|
|
14136
14167
|
demo: options.demo,
|
|
14168
|
+
onCopy: copyText,
|
|
14137
14169
|
onFeedScroll: (delta) => dispatch({ type: "scroll", delta: -delta }),
|
|
14138
14170
|
// One click focuses the panel, picks the row, and filters the feed to that
|
|
14139
14171
|
// source — "what has this one IP done" in a single click. Clicking the
|
|
@@ -16340,22 +16372,90 @@ var source_default = chalk;
|
|
|
16340
16372
|
// src/index.ts
|
|
16341
16373
|
var import_readline = __toESM(require("readline"));
|
|
16342
16374
|
var import_node_child_process15 = require("child_process");
|
|
16343
|
-
var
|
|
16375
|
+
var import_node_fs40 = require("fs");
|
|
16344
16376
|
var import_node_path27 = require("path");
|
|
16345
16377
|
var import_node_os11 = require("os");
|
|
16346
16378
|
|
|
16347
16379
|
// src/commands/monitor.ts
|
|
16348
|
-
var
|
|
16349
|
-
var import_node_readline = require("readline");
|
|
16380
|
+
var import_node_fs8 = require("fs");
|
|
16350
16381
|
|
|
16351
|
-
// src/core/
|
|
16382
|
+
// src/core/log-tail.ts
|
|
16352
16383
|
var import_node_fs = require("fs");
|
|
16384
|
+
var LogTail = class {
|
|
16385
|
+
/** Starts at the end of the file: history is not replayed. */
|
|
16386
|
+
constructor(path, fromStart = false) {
|
|
16387
|
+
this.path = path;
|
|
16388
|
+
try {
|
|
16389
|
+
const st = (0, import_node_fs.statSync)(path);
|
|
16390
|
+
this.pos = fromStart ? 0 : st.size;
|
|
16391
|
+
this.inode = st.ino;
|
|
16392
|
+
} catch {
|
|
16393
|
+
this.pos = 0;
|
|
16394
|
+
}
|
|
16395
|
+
}
|
|
16396
|
+
path;
|
|
16397
|
+
pos;
|
|
16398
|
+
partial = "";
|
|
16399
|
+
inode;
|
|
16400
|
+
get position() {
|
|
16401
|
+
return this.pos;
|
|
16402
|
+
}
|
|
16403
|
+
/**
|
|
16404
|
+
* Hands each complete new line to onLine. A line still being written (no
|
|
16405
|
+
* newline yet) waits for the next poll. Rotation (a new inode, or a file
|
|
16406
|
+
* shorter than our position) restarts at the top of the new file.
|
|
16407
|
+
* At most maxBytes are read per poll, so a burst is spread over polls
|
|
16408
|
+
* instead of blocking the daemon.
|
|
16409
|
+
*/
|
|
16410
|
+
poll(onLine, maxBytes = 4 << 20) {
|
|
16411
|
+
let fd;
|
|
16412
|
+
try {
|
|
16413
|
+
fd = (0, import_node_fs.openSync)(this.path, "r");
|
|
16414
|
+
} catch {
|
|
16415
|
+
return 0;
|
|
16416
|
+
}
|
|
16417
|
+
try {
|
|
16418
|
+
const st = (0, import_node_fs.fstatSync)(fd);
|
|
16419
|
+
if (this.inode !== void 0 && st.ino !== this.inode || st.size < this.pos) {
|
|
16420
|
+
this.pos = 0;
|
|
16421
|
+
this.partial = "";
|
|
16422
|
+
}
|
|
16423
|
+
this.inode = st.ino;
|
|
16424
|
+
const end = Math.min(st.size, this.pos + maxBytes);
|
|
16425
|
+
if (end <= this.pos) return 0;
|
|
16426
|
+
const buf = Buffer.allocUnsafe(Math.min(end - this.pos, 1 << 20));
|
|
16427
|
+
let lines = 0;
|
|
16428
|
+
while (this.pos < end) {
|
|
16429
|
+
const want = Math.min(buf.length, end - this.pos);
|
|
16430
|
+
const n = (0, import_node_fs.readSync)(fd, buf, 0, want, this.pos);
|
|
16431
|
+
if (n <= 0) break;
|
|
16432
|
+
this.pos += n;
|
|
16433
|
+
const text = this.partial + buf.toString("utf-8", 0, n);
|
|
16434
|
+
const parts = text.split("\n");
|
|
16435
|
+
this.partial = parts.pop() ?? "";
|
|
16436
|
+
for (const line of parts) {
|
|
16437
|
+
if (line.trim()) {
|
|
16438
|
+
lines++;
|
|
16439
|
+
onLine(line);
|
|
16440
|
+
}
|
|
16441
|
+
}
|
|
16442
|
+
}
|
|
16443
|
+
if (this.partial.length > 1 << 20) this.partial = "";
|
|
16444
|
+
return lines;
|
|
16445
|
+
} finally {
|
|
16446
|
+
(0, import_node_fs.closeSync)(fd);
|
|
16447
|
+
}
|
|
16448
|
+
}
|
|
16449
|
+
};
|
|
16450
|
+
|
|
16451
|
+
// src/core/version.ts
|
|
16452
|
+
var import_node_fs2 = require("fs");
|
|
16353
16453
|
var import_node_path = require("path");
|
|
16354
16454
|
var FALLBACK_VERSION = "0.0.0";
|
|
16355
16455
|
function readPackageVersion() {
|
|
16356
16456
|
try {
|
|
16357
16457
|
const pkg = JSON.parse(
|
|
16358
|
-
(0,
|
|
16458
|
+
(0, import_node_fs2.readFileSync)((0, import_node_path.join)(__dirname, "..", "package.json"), "utf-8")
|
|
16359
16459
|
);
|
|
16360
16460
|
return pkg.version || FALLBACK_VERSION;
|
|
16361
16461
|
} catch {
|
|
@@ -22603,12 +22703,12 @@ var TRANSFORMS = {
|
|
|
22603
22703
|
};
|
|
22604
22704
|
|
|
22605
22705
|
// src/core/crs/libinjection.ts
|
|
22606
|
-
var
|
|
22706
|
+
var import_node_fs3 = require("fs");
|
|
22607
22707
|
var import_node_path2 = require("path");
|
|
22608
22708
|
var wasm;
|
|
22609
22709
|
function loadLibinjection() {
|
|
22610
22710
|
if (!wasm) {
|
|
22611
|
-
const bytes = (0,
|
|
22711
|
+
const bytes = (0, import_node_fs3.readFileSync)((0, import_node_path2.join)(__dirname, "libinjection", "libinjection.wasm"));
|
|
22612
22712
|
wasm = new WebAssembly.Instance(new WebAssembly.Module(bytes), {}).exports;
|
|
22613
22713
|
wasm._initialize();
|
|
22614
22714
|
}
|
|
@@ -23105,7 +23205,7 @@ function parseSyslogTimestamp(s) {
|
|
|
23105
23205
|
}
|
|
23106
23206
|
|
|
23107
23207
|
// src/core/config.ts
|
|
23108
|
-
var
|
|
23208
|
+
var import_node_fs4 = require("fs");
|
|
23109
23209
|
var import_node_path3 = require("path");
|
|
23110
23210
|
var import_toml = __toESM(require_toml());
|
|
23111
23211
|
var DEFAULT_CONFIG_PATH = "/etc/threatcrush/threatcrushd.conf";
|
|
@@ -23132,11 +23232,11 @@ var DEFAULT_CONFIG = {
|
|
|
23132
23232
|
};
|
|
23133
23233
|
function loadConfig(configPath) {
|
|
23134
23234
|
const path = configPath || DEFAULT_CONFIG_PATH;
|
|
23135
|
-
if (!(0,
|
|
23235
|
+
if (!(0, import_node_fs4.existsSync)(path)) {
|
|
23136
23236
|
return { ...DEFAULT_CONFIG };
|
|
23137
23237
|
}
|
|
23138
23238
|
try {
|
|
23139
|
-
const raw = (0,
|
|
23239
|
+
const raw = (0, import_node_fs4.readFileSync)(path, "utf-8");
|
|
23140
23240
|
const parsed = import_toml.default.parse(raw);
|
|
23141
23241
|
return {
|
|
23142
23242
|
daemon: { ...DEFAULT_CONFIG.daemon, ...parsed.daemon },
|
|
@@ -23147,6 +23247,7 @@ function loadConfig(configPath) {
|
|
|
23147
23247
|
detection: parsed.detection,
|
|
23148
23248
|
cloud: parsed.cloud,
|
|
23149
23249
|
watchdog: parsed.watchdog,
|
|
23250
|
+
storage: parsed.storage,
|
|
23150
23251
|
license: parsed.license
|
|
23151
23252
|
};
|
|
23152
23253
|
} catch {
|
|
@@ -23156,13 +23257,13 @@ function loadConfig(configPath) {
|
|
|
23156
23257
|
function loadModuleConfigs(confDir) {
|
|
23157
23258
|
const dir = confDir || DEFAULT_CONFDIR;
|
|
23158
23259
|
const configs = /* @__PURE__ */ new Map();
|
|
23159
|
-
if (!(0,
|
|
23260
|
+
if (!(0, import_node_fs4.existsSync)(dir)) {
|
|
23160
23261
|
return configs;
|
|
23161
23262
|
}
|
|
23162
|
-
const files = (0,
|
|
23263
|
+
const files = (0, import_node_fs4.readdirSync)(dir).filter((f) => f.endsWith(".conf"));
|
|
23163
23264
|
for (const file of files) {
|
|
23164
23265
|
try {
|
|
23165
|
-
const raw = (0,
|
|
23266
|
+
const raw = (0, import_node_fs4.readFileSync)((0, import_node_path3.join)(dir, file), "utf-8");
|
|
23166
23267
|
const parsed = import_toml.default.parse(raw);
|
|
23167
23268
|
for (const [name, config] of Object.entries(parsed)) {
|
|
23168
23269
|
configs.set(name, config);
|
|
@@ -23283,35 +23384,70 @@ function getRecentEvents(limit = 50) {
|
|
|
23283
23384
|
`).all(limit);
|
|
23284
23385
|
return rows.map(rowToEvent);
|
|
23285
23386
|
}
|
|
23387
|
+
var RECENT_WINDOW = 2e5;
|
|
23388
|
+
function eventsEverRecorded(database) {
|
|
23389
|
+
const row = database.prepare(`SELECT seq FROM sqlite_sequence WHERE name = 'events'`).get();
|
|
23390
|
+
return row?.seq ?? 0;
|
|
23391
|
+
}
|
|
23286
23392
|
function getEventCount(since) {
|
|
23287
23393
|
const database = tryDb();
|
|
23288
23394
|
if (!database) return 0;
|
|
23289
23395
|
if (since) {
|
|
23290
|
-
return database.prepare(
|
|
23396
|
+
return database.prepare(
|
|
23397
|
+
`SELECT COUNT(*) as count FROM (SELECT timestamp FROM events ORDER BY id DESC LIMIT ?) WHERE timestamp >= ?`
|
|
23398
|
+
).get(RECENT_WINDOW, since.toISOString()).count;
|
|
23291
23399
|
}
|
|
23292
|
-
return database
|
|
23400
|
+
return eventsEverRecorded(database);
|
|
23293
23401
|
}
|
|
23294
23402
|
function getThreatCount(since) {
|
|
23295
23403
|
const database = tryDb();
|
|
23296
23404
|
if (!database) return 0;
|
|
23297
23405
|
const severities = "('medium','high','critical')";
|
|
23298
|
-
|
|
23299
|
-
return database.prepare(
|
|
23300
|
-
`SELECT COUNT(*) as count FROM events WHERE severity IN ${severities} AND timestamp >= ?`
|
|
23301
|
-
).get(since.toISOString()).count;
|
|
23302
|
-
}
|
|
23406
|
+
const cutoff = since ? since.toISOString() : "";
|
|
23303
23407
|
return database.prepare(
|
|
23304
|
-
`SELECT COUNT(*) as count FROM events
|
|
23305
|
-
|
|
23408
|
+
`SELECT COUNT(*) as count FROM (SELECT severity, timestamp FROM events ORDER BY id DESC LIMIT ?)
|
|
23409
|
+
WHERE severity IN ${severities} AND timestamp >= ?`
|
|
23410
|
+
).get(RECENT_WINDOW, cutoff).count;
|
|
23306
23411
|
}
|
|
23307
23412
|
function getTopSources(limit = 10) {
|
|
23308
23413
|
const database = tryDb();
|
|
23309
23414
|
if (!database) return [];
|
|
23310
23415
|
return database.prepare(`
|
|
23311
|
-
SELECT source_ip as ip, COUNT(*) as count
|
|
23416
|
+
SELECT source_ip as ip, COUNT(*) as count
|
|
23417
|
+
FROM (SELECT source_ip FROM events ORDER BY id DESC LIMIT ?)
|
|
23312
23418
|
WHERE source_ip IS NOT NULL
|
|
23313
23419
|
GROUP BY source_ip ORDER BY count DESC LIMIT ?
|
|
23314
|
-
`).all(limit);
|
|
23420
|
+
`).all(RECENT_WINDOW, limit);
|
|
23421
|
+
}
|
|
23422
|
+
var DEFAULT_EVENT_RETENTION_SECONDS = 14 * 86400;
|
|
23423
|
+
var DEFAULT_MAX_EVENTS = 2e6;
|
|
23424
|
+
function pruneEvents(opts = {}) {
|
|
23425
|
+
const database = tryDb();
|
|
23426
|
+
if (!database) return 0;
|
|
23427
|
+
const retention = opts.retentionSeconds ?? DEFAULT_EVENT_RETENTION_SECONDS;
|
|
23428
|
+
const maxEvents = opts.maxEvents ?? DEFAULT_MAX_EVENTS;
|
|
23429
|
+
const batch = opts.batch ?? 5e3;
|
|
23430
|
+
const maxBatches = opts.maxBatches ?? 20;
|
|
23431
|
+
const cutoff = new Date((opts.now ?? Date.now()) - retention * 1e3).toISOString();
|
|
23432
|
+
const byAge = database.prepare(
|
|
23433
|
+
`DELETE FROM events WHERE id IN (SELECT id FROM events WHERE timestamp < ? ORDER BY id LIMIT ?)`
|
|
23434
|
+
);
|
|
23435
|
+
const lowest = database.prepare(`SELECT MIN(id) as lo, MAX(id) as hi FROM events`);
|
|
23436
|
+
const byCount = database.prepare(`DELETE FROM events WHERE id < ?`);
|
|
23437
|
+
let deleted = 0;
|
|
23438
|
+
for (let i = 0; i < maxBatches; i++) {
|
|
23439
|
+
const n = byAge.run(cutoff, batch).changes;
|
|
23440
|
+
deleted += n;
|
|
23441
|
+
if (n < batch) break;
|
|
23442
|
+
}
|
|
23443
|
+
for (let i = 0; i < maxBatches; i++) {
|
|
23444
|
+
const { lo, hi } = lowest.get();
|
|
23445
|
+
if (lo === null || hi === null || hi - lo + 1 <= maxEvents) break;
|
|
23446
|
+
const n = byCount.run(Math.min(lo + batch, hi - maxEvents + 1)).changes;
|
|
23447
|
+
deleted += n;
|
|
23448
|
+
if (n === 0) break;
|
|
23449
|
+
}
|
|
23450
|
+
return deleted;
|
|
23315
23451
|
}
|
|
23316
23452
|
function getModuleState(module2, key) {
|
|
23317
23453
|
const database = db || initStateDB();
|
|
@@ -23368,9 +23504,9 @@ async function monitorCommand(options) {
|
|
|
23368
23504
|
const unreadable = [];
|
|
23369
23505
|
for (const s of LOG_SOURCES) {
|
|
23370
23506
|
if (moduleFilter && !moduleFilter.includes(s.name)) continue;
|
|
23371
|
-
if (!(0,
|
|
23507
|
+
if (!(0, import_node_fs8.existsSync)(s.path)) continue;
|
|
23372
23508
|
try {
|
|
23373
|
-
(0,
|
|
23509
|
+
(0, import_node_fs8.accessSync)(s.path, import_node_fs8.constants.R_OK);
|
|
23374
23510
|
availableSources.push(s);
|
|
23375
23511
|
} catch {
|
|
23376
23512
|
unreadable.push(s);
|
|
@@ -23387,11 +23523,11 @@ async function monitorCommand(options) {
|
|
|
23387
23523
|
logger.warn("No readable log files found to monitor.");
|
|
23388
23524
|
logger.info("Available log paths checked:");
|
|
23389
23525
|
for (const s of LOG_SOURCES) {
|
|
23390
|
-
const exists = (0,
|
|
23526
|
+
const exists = (0, import_node_fs8.existsSync)(s.path);
|
|
23391
23527
|
let readable = false;
|
|
23392
23528
|
if (exists) {
|
|
23393
23529
|
try {
|
|
23394
|
-
(0,
|
|
23530
|
+
(0, import_node_fs8.accessSync)(s.path, import_node_fs8.constants.R_OK);
|
|
23395
23531
|
readable = true;
|
|
23396
23532
|
} catch {
|
|
23397
23533
|
readable = false;
|
|
@@ -23424,31 +23560,14 @@ async function monitorCommand(options) {
|
|
|
23424
23560
|
}
|
|
23425
23561
|
function tailLog(source) {
|
|
23426
23562
|
const { path, name, category } = source;
|
|
23427
|
-
const
|
|
23428
|
-
let
|
|
23563
|
+
const tail = new LogTail(path);
|
|
23564
|
+
let warned = false;
|
|
23429
23565
|
const checkForNewData = () => {
|
|
23430
23566
|
try {
|
|
23431
|
-
|
|
23432
|
-
|
|
23433
|
-
|
|
23434
|
-
|
|
23435
|
-
}
|
|
23436
|
-
const stream = (0, import_node_fs7.createReadStream)(path, { start: position, encoding: "utf-8" });
|
|
23437
|
-
stream.on("error", (err) => {
|
|
23438
|
-
logger.warn(`stopped tailing ${path}: ${err.code || err.message}`);
|
|
23439
|
-
position = currentStat.size;
|
|
23440
|
-
});
|
|
23441
|
-
const rl = (0, import_node_readline.createInterface)({ input: stream });
|
|
23442
|
-
rl.on("error", () => {
|
|
23443
|
-
});
|
|
23444
|
-
rl.on("line", (line) => {
|
|
23445
|
-
if (!line.trim()) return;
|
|
23446
|
-
processLine(line, name, category);
|
|
23447
|
-
});
|
|
23448
|
-
rl.on("close", () => {
|
|
23449
|
-
position = currentStat.size;
|
|
23450
|
-
});
|
|
23451
|
-
} catch {
|
|
23567
|
+
tail.poll((line) => processLine(line, name, category));
|
|
23568
|
+
} catch (err) {
|
|
23569
|
+
if (!warned) logger.warn(`cannot read ${path}: ${err.code || err.message}`);
|
|
23570
|
+
warned = true;
|
|
23452
23571
|
}
|
|
23453
23572
|
};
|
|
23454
23573
|
setInterval(checkForNewData, 1e3);
|
|
@@ -23550,12 +23669,12 @@ async function runDemoMode() {
|
|
|
23550
23669
|
}
|
|
23551
23670
|
|
|
23552
23671
|
// src/commands/watchdog.ts
|
|
23553
|
-
var
|
|
23672
|
+
var import_node_fs10 = require("fs");
|
|
23554
23673
|
var import_node_path7 = require("path");
|
|
23555
23674
|
init_paths();
|
|
23556
23675
|
|
|
23557
23676
|
// src/core/watchdog.ts
|
|
23558
|
-
var
|
|
23677
|
+
var import_node_fs9 = require("fs");
|
|
23559
23678
|
var import_node_crypto2 = require("crypto");
|
|
23560
23679
|
var import_node_path6 = require("path");
|
|
23561
23680
|
var WATCHDOG_DIRNAME = "watchdog";
|
|
@@ -23642,7 +23761,7 @@ function dedupeKey(r) {
|
|
|
23642
23761
|
}
|
|
23643
23762
|
function isShared(dir) {
|
|
23644
23763
|
try {
|
|
23645
|
-
return ((0,
|
|
23764
|
+
return ((0, import_node_fs9.statSync)(dir).mode & 1024) !== 0;
|
|
23646
23765
|
} catch {
|
|
23647
23766
|
return false;
|
|
23648
23767
|
}
|
|
@@ -23658,10 +23777,10 @@ var WatchdogLog = class {
|
|
|
23658
23777
|
this.dir = dir;
|
|
23659
23778
|
this.file = (0, import_node_path6.join)(dir, LOG_FILE);
|
|
23660
23779
|
this.shareGid = options.shareGid;
|
|
23661
|
-
(0,
|
|
23780
|
+
(0, import_node_fs9.mkdirSync)(dir, { recursive: true, mode: this.shareGid === void 0 ? 448 : 1528 });
|
|
23662
23781
|
if (this.shareGid !== void 0) {
|
|
23663
|
-
(0,
|
|
23664
|
-
(0,
|
|
23782
|
+
(0, import_node_fs9.chownSync)(dir, process.getuid?.() ?? 0, this.shareGid);
|
|
23783
|
+
(0, import_node_fs9.chmodSync)(dir, 1528);
|
|
23665
23784
|
}
|
|
23666
23785
|
for (const r of readTail(this.file, 512 * 1024)) this.remember(dedupeKey(r));
|
|
23667
23786
|
}
|
|
@@ -23690,16 +23809,16 @@ var WatchdogLog = class {
|
|
|
23690
23809
|
const chunk = this.buffer.join("\n") + "\n";
|
|
23691
23810
|
this.buffer = [];
|
|
23692
23811
|
try {
|
|
23693
|
-
if ((0,
|
|
23694
|
-
(0,
|
|
23812
|
+
if ((0, import_node_fs9.statSync)(this.file).size > ROTATE_BYTES) {
|
|
23813
|
+
(0, import_node_fs9.renameSync)(this.file, (0, import_node_path6.join)(this.dir, ROTATED_FILE));
|
|
23695
23814
|
}
|
|
23696
23815
|
} catch {
|
|
23697
23816
|
}
|
|
23698
|
-
(0,
|
|
23817
|
+
(0, import_node_fs9.appendFileSync)(this.file, chunk, { mode: this.shareGid === void 0 ? 384 : 432 });
|
|
23699
23818
|
if (this.shareGid !== void 0) {
|
|
23700
23819
|
try {
|
|
23701
|
-
(0,
|
|
23702
|
-
(0,
|
|
23820
|
+
(0, import_node_fs9.chownSync)(this.file, process.getuid?.() ?? 0, this.shareGid);
|
|
23821
|
+
(0, import_node_fs9.chmodSync)(this.file, 432);
|
|
23703
23822
|
} catch {
|
|
23704
23823
|
}
|
|
23705
23824
|
}
|
|
@@ -23720,50 +23839,50 @@ function parseLines(text) {
|
|
|
23720
23839
|
function readTail(file, bytes) {
|
|
23721
23840
|
let fd;
|
|
23722
23841
|
try {
|
|
23723
|
-
fd = (0,
|
|
23842
|
+
fd = (0, import_node_fs9.openSync)(file, "r");
|
|
23724
23843
|
} catch (err) {
|
|
23725
23844
|
if (err.code === "ENOENT") return [];
|
|
23726
23845
|
throw err;
|
|
23727
23846
|
}
|
|
23728
23847
|
try {
|
|
23729
|
-
const size = (0,
|
|
23848
|
+
const size = (0, import_node_fs9.fstatSync)(fd).size;
|
|
23730
23849
|
const start = Math.max(0, size - bytes);
|
|
23731
23850
|
const buf = Buffer.alloc(size - start);
|
|
23732
|
-
(0,
|
|
23851
|
+
(0, import_node_fs9.readSync)(fd, buf, 0, buf.length, start);
|
|
23733
23852
|
let text = buf.toString("utf8");
|
|
23734
23853
|
if (start > 0) text = text.slice(text.indexOf("\n") + 1);
|
|
23735
23854
|
return parseLines(text);
|
|
23736
23855
|
} finally {
|
|
23737
|
-
(0,
|
|
23856
|
+
(0, import_node_fs9.closeSync)(fd);
|
|
23738
23857
|
}
|
|
23739
23858
|
}
|
|
23740
23859
|
function readAttacks(dir) {
|
|
23741
23860
|
const out = [];
|
|
23742
23861
|
for (const name of [ROTATED_FILE, LOG_FILE]) {
|
|
23743
23862
|
const file = (0, import_node_path6.join)(dir, name);
|
|
23744
|
-
if ((0,
|
|
23863
|
+
if ((0, import_node_fs9.existsSync)(file)) out.push(...parseLines((0, import_node_fs9.readFileSync)(file, "utf8")));
|
|
23745
23864
|
}
|
|
23746
23865
|
return out;
|
|
23747
23866
|
}
|
|
23748
23867
|
function readTriage(dir) {
|
|
23749
23868
|
const file = (0, import_node_path6.join)(dir, TRIAGE_FILE);
|
|
23750
|
-
if (!(0,
|
|
23869
|
+
if (!(0, import_node_fs9.existsSync)(file)) return {};
|
|
23751
23870
|
try {
|
|
23752
|
-
return JSON.parse((0,
|
|
23871
|
+
return JSON.parse((0, import_node_fs9.readFileSync)(file, "utf8"));
|
|
23753
23872
|
} catch {
|
|
23754
23873
|
return {};
|
|
23755
23874
|
}
|
|
23756
23875
|
}
|
|
23757
23876
|
function writeTriage(dir, map) {
|
|
23758
|
-
(0,
|
|
23877
|
+
(0, import_node_fs9.mkdirSync)(dir, { recursive: true });
|
|
23759
23878
|
const file = (0, import_node_path6.join)(dir, TRIAGE_FILE);
|
|
23760
23879
|
const mode = isShared(dir) ? 432 : 384;
|
|
23761
|
-
(0,
|
|
23880
|
+
(0, import_node_fs9.writeFileSync)(`${file}.tmp`, JSON.stringify(map, null, 2) + "\n", { mode });
|
|
23762
23881
|
try {
|
|
23763
|
-
(0,
|
|
23882
|
+
(0, import_node_fs9.chmodSync)(`${file}.tmp`, mode);
|
|
23764
23883
|
} catch {
|
|
23765
23884
|
}
|
|
23766
|
-
(0,
|
|
23885
|
+
(0, import_node_fs9.renameSync)(`${file}.tmp`, file);
|
|
23767
23886
|
}
|
|
23768
23887
|
function setTriage(dir, id, update, now = /* @__PURE__ */ new Date()) {
|
|
23769
23888
|
const map = readTriage(dir);
|
|
@@ -23907,7 +24026,7 @@ function resolveWatchdogDir(dir) {
|
|
|
23907
24026
|
if (dir) return dir;
|
|
23908
24027
|
if (process.env.THREATCRUSH_WATCHDOG_DIR) return process.env.THREATCRUSH_WATCHDOG_DIR;
|
|
23909
24028
|
const system = (0, import_node_path7.join)("/var/lib/threatcrush", WATCHDOG_DIRNAME);
|
|
23910
|
-
if ((0,
|
|
24029
|
+
if ((0, import_node_fs10.existsSync)(system)) return system;
|
|
23911
24030
|
return (0, import_node_path7.join)(PATHS.stateDir, WATCHDOG_DIRNAME);
|
|
23912
24031
|
}
|
|
23913
24032
|
function load(dir) {
|
|
@@ -23944,7 +24063,7 @@ function watchdogListCommand(opts) {
|
|
|
23944
24063
|
return;
|
|
23945
24064
|
}
|
|
23946
24065
|
if (findings.length === 0) {
|
|
23947
|
-
const empty = (0,
|
|
24066
|
+
const empty = (0, import_node_fs10.existsSync)((0, import_node_path7.join)(dir, "attacks.jsonl")) ? "Nothing to triage." + (opts.all ? "" : " (refused and triaged findings hidden; --all shows them)") : `No watchdog log at ${dir}. The daemon writes it: threatcrush start (or upgrade + restart an older daemon).`;
|
|
23948
24067
|
console.log(source_default.dim(empty));
|
|
23949
24068
|
return;
|
|
23950
24069
|
}
|
|
@@ -24011,7 +24130,7 @@ function watchdogPathCommand(opts) {
|
|
|
24011
24130
|
}
|
|
24012
24131
|
|
|
24013
24132
|
// src/commands/scan.ts
|
|
24014
|
-
var
|
|
24133
|
+
var import_node_fs13 = require("fs");
|
|
24015
24134
|
var import_node_path11 = require("path");
|
|
24016
24135
|
|
|
24017
24136
|
// ../../node_modules/.pnpm/ora@8.2.0/node_modules/ora/index.js
|
|
@@ -27926,7 +28045,7 @@ function meetsFailThreshold(findings, threshold) {
|
|
|
27926
28045
|
}
|
|
27927
28046
|
|
|
27928
28047
|
// ../../packages/scan/src/node/walk.ts
|
|
27929
|
-
var
|
|
28048
|
+
var import_node_fs11 = require("fs");
|
|
27930
28049
|
var import_node_path8 = require("path");
|
|
27931
28050
|
function compileExcludes(patterns) {
|
|
27932
28051
|
const matchers = patterns.map((p) => p.trim()).filter((p) => p.length > 0 && !p.startsWith("#")).map(compilePattern);
|
|
@@ -27998,7 +28117,7 @@ function matchPrefix(parts, segs) {
|
|
|
27998
28117
|
}
|
|
27999
28118
|
function readIgnoreFile(root) {
|
|
28000
28119
|
try {
|
|
28001
|
-
return (0,
|
|
28120
|
+
return (0, import_node_fs11.readFileSync)((0, import_node_path8.join)(root, ".threatcrushignore"), "utf-8").split("\n");
|
|
28002
28121
|
} catch {
|
|
28003
28122
|
return [];
|
|
28004
28123
|
}
|
|
@@ -28014,7 +28133,7 @@ function scanPath(targetPath, options = {}) {
|
|
|
28014
28133
|
let excluded = 0;
|
|
28015
28134
|
const rootIsDirectory = (() => {
|
|
28016
28135
|
try {
|
|
28017
|
-
return (0,
|
|
28136
|
+
return (0, import_node_fs11.statSync)(targetPath).isDirectory();
|
|
28018
28137
|
} catch {
|
|
28019
28138
|
return true;
|
|
28020
28139
|
}
|
|
@@ -28035,26 +28154,26 @@ function scanPath(targetPath, options = {}) {
|
|
|
28035
28154
|
let handle;
|
|
28036
28155
|
let declared = null;
|
|
28037
28156
|
try {
|
|
28038
|
-
handle = (0,
|
|
28157
|
+
handle = (0, import_node_fs11.openSync)(fullPath, "r");
|
|
28039
28158
|
} catch {
|
|
28040
28159
|
unreadable.push(relativePath);
|
|
28041
28160
|
return;
|
|
28042
28161
|
}
|
|
28043
28162
|
try {
|
|
28044
|
-
if ((0,
|
|
28163
|
+
if ((0, import_node_fs11.fstatSync)(handle).size > maxFileBytes) return;
|
|
28045
28164
|
if (mayDeclareInterpreter) {
|
|
28046
28165
|
const prefix = Buffer.alloc(128);
|
|
28047
|
-
const read = (0,
|
|
28166
|
+
const read = (0, import_node_fs11.readSync)(handle, prefix, 0, prefix.length, 0);
|
|
28048
28167
|
declared = languageOfShebang(prefix.subarray(0, read).toString("utf-8").split("\n", 1)[0] ?? "");
|
|
28049
28168
|
if (!declared) return;
|
|
28050
28169
|
}
|
|
28051
|
-
text = (0,
|
|
28170
|
+
text = (0, import_node_fs11.readFileSync)(handle, "utf-8");
|
|
28052
28171
|
} catch {
|
|
28053
28172
|
unreadable.push(relativePath);
|
|
28054
28173
|
return;
|
|
28055
28174
|
} finally {
|
|
28056
28175
|
try {
|
|
28057
|
-
(0,
|
|
28176
|
+
(0, import_node_fs11.closeSync)(handle);
|
|
28058
28177
|
} catch {
|
|
28059
28178
|
}
|
|
28060
28179
|
}
|
|
@@ -28072,7 +28191,7 @@ function scanPath(targetPath, options = {}) {
|
|
|
28072
28191
|
const walk = (currentPath) => {
|
|
28073
28192
|
let entries;
|
|
28074
28193
|
try {
|
|
28075
|
-
entries = (0,
|
|
28194
|
+
entries = (0, import_node_fs11.readdirSync)(currentPath, { withFileTypes: true });
|
|
28076
28195
|
} catch {
|
|
28077
28196
|
unreadable.push(toRelative(walkRoot, currentPath));
|
|
28078
28197
|
return;
|
|
@@ -28139,17 +28258,17 @@ function toRelative(base, target) {
|
|
|
28139
28258
|
}
|
|
28140
28259
|
|
|
28141
28260
|
// ../../packages/scan/src/node/dependencies.ts
|
|
28142
|
-
var
|
|
28261
|
+
var import_node_fs12 = require("fs");
|
|
28143
28262
|
var import_node_path9 = require("path");
|
|
28144
28263
|
var MAX_DEPS_PER_LOCKFILE = 50;
|
|
28145
28264
|
async function scanDependencies(targetPath) {
|
|
28146
28265
|
const findings = [];
|
|
28147
28266
|
for (const { file, ecosystem, parse: parse2 } of LOCKFILES) {
|
|
28148
28267
|
const lockPath = (0, import_node_path9.join)(targetPath, file);
|
|
28149
|
-
if (!(0,
|
|
28268
|
+
if (!(0, import_node_fs12.existsSync)(lockPath)) continue;
|
|
28150
28269
|
let deps;
|
|
28151
28270
|
try {
|
|
28152
|
-
deps = dedupe(parse2((0,
|
|
28271
|
+
deps = dedupe(parse2((0, import_node_fs12.readFileSync)(lockPath, "utf-8")));
|
|
28153
28272
|
} catch {
|
|
28154
28273
|
continue;
|
|
28155
28274
|
}
|
|
@@ -28500,7 +28619,7 @@ function readVersion() {
|
|
|
28500
28619
|
(0, import_node_path11.join)(__dirname, "..", "..", "package.json")
|
|
28501
28620
|
]) {
|
|
28502
28621
|
try {
|
|
28503
|
-
return JSON.parse((0,
|
|
28622
|
+
return JSON.parse((0, import_node_fs13.readFileSync)(candidate, "utf-8")).version ?? "0.0.0";
|
|
28504
28623
|
} catch {
|
|
28505
28624
|
}
|
|
28506
28625
|
}
|
|
@@ -28567,7 +28686,7 @@ async function scanCommand(targetPath, options = {}) {
|
|
|
28567
28686
|
const say = machineReadable ? (line) => process.stderr.write(`${line}
|
|
28568
28687
|
`) : (line) => process.stdout.write(`${line}
|
|
28569
28688
|
`);
|
|
28570
|
-
if (!(0,
|
|
28689
|
+
if (!(0, import_node_fs13.existsSync)(targetPath)) {
|
|
28571
28690
|
say(source_default.red(`Scan target does not exist: ${targetPath}`));
|
|
28572
28691
|
process.exitCode = 2;
|
|
28573
28692
|
return failedResult(targetPath, `no such path: ${targetPath}`);
|
|
@@ -28677,8 +28796,8 @@ function emitMachineReadable(format, outcome, targetPath, options, say) {
|
|
|
28677
28796
|
const serialized = `${JSON.stringify(payload, null, 2)}
|
|
28678
28797
|
`;
|
|
28679
28798
|
if (options.output) {
|
|
28680
|
-
(0,
|
|
28681
|
-
(0,
|
|
28799
|
+
(0, import_node_fs13.mkdirSync)((0, import_node_path11.dirname)((0, import_node_path11.resolve)(options.output)), { recursive: true });
|
|
28800
|
+
(0, import_node_fs13.writeFileSync)(options.output, serialized, "utf-8");
|
|
28682
28801
|
say(
|
|
28683
28802
|
source_default.gray(
|
|
28684
28803
|
` ${format.toUpperCase()} written to ${options.output} (${outcome.findings.length} finding(s))`
|
|
@@ -28730,11 +28849,11 @@ function printHuman(outcome) {
|
|
|
28730
28849
|
}
|
|
28731
28850
|
|
|
28732
28851
|
// src/commands/init.ts
|
|
28733
|
-
var
|
|
28852
|
+
var import_node_fs15 = require("fs");
|
|
28734
28853
|
var import_node_child_process = require("child_process");
|
|
28735
28854
|
|
|
28736
28855
|
// src/core/cli-config.ts
|
|
28737
|
-
var
|
|
28856
|
+
var import_node_fs14 = require("fs");
|
|
28738
28857
|
var import_node_path12 = require("path");
|
|
28739
28858
|
var import_node_os4 = require("os");
|
|
28740
28859
|
var CLI_CONFIG_DIR = (0, import_node_path12.join)((0, import_node_os4.homedir)(), ".threatcrush");
|
|
@@ -28744,16 +28863,16 @@ function apiUrl() {
|
|
|
28744
28863
|
}
|
|
28745
28864
|
function readCliConfig() {
|
|
28746
28865
|
try {
|
|
28747
|
-
return JSON.parse((0,
|
|
28866
|
+
return JSON.parse((0, import_node_fs14.readFileSync)(CLI_CONFIG_PATH, "utf-8"));
|
|
28748
28867
|
} catch {
|
|
28749
28868
|
return {};
|
|
28750
28869
|
}
|
|
28751
28870
|
}
|
|
28752
28871
|
function writeCliConfig(config) {
|
|
28753
|
-
if (!(0,
|
|
28754
|
-
(0,
|
|
28872
|
+
if (!(0, import_node_fs14.existsSync)(CLI_CONFIG_DIR)) (0, import_node_fs14.mkdirSync)(CLI_CONFIG_DIR, { recursive: true });
|
|
28873
|
+
(0, import_node_fs14.writeFileSync)(CLI_CONFIG_PATH, JSON.stringify(config, null, 2), "utf-8");
|
|
28755
28874
|
try {
|
|
28756
|
-
(0,
|
|
28875
|
+
(0, import_node_fs14.chmodSync)(CLI_CONFIG_PATH, 384);
|
|
28757
28876
|
} catch {
|
|
28758
28877
|
}
|
|
28759
28878
|
}
|
|
@@ -28844,7 +28963,7 @@ async function cloudFetch(path, init = {}) {
|
|
|
28844
28963
|
}
|
|
28845
28964
|
|
|
28846
28965
|
// src/core/prompt.ts
|
|
28847
|
-
var
|
|
28966
|
+
var import_node_readline = __toESM(require("readline"));
|
|
28848
28967
|
var import_node_stream = require("stream");
|
|
28849
28968
|
function stdinIsInteractive() {
|
|
28850
28969
|
return Boolean(process.stdin.isTTY);
|
|
@@ -28852,7 +28971,7 @@ function stdinIsInteractive() {
|
|
|
28852
28971
|
var piped = null;
|
|
28853
28972
|
function pipedReader() {
|
|
28854
28973
|
if (piped) return piped;
|
|
28855
|
-
const rl =
|
|
28974
|
+
const rl = import_node_readline.default.createInterface({ input: process.stdin, terminal: false });
|
|
28856
28975
|
const reader = { rl, lines: [], waiting: null, ended: false };
|
|
28857
28976
|
rl.on("line", (line) => {
|
|
28858
28977
|
const waiting = reader.waiting;
|
|
@@ -28891,7 +29010,7 @@ function askTerminal(question, mask) {
|
|
|
28891
29010
|
const output = mask ? new import_node_stream.Writable({ write(_chunk, _enc, cb) {
|
|
28892
29011
|
cb();
|
|
28893
29012
|
} }) : process.stdout;
|
|
28894
|
-
const rl =
|
|
29013
|
+
const rl = import_node_readline.default.createInterface({ input: process.stdin, output, terminal: true });
|
|
28895
29014
|
return new Promise((resolve6) => {
|
|
28896
29015
|
let settled = false;
|
|
28897
29016
|
rl.on("close", () => {
|
|
@@ -29223,7 +29342,7 @@ function binaryExists(name) {
|
|
|
29223
29342
|
}
|
|
29224
29343
|
}
|
|
29225
29344
|
function findLogPath(paths) {
|
|
29226
|
-
return paths.find((p) => (0,
|
|
29345
|
+
return paths.find((p) => (0, import_node_fs15.existsSync)(p));
|
|
29227
29346
|
}
|
|
29228
29347
|
async function promptYesNo(question, fallback2) {
|
|
29229
29348
|
const hint = fallback2 ? "(Y/n)" : "(y/N)";
|
|
@@ -29367,11 +29486,11 @@ async function initCommand(opts = {}) {
|
|
|
29367
29486
|
}
|
|
29368
29487
|
} else {
|
|
29369
29488
|
const spinner2 = ora({ text: "Writing configuration files...", color: "green" }).start();
|
|
29370
|
-
(0,
|
|
29371
|
-
(0,
|
|
29372
|
-
(0,
|
|
29489
|
+
(0, import_node_fs15.mkdirSync)(confDDir, { recursive: true });
|
|
29490
|
+
(0, import_node_fs15.mkdirSync)("/var/log/threatcrush", { recursive: true });
|
|
29491
|
+
(0, import_node_fs15.mkdirSync)("/var/lib/threatcrush", { recursive: true });
|
|
29373
29492
|
const mainConfig = generateDefaultConfig(detected.map((d) => d.name));
|
|
29374
|
-
(0,
|
|
29493
|
+
(0, import_node_fs15.writeFileSync)(`${configDir}/threatcrushd.conf`, mainConfig);
|
|
29375
29494
|
for (const svc of detected) {
|
|
29376
29495
|
const svcDef = SERVICES_TO_DETECT.find((s) => s.name === svc.name);
|
|
29377
29496
|
if (!svcDef) continue;
|
|
@@ -29380,7 +29499,7 @@ async function initCommand(opts = {}) {
|
|
|
29380
29499
|
...svcDef.moduleConfig,
|
|
29381
29500
|
log_path: svc.logPath || svcDef.logPaths[0]
|
|
29382
29501
|
});
|
|
29383
|
-
(0,
|
|
29502
|
+
(0, import_node_fs15.writeFileSync)(`${confDDir}/${modName}.conf`, modConfig);
|
|
29384
29503
|
}
|
|
29385
29504
|
spinner2.succeed("Configuration written successfully");
|
|
29386
29505
|
console.log();
|
|
@@ -29398,8 +29517,8 @@ async function initCommand(opts = {}) {
|
|
|
29398
29517
|
}
|
|
29399
29518
|
function checkWriteAccess(dir) {
|
|
29400
29519
|
try {
|
|
29401
|
-
if (!(0,
|
|
29402
|
-
(0,
|
|
29520
|
+
if (!(0, import_node_fs15.existsSync)(dir)) {
|
|
29521
|
+
(0, import_node_fs15.mkdirSync)(dir, { recursive: true });
|
|
29403
29522
|
}
|
|
29404
29523
|
return true;
|
|
29405
29524
|
} catch {
|
|
@@ -29417,7 +29536,7 @@ var BUILTIN_MODULES = [
|
|
|
29417
29536
|
];
|
|
29418
29537
|
|
|
29419
29538
|
// src/core/module-loader.ts
|
|
29420
|
-
var
|
|
29539
|
+
var import_node_fs16 = require("fs");
|
|
29421
29540
|
var import_node_path13 = require("path");
|
|
29422
29541
|
var import_toml2 = __toESM(require_toml());
|
|
29423
29542
|
init_paths();
|
|
@@ -29429,19 +29548,19 @@ function discoverModules(moduleDir, confDir) {
|
|
|
29429
29548
|
(0, import_node_path13.resolve)(process.cwd(), "modules")
|
|
29430
29549
|
];
|
|
29431
29550
|
const builtinDir = (0, import_node_path13.resolve)(__dirname || ".", "..", "modules");
|
|
29432
|
-
if ((0,
|
|
29551
|
+
if ((0, import_node_fs16.existsSync)(builtinDir)) {
|
|
29433
29552
|
searchPaths.push(builtinDir);
|
|
29434
29553
|
}
|
|
29435
29554
|
for (const basePath of searchPaths) {
|
|
29436
|
-
if (!(0,
|
|
29437
|
-
const entries = (0,
|
|
29555
|
+
if (!(0, import_node_fs16.existsSync)(basePath)) continue;
|
|
29556
|
+
const entries = (0, import_node_fs16.readdirSync)(basePath, { withFileTypes: true });
|
|
29438
29557
|
for (const entry of entries) {
|
|
29439
29558
|
if (!entry.isDirectory()) continue;
|
|
29440
29559
|
const modPath = (0, import_node_path13.join)(basePath, entry.name);
|
|
29441
29560
|
const manifestPath = (0, import_node_path13.join)(modPath, "mod.toml");
|
|
29442
|
-
if (!(0,
|
|
29561
|
+
if (!(0, import_node_fs16.existsSync)(manifestPath)) continue;
|
|
29443
29562
|
try {
|
|
29444
|
-
const raw = (0,
|
|
29563
|
+
const raw = (0, import_node_fs16.readFileSync)(manifestPath, "utf-8");
|
|
29445
29564
|
const manifest = import_toml2.default.parse(raw);
|
|
29446
29565
|
const config = configs.get(manifest.module.name) || { enabled: true };
|
|
29447
29566
|
modules.push({
|
|
@@ -29467,21 +29586,21 @@ function discoverModules(moduleDir, confDir) {
|
|
|
29467
29586
|
init_ipc_client();
|
|
29468
29587
|
|
|
29469
29588
|
// src/daemon/pidfile.ts
|
|
29470
|
-
var
|
|
29589
|
+
var import_node_fs17 = require("fs");
|
|
29471
29590
|
init_paths();
|
|
29472
29591
|
function writePidFile() {
|
|
29473
29592
|
ensureRuntimeDirs();
|
|
29474
|
-
(0,
|
|
29593
|
+
(0, import_node_fs17.writeFileSync)(PATHS.pidFile, String(process.pid), "utf-8");
|
|
29475
29594
|
}
|
|
29476
29595
|
function readPidFile() {
|
|
29477
|
-
if (!(0,
|
|
29478
|
-
const raw = (0,
|
|
29596
|
+
if (!(0, import_node_fs17.existsSync)(PATHS.pidFile)) return null;
|
|
29597
|
+
const raw = (0, import_node_fs17.readFileSync)(PATHS.pidFile, "utf-8").trim();
|
|
29479
29598
|
const pid = parseInt(raw, 10);
|
|
29480
29599
|
return Number.isFinite(pid) ? pid : null;
|
|
29481
29600
|
}
|
|
29482
29601
|
function removePidFile() {
|
|
29483
29602
|
try {
|
|
29484
|
-
if ((0,
|
|
29603
|
+
if ((0, import_node_fs17.existsSync)(PATHS.pidFile)) (0, import_node_fs17.unlinkSync)(PATHS.pidFile);
|
|
29485
29604
|
} catch {
|
|
29486
29605
|
}
|
|
29487
29606
|
}
|
|
@@ -29493,7 +29612,7 @@ function procStatState(stat) {
|
|
|
29493
29612
|
function isZombie(pid) {
|
|
29494
29613
|
let stat;
|
|
29495
29614
|
try {
|
|
29496
|
-
stat = (0,
|
|
29615
|
+
stat = (0, import_node_fs17.readFileSync)(`/proc/${pid}/stat`, "utf-8");
|
|
29497
29616
|
} catch {
|
|
29498
29617
|
return false;
|
|
29499
29618
|
}
|
|
@@ -29522,24 +29641,24 @@ async function statusCommand() {
|
|
|
29522
29641
|
banner();
|
|
29523
29642
|
const pid = findRunningDaemon();
|
|
29524
29643
|
const client = new IpcClient();
|
|
29525
|
-
let
|
|
29644
|
+
let live2 = null;
|
|
29526
29645
|
try {
|
|
29527
29646
|
await client.connect();
|
|
29528
|
-
|
|
29647
|
+
live2 = await client.status();
|
|
29529
29648
|
} catch {
|
|
29530
29649
|
} finally {
|
|
29531
29650
|
client.close();
|
|
29532
29651
|
}
|
|
29533
29652
|
console.log(source_default.green.bold(" Daemon Status"));
|
|
29534
29653
|
console.log(source_default.gray(" " + "\u2500".repeat(60)));
|
|
29535
|
-
if (
|
|
29654
|
+
if (live2) {
|
|
29536
29655
|
console.log(` Status: ${source_default.green.bold("\u25CF RUNNING")}`);
|
|
29537
|
-
console.log(` PID: ${source_default.white(String(
|
|
29538
|
-
console.log(` Started: ${source_default.white(
|
|
29539
|
-
console.log(` Uptime: ${source_default.white(formatUptime2(
|
|
29540
|
-
console.log(` Version: ${source_default.white(
|
|
29541
|
-
console.log(` Mode: ${source_default.white(
|
|
29542
|
-
console.log(` Socket: ${source_default.gray(
|
|
29656
|
+
console.log(` PID: ${source_default.white(String(live2.pid))}`);
|
|
29657
|
+
console.log(` Started: ${source_default.white(live2.startedAt)}`);
|
|
29658
|
+
console.log(` Uptime: ${source_default.white(formatUptime2(live2.uptimeSeconds))}`);
|
|
29659
|
+
console.log(` Version: ${source_default.white(live2.version)}`);
|
|
29660
|
+
console.log(` Mode: ${source_default.white(live2.mode)}`);
|
|
29661
|
+
console.log(` Socket: ${source_default.gray(live2.paths.socket)}`);
|
|
29543
29662
|
} else if (pid) {
|
|
29544
29663
|
console.log(` Status: ${source_default.yellow.bold("\u25CF RUNNING (IPC unreachable)")}`);
|
|
29545
29664
|
console.log(` PID: ${source_default.white(String(pid))}`);
|
|
@@ -29551,8 +29670,8 @@ async function statusCommand() {
|
|
|
29551
29670
|
console.log();
|
|
29552
29671
|
console.log(source_default.green.bold(" Modules"));
|
|
29553
29672
|
console.log(source_default.gray(" " + "\u2500".repeat(60)));
|
|
29554
|
-
if (
|
|
29555
|
-
for (const mod of
|
|
29673
|
+
if (live2 && live2.modules.length > 0) {
|
|
29674
|
+
for (const mod of live2.modules) {
|
|
29556
29675
|
const dot = mod.status === "running" ? source_default.green("\u25CF") : mod.status === "loaded" ? source_default.cyan("\u25CB") : mod.status === "error" ? source_default.red("\u2717") : source_default.gray("\u25CB");
|
|
29557
29676
|
console.log(` ${dot} ${source_default.white.bold(mod.name.padEnd(18))} ${source_default.gray(mod.status.padEnd(10))} ${source_default.dim(`${mod.events} events`)}`);
|
|
29558
29677
|
}
|
|
@@ -29581,9 +29700,9 @@ async function statusCommand() {
|
|
|
29581
29700
|
}
|
|
29582
29701
|
console.log();
|
|
29583
29702
|
try {
|
|
29584
|
-
if (!
|
|
29585
|
-
const total =
|
|
29586
|
-
const threats =
|
|
29703
|
+
if (!live2) initStateDB(PATHS.stateDb);
|
|
29704
|
+
const total = live2 ? live2.counters.events : getEventCount();
|
|
29705
|
+
const threats = live2 ? live2.counters.threats : getThreatCount();
|
|
29587
29706
|
const last24h = getEventCount(new Date(Date.now() - 864e5));
|
|
29588
29707
|
const threats24h = getThreatCount(new Date(Date.now() - 864e5));
|
|
29589
29708
|
console.log(source_default.green.bold(" Event Statistics"));
|
|
@@ -29619,21 +29738,21 @@ function formatUptime2(seconds) {
|
|
|
29619
29738
|
|
|
29620
29739
|
// src/commands/modules.ts
|
|
29621
29740
|
var import_node_child_process2 = require("child_process");
|
|
29622
|
-
var
|
|
29741
|
+
var import_node_fs19 = require("fs");
|
|
29623
29742
|
var import_node_path15 = require("path");
|
|
29624
29743
|
var import_toml3 = __toESM(require_toml());
|
|
29625
29744
|
init_paths();
|
|
29626
29745
|
|
|
29627
29746
|
// src/daemon/module-trust.ts
|
|
29628
29747
|
var import_node_crypto5 = require("crypto");
|
|
29629
|
-
var
|
|
29748
|
+
var import_node_fs18 = require("fs");
|
|
29630
29749
|
var import_node_path14 = require("path");
|
|
29631
29750
|
init_paths();
|
|
29632
29751
|
var TRUST_FILE = (0, import_node_path14.join)(PATHS.configDir, "trusted-modules.json");
|
|
29633
29752
|
var PUBLISHER_KEYS_FILE = (0, import_node_path14.join)(PATHS.configDir, "publisher-keys.json");
|
|
29634
29753
|
var DIGEST_EXCLUDED_DIRS = /* @__PURE__ */ new Set([".git"]);
|
|
29635
29754
|
function collectFiles(root, dir = root, out = []) {
|
|
29636
|
-
for (const entry of (0,
|
|
29755
|
+
for (const entry of (0, import_node_fs18.readdirSync)(dir, { withFileTypes: true }).sort(
|
|
29637
29756
|
(a, b) => a.name < b.name ? -1 : a.name > b.name ? 1 : 0
|
|
29638
29757
|
)) {
|
|
29639
29758
|
const full = (0, import_node_path14.join)(dir, entry.name);
|
|
@@ -29656,19 +29775,19 @@ function computeModuleDigest(modulePath) {
|
|
|
29656
29775
|
hash.update("\0");
|
|
29657
29776
|
if (entry.isSymlink) {
|
|
29658
29777
|
hash.update("symlink:");
|
|
29659
|
-
hash.update((0,
|
|
29778
|
+
hash.update((0, import_node_fs18.readlinkSync)(entry.path));
|
|
29660
29779
|
} else {
|
|
29661
29780
|
hash.update("file:");
|
|
29662
|
-
hash.update((0,
|
|
29781
|
+
hash.update((0, import_node_fs18.readFileSync)(entry.path));
|
|
29663
29782
|
}
|
|
29664
29783
|
hash.update("\0");
|
|
29665
29784
|
}
|
|
29666
29785
|
return hash.digest("hex");
|
|
29667
29786
|
}
|
|
29668
29787
|
function readTrustFile() {
|
|
29669
|
-
if (!(0,
|
|
29788
|
+
if (!(0, import_node_fs18.existsSync)(TRUST_FILE)) return { version: 1, modules: {} };
|
|
29670
29789
|
try {
|
|
29671
|
-
const parsed = JSON.parse((0,
|
|
29790
|
+
const parsed = JSON.parse((0, import_node_fs18.readFileSync)(TRUST_FILE, "utf-8"));
|
|
29672
29791
|
if (parsed.version !== 1 || typeof parsed.modules !== "object" || !parsed.modules) {
|
|
29673
29792
|
return { version: 1, modules: {} };
|
|
29674
29793
|
}
|
|
@@ -29678,9 +29797,9 @@ function readTrustFile() {
|
|
|
29678
29797
|
}
|
|
29679
29798
|
}
|
|
29680
29799
|
function writeTrustFile(file) {
|
|
29681
|
-
(0,
|
|
29800
|
+
(0, import_node_fs18.writeFileSync)(TRUST_FILE, JSON.stringify(file, null, 2) + "\n", { mode: 384 });
|
|
29682
29801
|
try {
|
|
29683
|
-
(0,
|
|
29802
|
+
(0, import_node_fs18.chmodSync)(TRUST_FILE, 384);
|
|
29684
29803
|
} catch {
|
|
29685
29804
|
}
|
|
29686
29805
|
}
|
|
@@ -29706,9 +29825,9 @@ function listTrustedModules() {
|
|
|
29706
29825
|
return readTrustFile().modules;
|
|
29707
29826
|
}
|
|
29708
29827
|
function readPublisherKeys() {
|
|
29709
|
-
if (!(0,
|
|
29828
|
+
if (!(0, import_node_fs18.existsSync)(PUBLISHER_KEYS_FILE)) return {};
|
|
29710
29829
|
try {
|
|
29711
|
-
const parsed = JSON.parse((0,
|
|
29830
|
+
const parsed = JSON.parse((0, import_node_fs18.readFileSync)(PUBLISHER_KEYS_FILE, "utf-8"));
|
|
29712
29831
|
return parsed && typeof parsed === "object" ? parsed : {};
|
|
29713
29832
|
} catch {
|
|
29714
29833
|
return {};
|
|
@@ -29721,12 +29840,12 @@ function verifyModuleSignature(modulePath, digest) {
|
|
|
29721
29840
|
if (pinnedKeyIds.length === 0) {
|
|
29722
29841
|
return { ok: true };
|
|
29723
29842
|
}
|
|
29724
|
-
if (!(0,
|
|
29843
|
+
if (!(0, import_node_fs18.existsSync)(sigPath)) {
|
|
29725
29844
|
return { ok: false, reason: "publisher keys are pinned but the module ships no mod.sig" };
|
|
29726
29845
|
}
|
|
29727
29846
|
let parsed;
|
|
29728
29847
|
try {
|
|
29729
|
-
parsed = JSON.parse((0,
|
|
29848
|
+
parsed = JSON.parse((0, import_node_fs18.readFileSync)(sigPath, "utf-8"));
|
|
29730
29849
|
} catch {
|
|
29731
29850
|
return { ok: false, reason: "mod.sig is not valid JSON" };
|
|
29732
29851
|
}
|
|
@@ -29801,11 +29920,11 @@ function assertSafeTarballEntries(tarPath) {
|
|
|
29801
29920
|
}
|
|
29802
29921
|
function validateManifest(modPath) {
|
|
29803
29922
|
const manifestPath = (0, import_node_path15.join)(modPath, "mod.toml");
|
|
29804
|
-
if (!(0,
|
|
29923
|
+
if (!(0, import_node_fs19.existsSync)(manifestPath)) {
|
|
29805
29924
|
return { ok: false, error: `mod.toml not found at ${manifestPath}` };
|
|
29806
29925
|
}
|
|
29807
29926
|
try {
|
|
29808
|
-
const raw = (0,
|
|
29927
|
+
const raw = (0, import_node_fs19.readFileSync)(manifestPath, "utf-8");
|
|
29809
29928
|
const parsed = import_toml3.default.parse(raw);
|
|
29810
29929
|
const name = parsed.module?.name;
|
|
29811
29930
|
const version = parsed.module?.version;
|
|
@@ -29868,7 +29987,7 @@ async function modulesInstallCommand(source) {
|
|
|
29868
29987
|
const dir = modulesDir();
|
|
29869
29988
|
if (source.startsWith("./") || source.startsWith("/") || source.startsWith("~")) {
|
|
29870
29989
|
const absPath = (0, import_node_path15.resolve)(source.startsWith("~") ? source.replace("~", process.env.HOME || "") : source);
|
|
29871
|
-
if (!(0,
|
|
29990
|
+
if (!(0, import_node_fs19.existsSync)(absPath)) {
|
|
29872
29991
|
console.log(source_default.red(` \u2717 Path not found: ${absPath}
|
|
29873
29992
|
`));
|
|
29874
29993
|
return;
|
|
@@ -29887,7 +30006,7 @@ async function modulesInstallCommand(source) {
|
|
|
29887
30006
|
`));
|
|
29888
30007
|
return;
|
|
29889
30008
|
}
|
|
29890
|
-
if ((0,
|
|
30009
|
+
if ((0, import_node_fs19.existsSync)(dest2)) {
|
|
29891
30010
|
console.log(source_default.yellow(` ! ${check.name} is already installed at ${dest2}`));
|
|
29892
30011
|
console.log(source_default.dim(` Run ${source_default.white(`threatcrush modules remove ${check.name}`)} first.
|
|
29893
30012
|
`));
|
|
@@ -29895,7 +30014,7 @@ async function modulesInstallCommand(source) {
|
|
|
29895
30014
|
}
|
|
29896
30015
|
const spinner2 = ora({ text: `Copying module files...`, color: "green" }).start();
|
|
29897
30016
|
try {
|
|
29898
|
-
(0,
|
|
30017
|
+
(0, import_node_fs19.cpSync)(absPath, dest2, { recursive: true, dereference: true });
|
|
29899
30018
|
spinner2.succeed(`Installed ${source_default.white(check.name)} v${check.version} \u2192 ${dest2}`);
|
|
29900
30019
|
} catch (err) {
|
|
29901
30020
|
spinner2.fail(`Copy failed: ${err.message}`);
|
|
@@ -29918,7 +30037,7 @@ async function modulesInstallCommand(source) {
|
|
|
29918
30037
|
`));
|
|
29919
30038
|
return;
|
|
29920
30039
|
}
|
|
29921
|
-
if ((0,
|
|
30040
|
+
if ((0, import_node_fs19.existsSync)(dest2)) {
|
|
29922
30041
|
console.log(source_default.yellow(` ! ${name} is already installed at ${dest2}`));
|
|
29923
30042
|
console.log(source_default.dim(` Run ${source_default.white(`threatcrush modules remove ${name}`)} first.
|
|
29924
30043
|
`));
|
|
@@ -29935,7 +30054,7 @@ async function modulesInstallCommand(source) {
|
|
|
29935
30054
|
if (!check.ok) {
|
|
29936
30055
|
spinner2.fail(check.error);
|
|
29937
30056
|
try {
|
|
29938
|
-
(0,
|
|
30057
|
+
(0, import_node_fs19.rmSync)(dest2, { recursive: true, force: true });
|
|
29939
30058
|
} catch {
|
|
29940
30059
|
}
|
|
29941
30060
|
return;
|
|
@@ -29982,7 +30101,7 @@ async function modulesInstallCommand(source) {
|
|
|
29982
30101
|
`));
|
|
29983
30102
|
return;
|
|
29984
30103
|
}
|
|
29985
|
-
if ((0,
|
|
30104
|
+
if ((0, import_node_fs19.existsSync)(dest)) {
|
|
29986
30105
|
console.log(source_default.yellow(` ! ${mod.slug} is already installed at ${dest}
|
|
29987
30106
|
`));
|
|
29988
30107
|
return;
|
|
@@ -30008,7 +30127,7 @@ async function modulesInstallCommand(source) {
|
|
|
30008
30127
|
if (!check.ok) {
|
|
30009
30128
|
cloneSpinner.fail(check.error);
|
|
30010
30129
|
try {
|
|
30011
|
-
(0,
|
|
30130
|
+
(0, import_node_fs19.rmSync)(dest, { recursive: true, force: true });
|
|
30012
30131
|
} catch {
|
|
30013
30132
|
}
|
|
30014
30133
|
return;
|
|
@@ -30023,10 +30142,10 @@ async function modulesInstallCommand(source) {
|
|
|
30023
30142
|
return;
|
|
30024
30143
|
}
|
|
30025
30144
|
const tar = (0, import_node_path15.join)(dir, `${mod.slug}.tar.gz`);
|
|
30026
|
-
(0,
|
|
30145
|
+
(0, import_node_fs19.writeFileSync)(tar, Buffer.from(await res.arrayBuffer()));
|
|
30027
30146
|
assertSafeTarballEntries(tar);
|
|
30028
30147
|
(0, import_node_child_process2.execFileSync)("tar", ["-xzf", tar, "-C", dir], { stdio: "pipe" });
|
|
30029
|
-
(0,
|
|
30148
|
+
(0, import_node_fs19.rmSync)(tar, { force: true });
|
|
30030
30149
|
const check = validateManifest(dest);
|
|
30031
30150
|
if (!check.ok) {
|
|
30032
30151
|
dlSpinner.fail(check.error);
|
|
@@ -30058,7 +30177,7 @@ async function modulesRemoveCommand(name) {
|
|
|
30058
30177
|
`));
|
|
30059
30178
|
return;
|
|
30060
30179
|
}
|
|
30061
|
-
if (!(0,
|
|
30180
|
+
if (!(0, import_node_fs19.existsSync)(target)) {
|
|
30062
30181
|
console.log(source_default.yellow(` ! No module named "${name}" in ${dir}
|
|
30063
30182
|
`));
|
|
30064
30183
|
return;
|
|
@@ -30068,7 +30187,7 @@ async function modulesRemoveCommand(name) {
|
|
|
30068
30187
|
console.log(source_default.yellow(` ! Directory name "${name}" does not match manifest name "${check.name}"`));
|
|
30069
30188
|
}
|
|
30070
30189
|
try {
|
|
30071
|
-
(0,
|
|
30190
|
+
(0, import_node_fs19.rmSync)(target, { recursive: true, force: true });
|
|
30072
30191
|
untrustModule(name);
|
|
30073
30192
|
console.log(source_default.green(` \u2713 Removed ${name} from ${dir}
|
|
30074
30193
|
`));
|
|
@@ -30093,7 +30212,7 @@ async function modulesTrustCommand(name) {
|
|
|
30093
30212
|
`));
|
|
30094
30213
|
return;
|
|
30095
30214
|
}
|
|
30096
|
-
if (!(0,
|
|
30215
|
+
if (!(0, import_node_fs19.existsSync)(target)) {
|
|
30097
30216
|
console.log(source_default.yellow(` ! No module named "${name}" in ${dir}
|
|
30098
30217
|
`));
|
|
30099
30218
|
return;
|
|
@@ -30528,7 +30647,7 @@ async function pentestCommand(targetUrl) {
|
|
|
30528
30647
|
}
|
|
30529
30648
|
|
|
30530
30649
|
// src/commands/ai-scan.ts
|
|
30531
|
-
var
|
|
30650
|
+
var import_node_fs20 = require("fs");
|
|
30532
30651
|
|
|
30533
30652
|
// src/aiscan/probes.ts
|
|
30534
30653
|
function makeCanary() {
|
|
@@ -30805,7 +30924,7 @@ async function aiScanMoshCommand(file, args) {
|
|
|
30805
30924
|
console.log();
|
|
30806
30925
|
let source;
|
|
30807
30926
|
try {
|
|
30808
|
-
source = (0,
|
|
30927
|
+
source = (0, import_node_fs20.readFileSync)(file, "utf-8");
|
|
30809
30928
|
} catch (err) {
|
|
30810
30929
|
logger.error(`Cannot read ${file}: ${err.message}`);
|
|
30811
30930
|
process.exitCode = 1;
|
|
@@ -30826,21 +30945,21 @@ async function aiScanMoshCommand(file, args) {
|
|
|
30826
30945
|
|
|
30827
30946
|
// src/commands/orgs.ts
|
|
30828
30947
|
var import_node_os6 = require("os");
|
|
30829
|
-
var
|
|
30948
|
+
var import_node_fs21 = require("fs");
|
|
30830
30949
|
var import_node_path16 = require("path");
|
|
30831
30950
|
var API_URL3 = process.env.THREATCRUSH_API_URL || "https://threatcrush.com";
|
|
30832
30951
|
var CONFIG_PATH = (0, import_node_path16.join)((0, import_node_os6.homedir)(), ".threatcrush", "config.json");
|
|
30833
30952
|
function readConfig() {
|
|
30834
30953
|
try {
|
|
30835
|
-
return JSON.parse((0,
|
|
30954
|
+
return JSON.parse((0, import_node_fs21.readFileSync)(CONFIG_PATH, "utf-8"));
|
|
30836
30955
|
} catch {
|
|
30837
30956
|
return {};
|
|
30838
30957
|
}
|
|
30839
30958
|
}
|
|
30840
30959
|
function writeConfig(config) {
|
|
30841
30960
|
const dir = (0, import_node_path16.join)((0, import_node_os6.homedir)(), ".threatcrush");
|
|
30842
|
-
if (!(0,
|
|
30843
|
-
(0,
|
|
30961
|
+
if (!(0, import_node_fs21.existsSync)(dir)) (0, import_node_fs21.mkdirSync)(dir, { recursive: true });
|
|
30962
|
+
(0, import_node_fs21.writeFileSync)(CONFIG_PATH, JSON.stringify(config, null, 2));
|
|
30844
30963
|
}
|
|
30845
30964
|
function getAuthHeaders() {
|
|
30846
30965
|
const config = readConfig();
|
|
@@ -30975,7 +31094,7 @@ async function useOrganization(slug) {
|
|
|
30975
31094
|
}
|
|
30976
31095
|
|
|
30977
31096
|
// src/commands/servers.ts
|
|
30978
|
-
var
|
|
31097
|
+
var import_node_readline2 = __toESM(require("readline"));
|
|
30979
31098
|
var import_node_os7 = require("os");
|
|
30980
31099
|
async function serversCommand(options) {
|
|
30981
31100
|
const { action, name, org } = options;
|
|
@@ -31040,7 +31159,7 @@ async function fetchOrgs() {
|
|
|
31040
31159
|
return data.organizations || [];
|
|
31041
31160
|
}
|
|
31042
31161
|
function askChoice(question) {
|
|
31043
|
-
const rl =
|
|
31162
|
+
const rl = import_node_readline2.default.createInterface({ input: process.stdin, output: process.stdout });
|
|
31044
31163
|
return new Promise((resolve6) => rl.question(question, (answer) => {
|
|
31045
31164
|
rl.close();
|
|
31046
31165
|
resolve6(answer.trim());
|
|
@@ -31185,14 +31304,14 @@ function timeAgo(dateStr) {
|
|
|
31185
31304
|
|
|
31186
31305
|
// src/commands/connect.ts
|
|
31187
31306
|
var import_node_os8 = require("os");
|
|
31188
|
-
var
|
|
31307
|
+
var import_node_fs22 = require("fs");
|
|
31189
31308
|
var import_node_path17 = require("path");
|
|
31190
31309
|
var import_node_child_process3 = require("child_process");
|
|
31191
31310
|
var API_URL4 = process.env.THREATCRUSH_API_URL || "https://threatcrush.com";
|
|
31192
31311
|
var CONFIG_PATH2 = (0, import_node_path17.join)((0, import_node_os8.homedir)(), ".threatcrush", "config.json");
|
|
31193
31312
|
function readConfig2() {
|
|
31194
31313
|
try {
|
|
31195
|
-
return JSON.parse((0,
|
|
31314
|
+
return JSON.parse((0, import_node_fs22.readFileSync)(CONFIG_PATH2, "utf-8"));
|
|
31196
31315
|
} catch {
|
|
31197
31316
|
return {};
|
|
31198
31317
|
}
|
|
@@ -31347,12 +31466,12 @@ async function sshConnect(options) {
|
|
|
31347
31466
|
|
|
31348
31467
|
// src/commands/daemon.ts
|
|
31349
31468
|
var import_node_child_process11 = require("child_process");
|
|
31350
|
-
var import_node_fs33 = require("fs");
|
|
31351
|
-
var import_node_path24 = require("path");
|
|
31352
31469
|
var import_node_fs34 = require("fs");
|
|
31470
|
+
var import_node_path24 = require("path");
|
|
31471
|
+
var import_node_fs35 = require("fs");
|
|
31353
31472
|
|
|
31354
31473
|
// src/daemon/index.ts
|
|
31355
|
-
var
|
|
31474
|
+
var import_node_fs33 = require("fs");
|
|
31356
31475
|
var import_node_child_process10 = require("child_process");
|
|
31357
31476
|
var import_node_os9 = require("os");
|
|
31358
31477
|
var import_node_path23 = require("path");
|
|
@@ -31361,7 +31480,7 @@ init_paths();
|
|
|
31361
31480
|
|
|
31362
31481
|
// src/daemon/ipc-server.ts
|
|
31363
31482
|
var import_node_net2 = require("net");
|
|
31364
|
-
var
|
|
31483
|
+
var import_node_fs23 = require("fs");
|
|
31365
31484
|
init_paths();
|
|
31366
31485
|
init_control_token();
|
|
31367
31486
|
|
|
@@ -31415,9 +31534,9 @@ var IpcServer = class {
|
|
|
31415
31534
|
controlToken = "";
|
|
31416
31535
|
async start() {
|
|
31417
31536
|
this.controlToken = issueControlToken();
|
|
31418
|
-
if ((0,
|
|
31537
|
+
if ((0, import_node_fs23.existsSync)(PATHS.socket)) {
|
|
31419
31538
|
try {
|
|
31420
|
-
(0,
|
|
31539
|
+
(0, import_node_fs23.unlinkSync)(PATHS.socket);
|
|
31421
31540
|
} catch {
|
|
31422
31541
|
}
|
|
31423
31542
|
}
|
|
@@ -31453,14 +31572,14 @@ var IpcServer = class {
|
|
|
31453
31572
|
return new Promise((resolve6) => {
|
|
31454
31573
|
if (!this.server) {
|
|
31455
31574
|
try {
|
|
31456
|
-
if ((0,
|
|
31575
|
+
if ((0, import_node_fs23.existsSync)(PATHS.socket)) (0, import_node_fs23.unlinkSync)(PATHS.socket);
|
|
31457
31576
|
} catch {
|
|
31458
31577
|
}
|
|
31459
31578
|
return resolve6();
|
|
31460
31579
|
}
|
|
31461
31580
|
this.server.close(() => {
|
|
31462
31581
|
try {
|
|
31463
|
-
if ((0,
|
|
31582
|
+
if ((0, import_node_fs23.existsSync)(PATHS.socket)) (0, import_node_fs23.unlinkSync)(PATHS.socket);
|
|
31464
31583
|
} catch {
|
|
31465
31584
|
}
|
|
31466
31585
|
resolve6();
|
|
@@ -31642,15 +31761,14 @@ var IpcServer = class {
|
|
|
31642
31761
|
};
|
|
31643
31762
|
|
|
31644
31763
|
// src/daemon/module-host.ts
|
|
31645
|
-
var
|
|
31764
|
+
var import_node_fs27 = require("fs");
|
|
31646
31765
|
var import_node_path18 = require("path");
|
|
31647
31766
|
var import_node_url = require("url");
|
|
31648
31767
|
var import_toml4 = __toESM(require_toml());
|
|
31649
31768
|
init_paths();
|
|
31650
31769
|
|
|
31651
31770
|
// src/daemon/watchers/log-watcher.ts
|
|
31652
|
-
var
|
|
31653
|
-
var import_node_readline4 = require("readline");
|
|
31771
|
+
var import_node_fs24 = require("fs");
|
|
31654
31772
|
var DEFAULT_SOURCES = [
|
|
31655
31773
|
{ path: "/var/log/auth.log", module: "ssh-guard", category: "auth" },
|
|
31656
31774
|
{ path: "/var/log/secure", module: "ssh-guard", category: "auth" },
|
|
@@ -31665,14 +31783,14 @@ var LogWatcher = class {
|
|
|
31665
31783
|
bus;
|
|
31666
31784
|
sources;
|
|
31667
31785
|
timers = /* @__PURE__ */ new Map();
|
|
31668
|
-
|
|
31786
|
+
tails = /* @__PURE__ */ new Map();
|
|
31669
31787
|
active = /* @__PURE__ */ new Set();
|
|
31670
31788
|
start() {
|
|
31671
31789
|
const started = [];
|
|
31672
31790
|
for (const src of this.sources) {
|
|
31673
|
-
if (!(0,
|
|
31791
|
+
if (!(0, import_node_fs24.existsSync)(src.path)) continue;
|
|
31674
31792
|
try {
|
|
31675
|
-
(0,
|
|
31793
|
+
(0, import_node_fs24.accessSync)(src.path, import_node_fs24.constants.R_OK);
|
|
31676
31794
|
} catch {
|
|
31677
31795
|
continue;
|
|
31678
31796
|
}
|
|
@@ -31684,7 +31802,7 @@ var LogWatcher = class {
|
|
|
31684
31802
|
stop() {
|
|
31685
31803
|
for (const t of this.timers.values()) clearInterval(t);
|
|
31686
31804
|
this.timers.clear();
|
|
31687
|
-
this.
|
|
31805
|
+
this.tails.clear();
|
|
31688
31806
|
this.active.clear();
|
|
31689
31807
|
}
|
|
31690
31808
|
activeModules() {
|
|
@@ -31699,38 +31817,22 @@ var LogWatcher = class {
|
|
|
31699
31817
|
return this.sources.filter((s) => s.category === "auth" && this.timers.has(s.path)).map((s) => s.path);
|
|
31700
31818
|
}
|
|
31701
31819
|
tail(src) {
|
|
31702
|
-
|
|
31703
|
-
|
|
31704
|
-
} catch {
|
|
31705
|
-
this.positions.set(src.path, 0);
|
|
31706
|
-
}
|
|
31820
|
+
const tail = new LogTail(src.path);
|
|
31821
|
+
this.tails.set(src.path, tail);
|
|
31707
31822
|
const timer = setInterval(() => this.poll(src), 1e3);
|
|
31708
31823
|
this.timers.set(src.path, timer);
|
|
31709
31824
|
this.active.add(src.module);
|
|
31710
31825
|
}
|
|
31826
|
+
/** Each new line exactly once; see LogTail for why this is synchronous. */
|
|
31711
31827
|
poll(src) {
|
|
31712
|
-
|
|
31713
|
-
|
|
31714
|
-
|
|
31715
|
-
|
|
31716
|
-
|
|
31717
|
-
|
|
31718
|
-
|
|
31719
|
-
if (stat.size < prev) {
|
|
31720
|
-
this.positions.set(src.path, 0);
|
|
31721
|
-
return;
|
|
31722
|
-
}
|
|
31723
|
-
if (stat.size === prev) return;
|
|
31724
|
-
const stream = (0, import_node_fs23.createReadStream)(src.path, { start: prev, encoding: "utf-8" });
|
|
31725
|
-
stream.on("error", () => this.positions.set(src.path, stat.size));
|
|
31726
|
-
const rl = (0, import_node_readline4.createInterface)({ input: stream });
|
|
31727
|
-
rl.on("error", () => {
|
|
31728
|
-
});
|
|
31729
|
-
rl.on("line", (line) => {
|
|
31730
|
-
if (!line.trim()) return;
|
|
31731
|
-
this.process(line, src);
|
|
31828
|
+
const tail = this.tails.get(src.path);
|
|
31829
|
+
if (!tail) return 0;
|
|
31830
|
+
return tail.poll((line) => {
|
|
31831
|
+
try {
|
|
31832
|
+
this.process(line, src);
|
|
31833
|
+
} catch {
|
|
31834
|
+
}
|
|
31732
31835
|
});
|
|
31733
|
-
rl.on("close", () => this.positions.set(src.path, stat.size));
|
|
31734
31836
|
}
|
|
31735
31837
|
process(line, src) {
|
|
31736
31838
|
const parsed = autoDetectParser(line);
|
|
@@ -31985,7 +32087,7 @@ function realtimeToDate(rt) {
|
|
|
31985
32087
|
|
|
31986
32088
|
// src/modules/network-monitor/index.ts
|
|
31987
32089
|
var import_node_child_process5 = require("child_process");
|
|
31988
|
-
var
|
|
32090
|
+
var import_node_fs25 = require("fs");
|
|
31989
32091
|
var NetworkMonitor = class {
|
|
31990
32092
|
constructor(bus2) {
|
|
31991
32093
|
this.bus = bus2;
|
|
@@ -32049,7 +32151,7 @@ var NetworkMonitor = class {
|
|
|
32049
32151
|
hasConntrackOrSs() {
|
|
32050
32152
|
const ss = (0, import_node_child_process5.spawnSync)("ss", ["--version"], { stdio: "pipe" });
|
|
32051
32153
|
if (ss.status === 0) return true;
|
|
32052
|
-
return (0,
|
|
32154
|
+
return (0, import_node_fs25.existsSync)("/proc/net/tcp");
|
|
32053
32155
|
}
|
|
32054
32156
|
/**
|
|
32055
32157
|
* The TCP ports this host actually listens on.
|
|
@@ -32236,9 +32338,9 @@ var NetworkMonitor = class {
|
|
|
32236
32338
|
};
|
|
32237
32339
|
|
|
32238
32340
|
// src/modules/dns-monitor/index.ts
|
|
32239
|
-
var
|
|
32341
|
+
var import_node_fs26 = require("fs");
|
|
32240
32342
|
var import_node_child_process6 = require("child_process");
|
|
32241
|
-
var
|
|
32343
|
+
var import_node_readline3 = require("readline");
|
|
32242
32344
|
var DNS_LOG_SOURCES = [
|
|
32243
32345
|
"/var/log/syslog",
|
|
32244
32346
|
// systemd-resolved logs here
|
|
@@ -32279,7 +32381,7 @@ var DnsMonitor = class {
|
|
|
32279
32381
|
bus;
|
|
32280
32382
|
active = false;
|
|
32281
32383
|
timers = /* @__PURE__ */ new Map();
|
|
32282
|
-
|
|
32384
|
+
tails = /* @__PURE__ */ new Map();
|
|
32283
32385
|
// Tracking windows
|
|
32284
32386
|
txtQueryCounts = /* @__PURE__ */ new Map();
|
|
32285
32387
|
domainBuffer = [];
|
|
@@ -32318,9 +32420,9 @@ var DnsMonitor = class {
|
|
|
32318
32420
|
}
|
|
32319
32421
|
start() {
|
|
32320
32422
|
const sources = [...DNS_LOG_SOURCES, ...this.extraPaths].filter((p) => {
|
|
32321
|
-
if (!(0,
|
|
32423
|
+
if (!(0, import_node_fs26.existsSync)(p)) return false;
|
|
32322
32424
|
try {
|
|
32323
|
-
(0,
|
|
32425
|
+
(0, import_node_fs26.accessSync)(p, import_node_fs26.constants.R_OK);
|
|
32324
32426
|
return true;
|
|
32325
32427
|
} catch {
|
|
32326
32428
|
return false;
|
|
@@ -32383,7 +32485,7 @@ var DnsMonitor = class {
|
|
|
32383
32485
|
stdio: ["ignore", "pipe", "ignore"]
|
|
32384
32486
|
});
|
|
32385
32487
|
if (!child.stdout) return;
|
|
32386
|
-
const rl = (0,
|
|
32488
|
+
const rl = (0, import_node_readline3.createInterface)({ input: child.stdout });
|
|
32387
32489
|
rl.on("line", (line) => this.parseDnsLine(line));
|
|
32388
32490
|
child.on("error", () => {
|
|
32389
32491
|
});
|
|
@@ -32395,33 +32497,11 @@ var DnsMonitor = class {
|
|
|
32395
32497
|
return this.active;
|
|
32396
32498
|
}
|
|
32397
32499
|
tailLog(path) {
|
|
32398
|
-
|
|
32399
|
-
|
|
32400
|
-
|
|
32401
|
-
this.positions.set(path, 0);
|
|
32402
|
-
}
|
|
32403
|
-
const timer = setInterval(() => this.pollLog(path), 2e3);
|
|
32500
|
+
const tail = new LogTail(path);
|
|
32501
|
+
this.tails.set(path, tail);
|
|
32502
|
+
const timer = setInterval(() => tail.poll((line) => this.parseDnsLine(line)), 2e3);
|
|
32404
32503
|
this.timers.set(path, timer);
|
|
32405
32504
|
}
|
|
32406
|
-
pollLog(path) {
|
|
32407
|
-
let stat;
|
|
32408
|
-
try {
|
|
32409
|
-
stat = (0, import_node_fs25.statSync)(path);
|
|
32410
|
-
} catch {
|
|
32411
|
-
return;
|
|
32412
|
-
}
|
|
32413
|
-
const prev = this.positions.get(path) ?? 0;
|
|
32414
|
-
if (stat.size < prev) {
|
|
32415
|
-
this.positions.set(path, 0);
|
|
32416
|
-
return;
|
|
32417
|
-
}
|
|
32418
|
-
if (stat.size === prev) return;
|
|
32419
|
-
const stream = (0, import_node_fs25.createReadStream)(path, { start: prev, encoding: "utf-8" });
|
|
32420
|
-
stream.on("error", () => this.positions.set(path, stat.size));
|
|
32421
|
-
const rl = (0, import_node_readline5.createInterface)({ input: stream });
|
|
32422
|
-
rl.on("line", (line) => this.parseDnsLine(line));
|
|
32423
|
-
rl.on("close", () => this.positions.set(path, stat.size));
|
|
32424
|
-
}
|
|
32425
32505
|
parseDnsLine(line) {
|
|
32426
32506
|
const moshpit = line.match(
|
|
32427
32507
|
/\[dns\]\s+(?:udp|tcp|doh)\s+(\S+)\s+(\S+)\s+(\d+)\s+(\w+)/i
|
|
@@ -32675,15 +32755,15 @@ var ModuleHost = class {
|
|
|
32675
32755
|
}
|
|
32676
32756
|
}
|
|
32677
32757
|
async discoverAndStartInstalled() {
|
|
32678
|
-
if (!(0,
|
|
32758
|
+
if (!(0, import_node_fs27.existsSync)(PATHS.moduleDir)) return;
|
|
32679
32759
|
const configs = loadModuleConfigs(PATHS.confD);
|
|
32680
|
-
const entries = (0,
|
|
32760
|
+
const entries = (0, import_node_fs27.readdirSync)(PATHS.moduleDir, { withFileTypes: true });
|
|
32681
32761
|
for (const entry of entries) {
|
|
32682
32762
|
if (!entry.isDirectory()) continue;
|
|
32683
32763
|
const manifestPath = (0, import_node_path18.join)(PATHS.moduleDir, entry.name, "mod.toml");
|
|
32684
|
-
if (!(0,
|
|
32764
|
+
if (!(0, import_node_fs27.existsSync)(manifestPath)) continue;
|
|
32685
32765
|
try {
|
|
32686
|
-
const manifest = import_toml4.default.parse((0,
|
|
32766
|
+
const manifest = import_toml4.default.parse((0, import_node_fs27.readFileSync)(manifestPath, "utf-8"));
|
|
32687
32767
|
const name = manifest.module?.name || entry.name;
|
|
32688
32768
|
const defaults = manifest.module?.config?.defaults || {};
|
|
32689
32769
|
const config = {
|
|
@@ -32753,15 +32833,15 @@ var ModuleHost = class {
|
|
|
32753
32833
|
installedEntrypoint(modulePath) {
|
|
32754
32834
|
const packageJson = (0, import_node_path18.join)(modulePath, "package.json");
|
|
32755
32835
|
const candidates = [];
|
|
32756
|
-
if ((0,
|
|
32836
|
+
if ((0, import_node_fs27.existsSync)(packageJson)) {
|
|
32757
32837
|
try {
|
|
32758
|
-
const pkg = JSON.parse((0,
|
|
32838
|
+
const pkg = JSON.parse((0, import_node_fs27.readFileSync)(packageJson, "utf-8"));
|
|
32759
32839
|
if (pkg.main) candidates.push((0, import_node_path18.join)(modulePath, pkg.main));
|
|
32760
32840
|
} catch {
|
|
32761
32841
|
}
|
|
32762
32842
|
}
|
|
32763
32843
|
candidates.push((0, import_node_path18.join)(modulePath, "dist", "index.js"), (0, import_node_path18.join)(modulePath, "index.js"));
|
|
32764
|
-
return candidates.find((candidate) => (0,
|
|
32844
|
+
return candidates.find((candidate) => (0, import_node_fs27.existsSync)(candidate)) || null;
|
|
32765
32845
|
}
|
|
32766
32846
|
isThreatCrushModule(value) {
|
|
32767
32847
|
return Boolean(
|
|
@@ -33264,7 +33344,7 @@ function findingEvents(serverId, results, observedAt = /* @__PURE__ */ new Date(
|
|
|
33264
33344
|
}
|
|
33265
33345
|
|
|
33266
33346
|
// src/daemon/workers/cloud-spool.ts
|
|
33267
|
-
var
|
|
33347
|
+
var import_node_fs28 = require("fs");
|
|
33268
33348
|
var import_promises = require("fs/promises");
|
|
33269
33349
|
var import_node_path19 = require("path");
|
|
33270
33350
|
var DEFAULT_SPOOL_LIMITS = { maxEvents: 1e4, maxBytes: 20 * 1024 * 1024 };
|
|
@@ -33273,7 +33353,7 @@ var CloudSpool = class {
|
|
|
33273
33353
|
this.path = path;
|
|
33274
33354
|
this.limits = limits;
|
|
33275
33355
|
try {
|
|
33276
|
-
(0,
|
|
33356
|
+
(0, import_node_fs28.mkdirSync)((0, import_node_path19.dirname)(path), { recursive: true });
|
|
33277
33357
|
} catch {
|
|
33278
33358
|
}
|
|
33279
33359
|
this.load();
|
|
@@ -33342,7 +33422,7 @@ var CloudSpool = class {
|
|
|
33342
33422
|
load() {
|
|
33343
33423
|
let raw;
|
|
33344
33424
|
try {
|
|
33345
|
-
raw = (0,
|
|
33425
|
+
raw = (0, import_node_fs28.readFileSync)(this.path, "utf-8");
|
|
33346
33426
|
} catch {
|
|
33347
33427
|
return;
|
|
33348
33428
|
}
|
|
@@ -33927,6 +34007,19 @@ var RemediationConsumer = class {
|
|
|
33927
34007
|
};
|
|
33928
34008
|
|
|
33929
34009
|
// src/daemon/rules/engine.ts
|
|
34010
|
+
var MAX_WINDOW_ENTRIES = 1e3;
|
|
34011
|
+
function live(window) {
|
|
34012
|
+
return window.times.length - window.head;
|
|
34013
|
+
}
|
|
34014
|
+
function expire(window, now) {
|
|
34015
|
+
const cutoff = now - window.windowMs;
|
|
34016
|
+
while (window.head < window.times.length && window.times[window.head] < cutoff) window.head++;
|
|
34017
|
+
if (window.head > 64 && window.head * 2 > window.times.length) {
|
|
34018
|
+
window.times = window.times.slice(window.head);
|
|
34019
|
+
window.ips = window.ips.slice(window.head);
|
|
34020
|
+
window.head = 0;
|
|
34021
|
+
}
|
|
34022
|
+
}
|
|
33930
34023
|
var RuleEngine = class {
|
|
33931
34024
|
constructor(onDetection) {
|
|
33932
34025
|
this.onDetection = onDetection;
|
|
@@ -33952,27 +34045,42 @@ var RuleEngine = class {
|
|
|
33952
34045
|
const windowKey = `${rule.id}:${groupBy}:${groupValue}`;
|
|
33953
34046
|
let window = this.windows.get(windowKey);
|
|
33954
34047
|
if (!window) {
|
|
33955
|
-
window = {
|
|
34048
|
+
window = {
|
|
34049
|
+
times: [],
|
|
34050
|
+
ips: [],
|
|
34051
|
+
head: 0,
|
|
34052
|
+
lastAlert: 0,
|
|
34053
|
+
windowMs: rule.window_seconds * 1e3,
|
|
34054
|
+
cooldownMs: rule.cooldown_seconds * 1e3
|
|
34055
|
+
};
|
|
33956
34056
|
this.windows.set(windowKey, window);
|
|
33957
34057
|
}
|
|
33958
|
-
window.
|
|
33959
|
-
|
|
33960
|
-
window
|
|
33961
|
-
|
|
33962
|
-
|
|
33963
|
-
|
|
33964
|
-
|
|
33965
|
-
|
|
33966
|
-
|
|
34058
|
+
window.times.push(now);
|
|
34059
|
+
window.ips.push(event.source_ip);
|
|
34060
|
+
expire(window, now);
|
|
34061
|
+
const cap = Math.max(rule.threshold, MAX_WINDOW_ENTRIES);
|
|
34062
|
+
let capped = false;
|
|
34063
|
+
if (live(window) > cap) {
|
|
34064
|
+
window.head = window.times.length - cap;
|
|
34065
|
+
capped = true;
|
|
34066
|
+
expire(window, now);
|
|
34067
|
+
}
|
|
34068
|
+
if (live(window) < rule.threshold) continue;
|
|
34069
|
+
if (window.lastAlert > 0 && now - window.lastAlert < window.cooldownMs) continue;
|
|
34070
|
+
const hits = live(window);
|
|
34071
|
+
const distinctIps = new Set(window.ips.slice(window.head).filter(Boolean)).size;
|
|
33967
34072
|
window.lastAlert = now;
|
|
33968
|
-
window.
|
|
34073
|
+
window.times = [];
|
|
34074
|
+
window.ips = [];
|
|
34075
|
+
window.head = 0;
|
|
34076
|
+
const hitsLabel = capped ? `${hits}+` : String(hits);
|
|
33969
34077
|
const aggregate = groupBy !== "source_ip";
|
|
33970
34078
|
const spread = aggregate ? ` from ${distinctIps} address${distinctIps === 1 ? "" : "es"}${groupBy === "endpoint" ? ` against ${groupValue}` : ""}` : "";
|
|
33971
34079
|
this.onDetection({
|
|
33972
34080
|
rule_id: rule.id,
|
|
33973
34081
|
severity: rule.severity,
|
|
33974
34082
|
title: rule.title,
|
|
33975
|
-
description: `${rule.description} (${
|
|
34083
|
+
description: `${rule.description} (${hitsLabel} events in ${rule.window_seconds}s${spread})`,
|
|
33976
34084
|
source_ip: aggregate ? void 0 : event.source_ip,
|
|
33977
34085
|
username: event.details?.user || void 0,
|
|
33978
34086
|
raw_metadata: {
|
|
@@ -34035,19 +34143,31 @@ var RuleEngine = class {
|
|
|
34035
34143
|
return event.details?.[field];
|
|
34036
34144
|
}
|
|
34037
34145
|
}
|
|
34038
|
-
|
|
34039
|
-
|
|
34040
|
-
|
|
34041
|
-
|
|
34042
|
-
|
|
34146
|
+
/**
|
|
34147
|
+
* Expire every window by its own rule's length, and forget windows with
|
|
34148
|
+
* nothing left in them once their cooldown is over (a cooldown must outlive
|
|
34149
|
+
* an empty window, or a source could re-trigger the moment it was forgotten).
|
|
34150
|
+
* Without the expiry, a window whose address never came back was kept forever.
|
|
34151
|
+
*/
|
|
34152
|
+
cleanup(now = Date.now()) {
|
|
34153
|
+
let removed = 0;
|
|
34154
|
+
for (const [key, window] of this.windows) {
|
|
34155
|
+
expire(window, now);
|
|
34156
|
+
if (live(window) === 0 && (window.lastAlert === 0 || now - window.lastAlert >= window.cooldownMs)) {
|
|
34043
34157
|
this.windows.delete(key);
|
|
34158
|
+
removed++;
|
|
34044
34159
|
}
|
|
34045
34160
|
}
|
|
34161
|
+
return removed;
|
|
34162
|
+
}
|
|
34163
|
+
/** How many windows are open, for tests and the status report. */
|
|
34164
|
+
windowCount() {
|
|
34165
|
+
return this.windows.size;
|
|
34046
34166
|
}
|
|
34047
34167
|
};
|
|
34048
34168
|
|
|
34049
34169
|
// src/daemon/rules/loader.ts
|
|
34050
|
-
var
|
|
34170
|
+
var import_node_fs29 = require("fs");
|
|
34051
34171
|
var import_node_path20 = require("path");
|
|
34052
34172
|
|
|
34053
34173
|
// src/daemon/rules/default-rules.ts
|
|
@@ -34449,11 +34569,11 @@ var RULES_DIR = "/etc/threatcrush/rules.d";
|
|
|
34449
34569
|
function loadAllRules(customDir) {
|
|
34450
34570
|
const rules = [...DEFAULT_RULES];
|
|
34451
34571
|
const dir = customDir || RULES_DIR;
|
|
34452
|
-
if ((0,
|
|
34453
|
-
const files = (0,
|
|
34572
|
+
if ((0, import_node_fs29.existsSync)(dir)) {
|
|
34573
|
+
const files = (0, import_node_fs29.readdirSync)(dir).filter((f) => f.endsWith(".json"));
|
|
34454
34574
|
for (const file of files) {
|
|
34455
34575
|
try {
|
|
34456
|
-
const raw = (0,
|
|
34576
|
+
const raw = (0, import_node_fs29.readFileSync)((0, import_node_path20.join)(dir, file), "utf-8");
|
|
34457
34577
|
const parsed = JSON.parse(raw);
|
|
34458
34578
|
const customRules = Array.isArray(parsed) ? parsed : [parsed];
|
|
34459
34579
|
for (const rule of customRules) {
|
|
@@ -34485,7 +34605,7 @@ function loadAllRules(customDir) {
|
|
|
34485
34605
|
|
|
34486
34606
|
// src/daemon/firewall/adapters.ts
|
|
34487
34607
|
var import_node_child_process8 = require("child_process");
|
|
34488
|
-
var
|
|
34608
|
+
var import_node_fs30 = require("fs");
|
|
34489
34609
|
var import_node_path21 = require("path");
|
|
34490
34610
|
var import_node_net7 = require("net");
|
|
34491
34611
|
function assertValidFirewallIp(ip) {
|
|
@@ -34499,9 +34619,9 @@ function assertBannableIp(ip) {
|
|
|
34499
34619
|
}
|
|
34500
34620
|
}
|
|
34501
34621
|
function writeIfAbsent(path, contents) {
|
|
34502
|
-
(0,
|
|
34622
|
+
(0, import_node_fs30.mkdirSync)((0, import_node_path21.dirname)(path), { recursive: true });
|
|
34503
34623
|
try {
|
|
34504
|
-
(0,
|
|
34624
|
+
(0, import_node_fs30.writeFileSync)(path, contents, { flag: "wx" });
|
|
34505
34625
|
} catch (err) {
|
|
34506
34626
|
if (err.code !== "EEXIST") throw err;
|
|
34507
34627
|
}
|
|
@@ -34756,7 +34876,7 @@ function detectFirewallAdapter(backend = "auto") {
|
|
|
34756
34876
|
}
|
|
34757
34877
|
|
|
34758
34878
|
// src/daemon/firewall/remediation.ts
|
|
34759
|
-
var
|
|
34879
|
+
var import_node_fs31 = require("fs");
|
|
34760
34880
|
var import_node_net8 = require("net");
|
|
34761
34881
|
init_paths();
|
|
34762
34882
|
init_backoff();
|
|
@@ -35082,7 +35202,7 @@ var RemediationManager = class {
|
|
|
35082
35202
|
*/
|
|
35083
35203
|
async reconcile() {
|
|
35084
35204
|
const now = Date.now();
|
|
35085
|
-
const
|
|
35205
|
+
const live2 = this.blocklist.filter((b) => b.expires_at > now);
|
|
35086
35206
|
const stale = this.blocklist.filter((b) => b.expires_at <= now);
|
|
35087
35207
|
for (const entry of stale) {
|
|
35088
35208
|
if (!entry.dry_run) {
|
|
@@ -35092,7 +35212,7 @@ var RemediationManager = class {
|
|
|
35092
35212
|
}
|
|
35093
35213
|
}
|
|
35094
35214
|
}
|
|
35095
|
-
this.blocklist =
|
|
35215
|
+
this.blocklist = live2;
|
|
35096
35216
|
this.saveState();
|
|
35097
35217
|
let applied = null;
|
|
35098
35218
|
try {
|
|
@@ -35100,7 +35220,7 @@ var RemediationManager = class {
|
|
|
35100
35220
|
} catch {
|
|
35101
35221
|
}
|
|
35102
35222
|
if (applied) {
|
|
35103
|
-
const known = new Set(
|
|
35223
|
+
const known = new Set(live2.filter((b) => !b.dry_run).map((b) => b.ip));
|
|
35104
35224
|
for (const ip of applied) {
|
|
35105
35225
|
if (known.has(ip)) continue;
|
|
35106
35226
|
this.logLine(`[firewall] reconcile: dropping stale rule for ${ip} (no record of why)`);
|
|
@@ -35111,7 +35231,7 @@ var RemediationManager = class {
|
|
|
35111
35231
|
}
|
|
35112
35232
|
}
|
|
35113
35233
|
if (this.config.dry_run) return;
|
|
35114
|
-
for (const entry of
|
|
35234
|
+
for (const entry of live2) {
|
|
35115
35235
|
if (applied?.includes(entry.ip)) continue;
|
|
35116
35236
|
try {
|
|
35117
35237
|
await this.adapter.block(entry.ip);
|
|
@@ -35166,7 +35286,7 @@ var RemediationManager = class {
|
|
|
35166
35286
|
}
|
|
35167
35287
|
logLine(line) {
|
|
35168
35288
|
try {
|
|
35169
|
-
(0,
|
|
35289
|
+
(0, import_node_fs31.appendFileSync)(PATHS.logFile, `${(/* @__PURE__ */ new Date()).toISOString()} ${line}
|
|
35170
35290
|
`);
|
|
35171
35291
|
} catch {
|
|
35172
35292
|
}
|
|
@@ -35204,13 +35324,13 @@ function remediationSettings(section) {
|
|
|
35204
35324
|
init_backoff();
|
|
35205
35325
|
|
|
35206
35326
|
// src/daemon/watchdog-service.ts
|
|
35207
|
-
var
|
|
35327
|
+
var import_node_fs32 = require("fs");
|
|
35208
35328
|
var import_node_path22 = require("path");
|
|
35209
35329
|
function defaultShareGid() {
|
|
35210
35330
|
const isRoot4 = process.platform === "linux" && typeof process.getuid === "function" && process.getuid() === 0;
|
|
35211
35331
|
if (!isRoot4) return void 0;
|
|
35212
35332
|
try {
|
|
35213
|
-
return (0,
|
|
35333
|
+
return (0, import_node_fs32.statSync)("/var/log/auth.log").gid;
|
|
35214
35334
|
} catch {
|
|
35215
35335
|
return void 0;
|
|
35216
35336
|
}
|
|
@@ -35260,6 +35380,9 @@ function startWatchdog(opts) {
|
|
|
35260
35380
|
};
|
|
35261
35381
|
}
|
|
35262
35382
|
|
|
35383
|
+
// src/daemon/index.ts
|
|
35384
|
+
init_backoff();
|
|
35385
|
+
|
|
35263
35386
|
// src/core/telemetry.ts
|
|
35264
35387
|
var ready = false;
|
|
35265
35388
|
var sentry = null;
|
|
@@ -35328,7 +35451,7 @@ function versionOfNode(path) {
|
|
|
35328
35451
|
// src/daemon/index.ts
|
|
35329
35452
|
function readVersion2() {
|
|
35330
35453
|
try {
|
|
35331
|
-
const pkg = JSON.parse((0,
|
|
35454
|
+
const pkg = JSON.parse((0, import_node_fs33.readFileSync)((0, import_node_path23.join)(__dirname, "..", "package.json"), "utf-8"));
|
|
35332
35455
|
return pkg.version || "0.0.0";
|
|
35333
35456
|
} catch {
|
|
35334
35457
|
return "0.0.0";
|
|
@@ -35345,7 +35468,7 @@ async function runHardeningChecks() {
|
|
|
35345
35468
|
}
|
|
35346
35469
|
function logLine(line) {
|
|
35347
35470
|
try {
|
|
35348
|
-
(0,
|
|
35471
|
+
(0, import_node_fs33.appendFileSync)(PATHS.logFile, `${(/* @__PURE__ */ new Date()).toISOString()} ${line}
|
|
35349
35472
|
`);
|
|
35350
35473
|
} catch {
|
|
35351
35474
|
}
|
|
@@ -35387,8 +35510,20 @@ async function runDaemon() {
|
|
|
35387
35510
|
} catch (err) {
|
|
35388
35511
|
logLine(`[daemon] state db unavailable: ${err.message}`);
|
|
35389
35512
|
}
|
|
35390
|
-
const config = loadConfig((0,
|
|
35513
|
+
const config = loadConfig((0, import_node_fs33.existsSync)(PATHS.configFile) ? PATHS.configFile : void 0);
|
|
35391
35514
|
configureAttackDetection(config.detection);
|
|
35515
|
+
const retentionSeconds = parseDuration(config.storage?.event_retention) ?? DEFAULT_EVENT_RETENTION_SECONDS;
|
|
35516
|
+
const maxEvents = config.storage?.max_events ?? DEFAULT_MAX_EVENTS;
|
|
35517
|
+
const prune = () => {
|
|
35518
|
+
try {
|
|
35519
|
+
const n = pruneEvents({ retentionSeconds, maxEvents });
|
|
35520
|
+
if (n) logLine(`[daemon] pruned ${n} events (keep ${config.storage?.event_retention ?? "14d"}, at most ${maxEvents})`);
|
|
35521
|
+
} catch (err) {
|
|
35522
|
+
logLine(`[daemon] event pruning failed: ${err.message}`);
|
|
35523
|
+
}
|
|
35524
|
+
};
|
|
35525
|
+
setTimeout(prune, 5e3);
|
|
35526
|
+
setInterval(prune, 6e4);
|
|
35392
35527
|
bus.on("event", (event) => {
|
|
35393
35528
|
logLine(`[event] ${event.severity} ${event.module} ${event.message}`);
|
|
35394
35529
|
});
|
|
@@ -35416,7 +35551,7 @@ async function runDaemon() {
|
|
|
35416
35551
|
if (event.module !== "rule-engine") ruleEngine.evaluate(event);
|
|
35417
35552
|
});
|
|
35418
35553
|
logLine(`[daemon] rule engine loaded ${ruleEngine.getRules().length} rules`);
|
|
35419
|
-
setInterval(() => ruleEngine.cleanup(),
|
|
35554
|
+
setInterval(() => ruleEngine.cleanup(), 6e4);
|
|
35420
35555
|
const settings = remediationSettings(config.remediation);
|
|
35421
35556
|
const firewallAdapter = detectFirewallAdapter(config.remediation?.backend ?? "auto");
|
|
35422
35557
|
const remediation = new RemediationManager(firewallAdapter, bus, settings);
|
|
@@ -35557,7 +35692,7 @@ async function daemonStart() {
|
|
|
35557
35692
|
return;
|
|
35558
35693
|
}
|
|
35559
35694
|
ensureRuntimeDirs();
|
|
35560
|
-
if (!(0,
|
|
35695
|
+
if (!(0, import_node_fs34.existsSync)(DAEMON_ENTRY)) {
|
|
35561
35696
|
console.log(source_default.red(` Daemon entry not found at ${DAEMON_ENTRY}.`));
|
|
35562
35697
|
console.log(source_default.dim(" Reinstall with `threatcrush update` or `pnpm run build` in the cli package."));
|
|
35563
35698
|
return;
|
|
@@ -35565,8 +35700,8 @@ async function daemonStart() {
|
|
|
35565
35700
|
let out;
|
|
35566
35701
|
let err;
|
|
35567
35702
|
try {
|
|
35568
|
-
out = (0,
|
|
35569
|
-
err = (0,
|
|
35703
|
+
out = (0, import_node_fs35.openSync)(PATHS.logFile, "a");
|
|
35704
|
+
err = (0, import_node_fs35.openSync)(PATHS.logFile, "a");
|
|
35570
35705
|
} catch (e) {
|
|
35571
35706
|
const code = e.code;
|
|
35572
35707
|
console.log(source_default.red(` \u2717 Cannot write daemon log at ${PATHS.logFile} (${code ?? "error"}).`));
|
|
@@ -35654,12 +35789,12 @@ async function waitForExit(pid, timeoutMs) {
|
|
|
35654
35789
|
}
|
|
35655
35790
|
async function releaseStaleSocket() {
|
|
35656
35791
|
const deadline = Date.now() + 2e3;
|
|
35657
|
-
while ((0,
|
|
35792
|
+
while ((0, import_node_fs34.existsSync)(PATHS.socket) && Date.now() < deadline) {
|
|
35658
35793
|
await new Promise((r) => setTimeout(r, 50));
|
|
35659
35794
|
}
|
|
35660
|
-
if ((0,
|
|
35795
|
+
if ((0, import_node_fs34.existsSync)(PATHS.socket)) {
|
|
35661
35796
|
try {
|
|
35662
|
-
(0,
|
|
35797
|
+
(0, import_node_fs34.unlinkSync)(PATHS.socket);
|
|
35663
35798
|
} catch {
|
|
35664
35799
|
}
|
|
35665
35800
|
}
|
|
@@ -35667,26 +35802,26 @@ async function releaseStaleSocket() {
|
|
|
35667
35802
|
|
|
35668
35803
|
// src/commands/service.ts
|
|
35669
35804
|
var import_node_child_process12 = require("child_process");
|
|
35670
|
-
var
|
|
35805
|
+
var import_node_fs36 = require("fs");
|
|
35671
35806
|
var import_node_path25 = require("path");
|
|
35672
35807
|
var import_node_os10 = require("os");
|
|
35673
35808
|
var UNIT_PATH = "/etc/systemd/system/threatcrushd.service";
|
|
35674
35809
|
var DAEMON_UNIT_NAME = "threatcrushd.service";
|
|
35675
35810
|
function resolveTemplate() {
|
|
35676
35811
|
const templatePath = (0, import_node_path25.join)(__dirname, "systemd", "threatcrushd.service");
|
|
35677
|
-
if (!(0,
|
|
35812
|
+
if (!(0, import_node_fs36.existsSync)(templatePath)) {
|
|
35678
35813
|
throw new Error(`systemd unit template not found at ${templatePath}`);
|
|
35679
35814
|
}
|
|
35680
|
-
return (0,
|
|
35815
|
+
return (0, import_node_fs36.readFileSync)(templatePath, "utf-8");
|
|
35681
35816
|
}
|
|
35682
35817
|
function stableBinPath(binPath) {
|
|
35683
35818
|
const stable = binPath.replace(/\/\.pnpm\/[^/]+\/node_modules\//, "/node_modules/");
|
|
35684
35819
|
if (stable === binPath) return binPath;
|
|
35685
|
-
return (0,
|
|
35820
|
+
return (0, import_node_fs36.existsSync)(stable) ? stable : binPath;
|
|
35686
35821
|
}
|
|
35687
35822
|
function resolveBinPath() {
|
|
35688
35823
|
const arg = process.argv[1];
|
|
35689
|
-
if (arg && (0,
|
|
35824
|
+
if (arg && (0, import_node_fs36.existsSync)(arg)) return stableBinPath(arg);
|
|
35690
35825
|
try {
|
|
35691
35826
|
return (0, import_node_child_process12.execSync)("command -v threatcrush", { encoding: "utf-8" }).trim();
|
|
35692
35827
|
} catch {
|
|
@@ -35696,11 +35831,11 @@ function resolveBinPath() {
|
|
|
35696
35831
|
function stableNodePath(execPath) {
|
|
35697
35832
|
const stable = execPath.replace(/(\/installs\/node\/)[^/]+\//, "$1latest/");
|
|
35698
35833
|
if (stable === execPath) return execPath;
|
|
35699
|
-
return (0,
|
|
35834
|
+
return (0, import_node_fs36.existsSync)(stable) ? stable : execPath;
|
|
35700
35835
|
}
|
|
35701
35836
|
function looksLikeNodeScript(path) {
|
|
35702
35837
|
try {
|
|
35703
|
-
const head = (0,
|
|
35838
|
+
const head = (0, import_node_fs36.readFileSync)(path, "utf-8").slice(0, 120);
|
|
35704
35839
|
return /^#!.*\bnode\b/.test(head);
|
|
35705
35840
|
} catch {
|
|
35706
35841
|
return false;
|
|
@@ -35740,7 +35875,7 @@ function callerHome() {
|
|
|
35740
35875
|
try {
|
|
35741
35876
|
const line = (0, import_node_child_process12.execSync)(`getent passwd ${user}`, { encoding: "utf-8" }).trim();
|
|
35742
35877
|
const home = line.split(":")[5];
|
|
35743
|
-
return home && (0,
|
|
35878
|
+
return home && (0, import_node_fs36.existsSync)(home) ? home : null;
|
|
35744
35879
|
} catch {
|
|
35745
35880
|
return null;
|
|
35746
35881
|
}
|
|
@@ -35751,10 +35886,10 @@ function stopUserModeDaemon() {
|
|
|
35751
35886
|
const runDir = (0, import_node_path25.join)(home, ".threatcrush", "run");
|
|
35752
35887
|
const pidFile = (0, import_node_path25.join)(runDir, "threatcrushd.pid");
|
|
35753
35888
|
const socket = (0, import_node_path25.join)(runDir, "threatcrushd.sock");
|
|
35754
|
-
if (!(0,
|
|
35889
|
+
if (!(0, import_node_fs36.existsSync)(pidFile) && !(0, import_node_fs36.existsSync)(socket)) return;
|
|
35755
35890
|
let pid = null;
|
|
35756
35891
|
try {
|
|
35757
|
-
pid = Number.parseInt((0,
|
|
35892
|
+
pid = Number.parseInt((0, import_node_fs36.readFileSync)(pidFile, "utf-8").trim(), 10);
|
|
35758
35893
|
} catch {
|
|
35759
35894
|
pid = null;
|
|
35760
35895
|
}
|
|
@@ -35767,7 +35902,7 @@ function stopUserModeDaemon() {
|
|
|
35767
35902
|
}
|
|
35768
35903
|
for (const path of [socket, pidFile]) {
|
|
35769
35904
|
try {
|
|
35770
|
-
if ((0,
|
|
35905
|
+
if ((0, import_node_fs36.existsSync)(path)) (0, import_node_fs36.rmSync)(path);
|
|
35771
35906
|
} catch {
|
|
35772
35907
|
}
|
|
35773
35908
|
}
|
|
@@ -35785,7 +35920,7 @@ async function installServiceCommand() {
|
|
|
35785
35920
|
}
|
|
35786
35921
|
const noSystemd = systemdUnavailableReason({
|
|
35787
35922
|
hasSystemctl: (0, import_node_child_process12.spawnSync)("sh", ["-c", "command -v systemctl"], { stdio: "ignore" }).status === 0,
|
|
35788
|
-
booted: (0,
|
|
35923
|
+
booted: (0, import_node_fs36.existsSync)("/run/systemd/system")
|
|
35789
35924
|
});
|
|
35790
35925
|
if (noSystemd) {
|
|
35791
35926
|
console.log(source_default.red(` \u2717 systemd isn't available here: ${noSystemd}.`));
|
|
@@ -35807,7 +35942,7 @@ async function installServiceCommand() {
|
|
|
35807
35942
|
const isNodeScript = looksLikeNodeScript(binPath);
|
|
35808
35943
|
let nodePath = stableNodePath(process.execPath);
|
|
35809
35944
|
if (isNodeScript && nodeVersionDir(binPath)) {
|
|
35810
|
-
binPath = (0,
|
|
35945
|
+
binPath = (0, import_node_fs36.realpathSync)(binPath);
|
|
35811
35946
|
nodePath = process.execPath;
|
|
35812
35947
|
}
|
|
35813
35948
|
if (isNodeScript) {
|
|
@@ -35822,7 +35957,7 @@ async function installServiceCommand() {
|
|
|
35822
35957
|
const exec = execStartCommand(binPath, nodePath, isNodeScript);
|
|
35823
35958
|
const configDir = (0, import_node_path25.join)((0, import_node_os10.homedir)(), ".threatcrush");
|
|
35824
35959
|
const unit = renderUnit(resolveTemplate(), { exec, binPath, configDir });
|
|
35825
|
-
(0,
|
|
35960
|
+
(0, import_node_fs36.writeFileSync)(UNIT_PATH, unit, { mode: 420 });
|
|
35826
35961
|
console.log(source_default.green(` \u2713 Installed unit file: ${UNIT_PATH}`));
|
|
35827
35962
|
console.log(source_default.dim(` ExecStart=${exec}`));
|
|
35828
35963
|
const nodeDir = nodeVersionDir(nodePath);
|
|
@@ -35871,20 +36006,20 @@ function ensureSystemDirs(configDir) {
|
|
|
35871
36006
|
];
|
|
35872
36007
|
let admGid = null;
|
|
35873
36008
|
try {
|
|
35874
|
-
admGid = (0,
|
|
36009
|
+
admGid = (0, import_node_fs36.statSync)("/var/log/auth.log").gid;
|
|
35875
36010
|
} catch {
|
|
35876
36011
|
}
|
|
35877
36012
|
for (const { path, groupWritable, sticky } of dirs) {
|
|
35878
36013
|
try {
|
|
35879
|
-
(0,
|
|
36014
|
+
(0, import_node_fs36.mkdirSync)(path, { recursive: true });
|
|
35880
36015
|
} catch {
|
|
35881
36016
|
}
|
|
35882
36017
|
try {
|
|
35883
36018
|
if (groupWritable && admGid !== null) {
|
|
35884
|
-
(0,
|
|
36019
|
+
(0, import_node_fs36.chmodSync)(path, sticky ? 1533 : 509);
|
|
35885
36020
|
(0, import_node_child_process12.execSync)(`chgrp adm ${path}`, { stdio: "ignore" });
|
|
35886
36021
|
} else {
|
|
35887
|
-
(0,
|
|
36022
|
+
(0, import_node_fs36.chmodSync)(path, 493);
|
|
35888
36023
|
}
|
|
35889
36024
|
} catch {
|
|
35890
36025
|
}
|
|
@@ -35912,7 +36047,7 @@ async function uninstallServiceCommand() {
|
|
|
35912
36047
|
} catch {
|
|
35913
36048
|
}
|
|
35914
36049
|
try {
|
|
35915
|
-
if ((0,
|
|
36050
|
+
if ((0, import_node_fs36.existsSync)(UNIT_PATH)) {
|
|
35916
36051
|
(0, import_node_child_process12.execSync)(`rm -f ${UNIT_PATH}`);
|
|
35917
36052
|
console.log(source_default.green(` \u2713 Removed unit file: ${UNIT_PATH}`));
|
|
35918
36053
|
}
|
|
@@ -35926,7 +36061,7 @@ async function uninstallServiceCommand() {
|
|
|
35926
36061
|
}
|
|
35927
36062
|
|
|
35928
36063
|
// src/commands/save.ts
|
|
35929
|
-
var
|
|
36064
|
+
var import_node_fs37 = require("fs");
|
|
35930
36065
|
var import_toml5 = __toESM(require_toml());
|
|
35931
36066
|
init_paths();
|
|
35932
36067
|
var SECRET_KEY = /pass(word)?|secret|token|api_?key|private|webhook|dsn|url|routing_key|auth/i;
|
|
@@ -35964,9 +36099,9 @@ async function saveCommand(opts = {}) {
|
|
|
35964
36099
|
}
|
|
35965
36100
|
const path = opts.config ?? PATHS.configFile;
|
|
35966
36101
|
let config = {};
|
|
35967
|
-
if ((0,
|
|
36102
|
+
if ((0, import_node_fs37.existsSync)(path)) {
|
|
35968
36103
|
try {
|
|
35969
|
-
config = import_toml5.default.parse((0,
|
|
36104
|
+
config = import_toml5.default.parse((0, import_node_fs37.readFileSync)(path, "utf-8"));
|
|
35970
36105
|
} catch (err) {
|
|
35971
36106
|
console.log(source_default.red(` \u2717 ${path} is not valid TOML: ${err.message}
|
|
35972
36107
|
`));
|
|
@@ -36078,13 +36213,13 @@ function welcomeCommand() {
|
|
|
36078
36213
|
}
|
|
36079
36214
|
|
|
36080
36215
|
// src/commands/properties.ts
|
|
36081
|
-
var
|
|
36216
|
+
var import_node_fs38 = require("fs");
|
|
36082
36217
|
var import_node_path26 = require("path");
|
|
36083
|
-
var
|
|
36218
|
+
var import_node_readline4 = __toESM(require("readline"));
|
|
36084
36219
|
var API_URL5 = process.env.THREATCRUSH_API_URL || "https://threatcrush.com";
|
|
36085
36220
|
var KINDS = ["url", "api", "domain", "ip", "repo"];
|
|
36086
36221
|
function prompt(question) {
|
|
36087
|
-
const rl =
|
|
36222
|
+
const rl = import_node_readline4.default.createInterface({ input: process.stdin, output: process.stdout });
|
|
36088
36223
|
return new Promise((resolve6) => rl.question(question, (answer) => {
|
|
36089
36224
|
rl.close();
|
|
36090
36225
|
resolve6(answer.trim());
|
|
@@ -36409,7 +36544,7 @@ async function propertiesRunsCommand(opts) {
|
|
|
36409
36544
|
}
|
|
36410
36545
|
function parseImportFile(path) {
|
|
36411
36546
|
const ext = (0, import_node_path26.extname)(path).toLowerCase();
|
|
36412
|
-
const raw = (0,
|
|
36547
|
+
const raw = (0, import_node_fs38.readFileSync)(path, "utf-8");
|
|
36413
36548
|
if (ext === ".json") {
|
|
36414
36549
|
const parsed = JSON.parse(raw);
|
|
36415
36550
|
if (!Array.isArray(parsed)) throw new Error("JSON must be an array of objects");
|
|
@@ -36604,7 +36739,7 @@ async function rulesCommand(opts) {
|
|
|
36604
36739
|
}
|
|
36605
36740
|
|
|
36606
36741
|
// src/commands/harden.ts
|
|
36607
|
-
var
|
|
36742
|
+
var import_node_fs39 = require("fs");
|
|
36608
36743
|
var import_node_child_process13 = require("child_process");
|
|
36609
36744
|
function tryExec(cmd) {
|
|
36610
36745
|
try {
|
|
@@ -36615,7 +36750,7 @@ function tryExec(cmd) {
|
|
|
36615
36750
|
}
|
|
36616
36751
|
function tryRead(path) {
|
|
36617
36752
|
try {
|
|
36618
|
-
return (0,
|
|
36753
|
+
return (0, import_node_fs39.readFileSync)(path, "utf-8");
|
|
36619
36754
|
} catch {
|
|
36620
36755
|
return null;
|
|
36621
36756
|
}
|
|
@@ -36719,8 +36854,8 @@ function checkSshWeakConfig() {
|
|
|
36719
36854
|
};
|
|
36720
36855
|
}
|
|
36721
36856
|
function checkAutoUpdates() {
|
|
36722
|
-
const unattended = (0,
|
|
36723
|
-
const dnfAuto = (0,
|
|
36857
|
+
const unattended = (0, import_node_fs39.existsSync)("/etc/apt/apt.conf.d/20auto-upgrades") || (0, import_node_fs39.existsSync)("/etc/apt/apt.conf.d/50unattended-upgrades");
|
|
36858
|
+
const dnfAuto = (0, import_node_fs39.existsSync)("/etc/dnf/automatic.conf");
|
|
36724
36859
|
if (unattended || dnfAuto) {
|
|
36725
36860
|
return {
|
|
36726
36861
|
key: "auto-updates",
|
|
@@ -36846,7 +36981,7 @@ function checkFail2ban() {
|
|
|
36846
36981
|
explanation: "fail2ban is installed and running."
|
|
36847
36982
|
};
|
|
36848
36983
|
}
|
|
36849
|
-
if ((0,
|
|
36984
|
+
if ((0, import_node_fs39.existsSync)("/etc/fail2ban/fail2ban.conf")) {
|
|
36850
36985
|
return {
|
|
36851
36986
|
key: checkKey,
|
|
36852
36987
|
status: "warn",
|
|
@@ -37320,7 +37455,7 @@ function detectPackageManager() {
|
|
|
37320
37455
|
}
|
|
37321
37456
|
function readInstallConfig() {
|
|
37322
37457
|
try {
|
|
37323
|
-
return JSON.parse((0,
|
|
37458
|
+
return JSON.parse((0, import_node_fs40.readFileSync)(INSTALL_CONFIG_PATH, "utf-8"));
|
|
37324
37459
|
} catch {
|
|
37325
37460
|
return {};
|
|
37326
37461
|
}
|
|
@@ -37360,8 +37495,8 @@ async function liveDaemon() {
|
|
|
37360
37495
|
const client = new IpcClient();
|
|
37361
37496
|
try {
|
|
37362
37497
|
await client.connect();
|
|
37363
|
-
const
|
|
37364
|
-
return { pid, version:
|
|
37498
|
+
const live2 = await client.status();
|
|
37499
|
+
return { pid, version: live2.version ?? null };
|
|
37365
37500
|
} catch {
|
|
37366
37501
|
return { pid, version: null };
|
|
37367
37502
|
} finally {
|
|
@@ -37613,7 +37748,7 @@ program2.command("uninstall-service").description("Remove the threatcrushd syste
|
|
|
37613
37748
|
program2.command("logs").description("Tail daemon logs").action(async () => {
|
|
37614
37749
|
console.log(LOGO2);
|
|
37615
37750
|
const logPath = PATHS.logFile;
|
|
37616
|
-
if (!(0,
|
|
37751
|
+
if (!(0, import_node_fs40.existsSync)(logPath)) {
|
|
37617
37752
|
console.log(source_default.yellow(` No log file found at ${logPath}`));
|
|
37618
37753
|
console.log(source_default.dim(" Start the daemon first with `threatcrush start`\n"));
|
|
37619
37754
|
return;
|
|
@@ -37789,7 +37924,7 @@ storeCmd.command("publish <url>").description("Publish a module from a git URL o
|
|
|
37789
37924
|
const configPath = (0, import_node_path27.join)((0, import_node_os11.homedir)(), ".threatcrush", "config.json");
|
|
37790
37925
|
let email = "";
|
|
37791
37926
|
try {
|
|
37792
|
-
const config = JSON.parse((0,
|
|
37927
|
+
const config = JSON.parse((0, import_node_fs40.readFileSync)(configPath, "utf-8"));
|
|
37793
37928
|
email = config.email || "";
|
|
37794
37929
|
} catch {
|
|
37795
37930
|
}
|
|
@@ -37807,8 +37942,8 @@ storeCmd.command("publish <url>").description("Publish a module from a git URL o
|
|
|
37807
37942
|
}
|
|
37808
37943
|
try {
|
|
37809
37944
|
const dir = (0, import_node_path27.join)((0, import_node_os11.homedir)(), ".threatcrush");
|
|
37810
|
-
if (!(0,
|
|
37811
|
-
(0,
|
|
37945
|
+
if (!(0, import_node_fs40.existsSync)(dir)) (0, import_node_fs40.mkdirSync)(dir, { recursive: true });
|
|
37946
|
+
(0, import_node_fs40.writeFileSync)(configPath, JSON.stringify({ email }, null, 2));
|
|
37812
37947
|
console.log(source_default.dim(` Saved email to ${configPath}`));
|
|
37813
37948
|
} catch {
|
|
37814
37949
|
}
|
|
@@ -37905,8 +38040,8 @@ orgsCmd.command("create <name>").description("Create a new organization").action
|
|
|
37905
38040
|
orgsCmd.command("use <slug>").description("Switch to an organization").action(async (slug) => {
|
|
37906
38041
|
await orgsCommand({ action: "use", name: slug });
|
|
37907
38042
|
});
|
|
37908
|
-
var serversCmd = program2.command("servers").alias("server").alias("srv").description("Manage servers").action(async (
|
|
37909
|
-
await serversCommand({ action:
|
|
38043
|
+
var serversCmd = program2.command("servers").alias("server").alias("srv").description("Manage servers").action(async () => {
|
|
38044
|
+
await serversCommand({ action: "list" });
|
|
37910
38045
|
});
|
|
37911
38046
|
serversCmd.command("list").alias("ls").option("--org <slug>", "Filter by organization slug").description("List servers in current organization").action(async (opts) => {
|
|
37912
38047
|
await serversCommand({ action: "list", org: opts.org });
|