Skip to content

Commit 7aa1b51

Browse files
mvanhornmorlutoclaudekaoru0822-kitaujicursoragent
authored
feat(diagnostics): report a byte entropy profile for one local file (#2331)
* feat(diagnostics): report a byte entropy profile for one local file The diff adds the MCP tool inspect_byte_entropy and the CLI command inspect-byte-entropy, both implemented by ByteEntropyService. One absolute regular file is read offline into observed Evidence: file-wide SHA-256 and Shannon entropy, then fixed blocks and adjacent runs for an optional window, using a power-of-two block size from 256 to 65536 (default 4096), with high at 7.2 or above and low at 1.0 or below. A window past EOF, a missing path, or a non-file is an input error; a size or identity change during the read is an artifact-changed error; empty input reports entropy 0; cancellation is honored before and during the read. Summary detail retains the complete series and returns file entropy, counts, and the ten longest runs for inspect_analysis_view, while the registered effect remains read-only Evidence and the diagnostics docs record the operation and its errors. * fix(diagnostics): flag zero-size files as not shown empty Generated files such as Linux /proc entries report size 0 while holding content, so inspect_byte_entropy read nothing and returned an empty-looking report. A file that reports size 0 now carries a limitation that the result does not show the file is empty. The result stays a success because the tool reads the declared size, which the file-stability checks depend on. * fix(mcp): reject unsupported diagnostic summaries before inspection * fix(ci): repair integrated checks and Hopper test baseline * test(entropy): use shared MCP schema verification after merge * fix(catalog): list inspect_byte_entropy under its own provider Adding inspect_byte_entropy to BINARY_DIAGNOSTICS_TOOL_CONTRACTS put it under pwntools-elf in the generated product catalog, because the pwntools group took every binary diagnostics tool except inspect_pe_resources. The service reports provider byte-entropy. Add a BYTE_ENTROPY_PROVIDER identity beside PE_RESOURCES_PROVIDER, use it in ByteEntropyService, give it its own catalog group, and name the pwntools tools (inspect_binary_layout, inspect_recorded_crash) explicitly so a new diagnostics tool is not attributed to pwntools by default. The catalog test pins both groups. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01KFTE4BHbGYzgf8Uhzcppdh * fix(cli): resolve the byte-entropy path with absoluteCallerPath main replaced the node:path resolve import in binaryDiagnosticsCommands.ts with absoluteCallerPath (#4067), so the byte-entropy command's resolve(args.path) no longer type-checks once the PR is merged with main. Use absoluteCallerPath like the other commands, and add src/filesystem/callerPath.ts matching main so the branch builds on its own. * fix(diagnostics): retain entropy read cleanup ownership * docs(diagnostics): explain entropy output capacity * fix(diagnostics): preserve entropy cancellation outcomes * fix(diagnostics): close the entropy input and warn on large reports Public inspect_byte_entropy input now uses publicInputStrictObject before detail is added, so unknown keys stay rejected on the advertised schema. The tool description warns that complete output can become very large and points callers to a shorter length or a larger block_size. Co-authored-by: morluto <morluto@users.noreply.github.com> * fix(diagnostics): keep byte-entropy registration mergeable with main Register inspect_byte_entropy beside layout inspection and reject unretained summaries before the file read, without editing workflows. Co-authored-by: morluto <morluto@users.noreply.github.com> * fix(diagnostics): register byte entropy with the shared tool options Use toolRegistrationOptions so the handler typechecks on this branch and on main, and still refuse unretained summaries before the file read. Co-authored-by: morluto <morluto@users.noreply.github.com> --------- Co-authored-by: Matt Van Horn <455140+mvanhorn@users.noreply.github.com> Co-authored-by: morluto <76467478+morluto@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Kaoru0822 <208281322+kaoru0822-kitauji@users.noreply.github.com> Co-authored-by: Cursor Agent <cursoragent@cursor.com> Co-authored-by: morluto <morluto@users.noreply.github.com>
1 parent 538017d commit 7aa1b51

29 files changed

Lines changed: 1510 additions & 3 deletions

‎.agents/skills/reverse-engineer-anything/references/native-and-artifacts.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,8 @@ For headerless DOS COM, explicitly open with `format: "dos-com"` (CLI
368368
analysis: the entry is `0x10100`, file offset zero, with imposed real-mode segment
369369
context. PSP/stack/device state remains unmodeled. See [DOS guide](https://github.com/morluto/rea/blob/main/docs/ghidra-dos.md).
370370

371+
Use `inspect_byte_entropy` to triage high- and low-entropy ranges before extraction.
372+
371373
For other headerless images (embedded firmware without ELF/Mach-O/PE/MZ
372374
headers), open with `format: "raw-binary"`, `architecture` (`x86`, `x86_64`,
373375
`arm`, `arm64`, `6502`, or `m68k`), and `entry_point` (`0x` hex). File offset 0 is mapped at

‎docs/binary-diagnostics.md‎

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,3 +215,87 @@ Large Evidence envelopes may require a caller-configured MCP transport receive
215215
buffer because envelope copies add overhead. Requests honor cancellation during
216216
snapshot reading, directory traversal and payload hashing. The operation reads
217217
regular files only and rejects symbolic links.
218+
219+
## Byte entropy triage
220+
221+
`inspect_byte_entropy` / `inspect-byte-entropy` profiles any local regular file
222+
without an analysis engine, execution, extraction or network access.
223+
224+
```sh
225+
rea inspect-byte-entropy ./firmware.bin --block-size 4096 --json
226+
rea inspect-byte-entropy ./firmware.bin --offset 4096 --length 65536 --json
227+
```
228+
229+
```json
230+
{
231+
"name": "inspect_byte_entropy",
232+
"arguments": { "path": "/artifacts/firmware.bin", "detail": "summary" }
233+
}
234+
```
235+
236+
Complete Evidence includes `artifact` (original path, byte size and SHA-256),
237+
`block_size`, `window`, `file_entropy`, `thresholds`, `blocks`, `runs` and
238+
`limitations`. For a 4096-byte zero-filled file, the entropy fields are:
239+
240+
```json
241+
{
242+
"block_size": 4096,
243+
"window": { "offset": 0, "length": 4096 },
244+
"file_entropy": 0,
245+
"thresholds": { "high": 7.2, "low": 1 },
246+
"blocks": [{ "offset": 0, "length": 4096, "entropy": 0 }],
247+
"runs": [{ "offset": 0, "length": 4096, "classification": "low" }]
248+
}
249+
```
250+
251+
Offsets and lengths are decimal byte counts. Blocks begin at the requested
252+
`offset` (default zero); omitted `length` extends to EOF. Explicit windows past
253+
EOF fail rather than silently clipping. The final block may be shorter.
254+
`block_size` must be a power of two from 256 through 65536, default 4096. A
255+
window may produce at most 32,768 block rows; larger requests fail before the
256+
file scan with a capacity error. Choose a shorter window or larger block size.
257+
SHA-256 and `file_entropy` always cover the entire file, including when a window
258+
is selected. Empty files and empty windows are accepted; empty entropy is zero
259+
by convention. The tool reads the size the file reports. A file that reports size
260+
0, such as a Linux `/proc` entry, returns no blocks with a limitation that the
261+
result does not show the file is empty. File identity, size and timestamps are checked across the read;
262+
detected changes produce an error rather than Evidence.
263+
264+
Shannon entropy is reported in bits per byte from 0 to 8, rounded to four decimal
265+
places. Adjacent blocks with reported entropy at least 7.2 form `high` runs;
266+
blocks at most 1.0 form `low` runs. Other blocks interrupt runs. Entropy does not
267+
prove encryption, and this measure cannot distinguish encrypted from compressed
268+
data. Small blocks can underestimate entropy; the result is a triage clue.
269+
270+
MCP `detail: "summary"` retains complete Evidence in the session and returns
271+
file entropy, block/run counts, thresholds, the selected window, and the ten
272+
longest runs (ties ordered by file offset). Use its `parent_evidence_id` to
273+
retrieve complete Evidence, or request its summary with `inspect_analysis_view`.
274+
The CLI returns complete Evidence through the same service. Summary detail
275+
requires a session capable of retaining Evidence.
276+
277+
Reads use one block-sized buffer and a 256-entry histogram. Complete output
278+
can become very large because it includes one row per selected block, and
279+
summary delivery still retains that full list. Use a shorter length or a
280+
larger block size to reduce it. Full Evidence and its serialization require
281+
memory proportional to the number of selected blocks. The 32,768-row ceiling bounds the actual retained block objects and
282+
their serialized representation: a cap-sized run below produced 1,514,877
283+
serialized bytes and increased V8 heap use by 21.2 MiB on the measured host.
284+
The cap therefore bounds this output independently of file size; it is not a
285+
portable process-memory guarantee. Narrow the window or increase block size to
286+
reduce retained rows further.
287+
288+
Local service measurement (macOS arm64, Node 24.18.0, 100,000,000-byte file,
289+
4096-byte blocks, file already in the OS cache): first process run took 1.113 s
290+
wall / 1.148 s CPU with 145.3 MiB peak RSS; a second run in the same process took
291+
1.015 s / 1.035 s CPU, with cumulative peak RSS 155.8 MiB. Each read 100,000,000
292+
bytes and produced 24,415 blocks. Measurements include Evidence construction,
293+
summary projection and JSON serialization, with no child processes. Complete
294+
Evidence serialized to 1,122,303 bytes and its summary to 2,705 bytes. A
295+
cap-sized 134,217,728-byte sparse file with 4096-byte blocks produced 32,768
296+
rows and 1,514,877 serialized bytes; V8 heap use increased by 21.2 MiB and RSS
297+
by 18.1 MiB in that process. This separate measurement used the same macOS
298+
arm64 host and Node version. Cold disk I/O, token counts and an agent
299+
investigation were not measured; there was no previous equivalent tool to
300+
benchmark. These figures do not establish a portable peak-memory bound,
301+
constant memory for retained Evidence or faster agent task completion.

‎docs/user-visible-errors.csv‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,5 @@ Then rerun this installer.",implemented,verified by shell branch inspection
116116
90,install.sh:version mismatch,Installed rea reports a different version,installed version ACTUAL does not match EXPECTED.,Low risk; gives exact integrity mismatch,Keep current copy,unchanged,verified by exact installer test
117117
90.5,@modelcontextprotocol/server:tool input validation,Caller sends arguments that violate a declared tool schema,"Canonical invalid_request with the SDK diagnostic plus recovery.kind=inspect_input_contract and a bounded instruction to read the advertised schema or call tools/list",Keeps field/type correction while telling the agent how to recover; diagnostics over 4 KiB stay on the existing retained-error path,Normalize ordinary validation failures and retain oversized diagnostics,verified by real in-memory and stdio MCP client/server calls with wrong argument types
118118
90.6,incur:unknown CLI command,Caller enters an unknown REA command,"'COMMAND' is not a command for 'rea'. Suggested command: rea --help",Low risk; identifies input and exact discovery action,Keep framework-generated copy,unchanged,verified by real packaged dispatcher invocation
119+
91,src/application/binaryDiagnostics/ByteEntropyService.ts,Invalid file or window,"Expected a regular file.; Selected file could not be found.; Selected byte window extends beyond the end of the file.; File size exceeds exact numeric offset representation.",Invalid selection cannot produce complete entropy Evidence,Choose a readable regular file and a window within its byte size,implemented,entropy service and MCP boundary tests
120+
92,src/domain/analysisView/byteEntropyView.ts,Non-summary entropy view,Byte entropy Evidence supports the summary view; retrieve parent Evidence for the complete block series.,Unsupported view selection,Use the summary view or retrieve complete parent Evidence,implemented,typed input rejection

‎scripts/lib/catalog-builders.mjs‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,9 @@ export const providerCatalog = (sources) => {
238238
identity: sources.pwntoolsProvider.PWNTOOLS_PROVIDER_IDENTITY,
239239
contracts:
240240
sources.binaryDiagnosticsContracts.BINARY_DIAGNOSTICS_TOOL_CONTRACTS.filter(
241-
({ name }) => name !== "inspect_pe_resources",
241+
({ name }) =>
242+
name === "inspect_binary_layout" ||
243+
name === "inspect_recorded_crash",
242244
),
243245
},
244246
{
@@ -248,6 +250,13 @@ export const providerCatalog = (sources) => {
248250
({ name }) => name === "inspect_pe_resources",
249251
),
250252
},
253+
{
254+
identity: sources.artifactProviders.BYTE_ENTROPY_PROVIDER,
255+
contracts:
256+
sources.binaryDiagnosticsContracts.BINARY_DIAGNOSTICS_TOOL_CONTRACTS.filter(
257+
({ name }) => name === "inspect_byte_entropy",
258+
),
259+
},
251260
{
252261
identity: sources.captureProvider.TSHARK_PROVIDER_IDENTITY,
253262
contracts: sources.captureContracts.CAPTURE_TOOL_CONTRACTS,

‎src/application/CapabilityInventory.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,7 @@ export const compactToolAvailability = (
350350
const availabilityFor = (context: AvailabilityContext): Availability => {
351351
// These built-in workflows accept a caller-selected endpoint; they do not load the optional passive provider.
352352
if (
353+
context.name === "inspect_byte_entropy" ||
353354
context.name === "observe_web_execution" ||
354355
context.name === "inspect_web_event_listeners"
355356
)

‎src/application/InvestigationProviders.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ export const PE_RESOURCES_PROVIDER = Object.freeze({
4848
version: "1",
4949
} as const);
5050

51+
/** Provider identity for execution-free Shannon byte-entropy profiling. */
52+
export const BYTE_ENTROPY_PROVIDER = Object.freeze({
53+
id: "byte-entropy",
54+
name: "REA byte entropy",
55+
version: "1",
56+
} as const);
57+
5158
/** Provider identity for deterministic managed-code comparison workflows. */
5259
export const MANAGED_WORKFLOW_PROVIDER = {
5360
id: "rea-dotnet-workflows",

0 commit comments

Comments
 (0)