# Hiding sensitive calls behind proxy methods


Rename every symbol, encrypt every string, and a decompiler still prints one line that hands an attacker a map: `call bool Licensing.LicenseCheck::Verify(string)`. The method name may be mangled, but the *edge* is right there — this method calls that method — and so is the fully-qualified target when the callee is a BCL type like `System.Security.Cryptography.Aes` or `RSA.VerifyData`. Reading a protected assembly often starts with "find every call into `Cryptography`" and working outward. Those call sites are a search index into your logic.

**Proxy-call obfuscation** — sometimes called reference obfuscation — takes that index away. It replaces the direct `call`/`callvirt` with a jump through an injected intermediary, so the decompiled caller no longer shows a clear edge to the sensitive method, and the method's name is no longer sitting in the call site's token.

This is a deep look at how that works at the IL level: three real forms of indirection, the before/after IL for each, what they actually hide, and the honest cost.

## What a direct call gives away

Start with an ordinary guard:

```csharp
public bool Activate(string key)
{
    return LicenseCheck.Verify(key);
}
```

The IL is exactly what you'd expect — load the argument, call the target by its full metadata token, return:

```
.method public instance bool Activate(string key)
{
  IL_0000: ldarg.1
  IL_0001: call      bool Licensing.LicenseCheck::Verify(string)
  IL_0006: ret
}
```

Two facts leak from `IL_0001`. First, the **name and signature** of the target are encoded in the `MemberRef`/`MethodDef` token the `call` points at — a decompiler resolves it and prints `LicenseCheck::Verify`. Second, the **call-graph edge** `Activate → Verify` is explicit: any tool that walks call instructions draws that arrow for free. Chain those arrows and you have the control structure of the program before you've read a single algorithm.

## The idea: put something in the middle

Every proxy form shares one move — insert an intermediary the caller invokes instead, and have *that* reach the real target by a route the static call-instruction scan does not follow.

<figure class="my-8">
<svg viewBox="0 0 660 300" role="img" aria-labelledby="fig-title" xmlns="http://www.w3.org/2000/svg" style="max-width:660px;width:100%;height:auto;font-family:system-ui,sans-serif;font-size:13px">
<title id="fig-title">A direct call edge is replaced by a proxy that reaches the target without a static call instruction</title>
<text x="165" y="24" text-anchor="middle" fill="#64748b" font-weight="bold">Direct call</text>
<text x="495" y="24" text-anchor="middle" fill="#64748b" font-weight="bold">Through a proxy</text>
<rect x="100" y="48" width="130" height="36" rx="6" fill="none" stroke="#6366f1" stroke-width="2"/>
<text x="165" y="71" text-anchor="middle" fill="#6366f1">Activate</text>
<rect x="100" y="168" width="130" height="36" rx="6" fill="none" stroke="#0891b2" stroke-width="2"/>
<text x="165" y="191" text-anchor="middle" fill="#0891b2">Verify</text>
<line x1="165" y1="84" x2="165" y2="168" stroke="#64748b" stroke-width="2" marker-end="url(#ah)"/>
<text x="175" y="130" fill="#64748b">call Verify</text>
<rect x="430" y="48" width="130" height="36" rx="6" fill="none" stroke="#6366f1" stroke-width="2"/>
<text x="495" y="71" text-anchor="middle" fill="#6366f1">Activate</text>
<rect x="430" y="118" width="130" height="36" rx="6" fill="#6366f1" fill-opacity="0.12" stroke="#6366f1" stroke-width="2"/>
<text x="495" y="141" text-anchor="middle" fill="#6366f1">Proxy0</text>
<rect x="430" y="188" width="130" height="36" rx="6" fill="none" stroke="#0891b2" stroke-width="2" stroke-dasharray="5 4"/>
<text x="495" y="211" text-anchor="middle" fill="#0891b2">Verify</text>
<line x1="495" y1="84" x2="495" y2="118" stroke="#64748b" stroke-width="2" marker-end="url(#ah)"/>
<text x="505" y="104" fill="#64748b">callvirt Invoke</text>
<line x1="495" y1="154" x2="495" y2="188" stroke="#10b981" stroke-width="1.5" stroke-dasharray="5 4" marker-end="url(#ah)"/>
<text x="505" y="174" fill="#10b981">token / ldftn</text>
<text x="330" y="258" text-anchor="middle" fill="#64748b">The solid edge is what a call-instruction scan sees.</text>
<text x="330" y="278" text-anchor="middle" fill="#64748b">On the right, that scan stops at Proxy0 — the dashed edge is not a plain call.</text>
<defs><marker id="ah" markerWidth="8" markerHeight="8" refX="6" refY="3" orient="auto"><path d="M0,0 L6,3 L0,6 Z" fill="#64748b"/></marker></defs>
</svg>
<figcaption class="text-sm text-muted mt-2">The caller's direct edge to the target is cut; it now calls a proxy, which reaches the target through a delegate, a metadata token, or a function pointer rather than a plain <code>call</code>.</figcaption>
</figure>

