Skip to content

CAPE: Fix disguised/zeroed-DOS PE recovery and relocated ImageBase fallback - #262

Open
doomedraven wants to merge 1 commit into
kevoreilly:capemonfrom
doomedraven:fix/disguised-pe-and-imagebase
Open

doomedraven wants to merge 1 commit into
kevoreilly:capemonfrom
doomedraven:fix/disguised-pe-and-imagebase

Conversation

@doomedraven

Copy link
Copy Markdown
Collaborator

Summary

  • Disguised / zeroed-DOS PE detection & recovery (CAPE/CAPE.{h,c}, CAPE/Injection.c, CAPE/ScyllaHarness.cpp, CAPE/Scylla/PeParser.{h,cpp}):
    • Add bounded FindDisguisedNtHeader() supporting zeroed/stomped e_lfanew (Machine + Magic + SizeOfOptionalHeader + TestPERequirements + non-empty section + 4-byte-aligned HasInnerDosHeader() to avoid mis-anchoring embedded PEs or false-rejecting Rich headers).
    • Keep ScanForDisguisedPE() O(N) (AllowZeroedDos only at p == 0 and page boundaries) and make IsDisguisedPEHeader() return 0/1 (never truthy -1).
    • Track headerMemorySize and sectionHeadersLoaded in PeParser so isValidPeFile() enforces bounds without breaking ApiReader::parseModuleWithProcess (readSectionHeaders == false) on large system DLLs.
    • Replace unvalidated MachineProbe loops in DumpImageInCurrentProcess(), ScyllaDumpProcess(), and PeParser::getDosAndNtHeader(), populate dosStubSize/pDosStub on recovered zeroed-DOS headers, initialize/reset SlackData, and restore MZ/PE signatures in ScyllaDumpProcess().
    • Fix IsDotNetImage() PE32/PE64 dispatch on x64 (DataDirectory[14] offset) and wrap post-lookup dereferences in SEH (FileOffsetFromRVA, GetEntryPoint, GetTimeStamp, DumpImageInCurrentProcess, WriteMemoryHandler).
  • Relocated OptionalHeader.ImageBase fallback (CAPE/Scylla/PeParser.cpp):
    • In readPeSectionsFromProcess(), probe for live relocation blocks via readMemoryPartlyFromProcess() before calling reBasePEImage(); when no live relocations exist (stripped/zeroed .reloc), update OptionalHeader.ImageBase to the 64 KiB-aligned moduleBaseAddress so dumped pointers match the PE header without risking double-mismatch on partial rebases.

Verification

…llback

* Add bounded FindDisguisedNtHeader() supporting zeroed/stomped e_lfanew
  with Machine+Magic+section validation, inner-DOS anti-misanchoring,
  and O(N) page-aligned probing in ScanForDisguisedPE(); return 0/1
  (never truthy -1) from IsDisguisedPEHeader().
* Fix PeParser::getDosAndNtHeader() / isValidPeFile() / ScyllaDumpProcess()
  to bound headerMemorySize, populate dosStubSize/pDosStub on recovered
  zeroed-DOS headers, reset SlackData, restore MZ/PE signatures, and
  preserve ApiReader readSectionHeaders=false behavior.
* Update OptionalHeader.ImageBase to moduleBaseAddress in
  PeParser::readPeSectionsFromProcess() when no live relocations exist,
  and fix IsDotNetImage() PE32/PE64 dispatch plus SEH guards around
  post-lookup header dereferences.
@doomedraven
doomedraven marked this pull request as ready for review November 22, 2026 07:07
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