fix(pe): keep pefile's unrounded raw offset for a sub-page section

`createPefileReadCompatibility` models pefile's section reads so an import hash is published only
when both readers see the same bytes. Its layout gate rejected any section whose
`PointerToRawData` was not 512-aligned — but pefile 2024.8.26's `get_PointerToRawData_adj()` skips
that rounding when `SectionAlignment < 0x1000` and `PointerToRawData == VirtualAddress`, the
`LdrpWx86FormatVirtualImage` layout ntoskrnl-era drivers use.

The gate already modelled the sub-page case two lines above (`SectionAlignment < 0x1000 ?
FileAlignment : SectionAlignment`), then rejected it against an absolute 512 rule — and
`docs/pe-imports.md` scopes the withhold to an incomplete table or a different row sequence, not to
a file offset.

Measured on a minimal image with `SectionAlignment = FileAlignment = 0x80` and `.idata` at RVA 0x300
== file offset 0x300:

| | before | after |
| --- | --- | --- |
| coverage | `complete` | `complete` |
| issues | `[]` | `[]` |
| `imphash.md5` | `null` | `92cef6cc91a85825f3c8a0516517c66f` |

The withheld hash is the only thing that differed: both readers were already reading the same bytes.

The exception is exact rather than approximate — a section with the same sub-page alignment whose
`PointerToRawData` differs from its `VirtualAddress` still withholds, because pefile does round it
there. That case is the second new test.

pefile's current `master` broadened the condition to every sub-page image; this matches the
2024.8.26 revision the module documents, which is the narrower and safer rule.

`tests/fixtures/binaryDiagnostics/peImports.ts` takes an optional `layout`, so the shared builder
produces the geometry instead of a test hand-encoding a second image. Its default is unchanged —
`SectionAlignment`/`FileAlignment` are written only when supplied, so every existing fixture keeps
its exact bytes.

`src/native/pe` — 265 passing. Restoring the absolute 512 rule fails the sub-page case.
This commit is contained in:
XiaoBinGan authored and Ysopwnable committed 2026-11-21 21:08:49 +01:00
1 parent 360bcb4c6c
commit 985fa2fac9
3 files changed
+111 -12

No files matched your search

+49
View File
@@ -258,6 +258,55 @@ it("withholds the hash beyond pefile's section parser capacity", async () => {
expect(report.imphash.reason).toMatch(/mapping|compatibility/u);
});
it("hashes a sub-page-aligned section pefile reads at its unrounded offset", async () => {
// SectionAlignment == FileAlignment == 0x80 with .idata at RVA 0x300 == file offset 0x300:
// pefile 2024.8.26 keeps PointerToRawData unrounded when it equals VirtualAddress
// (LdrpWx86FormatVirtualImage), so both readers see the same bytes and the hash is provable.
const report = await parse(
peImportImage({
bits: 64,
layout: {
sectionRva: 0x300,
sectionOffset: 0x300,
headerBytes: 0x200,
sectionAlignment: 0x80,
fileAlignment: 0x80,
},
}),
);
expect(report.imports[0]?.functions.map((row) => row.name)).toEqual([
"GetProcAddress",
"ExitProcess",
]);
expect(report.coverage.status).toBe("complete");
expect(report.issues).toEqual([]);
expect(report.imphash.reason).toBeNull();
expect(report.imphash.md5).toBe(
createHash("md5")
.update("kernel32.getprocaddress,kernel32.exitprocess", "utf8")
.digest("hex"),
);
});
it("still withholds the hash when a sub-page section rounds to a different offset", async () => {
// Same sub-page alignment, but PointerToRawData 0x380 differs from VirtualAddress 0x400, so
// pefile does round it and the native mapping is not the one pefile reads.
const report = await parse(
peImportImage({
bits: 64,
layout: {
sectionRva: 0x400,
sectionOffset: 0x380,
headerBytes: 0x200,
sectionAlignment: 0x80,
fileAlignment: 0x80,
},
}),
);
expect(report.coverage.status).toBe("complete");
expect(report.imphash.md5).toBeNull();
expect(report.imphash.reason).toMatch(/mapping|compatibility/u);
});
it.each([false, true])(
"withholds the hash when pefile stops at an earlier section (suspicious: %s)",
async (suspicious) => {
+9 -1
View File
@@ -32,6 +32,14 @@ export const createPefileReadCompatibility = (
section.raw_size === 0 &&
section.raw_offset === 0 &&
section.characteristics === 0;
// pefile 2024.8.26 `get_PointerToRawData_adj()` skips the 512-byte rounding below the page
// size when the section's `PointerToRawData` equals its `VirtualAddress` — the
// `LdrpWx86FormatVirtualImage` layout ntoskrnl-era drivers use, where `SectionAlignment` and
// `FileAlignment` are both sub-page and the two values coincide. The gate models
// `adjust_PointerToRawData`, so it has to model that exception too, or such an image loses
// its hash while both readers are already reading the same bytes.
const unroundedRawOffset =
image.pe_section_alignment < 0x1000 && section.raw_offset === start;
const sectionWarnings =
Number(section.raw_offset + section.raw_size > fileBytes) +
Number(Math.floor(section.raw_offset / 512) * 512 > fileBytes) +
@@ -45,7 +53,7 @@ export const createPefileReadCompatibility = (
emptyHeader ||
sectionWarnings >= 3 ||
(alignment > 0 && start % alignment !== 0) ||
section.raw_offset % 0x200 !== 0 ||
(!unroundedRawOffset && section.raw_offset % 0x200 !== 0) ||
start < previousEnd
)
layoutAgrees = false;