### Form 1 — a cached delegate

The caller invokes a `Func<>` delegate instead of the method. The delegate is built once, with `ldftn` + the delegate constructor, and cached in a static field:

```csharp
// injected by Nebula
static Func<string, bool> __p0;

static bool Proxy0(string key)
{
    Func<string, bool> d = __p0 ?? (__p0 = BuildP0());
    return d(key);
}

static Func<string, bool> BuildP0()
{
    // ldftn target + Func`2::.ctor
    return new Func<string, bool>(LicenseCheck.Verify);
}

public bool Activate(string key) => Proxy0(key);   // was: LicenseCheck.Verify(key)
```

In `Activate`, the `call LicenseCheck::Verify` is gone. The only call instruction left at the site is a `callvirt` into the delegate's `Invoke`:

```
IL_0000: ldarg.1
IL_0001: call      bool App.Stubs::Proxy0(string)
IL_0006: ret
// inside Proxy0, the actual invocation:
IL_000a: callvirt  instance bool class [System.Runtime]System.Func`2<string,bool>::Invoke(string)
```

A tool that indexes call targets now records a call to `Func.Invoke` — which every delegate in the program shares — not to `Verify`. The call-graph edge `Activate → Verify` is broken. The target name still appears once, in the `ldftn` inside `BuildP0`, but it's no longer at the use site and no longer tied to `Activate`; fan a hundred proxies through one builder table and the edges all collapse into a single anonymous hub.

### Form 2 — reflection by token

Delegate construction still leaves an `ldftn <name>` in the IL. To remove the name entirely, resolve the target from its **numeric metadata token** at runtime:

```csharp
static MethodInfo __m0;

static bool Proxy0(string key)
{
    MethodInfo m = __m0 ?? (__m0 =
        (MethodInfo)typeof(Proxy0Host).Module.ResolveMethod(0x06000012));
    return (bool)m.Invoke(null, new object[] { key });
}
```

Now the IL contains no reference to `Verify` at all — only the integer `0x06000012` handed to `Module.ResolveMethod`:

```
IL_0007: ldc.i4    0x06000012
IL_000c: callvirt  instance class [System.Runtime]System.Reflection.MethodBase
                    [System.Runtime]System.Reflection.Module::ResolveMethod(int32)
IL_0018: callvirt  instance object
                    [System.Runtime]System.Reflection.MethodInfo::Invoke(object, object[])
```

A casual decompile shows a reflection call against an opaque number. The name is recoverable — the token resolves against the same module's `#~` tables — but that is a deliberate step an analyst must take, not a label the decompiler hands them. Mask the constant with an opaque arithmetic expression and even the token isn't a plain literal.

### Form 3 — calli through a function pointer

