How To Decompile .NET Applications (Easy)✅

How To Decompile .NET Applications (Easy)✅
December 2, 2021
13 minutes read

DisclaimerThis article is for educational and training orientation, we are not responsible for the misuse of the techniques and explanations mentioned in this article and/or the use of the tools mentioned/provided, also we do not assume responsibility for the actions carried out with the information of the same. Please use this information for ethical purposes.

All tools and decompilers mentioned are our own recommendations, nothing sponsored.


Why decompile .NET applications?

From ByteHide we do this article for a very simple reason, in our offices (a coworking space) there are quite a few companies that are developed in .NET.

One day at lunch I told them what we were doing and they didn’t understand anything, it turns out that they had no idea what decompilation is, they didn’t know that with a simple tool they could see, copy or modify their development.

Not in all cases you should worry, but for some of them the core of the business is the development itself and obviously it was not very funny.

I have to say that this happens in all compiled languages, but I will focus on .NET which is what we master.

What could be better than a step-by-step tutorial on how to decompile .NET applications?

In this article I am going to show basic notions about decompilation and reverse engineering, if you are interested in something more complex, do not hesitate to ask us!

There are many .NET decompilers that you can use as an alternative to DnSpy like ilspy, justDecompile telerik decompiler, jetbrains dotPeek decompiler, Spices .NET decompiler, Redgate .NET Reflector and more (you can find them for Windows, Linux or macOs).

What is decompilation in .NET?

You might know that C# is a high level programming language and it uses a combination of static and JIT compilation. This means that the code is compiled down to bytecode.

Decompilation is the reverse of compilation (I’m going to patent this great explanation).

That is, the COMPILATION is basically:

  • You write code
  • You push the magic  button and it becomes an executable/file/.exe/dll… (well, it should… surely you forgot a semicolon or something like that 😪) you will also have 103 warnings ⚠, but if you don’t open your eyes, you don’t see them 🤭.

Well, decompile process is the opposite, you have an executable file, .dll, .exe…, and with a tool (decompiler) you get the original source code (in other words, the compiled code is converted into readable source code).

bytehide.com

And is it simple?🙄

I’d say it’s easier than compiling, but let’s see how it’s done.

If you are wondering how long it takes to decompile a .NET application, too little.

How To Decompile .NET and .NET Core Applications

Let’s decompile a simple .NET application:

bytehide.com

In order to explain it well, we will use the DnSpy C# decompiler. There are several dll decompilers and each one is good for something in particular, but DnSpy is one of the best for decompiling and debugging .NET applications.

This can be used for any type of .NET assembly. It doesn’t matter if it is an ASP.NET web application or VB.NET application or even mobile applications (apk). Anything within the .NET Framework.

When we compile our .NET Core application, we will obtain the compiled files, in the case of .NET Core the .exe file will be the executable, but it will be in charge of executing the .dll that contains the code of our application.

Ok, let’s proceed to decompile the .dll file:

bytehide.com

To do this, all we have to do is drag it to DnSpy:

bytehide.com

Once loaded, the original source code will be shown and we can work on it, we can analyze it, modify it, debug it, etc.

Viewing the source code of the decompiled .NET application

Once our application is decompiled, we can navigate through the source code as if it were our own project:

bytehide.com

Debug the .NET application

This will be useful to understand how it works or to obtain some values in memory.

For example, let’s put a breakpoint before doing a subtraction:

bytehide.com

and then we will run the application:

bytehide.com

Here is a simple example:

bytehide.com

This is a function that can be very useful at times, but it can also be dangerous ⚡.

Modify the code of the decompiled .NET application

We can modify and re-compile the decompiled .NET application without any problem:

bytehide.com

We can also do it using IL code, but if we do not know it it will be much more complex.

We change that simple text:

bytehide.com

and we compile again:

bytehide.com

Now to save our modified application, we will give:

bytehide.com

We will select the name of the file, its path and we will mark the following options:

bytehide.com

and we will have our modified application:

bytehide.com

Ok, these are simple and unimportant examples, I leave to your imagination 💭 everything that could be done with these simple tools.

Now we are going to see other types of applications, because compile process in .NET is the same, it does not matter if you use C#, VB (Visual Basic , ASP, Xamarin, Blazor, everything works in the same way, let’s see then with a Blazor application.

Decompiling a Blazor Web Application

Now we have created a sample web application with Blazor, this is the application code in visual studio:

bytehide.com

Basically it is an application that shows the weather and we have added Dotnetsafer in the middle so that we can see it later in the decompiled application, here is the web application:

bytehide.com

Ok, let’s get the code again.

First we will use our browser (it does not matter which one), since when loading the .NET libraries to display the application, it makes a call to obtain them.

We must enter inspection mode Ctrl + Shift + C and go to section Network, later reload the page with Ctrl + F5.

Here will be the file that contains the code of our web application:

bytehide.com

You can also obtain all the dependencies or own .net files that are used by the application

Well, we download it and open it again with DnSpy:

bytehide.com

Once we have the decompiled file we can do everything that I mentioned in the previous point.

Decompiling a Xamarin Mobile Application

We raised the level of difficulty a little 🧨, but the operation is still the same.

We have created a simple application for mobile devices with Xamarin, the compiled application will have the extension .APK

The first thing we will do is unzip the APK, there are tools for this, but it is not necessary, for something so simple we can use winrar, (yes, the program that never expires 🆓):

bytehide.com

and we will copy all the files in a folder:

bytehide.com

Inside the folder Assemblies all .NET files will be found:

bytehide.com

Here comes the interesting thing, these files have lz4 compression, which will give us an error if we try to decompile it with DnSpy:

bytehide.com

For this I bring a simple solution, a small Python script that will help us decompress all the .NET .dll files.

Yes, this sounds very complex, and it seems that you must be an expert:

bytehide.com

But it really is something very simple that we will do in less than a minute.

Decompress Xamarin .NET Libraries

If you don’t have Python on your machine, install it previously.

Once we have Python (I run it from the VSCode terminal) we proceed:

Install lz4:

bytehide.com

python -m pip install lz4 or simply pip install lz4

Use of the decompression script

Loading code snippet...

The author of this script is Christian Reitter, X41 D-Sec GmbH, thank you for your article at X41 D-Sec GmbH, we have modified it to unzip all the files in the directory.

We will copy the code into a file called decompress.py and we will save it in the same path as our .dll files.

Decompress the files we need

bytehide.com

We will do it with py .\decompressor.py .\, we’ll let it decompress all the .dll files in the directory.

In this case I am only interested in the two files corresponding to my application.

What has not been so difficult?

bytehide.com

Now we have the files ready to open with the DnSpy dll decompiler:

bytehide.com

Now again we can do everything I mentioned earlier, the procedure is always the same, only the way in which the files are compiled varies.

This applies to all applications under .NET, such as games developed in Unity with #, virtual reality applications and augmented reality.

Did you like this article?

It has been very simple but I consider it fine for a basic level and learn a little about these topics 😊.

If you have found it interesting, do not hesitate to support it, and tell us below 👇 if you want to know something else, like let’s talk about crackinginjection and other aspects.

Also, if you want to learn more about decompiling and reverse engineering I would recommend you to look for reverse engineering forums (yes, there are such forums).

Thank you for reading the article ❤ and remember that in ByteHide we will be uploading articles about security in relation to .NET 🛡.

Thank you for your attention, we hope you have learned a lot!

bytehide.com

You May Also Like

Optional Parameters in C#: What You Need to Know

Optional Parameters in C#: What You Need to Know

Programming in C# often requires flexibility and simplicity ...

What is Lock Keyword in C#? Main Usages

What is Lock Keyword in C#? Main Usages

IndexUnderstanding the C# Lock Keyword Can’t tell a lock fro...

Enumerate in C#: Detailed Explanation

Enumerate in C#: Detailed Explanation

Desperate to decode the mystery that is enumeration in C#? I...

Creating a JSON Class in C#: Detailed Guide

Creating a JSON Class in C#: Detailed Guide

In the world of application development, manipulation of dat...

Static Class in C#: How to Use It?

Static Class in C#: How to Use It?

Hello there, future C# aficionado! It’s time to roll down th...

DateTime Formatting in C#: Dev Guide

DateTime Formatting in C#: Dev Guide

Have you ever gotten frustrated handling dates and times in ...

Leave a reply

Loading comment form...

Comments (1)

Umair

Thank you for the article. I have a windows software exe. Is it in any way possible to convert it into android app?

Loading reply form...