Skip to content

armgit / rea

Public Imported from GitHub
0

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

9.4 KB
Raw Blame
import { describe, expect, it } from "vitest";

import { dosMz, pe } from "./binaryTarget.fixture.js";
import { parseExecutableHeader } from "./binaryTarget.js";
import {
  observeMzWindowsHeader,
  parseDosMzHeader,
  parseMzHeaderEvidence,
} from "./dosMz.js";

describe("DOS MZ load-module validation", () => {
  it("separates initialized module and appended overlay bytes", () => {
    const image = dosMz(480);
    const bytes = Buffer.concat([image, Buffer.from("overlay")]);
    expect(parseDosMzHeader(bytes)).toEqual({
      ok: true,
      value: {
        headerBytes: 32,
        imageBytes: 512,
        moduleBytes: 480,
        overlayBytes: 7,
        relocationCount: 0,
        relocationTableOffset: 28,
        entrySegment: 0,
        entryOffset: 0,
      },
    });
  });

  it("accepts relocation records overlapping the Windows new-header field", () => {
    const bytes = Buffer.alloc(224);
    dosMz().copy(bytes, 0, 0, 28);
    bytes.writeUInt16LE(bytes.length, 2);
    bytes.writeUInt16LE(6, 8);
    bytes.writeUInt16LE(2, 6);
    bytes.writeUInt16LE(62, 24);
    bytes.writeUInt16LE(1, 62);
    bytes.writeUInt16LE(8, 66);
    expect(bytes.readUInt32LE(60)).toBe(65536);
    expect(observeMzWindowsHeader(bytes)).toEqual({ state: "absent" });
    expect(parseExecutableHeader(bytes, "x64")).toMatchObject({
      ok: true,
      value: { format: "dos-mz" },
    });
    expect(parseDosMzHeader(bytes)).toMatchObject({
      ok: true,
      value: { headerBytes: 96, relocationCount: 2, moduleBytes: 128 },
    });
  });

  it("uses the actual file length rather than the bounded prefix length", () => {
    const bytes = dosMz(8192);
    expect(parseDosMzHeader(bytes.subarray(0, 64), bytes.length).ok).toBe(true);
    expect(parseDosMzHeader(bytes.subarray(0, 64))).toMatchObject({
      ok: false,
      error: "truncated DOS MZ load module",
    });
  });

  it("keeps a partial DOS prefix unknown until the Windows pointer is examined", () => {
    const bytes = dosMz(64);

    expect(observeMzWindowsHeader(bytes.subarray(0, 32), bytes.length)).toEqual(
      {
        state: "header-needed",
      },
    );
    expect(
      parseExecutableHeader(bytes.subarray(0, 32), "x64", bytes.length),
    ).toMatchObject({ ok: false });
    expect(observeMzWindowsHeader(bytes.subarray(0, 32))).toEqual({
      state: "absent",
    });
  });

  it("parses a tiny known-length DOS module without requiring an impossible pointer", () => {
    const bytes = Buffer.alloc(33);
    bytes.write("MZ", 0, "ascii");
    bytes.writeUInt16LE(bytes.length, 2);
    bytes.writeUInt16LE(1, 4);
    bytes.writeUInt16LE(2, 8);
    bytes.writeUInt16LE(28, 24);
    bytes[32] = 0xc3;

    expect(observeMzWindowsHeader(bytes.subarray(0, 28), bytes.length)).toEqual(
      {
        state: "absent",
      },
    );
    expect(
      parseExecutableHeader(bytes.subarray(0, 28), "x64", bytes.length),
    ).toMatchObject({
      ok: true,
      value: { format: "dos-mz", architecture: "x86" },
    });
  });

  it("preserves PE classification when low relocation records do not overlap e_lfanew", () => {
    const bytes = pe(0x8664);
    bytes.writeUInt16LE(4, 8);
    bytes.writeUInt16LE(1, 6);
    bytes.writeUInt16LE(28, 24);
    expect(observeMzWindowsHeader(bytes)).toEqual({
      state: "declared",
      offset: 64,
      signature: "PE",
    });
    expect(parseExecutableHeader(bytes, "x64")).toMatchObject({
      ok: true,
      value: { format: "pe", architecture: "x86_64" },
    });
    expect(parseDosMzHeader(bytes).ok).toBe(false);
  });

  it.each([
    [
      "page count",
      (bytes: Buffer) => bytes.writeUInt16LE(0, 4),
      "invalid DOS MZ page count or final-page size",
    ],
    [
      "final-page size",
      (bytes: Buffer) => bytes.writeUInt16LE(512, 2),
      "invalid DOS MZ page count or final-page size",
    ],
    [
      "header size",
      (bytes: Buffer) => bytes.writeUInt16LE(1, 8),
      "invalid DOS MZ header size or empty load module",
    ],
    [
      "entry point",
      (bytes: Buffer) => bytes.writeUInt16LE(128, 20),
      "DOS MZ entry point lies outside its initialized load module",
    ],
    [
      "relocation-table bounds",
      (bytes: Buffer) => {
        bytes.writeUInt16LE(2, 6);
      },
      "DOS MZ relocation table lies outside its header",
    ],
    [
      "relocation target",
      (bytes: Buffer) => {
        bytes.writeUInt16LE(1, 6);
        bytes.writeUInt16LE(127, 28);
      },
      "DOS MZ relocation 0 points outside its load module",
    ],
  ] as const)("rejects invalid %s", (_name, damage, error) => {
    const bytes = dosMz();
    damage(bytes);
    expect(parseDosMzHeader(bytes)).toEqual({ ok: false, error });
  });

  it("rejects a truncated relocation table even when the file size is known", () => {
    const bytes = dosMz();
    bytes.writeUInt16LE(1, 6);
    expect(parseDosMzHeader(bytes.subarray(0, 30), bytes.length)).toMatchObject(
      {
        ok: false,
        error: "truncated DOS MZ relocation table",
      },
    );
  });
});

