tooling: add a read-only Authenticode SHA-256 digest tool #1904

Merged
Makto94 merged 2 commits from upstream-pr/1911 into main 2026-11-24 21:25:12 +00:00
Makto94 commented 2026-11-24 20:54:20 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Makto94 on 2026-11-24T20:54:20Z.


A read-only tool that recomputes the Authenticode SHA-256 digest of a PE file, with self-built PE controls. This follows the maintainer's note in #52 that "a source-only version with a self-built PE control would be useful". It does not explain the mismatch reported there, and it does not use or name any packaged file.

Refs #52

Change

  • tools/pe_authentihash.py: PE32 and PE32+, SHA-256 only. No new dependency (struct, hashlib, mmap). It does not read or verify the signature, and it never writes, repairs or signs a file. Layouts it does not support are refused with an error and no digest:

    • fewer than 5 data directories;
    • no section with raw data;
    • overlapping sections;
    • a certificate table that is not 8-byte aligned and sized, not complete, or not the last block of the file.

    The docstring lists these refusals as limits of this tool.

  • tests/pe_authentihash_check.py (pure Python, added to the python-linux job): builds PE32 and PE32+ files in a temporary directory. It compares the tool with digests pinned from the Windows API for the same bytes (Windows 11 build 26300). Cases: section ends not aligned to 8, gaps and filler between headers and sections, overlays of 1-13 bytes, certificate tables, and the refusals above. It also checks that a changed section byte changes the digest, and that CheckSum, the certificate data-directory entry and the certificate table do not. No PE file is committed.

  • tests/pe_authentihash_oracle_check.py (Windows only; exits 77 elsewhere): compares the tool with CryptCATAdminAcquireContext2(NULL, "SHA256") + CryptCATAdminCalcHashFromFileHandle2 on the synthetic files and on the built xmem.dll. It does not touch a certificate store. The registered test uses only layouts that occur in real files; --exotic also checks the synthetic edge cases.

  • Registration: AGENTS.md, the python-linux job, two CTest entries (pe_authentihash, pe_authentihash_oracle with SKIP_RETURN_CODE 77) and one line next to the Authenticode note in README.md.

Two behaviours follow what the Windows API returned, not a reading of the specification: the handling of filler between sections and the zero padding at the end.

On this PR, the python-linux job runs the pure check. The pr preset does not include the two CTest entries.

Verification

Head cdd188ec; its tree is identical to 4eb6a33b, where the runs below were made (only the commit message changed). Windows 11 Pro 26300.9974, Python 3.12.0.

On 4eb6a33b:

  • python -B tests/pe_authentihash_check.py: 12 tests OK, on Windows and in WSL Ubuntu with Python 3.12.3. In WSL the oracle check exits 77.
  • ctest --test-dir <build> --no-tests=error -R pe_authentihash: 2/2 passed, no skip.
  • python -B tests/pe_authentihash_oracle_check.py --exotic: 0 mismatches.
  • The tool against the Windows API on 74,722 local files (System32, SysWOW64, WinSxS, Program Files, Program Files (x86), build outputs):
    • 60,429 PE files: same digest, 0 different, 0 refused. Refusals occur only for synthetic layouts.
    • 14,291 non-PE files: refused.
    • 1 empty file.
    • On 1 file the Windows API returned an error.
  • docs_links_check.py, bidi_controls_check.py and form_coverage_check.py: passed.

On the same tool code before a final edit (an unused algorithm parameter removed, CMake quoting, the README line):

  • 4,250 header mutations and truncations of 25 system DLLs: 0 unhandled exceptions, 0 digest mismatches where both returned a digest;
  • each of these mutants makes the check fail:
    • padding computed from the tail;
    • no padding;
    • tail not hashed;
    • CheckSum included;
    • certificate entry or table included.

The corpus, fuzz and mutant runs were independent passes with separate scripts on the same machine. Two earlier versions failed them and were fixed before this head:

  • the padding length was wrong for section ends not aligned to 8 (9 real executables);
  • the tool refused a real file whose SizeOfHeaders is larger than the first section pointer.