The lowest-level form uses the `calli` instruction, which calls through a function pointer on the stack against an explicit signature. If the pointer is loaded from a table (or computed) rather than via a literal `ldftn <name>`, the call site names neither a method nor a delegate type:

```
// fnptr pushed from a runtime-built table, not ldftn <name>
IL_0005: ldarg.1
IL_0006: ldsfld    native int App.Stubs::_ptr0
IL_000b: calli     bool(string)
```

`calli` is the most efficient of the three — no delegate allocation, no reflection — but it is also the most fragile: the signature is hand-encoded as a standalone signature token, and a wrong pointer is an immediate crash rather than an exception.

## Turning it on

Proxy calls live under their own block in `nebula.config.json`, scoped to the namespaces whose call sites you most want to hide:

```json
{
  "renaming": { "mode": "dictionary" },
  "encryptStrings": true,
  "proxyCalls": {
    "enabled": true,
    "mode": "delegate",
    "scope": ["MyApp.Licensing", "MyApp.Crypto"],
    "cacheDelegates": true
  }
}
```

Then run the obfuscator over your built assembly:

```bash
nebula protect ./bin/Release/net8.0/App.dll \
  --config nebula.config.json \
  --out ./protected/
```

`mode` picks the indirection: `delegate` (fast, keeps a static `ldftn` reference — safe for trimming), `reflection` (hides the name via token, costly and linker-hostile), or `calli` (fastest indirection, no name, most fragile). `scope` is the dial that matters: proxying *every* call bloats the assembly and taxes startup, so point it at the methods that actually reach your IP — licence verification, key derivation, crypto, sensitive P/Invoke — and leave hot inner loops alone.

## The cost, honestly

Indirection is never free, and the three forms are not equally cheap.

- **Performance.** A cached-delegate call is a virtual dispatch plus the delegate's own indirection — nanoseconds, fine outside a tight loop. `calli` is similar. **Reflection is in a different universe**: `MethodInfo.Invoke` is on the order of hundreds of times slower than a direct call and boxes every argument and the return value into `object`, allocating on each invocation. Never put a reflection proxy on a hot path.
- **Trimming and AOT.** This is the sharp edge. A trimmer and the NativeAOT compiler decide what to keep by following *static* calls. A reflection proxy's target is reachable only through a token inside `Invoke`, which they cannot see — so the target may be **trimmed away** or fail to resolve under AOT, turning a hidden call into a runtime `MissingMethodException`. Delegate and `calli` proxies keep a static `ldftn` reference, so the linker still roots the target. If you publish trimmed or AOT, prefer `delegate` and verify with a published build.
- **Debuggability.** A proxied stack trace shows `Proxy0 → Func.Invoke` instead of your real method, and reflection frames are noisy. Keep your renaming/symbol artifacts so you can retrace production traces back to source.

## Where it breaks

Be clear-eyed about what this does and does not buy. Proxy calls defeat the *cheap* attack — the call-instruction scan and the auto-generated call graph that a decompiler or a tool like de4dot produces in one click. They do **not** defeat a determined analyst. The delegate target, the resolved token, and the function pointer all point somewhere real at runtime, and dataflow analysis or a single breakpoint on `Invoke` recovers it. What changes is the *cost*: "list every call into `Cryptography`" stops being a menu item and becomes a per-site investigation. Combined with renaming, string encryption and control-flow obfuscation, that is often the difference between a one-afternoon decompile and a project an attacker has to decide is worth their week.

## Which tier

Delegate and token-based proxy calls are a **Pro** feature, alongside code virtualization and method encryption. The most aggressive variant — reflection with runtime-computed tokens, correlated with the aggressive control-flow tier so the proxy dispatch is itself buried in bogus branches — is **Enterprise**. As always, it is legitimate protection for your own IP: raise the floor across the whole assembly with renaming and strings, then spend proxy indirection where the sensitive edges actually are. See [Nebula.NET](https://delta1labs.com/products/nebula) and the [documentation](https://delta1labs.com/docs/nebula/).

