Skip to content

feat(pe): report the overlay appended after a PE image's last section - #13245

Merged
morluto merged 24 commits into
morluto:mainfrom
mvanhorn:fix/pe-overlay-inventory
Nov 26, 2026
Merged

morluto merged 24 commits into
morluto:mainfrom
mvanhorn:fix/pe-overlay-inventory

Conversation

@mvanhorn

@mvanhorn mvanhorn commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Installers, self-extracting archives and tampered executables often carry data appended after the last section of a PE file. REA can parse PE headers, resources and certificates, but it has no direct way to answer "is there anything after the image, where does it start, and what is it". Today an analyst works that out by hand from section tables or with a separate tool, with no Evidence tying the answer to the file.

This adds inspect_pe_overlay (CLI rea inspect-pe-overlay <path> --json), an offline report of the bytes after the end of a PE32 or PE32+ image. No issue exists for this; it is a static format-inspection addition within the supported contribution scope.

Problem and expected behavior

Use case: given setup.exe, find whether it carries an overlay, separate a trailing Authenticode certificate table from other appended bytes, and get each remaining range's offset, size, hash and leading format.

Observable outcome:

  • The image end is the larger of SizeOfHeaders and every contributing section's declared PointerToRawData + SizeOfRawData, so section padding is never reported as overlay. Zero-size and uninitialized-only sections are listed but do not move the image end.
  • present is false when the file ends at the image end.
  • security_directory reports the declared IMAGE_DIRECTORY_ENTRY_SECURITY as absent, table, or malformed with the defect. A valid table that lies entirely inside the overlay becomes its own certificate_table range; a malformed or straddling table stays in ordinary ranges with a limitation naming why.
  • Each other range carries offset, size, SHA-256, a Shannon entropy sample over at most the first 4096 bytes, the first 16 bytes as hex, and a leading_format from REA's existing recognizers (otherwise unrecognized).
  • At most 96 sections are listed (the documented loader limit); when more are declared, section_coverage.status is partial and every declared section still counts toward the image end.

Change and scope

  • src/domain/native/peOverlay.ts computes the image end, section rows, raw pointer rounding, overlay span and range split from parsed headers.
  • src/native/pe/PeOverlayParser.ts reads headers and the overlay through bounded reads; PeOverlayProvider.ts and src/application/binaryDiagnostics/PeOverlayService.ts wire it as a binary diagnostics provider. The service re-validates certificate ranges against the declared security directory before splitting.
  • src/domain/pe/peSecurityDirectory.ts holds the security directory checks shared with inspect_pe_certificates (zero size, offset inside headers, 8-byte alignment, range outside the file), so both tools classify the same directory the same way. PeCertificateParser.ts now uses it.
  • CLI, MCP tool contract, output schema group, capability declaration and server bindings register the new operation.
  • Docs: new docs/guides/native/pe-overlay.md, sidebar entry, CLI reference, docs index and skill references.

Not included: packer identification, Authenticode verification, decoding certificate entries, and detecting data hidden in section slack inside the mapped image (stated as a limitation in every report).

Contract and boundary impact

  • Semantic owner and earliest changed stage: peOverlay.ts owns overlay facts; parser, provider and service consume them.
  • Contribution scope fit and linked maintainer decision: static inspection of a local file; not applicable (no target execution, injection, mutation, or protection recovery).
  • CLI and MCP/tool-catalog contract: one new tool, inspect_pe_overlay (path, optional max_file_bytes, default 64 MiB, ceiling 512 MiB), and the CLI command inspect-pe-overlay. Existing tools unchanged.
  • Provider, bridge, target-format, or platform compatibility: new provider rea-pe-overlay; PE32 and PE32+ only.
  • Evidence, artifact, provenance, or reconstruction contract: new inspect_pe_overlay normalized result; existing Evidence shapes unchanged.
  • Target selection, authority, upstream behavior, and cleanup details (if applicable): not applicable.
  • Generated metadata (docs/public/product-catalog.json), package, or installation impact: none committed; authored skill references updated under .agents/skills/reverse-engineer-anything/.

Expected effects:

  • Read existing files or captured data.
  • Launch or attach to a process.
  • Inject instrumentation or run scripts/code.
  • Change process memory, registers, files, or configuration.
  • Contact network services.
  • Recover protected code or work around protections.
  • Documentation or usability only; no target interaction.
  • Not sure yet; investigation or maintainer guidance needed.

