Algorithm Search⌘K

Techniquecore

Opaque predicate

Also called opaque predicates.

A condition whose value the obfuscator knows but a static analyzer cannot easily find, used to add fake control flow.

An opaque predicate is a condition in a program whose value at a given point the obfuscator knows when it transforms the code, but that is hard for an analysis of the code to find. A predicate that is always true at its point is written PTP^T, one that is always false PFP^F, and one that can take either value, but where both branches do the same thing, P?P^?. Obfuscators use them to add branches that look real: one side leads to the original code, and the other side leads to bogus code that never runs. A static analysis that cannot prove the value must keep both sides. This makes the control flow graph larger and wrong, it can send a disassembler into bytes that are not code, and it hides which parts of the program matter. Collberg, Thomborson and Low introduced the term in their taxonomy of obfuscating transformations. This page shows the abstract shape on a toy graph with made-up names, and spends most of its length on how opaque predicates are detected and removed.

In plain words

Imagine a recipe that says: "If the number of days in a week is seven, stir the soup. Otherwise, add a cup of salt." The second branch never happens, and the cook knows it. But a robot that reads the recipe word by word, without knowing facts about weeks, must assume that both branches can happen. It now thinks that salt may be part of the recipe.

An opaque predicate is a test like "the week has seven days", written in a way that is hard for a program analysis to work out. The fake branch confuses the analysis. It never changes what the program does.

When it applies

What it needs. A point in the code, and a predicate whose value at that point the obfuscator can guarantee. The guarantee can come from a fact of arithmetic, from an invariant of data that the obfuscated program itself keeps, or from the order in which threads run, which is the range of constructions in Collberg, Thomborson and Low (1998). For P?P^?, both branches must have the same behavior, for example two differently obfuscated copies of the same code.

What it hides. The real control flow, because each opaque branch adds a fake edge. Code that is never run looks reachable, and the analyst must treat it as live. When the bogus side holds bytes that are not code, a disassembler that follows both sides decodes garbage.

What it does not hide. The predicate is evaluated each time it runs, so a run shows which side is taken. The bogus code never runs. An analysis that can prove the value of the predicate removes the branch.

The threat model of the protection. The defender of the code wants to slow down an analyst who has the binary. The resilience of a predicate is the effort that an automatic analysis needs to find its value. Collberg, Thomborson and Low rank predicates by this effort, which is an informal measure, not a proof.

The template

The abstract shape of an opaque branch, with named holes and the points where analysis breaks it:

  1. OPAQUE-BRANCH-SHAPE(a point between block A and block B) ▷ the shape only, with named holes
  2. 1after A, evaluate ⟨a predicate P whose value here the obfuscator knows⟩ ▷ detection: prove that P has one value at this point
  3. 2if P then go to B ▷ the real edge
  4. 3else go to ⟨a bogus block F⟩ ▷ F never runs; once P is proved, F is unreachable

The shape on a toy graph. Take a made-up function with three blocks in a row: A, then B, then C.

graph edges what a static analysis that cannot prove P sees
original A to B, B to C a straight line
with one opaque predicate PTP^T A to B (P true), A to F (P false), F to C, B to C an if-else where F looks as real as B

The bogus block F can go anywhere, for example to C, back to A, or into the middle of another block. Each choice adds a fake edge that every analysis of the graph must keep until P is proved.

Where analysis breaks it.

  • Proving arithmetic predicates. A predicate built from a fact of arithmetic is a formula over machine words. An SMT solver with the theory of bit vectors decides whether it is always true. A classic textbook example is that x(x+1)x(x + 1) is even for every integer xx. This also holds for words of ww bits, because the remainder modulo 2 does not change when the product wraps modulo 2w2^w. The solver must work on machine words, not on mathematical integers: some facts of integers are false for words (see the exercises).
  • Abstract interpretation. Dalla Preda, Madou, De Bosschere and Giacobazzi (2006) detected numerical opaque predicates with abstract interpretation, and showed that the domain detects a predicate exactly when it is precise enough for the operations in it. This is a static method that needs no run.
  • Traces and a theorem prover. LOOP (Ming, Xu, Wang and Wu, 2015) takes a run, finds the conditional branches in it, and asks a theorem prover, through symbolic execution of the trace, whether each condition is constant. It also handles predicates whose value depends on earlier predicates in the trace.
  • Predicates based on aliasing. The strongest constructions of Collberg, Thomborson and Low use invariants of pointer structures that the obfuscated program builds and updates. Proving such a predicate needs a precise alias analysis, and precise alias analysis is undecidable in general, an instance of Rice's theorem. Here, dynamic analysis is the practical answer: the bogus side never runs.
  • Removal. Once P is proved, replace the branch by a jump to B, delete F if nothing else reaches it, and run recursive traversal and structuring again.

