Manual unpacking is the analyst doing what the stub does, then stealing the result: run the packer stub in a debugger, let it decompress the real program into memory, pause at the moment control transfers to the original code, and dump that memory as a working executable. This sub-skill is the step-by-step for the parent skill's manual workflow. Scope: unpack only samples you are authorized to handle, in an isolated lab VM, during authorized DFIR or malware research. Work on copies; never on the original sample file.
Quick Workflow
- Open a copy of the packed sample in x64dbg; note the stub entry point and section layout.
- Set breakpoints on VirtualAlloc / NtAllocateVirtualMemory to catch the unpack buffer allocation.
- Run and record the allocated regions; set memory-write breakpoints on them.
- Trace or run to the decompression loop; identify its shape (tight loop, rep movs, XOR).
- Catch the tail jump to freshly written memory; verify the landing code looks like a real entry point.
- Confirm the OEP with heuristics below; fix it in Scylla.
- Dump with Scylla, rebuild imports, validate the output.
1. Breaking on allocation
Most stubs allocate the unpack buffer with VirtualAlloc or NtAllocateVirtualMemory. In x64dbg:
- Go to the Symbols tab, find VirtualAlloc in kernel32.
- Set a breakpoint; run the sample.
- On hit, check the requested size: multi-hundred-KB to MB allocations with PAGE_EXECUTE_READWRITE are the unpack buffer (small allocations are stub housekeeping).
- Note the returned address; set a memory breakpoint on write for that range.
Breakpoint hit shape:
kernel32.VirtualAlloc(0, 0x3A000, MEM_COMMIT, PAGE_EXECUTE_READWRITE)
-> returned 0x00450000 (unpack buffer)
Some stubs unpack in place (overwriting their own section); then skip allocation breakpoints and use memory-write breakpoints on the target section directly.
2. Tracing the decompression loop
Step a few iterations and recognize the pattern:
Typical loop shape:
00401240 mov al, [esi] ; read compressed byte
00401242 xor al, bl ; decode step
00401244 mov [edi], al ; write unpacked byte
00401246 inc esi / inc edi
00401248 loop 00401240 ; repeat
Once you recognize the loop, do not single-step ten thousand iterations: set a breakpoint after the loop (on the instruction following it) and run. If the loop is huge, use x64dbg's trace with a condition to skip it faster.
3. Catching the tail jump and confirming OEP
After decompression, the stub jumps to the original code. The classic
tail jump forms: jmp eax, jmp [reg], push addr; ret, call reg.
Set a breakpoint on the jump, then inspect the target:
OEP confirmation heuristics (need at least three):
- Standard prologue:
push ebp; mov ebp, esp(32-bit) or the 64-bit equivalent register setup. - First calls go to GetModuleHandleA, GetCommandLineA, or CRT startup.
- The code references the import table (calls through IAT thunks).
- Strings nearby match the unpacked program's purpose.
- In PE-bear, the RVA sits inside what should be
.text.
Confirmed OEP shape:
00401000 55 push ebp
00401001 8B EC mov ebp, esp
00401003 E8 .. .. .. .. call GetModuleHandleA
Record the OEP as an RVA: OEP_VA minus image base.
4. Dumping and rebuilding imports with Scylla
- With execution paused at the OEP, open Scylla (Plugins menu).
- Enter the OEP RVA.
- Click "IAT Autosearch": Scylla scans for the import address table.
- Click "Get Imports": review the list; valid imports show resolved names, broken ones show red.
- For broken (stolen-bytes) thunks: double-click, trace to the real API in the debugger, and correct the entry.
- Click "Dump" then "Fix Dump": Scylla writes the rebuilt executable.
Common IAT problems: the stub filled thunks with resolved addresses but Scylla misidentified the table start (try the next candidate address); some imports resolve lazily and are zero at dump time (acceptable if the program's main paths work for analysis).
5. Anti-dump and stolen-bytes handling
- PE header erasure: the stub zeroes its in-memory headers. Fix by copying the header from the packed file on disk onto the dump.
- Checksums: the stub checksums its code; software breakpoints (INT3) change bytes and trip the check. Use hardware breakpoints (DRx) which do not modify memory.
- Stolen bytes: the first bytes of imported functions are missing from the IAT thunks. Repair by finding each function's real prologue in the loaded DLL and grafting the missing bytes.
- Dual OEP / fake OEP: the first jump lands in more stub code. Keep tracing until the heuristics in section 3 pass.
6. Validation
# Strings and imports must look like a real program now
floss dump_fixed.exe | wc -l # thousands
diec dump_fixed.exe # compiler, not packer
# In Ghidra: the decompiler should produce readable functions,
# imports should resolve to named APIs
Behavior parity check: run the dump in the sandbox alongside the packed original. Same C2, same drops, same persistence means the dump is faithful. A dump that analyzes cleanly in Ghidra but crashes when executed is fine for static work; note it and move on.
Edge cases
- TLS callbacks run before the entry point: the real unpacking may happen there; break on module load and step from the TLS callback.
- Thread-local unpacking: the stub spawns a thread to unpack; follow the thread, not the main flow.
- .NET assemblies: this workflow does not apply; dump the managed assembly from memory after the CLR loads it.
- 64-bit targets: same workflow, different registers and prologues; Scylla handles both.
- Dumps larger than the original: normal when the stub reserved big zero regions; trim only if analysis tools choke.
Escalation
If the stub is virtualized (VMProtect/Themida), this workflow does not apply directly; escalate to devirtualization scoping (unpack only the functions you need).
If import reconstruction fails after genuine effort, analyze the dump dynamically (debugger with resolved imports at runtime) instead of perfecting the static file.
If the stub unpacks different payloads based on environment (conditional unpacking), dump in each relevant environment and compare.
If Scylla's IAT autosearch finds multiple candidates, test each dump's behavior; the wrong table produces a subtly broken binary.
Detection and defender view
Manual unpacking produces the payload that feeds every downstream detection: YARA rules, capa capabilities, config extraction. Document the method per sample (OEP RVA, tools, tricks encountered) so the result is reproducible and defensible. When the same custom packer appears across samples, invest once in a scripted unpacker (a Python or x64dbg script automating your manual steps); the tenth sample then takes minutes, not hours.
Key references
- x64dbg documentation: https://x64dbg.com/
- Scylla (import reconstruction): https://github.com/NtQuery/Scylla
- Ghidra documentation: https://ghidra-sre.org/
- PE-bear: https://github.com/hasherezade/pe-bear
- MITRE ATT&CK: T1027.002 (Software Packing)
- Practical Malware Analysis (Sikorski and Honig), Chapter 19 (manual unpacking walkthrough)