Not covered: the CI runner's Windows build (26100) was not compared with the oracle.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6591204505 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1911). Originally posted by [Makto94](https://github.com/Makto94) on 2026-11-24T20:54:20Z. --- A read-only tool that recomputes the Authenticode SHA-256 digest of a PE file, with self-built PE controls. This follows the [maintainer's note in #52](https://github.com/R0Y00/Xbox2Windows/issues/52#issuecomment-7116723741) that "a source-only version with a self-built PE control would be useful". It does not explain the mismatch reported there, and it does not use or name any packaged file. Refs #52 ## Change - `tools/pe_authentihash.py`: PE32 and PE32+, SHA-256 only. No new dependency (`struct`, `hashlib`, `mmap`). It does not read or verify the signature, and it never writes, repairs or signs a file. Layouts it does not support are refused with an error and no digest: - fewer than 5 data directories; - no section with raw data; - overlapping sections; - a certificate table that is not 8-byte aligned and sized, not complete, or not the last block of the file. The docstring lists these refusals as limits of this tool. - `tests/pe_authentihash_check.py` (pure Python, added to the `python-linux` job): builds PE32 and PE32+ files in a temporary directory. It compares the tool with digests pinned from the Windows API for the same bytes (Windows 11 build 26300). Cases: section ends not aligned to 8, gaps and filler between headers and sections, overlays of 1-13 bytes, certificate tables, and the refusals above. It also checks that a changed section byte changes the digest, and that CheckSum, the certificate data-directory entry and the certificate table do not. No PE file is committed. - `tests/pe_authentihash_oracle_check.py` (Windows only; exits 77 elsewhere): compares the tool with `CryptCATAdminAcquireContext2(NULL, "SHA256")` + `CryptCATAdminCalcHashFromFileHandle2` on the synthetic files and on the built `xmem.dll`. It does not touch a certificate store. The registered test uses only layouts that occur in real files; `--exotic` also checks the synthetic edge cases. - Registration: `AGENTS.md`, the `python-linux` job, two CTest entries (`pe_authentihash`, `pe_authentihash_oracle` with `SKIP_RETURN_CODE 77`) and one line next to the Authenticode note in `README.md`. Two behaviours follow what the Windows API returned, not a reading of the specification: the handling of filler between sections and the zero padding at the end. On this PR, the `python-linux` job runs the pure check. The `pr` preset does not include the two CTest entries. ## Verification Head `cdd188ec`; its tree is identical to `4eb6a33b`, where the runs below were made (only the commit message changed). Windows 11 Pro 26300.9974, Python 3.12.0. On `4eb6a33b`: - `python -B tests/pe_authentihash_check.py`: 12 tests OK, on Windows and in WSL Ubuntu with Python 3.12.3. In WSL the oracle check exits 77. - `ctest --test-dir <build> --no-tests=error -R pe_authentihash`: 2/2 passed, no skip. - `python -B tests/pe_authentihash_oracle_check.py --exotic`: 0 mismatches. - The tool against the Windows API on 74,722 local files (System32, SysWOW64, WinSxS, Program Files, Program Files (x86), build outputs): - 60,429 PE files: same digest, 0 different, 0 refused. Refusals occur only for synthetic layouts. - 14,291 non-PE files: refused. - 1 empty file. - On 1 file the Windows API returned an error. - `docs_links_check.py`, `bidi_controls_check.py` and `form_coverage_check.py`: passed. On the same tool code before a final edit (an unused `algorithm` parameter removed, CMake quoting, the README line): - 4,250 header mutations and truncations of 25 system DLLs: 0 unhandled exceptions, 0 digest mismatches where both returned a digest; - each of these mutants makes the check fail: - padding computed from the tail; - no padding; - tail not hashed; - CheckSum included; - certificate entry or table included. The corpus, fuzz and mutant runs were independent passes with separate scripts on the same machine. Two earlier versions failed them and were fixed before this head: - the padding length was wrong for section ends not aligned to 8 (9 real executables); - the tool refused a real file whose SizeOfHeaders is larger than the first section pointer. Not covered: the CI runner's Windows build (26100) was not compared with the oracle.
upstream-sync closed this pull request 2026-11-24 21:27:33 +00:00
tedin7 commented 2026-11-24 21:27:33 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T21:10:50Z.

Review: COMMENTED


Review of cdd188ec9ad1d32fe2cfe90c5ac72ab6a86b58ce (comment only, no approval).

