Decompiling .NET Applications

Posted by Filip Ekberg on February 14 2013 14 Comments

There are many reasons to why you might want to decompile an application after it’s been compiled. Compiling C# code “just” translates it into MS IL. The compiler of course does some magic and tweaks the code as much as possible. There’s no metadata stored after compilation which means that comments and such will not be available in the IL output.

The following image illustrates what happens when we compile something, we put the C# code into a basket and tell the compiler to give us a binary of this which is sort of a black box at the moment. We know that whenever we want to use this black box we have something behind the curtain that knows how to open it and use it properly (read: CLR).

Compiling C# Code

Let’s consider a basic variable instantiation and an equality check, when this is compiled it will output something partially readable. To me the output is readable but that’s just because I have a weird love for IL. When this basic snippet was compiled using LINQPad it generated some IL which you can see below.

Compiled C# Code

Imagine that you got a DLL from an old co-worker and the code is long gone but you need to make some changes to the code. What do you do? One option is to mimic the functionality if the application is not too big and create it from scratch but that is just cumbersome. Instead what we want to do is something like you can see illustrated below; we want to go back from IL to C#!

Decompiling C# Code

So how do we do this? By using a decompiler!

As I tend to do this quite often to understand how libraries work that I have no control over, I have tried some different tools for just this cause. Let’s take a look at four of the most common ones on the market. Don’t worry, there’s both free versions and paid ones out there!

Telerik JustDecompile

The first one that we’re looking at is a product from the Just* family created by Telerik. I do like the products from Telerik so this one should be quite interesting!

JustDecompile is completely free and available for download over at Teleriks website. One thing that I didn’t like thought was the installer, generally Telerik’s installers are pretty nice, but I don’t like being “guided” to install other stuff than what I’ve asked for.

Below is a screenshot of the installer and as you can see it advices you to install a lot of trials for other Telerik products.

JustDecompile Installer

After selecting only to install JustDecompile the installation will only take up 36MB. You’ll also need to create a Telerik account if you don’t already have one which can also be a hassle, but it’s free so why not!

I’ve setup a project that has the same variable declarations and the equality check from above and then compiled and opened the executable in JustDecompile. The result is quite similar to the original source as you can see in the following image.

Decompiling with JustDecompile

We can also select to show the result as IL instead of C# code!

Decompile with JustDecompile show IL

There are a couple of things that I didn’t find straight forward using JustDecompile.

Pros

  • It’s free!
  • It’s fast!
  • The UI is beautiful
  • It looks easy
  • Does what it should (partially)

Cons

  • There’s a button for creating a project, I expected it to be able to export my binary to a complete VS Solution but it’s grayed out and there’s no tooltip on why that is so.
  • Showing the source as VB instead of C# shows nothing at all, shouldn’t matter if the original code was C# or not.

JustDecompile Summary

Even though there are some cons; would I recommend you using it? Of course! If you haven’t installed it already go ahead and do so! It can only become better if more people support it and give them suggestions. There’s even a suggestion button where you can submit requests.

ILSpy

This one is interesting, ILSpy is an open source assembly browser and decompiler for .NET Applications! This means that if you don’t like what it does or if you have feature suggestions, “you can just” provide the fix yourself! The tool itself is equal to what JustDecompile offers but the installation process is much easier. You simply grab the binaries or source from the ILSpy website and unzip it wherever you want it!

ILSpy

I simply performed the same process as I did with JustDecompile; I started ILSpy and opened up my executable but this is where it gets interesting. The code that it decompiles to looks Exactly like the code that I wrote in Visual Studio as you can see in the following image.

ILSpy Decompile

Decompiling to VB also works right out of the box, this tool has what it takes!

ILSpy Decompile to VB

Now to the more interesting feature; Can we save/export this to a C# Project?

By the looks of it there’s a “Save Code” action in the File menu, selecting the assembly and then pressing ctrl+s or the “Save Code” action actually lets us save a csproj file! If you open up the location in your file explorer you will see that it actually generated a project file and the code file!

ILSpy Save project and Export Code files

I think we’ve looked enough at ILSpy to write up a pros and cons!

Pros

  • Free!
  • Open-Source, do I need to say more?
  • All features seem to work as they should
  • Easy to use interface
  • Fast!

Cons

  • The UI isn’t really that good lookig but it’s functional
  • The application seemed to freeze once but it just took a while to analyze a code file, I had to stretch for this one..

