Techniqueadvanced
Generic unpacking
Also called write-then-execute unpacking.
Runs a packed program under monitoring and dumps memory when it executes a page it wrote, which reveals the unpacked code.
Generic unpacking recovers the hidden code of a packed program without knowing which packer made it. A packer compresses or encrypts the code of a program and adds a routine that restores the code in memory at run time. A static scanner that reads the file sees only the restore routine and data that looks random. Generic unpacking runs the program in an isolated, monitored environment and waits for the restored code to appear. The common rule is write then execute: if the program runs code from memory that it wrote during the same run, that memory holds generated code, and the analyst copies it out. The copy can then be scanned with signatures, disassembled or compared with known samples. This page is about the analysis method and its limits. It gives the stages at the level of the published papers and no tool. Because generic unpacking runs code that may be malicious, it is only done in an isolated environment that is built for that purpose.
In plain words
Imagine a letter written in a secret code, together with a small note that says how to decode it. You cannot read the letter, but you can watch someone follow the note. At some point they write the decoded text on a sheet of paper and start to read it aloud. That is the moment to take a photo of the sheet.
A packed program works the same way. It writes its real code into memory and then jumps to it. The analysis watches every write and every jump. When the program jumps into memory that it wrote itself, the analysis copies that memory. The copy is the hidden code, ready to study.
When it applies
The threat model. The analyst is a defender who holds a sample and wants to know what its code does. The author of the sample wants to hide the code from static analysis, so the file holds only packed data and a restore routine. The analyst can run the sample, but only in an isolated environment, such as an emulator or a virtual machine with no access to real networks or data, and can observe its memory and the instructions it runs.
The conditions the method needs, from the published work:
- The hidden code must be in memory, in a form that the processor runs, at the moment it runs. This is the observation that all write-then-execute methods use: whatever the packer does, the processor can only run real instructions.
- The run must reach the restored code. If the sample restores its code only under some condition that the run does not meet, nothing is copied.
- The environment must see every write and every execution in the part of memory that matters, at the granularity it tracks: each byte, each instruction or each page.
- For the copy to be complete, the code must be restored as a whole before it runs, or in a few layers. A packer that restores only a small part at a time, and hides it again after use, never leaves the whole program in memory.
It does not apply to virtualization obfuscation, where the original code is translated into the bytecode of a custom interpreter. There, the code that runs is the interpreter, and the original instructions never appear in memory.
The template
The stages of the published methods, at the level of their abstracts, with the holes that differ between them:
GENERIC-UNPACKING(sample) ▷ the stages only, no parameters- 1
run the sample in an isolated environment that observes its memory writes and the code it runs - 2
record each part of memory that the sample writes during the run - 3
when the sample runs code from a part it wrote, mark that part as restored code ▷ the write-then-execute rule - 4
copy the marked part out for analysis, and keep watching for ⟨a next layer⟩ - 5
stop at ⟨a stopping rule⟩ and return the copies for scanning and disassembly
The holes are the granularity of line L2 and the stopping rule of line L5. Renovo records the written bytes in a shadow record inside an emulator, so it sees each instruction. OmniUnpack works with pages and the memory protection of the system, which is faster but coarser. Its stopping rule is the first action of the sample that can cause harm: at that point it calls a malware detector on the pages that were written and then run. PolyUnpack uses a different rule in line L3: it first builds a static model of the code in the file, then runs the sample step by step, and marks code that runs but is not in the static model.
An abstract model and what the rule guarantees
Take a made-up memory with parts R1 to R4. R1 holds the restore routine from the file. A run is a list of events: , the sample writes part , and , the sample runs code in .
| step | event | parts written so far | action of the rule |
|---|---|---|---|
| 1 | x(R1) | none | R1 comes from the file, nothing to mark |
| 2 | w(R3) | R3 | record the write |
| 3 | w(R3) | R3 | record the write |
| 4 | x(R1) | R3 | nothing to mark |
| 5 | x(R3) | R3 | mark R3 and copy it, layer 1 |
| 6 | w(R4) | R3, R4 | record the write |
| 7 | x(R4) | R3, R4 | mark R4 and copy it, layer 2 |
Claim. In this model, suppose that only the sample changes memory during the run, and that the environment sees every event. If code runs from a part whose content at that time differs from its content in the file, then the rule marks that part at or before that event.
Proof. The content of a part differs from the file only if some write changed it, and by the first assumption that write is an event of the sample, which comes before the event . The environment sees both events, so at the part is in the written set and line L3 marks it.
The claim is about coverage in the model, not about the success of an analysis. The copy of line L4 is taken at one moment. If the sample writes the same part again later, the copy is out of date, which is why line L4 keeps watching. The rule can also mark a part that the sample only copied from the file without change, which is harmless.
Classic examples
- PolyUnpack, Royal and coauthors (2006). The static model and the step-by-step run show which code was not in the file. The method needs no knowledge of the packer, which is the meaning of "generic".
- Renovo, Kang, Poosankam and Yin (2007). An emulator keeps a shadow memory of the bytes that the sample wrote. When the program counter enters such bytes, the written region is extracted, and the record is reset to find the next layer. The shadow record is the same idea as the marks of taint analysis, with "written by the sample" as the taint.
- OmniUnpack, Martignoni, Christodorescu and Jha (2007). The pages that the sample writes are tracked with the page protection of the processor, the same mechanism as data execution prevention, so the cost is low enough to run on an end host. A malware detector scans the written and run pages before the sample makes an action that can cause harm.
- Packer detection first. A fast entropy test picks the files that are probably packed, and only these go to the slower unpacking stage.
- Analysis after unpacking. The copies feed signature matching and classifiers, static disassembly, and behavior clustering of families.
Variations
- The granularity. Byte or instruction tracking in an emulator is exact and slow. Page tracking with memory protection is fast and coarse: a page that holds both data and restored code is marked as a whole.
- The environment. A full system emulator, a virtual machine monitor that uses the virtualization support of the processor, or a monitor on the host. The more the environment differs from a normal system, the easier it is for a sample to notice it and change its behavior, and the more of the system it can observe.
- The stopping rule. Stop at the first run of written code, after a fixed number of layers, before an action that can cause harm, or when the sample stops making progress.
- Packer complexity. Ugarte-Pedrero and coauthors (2015) classified packers by how they restore code: all at once, in several layers in order, in layers that call back into earlier layers, or with restored parts that are mixed with the restore code or that appear only one fragment at a time. They found that the simple classes were the most common in their data, and that the complex classes defeat a single dump.
Pitfalls
- Running a sample outside isolation. The method runs untrusted code. Run it only in an environment that has no access to real networks, real data or other machines, and that can be reset after each run.
- One copy taken as the whole program. A packer with several layers, or one that restores code in parts, leaves only part of the program in memory at any time. Keep watching after the first copy, and report which parts were never seen.
- A copy taken as a runnable file. The copied memory is not a valid executable. The entry point, the table of imported functions and the section layout must be rebuilt before some tools can read it. Use the copy as input for analysis, not as a program.
- No copy taken as "not packed". The sample may not restore its code in the run: it may need an input, a date or a condition of the system that the environment does not give. A run without restored code shows only that this run did not restore code.
- Code that runs only through an interpreter. For a sample protected by virtualization obfuscation, the rule finds the interpreter and the bytecode, not the original code. Use the deobfuscation methods of that page.
References
- Paul Royal, Mitch Halpin, David Dagon, Robert Edmonds and Wenke Lee, PolyUnpack: automating the hidden-code extraction of unpack-executing malware, Annual Computer Security Applications Conference (ACSAC) 2006, pages 289 to 300, the static model of the file, the step-by-step execution that compares each run instruction with it, and the evaluation on packed samples
- Min Gyung Kang, Pongsin Poosankam and Heng Yin, Renovo: a hidden code extractor for packed executables, ACM Workshop on Recurring Malcode (WORM) 2007, pages 46 to 53, the shadow record of written memory in an emulator, the extraction of code when it runs from written memory, and the handling of several layers
- Lorenzo Martignoni, Mihai Christodorescu and Somesh Jha, OmniUnpack: fast, generic, and safe unpacking of malware, Annual Computer Security Applications Conference (ACSAC) 2007, pages 431 to 441, page-level tracking of written and executed memory, and the call of a malware detector before actions that can cause harm
- Xabier Ugarte-Pedrero, Davide Balzarotti, Igor Santos and Pablo G. Bringas, SoK: Deep packer inspection: a longitudinal study of the complexity of run-time packers, IEEE Symposium on Security and Privacy 2015, the classes of packers by the way they restore code, and the limits of unpacking by a single dump
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W3 standard |
|
Tag V59723 Edit this page History Report an error Markdown