Skip to content

Scylla: support MOV-based IAT discovery and clear stale OFT=0 imports - #254

Open
doomedraven wants to merge 1 commit into
kevoreilly:capemonfrom
doomedraven:fix/scylla-mov-iat-and-stale-imports
Open

doomedraven wants to merge 1 commit into
kevoreilly:capemonfrom
doomedraven:fix/scylla-mov-iat-and-stale-imports

Conversation

@doomedraven

Copy link
Copy Markdown
Collaborator

Summary

Fixes two coupled failure modes when dumping unpacked Go / UPX binaries at OEP with Scylla:

  1. MOV-based IAT discovery (CAPE/Scylla/IATSearch.{h,cpp}):

    • Go binaries on Windows (386 and amd64) load //go:cgo_import_dynamic Win32 API pointers from the IAT via MOV reg, [IAT_slot] (8B /r disp32 on x86, RIP-relative MOV on x64) and invoke them through runtime.asmstdcall (CALL reg) without emitting CALL [IAT] or JMP [IAT] thunks.
    • IATSearch::findIATPointers() previously inspected only FC_CALL || FC_UNC_BRANCH, causing findIATAdvanced() to miss Go's unpacked IAT completely even with imprec=1 (UPX.yar step2oep).
    • Collects flat-segment (R_NONE / R_DS), pointer-aligned, in-image I_MOV -> O_REG memory load targets during the existing disassembly pass and uses them strictly as a fallback when CALL/JMP IAT discovery yields no entries. Candidate slots are validated via isIATPointerValid(), grouped into <= 0x100-byte contiguous clusters (requiring >= 2 resolved APIs), and extended across adjacent valid thunks and single-NULL module separators.
    • Frees dataBuffer immediately after the disassembly loop in findIATAdvanced(), fixing three early-return new BYTE[] leaks.
  2. Stale OriginalFirstThunk == 0 import directory cleanup on unfixed dump fallback (CAPE/Scylla/PeParser.{h,cpp}):

    • When PeParser::dumpProcess() or dumpProcessToHandle() runs without ImportRebuilder (either FixImports == FALSE or IAT discovery fails), removeIatDirectory() zeroes IMAGE_DIRECTORY_ENTRY_IAT while leaving IMAGE_DIRECTORY_ENTRY_IMPORT untouched.
    • For packers like UPX that emit OriginalFirstThunk == 0, the Windows loader overwrites FirstThunk in RAM with live DLL virtual addresses (0x777fxxxx / 0x00007ff...). Leaving IMAGE_DIRECTORY_ENTRY_IMPORT pointing at snapped OriginalFirstThunk == 0 thunks causes ntdll!LdrpSnapThunk to interpret live VAs as IMAGE_IMPORT_BY_NAME RVAs and crash immediately with STATUS_ACCESS_VIOLATION (0xC0000005) if the dumped PE is executed.
    • PeParser::sanitizeUnrebuiltImportDirectory() inspects OriginalFirstThunk == 0 descriptors on unfixed dump paths and zeroes IMAGE_DIRECTORY_ENTRY_IMPORT when FirstThunk entries point outside mapped image sections, while preserving OriginalFirstThunk != 0 descriptors and ImportRebuilder::rebuildImportTable() outputs untouched.

Are these related?

Yes — unlike the coupled NDEBUG case where build flags alter macro semantics across translation units, both changes here sit directly on the ScyllaDumpProcess() OEP unpacker path for UPX/packed binaries: IATSearch reconstructs MOV-referenced IATs into .CAPE when imprec=1 is active, and PeParser::sanitizeUnrebuiltImportDirectory() ensures the unfixed dump path (imprec=0 or dynamic-only imports) never emits a loader-crashing OriginalFirstThunk == 0 directory.

Verification

  • Note on MSVC compilation: No MSVC toolchain is available on this Linux host, so full Visual Studio solution builds (Win32 / x64) are unverified beyond source inspection.
  • POSIX C++17 + distorm ASan/UBSan harness: Compiled distorm + IATSearch / PeParser logic under g++ -std=c++17 -O1 -g -Wall -Wextra -Werror -fsanitize=address,undefined in both 64-bit (-D_WIN64) and 32-bit (-m32 -U_WIN64) modes covering MOV IAT clustering, .bss noise rejection, FS:/GS: segment override filtering, single-NULL separator extension, CALL/JMP precedence, and 32/64-bit OriginalFirstThunk == 0 stale-thunk sanitization (0 warnings, 0 ASan/UBSan findings).

Series

Builds cleanly on latest upstream/capemon (28c990f) with zero conflicts against open PRs (#207, #209, #210, #213, #214, #215, #243, #251, #253).

Go binaries on Windows (386 and amd64) load //go:cgo_import_dynamic Win32 API
addresses via MOV reg, [IAT_slot] (8B /r disp32 on x86, RIP-relative MOV on
x64) and dispatch them through runtime.asmstdcall (CALL reg) rather than
emitting CALL [IAT] or JMP [IAT] thunks. Because IATSearch::findIATPointers()
filtered strictly on FC_CALL || FC_UNC_BRANCH, Scylla missed unpacked Go IATs
during OEP dumping (e.g. UPX step2oep with imprec=1) and fell back to an
unfixed PeParser::dumpProcess() dump.

In addition, when PeParser::dumpProcess() or dumpProcessToHandle() ran without
rebuilt imports, removeIatDirectory() zeroed IMAGE_DIRECTORY_ENTRY_IAT while
leaving IMAGE_DIRECTORY_ENTRY_IMPORT intact. For packers such as UPX whose
stub import descriptor sets OriginalFirstThunk = 0, the OS loader had already
overwritten FirstThunk in memory with live DLL virtual addresses; leaving that
descriptor active caused ntdll!LdrpSnapThunk to treat snapped VAs as RVA
import-by-name offsets and crash immediately with STATUS_ACCESS_VIOLATION on
re-execution.

Changes:
- Extend IATSearch::findIATPointers() with an optional movPointers collector
  for flat-segment I_MOV memory-to-register loads targeting pointer-aligned
  in-image slots when CALL/JMP IAT scanning finds no entries.
- Validate, cluster (<= 0x100 stride), and extend contiguous MOV-referenced
  API pointer ranges across adjacent valid thunks and single-NULL module
  separators in IATSearch::findIATAdvanced(), and free dataBuffer immediately
  after disassembly to fix three early-return leaks.
- Add PeParser::sanitizeUnrebuiltImportDirectory() on unfixed dumpProcess() /
  dumpProcessToHandle() paths to clear IMAGE_DIRECTORY_ENTRY_IMPORT when an
  OriginalFirstThunk == 0 descriptor has FirstThunk entries pointing outside
  mapped image sections, while leaving OriginalFirstThunk != 0 and
  ImportRebuilder outputs untouched.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant