Skip to content
← All posts
· Delta1 Labs Nebula.NETAnti-TamperSecurity

Fusing integrity checks into real computation

A separate "hash my code, compare, call a tamper handler" check is a branch an attacker finds and NOPs. The durable alternative derives a value the program actually needs — a decryption key, a string XOR key, a jump offset — from a checksum of the protected method bodies, so a one-byte patch silently corrupts output instead of tripping a removable branch. Here it is in real C# and IL, with the honest limits.

Rename everything, encrypt every string, and the cheapest attack on a managed assembly is still a one-byte IL patch. The usual defence is a self-integrity check: hash your own method bodies at runtime, compare against a value baked in at protect time, and call a tamper handler if they differ. It works — until the attacker opens the assembly, finds the comparison, and deletes it. The whole defence lives behind one conditional branch, and that branch is a detachable part: NOP it and the program runs patched and content. This post is about a different shape of the same idea — one with no branch to find. Instead of comparing the checksum, you use it: derive a value the program genuinely needs from a hash of your protected method bodies, so that patching any byte silently corrupts that value and the program produces garbage, with no isolated failure path to locate and remove.

The weak version: detect and branch

Here is the honest baseline — a self-check written the obvious way:

static void VerifyIntegrity()
{
    byte[] actual = Sha256(GuardedMethodBodies());   // hash my own IL
    if (!actual.SequenceEqual(Expected))             // Expected baked in at protect time
        TamperResponse.Trigger();                    // the detachable part
}

The problem is the shape, not the hash. Compiled, the decision is a single conditional:

IL_0014: call      bool [App]App.Integrity::SequenceEqual(uint8[], uint8[])
IL_0019: brtrue.s  IL_0020      // equal -> skip the response
IL_001b: call      void [App]App.TamperResponse::Trigger()
IL_0020: ret

The attacker does not care how good your hash is. They find brtrue.s (opcode 0x2D) and the Trigger call, and they have two one-byte moves. Invert the branch to brfalse.s (0x2C) and the response only fires on a clean assembly. Or NOP the three bytes of the call and it never fires at all:

// as shipped
IL_0019: brtrue.s  IL_0020
IL_001b: call      void [App]App.TamperResponse::Trigger()

// after NOPing the response: 0x28 xx xx -> 0x00 0x00 0x00
IL_0019: brtrue.s  IL_0020
IL_001b: nop
IL_001c: nop
IL_001d: nop

The hash still computes, still mismatches, and nobody is listening. Everything that follows — delayed reactions, decoupled flags, scattered verifiers — is damage control for one structural fact: a comparison and a response are a separable thing, and separable things get separated.

The real question

What if the integrity value were not something you check but something you consume? If the program cannot take a step without the true hash — because the true hash is the decryption key, or the XOR key for your string table, or an offset it jumps to — then there is nothing to detach. A patch does not trip a branch; it changes the hash, which changes the derived value, which makes the next real operation produce nonsense. The failure is not reported. It is baked into the arithmetic the program already had to do.

Fusing the checksum into a string-decryption key

Take string encryption — a transform you already want. Nebula Pro stores your string literals as an encrypted table and decrypts them on demand with a key. The naive design stores that key as a constant. The fused design derives it from a checksum of the guarded method bodies:

static byte[] DeriveStringKey()
{
    // H over the IL of the methods we froze at protect time.
    byte[] h = Sha256(GuardedMethodBodies());
    // The table was encrypted at protect time under THIS exact value.
    return h;                       // no comparison, no branch — it is the key
}

static string S(int id)
{
    byte[] key = DeriveStringKey();
    return Decrypt(_stringTable[id], key);   // every string flows through the hash
}

At protect time Nebula hashes the final, shipped IL of the guarded methods and encrypts the string table under that digest. There is no Expected constant anywhere and no if. The only use of the hash is as the key. On the bytes you shipped, DeriveStringKey() reproduces the exact digest the table was encrypted under, every string decrypts correctly, and the program runs. Patch a single byte in any guarded method and the digest changes — so the key is wrong, and every string the program decrypts is garbage. Error messages, format strings, SQL, resource names, config keys: all corrupt, everywhere, with no line that says why.

Look at what the attacker now finds in the hot path. There is no branch to invert:

// S(int id) — the decrypt path. No brtrue/brfalse anywhere.
IL_0000: call      uint8[] App.Integrity::DeriveStringKey()
IL_0005: stloc.0
IL_0006: ldsfld    uint8[][] App.Strings::_stringTable
IL_000b: ldarg.0
IL_000c: ldelem.ref
IL_000d: ldloc.0
IL_000e: call      string App.Strings::Decrypt(uint8[], uint8[])
IL_0013: ret

de4dot and dnSpy are built to defeat the detect-and-branch model: locate the guarding conditional, invert or remove it. Here there is no conditional. The hash result is consumed as data by Decrypt — ordinary work the program must do to function. To break this an attacker cannot NOP a branch; they must realise the string key is a method-body hash, compute the correct hash for their patched bytes, and re-embed it so the table decrypts again. The checksum is load-bearing: the program is simply wrong without the true value.

