Introduction
UPX is an open source program that compresses executables. It is not a file format of its own: a packed program is still a file of the original kind (for a Windows program or DLL, a PE file; DLL modules such as .sem and .sep can be packed too), whose code and data have been replaced by a compressed copy and a small decompressing stub. The operating system loads the file as usual, the stub runs first, unpacks the original into memory and jumps to it.
The explorer detects the packing on a file it opens, says so, and shows the file as it is on disk. It does not unpack it.
How to recognise a packed PE file
- The sections are named
UPX0,UPX1and usuallyUPX2, instead of.text,.dataand so on.UPX0takes no space in the file (its size on disk is 0) but reserves address space for the unpacked program;UPX1holds the stub and the compressed data;UPX2holds the resources and the data the stub needs. - The text
UPX!appears inUPX1. It is the start of the pack header, which UPX writes near the compressed data. The header holds the version of the packer, the format, the compression method and level, the compressed and uncompressed sizes, and a checksum; the explorer reads these and checks the checksum of newer headers. - The explorer treats a file as packed when the first section name begins with
UPX,UPX!is found inUPX1and the header parses. Section names can be changed by hand, so a file that has been renamed is not recognised, and a file with only the names changed is reported without a header.
The stub and unpacking
The entry point of the packed file is in UPX1. The stub allocates
nothing of its own: it decompresses into the address space that UPX0
reserved, rebuilds the import table the original program had, restores the original
relocations where the file needs them, and jumps to the original entry point. The
compression methods UPX uses are NRV, UCL and LZMA; the method found in the header is
shown by the explorer by name.
upx -d reverses the packing. It reads the same header, decompresses the
data and writes back a PE file with the original section layout. Because the stub and
the original are kept in the packed file, a file that has not been modified after
packing can be restored; a file that has been altered afterwards, or one packed with a
tampered or custom stub, may not be.
Not described
Known, not yet covered
- Viewer: unpacking. The explorer would have to implement the decompression methods and the rebuilding of imports; it shows the packed file only.
- Reference: the packing of other kinds of file (ELF, Mach-O, DOS programs) and the full header layout of each: only the Windows case is covered here.
Unknown
- The exact meaning of every byte of the stub’s variants. The sources read describe the header and the process, not the machine code of each stub.
Links
Software
- UPX – the packer and unpacker (command line, open source)
Developers
- UPX source code – the packer, including the code that reads and writes the pack header (C++)
Reference
- PE Format – Microsoft’s specification of the file that UPX packs