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.
42 lines
1.6 KiB
Python
42 lines
1.6 KiB
Python
"""Public shellcode reports retain input literals and runtime stack strings."""
|
|
|
|
from speakeasy import Speakeasy
|
|
|
|
|
|
def test_public_shellcode_reports_ansi_and_utf16_input_strings(config):
|
|
config["analysis"]["strings"] = True
|
|
ansi = "INPUT_ANSI_LITERAL"
|
|
wide = "INPUT_WIDE_LITERAL"
|
|
data = b"\xc3\0" + ansi.encode() + b"\0\0" + wide.encode("utf-16le") + b"\0\0"
|
|
with Speakeasy(config=config) as se:
|
|
base = se.load_shellcode(data=data, arch="x86")
|
|
|
|
se.run_shellcode(base)
|
|
|
|
report = se.get_report()
|
|
assert report.entry_points[0].error is None
|
|
assert ansi in report.strings.static.ansi
|
|
assert wide in report.strings.static.unicode
|
|
# Synthetic API export names must not replace the input inventory.
|
|
assert "GetTickCount" not in report.strings.static.ansi
|
|
|
|
|
|
def test_public_shellcode_extracts_new_stack_string_after_guest_stores(config):
|
|
config["analysis"]["strings"] = True
|
|
marker = "DECODED_STACK_MARKER"
|
|
# Byte-by-byte stores keep the complete text out of the input image. RET
|
|
# leaves the bytes below SP for the completion-time string scanner.
|
|
code = b"\x83\xec\x20"
|
|
for offset, value in enumerate(marker.encode() + b"\0"):
|
|
code += b"\xc6\x44\x24" + bytes([offset, value])
|
|
code += b"\x83\xc4\x20\xc3"
|
|
with Speakeasy(config=config) as se:
|
|
base = se.load_shellcode(data=code, arch="x86")
|
|
|
|
se.run_shellcode(base)
|
|
|
|
report = se.get_report()
|
|
assert report.entry_points[0].error is None
|
|
assert marker not in report.strings.static.ansi
|
|
assert marker in report.strings.in_memory.ansi
|