Files
speakeasy/tests/test_file_access.py
Willi Ballenthin 4730f34fbe Resolve Windows APIs through mapped entry points
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.
2026-10-11 20:08:51 +01:00

37 lines
1011 B
Python

# Copyright (C) 2020 FireEye, Inc. All Rights Reserved.
import pytest
def get_api_calls(ep, api_name):
return [evt for evt in ep.events or [] if evt.event == "api" and evt.api_name == api_name]
@pytest.mark.parametrize(
"bin_file",
[
"file_access_test_x86.exe.xz",
"file_access_test_x64.exe.xz",
],
)
def test_file_access(config, load_test_bin, run_test, bin_file):
data = load_test_bin(bin_file)
report = run_test(config, data)
eps = report.entry_points
driver_entry = eps[0]
create_file = get_api_calls(driver_entry, "ntdll.NtCreateFile")
assert len(create_file) == 1
create_file = create_file[0]
assert create_file.args[2].display == "\\??\\c:\\myfile.txt"
read_file = get_api_calls(driver_entry, "ntdll.NtReadFile")
assert len(read_file) == 1
printf = get_api_calls(driver_entry, "msvcrt.__stdio_common_vfprintf")
assert len(printf) == 5
printf = printf[-1]
assert "File contained:" in printf.args[2].display