Convert - Exe To Shellcode
You can automate the process using a script. Here's a basic example using Python and the subprocess module:
import subprocess
def exe_to_shellcode(exe_path):
# Extract binary data
subprocess.run(["dumpbin", "/raw", exe_path], stdout=open("example.bin", "wb"))
# Remove headers and metadata
subprocess.run(["dd", "if=example.bin", "of=example.bin.noheader", "bs=1", "skip=64"])
# Align to page boundary
subprocess.run(["msvc", "-c", "example.bin.noheader", "-Fo", "example.bin.aligned"])
# Return the generated shellcode
with open("example.bin.aligned", "rb") as f:
return f.read()
# Usage:
shellcode = exe_to_shellcode("example.exe")
print(shellcode.hex())
Note that this is a simplified example. Depending on your specific requirements, you might need to adjust the process.
Finally, after the image is loaded in memory and fixed up, the shellcode jumps to the EntryPoint of the EXE.
Use objdump to extract the binary data from the EXE file:
objdump -d example.exe -M intel -S
This will disassemble the EXE file and display the binary data. You can redirect the output to a file: convert exe to shellcode
objdump -d example.exe -M intel -S > example.disasm
Prerequisites: Download donut.exe from the GitHub releases or compile it yourself.
Command:
donut -f my_payload.exe -o shellcode.bin -a 2 -z 2 -x 1
Parameter breakdown:
Example:
donut -f my_beacon.exe -o beacon.bin -a 2 -z 2
After execution, you get beacon.bin – pure shellcode. You can now:
Understanding this technique is crucial for defenders. If you see:
...you are likely looking at reflective PE injection.
Mitigations:
This technique can be used for:
Never use this for:
Save this as popup.c and compile with mingw or Visual Studio:
#include <windows.h>
int main() MessageBoxA(NULL, "I am shellcode now!", "Converted EXE", MB_OK); return 0;You can automate the process using a script
Compile: x86_64-w64-mingw32-gcc popup.c -o popup.exe