Skip to content
← All posts
· Delta1 Labs NebulaObfuscation

Encrypting the byte arrays obfuscators miss

You turned on string encryption and your embedded S-box, public key, and lookup tables are still sitting in the PE as plaintext bytes. This deep-dive shows where byte[] initializers and "..."u8 literals actually live — the <PrivateImplementationDetails> FieldRVA blob — the real IL for InitializeArray and CreateSpan, and how Nebula encrypts those blobs and decrypts them at runtime.

You enabled string encryption, rebuilt, and opened the result in a hex viewer to check your work. The strings are gone — good. Then you scrolled a little further and there it was: your 256-byte AES S-box, byte for byte, in the clear. A few kilobytes later, the DER bytes of the RSA public key you verify licences against. Then the flat lookup table that maps a tier id to a feature mask. None of it is a string, so the string pass never saw it — and all of it is exactly the kind of embedded IP you turned on obfuscation to protect.

This is a real and common gap. String encryption rewrites the #US heap. Constant encryption rewrites scalar ldc operands. But a byte[] initialized from a literal, and every "..."u8 and ReadOnlySpan<byte> collection literal, lives somewhere neither pass looks. This post shows exactly where, what the IL looks like, and how Nebula encrypts it.

Where a byte array actually lives

Write the obvious thing:

static class Crypto
{
    static readonly byte[] SBox =
    {
        0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, /* …248 more… */
    };
}

The C# compiler does not emit 256 stelem instructions. For a blittable element type and an all-constant initializer, it writes the raw bytes once into a synthetic type called <PrivateImplementationDetails> as a field with an RVA — a field whose storage is a fixed region of the PE image — and emits a single call that copies that region into a freshly allocated array:

IL_0000: ldc.i4    0x100
IL_0005: newarr    [System.Runtime]System.Byte
IL_000a: dup
IL_000b: ldtoken   field valuetype '<PrivateImplementationDetails>'/'__StaticArrayInitTypeSize=256' '<PrivateImplementationDetails>'::'2C26B4...'
IL_0010: call      void [System.Runtime]System.Runtime.CompilerServices.RuntimeHelpers::InitializeArray(class [System.Runtime]System.Array, valuetype [System.Runtime]System.RuntimeFieldHandle)
IL_0015: stsfld    uint8[] Crypto::SBox

The payload sits in the metadata as a .data directive, verbatim:

.field static assembly initonly valuetype '<PrivateImplementationDetails>'/'__StaticArrayInitTypeSize=256' '2C26B4...' at D_00002450
.data cil D_00002450 = bytearray (63 7C 77 7B F2 6B 6F C5 /* … */)

Those are your S-box bytes, in file order, grep-able with a hex editor. Renaming turned SBox into b and Crypto into a, but the field name was never the secret — the bytes were, and they are untouched. InitializeArray is not an encryption boundary; it is a memcpy from the image into the heap.

The u8 path is even more exposed

The modern collection-literal and UTF-8 forms look different in IL but store data the same way. A ReadOnlySpan<byte> built from a "..."u8 literal or a byte collection expression compiles to a zero-copy span straight over the FieldRVA blob:

static ReadOnlySpan<byte> Magic => "PK\x03\x04"u8;
IL_0000: ldtoken   field valuetype '<PrivateImplementationDetails>'/'__StaticArrayInitTypeSize=4' '<PrivateImplementationDetails>'::'9F86D0...'
IL_0005: call      valuetype [System.Runtime]System.ReadOnlySpan`1<uint8> [System.Runtime]System.Runtime.CompilerServices.RuntimeHelpers::CreateSpan<uint8>(valuetype [System.Runtime]System.RuntimeFieldHandle)
IL_000a: ret

There is no allocation and no copy: CreateSpan returns a span that points directly into the loaded image. Fast — and completely transparent on disk. The bytes are right there at RVA 9F86D0….

What Nebula does

Nebula’s blob pass does not scan the whole PE for high-entropy regions and guess. It walks the IL, finds every InitializeArray and CreateSpan call site, follows the ldtoken to the <PrivateImplementationDetails> field it references, and treats that field’s data as a protect-me blob. Then two things happen: the stored bytes are encrypted in place in the image, and the call site is rewritten to decrypt at runtime.

For the InitializeArray case the rewrite is small, because the array is already a fresh heap allocation by the time the bytes land in it. Encrypt the blob, let InitializeArray copy the ciphertext into the new array, then transform it in place before it is stored:

IL_0000: ldc.i4    0x100
IL_0005: newarr    [System.Runtime]System.Byte
IL_000a: dup
IL_000b: ldtoken   field ... '2C26B4...'        // now holds ciphertext
IL_0010: call      void ...RuntimeHelpers::InitializeArray(...)
IL_0015: dup
IL_0016: call      void Nebula.Runtime.Blob::Decrypt(uint8[])   // in-place, per-blob key
IL_001b: stsfld    uint8[] Crypto::SBox

Because SBox is a static readonly field, this whole sequence runs inside the type initializer — once — so the decrypt is a one-time cost paid the first time Crypto is touched. After that, every reader sees a normal plaintext array with no per-access overhead.

The CreateSpan case cannot be rewritten in place: the span points into read-only image memory, and the whole point of the u8 path was to avoid an allocation. Nebula replaces the CreateSpan call with a stub that reads the ciphertext through the same field handle, allocates a heap array, decrypts into it, and returns a span over that:

IL_0000: ldtoken   field ... '9F86D0...'        // ciphertext
IL_0005: call      valuetype [System.Runtime]System.ReadOnlySpan`1<uint8> Nebula.Runtime.Blob::DecryptSpan(valuetype [System.Runtime]System.RuntimeFieldHandle)
IL_000a: ret

