Menu

1 / 2

How I load an exe directly into memory without touching disk — manual PE mapping

DEV Community·iamsopotatoe·6 months ago
#1qtj9yjQ
#cpp#programming#security#base#byte
Reading 0:00
15s threshold

most people think running an exe means writing it to disk first. it doesn't.

as part of building TinyLoad, a Windows PE packer, I had to write a PE loader that maps an executable directly into memory and runs it without ever creating a file. here's how it works.

what is a PE file

PE (Portable Executable) is the format Windows uses for .exe and .dll files. it's basically a structured blob with a header describing how to load it, followed by sections containing code, data, resources etc.

to run a PE file manually you have to do what the Windows loader does — but yourself, in memory.

step 1: parse the headers

every PE starts with a DOS header, then an NT header. the NT header tells you everything you need:

SizeOfImage — how much memory to allocate

ImageBase — where the linker expected the binary to live

AddressOfEntryPoint — where to jump to start execution

SizeOfHeaders — how much of the front to copy as-is

IMAGE_DOS_HEADER * dos = ( IMAGE_DOS_HEADER * ) data .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.