Skip to content

Commit 239db5a

Browse files
committed
feat(evtx): inventory Windows event log records offline
Windows event logs are routinely supplied during incident response, but rea could only read them by booting the source system or reaching a live Event Log service. This adds one offline reader over the existing artifact and Evidence machinery, so nothing outside the selected file is touched. This change covers the first independently mergeable step: file and chunk structure, record numbers, record-header timestamps and declared sizes, plus chunk header checksum evaluation. The channel, provider, event ID and EventData live in the embedded event XML and are deliberately absent rather than guessed, and source_coverage names each of those as not-decoded. That keeps the contract honest while the XML and template decode follow in later steps. A short or wrapped chunk does not discard the events it produced, and a chunk that fails its checksum is reported instead of corrected.
1 parent fdc4e39 commit 239db5a

16 files changed

Lines changed: 1120 additions & 0 deletions

‎docs/evtx-archives.md‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Offline Windows event log inventory
2+
3+
`inspect_evtx_archive` / `rea inspect-evtx-archive` reads one local Windows
4+
Event Log (`.evtx`) file and lists its records. It never contacts a live Event
5+
Log service, boots the source system, or executes event-provided content.
6+
7+
## What this version reads
8+
9+
Records are reported in file order. For each record the inventory keeps the
10+
chunk it was found in, its byte offset inside that 64 KiB chunk, the
11+
`EventRecordID` its header states, the `SystemTime` its header states, and the
12+
byte extent the record declares. Each chunk reports its stated number, the
13+
event numbers and timestamps of its first and last record, and whether its
14+
header checksum matched.
15+
16+
The channel, provider, event ID, level, `EventData` values and user identifiers
17+
live inside the embedded event XML, which this version does not decode: they
18+
are absent rather than inferred from the record number. Typed payload values,
19+
event templates, substitution arrays and per-record checksums are likewise not
20+
resolved.
21+
22+
## Sources and coverage
23+
24+
`source_coverage` states what the inventory examined. `event_xml`,
25+
`typed_payload_values` and `templates` are always `not-decoded`,
26+
`record_checksums` is always `not-verified` and `network` is always
27+
`not-contacted`. `status` is `complete-within-scope` only when every chunk was
28+
read without any diagnostic; a limit, a truncated record, an undecodable chunk
29+
or a chunk whose stated number disagrees with its position makes it `partial`
30+
and each cause appears in `diagnostics`.
31+
32+
A wrapped log leaves stale bytes after its last record. Those bytes are not
33+
reported as a truncation, and they do not discard the events the chunk did
34+
contain.
35+
36+
## Structure and bounds
37+
38+
The file must start with the `ElfFile` signature and the 128-byte block header
39+
must be at least as large as itself; anything else is reported as
40+
`not-an-evtx-file` or `invalid-header` rather than being scanned as records. A
41+
chunk that does not start with `ElfChnk` is skipped with `invalid-chunk`, and
42+
its neighbours are still read.
43+
44+
`max_file_bytes` (64 MiB) bounds the read and `max_records` (20000) bounds the
45+
number of records decoded; both report a diagnostic instead of silently
46+
truncating. Diagnostic growth is bounded separately, because one malformed
47+
chunk can contain many bad records.

‎docs/mcp-contracts.md‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,20 @@ Coverage describes examined declarations and does not establish runtime behavior
591591
or full specification/operation validity. See [offline API contracts](api-contracts.md)
592592
for source identities, parser allowances, supported scope and CLI publication.
593593

594+
## Offline Windows event log records
595+
596+
`inspect_evtx_archive` lists the records of one local Windows Event Log
597+
(`.evtx`) file without contacting a live Event Log service or executing
598+
event-provided content. File and chunk headers, chunk numbers, event record
599+
numbers, record-header timestamps and declared record sizes are reported in file
600+
order, and each chunk reports whether its header checksum matched. Embedded
601+
event XML is not decoded in this version, so channel, provider, event ID, level
602+
and `EventData` values are absent rather than inferred. Typed payload values and
603+
event templates are not resolved. Truncated records, undecodable chunks and
604+
disagreeing chunk numbers remain diagnostics instead of ending the inventory
605+
silently. See [offline Windows event logs](evtx-archives.md) for identities and
606+
resource bounds.
607+
594608
## Retained application Evidence inputs
595609

