Blog | G5 Cyber Security

PE Binary Backdoor with ASLR

TL;DR

This guide shows how to add a backdoor to a Windows Portable Executable (PE) binary compiled with Address Space Layout Randomization (ASLR). We’ll modify the PE header to include space for shellcode, then inject that shellcode and redirect execution. It assumes basic familiarity with hex editors and the PE file format.

Prerequisites

Steps

  1. Understand ASLR
    • ASLR randomizes the base address of modules when loaded into memory, making it harder for attackers to predict where code is located. This guide bypasses this by modifying the PE header itself.
  2. Identify a Suitable Location
    • Open the PE file in your hex editor.
    • Look for unused sections or padding within existing sections. The larger, the better. We need enough space to store our shellcode and potentially some additional code. Common targets are the .data or .bss sections if they have sufficient free space.
  3. Calculate Space Requirements
    • Determine the size of your shellcode (the backdoor). For example, a simple reverse shell might be 100-200 bytes.
    • Add some extra space for potential future expansion or error handling. A total of 512 bytes is often sufficient.
  4. Modify the PE Header
    • Navigate to the Section Header Table in your hex editor (usually after the DOS header and NT headers). The exact offset varies, but tools like CFF Explorer can help you find it.
    • Find the section you identified in Step 2.
    • Increase the SizeOfRawData and VirtualSize fields of that section to accommodate your shellcode. Be careful not to overwrite other important data!
    • Adjust the PointerToRawData field if necessary to point to the new location where you’ll insert the shellcode.
  5. Inject Shellcode
    • Navigate to the calculated offset (from Step 4) within the PE file.
    • Paste your shellcode into this location. Ensure it doesn’t overwrite any existing valid code or data.
  6. Redirect Execution
    • Find the Entry Point of the executable in the PE header (AddressOfEntryPoint). This is where execution begins.
    • Overwrite the first few bytes of the original entry point with a jump instruction to your shellcode’s address. For example, using JMP . The exact assembly code depends on your architecture (x86 or x64).
    • Example (x86):
      EB 

      , where `` is the relative offset from the current instruction to the start of your shellcode. Calculate this carefully!

  7. Test and Debug
    • Save the modified PE file.
    • Run the executable. If everything went correctly, your shellcode should execute.
    • Use a debugger (e.g., x64dbg) to verify that execution is redirected to your shellcode and that it’s running as expected. Set breakpoints at the start of your shellcode.

Important Considerations

Exit mobile version