mirror of
https://github.com/mandiant/speakeasy.git
synced 2026-10-12 02:18:47 +00:00
Synthetic API modules were decoy PE husks whose export stubs only returned an index. Import binding and GetProcAddress gave out sentinel addresses such as 0xFEEDFACE outside any mapping, and the emulator caught calls through the invalid-fetch callback and a code hook on each module. The export table and the IAT therefore gave different addresses for one function, and a guest or debugger could not read, hook, patch or break on the address of an imported API. A module without a handler loaded the default_exe or default_driver decoy PE. One session API registry now owns every synthesized entry. ApiModuleLoader in api_image.py builds a mapped image for each API module, from the exact declarations of the signature database and the explicit function and data handlers. The export table, the IAT and the resolvers give the same mapped entry for a function. Each function entry jumps to a private trap token in a permanently unmapped 1 MiB reservation. A fetch of a token stops Unicorn, and _run_api_engine dispatches the handler outside the native callback. Other accesses to the reservation raise an access violation in the guest. The pc of an API event is the call instruction when memory tracing is enabled, and the caller's return address otherwise. A module without a handler gets an empty placeholder image with a dynamic arena. The get_proc and resolve_export resolvers back GetProcAddress, LdrGetProcedureAddress and MmGetSystemRoutineAddress. They follow forwarders and create dynamic-only entries in the .dyn arena of a synthetic module. Import binding works on each IAT slot: a missing synthetic export gets a dynamic entry, and an invalid slot keeps its bytes. Data exports have writable storage in .data, and the CRT _fmode, _commode and __initenv exports share it with their pointer accessors. The per-run timeout and max_instructions limits now count across API yields, with exact instruction accounting when a limit is set. The sentinel addresses, import_table, impdata_queue, the module access hook and the default_exe/default_driver fallback are removed. Handler callbacks always run inside an API frame, so setup_callback no longer takes caller arguments, and the unused _normalize_mod_name is removed. A guest PE without relocation data cannot be rebased. get_symbols returns a snapshot, GDB breakpoints use the public entries, and doc/unified-api-addresses.md describes the design.
40 lines
1004 B
Python
40 lines
1004 B
Python
import os
|
|
|
|
import pytest
|
|
|
|
from tests.pma_cases import PMA_CASES
|
|
from tests.pma_harness import assert_case, collect_behavior, get_sample_path, run_case
|
|
|
|
CURATED_CASE_NAMES = {
|
|
"pma-01-02-exe",
|
|
"pma-03-02-dll",
|
|
"pma-03-04-in",
|
|
"pma-05-01-dll",
|
|
"pma-06-03-exe",
|
|
"pma-10-03-sys",
|
|
"pma-11-02-dll",
|
|
"pma-12-02-exe",
|
|
"pma-12-04-exe",
|
|
"pma-14-01-exe",
|
|
"pma-16-03-exe",
|
|
"pma-18-03-ocl",
|
|
"pma-21-01-exe",
|
|
}
|
|
|
|
|
|
if os.environ.get("SPEAKEASY_PMA_FULL") == "1":
|
|
CASES = PMA_CASES
|
|
else:
|
|
CASES = tuple(case for case in PMA_CASES if case.name in CURATED_CASE_NAMES)
|
|
|
|
|
|
@pytest.mark.parametrize("case", CASES, ids=[case.name for case in CASES])
|
|
def test_pma_case_declarative(base_config, case, tmp_path):
|
|
sample_path = get_sample_path(case)
|
|
if not sample_path.exists():
|
|
pytest.skip(f"missing sample: {sample_path}")
|
|
|
|
report = run_case(base_config, case, tmp_path)
|
|
observed = collect_behavior(report)
|
|
assert_case(case, report, observed)
|