Thursday 16 March 2023

How to get code from a published ASP.Net build where we don't have source code?


If you have ever lost or misplaced your source code for an ASP.Net web application, you might wonder if there is a way to recover it from the published build on your server. The good news is that it is possible, but only with some challenges and limitations. In this blog post, I will explain how to get code from a published ASP.Net build using some tools and techniques.


What is a published ASP.Net build?

A published ASP.Net build results from compiling and packaging your web application into a set of files that can be deployed to a web server. Depending on the type of project you are using (WebForms or MVC), the structure and content of these files may vary.



--> For WebForms projects, a published build typically consists of the following:
- **.aspx** files: These web pages contain HTML markup and server-side controls. They are usually not compiled and can be edited directly.
- **bin** folder: This folder contains one or more **.dll** files that are the compiled code-behind classes for your web pages and other components. These files are binary and cannot be edited directly.
- **web.config** file: This file contains configuration settings for your web application, such as connection strings, app settings, etc.
- Other optional files and folders: These may include images, scripts, stylesheets, etc.



--> For MVC projects, a published build typically consists of the following:
- **Views** folder: This folder contains **.cshtml** or **.vbhtml** files that are the Razor views for your web pages. They are usually not compiled and can be edited directly.
- **bin** folder: This folder contains one or more **.dll** files that are your web application's compiled controllers, models, helpers, and other components. These files are binary and cannot be edited directly.
- **web.config** file: This file contains configuration settings for your web application, such as connection strings, app settings, etc.
- Other optional files and folders: These may include images, scripts, stylesheets, etc.
 

How to get code from .dll files?

The main challenge in getting code from a published ASP.Net build is to extract the source code from the .dll files in the bin folder. These files contain intermediate language (IL) code that can be executed by the .Net runtime but cannot be easily read by humans.
To get code from .dll files, we need to use a tool called a decompiler. A decompiler program can reverse-engineer IL code into high-level source code in C#, VB.NET or other languages.
There are many decompilers available online, some of them are free, and some of them are paid. Some examples of popular decompilers are:
- [ILSpy](https://github.com/icsharpcode/ILSpy): A free open-source decompiler with a graphical user interface (GUI) and command-line options.
- [dotPeek](https://www.jetbrains.com/decompiler/): A free decompiler by JetBrains with a GUI and integration with Visual Studio.
- [dnSpy](https://github.com/dnSpy/dnSpy): A free open-source decompiler with advanced features such as debugging and editing IL code.

To use any of these tools, you need to open the .dll file(s) in their GUI or command-line interface and browse through their namespaces, classes, methods, properties, etc., until you find the piece of code you are looking. You can copy the source code into a text editor or save it as a new file. However, there are some limitations and caveats when using decompilers:
- The source code may differ from the original one because some information may be lost during compilation (such as comments, variable names, formatting, etc.).
- The source code may only compile with errors because some dependencies (such as references, libraries, frameworks, etc.) may be missing or incompatible.
- The source code may not run correctly because some logic or functionality may be altered or broken during compilation or decompilation (such as encryption, obfuscation, optimization, etc.).


No comments:

Post a Comment

How to get code from a published ASP.Net build where we don't have source code?

If you have ever lost or misplaced your source code for an ASP.Net web application, you might wonder if there is a way to recover it from th...