Most obfuscation you have read about attacks the shape of a method — flatten the graph, plant a bogus branch, scatter the state. All of it still lives inside ordinary, linear control flow: branches, loops, calls. There is one corner of the CLR where control flow is not linear at all, where a block of your code runs before the stack has even begun to unwind, and where decompilers have historically been at their weakest. That corner is the exception filter — the catch (…) when (…) clause — and it makes a surprisingly good place to hide a decision.
This post is about using filters as an opaque dispatch primitive: moving a real branch out of the normal flow and into a filter predicate whose answer is fixed but whose timing and structure a tool cannot model with a linear-handler assumption.
The two-pass model nobody draws
Structured exception handling on the CLR is not one pass. When a throw executes, the runtime runs the handler search in two distinct passes, and the gap between them is the whole trick.
First pass — search. The runtime walks up the chain of enclosing handlers from the throw site, and for each one that has a filter, it calls the filter funclet to ask a single question: do you want this exception? The filter returns EXCEPTION_EXECUTE_HANDLER (1) or EXCEPTION_CONTINUE_SEARCH (0). Crucially, during this pass nothing is unwound. No finally block runs. The faulting frame is still live, its locals still on the stack exactly as they were at the point of the throw.
Second pass — unwind. Only once a handler has been selected does the runtime unwind: it runs every finally and fault block between the throw site and the chosen handler, pops those frames, and then transfers control into the handler body.
An ordinary catch body runs entirely in the second pass. A filter runs in the first pass — earlier than any finally, earlier than any unwinding. That is the one fact that makes filters distinct, and it is the fact a deobfuscator has to respect.
The real question
If a filter is just a boolean that the runtime consults, what stops us from putting the decision itself — and its side effects — inside that boolean, and making the predicate opaque so a tool cannot see that it always answers the same way?
Nothing does. Here is an ordinary licensing check:
bool Validate(string key) { bool ok = Checksum(key) == _expected && Activate(key); return ok; } A decompiler hands this straight back: two calls, an &&, a return. The branch is right there to read. Now route it through a filter.
The lowered shape
Nebula rewrites the method so the real work happens inside a filter predicate that is always true, raising a private signal to enter the try and letting the filter pass do the deciding:
bool Validate(string key) { bool ok = false; try { throw new DispatchSignal(); } catch (DispatchSignal) when (Route(key, out ok)) { return ok; } } // Always returns true; the real decision is the out-parameter side effect. bool Route(string key, out bool ok) { ok = Checksum(key) == _expected && Activate(key); int x = key.Length; return (x * (x + 1) & 1) == 0; // x*(x+1) is always even -> opaque-true } Read what an analyst now sees. A method throws a private exception at itself, immediately, unconditionally. A catch … when (Route(…)) guards the handler. The actual decision — the checksum, the activation — is buried as a side effect of a predicate whose return value is a number-theoretic identity: x * (x + 1) is even for every integer, so Route always returns true and the handler always runs. The analyst cannot read the flow as "call, compare, return"; they have to understand that the body executed inside a filter, during the search pass, before the try even finished unwinding.
The IL: filter … endfilter … handler
At the IL level the clause is not a catch at all — it is a filtered handler, described by a clause record and three regions. Trimmed to the shape that matters:
.try IL_0000 to IL_0006 filter IL_0006 handler IL_0028 to IL_002b // --- protected region: raise the dispatch signal --- IL_0000: newobj instance void Demo.DispatchSignal::.ctor() IL_0005: throw // --- filter funclet: runs in PASS 1, stack still intact --- IL_0006: pop // the exception object, unused IL_0007: ldarg.0 IL_0008: ldarg.1 // key IL_0009: ldloca.s ok IL_000b: call instance bool Demo.Validator::Route(string, bool&) IL_0010: pop // Route's real decision is now in `ok` IL_0011: ldloc.1 // x : a runtime value IL_0012: ldloc.1 IL_0013: ldc.i4.1 IL_0014: add IL_0015: mul // x * (x + 1) — provably even IL_0016: ldc.i4.1 IL_0017: and IL_0018: ldc.i4.0 IL_0019: ceq // (x*(x+1) & 1) == 0 -> always 1 IL_001b: endfilter // leaves 1 -> EXECUTE_HANDLER (always) // --- handler: runs in PASS 2, after unwind --- IL_0028: pop // the exception object, unused IL_0029: ldloc.0 // ok, computed during the filter pass IL_002a: ret Three things are worth staring at. The clause record names a filter region that is a separate stretch of IL with its own entry — the runtime enters it with the exception reference on the stack. It terminates with endfilter, which pops a single int32: the 0-or-1 answer. And the handler begins after endfilter, also with the exception on the stack. The opaque predicate sits entirely inside the filter region, so the value that decides whether the handler runs is computed in the first pass — and so is Route's side effect that sets ok.
The decision is made inside the filter funclet during the first pass. By the time the handler body runs in the second pass, the real work — the
out side effect — is already done. Why decompilers and de4dot lose
Generic decompilers and de4dot-style deobfuscators are built around a model of exception handling as a set of linear handlers: a protected region and a catch body that runs after the exception is caught. A filter breaks three of their working assumptions at once.
They must model a second control-flow region. The filter is a funclet — its own little method body with its own branches that produces a boolean. A tool that only reconstructs try/catch/finally has to specially recognise the filtered-handler clause, decompile the funclet, and render it back as a when. Tools vary wildly here; many render filters awkwardly, inline them incorrectly, or give up and emit a raw clause. Any decision you hide in that funclet inherits that fragility.
They cannot hoist the filter into the handler. The obvious "simplification" — move the filter body into the start of the catch, since it always runs anyway — is unsound, and a careful deobfuscator knows it. The filter runs in pass 1; the catch body runs in pass 2. Between them, every intervening finally executes. Move the code across that boundary and you reorder it relative to those finally blocks and relative to any outer filter — an observable behaviour change. A deobfuscator's prime directive is do not change behaviour, so the safe move is to keep the filter exactly where it is, which is precisely what we want.
The predicate has no constant to fold. (x * (x + 1) & 1) == 0 holds for every integer because the product of consecutive integers is even. That is a property of arithmetic, not a literal in the IL. A constant folder has nothing to grab, so it cannot prove the filter is always-true and collapse the try. The branch — and the two-pass machinery around it — survives cleanup intact.
Applying it in Nebula
Filter-based dispatch is a per-method control-flow transform. In Nebula it lives under the control-flow section of your project config. These are illustrative option names, not internal keys:
{ "renaming": { "mode": "dictionary" }, "encryptStrings": true, "controlFlow": { "filterDispatch": true, "opaquePredicates": true, "scope": ["MyApp.Licensing.*", "MyApp.Core.KeyCheck"] } } Then run the obfuscator over your built assembly:
nebula protect ./bin/Release/net8.0/MyApp.dll \ --config nebula.config.json \ --out ./protected/ The scope is the important dial. Filter dispatch earns its cost only on the handful of decisions worth protecting — a license verification, a key-derivation gate, one critical business rule — and should be nowhere near your hot paths. Pair it with encryptStrings, because, like every structural transform, it hides where a decision happens, not what it checks: a plaintext error message or magic constant re-anchors an analyst instantly. Filter dispatch is a Pro-tier control-flow transform; combine it with the string and constant protection in the same tier.
Caveats and when it breaks
Filters are a sharp tool, and the honest limits matter more than the trick.
- Exceptions are slow. Raising and dispatching an exception costs on the order of microseconds — orders of magnitude more than a branch. This is a cold-path technique for sensitive decisions, full stop. Put it in a loop and you will feel it.
- Keep filters total. A filter that throws is, on modern runtimes, swallowed and treated as
CONTINUE_SEARCH. If your opaque predicate can fault — a null deref, an overflow in checked context — you will silently skip the handler and change behaviour. Nebula keeps the generated predicate side-effect-safe and total; if you hand-roll one, do the same. - It interacts with debuggers. Filters run in the first pass, which makes first-chance exception noise, "break on unhandled", and stepping through a
whenclause genuinely confusing — for you, during support, as much as for an attacker. Keep your renaming map and symbol artifacts so you can retrace production stacks. - It encrypts nothing. The funclet IL is plain IL. A determined human who understands the two-pass model can read it, recover the
outside effect, and rewrite the method. Filter dispatch raises the cost of automated and casual analysis; it does not lock the door.
Takeaway
Exception filters give you a place to put a decision that is off the linear path entirely — a funclet that runs in the first pass of a two-pass model, before any unwinding, guarded by a predicate a constant folder cannot collapse. That combination is what frustrates decompilers and de4dot-style tools: they must model the filter funclet and the pass-1/pass-2 boundary faithfully, and they cannot simplify across it without risking correctness. Use it the way it pays off — on cold, sensitive decisions, scoped tightly, with the literals encrypted alongside — and a routine decompile turns into a careful reading of the CLR's exception machinery. Use it everywhere and you have just made your program slow and your own stack traces unreadable.
