Deterministic compilation is one of the quieter wins of modern .NET. Turn on <Deterministic>true</Deterministic> — it is the default for SDK-style projects — and Roslyn will emit a byte-identical assembly from identical source, identical references and an identical compiler. No wall-clock timestamp, no random module GUID. Two engineers on two machines get the same DLL, and CI can prove the shipped binary matches the source.
Then you run it through an obfuscator, and the guarantee evaporates. Hash the protected assembly from two consecutive runs of the exact same inputs and you get two different hashes. Nothing about your code changed. So where did the determinism go?
The real question
An obfuscator reads an assembly, rewrites its metadata and IL, and writes a new assembly. Two fields in that output are, by long tradition, filled from the environment rather than from the input — and a third source of variance hides in how the renamer walks your members. Find those three and you can put the guarantee back.
Where the nondeterminism lives
The first is the module identity. Every .NET module carries a Module Version ID, a GUID stored in the #GUID metadata heap and referenced from the single row of the Module table. ildasm prints it at the top of the module:
// Metadata: Module (table 0x00)
// Gen Name Mvid (-> #GUID) EncId EncBaseId
// 0 MyApp.dll 1 0 0
// #GUID[1] = {3f2504e0-4f89-41d3-9a0c-0305e82c3301}
Roslyn, in deterministic mode, sets that GUID to a hash of the compilation inputs. A typical obfuscator re-emits the module and sets it to Guid.NewGuid() — a fresh random value every run. One field, and reproducibility is already gone.
The second is the PE header timestamp. The COFF header has a 32-bit TimeDateStamp. Deterministic Roslyn does not write the real time there; it writes a hash of the content with the high bit set, so tools can tell it is not a timestamp:
// PE / COFF header
// Machine: 0x014C (I386)
// TimeDateStamp: 0x8E0F3A21 // high bit set: content hash, not a real time
An obfuscator that calls DateTimeOffset.UtcNow to fill that field makes the output differ every second.
The third is subtler and lives in the renamer. Giving a member its obfuscated name is deterministic in isolation, but the order in which members are visited usually is not. Walk a type's methods through a HashSet or a reflection-ordered collection and the iteration order can shift between runs or framework versions; seed the name generator from Environment.TickCount and the very first name changes every run. Here is the shape of a naive implementation — every line is a reproducibility leak:
module.Mvid = Guid.NewGuid(); // random identity
peHeader.TimeDateStamp = (uint)DateTimeOffset.UtcNow
.ToUnixTimeSeconds(); // wall clock
var rng = new NameGenerator(seed: Environment.TickCount); // random seed
foreach (var m in type.Methods) // unstable order
m.Name = rng.Next(); // -> different map
Even string encryption contributes: if the per-build key comes from RandomNumberGenerator, the encrypted blobs in the #US-adjacent data differ every run even though they decrypt to the same strings.
Deriving identity from content
The fix is to replace every environment-sourced value with a function of the input. Nebula.NET's deterministic mode computes a digest over the transformed module in a canonical form — the metadata tables and IL after rewriting, read in table order, with the name-dependent fields masked so the digest does not depend on names it has not assigned yet — and uses that digest as the single source of entropy:
byte[] digest = SHA256.HashData(canonicalModuleBytes); // stable over inputs + config
module.Mvid = new Guid(digest.AsSpan(0, 16)); // identity from content
peHeader.TimeDateStamp =
BinaryPrimitives.ReadUInt32LittleEndian(digest.AsSpan(16)) | 0x8000_0000u; // hash, flagged
var rng = new NameGenerator(seed: digest); // reproducible map
foreach (var m in type.Methods.OrderBy(StableKey)) // (declaring token, signature)
m.Name = rng.Next();
var stringKey = HKDF.DeriveKey(HashAlgorithmName.SHA256, digest, 32, info: "nebula/strings"u8);
StableKey is the part that makes the renamer honest: members are sorted by a tuple of their declaring type's metadata token and their full signature, so the walk order does not depend on collection internals. The string-encryption key is derived from the same digest with HKDF, so the encrypted blobs are stable too — and since that key already has to live inside the assembly for the runtime to decrypt, deriving it from content gives an attacker nothing new.
You opt in with one config key:
{
"assemblies": ["MyApp.dll"],
"rename": { "enabled": true },
"controlFlow": { "enabled": true },
"stringEncryption": { "enabled": true },
"deterministic": true
}
Now the pipeline is a pure function. Run it twice and compare:
nebula --config nebula.config.json --out run1/
nebula --config nebula.config.json --out run2/
sha256sum run1/MyApp.dll run2/MyApp.dll
# 4e02a9… run1/MyApp.dll
# 4e02a9… run2/MyApp.dll <- identical
What it buys you
Attestation. A reproducible protected assembly can be rebuilt by anyone holding the input assembly and the config, and the hash they get is the hash you shipped. That turns "trust our build server" into "rebuild it yourself and compare," which is the whole point of a reproducible-builds posture for a supply chain.
Caching. Because the output is a function of (input assembly, config, Nebula.NET version), you can key a CI cache on the hash of exactly those three. On a branch that did not touch the assembly, the cache hits and you skip obfuscation entirely — safe, because a hit guarantees byte-identical output to what you would have produced.
Diffability. With a pinned MVID and timestamp, a binary diff between two releases shows only metadata and IL that actually changed, instead of being swamped by a new GUID and a new timestamp on every build. Pair this with a stable rename map and the diff reflects real change.
When it breaks
Reproducibility is only as deterministic as its weakest input, and three are worth calling out honestly.
Tool version is part of the key. A new Nebula.NET version may add or refine a transform, which legitimately changes the output. Deterministic output is reproducible for a fixed version; pin the version in CI and record it alongside the hash.
Authenticode timestamping is not reproducible. Strong-name signing is deterministic — it is a function of the key and the content — but an Authenticode signature with an RFC 3161 countersignature embeds the signing time and the TSA nonce, which no two signatures share. Keep signing as the final step and attest the hash of the unsigned protected assembly, or compare the to-be-signed content.
Embedded build metadata leaks through. If your own code bakes DateTime.Now, a build counter or a fresh GUID into a constant, that nondeterminism is in the input assembly before Nebula.NET ever sees it. Deterministic compilation upstream is a prerequisite, not an afterthought — fix the source first.
Takeaway
Obfuscation does not have to cost you a reproducible build. The nondeterminism is not inherent to rewriting an assembly; it lives in three replaceable decisions — the module GUID, the PE timestamp and the rename seed — plus the key material of any content-encrypting transform. Source each of them from a hash of the input instead of from the clock or a random generator, walk your members in a stable order, and the protected DLL hashes the same on every run. Turn on deterministic: true, pin the version, and your hardened artifact becomes something you can attest to and cache rather than something that changes every time you look at it.
Top comments (0)