describe("MZ Windows header observations", () => {
  it.each(["NE", "LE", "LX"])(
    "distinguishes unsupported %s from DOS",
    (signature) => {
      const bytes = pe(0x8664);
      bytes.writeUInt16LE(4, 8);
      bytes.writeUInt16LE(64, 24);
      bytes.write(signature, 64, "ascii");
      expect(parseExecutableHeader(bytes, "x64")).toEqual({
        ok: false,
        error: `unsupported ${signature} executable in MZ image`,
      });
    },
  );

  it("keeps a short-header nonzero module value as DOS without a signature", () => {
    const bytes = dosMz(64);
    bytes.writeUInt32LE(bytes.length, 60);

    expect(observeMzWindowsHeader(bytes)).toEqual({ state: "absent" });
    expect(parseExecutableHeader(bytes, "x64")).toMatchObject({
      ok: true,
      value: { format: "dos-mz", architecture: "x86" },
    });
  });

  it.each(["PEZZ", "NE", "LE", "LX", "PE"])(
    "keeps a non-Windows %s signature candidate as DOS",
    (signature) => {
      const bytes = dosMz(64);
      const offset = 32;
      bytes.writeUInt32LE(offset, 60);
      const candidate = Buffer.from(
        [...Buffer.from(signature)].map((byte) =>
          signature === "PEZZ" ? byte : byte | 0x80,
        ),
      );
      bytes.set(candidate, offset);

      expect(observeMzWindowsHeader(bytes)).toEqual({ state: "absent" });
      expect(parseExecutableHeader(bytes, "x64")).toMatchObject({
        ok: true,
        value: { format: "dos-mz", architecture: "x86" },
      });
    },
  );

  it("accepts a short-header candidate only when it points at a Windows signature", () => {
    const stub = dosMz(64);
    const offset = stub.length;
    stub.writeUInt32LE(offset, 60);
    const record = pe(0x8664).subarray(64);
    const bytes = Buffer.concat([stub, record]);

    expect(observeMzWindowsHeader(bytes)).toMatchObject({
      state: "declared",
      offset,
      signature: "PE",
    });
    expect(parseExecutableHeader(bytes, "x64")).toMatchObject({
      ok: true,
      value: { format: "pe", architecture: "x86_64" },
    });
  });

  it.each(["NE", "LE", "LX"])(
    "recognizes short-header %s signatures as Windows declarations",
    (signature) => {
      const stub = dosMz(64);
      const offset = stub.length;
      stub.writeUInt32LE(offset, 60);
      const bytes = Buffer.concat([stub, Buffer.alloc(24)]);
      bytes.write(signature, offset, "ascii");

      expect(observeMzWindowsHeader(bytes)).toMatchObject({
        state: "declared",
        offset,
        signature,
      });
      expect(parseExecutableHeader(bytes, "x64")).toEqual({
        ok: false,
        error: `unsupported ${signature} executable in MZ image`,
      });
    },
  );

  it("requests the signature when a short-header candidate is outside its prefix", () => {
    const offset = 8192;
    const bytes = dosMz(64);
    bytes.writeUInt32LE(offset, 60);
    const prefix = Buffer.alloc(4096);
    bytes.copy(prefix);

    expect(observeMzWindowsHeader(prefix, offset + 512)).toEqual({
      state: "signature-needed",
      offset,
    });
    expect(
      observeMzWindowsHeader(
        prefix,
        offset + 512,
        Buffer.from("PE\0\0", "binary"),
      ),
    ).toEqual({ state: "declared", offset, signature: "PE" });
  });

  it("preserves DOS facts while leaving an unsigned Windows pointer unresolved", () => {
    const bytes = Buffer.alloc(128);
    bytes.write("MZ", 0, "ascii");
    bytes.writeUInt16LE(128, 2);
    bytes.writeUInt16LE(1, 4);
    bytes.writeUInt16LE(2, 6);
    bytes.writeUInt16LE(4, 8);
    bytes.writeUInt16LE(28, 24);
    bytes.writeUInt32LE(0, 28);
    bytes.writeUInt32LE(0, 32);
    bytes.write("ABCD", 60, "ascii");

    const evidence = parseMzHeaderEvidence(bytes);
    expect(evidence.dos).toMatchObject({
      ok: true,
      value: {
        headerBytes: 64,
        imageBytes: 128,
        moduleBytes: 64,
        relocationCount: 2,
        relocationTableOffset: 28,
      },
    });
    expect(evidence.windowsHeader).toEqual({
      state: "candidate",
      offset: 0x44434241,
      signature: "unobserved",
    });
    expect(parseExecutableHeader(bytes, "x64")).toEqual({
      ok: false,
      error: "MZ Windows new-header candidate is unresolved",
    });

    bytes.writeUInt32LE(0xfffffff0, 60);
    expect(parseMzHeaderEvidence(bytes).windowsHeader).toEqual({
      state: "candidate",
      offset: 0xfffffff0,
      signature: "unobserved",
    });
    expect(parseExecutableHeader(bytes, "x64")).toMatchObject({ ok: false });
  });
});