596610
`analyze_javascript_application`, `inspect_binary_layout`, `inspect_artifact`,
Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
import { isAbsolute } from "node:path";
2+
import {
3+
artifactFileCancellationCause,
4+
artifactFilePrimaryFacts,
5+
} from "../../artifacts/ArtifactFileOperation.js";
6+
import { ArtifactReaderFailure } from "../../artifacts/ArtifactReader.js";
7+
import type { ArtifactResourceScope } from "../../artifacts/ArtifactResourceScope.js";
8+
import { readStableArtifact } from "../../artifacts/readStableArtifact.js";
9+
import {
10+
projectStableArtifactFailure,
11+
stableArtifactInputRecovery,
12+
} from "../../artifacts/StableArtifactFailure.js";
13+
import type { AnalysisError } from "../../domain/analysisErrorBase.js";
14+
import {
15+
AnalysisAccessDeniedError,
16+
AnalysisArtifactChangedError,
17+
AnalysisCancelledError,
18+
AnalysisInputError,
19+
AnalysisResourceConstraintError,
20+
} from "../../domain/analysisErrorCore.js";
21+
import { ArtifactOperationError } from "../../domain/artifactOperationError.js";
22+
import { createEvidence, type Evidence } from "../../domain/evidence.js";
23+
import {
24+
EVTX_ARCHIVE_OPERATION as operation,
25+
EVTX_ARCHIVE_PROVIDER,
26+
type EvtxArchive,
27+
type EvtxArchiveArtifact,
28+
} from "../../domain/evtxArchive/evtxArchive.js";
29+
import {
30+
inspectEvtxArchiveInputSchema,
31+
type InspectEvtxArchiveInput,
32+
} from "../../domain/evtxArchive/evtxArchiveQuery.js";
33+
import { parseEvtxArchive } from "../../domain/evtxArchive/parseEvtxArchive.js";
34+
import { err, ok, type Result } from "../../domain/result.js";
35+
import { parseAnalysisInput } from "../AnalysisInput.js";
36+
import { completeProviderEvidence } from "../ProviderOperationExecution.js";
37+
38+
const inputFailure = (message: string, cause?: unknown) =>
39+
new AnalysisInputError(
40+
operation,
41+
{
42+
cause,
43+
...(cause instanceof ArtifactReaderFailure
44+
? stableArtifactInputRecovery(cause)
45+
: {}),
46+
},
47+
[{ path: ["path"], reason: "invalid_format", message }],
48+
);
49+
50+
/** Keep the reader's typed reasons instead of flattening every failure into "io". */
51+
const readFailure = (
52+
failure: unknown,
53+
input: InspectEvtxArchiveInput,
54+
): AnalysisError => {
55+
const { cause, origin } = artifactFilePrimaryFacts(failure);
56+
if (
57+
origin === "owned-or-classified" &&
58+
cause instanceof ArtifactReaderFailure
59+
) {
60+
if (cause.reason === "cancelled")
61+
return new AnalysisCancelledError(operation, {
62+
cause: artifactFileCancellationCause(cause),
63+
});
64+
if (cause.reason === "integrity")
65+
return new AnalysisArtifactChangedError(
66+
operation,
67+
input.path,
68+
cause.message,
69+
{ cause },
70+
);
71+
if (cause.reason === "limit")
72+
return new AnalysisResourceConstraintError(
73+
operation,
74+
cause.boundary ?? "file-size",
75+
cause.message,
76+
{ max_file_bytes: input.max_file_bytes },
77+
{
78+
cause,
79+
retryable: false,
80+
remediationAction:
81+
"Select a log within max_file_bytes; reply paging does not change this file admission.",
82+
},
83+
);
84+
if (cause.reason === "path" || cause.reason === "format")
85+
return inputFailure(cause.message, cause);
86+
}
87+
if (cause instanceof Error && "code" in cause) {
88+
if (cause.code === "EACCES" || cause.code === "EPERM")
89+
return new AnalysisAccessDeniedError(operation, input.path, cause.code, {
90+
cause,
91+
});
92+
}
93+
return new ArtifactOperationError(
94+
operation,
95+
"io",
96+
undefined,
97+
"Event log read failed.",
98+
{ cause: failure },
99+
);
100+
};
101+
102+
const readArchive = async (
103+
input: InspectEvtxArchiveInput,
104+
resources: ArtifactResourceScope,
105+
signal?: AbortSignal,
106+
): Promise<Result<{ bytes: Buffer; sha256: string }, AnalysisError>> => {
107+
try {
108+
const source = await resources.run(() =>
109+
readStableArtifact(
110+
input.path,
111+
input.max_file_bytes,
112+
signal,
113+
undefined,
114+
resources,
115+
),
116+
);
117+
return ok(source);
118+
} catch (failure: unknown) {
119+
return err(
120+
projectStableArtifactFailure(
121+
failure,
122+
readFailure(failure, input),
123+
input.path,
124+
EVTX_ARCHIVE_PROVIDER.id,
125+
operation,
126+
),
127+
);
128+
}
129+
};
130+
131+
export interface EvtxArchiveInspection {
132+
readonly evidence: Evidence;
133+
/** The verified file snapshot, retained when cancellation follows the read. */
134+
readonly observation: EvtxArchive["artifact"];
135+
}
136+
137+
/** Shared CLI/MCP import: one verified file read, no network, no replay, no execution. */
138+
export const inspectEvtxArchiveEvidence = async (
139+
rawInput: unknown,
140+
resources: ArtifactResourceScope,
141+
options: { readonly signal?: AbortSignal } = {},
142+
): Promise<Result<EvtxArchiveInspection, AnalysisError>> => {
143+
const admitted = parseAnalysisInput(
144+
operation,
145+
rawInput,
146+
inspectEvtxArchiveInputSchema,
147+
options.signal,
148+
);
149+
if (!admitted.ok) return admitted;
150+
const input = admitted.value;
151+
if (!isAbsolute(input.path))
152+
return err(
153+
inputFailure("Expected an absolute filesystem path on this host."),
154+
);
155+
const source = await readArchive(input, resources, options.signal);
156+
if (!source.ok) return source;
157+
const { bytes, sha256 } = source.value;
158+
const artifact: EvtxArchiveArtifact = {
159+
path: input.path,
160+
sha256,
161+
bytes: bytes.length,
162+
};
163+
const result = parseEvtxArchive(bytes, artifact, {
164+
maxRecords: input.max_records,
165+
});
166+
if (!result.ok) return result;
167+
const archive = result.value;
168+
const evidence = createEvidence(
169+
{ path: input.path, format: "file", sha256 },
170+
EVTX_ARCHIVE_PROVIDER,
171+
{
172+
operation,
173+
predicateType: "rea.evtx-archive-inventory",
174+
parameters: {
175+
path: input.path,
176+
format: input.format,
177+
max_file_bytes: input.max_file_bytes,
178+
max_records: input.max_records,
179+
},
180+
result: archive,
181+
confidence: "observed",
182+
limitations: archive.limitations,
183+
locations: [{ kind: "artifact-path", path: input.path }],
184+
},
185+
);
186+
const completed = completeProviderEvidence(evidence, options.signal);
187+
return completed.ok
188+
? ok({ evidence: completed.value, observation: artifact })
189+
: completed;
190+
};

