A packer compresses or encrypts an executable and prepends a small stub that restores the original code at runtime. Static analysis of a packed binary sees only the stub, which is why packer-analysis exists: identify the packer, reverse its transformation, and recover the unpacked payload for real analysis. This skill covers identification, the easy UPX case, reading section and entropy evidence, the standard memory-dump unpacking technique, overlay handling, and validating the result. Scope: unpack only samples you are authorized to analyze, in an isolated lab VM. Unpacking executes the stub's logic in some form, so treat every step with the same containment discipline as dynamic analysis.
Quick Workflow
- Confirm the binary is packed: entropy, section names, and Detect It Easy signatures.
- Identify the exact packer and version.
- If UPX, unpack with
upx -dand verify; done in one command. - For other packers, map the packed regions: which sections are high entropy, where the stub ends and payload begins.
- Recover the payload via memory dump after the stub unpacks it (isolated VM, debugger or process dump at the original entry point).
- Fix up the dumped image (imports, sections) enough for static analysis.
- Re-run the full static triage on the unpacked payload.
- Write YARA rules against the payload, not the packer.
1. Confirming and identifying the packer
Three independent signals should agree before you commit to an unpacking strategy.
# Signal 1: Detect It Easy signatures
diec packed.bin
# Signal 2: section names and entropy (UPX0/UPX1, .themida, .aspack, UPX!)
python3 - <<'EOF'
import struct, math
from collections import Counter
data = open('packed.bin','rb').read()
e_lfanew = struct.unpack('<I', data[0x3c:0x40])[0]
coff = e_lfanew + 4
nsec = struct.unpack('<H', data[coff+2:coff+4])[0]
opt_size = struct.unpack('<H', data[coff+16:coff+18])[0]
sec_off = coff + 20 + opt_size
for i in range(nsec):
s = data[sec_off+i*40:sec_off+(i+1)*40]
name = s[:8].rstrip(b'\x00').decode(errors='ignore')
rawsize, rawptr = struct.unpack('<II', s[16:24])
chunk = data[rawptr:rawptr+rawsize]
ent = -sum((v/len(chunk))*math.log2(v/len(chunk)) for v in Counter(chunk).values()) if chunk else 0
print(f'{name:8} size={rawsize:8} entropy={ent:.2f}')
EOF
# Signal 3: entry point location (packer stubs often start in the last section)
# and import table size (stubs import almost nothing)
Typical tells: two or three sections with odd names, .text entropy above
7.5, an import table with only LoadLibrary/GetProcAddress, and a DIE hit
naming the packer. If DIE says "UPX" and you see UPX0/UPX1 sections, skip to
section 2.
2. The UPX case
UPX is the most common packer in the wild and the easiest to reverse, because
upx -d exists. Always try it first.
# Test what UPX thinks of the file
upx -t packed.bin
# Unpack to a new file (keep the original)
upx -d packed.bin -o unpacked.bin
ls -l packed.bin unpacked.bin
# Verify: the unpacked file should have normal sections and strings
file unpacked.bin
Modified UPX (attackers tweak the stub to break upx -d) fails here with a
"not packed by UPX" or checksum error. That is a signal, not a dead end: the
underlying compression is usually still NRV/LZMA, and the memory-dump method
in section 4 works regardless of stub modifications.
3. Mapping the packed regions
For non-UPX packers, understand the layout before attempting recovery.
# Where does the entry point point? (section containing EP = stub usually)
python3 - <<'EOF'
import struct
data = open('packed.bin','rb').read()
e_lfanew = struct.unpack('<I', data[0x3c:0x40])[0]
opt = e_lfanew + 4 + 20
ep = struct.unpack('<I', data[opt+16:opt+20])[0]
print(f'entry point RVA: 0x{ep:x}')
EOF
# Overlay: data appended after the last section (often the packed payload)
python3 - <<'EOF'
import struct
data = open('packed.bin','rb').read()
e_lfanew = struct.unpack('<I', data[0x3c:0x40])[0]
coff = e_lfanew + 4
nsec = struct.unpack('<H', data[coff+2:coff+4])[0]
opt_size = struct.unpack('<H', data[coff+16:coff+18])[0]
sec_off = coff + 20 + opt_size
end = 0
for i in range(nsec):
s = data[sec_off+i*40:sec_off+(i+1)*40]
rawsize, rawptr = struct.unpack('<II', s[16:24])
end = max(end, rawptr + rawsize)
print(f'file size: {len(data)}, last section ends at: {end}, overlay: {len(data)-end} bytes')
EOF
A large overlay plus a tiny stub means the payload is sitting in the overlay waiting to be mapped; some unpackers just need the overlay carved and decompressed. Check the overlay's entropy and first bytes before assuming.
4. Memory-dump unpacking
The universal technique: let the stub do its job in a controlled environment, then dump the process at the moment the original code is unpacked but before it runs. This works against most packers because the stub must produce executable unpacked code in memory.
# In the isolated VM:
# 1. Load the packed binary in a debugger (x64dbg, WinDbg)
# 2. The stub unpacks and jumps to the Original Entry Point (OEP).
# Find the OEP by watching for a jump to a newly-written executable region,
# or use the classic "hardware breakpoint on write then execute" method.
# 3. At the OEP, dump the process memory.
# Alternative without a debugger: run under a process dumper that snapshots
# at a timed delay (crude but works for simple stubs)
timeout 30 ./packed.bin & sleep 5 && gcore -o dump $! 2>/dev/null
After dumping, the image needs repair before tools accept it: the import
table is often destroyed or redirected through the stub. Rebuild imports
with an import reconstructer, fix section headers to match the dumped
regions, and set the entry point to the OEP. This is fiddly work; budget
time for it, and validate each fix by re-running file and your triage
pipeline.
# Sanity checks on the dumped image
file dumped.bin
# Should report a PE/ELF with plausible sections, not 'data'
python3 - <<'EOF'
data = open('dumped.bin','rb').read()
print('MZ header:', data[:2] == b'MZ')
print('size:', len(data))
EOF
5. Special cases
Virtualizer packers (Themida, VMProtect) do not just compress, they translate code into a custom bytecode run by an embedded interpreter. There is no "unpacked PE" to recover in the simple sense; analysis shifts to the VM handlers or to dynamic behavior. Recognize this early from DIE output so you do not burn days on a memory dump that yields bytecode.
diec packed.bin | grep -iE 'themida|vmprotect|enigma|obsidium'
# Any hit above: stop manual unpacking, escalate to dynamic analysis
.NET packers (ConfuserEx and friends) need .NET-aware unpackers; the payload is IL code, and tools like de4dot-style deobfuscators handle the standard cases. SFX archives (self-extracting zips/rars) are not packers at all: just extract them as archives and triage the contents.
6. Re-triage and rule writing
The unpacked payload goes back through the full static pipeline, because the packed triage told you almost nothing about the real code.
# Fresh triage on the unpacked binary
sha256sum unpacked.bin
file unpacked.bin
strings unpacked.bin | grep -a -i -E 'http|cmd|powershell|SOFTWARE' | head -30
yara -r /opt/rules/index.yar unpacked.bin
# Now write the family rule from PAYLOAD strings (yara-rules)
# A rule built on packer-stub strings fires on every packed benign binary too
Compare the packed and unpacked string sets; the delta is the payload's vocabulary and usually the best rule material you will get.
Edge cases
upx -dfails on a UPX-packed file: modified UPX. Fall back to memory dump, or tryupx -dafter restoring the UPX magic bytes if you can identify the modification.- Dumped image will not disassemble: imports not rebuilt, or the OEP guess was wrong and you dumped mid-unpack. Re-dump at a later breakpoint.
- Multiple packing layers (packed twice): unpack iteratively; each layer's output is the next layer's input. DIE on each intermediate to track progress.
- Packer stub is also the anti-debug: the memory-dump method needs debugger evasion (hide the debugger, patch IsDebuggerPresent) which is scope-gated; simpler to use timed process dumps instead.
- ELF packers (UPX on Linux): same
upx -dworkflow; memory dump viagcoreat the right moment works identically.
Escalation
- Manual unpacking stalls: escalate to dynamic-sandbox and detect on behavior instead of bytes. Not every sample needs to be unpacked.
- Virtualizer packer confirmed: shift to dynamic analysis plus VM-handler analysis in Ghidra; consider whether the engagement actually needs the unpacked code or just the behavior and IOCs.
- Custom packer with no public unpacker: the stub itself is now the analysis target; reverse the decompression routine in Ghidra and reimplement it in Python (stdlib only) to decrypt the payload offline.
- Time-box it: set a hard limit (e.g., 4 hours) for manual unpacking. Past that, dynamic analysis plus memory carving usually yields more value per hour.
Detection and defender view
Packer detection is a triage signal, not a verdict: packers are dual-use (legitimate software uses them too), so defenders should combine packer identification with reputation, signing status, and behavior. Detection opportunities: alert on high-entropy executables arriving via email or download, on processes whose in-memory image differs from their on-disk image (hollowshunter-style checks in EDR), and on the classic RWX private memory regions that unpacking stubs create. For the lab, the defensive value of this skill is recovering payloads that become YARA rules and IOCs; every unpacked payload should produce detection content. Misuse is using unpacking to strip protections from software you have no right to analyze, or to improve a packer against detection; both are out of scope.
Key references
- MITRE ATT&CK T1027.002 (Software Packing): the technique under analysis.
- MITRE ATT&CK T1027 (Obfuscated Files or Information): the parent technique.
- MITRE ATT&CK T1055 (Process Injection): what stubs often do next.
- UPX documentation: https://upx.github.io
- Detect It Easy: https://github.com/horsicq/Detect-It-Easy
- Ghidra documentation: https://ghidra-sre.org
See also
malware-packer-triage: Use it instead for the deeper unpacking workflow, including finding the original entry point and dumping and fixing imports.
