@astrofoundry/pi-astro 0.36.1 → 0.37.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/extensions/astro-discord/config.ts +6 -2
- package/extensions/astro-discord/discord.test.ts +18 -0
- package/extensions/astro-discord/index.ts +68 -7
- package/extensions/astro-discord/reports.ts +15 -4
- package/extensions/astro-discord/rest.ts +15 -3
- package/extensions/astro-discord/security-monitor.test.ts +338 -0
- package/extensions/astro-discord/security-monitor.ts +602 -0
- package/extensions/astro-discord/security.test.ts +498 -0
- package/extensions/astro-discord/security.ts +740 -0
- package/package.json +1 -1
- package/skills/arcane/SKILL.md +2 -0
- package/skills/security/SKILL.md +6 -3
- package/specialists/AGENTS.md +2 -1
- package/specialists/README.md +2 -2
- package/specialists/arcane/run.ts +42 -1
- package/specialists/entry/remote/spc-security-obs-entry.sh +1 -1
- package/specialists/lib/change-origin.test.ts +97 -0
- package/specialists/lib/change-origin.ts +121 -0
- package/specialists/security/run.ts +199 -31
- package/specialists/wrappers.test.ts +111 -1
|
@@ -0,0 +1,602 @@
|
|
|
1
|
+
import { chunkMessage } from "./chunk.ts";
|
|
2
|
+
import { type DiscordConfig, isOwner, type ReportSchedule, reportSchedule } from "./config.ts";
|
|
3
|
+
import { KeyedQueue } from "./media.ts";
|
|
4
|
+
import { MONDAY, writeReportState, zonedWeekday } from "./reports.ts";
|
|
5
|
+
import type { CreateMessage, DiscordMessage, MessageComponent } from "./rest.ts";
|
|
6
|
+
import {
|
|
7
|
+
acknowledge,
|
|
8
|
+
type AlertSummary,
|
|
9
|
+
alertComponents,
|
|
10
|
+
alertKey,
|
|
11
|
+
applyVulnWindow,
|
|
12
|
+
type Attention,
|
|
13
|
+
backupGuests,
|
|
14
|
+
checksFailingText,
|
|
15
|
+
checksRecoveredText,
|
|
16
|
+
classify,
|
|
17
|
+
clockText,
|
|
18
|
+
crlFromEvents,
|
|
19
|
+
dailyNeedsOwner,
|
|
20
|
+
dailyText,
|
|
21
|
+
healthChange,
|
|
22
|
+
investigationTask,
|
|
23
|
+
isAcked,
|
|
24
|
+
MAX_POSTS_PER_CHECK,
|
|
25
|
+
needsOrigin,
|
|
26
|
+
noticeText,
|
|
27
|
+
ORIGIN_RETRY_MS,
|
|
28
|
+
type OriginVerdict,
|
|
29
|
+
originArgs,
|
|
30
|
+
parseAttention,
|
|
31
|
+
parseOrigin,
|
|
32
|
+
parseSecurityButton,
|
|
33
|
+
type PatchingFacts,
|
|
34
|
+
type PostedAlert,
|
|
35
|
+
pruneState,
|
|
36
|
+
readSecurityState,
|
|
37
|
+
repeatText,
|
|
38
|
+
resticStatus,
|
|
39
|
+
type SecurityState,
|
|
40
|
+
type VulnEvent,
|
|
41
|
+
vulnWindow,
|
|
42
|
+
WATCH_LEVEL,
|
|
43
|
+
type WazuhEvent,
|
|
44
|
+
wazuhDigest,
|
|
45
|
+
weekRange,
|
|
46
|
+
weeklyText,
|
|
47
|
+
writeSecurityState,
|
|
48
|
+
} from "./security.ts";
|
|
49
|
+
|
|
50
|
+
/** The Discord calls the monitor needs; `DiscordRest` has them all. */
|
|
51
|
+
export interface SecurityRest {
|
|
52
|
+
createMessage(channelId: string, message: CreateMessage): Promise<DiscordMessage>;
|
|
53
|
+
createComponentsMessage(channelId: string, components: MessageComponent[], mentionUsers?: string[]): Promise<DiscordMessage>;
|
|
54
|
+
editComponents(channelId: string, messageId: string, components: MessageComponent[]): Promise<void>;
|
|
55
|
+
ephemeralReply(interactionId: string, interactionToken: string, content: string): Promise<void>;
|
|
56
|
+
deferUpdate(interactionId: string, interactionToken: string): Promise<void>;
|
|
57
|
+
startThread(channelId: string, messageId: string, name: string): Promise<{ id: string }>;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export interface InvestigationRequest {
|
|
61
|
+
threadId: string;
|
|
62
|
+
/** The bot's first message in the thread; the run's reactions and replies attach to it. */
|
|
63
|
+
starterId: string;
|
|
64
|
+
userId: string;
|
|
65
|
+
task: string;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export interface SecurityDeps {
|
|
69
|
+
rest: SecurityRest;
|
|
70
|
+
runEntry: (service: string, caller: string, args: string[], timeoutMs: number) => Promise<string>;
|
|
71
|
+
config: () => DiscordConfig;
|
|
72
|
+
log: (line: string) => void;
|
|
73
|
+
/** Runs astro.security on the task in the thread; resolves true when it answered. */
|
|
74
|
+
investigate: (request: InvestigationRequest) => Promise<boolean>;
|
|
75
|
+
stopping: () => boolean;
|
|
76
|
+
now: () => number;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export interface SecurityClick {
|
|
80
|
+
interactionId: string;
|
|
81
|
+
token: string;
|
|
82
|
+
userId: string;
|
|
83
|
+
messageId: string;
|
|
84
|
+
customId: string;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const MINUTE = 60_000;
|
|
88
|
+
const DAY = 86_400_000;
|
|
89
|
+
export const POLL_MS = 2 * MINUTE;
|
|
90
|
+
export const HEALTH_MS = 10 * MINUTE;
|
|
91
|
+
/** Each check reads a little before the last one started: Wazuh writes an alert a moment after its timestamp. */
|
|
92
|
+
const OVERLAP_MINUTES = 5;
|
|
93
|
+
const FIRST_WINDOW_MINUTES = 10;
|
|
94
|
+
/** The guest dump covers about two days; a longer outage is left to the reports. */
|
|
95
|
+
const MAX_WINDOW_MINUTES = 2880;
|
|
96
|
+
/** Fifteen failed checks (30 minutes) or three failed health reads (30 minutes) tag the owners once. */
|
|
97
|
+
const POLL_FAILS_TO_WARN = 15;
|
|
98
|
+
const HEALTH_FAILS_TO_WARN = 3;
|
|
99
|
+
const MAX_ORIGIN_CALLS = 20;
|
|
100
|
+
const CALLER = "security-alerts";
|
|
101
|
+
const REPORT_CALLER = "security-report";
|
|
102
|
+
|
|
103
|
+
function reason(err: unknown): string {
|
|
104
|
+
return (err instanceof Error ? err.message : String(err)).split("\n")[0].slice(0, 300);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function isEvent(value: unknown): value is WazuhEvent {
|
|
108
|
+
const e = value as Partial<WazuhEvent> | null;
|
|
109
|
+
return !!e && typeof e.id === "string" && typeof e.timestamp === "string" && typeof e.rule === "string" && typeof e.agent === "string" && typeof e.level === "number" && typeof e.description === "string";
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export function parseEvents(value: unknown): WazuhEvent[] | null {
|
|
113
|
+
const r = value as { events?: unknown } | null;
|
|
114
|
+
return r && Array.isArray(r.events) && r.events.every(isEvent) ? r.events : null;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export function parseVulnEvents(value: unknown): VulnEvent[] | null {
|
|
118
|
+
const r = value as { events?: unknown } | null;
|
|
119
|
+
if (!r || !Array.isArray(r.events)) return null;
|
|
120
|
+
return r.events.every((e: Partial<VulnEvent>) => typeof e.agent === "string" && typeof e.cve === "string" && typeof e.status === "string" && typeof e.timestamp === "string") ? (r.events as VulnEvent[]) : null;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** `alerts <days>`; a wrapper older than the ban-length fields gives empty lists for them. */
|
|
124
|
+
export function parseAlertSummary(value: unknown): AlertSummary | null {
|
|
125
|
+
const r = value as Partial<AlertSummary> | null;
|
|
126
|
+
if (!r || typeof r.count !== "number" || typeof r.uniqueIps !== "number" || !Array.isArray(r.scenarios)) return null;
|
|
127
|
+
return { ...(r as AlertSummary), bans: r.bans ?? [], escalated: r.escalated ?? [], countries: r.countries ?? [], networks: r.networks ?? [] };
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** Installs, agent restarts, and pending reboots from `wazuh-alerts 7 3` groups and the week's level-10 events. */
|
|
131
|
+
export function patchingFacts(summary: unknown, events: readonly WazuhEvent[] | null, now: number): PatchingFacts | null {
|
|
132
|
+
const groups = (summary as { groups?: { rule?: unknown; agent?: unknown; count?: unknown; last?: unknown }[] } | null)?.groups;
|
|
133
|
+
if (!Array.isArray(groups)) return null;
|
|
134
|
+
const installs = new Map<string, number>();
|
|
135
|
+
const restarts: PatchingFacts["restarts"] = [];
|
|
136
|
+
for (const g of groups) {
|
|
137
|
+
if (typeof g.agent !== "string" || typeof g.count !== "number") continue;
|
|
138
|
+
if (g.rule === "100001") installs.set(g.agent, (installs.get(g.agent) ?? 0) + g.count);
|
|
139
|
+
if (g.rule === "503" && typeof g.last === "string") restarts.push({ agent: g.agent, last: g.last });
|
|
140
|
+
}
|
|
141
|
+
const rebootPending = [...new Set((events ?? []).filter((e) => e.rule === "100010" && Date.parse(e.timestamp) >= now - 2 * DAY).map((e) => e.agent))].sort();
|
|
142
|
+
return { installs: [...installs.entries()].map(([agent, count]) => ({ agent, count })), restarts, rebootPending };
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** Whether an urgent event's key is or was acknowledged; acknowledged alerts still count in the reports. */
|
|
146
|
+
function acked(state: SecurityState, e: WazuhEvent): boolean {
|
|
147
|
+
const key = alertKey(e);
|
|
148
|
+
return state.acks[key] !== undefined || state.alerts.some((a) => a.key === key && a.ack !== undefined);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function plainOrigin(o: (OriginVerdict & { at?: string }) | undefined): OriginVerdict | undefined {
|
|
152
|
+
return o ? { verdict: o.verdict, reason: o.reason } : undefined;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Urgent Wazuh alerts every two minutes, health every ten, and the Security
|
|
157
|
+
* Daily and Weekly Reports, all through the security specialist's read-only
|
|
158
|
+
* wrapper (and `arcane change-origin` for deployment files), without a model.
|
|
159
|
+
* Every state change goes through one queue, so a click never overwrites a check.
|
|
160
|
+
*/
|
|
161
|
+
export class SecurityMonitor {
|
|
162
|
+
private readonly deps: SecurityDeps;
|
|
163
|
+
private readonly queue = new KeyedQueue();
|
|
164
|
+
private polling = false;
|
|
165
|
+
private checkingHealth = false;
|
|
166
|
+
private lastPoll = 0;
|
|
167
|
+
private lastHealth = 0;
|
|
168
|
+
|
|
169
|
+
constructor(deps: SecurityDeps) {
|
|
170
|
+
this.deps = deps;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
private schedule(): ReportSchedule | undefined {
|
|
174
|
+
return reportSchedule(this.deps.config(), "security");
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
private owners(): string[] {
|
|
178
|
+
return this.deps.config().owners;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/** One state change at a time; returns what `fn` returns. */
|
|
182
|
+
private async update<T>(fn: (state: SecurityState) => Promise<T> | T): Promise<T> {
|
|
183
|
+
let out: T | undefined;
|
|
184
|
+
await this.queue.run("state", async () => {
|
|
185
|
+
const state = readSecurityState();
|
|
186
|
+
out = await fn(state);
|
|
187
|
+
writeSecurityState(state);
|
|
188
|
+
});
|
|
189
|
+
return out as T;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/** Called every minute: starts the alert check and the health check when each is due. */
|
|
193
|
+
tick(): void {
|
|
194
|
+
const s = this.schedule();
|
|
195
|
+
if (!s || this.deps.stopping()) return;
|
|
196
|
+
const now = this.deps.now();
|
|
197
|
+
if (!this.polling && now - this.lastPoll >= POLL_MS - 5000) {
|
|
198
|
+
this.polling = true;
|
|
199
|
+
this.lastPoll = now;
|
|
200
|
+
void this.poll(s)
|
|
201
|
+
.catch((err) => this.deps.log(`security: check failed: ${reason(err)}`))
|
|
202
|
+
.finally(() => (this.polling = false));
|
|
203
|
+
}
|
|
204
|
+
if (!this.checkingHealth && now - this.lastHealth >= HEALTH_MS - 5000) {
|
|
205
|
+
this.checkingHealth = true;
|
|
206
|
+
this.lastHealth = now;
|
|
207
|
+
void this.checkHealth(s)
|
|
208
|
+
.catch((err) => this.deps.log(`security: health check failed: ${reason(err)}`))
|
|
209
|
+
.finally(() => (this.checkingHealth = false));
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
private async entryJson(service: string, caller: string, args: string[], timeoutMs: number): Promise<unknown> {
|
|
214
|
+
return JSON.parse(await this.deps.runEntry(service, caller, args, timeoutMs));
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/** The origin of each deployment-file change, or the error when the check failed. */
|
|
218
|
+
private async origins(events: readonly WazuhEvent[], caller: string): Promise<Map<string, OriginVerdict | { error: string }>> {
|
|
219
|
+
const out = new Map<string, OriginVerdict | { error: string }>();
|
|
220
|
+
for (const e of events.slice(0, MAX_ORIGIN_CALLS)) {
|
|
221
|
+
const args = originArgs(e);
|
|
222
|
+
if (!args) {
|
|
223
|
+
out.set(e.id, { verdict: "unchecked", reason: "Wazuh sent no SHA-256 for the new content" });
|
|
224
|
+
continue;
|
|
225
|
+
}
|
|
226
|
+
try {
|
|
227
|
+
const verdict = parseOrigin(await this.entryJson("arcane", caller, args, 3 * MINUTE));
|
|
228
|
+
out.set(e.id, verdict ?? { error: "change-origin returned an unexpected answer" });
|
|
229
|
+
} catch (err) {
|
|
230
|
+
out.set(e.id, { error: reason(err) });
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
for (const e of events.slice(MAX_ORIGIN_CALLS)) out.set(e.id, { error: "too many changes in one check; retried next time" });
|
|
234
|
+
return out;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
async poll(s: ReportSchedule): Promise<void> {
|
|
238
|
+
const started = this.deps.now();
|
|
239
|
+
const before = readSecurityState();
|
|
240
|
+
const cursor = before.cursor ? Date.parse(before.cursor) : started - FIRST_WINDOW_MINUTES * MINUTE;
|
|
241
|
+
const minutes = Math.min(MAX_WINDOW_MINUTES, Math.max(FIRST_WINDOW_MINUTES, Math.ceil((started - cursor) / MINUTE) + OVERLAP_MINUTES));
|
|
242
|
+
let events: WazuhEvent[] | null;
|
|
243
|
+
try {
|
|
244
|
+
events = parseEvents(await this.entryJson("security", CALLER, ["wazuh-events", String(minutes), String(WATCH_LEVEL)], 3 * MINUTE));
|
|
245
|
+
if (!events) throw new Error("wazuh-events returned an unexpected answer");
|
|
246
|
+
} catch (err) {
|
|
247
|
+
await this.pollFailed(s, reason(err));
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
const fresh = events.filter((e) => before.seen[e.id] === undefined);
|
|
251
|
+
const checks = await this.origins([...before.pending.map((p) => p.event), ...fresh.filter(needsOrigin)], CALLER);
|
|
252
|
+
await this.update(async (state) => {
|
|
253
|
+
const now = this.deps.now();
|
|
254
|
+
pruneState(state, now);
|
|
255
|
+
if (state.poll.warned) await this.post(s.channelId, { content: checksRecoveredText("Wazuh alerts") });
|
|
256
|
+
state.poll = { failures: 0, warned: false };
|
|
257
|
+
const ready: WazuhEvent[] = [];
|
|
258
|
+
const pending: SecurityState["pending"] = [];
|
|
259
|
+
const settle = (e: WazuhEvent, since: string) => {
|
|
260
|
+
const r = checks.get(e.id);
|
|
261
|
+
if (r && !("error" in r)) {
|
|
262
|
+
state.origins[e.id] = { ...r, at: e.timestamp };
|
|
263
|
+
ready.push(e);
|
|
264
|
+
} else if (now - Date.parse(since) >= ORIGIN_RETRY_MS) {
|
|
265
|
+
state.origins[e.id] = { verdict: "unchecked", reason: `the origin check kept failing: ${r?.error ?? "no answer"}`, at: e.timestamp };
|
|
266
|
+
ready.push(e);
|
|
267
|
+
} else {
|
|
268
|
+
pending.push({ event: e, since, error: r?.error ?? "not checked yet" });
|
|
269
|
+
}
|
|
270
|
+
};
|
|
271
|
+
for (const p of state.pending) settle(p.event, p.since);
|
|
272
|
+
for (const e of fresh) {
|
|
273
|
+
if (state.seen[e.id] !== undefined) continue;
|
|
274
|
+
state.seen[e.id] = e.timestamp;
|
|
275
|
+
if (needsOrigin(e)) settle(e, new Date(now).toISOString());
|
|
276
|
+
else ready.push(e);
|
|
277
|
+
}
|
|
278
|
+
state.pending = pending;
|
|
279
|
+
const failed = await this.publish(state, s, ready, now);
|
|
280
|
+
for (const e of failed) delete state.seen[e.id];
|
|
281
|
+
// A failed post keeps the window's start, so the next check reads it again and posts what is missing.
|
|
282
|
+
state.cursor = new Date(failed.length === 0 ? started : cursor).toISOString();
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/** Posts urgent groups and notices; returns the events whose message could not be sent. */
|
|
287
|
+
private async publish(state: SecurityState, s: ReportSchedule, ready: readonly WazuhEvent[], now: number): Promise<WazuhEvent[]> {
|
|
288
|
+
const groups = new Map<string, WazuhEvent[]>();
|
|
289
|
+
const notices: WazuhEvent[] = [];
|
|
290
|
+
for (const e of ready) {
|
|
291
|
+
const cls = classify(e, state.origins[e.id]);
|
|
292
|
+
if (cls === "notice") notices.push(e);
|
|
293
|
+
if (cls === "urgent") groups.set(alertKey(e), [...(groups.get(alertKey(e)) ?? []), e]);
|
|
294
|
+
}
|
|
295
|
+
const failed: WazuhEvent[] = [];
|
|
296
|
+
const owners = this.owners();
|
|
297
|
+
let posted = 0;
|
|
298
|
+
let overflow = 0;
|
|
299
|
+
for (const [key, group] of groups) {
|
|
300
|
+
const repeat = repeatText(state.occurrences, key, now, group.length, s.timeZone);
|
|
301
|
+
if (isAcked(state, key, now)) {
|
|
302
|
+
state.occurrences.push(...group.map((e) => ({ key, at: e.timestamp })));
|
|
303
|
+
this.deps.log(`security: ${key} acknowledged; ${group.length} event(s) not posted`);
|
|
304
|
+
continue;
|
|
305
|
+
}
|
|
306
|
+
if (posted >= MAX_POSTS_PER_CHECK) {
|
|
307
|
+
overflow += group.length;
|
|
308
|
+
state.occurrences.push(...group.map((e) => ({ key, at: e.timestamp })));
|
|
309
|
+
continue;
|
|
310
|
+
}
|
|
311
|
+
const alert: PostedAlert = { messageId: "", channelId: s.channelId, key, event: group[0], count: group.length, origin: plainOrigin(state.origins[group[0].id]), repeat, postedAt: new Date(now).toISOString(), investigation: "none" };
|
|
312
|
+
try {
|
|
313
|
+
alert.messageId = (await this.deps.rest.createComponentsMessage(s.channelId, alertComponents(alert, owners, s.timeZone), owners)).id;
|
|
314
|
+
state.alerts.push(alert);
|
|
315
|
+
state.occurrences.push(...group.map((e) => ({ key, at: e.timestamp })));
|
|
316
|
+
posted++;
|
|
317
|
+
this.deps.log(`security: posted ${key} (level ${alert.event.level}, ${group.length} event(s))`);
|
|
318
|
+
} catch (err) {
|
|
319
|
+
this.deps.log(`security: posting ${key} failed: ${reason(err)}`);
|
|
320
|
+
failed.push(...group);
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
if (overflow > 0) await this.post(s.channelId, { content: `🚨 ${overflow} more urgent event(s) in this check were not posted one by one; the next Security Daily Report lists them.` });
|
|
324
|
+
for (const e of notices) {
|
|
325
|
+
if (!(await this.post(s.channelId, { content: noticeText(e, s.timeZone) }))) failed.push(e);
|
|
326
|
+
}
|
|
327
|
+
return failed;
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
private async post(channelId: string, message: CreateMessage): Promise<boolean> {
|
|
331
|
+
try {
|
|
332
|
+
await this.deps.rest.createMessage(channelId, message);
|
|
333
|
+
return true;
|
|
334
|
+
} catch (err) {
|
|
335
|
+
this.deps.log(`security: message failed: ${reason(err)}`);
|
|
336
|
+
return false;
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
private async pollFailed(s: ReportSchedule, why: string): Promise<void> {
|
|
341
|
+
this.deps.log(`security: wazuh-events failed: ${why}`);
|
|
342
|
+
await this.update(async (state) => {
|
|
343
|
+
state.poll.failures++;
|
|
344
|
+
if (state.poll.failures >= POLL_FAILS_TO_WARN && !state.poll.warned) {
|
|
345
|
+
const owners = this.owners();
|
|
346
|
+
state.poll.warned = await this.post(s.channelId, { content: `${checksFailingText("Wazuh alerts", (state.poll.failures * POLL_MS) / MINUTE, why)}\n${owners.map((id) => `<@${id}>`).join(" ")}`, mentionUsers: owners });
|
|
347
|
+
}
|
|
348
|
+
});
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
async checkHealth(s: ReportSchedule): Promise<void> {
|
|
352
|
+
let attention: Attention | null = null;
|
|
353
|
+
let why = "";
|
|
354
|
+
try {
|
|
355
|
+
attention = parseAttention(await this.entryJson("security", CALLER, ["attention"], 2 * MINUTE));
|
|
356
|
+
if (!attention) why = "attention returned an unexpected answer";
|
|
357
|
+
} catch (err) {
|
|
358
|
+
why = reason(err);
|
|
359
|
+
}
|
|
360
|
+
const owners = this.owners();
|
|
361
|
+
const tag = owners.map((id) => `<@${id}>`).join(" ");
|
|
362
|
+
await this.update(async (state) => {
|
|
363
|
+
if (!attention) {
|
|
364
|
+
this.deps.log(`security: attention failed: ${why}`);
|
|
365
|
+
state.health.failures++;
|
|
366
|
+
if (state.health.failures >= HEALTH_FAILS_TO_WARN && !state.health.warned) {
|
|
367
|
+
state.health.warned = await this.post(s.channelId, { content: `${checksFailingText("the observability VM's health status", (state.health.failures * HEALTH_MS) / MINUTE, why)}\n${tag}`, mentionUsers: owners });
|
|
368
|
+
}
|
|
369
|
+
return;
|
|
370
|
+
}
|
|
371
|
+
if (state.health.warned) await this.post(s.channelId, { content: checksRecoveredText("the health status") });
|
|
372
|
+
state.health.failures = 0;
|
|
373
|
+
state.health.warned = false;
|
|
374
|
+
const change = healthChange(state.health.codes, attention.issues);
|
|
375
|
+
if (change && !(await this.post(s.channelId, change.mention ? { content: `${change.text}\n${tag}`, mentionUsers: owners } : { content: change.text }))) return;
|
|
376
|
+
state.health.codes = attention.issues.map((i) => i.code).sort();
|
|
377
|
+
});
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
// ------------------------------------------------------------ reports
|
|
381
|
+
|
|
382
|
+
private async read<T>(what: string, service: string, args: string[], parse: (value: unknown) => T | null, timeoutMs = 3 * MINUTE): Promise<T | null> {
|
|
383
|
+
try {
|
|
384
|
+
const value = parse(JSON.parse(await this.deps.runEntry(service, REPORT_CALLER, args, timeoutMs)));
|
|
385
|
+
if (value === null) this.deps.log(`security report: ${what} returned an unexpected answer`);
|
|
386
|
+
return value;
|
|
387
|
+
} catch (err) {
|
|
388
|
+
this.deps.log(`security report: ${what} failed: ${reason(err)}`);
|
|
389
|
+
return null;
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
private async readText(what: string, service: string, args: string[]): Promise<string | null> {
|
|
394
|
+
try {
|
|
395
|
+
return await this.deps.runEntry(service, REPORT_CALLER, args, 2 * MINUTE);
|
|
396
|
+
} catch (err) {
|
|
397
|
+
this.deps.log(`security report: ${what} failed: ${reason(err)}`);
|
|
398
|
+
return null;
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
/** Origins the poller has not recorded (it was down, or the change is older than its state). */
|
|
403
|
+
private async fillOrigins(events: readonly WazuhEvent[] | null): Promise<void> {
|
|
404
|
+
if (!events) return;
|
|
405
|
+
const known = readSecurityState().origins;
|
|
406
|
+
const missing = events.filter((e) => needsOrigin(e) && known[e.id] === undefined);
|
|
407
|
+
if (missing.length === 0) return;
|
|
408
|
+
const checks = await this.origins(missing, REPORT_CALLER);
|
|
409
|
+
await this.update((state) => {
|
|
410
|
+
for (const e of missing) {
|
|
411
|
+
const r = checks.get(e.id);
|
|
412
|
+
state.origins[e.id] = r && !("error" in r) ? { ...r, at: e.timestamp } : { verdict: "unchecked", reason: `the origin check failed: ${r && "error" in r ? r.error : "no answer"}`, at: e.timestamp };
|
|
413
|
+
}
|
|
414
|
+
});
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
/** New findings and closed CVEs since the last report, recorded so each CVE is reported once per host. */
|
|
418
|
+
private async vulnerabilities(now: number): Promise<{ found: VulnEvent[]; closed: VulnEvent[] } | null> {
|
|
419
|
+
const from = Math.max(now - 7 * DAY, Date.parse(readSecurityState().vulns.processedUntil ?? "") || now - DAY);
|
|
420
|
+
const days = Math.min(8, Math.ceil((now - from) / DAY) + 1);
|
|
421
|
+
const events = await this.read("wazuh-vuln-events", "security", ["wazuh-vuln-events", String(days)], parseVulnEvents, 5 * MINUTE);
|
|
422
|
+
if (!events) return null;
|
|
423
|
+
return this.update((state) => {
|
|
424
|
+
const window = vulnWindow(events, from, now, state.vulns.reported);
|
|
425
|
+
applyVulnWindow(state.vulns.reported, window, now);
|
|
426
|
+
state.vulns.processedUntil = new Date(now).toISOString();
|
|
427
|
+
state.vulns.since ??= new Date(now).toISOString();
|
|
428
|
+
return window;
|
|
429
|
+
});
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
private async postReport(channelId: string, text: string, mention: boolean): Promise<string> {
|
|
433
|
+
const owners = this.owners();
|
|
434
|
+
let first = "";
|
|
435
|
+
for (const chunk of chunkMessage(text)) {
|
|
436
|
+
const posted = await this.deps.rest.createMessage(channelId, { content: chunk, ...(mention ? { mentionUsers: owners } : {}) });
|
|
437
|
+
first ||= posted.id;
|
|
438
|
+
}
|
|
439
|
+
return first;
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
async dailyReport(s: ReportSchedule, date: string): Promise<void> {
|
|
443
|
+
const now = this.deps.now();
|
|
444
|
+
if (reportSchedule(this.deps.config(), "security-weekly") && zonedWeekday(s.timeZone, new Date(now)) === MONDAY) {
|
|
445
|
+
writeReportState("security", { date, status: "skipped" });
|
|
446
|
+
this.deps.log(`security report ${date}: Monday, the weekly report takes its place`);
|
|
447
|
+
return;
|
|
448
|
+
}
|
|
449
|
+
this.deps.log(`security report ${date}: collecting`);
|
|
450
|
+
const crowdsec = await this.read("alerts", "security", ["alerts", "1"], parseAlertSummary);
|
|
451
|
+
const events = await this.read("wazuh-events", "security", ["wazuh-events", "1440", String(WATCH_LEVEL)], parseEvents);
|
|
452
|
+
const attention = await this.read("attention", "security", ["attention"], parseAttention, 2 * MINUTE);
|
|
453
|
+
const vulns = await this.vulnerabilities(now);
|
|
454
|
+
await this.fillOrigins(events);
|
|
455
|
+
const state = readSecurityState();
|
|
456
|
+
const wazuh = wazuhDigest(events, state.origins, (e) => acked(state, e));
|
|
457
|
+
if (this.deps.stopping()) return;
|
|
458
|
+
const data = { until: clockText(new Date(now).toISOString(), s.timeZone).replace(/^\d+ \w+ /, ""), crowdsec, wazuh, found: vulns?.found ?? null, closed: vulns?.closed ?? null, attention };
|
|
459
|
+
writeReportState("security", { date, status: "sending" });
|
|
460
|
+
const id = await this.postReport(s.channelId, dailyText(data, this.owners()), dailyNeedsOwner(data));
|
|
461
|
+
writeReportState("security", { date, status: "sent", messageId: id });
|
|
462
|
+
this.deps.log(`security report ${date}: posted`);
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
async weeklyReport(s: ReportSchedule, date: string): Promise<void> {
|
|
466
|
+
const now = this.deps.now();
|
|
467
|
+
this.deps.log(`security weekly report ${date}: collecting`);
|
|
468
|
+
const crowdsec = await this.read("alerts 7", "security", ["alerts", "7"], parseAlertSummary);
|
|
469
|
+
const lastDay = await this.read("alerts 1", "security", ["alerts", "1"], parseAlertSummary);
|
|
470
|
+
const events = await this.read("wazuh-events", "security", ["wazuh-events", "10080", String(WATCH_LEVEL)], parseEvents, 5 * MINUTE);
|
|
471
|
+
const low = await this.read("wazuh-alerts", "security", ["wazuh-alerts", "7", "3"], (v) => (v && typeof v === "object" ? v : null), 5 * MINUTE);
|
|
472
|
+
const weekVulns = await this.read("wazuh-vuln-events", "security", ["wazuh-vuln-events", "8"], parseVulnEvents, 5 * MINUTE);
|
|
473
|
+
const attention = await this.read("attention", "security", ["attention"], parseAttention, 2 * MINUTE);
|
|
474
|
+
const archives = await this.read("vzdump-archives", "backup", ["vzdump-archives"], backupGuests, 2 * MINUTE);
|
|
475
|
+
const resticText = await this.readText("restic-check-status", "backup", ["restic-check-status"]);
|
|
476
|
+
// Monday has no daily report, so the weekly one moves the vulnerability record forward.
|
|
477
|
+
const day = await this.vulnerabilities(now);
|
|
478
|
+
await this.fillOrigins(events);
|
|
479
|
+
const { state, previousBans } = await this.update((st) => {
|
|
480
|
+
const last = st.weeks[st.weeks.length - 1];
|
|
481
|
+
const prev = last && last.date !== date && Date.parse(date) - Date.parse(last.date) <= 8 * DAY ? last.bans : null;
|
|
482
|
+
if (crowdsec && last?.date !== date) st.weeks.push({ date, bans: crowdsec.count });
|
|
483
|
+
return { state: structuredClone(st), previousBans: prev };
|
|
484
|
+
});
|
|
485
|
+
const wazuh = wazuhDigest(events, state.origins, (e) => acked(state, e));
|
|
486
|
+
const week = weekVulns ? vulnWindow(weekVulns, now - 7 * DAY, now) : null;
|
|
487
|
+
const lastDayEvents = events?.filter((e) => Date.parse(e.timestamp) >= now - DAY) ?? null;
|
|
488
|
+
const data = {
|
|
489
|
+
range: weekRange(date),
|
|
490
|
+
timeZone: s.timeZone,
|
|
491
|
+
now,
|
|
492
|
+
crowdsec,
|
|
493
|
+
previousBans,
|
|
494
|
+
wazuh,
|
|
495
|
+
found: week?.found ?? null,
|
|
496
|
+
closed: week?.closed ?? null,
|
|
497
|
+
openWithFix: Object.values(state.vulns.reported).filter((v) => v.fix),
|
|
498
|
+
trackingSince: state.vulns.since,
|
|
499
|
+
patching: patchingFacts(low, events, now),
|
|
500
|
+
attention,
|
|
501
|
+
crl: events ? crlFromEvents(events, now) : ("unknown" as const),
|
|
502
|
+
backups: archives ? { guests: archives, restic: resticText === null ? null : resticStatus(resticText) } : null,
|
|
503
|
+
last24h: {
|
|
504
|
+
bans: lastDay?.count ?? null,
|
|
505
|
+
wazuh: lastDayEvents?.length ?? null,
|
|
506
|
+
urgent: lastDayEvents?.filter((e) => classify(e, state.origins[e.id]) === "urgent").length ?? 0,
|
|
507
|
+
found: day ? new Set(day.found.map((e) => `${e.agent}|${e.cve}`)).size : null,
|
|
508
|
+
closed: day ? new Set(day.closed.map((e) => `${e.agent}|${e.cve}`)).size : null,
|
|
509
|
+
health: attention?.issues.length ?? null,
|
|
510
|
+
},
|
|
511
|
+
};
|
|
512
|
+
if (this.deps.stopping()) return;
|
|
513
|
+
const report = weeklyText(data, this.owners());
|
|
514
|
+
writeReportState("security-weekly", { date, status: "sending" });
|
|
515
|
+
const id = await this.postReport(s.channelId, report.text, report.mention);
|
|
516
|
+
writeReportState("security-weekly", { date, status: "sent", messageId: id });
|
|
517
|
+
this.deps.log(`security weekly report ${date}: posted`);
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
// ------------------------------------------------------------ buttons
|
|
521
|
+
|
|
522
|
+
private async redraw(alert: PostedAlert): Promise<void> {
|
|
523
|
+
const tz = this.schedule()?.timeZone ?? "UTC";
|
|
524
|
+
await this.deps.rest.editComponents(alert.channelId, alert.messageId, alertComponents(alert, this.owners(), tz)).catch((err) => this.deps.log(`security: redraw failed: ${reason(err)}`));
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
/** Investigate and Acknowledge: owners only, on an alert the bridge posted within 14 days. */
|
|
528
|
+
async onButton(click: SecurityClick): Promise<void> {
|
|
529
|
+
const kind = parseSecurityButton(click.customId);
|
|
530
|
+
const reply = (text: string) => this.deps.rest.ephemeralReply(click.interactionId, click.token, text);
|
|
531
|
+
if (!kind) return reply("This button is no longer active.");
|
|
532
|
+
if (!isOwner(this.deps.config(), click.userId)) return reply("Only owners investigate or acknowledge security alerts.");
|
|
533
|
+
if (this.deps.stopping()) return reply("The bridge is restarting; try again in a minute.");
|
|
534
|
+
const found = readSecurityState().alerts.find((a) => a.messageId === click.messageId);
|
|
535
|
+
if (!found) return reply("This alert is no longer tracked (older than 14 days); ask the security specialist in the channel.");
|
|
536
|
+
if (kind === "ack") return this.onAcknowledge(click, found);
|
|
537
|
+
if (found.investigation === "running") return reply("An investigation of this alert is already running; its thread gets the answer.");
|
|
538
|
+
await this.deps.rest.deferUpdate(click.interactionId, click.token);
|
|
539
|
+
await this.runInvestigation(click, found);
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
private async onAcknowledge(click: SecurityClick, found: PostedAlert): Promise<void> {
|
|
543
|
+
if (found.ack) return this.deps.rest.ephemeralReply(click.interactionId, click.token, `Already acknowledged by <@${found.ack.by}>.`);
|
|
544
|
+
await this.deps.rest.deferUpdate(click.interactionId, click.token);
|
|
545
|
+
const changed = await this.update((state) => {
|
|
546
|
+
const alert = state.alerts.find((a) => a.messageId === click.messageId);
|
|
547
|
+
if (!alert || alert.ack) return [];
|
|
548
|
+
acknowledge(state, alert, click.userId, this.deps.now());
|
|
549
|
+
return state.alerts.filter((a) => a.key === alert.key);
|
|
550
|
+
});
|
|
551
|
+
for (const a of changed) await this.redraw(a);
|
|
552
|
+
this.deps.log(`security: ${found.key} acknowledged by ${click.userId}`);
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
private async setInvestigation(messageId: string, investigation: PostedAlert["investigation"], threadId?: string): Promise<PostedAlert | undefined> {
|
|
556
|
+
const alert = await this.update((state) => {
|
|
557
|
+
const a = state.alerts.find((x) => x.messageId === messageId);
|
|
558
|
+
if (a) {
|
|
559
|
+
a.investigation = investigation;
|
|
560
|
+
if (threadId) a.threadId = threadId;
|
|
561
|
+
}
|
|
562
|
+
return a ? structuredClone(a) : undefined;
|
|
563
|
+
});
|
|
564
|
+
if (alert) await this.redraw(alert);
|
|
565
|
+
return alert;
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
private async runInvestigation(click: SecurityClick, found: PostedAlert): Promise<void> {
|
|
569
|
+
const alert = await this.setInvestigation(found.messageId, "running");
|
|
570
|
+
if (!alert) return;
|
|
571
|
+
let threadId = alert.threadId;
|
|
572
|
+
if (!threadId) {
|
|
573
|
+
try {
|
|
574
|
+
threadId = (await this.deps.rest.startThread(alert.channelId, alert.messageId, `Rule ${alert.event.rule} on ${alert.event.agent}`)).id;
|
|
575
|
+
} catch (err) {
|
|
576
|
+
this.deps.log(`security: thread for ${alert.key} failed: ${reason(err)}`);
|
|
577
|
+
await this.post(alert.channelId, { content: `❌ Could not open a thread for this alert (${reason(err)}); ask the security specialist in the channel.`, replyTo: alert.messageId });
|
|
578
|
+
await this.setInvestigation(alert.messageId, "failed");
|
|
579
|
+
return;
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
let ok = false;
|
|
583
|
+
try {
|
|
584
|
+
const starter = await this.deps.rest.createMessage(threadId, { content: `🔎 Investigation requested by <@${click.userId}>. The security specialist reads only and may ask other specialists; a risky read still asks an owner here.` });
|
|
585
|
+
await this.setInvestigation(alert.messageId, "running", threadId);
|
|
586
|
+
ok = await this.deps.investigate({ threadId, starterId: starter.id, userId: click.userId, task: investigationTask(alert) });
|
|
587
|
+
} catch (err) {
|
|
588
|
+
this.deps.log(`security: investigation of ${alert.key} failed: ${reason(err)}`);
|
|
589
|
+
}
|
|
590
|
+
await this.setInvestigation(alert.messageId, ok ? "done" : "failed", threadId);
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
/** An investigation a restart cut off can be started again. */
|
|
594
|
+
async closeInterrupted(): Promise<void> {
|
|
595
|
+
const stuck = await this.update((state) => {
|
|
596
|
+
const list = state.alerts.filter((a) => a.investigation === "running");
|
|
597
|
+
for (const a of list) a.investigation = "failed";
|
|
598
|
+
return list;
|
|
599
|
+
});
|
|
600
|
+
for (const a of stuck) await this.redraw(a);
|
|
601
|
+
}
|
|
602
|
+
}
|