Algorithm Search⌘K

Techniquecore

Control-flow flattening

Also called chenxification, dispatcher-based obfuscation.

Puts all basic blocks of a function under one dispatcher loop that picks the next block by a state variable, which hides the control flow.

Control-flow flattening is a code obfuscation that removes the structure of a function's control flow graph. All basic blocks of the function are placed at the same level, under one loop with a dispatcher. A state variable holds the code of the next block. Each block runs its own statements, sets the state variable, and goes back to the dispatcher, which jumps to the block whose code matches the variable. The behavior of the function does not change. In the graph of the result, every block has the dispatcher as its only predecessor and its only successor, so the loops, branches and order of the original are no longer visible in the edges. They are hidden in the values of a variable. Flattening is used for software protection and in malware, and analysts meet it in reverse engineering. This page shows the abstract shape of the transformation on a three-block toy graph with made-up names, as in the taxonomy of Collberg, Thomborson and Low. Most of the page is on how the flow is recovered, how flattening is detected, and what theory says about obfuscation in general.

In plain words

A normal program is like a path through a building with signs on the doors: from the hall, go to the kitchen or to the office; from the kitchen, go to the office. You can draw a map from the signs alone.

Flattening removes all the signs. Every door now leads back to one central room. In the central room, a clerk reads a note that you carry and sends you to the next room. Each room gives you a new note before you leave. The route is the same as before, but a map of the doors shows only "every room connects to the central room". To learn the real route, you must read the notes.

When it applies

What it needs. Any function with a control flow graph can be flattened. Flattening is the normal form of Böhm and Jacopini, one loop with a variable that holds the next step (see control flow structuring). So the transformation always exists and always keeps the behavior.

What it hides. Many static analyses read the structure of a program from the edges of its graph: the recovery of loops and if statements, dominators, matching of control flow graphs between binaries, and signatures based on the shape of the graph. After flattening, these analyses see one loop with a switch, and every function looks much the same.

What it does not hide. The statements of each block are unchanged. The transitions are still in the code, as the values that each block gives the state variable. An analysis that tracks those values, or a run that records the order of the blocks, recovers the original edges. So flattening raises the cost of analysis. It does not prevent it.

The threat model of the protection. The defender of the code wants to slow down an analyst who has the binary and can run it. The analyst wants the original control flow. No proof says how much flattening slows the analyst: the impossibility result below shows that no transformation can give the strong, general guarantee that the name "obfuscation" suggests.

The template

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

  1. FLATTENED-SHAPE(a function with the blocks B1, ..., Bk) ▷ the shape only, with named holes
  2. 1s ← ⟨the code of the entry block⟩
  3. 2loop
  4. 3go to the block Bi whose ⟨code⟩ equals s, through ⟨the dispatcher⟩ ▷ detection: one block that dominates all others and follows nearly all of them
  5. 4run the statements of Bi
  6. 5s ← ⟨the code of the next block⟩, chosen by the condition of Bi ▷ deobfuscation: find the possible values of s at the end of Bi
  7. 6until Bi was an exit block

The shape on a toy graph. Take a made-up function with three blocks. Block P runs first and tests a condition c. If c is true it goes to Q, else to R. Q goes to R, and R returns.

graph edges what a reader of the edges learns
original P to Q, P to R, Q to R, R returns an if-then: P, then Q if c, then R
flattened D to P, D to Q, D to R, P to D, Q to D, R returns one loop through a dispatcher D

In the flattened form, each block ends by setting the state variable:

block statements the value given to s at the end
P the statements of P ⟨code of Q⟩ if c is true, else ⟨code of R⟩
Q the statements of Q ⟨code of R⟩
R the statements of R none, R returns

The second table holds the whole original graph. Each row says which blocks can follow, and under which condition. Deobfuscation is the work of rebuilding this table from the code.

Where deobfuscation breaks it.

  • Constant state values (line L5). In the plain form, each block gives ss a constant, or one of two constants chosen by its condition. Constant propagation inside each block, as in abstract interpretation, finds these values. The edges of the original graph follow, and control flow structuring can then rebuild the loops and if statements.
  • Computed state values. If the next value of ss is computed from earlier values, from memory or through pointers, an analysis must track it across blocks. Symbolic execution of each block, with the value of ss at the start as a symbol, gives the value at the end as a formula, and a solver finds which block codes it can take. Wang (2000) argued that when the value is computed through pointers, recovering it is as hard as precise alias analysis, which is hard in general. This is an argument about the worst case, not a measure of how hard real flattened code is.
  • Dynamic analysis. A run records the sequence of blocks that it visits. Each pair of consecutive blocks in the record is a real edge. This is exact for the paths that run and says nothing about the others. Udupa, Debray and Madou (2005) studied both the static and the dynamic approach on flattened code.
  • Detection (line L3). The dispatcher has a distinctive shape: one block that dominates all others, that is the successor of almost all of them, and that compares one variable with many constants. Detectors look for this shape.

