DEV Community

Zero Heartbeat
Zero Heartbeat

Posted on Edited on Originally published at delta1labs.com

Hiding the integer math that gates your software

Renaming attacks what a thing is called. String and constant encryption attack the literals it carries. But the single most valuable thing in a lot of .NET code is none of those — it is a small piece of arithmetic: the expression that turns a tier into a seat limit, folds a device fingerprint into a check digit, or decides whether bit 0x40 of an entitlement flag is set. That math survives compilation almost perfectly, and a decompiler hands it back as clean, readable C#. Mixed boolean-arithmetic (MBA) is the pass that takes it away.

This is a deep explainer: what MBA actually is, the algebraic identities behind it, what the rewritten IL looks like, why it specifically defeats the two tools an attacker reaches for — a constant folder and a symbolic-execution engine — and what you pay for it.

The problem: arithmetic is self-documenting

Here is a method that computes how many seats a licence grants. Legitimate IP: it is your pricing logic, and you would rather a competitor not read it off a decompile.

int SeatLimit(int tier, int flags)
{
    int baseSeats = tier * 5 + 10;
    if ((flags & 0x40) != 0)      // the "enterprise" bit
        baseSeats = baseSeats * 2;
    return baseSeats;
}
Enter fullscreen mode Exit fullscreen mode

Decompile the Release build and you get that back verbatim — the * 5, the + 10, the & 0x40, all of it. Renaming turns SeatLimit into a and tier into b, but the expression is untouched, and the expression is the secret. An attacker does not need the names. They need the formula, and the formula is sitting in plain sight.

The idea: replace an operation with an equal, uglier one

MBA rests on a family of identities that are true for every machine integer, mixing boolean operators (&, |, ^, ~) with arithmetic ones (+, -, *). A few of the workhorses:

  • a + b == (a | b) + (a & b)
  • a + b == (a ^ b) + 2 * (a & b)
  • a ^ b == (a | b) - (a & b)
  • a & b == (a | b) - (a ^ b)

Each holds for all of int, including overflow wraparound, because ^ is addition-without-carry and a & b shifted left is the carry. Nebula picks an identity per operator site, then recurses: the (a & b) it just introduced can itself be rewritten, and the constants can be split — 0x40 becomes 0x5A ^ 0x1A (both of which really do XOR to 0x40), and 10 becomes a small opaque sum. After two or three levels a one-token add is a bushy tree of ANDs, ORs, XORs and adds with no literal left to recognise.

Run through the first identity, the seat computation comes back from a decompiler looking like this:

int SeatLimit(int tier, int flags)
{
    int baseSeats = ((tier << 2) | tier) + ((tier << 2) & tier) + ((0x5A ^ 0x1A) - (10 & (0x5A ^ 0x1A)));
    if (((flags | (0x5A ^ 0x1A)) - (flags ^ (0x5A ^ 0x1A))) != 0)
        baseSeats = (baseSeats | baseSeats) + (baseSeats & baseSeats);
    return baseSeats;
}
Enter fullscreen mode Exit fullscreen mode

Every line still computes exactly the original value. But the * 5 is gone (rewritten as (tier << 2) + tier), the + 10 is buried, the & 0x40 is a subtraction of two OR/XOR terms over a constant that is itself a XOR, and the * 2 is an MBA add. Nothing here reads as pricing logic any more.

At the IL level

The whole point is that the rewrite happens in IL, so there is no source form to protect and the JIT still produces correct code. Original baseSeats = baseSeats * 2 is one multiply:

IL_0012: ldloc.0        // baseSeats
IL_0013: ldc.i4.2
IL_0014: mul
IL_0015: stloc.0
Enter fullscreen mode Exit fullscreen mode

After the MBA add identity a + a == (a | a) + (a & a), the same store is:

IL_0012: ldloc.0        // baseSeats
IL_0013: ldloc.0
IL_0014: or             // (a | a)
IL_0015: ldloc.0
IL_0016: ldloc.0
IL_0017: and            // (a & a)
IL_0018: add            // (a|a) + (a&a)  ==  2*a
IL_0019: stloc.0
Enter fullscreen mode Exit fullscreen mode

Four extra instructions, a provably identical result, and a tree with no multiply and no literal 2 for a pattern matcher to anchor on. The (flags & 0x40) test expands the same way, with 0x40 arriving only as the XOR of two other constants so there is no 0x40 in the metadata #US/blob heaps or the IL stream to grep for.

An MBA rewrite turns one add instruction into an equivalent bitwise-arithmetic tree a solver must normalise