Summary

There really aren’t many bad parts regarding ILSpy, I like it but if I have to complain about something it’s the UI and that it felt like it froze once. I really recommend trying out ILSpy and looking over the code for it, it’s great for educational purposes and I have co-workers that use it all the time and like it a lot.

What are you waiting for, go download!

dotPeek

Just as the two mentioned above dotPeek is available for free, it’s not open source though. dotPeek comes from JetBrains and is available on their website where you can also find a lot of interesting information about how to use it.

Installing this is easy and doesn’t force you or ask you to install anything else than what you really want in this case.

dotPeek

If you are familiar with ReSharper (R#) which is also a product from JetBrains, the keyboard shortcuts will be something of value to you. In fact dotPeek uses the same navigation as you might be used to from using ReSharper!

Let’s have a look at what dotPeek thinks of our executable. When opening up dotPeek you’re meet with a beautiful interface that feels like it’s a part of the Visual Studio family (except for the very colorful icons). Opening up the executable and looking at the code you can see that it didn’t really give us the same result as any of the other decompilers we’ve looked at.

dotPeek looking at the code

So instead of actually displaying what the IL tells us, it analyzes the IL and optimizes the code for us which is really not what we want to do. There’s also no way of swapping between C#, VB.NET, F# or IL. So in this case we are “stuck” with looking at some C# code without knowing what IL it comes from.

What is also a downside to this is that you cannot export the code that you are looking at, which means that this is a pure code browser.

Pros

  • Beautiful UI
  • Fast
  • Code inspection and navigation that you might be used to from ReSharper
  • Supports plugins just like ReSharper

Cons

  • Lack features such as show code in different ways; swapping between VB.NET, F# and IL
  • Doesn’t resemble the actual code that was compiled
  • Doesn’t support exporting code or projects
  • The application itself seem to be very light-weight and lacking configuration possibilities

Summary

While I like JetBrains products in general, this one feels like there’s something missing. Personally when I use a decompiler I want it to be able to show me the output code in different ways and give me options to export it. But if you are looking for an assembly browser that decompiles to C# and just does that, this is perfect. Even better if you are used to ReSharper, you will certainly find the keyboard shortcuts for navigation handy.

.NET Reflector

Last but not least, my personal choice .NET Reflector! I’m not really sure why this is my personal favorite but keep on reading and you might find that it’s because the mixture of good functionality with a common and easy to use UI.

.NET Reflector is available by RedGate but costs money which is maybe why it’s “better” than the alternatives. This wasn’t always the case though once upon a time it was available for free. I was lucky enough to win a free license a while back so I’ve been able to use it quite a lot. You can buy it or grab a free trial over at RedGate’s website.

The installation is easy and doesn’t try to force you to install a lot of other things that you don’t expect. .NET Reflector comes with a Visual Studio extension that will let you use the features in Visual Studio instead of starting a new instance of .NET Reflector.

Just as with JustDecompile and ILSpy I opened up the executable that we looked at before and as expected it shows the same result as ILSpy does!

.NET Reflector

As you can see here the UI is quite minimal and easy to understand. What is interesting here is that it also allows us to view the code as F# code as you can see in the following image.

.NET Reflector F#

If we want to export the code that we have just as we did with ILSpy we can right click the assembly and select “Export source code” this then asks us where to store it and gives us information about the exported files. As you can see in the two following images it exported more files than ILSpy did.

.NET Reflector Export Code

.NET Reflector Export Code Window

And the resulted files look exactly as we expect them to, the code file has the same code as we did in the original source code.

Pros

  • Easy to use
  • Everything is intuitive
  • Everything work as intended
  • Exports more code than alternatives
  • Fast
  • Beautiful UI
  • Cons

  • It costs $368 + tax if you want the VS Extension and this is quite expensive when good alternatives are completely free
  • Summary

    The costs is the only downside that I’ve found during my time using it which means that if .NET Reflector was free tool for Decompiling .NET Applications it would be the number 1 choice. It isn’t free and the cost must be factored in when comparing the value of the product.

    Conclusion

    If I hadn’t won a free license for .NET Reflector I probably would have used JustDecompile or ILSpy as both of them are very good tools for Decompiling .NET Applications!

    We’ve looked at how we can decompile applications and browse the code in different ways when only having the executable available. This also works with libraries in the global assembly cache.

    Looking around in the .NET Framework to see how they’ve done certain implementations can be great for educational purposes but using this at work to understand how someone implements a certain feature can be priceless.

    I hope you’ve found this read interesting and that you’ll be digging deeper with tools for Decompiling .NET Applications. Let me know which one is your favorite or if you’ve used another tool than the four mentioned above!

    Vote on HN

    Friday with Filip – Being productive

    Posted by Filip Ekberg on September 28 2012 3 Comments

    Welcome to this week’s Friday with Filip!

    Earlier this week I wrote about how to organize your thoughts by using tools such as Evernote and XMind. Both of these are very easy to work with but are somewhat limited to getting your thoughts into a persistent format. Since productivity is important to all of us, I wanted to spend this Friday on exploring more tools that can be of great use to primarily developers. We live in an era where everything needs to be done as quickly as possible, with the highest quality and costs as little as possible. This can pose as a problem in many cases because they don’t quite fit that well together.

    In my previous article, Let’s write better software, I discuss how we’ve been entering a world with more and more hardware and software; whereas bugs are more an expectation than an exception. This is of course a big problem, since we all want bug-free world. As many have pointed out though, the biggest problem are the people that are unwilling to pay for quality; of course, that’s not the way it’s sold. In many cases it’s either getting the contract or not getting the contract which can lead to doing the same thing within a more narrow time period.

    Before this get’s out of topic, let me explain why this is about productivity. When we need to do something faster but still maintain the same amount of quality. We need tools to help us with analysis, suggestions and in programming we want help with refactoring. Without such tools, we would be less productive. This expands over many areas, not only development.

    The right tool for the right job

    This week I want to be more concrete and give hands on examples on what tools that I use in my day-to-day work environment. Feel free to leave suggestions on what productivity tools that work best for you and for your team.

    There are a couple of areas where I try to be as productive as possible (of course I always want to be productive!):

    • When I’m in a meetings
    • When I’m coding
    • When I follow up on a bug

    As I stated in my previous post organize your thoughts, I use some tools to get my thoughts down quickly. But it is also important when you’re meeting with a customer or a college and a bug is presented, this needs to be documented in a good way.I’ve been using Pivotal Tracker for a while now and I like it; I would however like to give TFS 2012 a fair chance.

    Pivotal Tracker allows us to track development, bugs, ideas and both give the project members and the customer a great overview of the project progress. This together with Evernote and XMind is very powerful and gives me a total set of powerful applications to use in a meeting.

    Another tool that I like to use, which makes my understanding of a system better, is balsamiq mockup. This mockup tool lets you create nice overviews of how the UI will look when the system is done. When you’re brainstorming with your customer around what the UI will look like, this is a very productive tool to have in your collection.

    When we’re done with a mockup, we can simply save it on the corresponding bug, task, feature or idea in Pivotal Tracker.

    When I’m coding it’s very important to me that my tools are not in the way of my development. For instance, I don’t want a lot of non-natural keystrokes or popups to use. I’ve been swapping between ReSharper and JustCode.

    When using ReSharper I get a lot of nice instant feedback on what I can do to improve my code, this makes it easier if someone is going to review the code at a later stage:

    JustCode supplies the same amount of keyboard mappings, analysis and refactoring capabilities but one other thing that it provides is a way to stick a cheat-sheet on your desktop. This makes it very productive when I need to get use to some keyboard shortcut:

    If you want to code productively, here are some recommendations:

    • Spend a while analyzing what keyboard shortcuts your IDE provides
    • Use productivity tools such as ReSharper and JustCode
    • If you’re using Visual Studio, learn how to use Snippets
    • Use a source control that is not in your way

    These might seem obvious, but adapting to these sure helps me being productive. If I can solve a problem faster with just as much quality, I can spend the additional time that I just won on making the solution better.

    What are your recommendations on being productive?

    Vote on HN

    Organize your thoughts

    Posted by Filip Ekberg on September 25 2012 5 Comments

    Do you get a lot of ideas when you go to bed, that you forget in the morning?

    I sure do and I know that this is very common. The reason that I’ve got explained to me is that because when we go to bed, we relax our entire body & mind. When doing so we also start reflecting upon the day or even week that has passed so far; subconsciously.

    At least for me, seconds before I actually fall asleep (last thing that I can remember) I get the best ideas but often I lack things around me to get these thoughts on permanent record.

    As I live, love and work with code this is where most of my ideas orbit. But occasionally I get other ideas as well, things like where I want to travel, what I want to see and what kind of changes I like to do in our home.

    Last year, I attended NDC (Norwegian Developer Conference) and I somehow got to join the speaker/staff cruise. At this party, I met a lot of very interesting people and one of them gave me a very good insight into how he captures the moment; he always has a pen and some paper in his pocket. On these papers he writes all kinds of interesting ideas, suggestions and anything else that you can imagine.

    As I hate “analog” writing, I didn’t really like the idea of carrying pen and paper with me everywhere, but the idea was great; I just had to find something that worked for me. I’m still in the process of finding exactly what works best for me!

    Finding the right tools

    There is something that is always by my side, everywhere I go and it would be awesome if this accessory could have an easy way to log thoughts on. Of course, I would want to be able to access these thoughts anywhere, even if I drop the accessory.

    I’m of course talking about my smartphone and storing the content in the cloud!

    As for just writing down my thoughts, taking photos and storing smaller portions of information; I tend to fall back to evernote. This is by far the best application that I’ve used for rapidly store notes in the cloud!

    Here are some reasons to why I use Evernote:

    • It’s easy to rapidly create new notes/images
    • There’s a free version!
    • It works on all devices (PC, Mac, Android, iOS, Windows Phone)

    Since I always have my phone with me, no matter where I am; I can instantly add a note with my most recent ideas. Then it’s of course up to myself to actually follow up and checking my own notes now and then.

    This is what the PC version of Evernote looks like:

    One very cool feature is that you can actually search inside both your notes and your images! If there’s text in images, Evernote will be able to find that!

    Mapping your mind with a mind map!

    Up until recently I felt it was difficult keeping track of all my thoughts that I had in meetings together with all the feedback from customers and co-workers. This is because either I put everything in a text document using Notepad or an Excel spreadsheet. Neither of these two alternatives have been very fun to work with. Another problem is also that it’s not quite so fun sharing just plain text with your co-workers or customers. So I wanted to find something else that was fast and that gave me great possibilities at the same time.

    I actually stumbled upon a software called XMind, which is a tool for creating all sorts of diagrams. Best of all, there’s a free version! It doesn’t include all the fancy export possibilities or some of the more interesting modes; but the free version sure does the job well enough!

    XMind can be used to create the following diagrams (more can be downloaded):

    • Blank
    • Project Plan
    • Project Status Report
    • Project Dashboard
    • Organization chart
    • Short meeting
    • Meeting Manager
    • SWOT Analysis
    • Cause & Effect
    • Timeline
    • Make a Decision
    • Weekly Plan

    As you can see by the image below, there are a lot of very nice diagrams that can be created.

    When I just want to brainstorm and get my thoughts organized, I create a Blank document and just start adding nodes. By pressing Enter/Tab you create a child to the current node. So if you double click the first big blue box in the middle and name it, press Enter and then Tab, it will create a child to that.

    You can see on this image below that I’ve created a map over what I have and want to write about and then added sub-sections to each topic to make it easier for me to remember what I want to write about.

    The alternative that I used before is not as nice and does not provide as good of a overview. This could as mentioned above have been doing it in a notepad document, spreadsheet or even the built in Sticky Notes application in Windows.

    I think it is very important that we find good ways, especially ways that we are personally comfortable with, to keep track of our thoughts and ideas. It might feel a bit unusual to start, but stepping out of one’s comfort zone can lead to something very good.

    It’s a great feeling being able to enter a meeting, taking notes and directly after the meeting not be ashamed of sending your mind map to your co-workers.

    How do you normally organize your ideas and thoughts and what tools do you recommend for taking notes during a meeting?

    Vote on HN

    Visual Studio 11 and Visual Studio 2010 Side by Side

    Posted by Filip Ekberg on September 19 2011 4 Comments

    For those that don’t want to install the Windows 8 Developer Preview on their workstation just to try out and develop applications in Visual Studio 11, I can happily say that Visual Studio 11 runs perfectly fine side by side Visual Studio 2010 on Windows 7!

    You can download the Visual Studio 11 Developer Preview on MSDN by clicking here!

    Visual Studio 11 and Visual Studio 2010 on Windows 7

    Happy coding!

    Vote on HN