Algorithm Search⌘K

Techniqueadvanced

Virtualization obfuscation

Also called VM-based obfuscation.

Translates code into bytecode for a random custom virtual machine and ships the interpreter, so analysts must first decode the machine.

Virtualization obfuscation protects a function by translating it into the bytecode of a custom instruction set, and replacing it with an interpreter for that instruction set together with the bytecode. The function still does the same thing, but the native code that runs is only the interpreter: a loop that fetches the next bytecode instruction at a virtual program counter, decodes it, and jumps to a handler that carries it out. The instruction set is made for the protected program, and often differs between builds, so the analyst cannot use knowledge of a known machine. A disassembler shows the interpreter, not the function, and the original instructions never appear in memory, so generic unpacking does not recover them. It is the strongest step of the line that starts with control-flow flattening: there, the dispatcher picks blocks of native code; here, it picks handlers, and the blocks themselves are bytecode. Protections of commercial software and some malware use it. This page shows the abstract shape on a toy graph with made-up names, and spends most of its length on how the interpreter is analyzed and the original code recovered.

In plain words

Suppose a recipe is written in a private shorthand that only one cook knows, and the cook has a small card that explains each sign of the shorthand. If you watch the cook, you see someone who reads a sign, looks it up on the card, does one small task, and reads the next sign. Watching tells you how the card works, but the recipe itself stays in shorthand.

To read the recipe, you first learn the card: which sign means "stir", which means "add salt". Then you translate the shorthand back into plain words. Virtualization obfuscation makes the analyst do the same thing with code: first understand the custom machine, then translate the bytecode back.

When it applies

What it needs. A function to protect, a compiler from its code to the bytecode of ⟨a custom instruction set⟩, and an interpreter for that instruction set that is shipped with the program. The interpreter must reproduce the effect of the original code on registers and memory, so it keeps a virtual program counter, virtual registers or a virtual stack, and a handler for each kind of bytecode instruction.

What it hides. The native instructions and the control flow of the protected function. The control flow graph of the native code is the graph of the interpreter, which looks the same for every protected function. The branches of the function are now changes of the virtual program counter, held in data.

What it does not hide. The behavior. Every effect that the function has on memory, on the system or on its outputs still happens, through the handlers. A run records each handler that runs and each value that it reads and writes. The interpreter itself is ordinary native code with a distinctive shape, which analysts can find and study.

The cost. Each bytecode instruction costs a fetch, a decode and a dispatch, so protected code runs much slower than the original. In practice, only small parts of a program, such as checks of licenses or keys, are protected this way.

The threat model of the protection. The defender of the code wants to raise the effort of an analyst who has the binary and can run it. No proof says how much the effort rises, and the result of Barak and coauthors below shows that no general proof of the strongest kind is possible.

The template

The abstract shape of a function after virtualization, with named holes and the points where deobfuscation breaks it:

  1. VIRTUALIZED-SHAPE(a function f) ▷ the shape only, with named holes
  2. 1bytecode ← ⟨f translated to a custom instruction set⟩, stored as data in the program
  3. 2vpc ← the start of the bytecode; set up ⟨the virtual registers or stack⟩
  4. 3loop
  5. 4fetch the instruction at vpc and decode it by ⟨the encoding⟩ ▷ detection: a loop that reads data at one pointer and dispatches on it
  6. 5go to the ⟨handler⟩ for its kind and run it ▷ deobfuscation: learn the effect of each handler once
  7. 6vpc ← the next instruction, or the target of a virtual branch ▷ deobfuscation: rebuild the virtual control flow from the values of vpc
  8. 7until the handler for the exit runs

The shape on a toy graph. Take the same made-up function as on the flattening page. Block P tests a condition c. If c is true it goes to Q, else to R. Q goes to R, and R returns.

form what runs as native code where the edges P to Q, P to R and Q to R are
original the instructions of P, Q and R branch instructions in the native code
flattened P, Q and R, under one dispatcher values of a state variable, set at the end of each block
virtualized the interpreter only changes of vpc made by the handler of a virtual branch, and the targets stored in the bytecode

In the virtualized form, each of P, Q and R is a short run of bytecode instructions. The bytecode of P ends with a virtual conditional branch, whose two targets are the start of Q and the start of R in the bytecode. Rebuilding the graph of P, Q and R means finding these targets, which needs the meaning of the handler for virtual branches and the encoding of its operands.

Where deobfuscation breaks it.

  • Find the interpreter (line L4). The fetch, decode and dispatch loop has a clear shape: a loop that loads from memory at a pointer, uses the value to select a target, and advances the pointer. Sharif, Lanzi, Giffin and Lee (2009) found the virtual program counter and the dispatch loop in dynamic traces from this shape, and then recovered the syntax of the bytecode and the meaning of each kind of instruction.
  • Learn each handler once (line L5). The interpreter has a fixed, small set of handlers. Rolles (2009) described a static method: analyze each handler, write its effect on the virtual state as a simple instruction, and then translate the whole bytecode with this table. The translation can be optimized again, which removes much of the overhead the interpreter added.
  • Rebuild the virtual control flow (line L6). Once the handlers are known, the virtual branches give the edges between bytecode blocks. Recursive traversal over the bytecode, with the virtual branch targets as successors, gives the control flow graph of the original function.
  • Skip the model of the machine. Coogan, Lu and Debray (2011) did not reconstruct the interpreter. From a trace, they kept the instructions that affect the observable behavior, such as calls to the system and the values that flow into them, and removed the rest, which is mostly the work of the interpreter.
  • Simplify traces in general. Yadegari, Johannesmeyer, Whitely and Debray (2015) used taint analysis to find the instructions that depend on the inputs, simplified the trace by transformations that keep its meaning, and built a control flow graph from the result. The method does not depend on the kind of obfuscation, and it also applies to code that combines virtualization with other transformations. Symbolic execution of the handlers or of the trace is a common tool in all of these methods.