Correct, protected — and no longer zero-copy. That trade is the honest cost of hiding a u8 literal, and it is why you scope the pass rather than firehose it (more below).

A byte array literal compiles to a plaintext FieldRVA blob; Nebula encrypts the blob and decrypts it at runtimeWhere byte[] = {…} and "…"u8 really gobyte[] SBox = {…}"PK…"u8RoslynFieldRVA blob.data = 63 7C 77 7B F2 6B …plaintext in the PE — string pass never sees itAfter Nebula.data encrypted per-blobA4 11 9C 5E 2F …InitializeArray → ciphertextin fresh heap arrayBlob::Decryptonce, in the cctorstatic readonly byte[] decrypts once per type initializer — no per-access cost.A u8 / ReadOnlySpan<byte> literal must allocate to decrypt, losing CreateSpan'szero-copy — so aim the pass at the blobs that are worth it, not every table.
A byte-array or u8 literal stores raw bytes in a FieldRVA blob that string encryption never touches. Nebula encrypts the blob and decrypts it at the point of use.

Turning it on

The blob pass sits alongside the other literal passes in nebula.config.json (full configuration reference):

{
  "renaming": { "mode": "dictionary" },
  "encryptStrings": true,
  "encryptConstants": true,
  "dataBlobs": {
    "enabled": true,
    "minSize": 8,
    "includeSpans": true
  }
}

Then protect the built assembly with the CLI:

nebula --config nebula.config.json

minSize skips tiny blobs where a one-time decrypt stub costs more IL than the bytes it hides. includeSpans controls whether the pass also rewrites the zero-copy CreateSpan sites — leave it off if a hot u8 literal matters more to you than hiding it. In CI, drive the same config from an MSBuild target so developers keep clean local builds and only the shipped artifact is hardened:

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

Caveats, honestly

  • Zero-copy u8 literals lose zero-copy. A "..."u8 that was a free span becomes an allocating decrypt. If you have a u8 literal on a genuinely hot path — a header you compare on every request — either exclude it or decrypt once into a cached static readonly byte[] and slice that.
  • Per-call properties re-materialize. A ReadOnlySpan<byte> Magic => … property decrypts on every call. For anything read more than occasionally, back it with a static readonly byte[] so the cctor pays the cost once.
  • It is encoding, not a vault. The per-blob key travels inside the assembly, so a determined attacker who runs your code can recover the plaintext from memory — this raises the cost of static extraction, it does not make keys unextractable. Treat an embedded blob as obfuscated, never as a secret store; real secrets belong in a key vault or Keyright entitlements, not baked into the binary.
  • Interop still works. After decrypt the data is an ordinary heap array, so fixed, pinning, and passing it to native code behave exactly as before.

Takeaway

String and constant encryption cover the literals that live in the metadata heaps. A byte[] initializer and a "..."u8 literal do not live there — they are raw data in a <PrivateImplementationDetails> FieldRVA blob, copied out by InitializeArray or handed back zero-copy by CreateSpan, and a string-only pass leaves them in the clear. Point Nebula’s blob pass at them and the bytes become ciphertext on disk, decrypted at the one place they are used. The S-box, the embedded key, the lookup table that encodes your pricing — the things you actually wanted to hide — stop being a hex-viewer scroll away. Literal encryption is a Pro feature, alongside method encryption and code virtualization.

Try Nebula.NET

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