Skip to content

Fix shellcode emulation: use memory-mapped image for decoy PEs#284

Draft
williballenthin wants to merge 4 commits intomasterfrom
worktree-issue-45-shellcode
Draft

Fix shellcode emulation: use memory-mapped image for decoy PEs#284
williballenthin wants to merge 4 commits intomasterfrom
worktree-issue-45-shellcode

Conversation

@williballenthin
Copy link
Collaborator

Summary

  • Root cause: ApiModuleLoader wrote raw PE file bytes into emulated memory, but PE sections have different file offsets vs virtual addresses. Shellcode walking the PEB InInitializationOrderModuleList to find kernel32 and parse its export directory would read garbage data because .edata was at its file offset rather than its virtual address.
  • Fix: Use pefile.get_memory_mapped_image() so sections are placed at their correct virtual addresses, matching how Windows actually loads DLLs.
  • Bonus fix: Process.ldr_entries was a shared class-level mutable list (classic Python gotcha) instead of per-instance, causing cross-test contamination. Moved to __init__.
  • Config fix: Test config test.json now lists ntdll before kernel32, matching the default config order.

Test plan

  • New test_peb_shellcode.py with two tests:
    • PEB walk shellcode resolves WinExec from kernel32 via InInitializationOrderModuleList
    • Same shellcode succeeds even when config has kernel32 before ntdll (verifying _ordered_peb_modules())
  • All existing tests pass (47 passed, 1 skipped; 2 pre-existing failures from missing capa-testfiles)

Closes #45

🤖 Generated with Claude Code

williballenthin and others added 3 commits March 9, 2026 11:34
@google-cla
Copy link

google-cla bot commented Mar 9, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

The ApiModuleLoader was writing raw PE file bytes into emulated memory,
but PE sections have different file offsets vs virtual addresses. Shellcode
that walks the PEB InInitializationOrderModuleList to find kernel32 and
then parses its export directory would read garbage because the .edata
section data was at its file offset rather than its virtual address.

Use pefile's get_memory_mapped_image() so sections are placed at their
correct virtual addresses, matching how Windows loads DLLs.

Also fixes Process.ldr_entries being a shared class-level mutable default
instead of a per-instance list, and corrects the test config module order
to list ntdll before kernel32 (matching the default config).

Closes #45
@williballenthin williballenthin force-pushed the worktree-issue-45-shellcode branch from c5c7ea4 to 1e486dc Compare March 9, 2026 18:47

jit = JitPeFile(self._arch, base=self._base)
img_data = jit.get_decoy_pe_image(self._name, all_exports)
jit.get_decoy_pe_image(self._name, all_exports)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now this doesn't return anything that we use? that seems like the API is designed wrong, then.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are we using it for a side effect (in which case the name shouldn't be get_*)? or is this now dead code?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this file, too contrived.

@williballenthin williballenthin marked this pull request as draft March 10, 2026 11:54
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.

Shellcode emulation issue

2 participants