Classic examples

  • The step from flattening. An interpreter is a dispatcher whose state is a virtual program counter. Flattening hides the edges in the values of a variable. Virtualization also hides the statements, because the blocks are bytecode. Methods that recover the state values of a flattened function carry over to the virtual program counter.
  • Interpreters with obfuscated handlers. The handlers themselves can be obfuscated, for example with opaque predicates or with several different handlers for the same kind of instruction. Learning each handler then takes more work, but the set of handlers is still finite.
  • Several layers. The interpreter can itself be virtualized, so that one custom machine runs another. Each layer multiplies the cost of a run, and deobfuscation removes one layer at a time.
  • Software that differs in each build. A new instruction set and a new encoding for each build mean that a table of handlers learned for one sample does not apply to the next. This is the same idea as metamorphic code, applied to the machine rather than to the instructions. Methods based on traces and on meaning do not depend on the encoding, which is why they are preferred here.
  • Similarity of protected code. Functions protected this way all look like the same interpreter, so methods of binary code similarity that compare the shape of the graph fail on them until the bytecode is translated back.

Variations

  • The machine. A stack machine or a register machine; a fixed encoding or one that changes with position; one large handler for each kind or many small ones.
  • The dispatch. A table of handler addresses indexed by the opcode, a chain of comparisons, or handlers that end by jumping directly to the next handler, with no central loop. The last form is harder to detect because there is no single dispatch point.
  • What is protected. One function, a set of chosen functions, or most of the program, at a cost in speed that grows with the share of code that is protected.
  • Combination. Virtualization together with packing, flattening and opaque predicates. Deobfuscation must then undo each layer in turn, usually from the outside in.

The theoretical limit. Barak, Goldreich, Impagliazzo, Rudich, Sahai, Vadhan and Yang (CRYPTO 2001; JACM 2012) defined a virtual black box obfuscator, a compiler whose output reveals nothing beyond what black-box access to the function gives, and proved that no compiler meets this definition for all programs. Virtualization obfuscation, like every practical obfuscation, therefore has no general security proof of this kind. Its effect is measured in the effort of analysts and tools, and the work cited above shows that this effort can be reduced by automatic methods.

Pitfalls

  • Unpacking taken as enough. The write-then-execute rule of generic unpacking finds at most the interpreter. The bytecode is only read as data, and the original code never runs. Detect an interpreter and use the methods of this page.
  • A trace taken as the whole function. Methods based on traces recover the paths that ran. Branches of the bytecode that the run did not take are missing, so combine traces from several inputs, or use the static translation of the bytecode with the learned handlers.
  • The interpreter mistaken for the function. Similarity, signatures and structuring applied to the native code describe the interpreter, which is shared by all protected functions. Results about it say nothing about what the protected code does.
  • Virtualization taken as security. It raises the cost of analysis, and analysts and tools have repeatedly recovered protected code. Do not rely on it to keep a secret, such as a key, in code that an adversary can run.

Exercises

  1. For the toy graph above, write the bytecode of P, Q and R as a list of abstract virtual instructions of three kinds: run the statements of a block, branch on c to one of two bytecode positions, and return. Then show how the edges of the original graph can be read from the targets of the virtual branches.
  2. Show that if an interpreter has hh handlers, and the effect of each handler on the virtual state is known, then a bytecode of nn instructions can be translated back in O(n)O(n) steps, given that each instruction can be decoded in constant time.
  3. Explain why the write-then-execute rule of generic unpacking never marks the original code of a virtualized function. What does it mark if the interpreter is packed, and what if it is not?

References

  1. Rolf Rolles, Unpacking virtualization obfuscators, USENIX Workshop on Offensive Technologies (WOOT) 2009, the structure of interpreter-based protections, and the static method that learns the meaning of each handler and translates the bytecode back to native code
  2. Monirul Sharif, Andrea Lanzi, Jonathon Giffin and Wenke Lee, Automatic reverse engineering of malware emulators, IEEE Symposium on Security and Privacy 2009, the identification of the virtual program counter and the fetch, decode and dispatch loop from dynamic traces, and the recovery of the syntax and meaning of the bytecode
  3. Kevin Coogan, Gen Lu and Saumya Debray, Deobfuscation of virtualization-obfuscated software: a semantics-based approach, ACM Conference on Computer and Communications Security (CCS) 2011, the selection of the instructions in a trace that affect the observable behavior, without a model of the interpreter
  4. Babak Yadegari, Brian Johannesmeyer, Benjamin Whitely and Saumya Debray, A generic approach to automatic deobfuscation of executable code, IEEE Symposium on Security and Privacy 2015, the simplification of execution traces with taint analysis and semantics-preserving transformations, and the construction of a control flow graph from the result
  5. Christian Collberg and Jasvir Nagra, Surreptitious Software: Obfuscation, Watermarking, and Tamperproofing for Software Protection, Addison-Wesley, 2009, the chapters on interpretation as an obfuscation, on control flow obfuscation and on deobfuscation
  6. Boaz Barak, Oded Goldreich, Russell Impagliazzo, Steven Rudich, Amit Sahai, Salil Vadhan and Ke Yang, On the (im)possibility of obfuscating programs, Journal of the ACM 59(2), 2012, article 6 (conference version CRYPTO 2001, LNCS 2139, pages 1 to 18), the virtual black box definition of obfuscation and the proof that no general obfuscator meets it

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.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW3 standard
  • five exercises