Getting the ReadyToRun and obfuscation order right
A ReadyToRun build is a dual image — your IL plus a native copy of it, compiled by crossgen from whatever the IL looked like at publish time. Obfuscate after crossgen and that native copy is a clean, decompiler-independent record of your original logic; it also invalidates the R2R image so the runtime quietly falls back to JIT. Here is why ordering decides everything, and how to wire Nebula so crossgen compiles protected IL.
Most .NET protection advice treats obfuscation and publishing as two independent steps you can do in either order. For an ordinary framework-dependent build that is roughly true. For a ReadyToRun build it is not — and getting the order wrong produces a shipped artifact that leaks the exact logic you obfuscated, while silently throwing away the startup benefit you turned R2R on for in the first place.
The reason is structural, not a bug: a ReadyToRun assembly is not one representation of your code, it is two. Obfuscation only ever touches one of them. This post is about which one, why the second copy is compiled from whatever the first looked like at the moment crossgen ran, and how to sequence Nebula.NET so both copies end up protected.
What a ReadyToRun build actually produces
PublishReadyToRun=true runs the crossgen2 compiler during dotnet publish. Crossgen reads the managed assemblies and, for each method, emits native machine code ahead of time, embedding it into the assembly alongside a ReadyToRun header. The goal is faster startup: the runtime can execute the precompiled native code instead of JIT-compiling the method on first call.
The critical fact — the one the whole ordering question turns on — is that ReadyToRun is not Native AOT. It does not remove your IL. The published assembly is still a completely ordinary .NET assembly: full metadata, full IL, full managed method bodies, plus a native-code section that duplicates the hot methods. The IL stays because it is the authoritative fallback. If the R2R image is not valid for the current runtime — wrong architecture, a version-bubble mismatch, a modified module — the CLR discards the native code and JITs from the IL instead. R2R is an optimization layered over your assembly, never a replacement for it.
So after a ReadyToRun publish you are shipping two independent encodings of every precompiled method:
- the IL method body, which a decompiler turns back into near-original C#, and
- the native compilation of that same method, which a native disassembler reads directly.
Obfuscation is an IL-to-IL transform. It rewrites the first encoding. It cannot touch the second — the native code is opaque to it. The only lever you have over the native copy is what the IL looked like when crossgen compiled it.
The ordering trap
Picture the tempting pipeline: build, publish with ReadyToRun, then run the obfuscator over the finished publish folder because that is the artifact you ship. It feels right — protect the thing you hand out. It is exactly backwards.
By the time your obfuscator runs, crossgen has already compiled the native code from your clean, unobfuscated IL. Your license check, your key-derivation routine, your pricing math — all of it is now sitting in the native section as a faithful, fully-optimized machine-code implementation of the original algorithm. Your obfuscator then flattens the IL copy of that method, encrypts its strings, maybe virtualizes it. The IL copy is beautifully protected. The native copy, compiled minutes earlier, is an untouched second record of the same logic that no IL transform will ever reach. An attacker simply disassembles the native method and reads the control flow you thought you had hidden.
There is a second, quieter failure stacked on top. Obfuscation rewrites the module: new metadata tokens, new names, new method bodies, a new MVID. The ReadyToRun image was keyed to the pre-obfuscation module. Mutating the assembly after crossgen invalidates the R2R header’s integrity and version checks, so at load time the runtime rejects the native image and falls back to JITting the IL. You paid R2R’s size cost, shipped a native copy of your unprotected logic, and lost the startup win. Three bad outcomes from one ordering mistake.
Obfuscate before crossgen
The rule falls straight out of the diagram: the IL that crossgen compiles must already be obfuscated. Concretely, obfuscation has to land after the C# compiler produces your assemblies and before dotnet publish runs the ReadyToRun pass. There are two clean ways to arrange that.
CI two-stage. Build, obfuscate the compiled output, then publish with ReadyToRun pointed at the already-protected assemblies:
dotnet build -c Release -p:PublishReadyToRun=false
nebula --config nebula.config.json # rewrites the IL in place
dotnet publish -c Release --no-build \
-p:PublishReadyToRun=true -o out # crossgen now compiles protected IL
The nebula.config.json keeps everything R2R and reflection depend on, and applies the heavy transforms where they matter:
{
"schemaVersion": 1,
"inputs": ["bin/Release/net8.0/MyApp.dll", "bin/Release/net8.0/MyApp.Core.dll"],
"outputDirectory": "bin/Release/net8.0",
"preservePublicApi": true,
"encryptStrings": true,
"controlFlowObfuscation": true,
"autoProtectLicensing": true
}
Because crossgen reads the assemblies in bin/Release/net8.0 at publish time, the native code it emits is compiled from the flattened, string-encrypted IL — the two encodings now agree, and both are protected.
An MSBuild hook, so a plain dotnet publish is already correct. The one thing that matters is that the obfuscation Exec runs before the SDK’s ReadyToRun compilation, not after it:
<Target Name="NebulaProtect" AfterTargets="Build" BeforeTargets="Publish">
<Exec Command="nebula --config $(ProjectDir)nebula.config.json" />
</Target>
Whichever you use, the principle is the same one that governs Native AOT and single-file publishing: protection belongs on the managed IL, between compile and publish. R2R just raises the stakes, because here the publish step does not merely package your IL — it compiles a second, independent copy of it.
What each encoding looks like
Take a representative enforcement method:
public bool IsLicensed(string key)
{
if (!key.StartsWith("LIC-")) return false;
return ValidateSignature(key, "https://api.example.com/v1/activate");
}
Obfuscate the IL before crossgen and the IL copy decompiles to a flattened dispatcher with the prefix and endpoint pulled from the encrypted string store at runtime:
public bool a(string A_0)
{
int s = 0;
while (true) { switch (s) {
case 0: if (!A_0.StartsWith(Strings.Get(0x3f1))) { s = 9; break; } s = 4; break;
case 9: return false;
case 4: return b(A_0, Strings.Get(0x412));
/* ... */ } }
}
Crucially, the native method crossgen produces is now a compilation of that dispatcher — same opaque branch structure, no literal URL, nothing to read off cleanly. Note that .NET string literals live in the metadata #US heap, not inlined into native code, so string encryption protects both encodings through metadata regardless of R2R; it is the control flow and the shape of the algorithm that the native copy would otherwise expose, and here it exposes the obfuscated shape.
Obfuscate after crossgen and the IL copy looks identical to the protected version above — but the native section is a direct machine-code translation of the clean original: a recognizable prefix comparison, a call into signature validation, the branch structure of your real method, fully optimized and perfectly legible to anyone who disassembles the R2R image. The IL obfuscation was real work spent protecting the copy nobody needs to read.
Verifying the R2R build is actually protected
Two quick checks turn the ordering guarantee into evidence:
- The R2R image is present and valid. After the protected publish, confirm the assembly still carries a ReadyToRun header (the SDK’s
crossgen2/r2rdumptooling will dump it). If obfuscation had run after crossgen, the image would be absent or rejected at load and you would silently be back on the JIT — so a valid header is proof the ordering held. - The native method bodies map to renamed members. Dump the R2R method list; the entrypoints should correspond to Nebula’s renamed identifiers (
a,b, …), because they were compiled from the obfuscated metadata. Original names surfacing in the native method table mean crossgen ran first — a red flag.
For the IL side, verify as you would any protected build: open the shipped assembly in Glass.NET or your decompiler of choice and confirm the sensitive methods come back as flattened dispatchers rather than clean C#, exactly as covered in verifying obfuscation in CI.
The takeaway
ReadyToRun is a good startup optimization and worth shipping. But it fundamentally changes the obfuscation question, because it compiles a second, native copy of your code from whatever IL it is handed. Obfuscate first and that copy inherits your protection; obfuscate last and it becomes a clean, decompiler-independent record of the logic you were trying to hide — while the mismatched R2R image gets discarded at load, costing you the one benefit you turned it on for. There is no middle ground and no “we’ll protect the published folder later.” Crossgen must compile protected IL. Sequence Nebula before the ReadyToRun pass, verify the native methods carry the renamed shape, and both halves of the image tell the same, protected story.
Try Nebula.NET
Harden your .NET code in minutes — start with the free edition.