Compiling C # in Native?

I think I'm somewhat confused about compiling .NET bytecode with native code, or maybe I'm confused about the end result. So please be with me when I try to figure out what I think I understand so that you can help me figure out what I am missing.

What I would like to do is compile my application written in C # to the usual native code , as I would understand if I wrote it in C. My reasoning has nothing to do with performance, but rather with some degree of protection. I understand that my ultimate goal is not impossible (or even very difficult) to get around, but I just feel that reversing the x86 assembly is harder than reversing what Reflector gives me.

Right now, if I drop my C # application in Reflector, I basically return the source code. Usually, when I drop my unmanaged C / C ++ applications into IDAPro and use the HexRays decompiler, I don't quite get the same degree of decompilation, and I have to resort to wading through x86 parsing to understand the logical flow. I understand that such excellent decompilation comes from Reflector because the application is in MSIL instead of the more complex code that HexRays is trying to decompile.

I have no problem with a client machine still requiring .NET runtime. I am not trying to get around this. I would like to run regular obfuscation programs like upx in my program and do it like a .NET binary.

It was my understanding from this related question that ngen doing what I want. I tried using ngen . But after copying the output file from the directory C:\Windows\assemblies\...\applicationName.ni.exe somewhere, I can double-click somewhere, and trying to start it leads to an error about the absence of a “valid Win32 application”. Also, when I throw applicationName.ni.exe into Reflector, I get the same result as me, only with applicationName.exe . Since applicationName.ni.exe should be native code, I expected Reflector to fail, but it is not. If so, how should I do this, why did Reflector give me such a great decompilation anyway?

So, just to summarize my main question again: how can I compile my .NET program into a native binary that Reflector will not decompile so easily? Or some recommendations on how to protect a product written in .NET from newcomers-reversers?

If I need another tool, I would prefer something free rather than something like Codewall .

Thank!

UPDATE: I understand that what I'm looking for may limit some language features, such as Reflection, but I think I'm fine with that. None of my codes have explicit calls to Assembly.Load or anything like that. But could they just replace with GetProcAddress/LoadLibrary calls?

+64
compiler-construction c # native
Dec 17 '09 at 13:03
source share
11 answers

I just tested .Net Native on VS2015 and Windows 8.1 (if configured correctly, check .proj to check) and to build a specific architecture (maybe redundant, not confirmed), I will create my own file that will make it more difficult for you to reverse engineer the code ", which you looking for I was unable to read .dll through DotPeek (a free NetNet decompiler from JetBrains).

+21
Jun 15 '15 at 13:54 on
source share

This is not how ngen.exe works. It simply runs the JIT compiler to create the .ni.exe or .ni.dll module. This binary does not contain metadata, but only machine code generated from IL for the method bodies. The CLR still has to find the original assembly. Only then can he determine that there is an available image so that he can use machine code, and not generate it from the IL assembly.

Ngen.exe speeds up the startup time of your application, that's all.

My usual advice to anyone who might be interested in disassembling my builds is to point them to sourceforge.net. It has terabytes of source code written and supported by programmers who are usually better than me. Sometimes even with good comments. If your obfuscator is not working well, then shop around for the best. A lot of them.

+29
Dec 17 '09 at 13:21
source share

Yesterday, at Build 2014 , Microsoft announced the .NET Native . According to frequently asked questions , "... First, we focus on Windows Store applications with .NET Native. In the long run, we will continue to improve our own compilation for all .NET."

+18
03 Apr '14 at
source share

If you want to protect your code, an obfuscator is typical. Dotfuscator has long been in the arms race with a reflector, and we use it on our products. In practice, however, a skilled person can easily read code obfuscation.

Compiling into native code hits the goal of using a managed language. The main advantage is that it allows the target JIT IL environment to be optimally acceptable for the target CPU. If you want otherwise, you should use something like an option

+17
Dec 17 '09 at 13:09
source share

A spoon (formerly Xenocode) is a product that can suit your needs . We use it for the WPF-based installer user interface, so we do not need to download .net to load the installer itself.

+8
Dec 17 '09 at 13:37
source share

NGEN adds native code, but it does not remove MSIL. Therefore, any tool running on MSIL can work. You also need this for reflection, which would be very difficult for a true native compiler.

+7
Dec 17 '09 at 13:10
source share

it's a free obfuscator that is very quiet: eazfuscator

+5
Sep 08 '10 at 12:01
source share

Finally, it is possible using the native Microsoft.NET compiler

It automatically compiles the release version of applications written in managed code (C # or Visual Basic) for the .NET Framework and Windows 10 into native code.

..

• Your applications will provide superior native code performance.

• You can continue programming in C # or Visual Basic.

• You can continue to use resources provided by the .NET Framework, including a class library, automatic memory management and garbage collection, as well as exception handling.

For users of your .NET applications, Native offers the following benefits:

• Fast lead time

• Constantly fast start times

• Low deployment and upgrade costs

• Optimized application memory usage

But .NET Native involves more than compiling into native code. It transforms the way you create and run .NET Framework applications. In particular:

• During pre-compilation, the necessary parts of the .NET Framework are statically linked to your application. This allows the application to work with local application libraries of the .NET Framework, and the compiler to perform global analysis to ensure performance gains. As a result, applications run stably faster even after .NET Framework updates.

• The .NET Native environment is optimized for static pre-compilation and, therefore, can provide superior performance. At the same time, it retains the basic reflection functions that developers consider so productive.

• .NET Native uses the same server side as the C ++ compiler, which is optimized for static precompilation scripts.

https://msdn.microsoft.com/en-us/library/dn584397(v=vs.110).aspx

This is only available with VS.NET 2015.

+5
Oct 29 '15 at 15:32
source share

I can step back and ask why you are looking for this type of protection. I’m not trying to say that you don’t need protection, but I think it’s worth understanding the motivation.

For example, if you want to protect, because you have an algorithm in your system where it would be destructive for security, if someone turned it into a design, then you might have to consider a different approach. This means that there is a flaw in the algorithm, and no amount of obfuscation or native compilation will help you.

If it comes to IP, then I think obfuscation is probably your best approach. It is like putting a lock on a door. Someone may break the lock and enter, but they intentionally do it, and not just walk through the door.

+4
Dec 17 '09 at 13:43
source share

This is possible using the IL2CPU compiler. IL2CPU is developed by the same people who make COSMOS (C # open source managed operating system) and are only available when loading space. IL2CPU creates ASM files that can be compiled through Nasm (some other assemblers may work, but it is better to use nasm). the only problem with IL2CPU is that it is built into the Cosmos project and it is quite difficult to run it yourself.

+3
Aug 04 '13 at 15:53
source share

Other answers mention Microsoft's own .NET compiler, but it doesn't say how to do it.

Here 's a tutorial on compiling your C# project using CoreRT .

Note: I also had to comment out the following line for everything to work:

 <!-- <add key="helloworld" value="https://api.helloworld.org/v3/index.json" /> --> 

The result is an executable file of approximately 4MB :

Indeed, code is no longer readable with .NET decompilers, and IDA Pro (native code disassembler) recognizes it as native code.

+3
Apr 18 '19 at 10:44
source share



All Articles