refactor(test): keep coverage at behavioral boundaries (#474)
* refactor(test): keep coverage at behavioral boundaries * fix(ci): restore package workflow temp-root classification
This commit is contained in:
@@ -1,350 +0,0 @@
|
||||
import { createHash } from "node:crypto";
|
||||
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { projectManagedApplicationGraphEvidence } from "../../../../src/application/ManagedApplicationGraphService.js";
|
||||
import { traceApplicationFeatureEvidence } from "../../../../src/application/JavaScriptApplicationWorkflowService.js";
|
||||
import { MANAGED_STATIC_PROVIDER } from "../../../../src/application/InvestigationProviders.js";
|
||||
import type { BinaryTarget } from "../../../../src/domain/binaryTarget.js";
|
||||
import {
|
||||
createEvidence,
|
||||
parseEvidence,
|
||||
} from "../../../../src/domain/evidence.js";
|
||||
import { parseJavaScriptApplicationGraph } from "../../../../src/domain/javascriptApplicationGraph.js";
|
||||
import { managedApplicationGraphResultSchema } from "../../../../src/domain/managedApplicationGraph.js";
|
||||
import { inspectManagedArtifactBytes } from "../../../../src/dotnet/ManagedArtifactInspector.js";
|
||||
import { inspectManagedMembersBytes } from "../../../../src/dotnet/ManagedMemberInspector.js";
|
||||
import { inspectManagedNativeBoundariesBytes } from "../../../../src/dotnet/ManagedNativeBoundaryInspector.js";
|
||||
import { buildManagedPeFixture } from "../../../fixtures/managedPe.js";
|
||||
|
||||
const artifactLimits = {
|
||||
referenceOffset: 0,
|
||||
referenceLimit: 100,
|
||||
resourceOffset: 0,
|
||||
resourceLimit: 100,
|
||||
attributeOffset: 0,
|
||||
attributeLimit: 100,
|
||||
maxMetadataBytes: 1024 * 1024,
|
||||
maxTableRows: 1_000,
|
||||
maxHeapItemBytes: 1024 * 1024,
|
||||
};
|
||||
|
||||
const memberLimits = {
|
||||
typeOffset: 0,
|
||||
typeLimit: 100,
|
||||
methodOffset: 0,
|
||||
methodLimit: 100,
|
||||
fieldOffset: 0,
|
||||
fieldLimit: 100,
|
||||
memberRefOffset: 0,
|
||||
memberRefLimit: 100,
|
||||
edgeOffset: 0,
|
||||
edgeLimit: 100,
|
||||
instructionAnchorLimit: 100,
|
||||
maxMetadataBytes: 1024 * 1024,
|
||||
maxTableRows: 1_000,
|
||||
maxHeapItemBytes: 1024 * 1024,
|
||||
maxMethodBodyBytes: 1024 * 1024,
|
||||
maxMethodInstructions: 1_000,
|
||||
};
|
||||
|
||||
const boundaryLimits = {
|
||||
moduleRefOffset: 0,
|
||||
moduleRefLimit: 100,
|
||||
importOffset: 0,
|
||||
importLimit: 100,
|
||||
implementationOffset: 0,
|
||||
implementationLimit: 100,
|
||||
maxMetadataBytes: 1024 * 1024,
|
||||
maxTableRows: 1_000,
|
||||
maxHeapItemBytes: 1024 * 1024,
|
||||
};
|
||||
|
||||
describe("managed application graph projection", () => {
|
||||
it("projects managed metadata and native declarations into authenticated graph Evidence", () => {
|
||||
const bytes = buildManagedPeFixture({
|
||||
pinvoke: {
|
||||
moduleName: "user32.dll",
|
||||
importName: "MessageBoxW",
|
||||
mappingFlags: 0x0345,
|
||||
},
|
||||
});
|
||||
const binary = target(bytes);
|
||||
const managedArtifact = inspectManagedArtifactBytes(
|
||||
bytes,
|
||||
binary,
|
||||
artifactLimits,
|
||||
);
|
||||
const members = inspectManagedMembersBytes(bytes, binary, memberLimits);
|
||||
const boundaries = inspectManagedNativeBoundariesBytes(
|
||||
bytes,
|
||||
binary,
|
||||
boundaryLimits,
|
||||
);
|
||||
const artifactEvidence = createEvidence(binary, MANAGED_STATIC_PROVIDER, {
|
||||
operation: "inspect_managed_artifact",
|
||||
parameters: {},
|
||||
result: managedArtifact,
|
||||
rawResult: null,
|
||||
limitations: managedArtifact.limitations,
|
||||
locations: [{ kind: "artifact-path", path: binary.path }],
|
||||
});
|
||||
const memberEvidence = createEvidence(binary, MANAGED_STATIC_PROVIDER, {
|
||||
operation: "inspect_managed_members",
|
||||
parameters: {},
|
||||
result: members,
|
||||
rawResult: null,
|
||||
limitations: members.limitations,
|
||||
locations: [{ kind: "artifact-path", path: binary.path }],
|
||||
});
|
||||
const boundaryEvidence = createEvidence(binary, MANAGED_STATIC_PROVIDER, {
|
||||
operation: "inspect_managed_native_boundaries",
|
||||
parameters: {},
|
||||
result: boundaries,
|
||||
rawResult: null,
|
||||
limitations: boundaries.limitations,
|
||||
locations: [{ kind: "artifact-path", path: binary.path }],
|
||||
});
|
||||
|
||||
const evidence = projectManagedApplicationGraphEvidence({
|
||||
managed_artifact: artifactEvidence,
|
||||
managed_members: memberEvidence,
|
||||
managed_native_boundaries: boundaryEvidence,
|
||||
limits: {
|
||||
max_types: 100,
|
||||
max_methods: 100,
|
||||
max_fields: 100,
|
||||
max_pinvoke_imports: 100,
|
||||
max_native_implementations: 100,
|
||||
},
|
||||
});
|
||||
|
||||
expect(
|
||||
evidence.ok,
|
||||
evidence.ok
|
||||
? undefined
|
||||
: `${JSON.stringify(evidence.error)} cause=${String(evidence.error.cause)}`,
|
||||
).toBe(true);
|
||||
if (!evidence.ok) throw new Error("projection failed");
|
||||
const parsed = parseEvidence(evidence.value);
|
||||
expect(parsed).toMatchObject({
|
||||
operation: "project_managed_application_graph",
|
||||
predicate_type: "rea.managed-application-graph/v1",
|
||||
provider: { id: "rea-dotnet-workflows" },
|
||||
confidence: "inferred",
|
||||
authority: "analyst-inference",
|
||||
evidence_links: [
|
||||
artifactEvidence.evidence_id,
|
||||
memberEvidence.evidence_id,
|
||||
boundaryEvidence.evidence_id,
|
||||
],
|
||||
});
|
||||
const result = managedApplicationGraphResultSchema.parse(
|
||||
parsed.normalized_result,
|
||||
);
|
||||
const graph = parseJavaScriptApplicationGraph(result.graph);
|
||||
expect(result.summary).toMatchObject({
|
||||
assemblies: 1,
|
||||
modules: 1,
|
||||
types: 1,
|
||||
methods: 1,
|
||||
fields: 1,
|
||||
pinvoke_imports: 1,
|
||||
});
|
||||
expect(graph.nodes.map(({ kind }) => kind)).toEqual(
|
||||
expect.arrayContaining([
|
||||
"artifact",
|
||||
"managed-assembly",
|
||||
"managed-module",
|
||||
"managed-type",
|
||||
"managed-method",
|
||||
"managed-field",
|
||||
"managed-pinvoke-import",
|
||||
]),
|
||||
);
|
||||
const method = graph.nodes.find(
|
||||
({ kind, observations }) =>
|
||||
kind === "managed-method" &&
|
||||
observations[0]?.label === "Fixture.Program.Main",
|
||||
);
|
||||
expect(method).toBeDefined();
|
||||
expect(
|
||||
graph.edges.some(
|
||||
({ source_node_id, relation, target_node_id }) =>
|
||||
source_node_id === method?.node_id &&
|
||||
relation === "imports" &&
|
||||
graph.nodes.find(({ node_id: id }) => id === target_node_id)?.kind ===
|
||||
"managed-pinvoke-import",
|
||||
),
|
||||
).toBe(true);
|
||||
expect(
|
||||
graph.nodes.flatMap(({ observations }) =>
|
||||
observations.map(({ evidence }) => evidence.authority),
|
||||
),
|
||||
).toContain("managed-static-analysis");
|
||||
|
||||
const trace = traceApplicationFeatureEvidence({
|
||||
application: parsed,
|
||||
native_observations: [],
|
||||
seed: {
|
||||
kind: "string",
|
||||
value: "MessageBoxW",
|
||||
match: "exact",
|
||||
case_sensitive: true,
|
||||
},
|
||||
direction: "incoming",
|
||||
limits: {
|
||||
max_seed_matches: 5,
|
||||
max_depth: 4,
|
||||
max_nodes: 50,
|
||||
max_edges: 100,
|
||||
max_paths: 10,
|
||||
},
|
||||
});
|
||||
expect(trace.ok).toBe(true);
|
||||
if (!trace.ok) throw new Error("trace failed");
|
||||
expect(trace.value.normalized_result).toMatchObject({
|
||||
source_evidence_id: parsed.evidence_id,
|
||||
summary: { matched_seeds: 1 },
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("managed application graph coverage", () => {
|
||||
it("preserves source-page omissions in graph and per-fact coverage", () => {
|
||||
const bytes = buildManagedPeFixture();
|
||||
const binary = target(bytes);
|
||||
const members = inspectManagedMembersBytes(bytes, binary, memberLimits);
|
||||
const partialMembers = {
|
||||
...members,
|
||||
methods: {
|
||||
...members.methods,
|
||||
total: 2,
|
||||
returned: 1,
|
||||
dropped: 1,
|
||||
complete: false,
|
||||
},
|
||||
};
|
||||
const memberEvidence = createEvidence(binary, MANAGED_STATIC_PROVIDER, {
|
||||
operation: "inspect_managed_members",
|
||||
parameters: {},
|
||||
result: partialMembers,
|
||||
rawResult: null,
|
||||
limitations: partialMembers.limitations,
|
||||
locations: [{ kind: "artifact-path", path: binary.path }],
|
||||
});
|
||||
|
||||
const evidence = projectManagedApplicationGraphEvidence({
|
||||
managed_members: memberEvidence,
|
||||
limits: {
|
||||
max_types: 100,
|
||||
max_methods: 100,
|
||||
max_fields: 100,
|
||||
max_pinvoke_imports: 100,
|
||||
max_native_implementations: 100,
|
||||
},
|
||||
});
|
||||
|
||||
expect(
|
||||
evidence.ok,
|
||||
evidence.ok
|
||||
? undefined
|
||||
: `${JSON.stringify(evidence.error)} cause=${String(evidence.error.cause)}`,
|
||||
).toBe(true);
|
||||
if (!evidence.ok) throw new Error("projection failed");
|
||||
const result = managedApplicationGraphResultSchema.parse(
|
||||
parseEvidence(evidence.value).normalized_result,
|
||||
);
|
||||
const graph = parseJavaScriptApplicationGraph(result.graph);
|
||||
expect(result.coverage).toMatchObject({
|
||||
status: "truncated",
|
||||
omitted_types: 0,
|
||||
omitted_methods: 1,
|
||||
omitted_fields: 0,
|
||||
});
|
||||
expect(graph.coverage).toMatchObject({
|
||||
status: "partial",
|
||||
truncated: true,
|
||||
omitted_count: 1,
|
||||
});
|
||||
const method = graph.nodes.find(({ kind }) => kind === "managed-method");
|
||||
expect(method?.observations[0]?.evidence.coverage).toMatchObject({
|
||||
status: "partial",
|
||||
truncated: true,
|
||||
omitted_count: 1,
|
||||
});
|
||||
expect(
|
||||
graph.edges.find(
|
||||
({ target_node_id }) => target_node_id === method?.node_id,
|
||||
)?.evidence.coverage,
|
||||
).toMatchObject({
|
||||
status: "partial",
|
||||
truncated: true,
|
||||
omitted_count: 1,
|
||||
});
|
||||
|
||||
const parserPartialMembers = {
|
||||
...partialMembers,
|
||||
coverage: {
|
||||
state: "partial" as const,
|
||||
issues: [
|
||||
{
|
||||
code: "limit-exceeded" as const,
|
||||
scope: "method.0x06000001.body.instructions",
|
||||
offset: 0x0a00,
|
||||
detail: "instruction limit reached",
|
||||
},
|
||||
],
|
||||
},
|
||||
};
|
||||
const parserPartialEvidence = createEvidence(
|
||||
binary,
|
||||
MANAGED_STATIC_PROVIDER,
|
||||
{
|
||||
operation: "inspect_managed_members",
|
||||
parameters: {},
|
||||
result: parserPartialMembers,
|
||||
rawResult: null,
|
||||
limitations: parserPartialMembers.limitations,
|
||||
},
|
||||
);
|
||||
const parserPartialProjection = projectManagedApplicationGraphEvidence({
|
||||
managed_members: parserPartialEvidence,
|
||||
limits: {
|
||||
max_types: 100,
|
||||
max_methods: 100,
|
||||
max_fields: 100,
|
||||
max_pinvoke_imports: 100,
|
||||
max_native_implementations: 100,
|
||||
},
|
||||
});
|
||||
|
||||
expect(parserPartialProjection.ok).toBe(true);
|
||||
if (!parserPartialProjection.ok)
|
||||
throw new Error("partial projection failed");
|
||||
const parserPartialResult = managedApplicationGraphResultSchema.parse(
|
||||
parseEvidence(parserPartialProjection.value).normalized_result,
|
||||
);
|
||||
expect(parserPartialResult.coverage).toMatchObject({
|
||||
status: "partial",
|
||||
omitted_methods: 1,
|
||||
});
|
||||
expect(parserPartialResult.graph.coverage).toEqual({
|
||||
status: "partial",
|
||||
truncated: false,
|
||||
omitted_count: null,
|
||||
limits: [],
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
const target = (bytes: Buffer): BinaryTarget => ({
|
||||
path: "/fixture/ManagedInterop.exe",
|
||||
sha256: createHash("sha256").update(bytes).digest("hex"),
|
||||
kind: "executable",
|
||||
format: "pe",
|
||||
architecture: "x86",
|
||||
availableArchitectures: ["x86"],
|
||||
executableRole: "application",
|
||||
managed: true,
|
||||
});
|
||||
@@ -1,140 +0,0 @@
|
||||
import { createHash } from "node:crypto";
|
||||
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import type { BinaryTarget } from "../../../../src/domain/binaryTarget.js";
|
||||
import { inspectManagedMembersBytes } from "../../../../src/dotnet/ManagedMemberInspector.js";
|
||||
import { buildManagedPeFixture } from "../../../fixtures/managedPe.js";
|
||||
|
||||
const memberLimits = {
|
||||
typeOffset: 0,
|
||||
typeLimit: 100,
|
||||
methodOffset: 0,
|
||||
methodLimit: 100,
|
||||
fieldOffset: 0,
|
||||
fieldLimit: 100,
|
||||
memberRefOffset: 0,
|
||||
memberRefLimit: 100,
|
||||
edgeOffset: 0,
|
||||
edgeLimit: 100,
|
||||
instructionAnchorLimit: 100,
|
||||
maxMetadataBytes: 1024 * 1024,
|
||||
maxTableRows: 1_000,
|
||||
maxHeapItemBytes: 1024 * 1024,
|
||||
maxMethodBodyBytes: 1024 * 1024,
|
||||
maxMethodInstructions: 1_000,
|
||||
};
|
||||
|
||||
describe("managed CIL decoding", () => {
|
||||
it("reads fat method header size from the full flags-and-size word", () => {
|
||||
const il = Buffer.from([
|
||||
0x21, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x26, 0x22, 0x00,
|
||||
0x00, 0x80, 0x3f, 0x26, 0x23, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0,
|
||||
0x3f, 0x26, 0xfe, 0x06, 0x01, 0x00, 0x00, 0x06, 0x26, 0x2a,
|
||||
]);
|
||||
const header = Buffer.alloc(12);
|
||||
header.writeUInt16LE(0x3013, 0);
|
||||
header.writeUInt16LE(8, 2);
|
||||
header.writeUInt32LE(il.length, 4);
|
||||
const bytes = buildManagedPeFixture({
|
||||
ilBody: Buffer.concat([header, il]),
|
||||
});
|
||||
const result = inspectManagedMembersBytes(
|
||||
bytes,
|
||||
target(bytes),
|
||||
memberLimits,
|
||||
);
|
||||
|
||||
expect(result.methods.items[0]?.body).toMatchObject({
|
||||
status: "present",
|
||||
header_format: "fat",
|
||||
il_size: il.length,
|
||||
il_sha256: createHash("sha256").update(il).digest("hex"),
|
||||
opcode_counts: {
|
||||
"ldc.i8": 1,
|
||||
pop: 4,
|
||||
"ldc.r4": 1,
|
||||
"ldc.r8": 1,
|
||||
ldftn: 1,
|
||||
ret: 1,
|
||||
},
|
||||
issue: null,
|
||||
});
|
||||
});
|
||||
|
||||
it("does not normalize reserved CIL opcodes as operand-free instructions", () => {
|
||||
const bytes = buildManagedPeFixture({
|
||||
ilBody: Buffer.from([0x0a, 0x24, 0x2a]),
|
||||
});
|
||||
const result = inspectManagedMembersBytes(
|
||||
bytes,
|
||||
target(bytes),
|
||||
memberLimits,
|
||||
);
|
||||
|
||||
expect(result.methods.items[0]?.body).toMatchObject({
|
||||
status: "malformed",
|
||||
il_size: 2,
|
||||
normalized_il_sha256: null,
|
||||
decoded_instruction_count: 0,
|
||||
issue: "Unsupported CIL opcode 0x24 at IL offset 0",
|
||||
});
|
||||
});
|
||||
|
||||
it("keeps the documented decoded-CIL v1 golden vector stable", () => {
|
||||
const il = Buffer.from([0x00, 0x2a]);
|
||||
const tinyBytes = buildManagedPeFixture({
|
||||
ilBody: Buffer.from([0x0a, ...il]),
|
||||
});
|
||||
const fatHeader = Buffer.alloc(12);
|
||||
fatHeader.writeUInt16LE(0x3013, 0);
|
||||
fatHeader.writeUInt16LE(32, 2);
|
||||
fatHeader.writeUInt32LE(il.length, 4);
|
||||
fatHeader.writeUInt32LE(0x1100_0001, 8);
|
||||
const fatBytes = buildManagedPeFixture({
|
||||
ilBody: Buffer.concat([fatHeader, il]),
|
||||
});
|
||||
const tiny = inspectManagedMembersBytes(
|
||||
tinyBytes,
|
||||
target(tinyBytes),
|
||||
memberLimits,
|
||||
);
|
||||
const fat = inspectManagedMembersBytes(
|
||||
fatBytes,
|
||||
target(fatBytes),
|
||||
memberLimits,
|
||||
);
|
||||
|
||||
expect(tiny.methods.items[0]?.body).toMatchObject({
|
||||
status: "present",
|
||||
header_format: "tiny",
|
||||
max_stack: 8,
|
||||
init_locals: false,
|
||||
local_var_sig_token: null,
|
||||
il_size: 2,
|
||||
il_sha256: createHash("sha256").update(il).digest("hex"),
|
||||
normalized_il_sha256:
|
||||
"5e5fad7741cb44bca3a4f045546b7449990da343f612f5f34c0ca30e9eee0636",
|
||||
opcode_counts: { nop: 1, ret: 1 },
|
||||
exception_regions: [],
|
||||
});
|
||||
expect(fat.methods.items[0]?.body).toMatchObject({
|
||||
header_format: "fat",
|
||||
max_stack: 32,
|
||||
init_locals: true,
|
||||
local_var_sig_token: "0x11000001",
|
||||
il_sha256: tiny.methods.items[0]?.body.il_sha256,
|
||||
normalized_il_sha256: tiny.methods.items[0]?.body.normalized_il_sha256,
|
||||
});
|
||||
});
|
||||
});
|
||||
const target = (bytes: Buffer): BinaryTarget => ({
|
||||
path: "/fixture.exe",
|
||||
sha256: createHash("sha256").update(bytes).digest("hex"),
|
||||
kind: "executable",
|
||||
format: "pe",
|
||||
architecture: "x86",
|
||||
availableArchitectures: ["x86"],
|
||||
executableRole: "application",
|
||||
managed: true,
|
||||
});
|
||||
@@ -1,75 +0,0 @@
|
||||
import { createHash } from "node:crypto";
|
||||
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import type { BinaryTarget } from "../../../../src/domain/binaryTarget.js";
|
||||
import { inspectManagedMembersBytes } from "../../../../src/dotnet/ManagedMemberInspector.js";
|
||||
import { buildManagedPeFixture } from "../../../fixtures/managedPe.js";
|
||||
|
||||
const memberLimits = {
|
||||
typeOffset: 0,
|
||||
typeLimit: 100,
|
||||
methodOffset: 0,
|
||||
methodLimit: 100,
|
||||
fieldOffset: 0,
|
||||
fieldLimit: 100,
|
||||
memberRefOffset: 0,
|
||||
memberRefLimit: 100,
|
||||
edgeOffset: 0,
|
||||
edgeLimit: 100,
|
||||
instructionAnchorLimit: 100,
|
||||
maxMetadataBytes: 1024 * 1024,
|
||||
maxTableRows: 1_000,
|
||||
maxHeapItemBytes: 1024 * 1024,
|
||||
maxMethodBodyBytes: 1024 * 1024,
|
||||
maxMethodInstructions: 1_000,
|
||||
};
|
||||
|
||||
describe("managed CIL limits", () => {
|
||||
it("marks instruction-limited CIL as partial without assigning normalized identity", () => {
|
||||
const bytes = buildManagedPeFixture();
|
||||
const result = inspectManagedMembersBytes(bytes, target(bytes), {
|
||||
...memberLimits,
|
||||
maxMethodInstructions: 1,
|
||||
});
|
||||
const il = bytes.subarray(0x0a01, 0x0a0d);
|
||||
|
||||
expect(result.metadata.status).toBe("complete");
|
||||
expect(result.methods.items[0]?.body).toMatchObject({
|
||||
status: "partial",
|
||||
il_size: 12,
|
||||
il_sha256: createHash("sha256").update(il).digest("hex"),
|
||||
normalized_il_sha256: null,
|
||||
instruction_count: 1,
|
||||
decoded_instruction_count: 1,
|
||||
truncated_instructions: 1,
|
||||
issue:
|
||||
"Instruction decode reached max_method_instructions 1 before the end of the method body",
|
||||
});
|
||||
expect(result.coverage).toEqual({
|
||||
state: "partial",
|
||||
issues: [
|
||||
{
|
||||
code: "limit-exceeded",
|
||||
scope: "method.0x06000001.body.instructions",
|
||||
offset: 0x0a00,
|
||||
detail:
|
||||
"Instruction decode reached max_method_instructions 1 before the end of the method body",
|
||||
},
|
||||
],
|
||||
});
|
||||
expect(result.limitations.join(" ")).toContain(
|
||||
"has no normalized CIL identity",
|
||||
);
|
||||
});
|
||||
});
|
||||
const target = (bytes: Buffer): BinaryTarget => ({
|
||||
path: "/fixture.exe",
|
||||
sha256: createHash("sha256").update(bytes).digest("hex"),
|
||||
kind: "executable",
|
||||
format: "pe",
|
||||
architecture: "x86",
|
||||
availableArchitectures: ["x86"],
|
||||
executableRole: "application",
|
||||
managed: true,
|
||||
});
|
||||
@@ -1,173 +0,0 @@
|
||||
import { createHash } from "node:crypto";
|
||||
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import type { BinaryTarget } from "../../../../src/domain/binaryTarget.js";
|
||||
import { inspectManagedArtifactBytes } from "../../../../src/dotnet/ManagedArtifactInspector.js";
|
||||
import { inspectManagedMembersBytes } from "../../../../src/dotnet/ManagedMemberInspector.js";
|
||||
import { inspectManagedNativeBoundariesBytes } from "../../../../src/dotnet/ManagedNativeBoundaryInspector.js";
|
||||
import { buildManagedPeFixture } from "../../../fixtures/managedPe.js";
|
||||
|
||||
const limits = {
|
||||
referenceOffset: 0,
|
||||
referenceLimit: 100,
|
||||
resourceOffset: 0,
|
||||
resourceLimit: 100,
|
||||
attributeOffset: 0,
|
||||
attributeLimit: 100,
|
||||
maxMetadataBytes: 1024 * 1024,
|
||||
maxTableRows: 1_000,
|
||||
maxHeapItemBytes: 1024 * 1024,
|
||||
};
|
||||
|
||||
const memberLimits = {
|
||||
typeOffset: 0,
|
||||
typeLimit: 100,
|
||||
methodOffset: 0,
|
||||
methodLimit: 100,
|
||||
fieldOffset: 0,
|
||||
fieldLimit: 100,
|
||||
memberRefOffset: 0,
|
||||
memberRefLimit: 100,
|
||||
edgeOffset: 0,
|
||||
edgeLimit: 100,
|
||||
instructionAnchorLimit: 100,
|
||||
maxMetadataBytes: 1024 * 1024,
|
||||
maxTableRows: 1_000,
|
||||
maxHeapItemBytes: 1024 * 1024,
|
||||
maxMethodBodyBytes: 1024 * 1024,
|
||||
maxMethodInstructions: 1_000,
|
||||
};
|
||||
|
||||
const nativeBoundaryLimits = {
|
||||
moduleRefOffset: 0,
|
||||
moduleRefLimit: 100,
|
||||
importOffset: 0,
|
||||
importLimit: 100,
|
||||
implementationOffset: 0,
|
||||
implementationLimit: 100,
|
||||
maxMetadataBytes: 1024 * 1024,
|
||||
maxTableRows: 1_000,
|
||||
maxHeapItemBytes: 1024 * 1024,
|
||||
};
|
||||
|
||||
describe("managed artifact inventory", () => {
|
||||
it("inventories module, assembly, framework, references, and resources without loading CLR code", () => {
|
||||
const resource = Buffer.from("source-owned resource");
|
||||
const bytes = buildManagedPeFixture({ resourceData: resource });
|
||||
const result = inspectManagedArtifactBytes(bytes, target(bytes), limits);
|
||||
|
||||
expect(result.classification).toMatchObject({
|
||||
status: "managed",
|
||||
runtime_family: "modern-dotnet",
|
||||
implementation: "cil",
|
||||
managed_architecture: "anycpu",
|
||||
});
|
||||
expect(result.pe.cli).toMatchObject({
|
||||
flag_names: ["il-only"],
|
||||
entry_point: { kind: "metadata-token", value: "0x06000001" },
|
||||
});
|
||||
expect(result.module).toMatchObject({
|
||||
name: "Fixture.dll",
|
||||
mvid: "00112233-4455-6677-8899-aabbccddeeff",
|
||||
token: "0x00000001",
|
||||
});
|
||||
expect(result.assembly).toMatchObject({
|
||||
name: "Fixture.Managed",
|
||||
version: "1.2.3.4",
|
||||
token: "0x20000001",
|
||||
});
|
||||
expect(result.target_frameworks).toEqual([".NETCoreApp,Version=v8.0"]);
|
||||
expect(result.references).toMatchObject({
|
||||
total: 1,
|
||||
returned: 1,
|
||||
complete: true,
|
||||
items: [expect.objectContaining({ name: "System.Runtime" })],
|
||||
});
|
||||
expect(result.resources.items).toEqual([
|
||||
expect.objectContaining({
|
||||
name: "Fixture.resources",
|
||||
embedded: true,
|
||||
visibility: "private",
|
||||
data_length: resource.length,
|
||||
data_sha256: createHash("sha256").update(resource).digest("hex"),
|
||||
}),
|
||||
]);
|
||||
expect(result.coverage).toMatchObject({ state: "complete", issues: [] });
|
||||
});
|
||||
|
||||
it("keeps classification evidence complete when caller pages visible references", () => {
|
||||
const bytes = buildManagedPeFixture({
|
||||
references: ["System.Runtime", "UnityEngine.CoreModule"],
|
||||
});
|
||||
const result = inspectManagedArtifactBytes(bytes, target(bytes), {
|
||||
...limits,
|
||||
referenceLimit: 1,
|
||||
});
|
||||
|
||||
expect(result.classification.runtime_family).toBe("unity-mono");
|
||||
expect(result.references).toMatchObject({
|
||||
total: 2,
|
||||
returned: 1,
|
||||
complete: false,
|
||||
});
|
||||
expect(result.coverage.state).toBe("partial");
|
||||
expect(result.coverage.issues).toEqual([]);
|
||||
});
|
||||
|
||||
it("accepts CLI metadata GUIDs without RFC UUID version or variant bits", () => {
|
||||
const bytes = buildManagedPeFixture({
|
||||
mvid: Buffer.from("3aebc60edc4a544b1f458b4ed40b33b1", "hex"),
|
||||
});
|
||||
const result = inspectManagedArtifactBytes(bytes, target(bytes), limits);
|
||||
const members = inspectManagedMembersBytes(
|
||||
bytes,
|
||||
target(bytes),
|
||||
memberLimits,
|
||||
);
|
||||
const boundaries = inspectManagedNativeBoundariesBytes(
|
||||
bytes,
|
||||
target(bytes),
|
||||
nativeBoundaryLimits,
|
||||
);
|
||||
|
||||
expect(result.module?.mvid).toBe("0ec6eb3a-4adc-4b54-1f45-8b4ed40b33b1");
|
||||
expect(result.classification.status).toBe("managed");
|
||||
expect(members.identity_scope.requires_mvid).toBe(result.module?.mvid);
|
||||
expect(boundaries.identity_scope.requires_mvid).toBe(result.module?.mvid);
|
||||
});
|
||||
|
||||
it("decodes ECMA-335 pointer and byref element types without swapping them", () => {
|
||||
const bytes = buildManagedPeFixture({
|
||||
fieldSignature: Buffer.from([0x06, 0x1d, 0x0f, 0x0f, 0x08]),
|
||||
methodSignature: Buffer.from([
|
||||
0x00, 0x02, 0x10, 0x08, 0x0f, 0x08, 0x10, 0x0e,
|
||||
]),
|
||||
});
|
||||
const result = inspectManagedMembersBytes(
|
||||
bytes,
|
||||
target(bytes),
|
||||
memberLimits,
|
||||
);
|
||||
|
||||
expect(result.fields.items[0]?.signature).toMatchObject({
|
||||
parse_status: "decoded",
|
||||
field_type: "i4**[]",
|
||||
});
|
||||
expect(result.methods.items[0]?.signature).toMatchObject({
|
||||
parse_status: "decoded",
|
||||
return_type: "i4&",
|
||||
parameter_types: ["i4*", "string&"],
|
||||
});
|
||||
});
|
||||
});
|
||||
const target = (bytes: Buffer): BinaryTarget => ({
|
||||
path: "/fixture.exe",
|
||||
sha256: createHash("sha256").update(bytes).digest("hex"),
|
||||
kind: "executable",
|
||||
format: "pe",
|
||||
architecture: "x86",
|
||||
availableArchitectures: ["x86"],
|
||||
executableRole: "application",
|
||||
managed: true,
|
||||
});
|
||||
@@ -1,142 +0,0 @@
|
||||
import { createHash } from "node:crypto";
|
||||
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import type { BinaryTarget } from "../../../../src/domain/binaryTarget.js";
|
||||
import { inspectManagedMembersBytes } from "../../../../src/dotnet/ManagedMemberInspector.js";
|
||||
import { buildManagedPeFixture } from "../../../fixtures/managedPe.js";
|
||||
|
||||
const memberLimits = {
|
||||
typeOffset: 0,
|
||||
typeLimit: 100,
|
||||
methodOffset: 0,
|
||||
methodLimit: 100,
|
||||
fieldOffset: 0,
|
||||
fieldLimit: 100,
|
||||
memberRefOffset: 0,
|
||||
memberRefLimit: 100,
|
||||
edgeOffset: 0,
|
||||
edgeLimit: 100,
|
||||
instructionAnchorLimit: 100,
|
||||
maxMetadataBytes: 1024 * 1024,
|
||||
maxTableRows: 1_000,
|
||||
maxHeapItemBytes: 1024 * 1024,
|
||||
maxMethodBodyBytes: 1024 * 1024,
|
||||
maxMethodInstructions: 1_000,
|
||||
};
|
||||
|
||||
describe("managed member inspection", () => {
|
||||
it("inspects metadata members, signatures, CIL hashes, call edges, and field anchors", () => {
|
||||
const bytes = buildManagedPeFixture();
|
||||
const result = inspectManagedMembersBytes(
|
||||
bytes,
|
||||
target(bytes),
|
||||
memberLimits,
|
||||
);
|
||||
|
||||
expect(result.identity_scope).toEqual({
|
||||
token_identity: "build-local",
|
||||
requires_artifact_sha256: target(bytes).sha256,
|
||||
requires_mvid: "00112233-4455-6677-8899-aabbccddeeff",
|
||||
});
|
||||
expect(result.types.items).toEqual([
|
||||
expect.objectContaining({
|
||||
token: "0x02000001",
|
||||
full_name: "Fixture.Program",
|
||||
field_list: { first_row: 1, last_row: 1, count: 1 },
|
||||
method_list: { first_row: 1, last_row: 1, count: 1 },
|
||||
}),
|
||||
]);
|
||||
expect(result.fields.items).toEqual([
|
||||
expect.objectContaining({
|
||||
token: "0x04000001",
|
||||
declaring_type: "Fixture.Program",
|
||||
name: "counter",
|
||||
signature: expect.objectContaining({
|
||||
kind: "field",
|
||||
parse_status: "decoded",
|
||||
field_type: "i4",
|
||||
}),
|
||||
}),
|
||||
]);
|
||||
expect(result.methods.items).toEqual([
|
||||
expect.objectContaining({
|
||||
token: "0x06000001",
|
||||
declaring_type: "Fixture.Program",
|
||||
name: "Main",
|
||||
rva: 0x2800,
|
||||
signature: expect.objectContaining({
|
||||
kind: "method",
|
||||
parse_status: "decoded",
|
||||
return_type: "void",
|
||||
parameter_types: [],
|
||||
}),
|
||||
body: expect.objectContaining({
|
||||
status: "present",
|
||||
header_format: "tiny",
|
||||
file_offset: 0x0a00,
|
||||
il_size: 12,
|
||||
instruction_count: 4,
|
||||
decoded_instruction_count: 4,
|
||||
truncated_instructions: 0,
|
||||
opcode_counts: { "ldarg.0": 1, ldfld: 1, call: 1, ret: 1 },
|
||||
anchors: [
|
||||
{
|
||||
il_offset: 1,
|
||||
opcode: "ldfld",
|
||||
operand_kind: "field",
|
||||
operand: "0x04000001",
|
||||
},
|
||||
{
|
||||
il_offset: 6,
|
||||
opcode: "call",
|
||||
operand_kind: "method",
|
||||
operand: "0x0a000001",
|
||||
},
|
||||
],
|
||||
}),
|
||||
}),
|
||||
]);
|
||||
expect(result.member_refs.items).toEqual([
|
||||
expect.objectContaining({
|
||||
token: "0x0a000001",
|
||||
name: ".ctor",
|
||||
signature: expect.objectContaining({
|
||||
kind: "method",
|
||||
parse_status: "decoded",
|
||||
parameter_types: ["string"],
|
||||
}),
|
||||
}),
|
||||
]);
|
||||
expect(result.call_edges.items).toEqual([
|
||||
{
|
||||
caller_token: "0x06000001",
|
||||
caller: "Fixture.Program.Main",
|
||||
opcode: "call",
|
||||
target_token: "0x0a000001",
|
||||
target_kind: "member-ref",
|
||||
target_name: ".ctor",
|
||||
},
|
||||
]);
|
||||
expect(result.field_accesses.items).toEqual([
|
||||
{
|
||||
method_token: "0x06000001",
|
||||
method: "Fixture.Program.Main",
|
||||
opcode: "ldfld",
|
||||
field_token: "0x04000001",
|
||||
field_name: "counter",
|
||||
},
|
||||
]);
|
||||
expect(result.coverage).toMatchObject({ state: "complete", issues: [] });
|
||||
});
|
||||
});
|
||||
const target = (bytes: Buffer): BinaryTarget => ({
|
||||
path: "/fixture.exe",
|
||||
sha256: createHash("sha256").update(bytes).digest("hex"),
|
||||
kind: "executable",
|
||||
format: "pe",
|
||||
architecture: "x86",
|
||||
availableArchitectures: ["x86"],
|
||||
executableRole: "application",
|
||||
managed: true,
|
||||
});
|
||||
@@ -1,156 +0,0 @@
|
||||
import { createHash } from "node:crypto";
|
||||
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import type { BinaryTarget } from "../../../../src/domain/binaryTarget.js";
|
||||
import { inspectManagedMembersBytes } from "../../../../src/dotnet/ManagedMemberInspector.js";
|
||||
import { inspectManagedNativeBoundariesBytes } from "../../../../src/dotnet/ManagedNativeBoundaryInspector.js";
|
||||
import {
|
||||
buildManagedPeFixture,
|
||||
buildNativePeFixture,
|
||||
} from "../../../fixtures/managedPe.js";
|
||||
|
||||
const memberLimits = {
|
||||
typeOffset: 0,
|
||||
typeLimit: 100,
|
||||
methodOffset: 0,
|
||||
methodLimit: 100,
|
||||
fieldOffset: 0,
|
||||
fieldLimit: 100,
|
||||
memberRefOffset: 0,
|
||||
memberRefLimit: 100,
|
||||
edgeOffset: 0,
|
||||
edgeLimit: 100,
|
||||
instructionAnchorLimit: 100,
|
||||
maxMetadataBytes: 1024 * 1024,
|
||||
maxTableRows: 1_000,
|
||||
maxHeapItemBytes: 1024 * 1024,
|
||||
maxMethodBodyBytes: 1024 * 1024,
|
||||
maxMethodInstructions: 1_000,
|
||||
};
|
||||
|
||||
const nativeBoundaryLimits = {
|
||||
moduleRefOffset: 0,
|
||||
moduleRefLimit: 100,
|
||||
importOffset: 0,
|
||||
importLimit: 100,
|
||||
implementationOffset: 0,
|
||||
implementationLimit: 100,
|
||||
maxMetadataBytes: 1024 * 1024,
|
||||
maxTableRows: 1_000,
|
||||
maxHeapItemBytes: 1024 * 1024,
|
||||
};
|
||||
|
||||
describe("managed native boundaries", () => {
|
||||
it("inspects managed/native PInvoke declarations without verifying native exports", () => {
|
||||
const bytes = buildManagedPeFixture({
|
||||
pinvoke: {
|
||||
moduleName: "user32.dll",
|
||||
importName: "MessageBoxW",
|
||||
mappingFlags: 0x0345,
|
||||
},
|
||||
readyToRun: true,
|
||||
});
|
||||
const result = inspectManagedNativeBoundariesBytes(
|
||||
bytes,
|
||||
target(bytes),
|
||||
nativeBoundaryLimits,
|
||||
);
|
||||
|
||||
expect(result.cli_native).toMatchObject({
|
||||
il_only: true,
|
||||
ready_to_run_signature: true,
|
||||
managed_native_header_rva: 0x2700,
|
||||
managed_native_header_size: 4,
|
||||
});
|
||||
expect(result.module_refs.items).toEqual([
|
||||
{
|
||||
token: "0x1a000001",
|
||||
row_offset: expect.any(Number),
|
||||
name: "user32.dll",
|
||||
},
|
||||
]);
|
||||
expect(result.pinvoke_imports.items).toEqual([
|
||||
expect.objectContaining({
|
||||
token: "0x1c000001",
|
||||
member_token: "0x06000001",
|
||||
member_kind: "method",
|
||||
member_name: "Main",
|
||||
import_name: "MessageBoxW",
|
||||
import_scope_token: "0x1a000001",
|
||||
import_scope_name: "user32.dll",
|
||||
no_mangle: true,
|
||||
char_set: "unicode",
|
||||
call_convention: "stdcall",
|
||||
supports_last_error: true,
|
||||
verification: "managed-declaration-only",
|
||||
}),
|
||||
]);
|
||||
expect(result.native_implementations.items).toEqual([
|
||||
expect.objectContaining({
|
||||
token: "0x06000001",
|
||||
name: "Main",
|
||||
pinvoke_declared: true,
|
||||
boundary_kind: "pinvoke",
|
||||
body_interpretation: "native-or-runtime",
|
||||
}),
|
||||
]);
|
||||
expect(result.summary).toMatchObject({
|
||||
module_ref_count: 1,
|
||||
pinvoke_import_count: 1,
|
||||
native_implementation_count: 1,
|
||||
ready_to_run: true,
|
||||
mixed_mode_or_native_header: true,
|
||||
});
|
||||
expect(result.limitations).toContain(
|
||||
"P/Invoke rows prove managed import declarations only; this inspection does not verify that a native library, export, thunk, or provider-qualified function exists.",
|
||||
);
|
||||
});
|
||||
|
||||
it("keeps member evidence bounded and typed for pagination and unavailable metadata", () => {
|
||||
const bytes = buildManagedPeFixture();
|
||||
const paged = inspectManagedMembersBytes(bytes, target(bytes), {
|
||||
...memberLimits,
|
||||
methodLimit: 0 + 1,
|
||||
instructionAnchorLimit: 1,
|
||||
});
|
||||
expect(paged.methods).toMatchObject({
|
||||
total: 1,
|
||||
returned: 1,
|
||||
complete: true,
|
||||
});
|
||||
expect(paged.methods.items[0]?.body.anchors).toHaveLength(1);
|
||||
|
||||
const nativeBytes = buildNativePeFixture();
|
||||
const native = inspectManagedMembersBytes(
|
||||
nativeBytes,
|
||||
target(nativeBytes),
|
||||
memberLimits,
|
||||
);
|
||||
expect(native.metadata.status).toBe("absent");
|
||||
expect(native.coverage.state).toBe("unavailable");
|
||||
|
||||
const malformedBytes = buildManagedPeFixture({
|
||||
corruptMetadataSignature: true,
|
||||
});
|
||||
const malformed = inspectManagedMembersBytes(
|
||||
malformedBytes,
|
||||
target(malformedBytes),
|
||||
memberLimits,
|
||||
);
|
||||
expect(malformed.metadata.status).toBe("malformed");
|
||||
expect(malformed.coverage.issues).toEqual([
|
||||
expect.objectContaining({ code: "invalid-metadata-root" }),
|
||||
]);
|
||||
});
|
||||
});
|
||||
const target = (bytes: Buffer): BinaryTarget => ({
|
||||
path: "/fixture.exe",
|
||||
sha256: createHash("sha256").update(bytes).digest("hex"),
|
||||
kind: "executable",
|
||||
format: "pe",
|
||||
architecture: "x86",
|
||||
availableArchitectures: ["x86"],
|
||||
executableRole: "application",
|
||||
managed: true,
|
||||
});
|
||||
+3
-3
@@ -7,10 +7,10 @@ import { createTestTempDirectory } from "../../../fixtures/temporaryDirectory.js
|
||||
|
||||
import { runProviderAnalysis } from "../../../../src/application/DirectAnalysis.js";
|
||||
import { parseEvidence } from "../../../../src/domain/evidence.js";
|
||||
import { buildManagedPeFixture } from "../../../fixtures/managedPe.js";
|
||||
import { buildManagedPeFixture } from "../../../../src/dotnet/ManagedPe.fixture.js";
|
||||
|
||||
describe("managed CLI evidence", () => {
|
||||
it("has the same evidence shape through direct CLI analysis plumbing", async () => {
|
||||
describe("managed direct-analysis path boundary", () => {
|
||||
it("returns provider evidence through the shared analysis service", async () => {
|
||||
const directory = await createTestTempDirectory("rea-managed-cli-");
|
||||
const path = join(directory, "fixture.exe");
|
||||
await writeFile(path, buildManagedPeFixture());
|
||||
@@ -1,4 +1,3 @@
|
||||
import { createHash } from "node:crypto";
|
||||
import { writeFile } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
|
||||
@@ -7,292 +6,11 @@ import { describe, expect, it } from "vitest";
|
||||
import { createTestTempDirectory } from "../../../fixtures/temporaryDirectory.js";
|
||||
|
||||
import { compareManagedMemberPaths } from "../../../../src/application/ManagedMemberComparisonService.js";
|
||||
import { MANAGED_STATIC_PROVIDER } from "../../../../src/application/InvestigationProviders.js";
|
||||
import { managedMemberComparisonResultSchema } from "../../../../src/domain/managedMemberComparison.js";
|
||||
import {
|
||||
compareManagedMembers,
|
||||
managedMemberComparisonResultSchema,
|
||||
} from "../../../../src/domain/managedMemberComparison.js";
|
||||
import { createEvidence } from "../../../../src/domain/evidence.js";
|
||||
import type { BinaryTarget } from "../../../../src/domain/binaryTarget.js";
|
||||
import { inspectManagedMembersBytes } from "../../../../src/dotnet/ManagedMemberInspector.js";
|
||||
import { buildManagedPeFixture } from "../../../fixtures/managedPe.js";
|
||||
|
||||
const memberLimits = {
|
||||
typeOffset: 0,
|
||||
typeLimit: 100,
|
||||
methodOffset: 0,
|
||||
methodLimit: 100,
|
||||
fieldOffset: 0,
|
||||
fieldLimit: 100,
|
||||
memberRefOffset: 0,
|
||||
memberRefLimit: 100,
|
||||
edgeOffset: 0,
|
||||
edgeLimit: 100,
|
||||
instructionAnchorLimit: 100,
|
||||
maxMetadataBytes: 1024 * 1024,
|
||||
maxTableRows: 1_000,
|
||||
maxHeapItemBytes: 1024 * 1024,
|
||||
maxMethodBodyBytes: 1024 * 1024,
|
||||
maxMethodInstructions: 1_000,
|
||||
};
|
||||
|
||||
const pathMemberLimits = {
|
||||
...memberLimits,
|
||||
maxFileBytes: 1024 * 1024,
|
||||
};
|
||||
|
||||
const comparisonLimits = {
|
||||
max_method_matches: 100,
|
||||
max_field_matches: 100,
|
||||
max_candidates: 10,
|
||||
};
|
||||
|
||||
describe("managed member comparison", () => {
|
||||
it("remaps renamed methods by exact CIL/signature without using names", () => {
|
||||
const leftBytes = buildManagedPeFixture();
|
||||
const rightBytes = buildManagedPeFixture({
|
||||
mvid: Buffer.from("00112233445566778899aabbccddeefe", "hex"),
|
||||
typeName: "A",
|
||||
typeNamespace: "X",
|
||||
methodName: "b",
|
||||
fieldName: "c",
|
||||
});
|
||||
const left = inspect(leftBytes, "/tmp/left.dll");
|
||||
const right = inspect(rightBytes, "/tmp/right.dll");
|
||||
const result = compareManagedMembers(
|
||||
{ evidenceId: left.evidence.evidence_id, result: left.result },
|
||||
{ evidenceId: right.evidence.evidence_id, result: right.result },
|
||||
comparisonLimits,
|
||||
);
|
||||
|
||||
expect(result.algorithm.name_matching).toBe("not-used");
|
||||
expect(result.left.mvid).toBe("00112233-4455-6677-8899-aabbccddeeff");
|
||||
expect(result.right.mvid).toBe("33221100-5544-7766-8899-aabbccddeefe");
|
||||
expect(result.matching.exact_il_signature).toBe(1);
|
||||
expect(result.methods).toEqual([
|
||||
expect.objectContaining({
|
||||
status: "unchanged",
|
||||
left: expect.objectContaining({ token: "0x06000001", name: "Main" }),
|
||||
right: expect.objectContaining({ token: "0x06000001", name: "b" }),
|
||||
match: expect.objectContaining({
|
||||
status: "matched",
|
||||
basis: "exact-il-signature",
|
||||
confidence: "exact",
|
||||
}),
|
||||
dimensions: [],
|
||||
}),
|
||||
]);
|
||||
expect(result.limitations).toContain(
|
||||
"Names are reported as observations but are not used as a matching basis.",
|
||||
);
|
||||
});
|
||||
|
||||
it("uses structural method shape when build-local token operands drift", () => {
|
||||
const leftBytes = buildManagedPeFixture();
|
||||
const rightBytes = buildManagedPeFixture({
|
||||
ilBody: Buffer.from([
|
||||
0x32, 0x02, 0x7b, 0x02, 0x00, 0x00, 0x04, 0x28, 0x02, 0x00, 0x00, 0x0a,
|
||||
0x2a,
|
||||
]),
|
||||
});
|
||||
const left = inspect(leftBytes, "/tmp/left.dll");
|
||||
const right = inspect(rightBytes, "/tmp/right.dll");
|
||||
const result = compareManagedMembers(
|
||||
{ evidenceId: left.evidence.evidence_id, result: left.result },
|
||||
{ evidenceId: right.evidence.evidence_id, result: right.result },
|
||||
comparisonLimits,
|
||||
);
|
||||
|
||||
expect(result.matching.exact_il_signature).toBe(0);
|
||||
expect(result.matching.structural_method_shape).toBe(1);
|
||||
expect(result.methods[0]).toMatchObject({
|
||||
status: "changed",
|
||||
match: { status: "matched", basis: "structural-method-shape" },
|
||||
dimensions: ["cil"],
|
||||
});
|
||||
});
|
||||
|
||||
it("rejects member states that disagree with their observed sides or match", () => {
|
||||
const left = inspect(buildManagedPeFixture(), "/tmp/left.dll");
|
||||
const right = inspect(buildManagedPeFixture(), "/tmp/right.dll");
|
||||
const result = compareManagedMembers(
|
||||
{ evidenceId: left.evidence.evidence_id, result: left.result },
|
||||
{ evidenceId: right.evidence.evidence_id, result: right.result },
|
||||
comparisonLimits,
|
||||
);
|
||||
const method = result.methods[0];
|
||||
expect(method).toBeDefined();
|
||||
if (method === undefined) return;
|
||||
|
||||
expect(
|
||||
managedMemberComparisonResultSchema.safeParse({
|
||||
...result,
|
||||
methods: [{ ...method, status: "unchanged", right: null }],
|
||||
}).success,
|
||||
).toBe(false);
|
||||
expect(
|
||||
managedMemberComparisonResultSchema.safeParse({
|
||||
...result,
|
||||
methods: [
|
||||
{
|
||||
...method,
|
||||
match: {
|
||||
...method.match,
|
||||
basis: "none",
|
||||
confidence: "unknown",
|
||||
},
|
||||
},
|
||||
],
|
||||
}).success,
|
||||
).toBe(false);
|
||||
});
|
||||
|
||||
it("does not match identical instruction-limited method prefixes", () => {
|
||||
const limited = { ...memberLimits, maxMethodInstructions: 1 };
|
||||
const left = inspect(
|
||||
buildManagedPeFixture(),
|
||||
"/tmp/left-partial.dll",
|
||||
limited,
|
||||
);
|
||||
const right = inspect(
|
||||
buildManagedPeFixture(),
|
||||
"/tmp/right-partial.dll",
|
||||
limited,
|
||||
);
|
||||
const result = compareManagedMembers(
|
||||
{ evidenceId: left.evidence.evidence_id, result: left.result },
|
||||
{ evidenceId: right.evidence.evidence_id, result: right.result },
|
||||
comparisonLimits,
|
||||
);
|
||||
|
||||
expect(result.matching.exact_il_signature).toBe(0);
|
||||
expect(result.matching.structural_method_shape).toBe(0);
|
||||
expect(result.coverage).toMatchObject({
|
||||
status: "partial",
|
||||
left_status: "partial",
|
||||
right_status: "partial",
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("managed member comparison uncertainty", () => {
|
||||
it("keeps unmatched members unknown when the opposite page is incomplete", () => {
|
||||
const left = inspect(buildManagedPeFixture(), "/tmp/left-paged.dll");
|
||||
const right = inspect(buildManagedPeFixture(), "/tmp/right-paged.dll");
|
||||
const leftPartial = {
|
||||
...left.result,
|
||||
fields: {
|
||||
...left.result.fields,
|
||||
items: [],
|
||||
returned: 0,
|
||||
dropped: 1,
|
||||
complete: false,
|
||||
},
|
||||
};
|
||||
const rightPartial = {
|
||||
...right.result,
|
||||
methods: {
|
||||
...right.result.methods,
|
||||
items: [],
|
||||
returned: 0,
|
||||
dropped: 1,
|
||||
complete: false,
|
||||
},
|
||||
};
|
||||
const result = compareManagedMembers(
|
||||
{ evidenceId: left.evidence.evidence_id, result: leftPartial },
|
||||
{ evidenceId: right.evidence.evidence_id, result: rightPartial },
|
||||
comparisonLimits,
|
||||
);
|
||||
|
||||
expect(result.summary).toMatchObject({
|
||||
added: 0,
|
||||
removed: 0,
|
||||
unknown: 2,
|
||||
});
|
||||
expect(result.methods[0]).toMatchObject({
|
||||
status: "unknown",
|
||||
left: { token: "0x06000001" },
|
||||
right: null,
|
||||
limitations: [expect.stringContaining("unknown-within-unobserved-page")],
|
||||
});
|
||||
expect(result.fields[0]).toMatchObject({
|
||||
status: "unknown",
|
||||
left: null,
|
||||
right: { token: "0x04000001" },
|
||||
limitations: [expect.stringContaining("unknown-within-unobserved-page")],
|
||||
});
|
||||
expect(result.coverage).toEqual({
|
||||
status: "truncated",
|
||||
left_status: "partial",
|
||||
right_status: "partial",
|
||||
omitted_methods: 1,
|
||||
omitted_fields: 1,
|
||||
omitted_candidates: 0,
|
||||
});
|
||||
});
|
||||
|
||||
it("does not guess ambiguous field signature matches", () => {
|
||||
const left = inspect(buildManagedPeFixture(), "/tmp/left.dll");
|
||||
const leftField = left.result.fields.items[0];
|
||||
expect(leftField).toBeDefined();
|
||||
if (leftField === undefined) return;
|
||||
const duplicatedLeft = {
|
||||
...left.result,
|
||||
fields: {
|
||||
...left.result.fields,
|
||||
items: [
|
||||
leftField,
|
||||
{
|
||||
...leftField,
|
||||
token: "0x04000002",
|
||||
name: "other",
|
||||
},
|
||||
],
|
||||
total: 2,
|
||||
returned: 2,
|
||||
},
|
||||
};
|
||||
const right = inspect(buildManagedPeFixture(), "/tmp/right.dll");
|
||||
const rightField = right.result.fields.items[0];
|
||||
expect(rightField).toBeDefined();
|
||||
if (rightField === undefined) return;
|
||||
const duplicatedRight = {
|
||||
...right.result,
|
||||
fields: {
|
||||
...right.result.fields,
|
||||
items: [
|
||||
rightField,
|
||||
{
|
||||
...rightField,
|
||||
token: "0x04000002",
|
||||
name: "renamed",
|
||||
},
|
||||
],
|
||||
total: 2,
|
||||
returned: 2,
|
||||
},
|
||||
};
|
||||
const result = compareManagedMembers(
|
||||
{ evidenceId: left.evidence.evidence_id, result: duplicatedLeft },
|
||||
{ evidenceId: right.evidence.evidence_id, result: duplicatedRight },
|
||||
comparisonLimits,
|
||||
);
|
||||
|
||||
expect(result.matching.ambiguous).toBe(1);
|
||||
expect(result.fields).toEqual([
|
||||
expect.objectContaining({
|
||||
status: "unknown",
|
||||
match: expect.objectContaining({
|
||||
status: "ambiguous",
|
||||
basis: "field-signature",
|
||||
candidate_left_tokens: ["0x04000001", "0x04000002"],
|
||||
candidate_right_tokens: ["0x04000001", "0x04000002"],
|
||||
}),
|
||||
}),
|
||||
]);
|
||||
});
|
||||
});
|
||||
buildManagedPeFixture,
|
||||
MANAGED_MEMBER_PATH_FIXTURE_LIMITS,
|
||||
} from "../../../../src/dotnet/ManagedPe.fixture.js";
|
||||
|
||||
describe("managed member comparison path workflow", () => {
|
||||
it("compares two local paths and returns derived Evidence", async () => {
|
||||
@@ -308,49 +26,24 @@ describe("managed member comparison path workflow", () => {
|
||||
const result = await compareManagedMemberPaths({
|
||||
leftPath,
|
||||
rightPath,
|
||||
memberLimits: pathMemberLimits,
|
||||
comparisonLimits,
|
||||
memberLimits: MANAGED_MEMBER_PATH_FIXTURE_LIMITS,
|
||||
comparisonLimits: {
|
||||
max_method_matches: 100,
|
||||
max_field_matches: 100,
|
||||
max_candidates: 10,
|
||||
},
|
||||
});
|
||||
|
||||
expect(result.ok).toBe(true);
|
||||
if (!result.ok) return;
|
||||
expect(result.value.operation).toBe("compare_managed_members");
|
||||
expect(result.value.confidence).toBe("inferred");
|
||||
expect(result.value.authority).toBe("analyst-inference");
|
||||
expect(result.value).toMatchObject({
|
||||
operation: "compare_managed_members",
|
||||
confidence: "inferred",
|
||||
authority: "analyst-inference",
|
||||
});
|
||||
expect(
|
||||
managedMemberComparisonResultSchema.parse(result.value.normalized_result)
|
||||
.matching.exact_il_signature,
|
||||
).toBe(1);
|
||||
});
|
||||
});
|
||||
|
||||
const inspect = (
|
||||
bytes: Buffer,
|
||||
path: string,
|
||||
limits: typeof memberLimits = memberLimits,
|
||||
) => {
|
||||
const target: BinaryTarget = {
|
||||
path,
|
||||
sha256: hash(bytes),
|
||||
kind: "executable",
|
||||
format: "pe",
|
||||
architecture: "x86",
|
||||
availableArchitectures: ["x86"],
|
||||
executableRole: "application",
|
||||
managed: true,
|
||||
};
|
||||
const result = inspectManagedMembersBytes(bytes, target, limits);
|
||||
return {
|
||||
result,
|
||||
evidence: createEvidence(target, MANAGED_STATIC_PROVIDER, {
|
||||
operation: "inspect_managed_members",
|
||||
parameters: {},
|
||||
result,
|
||||
rawResult: null,
|
||||
limitations: result.limitations,
|
||||
}),
|
||||
};
|
||||
};
|
||||
|
||||
const hash = (bytes: Buffer): string =>
|
||||
createHash("sha256").update(bytes).digest("hex");
|
||||
|
||||
@@ -1,177 +0,0 @@
|
||||
import { createHash } from "node:crypto";
|
||||
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { importManagedReconstructionEvidence } from "../../../../src/application/ManagedReconstructionService.js";
|
||||
import { MANAGED_RECONSTRUCTION_IMPORT_EXAMPLE } from "../../../../src/contracts/managedWorkflowExamples.js";
|
||||
import {
|
||||
importManagedReconstruction,
|
||||
managedReconstructionImportInputSchema,
|
||||
} from "../../../../src/domain/managedReconstruction.js";
|
||||
import { managedMemberInspectionSchema } from "../../../../src/domain/managedArtifact.js";
|
||||
import { createEvidence } from "../../../../src/domain/evidence.js";
|
||||
|
||||
const exampleInput = () =>
|
||||
managedReconstructionImportInputSchema.parse(
|
||||
MANAGED_RECONSTRUCTION_IMPORT_EXAMPLE,
|
||||
);
|
||||
|
||||
const exampleMethod = () => {
|
||||
const method = exampleInput().methods[0];
|
||||
if (method === undefined) throw new Error("missing example method");
|
||||
return method;
|
||||
};
|
||||
|
||||
describe("managed decompiler reconstruction import", () => {
|
||||
it("imports decompiler output as inference locked to static IL evidence", () => {
|
||||
const result = importManagedReconstruction(exampleInput());
|
||||
|
||||
expect(result).toMatchObject({
|
||||
schema_version: 1,
|
||||
phase: "reconstruction-import",
|
||||
executed: false,
|
||||
static_observation: {
|
||||
artifact_sha256: "2".repeat(64),
|
||||
mvid: "11112222-3333-4444-9555-666677778888",
|
||||
},
|
||||
decompiler: {
|
||||
name: "ilspycmd",
|
||||
version: "9.1.0.7988",
|
||||
family: "ilspy",
|
||||
},
|
||||
summary: {
|
||||
imported_methods: 1,
|
||||
decompiled_csharp_methods: 1,
|
||||
},
|
||||
methods: [
|
||||
{
|
||||
token: "0x06000001",
|
||||
signature_sha256: "3".repeat(64),
|
||||
normalized_il_sha256: "5".repeat(64),
|
||||
validation: {
|
||||
matched_static_member: true,
|
||||
exact_build_required: true,
|
||||
canonical_observation: false,
|
||||
confidence_floor: "inference",
|
||||
},
|
||||
},
|
||||
],
|
||||
});
|
||||
expect(result.reconstruction_id).toMatch(/^mre_[a-f0-9]{64}$/u);
|
||||
expect(result.methods[0]?.reconstruction.text_sha256).toBe(
|
||||
createHash("sha256")
|
||||
.update("internal static void Main() { }")
|
||||
.digest("hex"),
|
||||
);
|
||||
expect(result.limitations.join(" ")).toContain(
|
||||
"metadata and IL observations remain canonical",
|
||||
);
|
||||
});
|
||||
|
||||
it("rejects reconstruction text hash drift", () => {
|
||||
expect(() =>
|
||||
importManagedReconstruction({
|
||||
...exampleInput(),
|
||||
methods: [
|
||||
{
|
||||
...exampleMethod(),
|
||||
reconstruction: {
|
||||
...exampleMethod().reconstruction,
|
||||
text_sha256: "0".repeat(64),
|
||||
},
|
||||
},
|
||||
],
|
||||
}),
|
||||
).toThrow(/text hash mismatch/u);
|
||||
});
|
||||
|
||||
it("rejects stale method locks", () => {
|
||||
expect(() =>
|
||||
importManagedReconstruction({
|
||||
...exampleInput(),
|
||||
methods: [
|
||||
{
|
||||
...exampleMethod(),
|
||||
normalized_il_sha256: "6".repeat(64),
|
||||
},
|
||||
],
|
||||
}),
|
||||
).toThrow(/does not match/u);
|
||||
});
|
||||
|
||||
it("rejects reconstruction locks for partial CIL without normalized identity", () => {
|
||||
const input = exampleInput();
|
||||
const observed = managedMemberInspectionSchema.parse(
|
||||
input.static_members.normalized_result,
|
||||
);
|
||||
const method = observed.methods.items[0];
|
||||
if (method === undefined) throw new Error("missing observed method");
|
||||
const partial = {
|
||||
...observed,
|
||||
methods: {
|
||||
...observed.methods,
|
||||
items: [
|
||||
{
|
||||
...method,
|
||||
body: {
|
||||
...method.body,
|
||||
status: "partial" as const,
|
||||
normalized_il_sha256: null,
|
||||
truncated_instructions: 1,
|
||||
issue: "instruction limit reached",
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
coverage: { state: "partial" as const, issues: [] },
|
||||
};
|
||||
const partialEvidence = createEvidence(
|
||||
undefined,
|
||||
input.static_members.provider,
|
||||
{
|
||||
operation: "inspect_managed_members",
|
||||
parameters: {},
|
||||
result: partial,
|
||||
rawResult: null,
|
||||
limitations: partial.limitations,
|
||||
},
|
||||
);
|
||||
|
||||
expect(() =>
|
||||
importManagedReconstruction({
|
||||
...input,
|
||||
static_members: partialEvidence,
|
||||
methods: [
|
||||
{
|
||||
...exampleMethod(),
|
||||
normalized_il_sha256: null,
|
||||
},
|
||||
],
|
||||
}),
|
||||
).toThrow(/does not match/u);
|
||||
});
|
||||
|
||||
it("wraps imported reconstruction in Evidence v2", () => {
|
||||
const evidence = importManagedReconstructionEvidence(exampleInput());
|
||||
|
||||
expect(evidence.ok).toBe(true);
|
||||
if (!evidence.ok) return;
|
||||
expect(evidence.value).toMatchObject({
|
||||
operation: "import_managed_reconstruction",
|
||||
provider: { id: "rea-dotnet-workflows" },
|
||||
confidence: "inferred",
|
||||
authority: "analyst-inference",
|
||||
normalized_result: {
|
||||
phase: "reconstruction-import",
|
||||
methods: [
|
||||
{
|
||||
reconstruction: {
|
||||
kind: "decompiled-csharp",
|
||||
language: "csharp",
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
});
|
||||
});
|
||||
});
|
||||
+2
-77
@@ -1,4 +1,3 @@
|
||||
import { createHash } from "node:crypto";
|
||||
import { writeFile } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
|
||||
@@ -7,79 +6,16 @@ import { describe, expect, it } from "vitest";
|
||||
import { createTestTempDirectory } from "../../../fixtures/temporaryDirectory.js";
|
||||
|
||||
import type { AnalysisExecution } from "../../../../src/application/AnalysisProvider.js";
|
||||
import type { BinaryTarget } from "../../../../src/domain/binaryTarget.js";
|
||||
import { parseBinaryTarget } from "../../../../src/domain/binaryTarget.js";
|
||||
import {
|
||||
managedArtifactInspectionSchema,
|
||||
managedMemberInspectionSchema,
|
||||
managedNativeBoundaryInspectionSchema,
|
||||
} from "../../../../src/domain/managedArtifact.js";
|
||||
import { inspectManagedArtifactBytes } from "../../../../src/dotnet/ManagedArtifactInspector.js";
|
||||
import { ManagedStaticProvider } from "../../../../src/dotnet/ManagedStaticProvider.js";
|
||||
import {
|
||||
buildManagedPeFixture,
|
||||
buildNativePeFixture,
|
||||
} from "../../../fixtures/managedPe.js";
|
||||
|
||||
const limits = {
|
||||
referenceOffset: 0,
|
||||
referenceLimit: 100,
|
||||
resourceOffset: 0,
|
||||
resourceLimit: 100,
|
||||
attributeOffset: 0,
|
||||
attributeLimit: 100,
|
||||
maxMetadataBytes: 1024 * 1024,
|
||||
maxTableRows: 1_000,
|
||||
maxHeapItemBytes: 1024 * 1024,
|
||||
};
|
||||
|
||||
describe("managed artifact failures", () => {
|
||||
it("reports native PE, malformed metadata, and bounded metadata failures as typed results", () => {
|
||||
const native = inspectManagedArtifactBytes(
|
||||
buildNativePeFixture(),
|
||||
target(buildNativePeFixture()),
|
||||
limits,
|
||||
);
|
||||
expect(native.classification.status).toBe("not-managed");
|
||||
expect(native.metadata.status).toBe("absent");
|
||||
expect(native.coverage.state).toBe("unavailable");
|
||||
|
||||
const malformedBytes = buildManagedPeFixture({
|
||||
corruptMetadataSignature: true,
|
||||
});
|
||||
const malformed = inspectManagedArtifactBytes(
|
||||
malformedBytes,
|
||||
target(malformedBytes),
|
||||
limits,
|
||||
);
|
||||
expect(malformed.classification.status).toBe("malformed");
|
||||
expect(malformed.coverage.issues).toEqual([
|
||||
expect.objectContaining({ code: "invalid-metadata-root" }),
|
||||
]);
|
||||
|
||||
const unsupportedTableBytes = buildManagedPeFixture({
|
||||
metadataValidMaskExtra: 1n << 50n,
|
||||
});
|
||||
const unsupportedTable = inspectManagedArtifactBytes(
|
||||
unsupportedTableBytes,
|
||||
target(unsupportedTableBytes),
|
||||
limits,
|
||||
);
|
||||
expect(unsupportedTable.coverage.issues).toEqual([
|
||||
expect.objectContaining({ code: "invalid-tables" }),
|
||||
]);
|
||||
|
||||
const limited = inspectManagedArtifactBytes(
|
||||
buildManagedPeFixture(),
|
||||
target(buildManagedPeFixture()),
|
||||
{ ...limits, maxMetadataBytes: 256 },
|
||||
);
|
||||
expect(limited.metadata.status).toBe("partial");
|
||||
expect(limited.coverage.issues).toEqual([
|
||||
expect.objectContaining({ code: "limit-exceeded" }),
|
||||
]);
|
||||
});
|
||||
import { buildManagedPeFixture } from "../../../../src/dotnet/ManagedPe.fixture.js";
|
||||
|
||||
describe("managed static provider path boundary", () => {
|
||||
it("executes as a read-only auxiliary provider with digest, cancellation, and format boundaries", async () => {
|
||||
const directory = await createTestTempDirectory("rea-managed-provider-");
|
||||
const bytes = buildManagedPeFixture();
|
||||
@@ -161,17 +97,6 @@ describe("managed artifact failures", () => {
|
||||
});
|
||||
});
|
||||
});
|
||||
const target = (bytes: Buffer): BinaryTarget => ({
|
||||
path: "/fixture.exe",
|
||||
sha256: createHash("sha256").update(bytes).digest("hex"),
|
||||
kind: "executable",
|
||||
format: "pe",
|
||||
architecture: "x86",
|
||||
availableArchitectures: ["x86"],
|
||||
executableRole: "application",
|
||||
managed: true,
|
||||
});
|
||||
|
||||
const asManagedResult = (execution: AnalysisExecution) =>
|
||||
managedArtifactInspectionSchema.parse(execution.result);
|
||||
|
||||
@@ -1,214 +0,0 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import {
|
||||
countExecutableSections,
|
||||
getExportedFunctions,
|
||||
getImportedDlls,
|
||||
hasPdbInfo,
|
||||
parseDebugType,
|
||||
parseImageCharacteristics,
|
||||
parseMachineType,
|
||||
parseSectionCharacteristics,
|
||||
peManifestSchema,
|
||||
type PeManifest,
|
||||
type PeSection,
|
||||
type PeImport,
|
||||
type PeExport,
|
||||
type PeResource,
|
||||
type PeDebugEntry,
|
||||
type PeTlsCallback,
|
||||
} from "../../../../src/domain/peInspection.js";
|
||||
|
||||
const validManifest: PeManifest = {
|
||||
dos_magic: 0x5a4d,
|
||||
pe_signature: 0x00004550,
|
||||
machine: "x64",
|
||||
number_of_sections: 3,
|
||||
timestamp: 0x12345678,
|
||||
is_dll: false,
|
||||
is_executable: true,
|
||||
is_console: true,
|
||||
entry_point: 0x1000,
|
||||
image_base: 0x400000,
|
||||
section_alignment: 0x1000,
|
||||
subsystem_version: "10.0",
|
||||
sections: [
|
||||
{
|
||||
name: ".text",
|
||||
virtual_size: 0x1000,
|
||||
virtual_address: 0x1000,
|
||||
raw_size: 0x1000,
|
||||
raw_offset: 0x400,
|
||||
characteristics: {
|
||||
has_code: true,
|
||||
has_initialized_data: false,
|
||||
has_uninitialized_data: false,
|
||||
readable: true,
|
||||
writable: false,
|
||||
executable: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: ".data",
|
||||
virtual_size: 0x500,
|
||||
virtual_address: 0x2000,
|
||||
raw_size: 0x500,
|
||||
raw_offset: 0x1400,
|
||||
characteristics: {
|
||||
has_code: false,
|
||||
has_initialized_data: true,
|
||||
has_uninitialized_data: false,
|
||||
readable: true,
|
||||
writable: true,
|
||||
executable: false,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: ".rdata",
|
||||
virtual_size: 0x200,
|
||||
virtual_address: 0x3000,
|
||||
raw_size: 0x200,
|
||||
raw_offset: 0x1900,
|
||||
characteristics: {
|
||||
has_code: false,
|
||||
has_initialized_data: true,
|
||||
has_uninitialized_data: false,
|
||||
readable: true,
|
||||
writable: false,
|
||||
executable: false,
|
||||
},
|
||||
},
|
||||
],
|
||||
imports: [
|
||||
{
|
||||
dll: "KERNEL32.DLL",
|
||||
function_name: "CreateFileW",
|
||||
ordinal: null,
|
||||
is_delay_import: false,
|
||||
},
|
||||
{
|
||||
dll: "KERNEL32.DLL",
|
||||
function_name: "WriteFile",
|
||||
ordinal: null,
|
||||
is_delay_import: false,
|
||||
},
|
||||
{
|
||||
dll: "USER32.DLL",
|
||||
function_name: "MessageBoxW",
|
||||
ordinal: 100,
|
||||
is_delay_import: false,
|
||||
},
|
||||
],
|
||||
exports: [
|
||||
{ name: "DllMain", ordinal: 1, rva: 0x1000, forwarder: null },
|
||||
{ name: "MyFunction", ordinal: 2, rva: 0x1050, forwarder: null },
|
||||
],
|
||||
resources: [
|
||||
{ type: "RT_VERSION", name: "1", language: 0x0409, rva: 0x4000, size: 100 },
|
||||
],
|
||||
debug_entries: [
|
||||
{
|
||||
type: "CODEVIEW",
|
||||
rva: 0x5000,
|
||||
size: 100,
|
||||
pdb_guid: "AABBCCDD-EEFF-GGHH-IIJJ-KKLLMMNNOOPP",
|
||||
pdb_age: 1,
|
||||
pdb_path: "C:\\src\\app.pdb",
|
||||
},
|
||||
],
|
||||
tls_callbacks: [{ callback_rva: 0x6000, index: 0 }],
|
||||
relocations_count: 50,
|
||||
image_characteristics: ["Executable", "Console"],
|
||||
};
|
||||
|
||||
describe("PE/COFF static inspection", () => {
|
||||
it("parses machine types", () => {
|
||||
expect(parseMachineType(0x014c)).toBe("x86");
|
||||
expect(parseMachineType(0x8664)).toBe("x64");
|
||||
expect(parseMachineType(0x01c0)).toBe("arm");
|
||||
expect(parseMachineType(0xaa64)).toBe("arm64");
|
||||
expect(parseMachineType(0x0200)).toBe("ia64");
|
||||
expect(parseMachineType(0xffff)).toBe("unknown");
|
||||
});
|
||||
|
||||
it("parses section characteristics", () => {
|
||||
const CODE = 0x00000020;
|
||||
const READ = 0x40000000;
|
||||
const EXECUTE = 0x20000000;
|
||||
const result = parseSectionCharacteristics(CODE | READ | EXECUTE);
|
||||
expect(result.has_code).toBe(true);
|
||||
expect(result.readable).toBe(true);
|
||||
expect(result.executable).toBe(true);
|
||||
expect(result.writable).toBe(false);
|
||||
});
|
||||
|
||||
it("parses image characteristics", () => {
|
||||
const DLL = 0x2000;
|
||||
const EXEC = 0x0002;
|
||||
const flags = parseImageCharacteristics(DLL | EXEC);
|
||||
expect(flags).toContain("DLL");
|
||||
expect(flags).toContain("Executable");
|
||||
});
|
||||
|
||||
it("parses debug types", () => {
|
||||
expect(parseDebugType(2)).toBe("CODEVIEW");
|
||||
expect(parseDebugType(1)).toBe("COFF");
|
||||
expect(parseDebugType(99)).toBe("UNKNOWN");
|
||||
});
|
||||
|
||||
it("validates a well-formed manifest", () => {
|
||||
const result = peManifestSchema.safeParse(validManifest);
|
||||
expect(result.success).toBe(true);
|
||||
});
|
||||
|
||||
it("detects PDB info", () => {
|
||||
expect(hasPdbInfo(validManifest)).toBe(true);
|
||||
const withoutPdb: PeManifest = {
|
||||
...validManifest,
|
||||
debug_entries: [
|
||||
{
|
||||
type: "COFF",
|
||||
rva: 0x5000,
|
||||
size: 100,
|
||||
pdb_guid: null,
|
||||
pdb_age: null,
|
||||
pdb_path: null,
|
||||
},
|
||||
],
|
||||
};
|
||||
expect(hasPdbInfo(withoutPdb)).toBe(false);
|
||||
});
|
||||
|
||||
it("gets imported DLLs", () => {
|
||||
const dlls = getImportedDlls(validManifest);
|
||||
expect(dlls).toHaveLength(2);
|
||||
expect(dlls).toContain("KERNEL32.DLL");
|
||||
expect(dlls).toContain("USER32.DLL");
|
||||
});
|
||||
|
||||
it("gets exported functions", () => {
|
||||
const exports = getExportedFunctions(validManifest);
|
||||
expect(exports).toHaveLength(2);
|
||||
expect(exports).toContain("DllMain");
|
||||
expect(exports).toContain("MyFunction");
|
||||
});
|
||||
|
||||
it("counts executable sections", () => {
|
||||
expect(countExecutableSections(validManifest)).toBe(1);
|
||||
});
|
||||
|
||||
it("uses all exported types", () => {
|
||||
const section: PeSection = validManifest.sections[0]!;
|
||||
expect(section.name).toBe(".text");
|
||||
const imp: PeImport = validManifest.imports[0]!;
|
||||
expect(imp.dll).toBe("KERNEL32.DLL");
|
||||
const exp: PeExport = validManifest.exports[0]!;
|
||||
expect(exp.name).toBe("DllMain");
|
||||
const res: PeResource = validManifest.resources[0]!;
|
||||
expect(res.type).toBe("RT_VERSION");
|
||||
const dbg: PeDebugEntry = validManifest.debug_entries[0]!;
|
||||
expect(dbg.type).toBe("CODEVIEW");
|
||||
const tls: PeTlsCallback = validManifest.tls_callbacks[0]!;
|
||||
expect(tls.index).toBe(0);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user