Evidence and regression coverage

  • Tests added or updated: src/native/pe/PeOverlayParser.test.ts (image end from headers and sections, padding, uninitialized and zero-size sections, raw pointer rounding, 96-section cap, certificate table inside, straddling and malformed, out-of-file section failures), src/application/binaryDiagnostics/PeOverlayService.test.ts (directory layout re-validation, derived Evidence), PeCertificateParser.test.ts (shared security directory checks), tests/acceptance/peOverlayCli.test.ts, tests/boundary/filesystem/peOverlay.test.ts, tests/boundary/mcp/peOverlayMcp.test.ts, plus the local tool contract test. Fixtures are generated in PeOverlay.fixture.ts.
  • Base reproduction or other evidence: executed. The tool does not exist on the base revision; the new boundary tests fail there.
  • User-visible CLI/MCP output (executed, rea inspect-pe-overlay setup.exe --json, excerpt of normalized_result, generated PE32 with a certificate table and an appended ZIP):
{
  "format": "pe32",
  "size_of_headers": 512,
  "image_end": 512,
  "file_size": 1366,
  "present": true,
  "section_coverage": { "status": "complete", "declared": 1, "reported": 1 },
  "security_directory": { "status": "table", "offset": 512, "size": 384 },
  "ranges": [
    { "role": "certificate_table", "offset": 512, "size": 384 },
    {
      "role": "other",
      "offset": 896,
      "size": 470,
      "sha256": "87cadb30e946d88313ea98c46ba82907aca205e89c7d2047cdff7c61c621e944",
      "entropy": { "window_bytes": 4096, "sampled_bytes": 470, "shannon_bits": 3.4771 },
      "prefix_hex": "504b0304140000000000000000000000",
      "leading_format": "zip"
    }
  ]
}

On a second fixture whose security directory points past the end of the file, the report shows "security_directory": { "status": "malformed", "offset": 1056, "size": 256, "defect": "outside-artifact" }, keeps the 64 trailing bytes as one other range, and adds the limitation "IMAGE_DIRECTORY_ENTRY_SECURITY is malformed: PE certificate table range leaves the artifact. No certificate table is separated; its bytes are reported as ordinary ranges."

  • Remaining proof gaps: tested against generated PE fixtures, not a corpus of real installers.

For evidence-bearing changes:

  • Observed, derived, and inferred claims remain distinguishable.
  • Artifact identity, source provenance, and failed attempts remain preserved.
  • Unsupported, incomplete, unavailable, or uncertain outcomes remain visible.

Demo

Rendered walkthrough of the CLI output above (simulated terminal, values taken from a real run on the generated fixtures).

demo

Validation performed

  • npx --no-install vitest run over the changed test files (overlay service, certificate parser, overlay parser, CLI acceptance, local tool contracts, filesystem boundary, MCP boundary): passed
  • npm run format:check: all matched files use the correct format

Compatibility, safety, and release

  • Breaking changes or migration steps: none. inspect_pe_certificates keeps its behavior; its directory checks moved into a shared helper.
  • Real Hopper/Ghidra, browser, or OS coverage: not applicable; offline parsing only, run on macOS.
  • Package or release metadata impact: one new tool and CLI command.
  • Security, privacy, process, or containment review: reads a local file only, within a 64 MiB default budget (512 MiB ceiling). Entropy and prefix sampling read a fixed window per range. Leading bytes are reported as hex, never interpreted.

AI was used for assistance.

Author readiness

Human owner: @mvanhorn

  • I reviewed the final diff and relevant existing code, understand the change and its edge cases, and take responsibility for the complete PR, including AI-assisted work.
  • I checked the evidence above, confirmed it supports the current change, and recorded meaningful validation gaps.
  • I declare this PR ready for initial maintainer review.

Before merge

  • This PR addresses a concrete problem or an agreed enhancement.
  • Material scope and authority questions are resolved, and any required maintainer scope decision covers the final diff.
  • The PR has one focused outcome and the title follows type(scope): outcome.
  • Related issue is linked, or the reason for not linking one is stated above.
  • Tests cover changed observable behavior and meaningful failure paths.
  • Owning docs, contracts, and generated metadata are updated where needed.
  • User-visible CLI/MCP changes include representative output.
  • I checked the final diff for secrets, unrelated cleanup, and unsupported claims.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 22, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ❌ Failed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ❌ Failed 2026-11-26T21:40:02.432423Z
We couldn’t complete this review. Comment @codex review to try again.
5cb7c0f New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@greptile-apps

greptile-apps Bot commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Confidence Score: 5/5 · The reviewed code appears safe to merge; no new blocking issue was found.RetriggerView in GreptileConfidence Score: 5/5 Tier: plus

[Medium impact] The reviewed code appears safe to merge; no new blocking issue was found.

Summary

Adds inspect_pe_overlay through the CLI and MCP. It reports the image end, separates eligible certificate tables, and measures the remaining appended ranges.

  • PE overlay reports show where an image ends and what bytes follow.
  • Certificate inspection and overlay reports share security-directory checks.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A["CLI or MCP request"] --> B["PeOverlayService"]
  B --> C["PeOverlayProvider"]
  C --> D["Bounded, stable file snapshot"]
  D --> E["Shared PE header reader"]
  E --> F["computePeOverlay"]
  F --> G["Shared security-directory checks"]
  G --> H["Certificate and other ranges"]
  H --> I["Hashes, entropy and format probes"]
  I --> J["Validated derived Evidence"]