Verdict: no P1/P2 found.

Read: the full diff vs origin/main (tools/pe_authentihash.py, the two tests, CMake/CI/AGENTS/README lines). The tool is read-only, bounds-checks the DOS/PE/optional header, data directories and section table before use, and refuses (error, no digest) the layouts its docstring lists. The README sentence does not claim more than the docstring.

Commands run locally:

  • git diff --check origin/main...HEAD: clean
  • python3 -B tests/pe_authentihash_check.py: 12 tests OK
  • python3 tests/docs_links_check.py: 377 links OK; python3 tests/bidi_controls_check.py: no bidirectional controls
  • python3 -B tests/pe_authentihash_oracle_check.py: SKIP (Windows catalog API absent on Linux)

Mutations (each reverted with git checkout -- ., tree clean afterwards):

  1. hash up to size instead of cert_offset (certificate table included): 11 failures
  2. drop the zero padding to a multiple of 8: 20 failures
  3. disable the overlapping-sections refusal: 1 failure

Limits: the comparison with the Windows hash (pe_authentihash_oracle_check.py) is Windows-only and was not run here, so agreement with the Windows API rests on the constants in the test and on the Windows run recorded on the PR.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6047468456 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1911#pullrequestreview-6047468456). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-24T21:10:50Z. **Review:** COMMENTED --- Review of `cdd188ec9ad1d32fe2cfe90c5ac72ab6a86b58ce` (comment only, no approval). **Verdict: no P1/P2 found.** Read: the full diff vs `origin/main` (`tools/pe_authentihash.py`, the two tests, CMake/CI/AGENTS/README lines). The tool is read-only, bounds-checks the DOS/PE/optional header, data directories and section table before use, and refuses (error, no digest) the layouts its docstring lists. The README sentence does not claim more than the docstring. Commands run locally: - `git diff --check origin/main...HEAD`: clean - `python3 -B tests/pe_authentihash_check.py`: 12 tests OK - `python3 tests/docs_links_check.py`: 377 links OK; `python3 tests/bidi_controls_check.py`: no bidirectional controls - `python3 -B tests/pe_authentihash_oracle_check.py`: SKIP (Windows catalog API absent on Linux) Mutations (each reverted with `git checkout -- .`, tree clean afterwards): 1. hash up to `size` instead of `cert_offset` (certificate table included): 11 failures 2. drop the zero padding to a multiple of 8: 20 failures 3. disable the overlapping-sections refusal: 1 failure Limits: the comparison with the Windows hash (`pe_authentihash_oracle_check.py`) is Windows-only and was not run here, so agreement with the Windows API rests on the constants in the test and on the Windows run recorded on the PR. AI-assisted; commands listed were executed locally.
tedin7 commented 2026-11-24 21:27:33 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T21:25:08Z.

Review: COMMENTED


Re-review of exact head 11a7de790821bb1c61c86ff246560e145b0ad64f (my previous comment was on cdd188ec). Comment only, no approval.

Verdict: no P1/P2 found. The new commit only rewords the docstring, so my earlier conclusions still hold.

What changed since cdd188ec

  • git diff cdd188ec HEAD touches only the module docstring of tools/pe_authentihash.py (+2/-1). The old sentence said the hash "does not read" SectionAlignment, FileAlignment and the PointerToRawData of an empty section. The new one says "Their bytes are hashed; the tool does not validate their values." That is the accurate statement: those bytes sit in the headers and are hashed, only their values are not checked. The docstring is now consistent with the code. No code, test, CMake, CI or README change in this commit.

Commands run locally (venv x2w-rev-venv, worktree at the exact SHA, git rev-parse HEAD == headRefOid)

  • git diff --check origin/main...HEAD -> exit 0, no output
  • python -B tests/pe_authentihash_check.py -> Ran 12 tests ... OK
  • python -B tests/pe_authentihash_oracle_check.py -> SKIP: Windows catalog-admin hash API is not available (expected on Linux)
  • python -B tests/bidi_controls_check.py -> Checked 1340 tracked ... files: no bidirectional controls.
  • python -B tests/docs_links_check.py -> Checked 18 Markdown files and 377 links.
  • Windows Server 2025 lane, x2w_pipe.py win2025 1911 --targets xmem_shim --regex '^pe_authentihash': Debug and Release both 2/2 passed, including pe_authentihash_oracle (the comparison with the Windows catalog-admin hash), which cannot run on Linux.