Classic examples

  • Fake edges for flattening. Control-flow flattening often adds bogus states, guarded by opaque predicates, so that the dispatcher seems to reach more blocks than it does.
  • Junk bytes against disassembly. A PTP^T branch whose false side points into bytes that are not code makes a disassembler that follows both sides decode garbage, and the garbage can overlap real instructions. The overlap check of recursive traversal shows the conflict. Resolving it needs the value of the predicate.
  • Copies of code behind P?P^?. Two differently transformed copies of the same block behind a P?P^? predicate double the code that an analyst must read, while either copy is correct.
  • Inside virtualization obfuscation and metamorphic code. Opaque predicates appear in the handlers of interpreters and in mutated code, and the same detection methods apply.

Variations

  • By value: always true, always false, or either way with equivalent branches.
  • By the source of the guarantee: facts of arithmetic, invariants of data structures that the program maintains, the values of global data that only the program sets, or the scheduling of threads.
  • By context: a predicate that is constant only at its point in the program, because of the values that reach it there, is harder to detect than one that is constant for every input. Ming and coauthors (2015) treat these contextual cases and predicates that depend on each other.

The theoretical limit. Barak and coauthors (CRYPTO 2001; JACM 2012) proved that no compiler can make every program into a virtual black box, that is, a program from which an efficient adversary learns nothing beyond what black-box access to its function gives. Opaque predicates add confusion without a proof of security, and no general proof of that strong kind is possible.

Pitfalls

  • A branch removed because it never ran. A branch that did not run in the tests may still run on other inputs. Remove a branch only when its predicate is proved constant, and keep the evidence of the proof.
  • Proofs over integers. A fact of mathematical integers can be false for machine words, because arithmetic wraps. Prove opaque predicates over bit vectors of the real width.
  • Disassembly before detection. Following both sides of every branch decodes bogus bytes as code. Detect and remove opaque branches first, then disassemble the rest again.
  • Obfuscation taken as security. An opaque predicate slows down analysis and does not stop it. Do not rely on it to protect a secret.

Exercises

  1. Prove that x(x+1)x(x + 1) is even for every integer xx, and that the same holds when the product is computed modulo 2w2^w for any w≥1w \ge 1.
  2. In 32-bit two's complement arithmetic, find an xx for which the computed value of x⋅xx \cdot x is negative. Conclude that x2≥0x^2 \ge 0 is not a valid predicate for machine words. One answer is x=46341x = 46341, since 463412=2147488281>231−146341^2 = 2147488281 > 2^{31} - 1.
  3. Explain why a P?P^? predicate keeps the behavior of the program only if its two branches are equivalent, and why a dynamic analysis cannot tell a P?P^? predicate from a real branch.

References

  1. Christian Collberg, Clark Thomborson and Douglas Low, Manufacturing cheap, resilient, and stealthy opaque constructs, ACM Symposium on Principles of Programming Languages (POPL) 1998, pages 184 to 196, the definition of opaque predicates, their use to add fake control flow, and opaque constructs based on aliasing in dynamic data structures and on concurrency
  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 obfuscating transformations, the always true, always false and either way predicates, and the measures of potency, resilience, stealth and cost
  3. Mila Dalla Preda, Matias Madou, Koen De Bosschere and Roberto Giacobazzi, Opaque predicates detection by abstract interpretation, Algebraic Methodology and Software Technology (AMAST) 2006, the detection of numerical opaque predicates by abstract interpretation, and its relation to the completeness of the abstract domain
  4. Jiang Ming, Dongpeng Xu, Li Wang and Dinghao Wu, LOOP: logic-oriented opaque predicate detection in obfuscated binary code, ACM Conference on Computer and Communications Security (CCS) 2015, the detection of opaque predicates in execution traces by symbolic execution and a theorem prover, including predicates that depend on each other
  5. Christian Collberg and Jasvir Nagra, Surreptitious Software: Obfuscation, Watermarking, and Tamperproofing for Software Protection, Addison-Wesley, 2009, the chapters on opaque predicates, 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