Skip to content

armgit / rea

Public Imported from GitHub
0

Reverse engineer anything with agents, from app behavior down to native binaries.

fix(native): report PE resource subject architecture

morluto926e18b3fcb3
diff --git a/src/application/binaryDiagnostics/PeResourcesService.test.ts b/src/application/binaryDiagnostics/PeResourcesService.test.ts
index d6c58431f..8274b1086 100644
--- a/src/application/binaryDiagnostics/PeResourcesService.test.ts
+++ b/src/application/binaryDiagnostics/PeResourcesService.test.ts
@@ -50,6 +50,33 @@ it.each([
},
);
+it.each([
+ ["i386 PE32", 0x014c, "pe32", "x86"],
+ ["AMD64 PE32+", 0x8664, "pe32-plus", "x86_64"],
+ ["ARM PE32", 0x01c0, "pe32", "arm"],
+ ["Thumb PE32", 0x01c2, "pe32", "arm"],
+ ["ARMNT PE32", 0x01c4, "pe32", "arm"],
+ ["ARM64 PE32+", 0xaa64, "pe32-plus", "arm64"],
+ ["AMD64 machine with PE32 magic", 0x8664, "pe32", null],
+ ["i386 machine with PE32+ magic", 0x014c, "pe32-plus", null],
+ ["ReadyToRun Linux override", 0x8664 ^ 0x7b79, "pe32-plus", null],
+ ["unknown machine", 0x01f0, "pe32", null],
+] as const)(
+ "names the subject architecture from the COFF machine: %s",
+ async (_case, machine, format, architecture) => {
+ const report = await observedReport();
+ report.machine = machine;
+ report.format = format;
+ const service = new PeResourcesService({
+ identity: PE_RESOURCES_PROVIDER,
+ inspect: () => Promise.resolve(ok(report)),
+ });
+ const result = await service.inspect({ path: report.artifact.path });
+ if (!result.ok) throw result.error;
+ expect(result.value.subject?.architecture ?? null).toBe(architecture);
+ },
+);
+
it("retains a completed observation on late cancellation and keeps output errors primary", async () => {
const report = await observedReport();
const controller = new AbortController();
@@ -71,6 +98,7 @@ it("retains a completed observation on late cancellation and keeps output errors
if (!late.ok) {
expect(parseEvidence(late.error.partialObservation)).toMatchObject({
operation: "inspect_pe_resources",
+ subject: { architecture: "x86" },
normalized_result: report,
});
}
diff --git a/src/application/binaryDiagnostics/PeResourcesService.ts b/src/application/binaryDiagnostics/PeResourcesService.ts
index c64838e57..457ac5f33 100644
--- a/src/application/binaryDiagnostics/PeResourcesService.ts
+++ b/src/application/binaryDiagnostics/PeResourcesService.ts
@@ -15,6 +15,11 @@ import {
type PeResourceIdentity,
} from "../../domain/native/peResources.js";
import { analysisInputErrorFromIssues } from "../../domain/inputIssueProjection.js";
+import {
+ decodePeMachine,
+ peOptionalHeaderMagic,
+ type PeArchitecture,
+} from "../../domain/peMachine.js";
import { err, ok, type Result } from "../../domain/result.js";
import { completeProviderEvidence } from "../ProviderOperationExecution.js";
@@ -69,12 +74,14 @@ export class PeResourcesService {
),
);
const value = report.data;
+ const architecture = subjectArchitecture(value);
return completeProviderEvidence(
createEvidence(
{
path: value.artifact.path,
format: "pe",
sha256: value.artifact.sha256,
+ ...(architecture === undefined ? {} : { architecture }),
},
this.provider.identity,
{
@@ -91,6 +98,23 @@ export class PeResourcesService {
}
}
+/**
+ * Name the subject architecture from the reported COFF machine. The resource
+ * reader does not inspect the CLI header, so a ReadyToRun OS-override machine
+ * needs separate proof. An optional-header magic that contradicts the machine
+ * is observed conflicting evidence. Both stay unknown.
+ */
+const subjectArchitecture = (
+ report: PeResources,
+): PeArchitecture | undefined => {
+ const machine = decodePeMachine(report.machine);
+ if (machine === undefined || machine.nativeOsOverride) return undefined;
+ const magic = report.format === "pe32-plus" ? 0x20b : 0x10b;
+ return magic === peOptionalHeaderMagic(machine.architecture)
+ ? machine.architecture
+ : undefined;
+};
+
/** Check cross-field invariants before provider output becomes public Evidence. */
const validReport = (
report: PeResources,
diff --git a/src/domain/binaryTarget.ts b/src/domain/binaryTarget.ts
index e8d778081..dcbf8f397 100644
--- a/src/domain/binaryTarget.ts
+++ b/src/domain/binaryTarget.ts
@@ -11,7 +11,11 @@ import type {
import { err, ok, type Result } from "./result.js";
import { readElfFixedHeader } from "./elfFixedHeader.js";
import { observeMzWindowsHeader, parseDosMzHeader } from "./dosMz.js";
-import { decodePeMachine } from "./peMachine.js";
+import {
+ decodePeMachine,
+ peOptionalHeaderMagic,
+ type PeArchitecture,
+} from "./peMachine.js";
import {
classifyFatContainerIdentity,
readFatContainerHeader,
@@ -500,13 +504,11 @@ export const parsePeCodeViewFromBuffer = (
const peManagedStatus = (
optionalHeader: Buffer,
- architecture: BinaryArchitecture,
+ architecture: PeArchitecture,
): Result<boolean, string> => {
if (optionalHeader.length < 2) return err("truncated PE optional header");
const magic = optionalHeader.readUInt16LE(0);
- const expectedMagic =
- architecture === "x86_64" || architecture === "arm64" ? 0x20b : 0x10b;
- if (magic !== expectedMagic)
+ if (magic !== peOptionalHeaderMagic(architecture))
return err("PE optional-header magic does not match its architecture");
const directoryOffset = magic === 0x20b ? 112 : 96;
const directoryCountOffset = directoryOffset - 4;
diff --git a/src/domain/peMachine.ts b/src/domain/peMachine.ts
index 18981c88c..c7cc16a11 100644
--- a/src/domain/peMachine.ts
+++ b/src/domain/peMachine.ts
@@ -26,6 +26,12 @@ const architectureForMachine = (
return undefined;
};
+/** The optional-header magic a machine's architecture requires: PE32+ for 64-bit, PE32 otherwise. */
+export const peOptionalHeaderMagic = (
+ architecture: PeArchitecture,
+): 0x10b | 0x20b =>
+ architecture === "x86_64" || architecture === "arm64" ? 0x20b : 0x10b;
+
/**
* ReadyToRun images compiled for a non-Windows OS store the COFF machine XORed
* with IMAGE_FILE_MACHINE_NATIVE_OS_OVERRIDE (dotnet/runtime
diff --git a/tests/acceptance/applications/peResources.test.ts b/tests/acceptance/applications/peResources.test.ts
index 213838cc7..5e2d7cc88 100644
--- a/tests/acceptance/applications/peResources.test.ts
+++ b/tests/acceptance/applications/peResources.test.ts
@@ -167,6 +167,7 @@ const expectPeMappingOwnershipAcrossAdapters = async (
});
expect(response.isError, name).not.toBe(true);
const mcp = contract.outputSchema.parse(response.structuredContent);
+ expect(mcp.subject).toEqual(evidence.subject);
expect(mcp.evidence_id).toBe(evidence.evidence_id);
expect(mcp.normalized_result).toEqual(evidence.normalized_result);
expect(evidence.normalized_result).toMatchObject({
@@ -220,6 +221,7 @@ const expectPeInventoriesAcrossAdapters = async (
expect(evidence.subject?.digest).toMatchObject({
sha256: createHash("sha256").update(fixture.bytes).digest("hex"),
});
+ expect(evidence.subject?.architecture).toBe(plus ? "x86_64" : "x86");
expect(report.coverage).toMatchObject({
status: "complete",
resources: 5,
@@ -255,6 +257,7 @@ const expectPeInventoriesAcrossAdapters = async (
});
expect(response.isError).not.toBe(true);
const mcp = contract.outputSchema.parse(response.structuredContent);
+ expect(mcp.subject).toEqual(evidence.subject);
expect(mcp.evidence_id).toBe(evidence.evidence_id);
expect(mcp.normalized_result).toEqual(evidence.normalized_result);
const text = response.content.find(({ type }) => type === "text");