‎src/cli/applicationCommands.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { registerPlaywrightTraceCommand } from "./playwrightTraceCommand.js";
22
import { registerApiContractCommand } from "./apiContractCommand.js";
3+
import { registerEvtxArchiveCommand } from "./evtxArchiveCommand.js";
34
import { Cli, z } from "incur";
45

56
import { CLI_COMMANDS } from "../cliCommandNames.js";
@@ -77,6 +78,7 @@ export const registerApplicationCommands = (
7778
registerSourceToBundleCommand(cli, logger);
7879
registerStaticObservedEndpointsCommand(cli, logger);
7980
registerApiContractCommand(cli, logger);
81+
registerEvtxArchiveCommand(cli, logger);
8082
registerPlaywrightTraceCommand(cli, logger);
8183
registerEvidenceJsonCommand({
8284
cli,

‎src/cli/evtxArchiveCommand.ts‎

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import type { Logger } from "pino";
2+
import type { CliInstance } from "./types.js";
3+
import { CLI_COMMANDS } from "../cliCommandNames.js";
4+
import { logCliCommand } from "../cliLogging.js";
5+
import { ArtifactResourceScope } from "../artifacts/ArtifactResourceScope.js";
6+
import { projectArtifactCloseFailure } from "../artifacts/StableArtifactFailure.js";
7+
import { absoluteCallerPath } from "../filesystem/callerPath.js";
8+
import { localPathStringSchema } from "../domain/localPath.js";
9+
import { projectAnalysisError } from "../domain/analysisErrorProjection.js";
10+
import {
11+
EVTX_ARCHIVE_OPERATION as operation,
12+
EVTX_ARCHIVE_PROVIDER,
13+
} from "../domain/evtxArchive/evtxArchive.js";
14+
import {
15+
evtxArchiveFileBytesSchema,
16+
evtxArchiveRecordsSchema,
17+
} from "../domain/evtxArchive/evtxArchiveQuery.js";
18+
import { z } from "incur";
19+
import { withCommandCancellation } from "./commandCancellation.js";
20+
import {
21+
analysisEvidenceDeliveryOptions,
22+
parseCliAnalysisEvidenceDelivery,
23+
deliverCliAnalysisEvidence,
24+
} from "./analysisEvidenceDelivery.js";
25+
26+
const deliverySchema = z.object({
27+
detail: analysisEvidenceDeliveryOptions.detail,
28+
});
29+
30+
/** One-shot publication keeps the complete inventory for later offline pages. */
31+
export const registerEvtxArchiveCommand = (
32+
cli: CliInstance,
33+
logger: Logger,
34+
): void => {
35+
cli.command(CLI_COMMANDS.inspectEvtxArchive, {
36+
description:
37+
"Inspect the records of a local Windows Event Log (.evtx) file offline",
38+
args: z.object({
39+
path: localPathStringSchema.describe("Local .evtx path"),
40+
}),
41+
options: z.object({
42+
...analysisEvidenceDeliveryOptions,
43+
maxFileBytes: evtxArchiveFileBytesSchema,
44+
maxRecords: evtxArchiveRecordsSchema,
45+
}),
46+
run: ({ args, options }) =>
47+
withCommandCancellation((signal) =>
48+
logCliCommand(logger, CLI_COMMANDS.inspectEvtxArchive, async () => {
49+
const selected = parseCliAnalysisEvidenceDelivery(
50+
operation,
51+
options,
52+
deliverySchema,
53+
);
54+
if (!selected.ok) return projectAnalysisError(selected.error);
55+
const resources = new ArtifactResourceScope();
56+
const { inspectEvtxArchiveEvidence } =
57+
await import("../application/evtxArchive/EvtxArchiveService.js");
58+
return deliverCliAnalysisEvidence({
59+
callerInputPath: args.path,
60+
run: async () => {
61+
const inspected = await inspectEvtxArchiveEvidence(
62+
{
63+
path: absoluteCallerPath(args.path),
64+
format: "evtx",
65+
max_file_bytes: options.maxFileBytes,
66+
max_records: options.maxRecords,
67+
},
68+
resources,
69+
{ signal },
70+
);
71+
return inspected.ok
72+
? { ok: true, value: inspected.value.evidence }
73+
: inspected;
74+
},
75+
service: {
76+
close: () =>
77+
resources.close().catch((cause: unknown) => {
78+
throw projectArtifactCloseFailure(
79+
cause,
80+
EVTX_ARCHIVE_PROVIDER.id,
81+
operation,
82+
);
83+
}),
84+
},
85+
providerId: EVTX_ARCHIVE_PROVIDER.id,
86+
operation,
87+
signal,
88+
detail: selected.value.detail,
89+
evidenceOutput: selected.value.evidenceOutput,
90+
});
91+
}),
92+
),
93+
});
94+
};

‎src/cliCommandNames.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ export const CLI_COMMANDS = Object.freeze({
6969
inspectIl2cppMetadata: "inspect-il2cpp-metadata",
7070
inspectModelArtifact: "inspect-model-artifact",
7171
inspectApiContract: "inspect-api-contract",
72+
inspectEvtxArchive: "inspect-evtx-archive",
7273
inspectPlaywrightTrace: "inspect-playwright-trace",
7374
inspectGpuCodeObjects: "inspect-gpu-code-objects",
7475
inspectRustBinary: "inspect-rust-binary",

‎src/contracts/applicationToolContracts.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { PLAYWRIGHT_TRACE_TOOL_CONTRACTS } from "./playwrightTraceToolContracts.js";
22
import { API_CONTRACT_TOOL_CONTRACTS } from "./apiContractToolContracts.js";
3+
import { EVTX_ARCHIVE_TOOL_CONTRACTS } from "./evtxArchiveToolContracts.js";
34
import { applicationInventoryProjectionInputSchema } from "../domain/mobileApplicationGraphProjection.js";
45
import { z } from "zod";
56
import {
@@ -95,6 +96,7 @@ const retained = (evidence_id: string) => ({
9596
/** Provider-neutral graph workflow contracts shared by MCP and CLI adapters. */
9697
export const APPLICATION_TOOL_CONTRACTS = [
9798
...API_CONTRACT_TOOL_CONTRACTS,
99+
...EVTX_ARCHIVE_TOOL_CONTRACTS,
98100
...PLAYWRIGHT_TRACE_TOOL_CONTRACTS,
99101
{
100102
name: "search_guidance",

0 commit comments

Comments
 (0)