Classic examples

  • Wang's thesis (2000). The transformation was described as part of a protection of software against analysis, with the argument that aliasing of the state variable makes static analysis hard. The alias "chenxification" refers to Chenxi Wang.
  • The taxonomy of Collberg, Thomborson and Low (1997). Their report classifies control transformations by what they change and measures each one by potency (how much harder it is for a person), resilience (how hard it is to undo automatically), stealth (how much the result stands out) and cost. Flattening has high potency and a clear cost: one dispatch for each block that runs.
  • Combination with opaque predicates. Flattened code often adds fake blocks that the dispatcher never reaches, selected by conditions whose value the obfuscator knows. Deobfuscation then needs both the analysis of the state and the detection of opaque predicates.
  • The step to virtualization obfuscation. An interpreter is a dispatcher whose state is a virtual program counter. Virtualization goes one step further than flattening: it also replaces the statements of each block by bytecode.
  • Code similarity and detection. Flattened functions defeat methods that compare the shape of control flow graphs, which is one reason why metamorphic code and protected code need semantic methods.

Variations

  • The dispatcher. A chain of comparisons, a jump table, or a tree of comparisons. Each gives a different shape to detect.
  • The encoding of the state. Plain block numbers, or values transformed by a function, so that the constants in the code do not show the block numbers directly.
  • Several dispatchers. One dispatcher for each part of the function, or a hierarchy of dispatchers.
  • Partial flattening. Only some blocks, or only some functions, are flattened, to limit the cost.

The theoretical limit. Barak, Goldreich, Impagliazzo, Rudich, Sahai, Vadhan and Yang (CRYPTO 2001; JACM 2012) defined a virtual black box obfuscator: a compiler such that anything an efficient adversary can compute from the obfuscated program, it could also compute from black-box access to the function alone. They proved that no compiler meets this definition for all programs: there are families of functions whose obfuscated code always reveals a property that black-box access hides. So flattening, like every practical obfuscation, has no general security proof of this kind. Barak and coauthors also defined the weaker notion of indistinguishability obfuscation, which later work built from cryptographic assumptions. That line of work is far from the transformations on this page.

Pitfalls

  • Flattening taken as security. A flattened function hides nothing that a careful analysis cannot recover. It raises the cost of analysis by a factor that no one has proved. Do not use it to protect a secret, such as a key, in code.
  • The graph of flattened code read as the real structure. Structuring, similarity and graph signatures applied directly to flattened code give results about the dispatcher, not about the function. Detect flattening first and rebuild the edges.
  • A dynamic trace taken as the whole graph. The edges seen in a run are real, but the edges that did not run are missing. Combine the trace with a static analysis of the state values.
  • The cost of the dispatcher forgotten. Every block that runs pays for one dispatch, and the processor can no longer predict the branches well. Measure the slowdown on the real workload before flattening code that runs often.

Exercises

  1. For the toy graph above, count the edges before and after flattening. Then show that any function with kk blocks and ee edges between them becomes a flattened function with at most 2k2k edges, whatever ee was.
  2. Show that one state variable that takes k+1k + 1 values is enough to flatten any function with kk blocks.
  3. Suppose that every block sets ss to a constant, or to one of two constants chosen by its condition. Show that constant propagation inside each block finds the exact successors of every block in the original graph.

References

  1. Chenxi Wang, A security architecture for survivability mechanisms, PhD thesis, University of Virginia, 2000, the transformation of a function into a dispatcher over a state variable, and the argument that aliasing makes its static analysis hard
  2. Christian Collberg, Clark Thomborson and Douglas Low, A taxonomy of obfuscating transformations, Technical Report 148, Department of Computer Science, University of Auckland, 1997, the classes of control transformations, and the measures of potency, resilience, stealth and cost
  3. Sharath K. Udupa, Saumya K. Debray and Matias Madou, Deobfuscation: reverse engineering obfuscated code, Working Conference on Reverse Engineering (WCRE) 2005, the static and dynamic analyses that recover the original control flow of flattened code
  4. Christian Collberg and Jasvir Nagra, Surreptitious Software: Obfuscation, Watermarking, and Tamperproofing for Software Protection, Addison-Wesley, 2009, the chapters on control flow obfuscation and on deobfuscation
  5. 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

Where this leads

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