Skip to content

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

Open
williballenthin wants to merge 1 commit intomasterfrom
worktree-issue-45-shellcode
Open

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

Conversation

@williballenthin
Copy link
Copy Markdown
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

@google-cla
Copy link
Copy Markdown

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.

@williballenthin williballenthin marked this pull request as draft March 10, 2026 11:54
@williballenthin

This comment was marked as resolved.

@williballenthin williballenthin force-pushed the worktree-issue-45-shellcode branch from 1e486dc to d9aa567 Compare April 2, 2026 11:16
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

remove unnecessary test file
@williballenthin williballenthin force-pushed the worktree-issue-45-shellcode branch from d9aa567 to 1cc9bcb Compare April 2, 2026 11:16
@williballenthin williballenthin marked this pull request as ready for review April 2, 2026 11:17
Copy link
Copy Markdown
Collaborator Author

@williballenthin williballenthin left a comment

Choose a reason for hiding this comment

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

lgtm

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

1 participant