Why a constant folder and a symbolic engine both stall

Two tools do the heavy lifting in a deobfuscation workflow, and MBA is designed against both.

A constant folder has nothing constant to fold

Generic deobfuscators lean on a constant-propagation pass: find a literal, push it through the arithmetic, simplify. MBA leaves no literal in the operation — 0x40 never appears, only 0x5A ^ 0x1A, and even that can be lifted to a computed value. There is no mul, no 2, no 0x40 sitting in the stream to seed the fold. The pass runs and changes nothing, because its precondition — a known value entering an operation — is never met.

A symbolic engine faces an equivalence it must prove, not see

The stronger attack is symbolic execution or an SMT solver: treat a and b as free variables, build the expression, and ask the solver to simplify. This is exactly where MBA bites. (a | b) + (a & b) == a + b is a theorem over 32-bit two's-complement arithmetic, and deciding equivalence of mixed boolean-arithmetic expressions is a known-hard problem — solvers slow down sharply as you nest identities and mix in arithmetic, because the boolean and the modular-integer theories interact badly. One level is annoying. Three nested levels with split constants turns a millisecond simplification into a query the attacker watches spin, per expression, across every site in the assembly.

Neither tool is defeated in the absolute sense — a determined human with time can normalise any one expression by hand. MBA is about raising the per-site cost until "read the formula off the decompile" becomes "reverse hundreds of hand-tangled expressions, any of which might be a decoy." That is the whole game.

Turning it on

MBA lives under the arithmetic block in nebula.config.json (full configuration reference):

{
  "renaming": { "mode": "dictionary" },
  "encryptStrings": true,
  "arithmetic": {
    "enabled": true,
    "rewriteOps": ["add", "sub", "and", "or", "xor", "mul"],
    "constantSplitting": true,
    "depth": 2
  }
}
Enter fullscreen mode Exit fullscreen mode

Then protect your built assembly with the CLI:

nebula --config nebula.config.json
Enter fullscreen mode Exit fullscreen mode

depth is the dial that matters most: 1 applies one identity per site, 3 recurses into the terms each identity introduces. constantSplitting controls whether literals like 0x40 are themselves decomposed. In CI, drive the same config from an MSBuild target so developers keep clean local builds and only the release artifact is hardened:

<Target Name="NebulaProtect" AfterTargets="Publish" Condition="'$(Configuration)' == 'Release'">
  <Exec Command="nebula --config nebula.config.json" WorkingDirectory="$(PublishDir)" />
</Target>
Enter fullscreen mode Exit fullscreen mode

Scope it. MBA earns its cost on the dozen methods that encode your licensing math, your check-digit algorithm, your feature-flag decoding — not on your JSON DTOs. Use keepList entries and per-namespace overrides to aim the pass at the arithmetic worth hiding and leave hot, boring code alone.

The cost, honestly

MBA is the most arithmetic-heavy pass Nebula ships, and it is not free.

  • Speed. Every rewritten operation becomes several. On cold, infrequently-called code — a licence check that runs once at startup — this is invisible. Inside a tight numeric loop it is not: four-to-ten extra integer ops per iteration, and a method bloated past the JIT's inlining budget stops being inlined. Never point MBA at a hot numeric kernel. Protect the method that decides something, not the loop that crunches something.
  • Size. Method bodies grow with depth. At depth: 2 expect the targeted methods to roughly double in IL; depth: 3 can triple them. Because you scope MBA to a handful of methods, the whole-assembly impact stays small — which is the argument for scoping rather than a global switch.
  • Debuggability. A rewritten expression in a crash dump is unreadable to you, too. Keep the symbol/renaming map Nebula emits and retrace production traces with nebula deobfuscate --map so a support ticket does not become an archaeology project.

Where it fits

MBA is a layer, and it composes with the others rather than replacing them. Rename to kill the names; encrypt strings to kill the text; use opaque predicates to kill the tools' automation; and use MBA to kill the one thing all of those leave intact — the readable arithmetic that is your algorithm. Together they turn a value an attacker could read in a glance into an equivalence they would have to prove.

Expression and arithmetic obfuscation is a Pro feature, alongside code virtualization and method encryption. The heaviest configuration — high depth with correlated constant networks that share a runtime-derived invariant across many sites — travels with the aggressive control-flow tier at Enterprise, because correlation is what pushes MBA from "a solver finds slow" to "a solver cannot batch at all." Start at depth: 2 on your licensing namespace, measure the size of those methods and the throughput of your hottest path, and move the dial from there.

Top comments (0)