Protecting intellectual properties in .NET, Part 1.

One thing that bothers many people and organizations about .NET is the ease of which IL code can be re-hydrated into source code (C#/VB/etc.).  While this has always been a problem with binaries, IL code is a much smaller set of instructions compared to the instruction sets of today's processors and is designed around accommodating high-level-language usage patterns–making it easier to translate into high-level source code.  Native binaries could always be disassembled and the assembler code be reassembled into another, new, application.  But, it was assembler code, and optimized–nearly impossible to translate into a high-level-language, let alone similar to the original code .  Everyone seems fine with this because it doesn't really look like the original code.  .NET IL an be re-hydrated into source that is almost identical to the original–sans comments.

One thing you can do with .NET is to force a method to be precompiled to native code.  This can be done by attributing the method with a little-known, not well documented attribute: System.Runtime.CompilerServices.MethodImpl and setting the MethodCodeType property to System.Runtime.CompilerServices.MethodCodeType.Native.

Some caveats

  • Seems to only work in a FullTrust environment.
  • Since native methods can't be reflected these methods cannot be uses as event handlers
  • May cause some grief for many debuggers.
  • The Runtime severely restricts where methods tagged as Native can be loaded.

One thought on “Protecting intellectual properties in .NET, Part 1.

  1. Hello Peter.

    I’m Jorge from Portugal and we at PG Stream are trying to hide completelly our licensing code, without having to buy additional tools for achieving it. We’ve tried obfuscators but they don’t totally get it as they always seem to leave something behind.
    So I used your approach that seemed fine, but I always received an exception on execution, that stated something as:

    Unhandled Exception: System.TypeLoadException: Managed native not presently supported.

    Of course I know what this stuff means but am I doing something wrong? I say this because you wrote the real caveats of this feature and, for some peace of mind, I would like to know if I went straight on a caveat,or if this stuff really is not supported.

    So I’m asking you for some guidance and sample code if you have the time.

    Thanks in advance.

Leave a Reply

Your email address will not be published. Required fields are marked *