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 |
Encrypt UPX Stub | upx-ucl --lzma payload.exe |
Harder to detect |
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 | upx-ucl --extract-stub=stub.bin |
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. Encrypting the UPX Header
By default, UPX leaves headers partially readable, which makes it easy for security tools to detect a UPX-packed file. You can encrypt the UPX stub to evade detection:
upx-ucl --lzma payload.exe
- Effect: Uses LZMA compression, making unpacking harder.
- Detection Evasion: Some EDRs struggle to analyze LZMA-packed binaries.
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
,UPX2
in 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:
upx-ucl --extract-stub=stub.bin payload.exe
- 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
UPX0
andUPX1
toCODE
andDATA
.
- 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