A removable branch versus a load-bearing valueDETACHABLE CHECKFUSED CHECKH(method bodies)compare == Expectedbranch -> TamperResponseattacker NOPs the branchdefence gone, program intactH(method bodies)used as decryption keyDecrypt(stringTable, key)patch -> wrong hash -> wrong keyevery string decrypts to garbage
Left: the checksum feeds a comparison whose branch is a separable part an attacker NOPs out. Right: the same checksum is the decryption key, so there is no branch to remove — a patched byte changes the hash, the key goes wrong, and every decrypted string turns to garbage.

Other values worth fusing

A string key is the clearest case, but any value the program derives at runtime is a candidate. Fold the checksum into it with an operation that is the identity on the true hash and noise on anything else:

// A numeric constant the program needs — correct only when H matches the baked-in digest.
static int ShardCount()
{
    int h32 = BitConverter.ToInt32(Sha256(GuardedMethodBodies()), 0);
    // At protect time: Mask was chosen so (h32 ^ Mask) == the real shard count.
    return h32 ^ Mask;        // clean -> 16; patched -> some wrong, non-crashing number
}
// A method-body decryption key, tying this post to encrypted bodies:
// the blob for a protected method was encrypted at protect time under H(other guarded bodies).
byte[] key = Sha256(GuardedMethodBodies());
byte[] il  = Decrypt(BlobFor(token), key);   // wrong key -> invalid IL -> it never rebuilds

The pattern is always the same: the hash is an input to arithmetic the program already performs, never the subject of a comparison. Fuse it into a jump offset and a patched assembly jumps somewhere wrong. Fuse it into a cache shard count and buckets scatter. The further the derived value travels from the checksum before it is consumed, the harder it is to see the connection at all.

How you turn it on

Fused derivation is part of the Nebula Pro anti-tamper pass, applied post-build so your source never carries the plumbing and the checksum is computed over the final protected bytes — after renaming, control-flow flattening and string encryption have run. The configuration names are illustrative — treat them as a sketch of the model, not guaranteed flags — but the shape is: you choose a guarded set of method bodies to checksum (integrity.guardedMethods), and you choose which derived values draw from that checksum (integrity.fuseInto: ["stringKey", "bodyKey"]), on a nebula protect-style invocation:

nebula protect ./App.dll \
  --integrity-guard "App.Licensing.*" \
  --integrity-fuse stringKey,bodyKey

Checksum the methods where a one-byte patch is the attack — the license gate, the activation guard, the key-derivation routine — and fuse the result into values those same features depend on. Pair it with controlFlow and encryptStrings so the derivation itself is not a tidy, readable function. For the string table it feeds, see encrypted method bodies and JIT decrypt; for the detect-and-branch sibling this post deliberately avoids, see how an assembly notices it was patched.

When it breaks, honestly

Fusing is a layer, not a wall, and it costs you real things.

It is harder to debug — for you, too. A fused check has no error message because it has no failure path; when something is wrong the symptom is garbage output far from the cause. A legitimate build problem — a stale checksum, a method edited after the digest was baked — presents as the same silent corruption an attacker would see. Keep your protect step reproducible and your rename map, or you will be the one staring at nonsense with no stack trace.

It must be deterministic across the JIT and the platform. The checksum has to be a pure function of bytes you froze at protect time. Hash the IL byte arrays or selected metadata blobs — never JIT-compiled native code, JITed addresses, or runtime handles. Those vary legitimately between a debug and release JIT, between x64 and Arm64, between framework versions, and if your fused input moves, your own clean build derives the wrong key. Under ReadyToRun or NativeAOT the IL you hashed may not be what executes; know your deployment model before relying on an IL checksum.

There is a one-time setup cost and an ordering constraint. The checksum must be computed over the final shipping bytes and the dependent values encrypted under it last, after every other transform. Get the order wrong and you ship a key derived from a build that never left the machine.

It is not unbreakable. A determined attacker who recognises the derivation can compute the correct hash for their patched bytes and re-embed it, and the table decrypts again. Fusing does not stop that attack; it removes the cheap one. There is no detachable branch, so the one-byte NOP that beats a naive check accomplishes nothing, and the attacker is forced into a full comprehension of how the value is produced and consumed — redone for every patch.

Takeaway

A tamper check you can point at is a tamper check an attacker can remove. The durable move is to stop checking and start consuming: derive a value the program actually needs — a string key, a body-decryption key, a constant, an offset — from a checksum of your protected method bodies, so the true hash is load-bearing and a patched byte silently corrupts output instead of tripping a branch. It is harder to debug, it demands determinism, and it is not unbreakable — but it deletes the easy attack entirely. See Nebula.NET for the full transform set, or the docs for how the anti-tamper pass fits with the rest of the pipeline.

Try Nebula.NET

Harden your .NET code in minutes — start with the free edition.