1e. Advanced UPX Methods for Modifying Binary Structure
1e. Advanced UPX Methods for Modifying Binary Structure
UPX offers several packing, obfuscation, and evasion techniques beyond basic compression.
$ upx-ucl --version
upx 4.2.2
UCL data compression library 1.03
zlib data compression library 1.3.0.1-motley
LZMA SDK version 4.43
| UPX Method | Command | Purpose |
|---|---|---|
| Basic UPX Compression | upx-ucl -9 payload.exe | Maximum compression |
| Fast UPX Packing | upx-ucl --best payload.exe | Faster execution |
| Brute-force Packing | upx-ucl --ultra-brute payload.exe | Best compression |
| LZMA Compression Mode | upx-ucl --lzma payload.exe | Changes compressed data layout; may hinder some static analysis (not encryption) |
| Change UPX Signature | hexedit payload.exe | Modify UPX! signature |
| Strip UPX Headers | upx-ucl --strip-relocs=0 payload.exe | Removes relocation info |
| Exclude Sections | upx-ucl --overlay=copy payload.exe | Leaves overlays intact |
| Manual UPX Stub Editing | (see note below — no built-in option) | Modify decompression stub |
| Unpacking UPX | upx-ucl -d payload.exe | Restore original binary |
1️. Basic UPX Compression
This is the most common way to pack an executable:
upx-ucl -9 payload.exe
-9→ Maximum compression- Effect: Reduces file size and modifies PE structure.
Use Case: Reduces detection by changing the binary’s hash.
2️. Using --best Mode
Optimizes the compression ratio while maintaining execution speed:
upx-ucl --best payload.exe
- Effect: Balances speed and compression.
- Use Case: Suitable when execution performance matters.
3️. Using --ultra-brute for Maximum Compression
upx-ucl --ultra-brute payload.exe
- Effect: Tries all available compression methods for the smallest possible file.
- Detection Evasion: Changes the binary’s entropy, which may confuse heuristic detection.
4. Using LZMA Compression
By default, UPX uses its own compression format. The --lzma flag switches to the LZMA compression algorithm, which changes the compressed data layout and may hinder some static analysis tools. This is not encryption — UPX has no built-in encryption capability; --lzma selects a compression codec only.
upx-ucl --lzma payload.exe
- Effect: Uses LZMA compression, changing the packed data layout compared to the default UCL algorithm.
- Detection Note: Some EDRs struggle to analyze LZMA-packed binaries due to the different decompression stub structure, but the binary is not encrypted and can be unpacked with standard tools.
5️. Changing UPX Magic Signature
Some AV/EDR tools detect UPX-packed binaries by looking for the UPX! signature in the PE header.
To modify this:
hexedit payload.exe
- Locate
UPX!,UPX1,UPX2in the hex editor. - Change it to
XYZ!or another unique signature. - Save the changes.
Effect: Makes the file undetectable by signature-based scanners.
6️. Stripping the UPX Header
To further evade detection, you can strip out the UPX headers so that the executable appears normal in static analysis:
upx-ucl --strip-relocs=0 payload.exe
- Effect: Removes unnecessary relocation data.
- Detection Evasion: Some AV engines use relocation info for analysis.
7️. Packing Only Certain Sections
UPX lets you choose which sections of the PE file to compress:
upx-ucl --compress-icons=1 payload.exe
- Effect: Only compresses icon resources, leaving the main code uncompressed.
You can also exclude some sections:
upx-ucl --overlay=copy payload.exe
- Effect: Keeps overlays (extra data) intact, making it look more like a normal binary.
8️. Manual UPX Stub Modification
- Pack the binary using UPX:
upx-ucl -9 payload.exe - Extract the stub from a packed file:
Note: UPX has no built-in
--extract-stuboption; use a hex editor orobjcopyto extract the stub bytes from the UPX-packed binary. For example:objcopy --dump-section UPX0=stub0.bin payload.exe # or use a hex editor to manually locate and extract the UPX stub region - Modify the stub (e.g., using a hex editor or assembler).
- Reinsert the stub into a new binary.
dd if=modified_stub.bin of=payload.exe bs=1 seek=0 count=25674 conv=notrunc
Effect: Breaks static detections looking for known UPX stubs.
9️. Manually Obfuscating UPX-Packed Files
After using UPX, further obfuscate the binary:
- XOR Encode the File:
xxd -p payload.exe | tr -d '\n' | xxd -r -p > payload_obfuscated.exe
- Rename Sections in the PE Header:
- Use PE-bear or CFF Explorer to rename sections from
UPX0andUPX1toCODEandDATA.
- Use PE-bear or CFF Explorer to rename sections from
10 . Unpacking and Repacking a UPX Binary
** Unpacking a UPX-Packed Binary**
If you receive a packed binary and need to analyze it:
upx-ucl -d payload.exe
- Effect: Fully restores the original file.
** Repacking After Modifications**
Once changes are made, repack it to retain obfuscation:
upx-ucl -9 payload_modified.exe