Mutations (each applied to tools/pe_authentihash.py, tests/pe_authentihash_check.py re-run, then git checkout -- .; tree clean afterwards)

# Mutation Result
M1 drop the zero padding to a multiple of 8 (digest.update(bytes(-tail_end % 8))) killed, 20 failures
M2 certificate table no longer required to end at the file end killed, 3 failures
M3 certificate offset/size 8-byte alignment check disabled killed, 4 failures
M4 hash starts 4 bytes late after CheckSum killed, 36 failures

4/4 killed.

Limits

  • Agreement with the real Windows hash is covered only by the Windows lane above, not by the Linux run. Mutations were run against the Python suite only.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6047667210 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1911#pullrequestreview-6047667210). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-24T21:25:08Z. **Review:** COMMENTED --- Re-review of exact head `11a7de790821bb1c61c86ff246560e145b0ad64f` (my previous comment was on `cdd188ec`). Comment only, no approval. **Verdict: no P1/P2 found. The new commit only rewords the docstring, so my earlier conclusions still hold.** ### What changed since `cdd188ec` - `git diff cdd188ec HEAD` touches only the module docstring of `tools/pe_authentihash.py` (+2/-1). The old sentence said the hash "does not read" SectionAlignment, FileAlignment and the PointerToRawData of an empty section. The new one says "Their bytes are hashed; the tool does not validate their values." That is the accurate statement: those bytes sit in the headers and are hashed, only their values are not checked. The docstring is now consistent with the code. No code, test, CMake, CI or README change in this commit. ### Commands run locally (venv `x2w-rev-venv`, worktree at the exact SHA, `git rev-parse HEAD` == `headRefOid`) - `git diff --check origin/main...HEAD` -> exit 0, no output - `python -B tests/pe_authentihash_check.py` -> `Ran 12 tests ... OK` - `python -B tests/pe_authentihash_oracle_check.py` -> `SKIP: Windows catalog-admin hash API is not available` (expected on Linux) - `python -B tests/bidi_controls_check.py` -> `Checked 1340 tracked ... files: no bidirectional controls.` - `python -B tests/docs_links_check.py` -> `Checked 18 Markdown files and 377 links.` - Windows Server 2025 lane, `x2w_pipe.py win2025 1911 --targets xmem_shim --regex '^pe_authentihash'`: Debug and Release both **2/2 passed**, including `pe_authentihash_oracle` (the comparison with the Windows catalog-admin hash), which cannot run on Linux. ### Mutations (each applied to `tools/pe_authentihash.py`, `tests/pe_authentihash_check.py` re-run, then `git checkout -- .`; tree clean afterwards) | # | Mutation | Result | |---|---|---| | M1 | drop the zero padding to a multiple of 8 (`digest.update(bytes(-tail_end % 8))`) | killed, 20 failures | | M2 | certificate table no longer required to end at the file end | killed, 3 failures | | M3 | certificate offset/size 8-byte alignment check disabled | killed, 4 failures | | M4 | hash starts 4 bytes late after CheckSum | killed, 36 failures | 4/4 killed. ### Limits - Agreement with the real Windows hash is covered only by the Windows lane above, not by the Linux run. Mutations were run against the Python suite only. AI-assisted; commands listed were executed locally.
R0Y00 commented 2026-11-24 21:27:40 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-24T21:20:54Z.


Reran the 12 synthetic tests and the Windows catalog digest comparison in --exotic mode on cdd188ec; all matched. Clarified that SectionAlignment, FileAlignment and the section header pointer are included in the digest bytes even when the tool does not validate their values. The synthetic tests still pass after this documentation-only edit.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7144809210 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1911#issuecomment-7144809210). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-24T21:20:54Z. --- Reran the 12 synthetic tests and the Windows catalog digest comparison in `--exotic` mode on `cdd188ec`; all matched. Clarified that SectionAlignment, FileAlignment and the section header pointer are included in the digest bytes even when the tool does not validate their values. The synthetic tests still pass after this documentation-only edit.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
R0Y00/Xbox2Windows!1904
No description provided.