Loading

Reviews (15) · Last reviewed commit: "Merge remote-tracking branch 'origin/mai..." · Reviewed by Greptile

mvanhorn and others added 5 commits November 21, 2026 16:33
Keep impossible security directory spans in measured overlay bytes and share bounded format recognition with inventory. Validate structured overlay facts in the domain while preserving provider diagnostics.
@morluto morluto added area: native Native analysis and decompilation, including Ghidra, IDA, and Hopper analysis behavior enhancement New feature or request labels Nov 22, 2026
@morluto

morluto commented Nov 24, 2026

Copy link
Copy Markdown
Owner

@mvanhorn thanks for the PR — this branch has merge conflicts with main. Could you merge or rebase onto the latest main and resolve them? Once it's clean and CI is green we can take another look.
cc @gensokyo-dev @dandgabr

mvanhorn and others added 4 commits November 24, 2026 14:35
Resolve conflicts against upstream main (42bbf33) and carry the PE
overlay feature onto upstream's new structure:

- composition: binaryDiagnostics.ts was split into per-service modules;
  add composition/binaryDiagnostics/peOverlay.ts and import it lazily from
  the CLI as the other services do.
- server: bind inspect_pe_overlay in offlineInspectionToolBindings.ts.
- PE headers: peImageHeaders.ts no longer reads headers; the overlay
  parser now admits headers through readPeImage(bytes, "headers").
- certificates: keep upstream's parser and failure kinds; apply the
  shared security-directory admission for absent, in-header, unaligned
  and out-of-file tables, and leave short or off-grid sizes to the entry
  walk, which reports the truncated header or padding.
- recognition: recognizeBoundedArtifactFormat takes the caller's XML
  plist probe result, as inventory now probes plists incrementally;
  overlay ranges run the same bounded probe. Leading formats include
  upstream's new recognizers.
- register the overlay structural allowance in the fixed-ceiling
  registry; restore computeBlockEntropy for single-sample entropy.
- tests follow upstream's malformed-target and certificate failure
  shapes; regenerate the nushell completion golden.
Resolve conflicts against upstream main (26cb5b5), which added the
ISO 9660 disc image reader:

- recognition: keep recognizeBoundedArtifactFormat beside upstream's new
  recognizesArtifactPrefix.
- inventory: classifyArtifactHeader keeps the shared bounded recognizer
  and now passes upstream's ISO 9660 descriptor through to it.
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.
Repo admins can enable using credits for code reviews in their settings.

@mvanhorn

Copy link
Copy Markdown
Contributor Author

Conflicts with main are resolved in a4b6e6f, which brings in the ISO 9660 reader and keeps it working next to the PE overlay recognition. CI is running on the new head now. npm run check:fast passed. npm run format:check passed. npm run metadata:check passed. npm run test:fast passed.

Comment thread src/native/pe/PeOverlayParser.ts Resolved
@mvanhorn

Copy link
Copy Markdown
Contributor Author

@morluto CI is green on a4b6e6f and GitHub reports the branch as mergeable with no conflicts against main. All 12 test shards, static checks, the Windows lanes and the native package baselines passed, so it's ready for another look.

parsePeOverlay dropped pe_header_anomalies from readPeImage, so a
SizeOfHeaders that ends before the section table reached the overlay
report without its warning. Carry header_anomalies through the output
contract and Evidence result, as the certificate, import and load
config reports already do.
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.
Repo admins can enable using credits for code reviews in their settings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.
Repo admins can enable using credits for code reviews in their settings.

Comment thread src/native/pe/PeOverlayProvider.ts Resolved
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

Comment thread src/native/pe/PeBinaryFailure.ts Resolved
# Conflicts:
#	src/native/pe/PeCertificateParser.ts
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@morluto
morluto enabled auto-merge (squash) November 26, 2026 18:13
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

Comment thread tests/acceptance/peOverlayCli.test.ts Resolved
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@morluto
morluto merged commit 47daa05 into morluto:main Nov 26, 2026
55 checks passed
morluto added a commit to mvanhorn/rea that referenced this pull request Nov 26, 2026
Preserve both origin/main (morluto#12469 VSIX, morluto#13245 PE overlay, morluto#14649 Qualcomm MBN) and YARA entries across conflicted skill docs and test-projects, and include qualcomm-mbn in PE_OVERLAY_LEADING_FORMATS to resolve the typecheck regression between morluto#13245 and morluto#14649.
morluto added a commit that referenced this pull request Nov 26, 2026
The PE overlay (#13245) and Qualcomm MBN recognition (#14649) merged
concurrently. Bounded artifact recognition can now return qualcomm-mbn,
which the overlay's leading-format enum did not list, so main failed to
typecheck and build.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: native Native analysis and decompilation, including Ghidra, IDA, and Hopper analysis behavior enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants