Decompiling .net for dummies

How to decompile .NET applications – 5 easy steps

Dotnetsafer
Dotnetsafer

Why do we explain how to decompile .NET applications?
From Dotnetsafer 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.

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

Disclaimer This 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.

Note: To carry out the practical parts we use DnSpy, but we provide you with the same tool from https://decompiler.dotnetsafer.com so that you can carry out the tutorial from your browser without having to download anything ♥. (Simply to decompile drag the .NET application to the left part shown on the screen, the code will be displayed on the right part).

What is decompilation

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

That is, the COMPILATION is basically:

  1. You write code
  2. You push the magic compile 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 🤭.

The process will be something like this:

compile .NET applications

Well, decompile process is the opposite, you have an executable file, .dll, .exe…, and with a tool (decompiler) you get the original source code.

decompile .NET applications

And is it simple 🙄?
I’d say it’s easier than compiling, but let’s see how it’s done.

Decompile .NET / .NET Core applications

Let’s decompile a simple application:

decompile .NET applications

For this example we will use DnSpy. There are several tools and each one is good for something in particular, but DnSpy is one of the best for decompiling and debugging .NET applications.

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:

decompile .NET applications

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

dnspy .net core application

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:

source code .net application decompiled

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:

dnspy breakpoint on a decompiled .NET application

and then we will run the application:

dnspy debug on a decompiled .NET application

Here is a simple example:

decompile .NET applications

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:

craking decompiled .net core application

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:

changing cil body .net core application

and we compile again:

compiling .net core application

Now to save our modified application, we will give:

saving module dnspy

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

metadata dnspy

and we will have our modified application:

modified net core app

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, ASP, Xamarin, Blazor, everything works in the same way, let’s see then with a Blazor application.

Decompiling a web application with Blazor

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

decompile Blazor . NET application

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:

decompile .NET applications

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:

.net blazor file

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:

decompiling blazor with DnSpy

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

Decompiling a mobile app with Xamarin

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 🆓):

decompiling apk

and we will copy all the files in a folder:

decompiling xamarin apk

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

.net xamarin files

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

dnspy error decompiling xamarin files

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:

hacking .net

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:

https://dev-to-uploads.s3.amazonaws.com/i/ua8pyhcf6ard0h1dx2pl.png

python -m pip install lz4 or simply pip install lz4

Use of the decompression script

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:

decompress .net xamarin applications

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?

decompile and hacking xamarin applications

Now we have the files ready to open with DnSpy:

decompiling xamarin applications with dnspy

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 C #, 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.

We could do some interesting practices or whatever you want to see around here.

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

You have the web version of Dnspy at https://decompiler.dotnetsafer.com so that you can comfortably do all the tests in this article without having to download any program.

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

goodbye
Summary
How to decompile .NET applications - 5 easy steps
Article Name
How to decompile .NET applications - 5 easy steps
Description
Do you know how easy it is to decompile .NET applications? I explain in a simple way how to decompile .NET applications.
Author
Publisher Name
Dotnetsafer
Publisher Logo

0 Comments

Leave a Reply

More great articles

Blazor Desktop

Blazor Desktop: Like Electron for Javascript but… for .NET?

Blazor Desktop: Electron for .NET? 🤔 Not too long ago, Blazor WebAssembly was just a runtime experiment for C# in a Microsoft…

Read Story
github copilot

GitHub Copilot: Will artificial intelligence put us out of job?

I’m sure you’ve seen the announcement for Copilot, the new Github product that uses Artificial Intelligence to suggest code to you. At the time it…

Read Story
Top 5 tools for developoer

Top 5 tools for any .NET developer

Visual Studio is the best tool for .NET development thanks to all the facilities it offers us. Although it is very complete, it…

Read Story
Arrow-up