Home > .Net / C#, C/C++, Security, Tutorials > Disassembling, Decompiling and Modifying executables

Disassembling, Decompiling and Modifying executables

Motivation for writing

As professional developers, we create products. We implement ideas, which are usually driven from some business craving for acceptance in the global market, from their target group. We try to deliver elegant, fast and reliable solutions and, quite honestly, we hate when someone use our work without at least saying "thanks, you've really made a great thing". That is why we need to protect our work. And in order to do that, we should be aware of the common vectors used by crackers to hack our software.

In this article, I'm gonna show you how to disassemble and decompile pure executable written in C++, among other interesting things related to managed and unmanaged environments. 

First, we’ll need a little bit of a theory so we can really understand what we are doing and why.

 

Difference between static and dynamic libraries

Historically, the static libraries are the first type of libraries to appear. In Windows you can find them by the extensions .lib and .dll. The main difference between the static and the dynamic libraries is that the static library is directly embedded in the executable, thus increasing its size. The dynamic library, on the other hand, is a separate file which uploads a different image of itself in memory every time it is called from a program. The dll is one, but the image is different and this way any inter-process concurrent issues are avoided. This also enables more manageable updates, but implies a slight performance degradation, which is not considered a big issue.

In general, the dynamic libraries are the preferred approach for building applications. Even in the latest versions of Visual Studio there is no option to create a static library; by default all libraries are considered dynamic. Yet it is still possible to create statically linked libraries through the console environment.

 

 

 

 

The CPU registers

The CPU registers are the fastest memory located in the CPU itself. They are basically used for every low – level operation, they are the super-fast data storage of the processor. For x86 architectures there are usually 8 32 bit long registers, 2 of which hold the base pointer and the stack pointer that are used for navigation between the instructions. The registers are even faster than the Static RAM (SRAM, known as the cache) and, of course,  the Dynamic RAM.

registers-diagram

 

 

 

Quick overview of the Assembly language

For this article we need to know few basic things about the assembly language so we can actually understand what we are doing. The Assembly language is unstructured and is based on very primitive instructions, which are divided in the following general types (I’ll describe only the basic operations) :

Data movement instructions

mov – used to copy data from one cell to another, between registers, or between a register and a cell in the memory
push/pop – operates on the memory supported stack

Arithmetic instructions

add/sub/inc – arithmetic operations. Can operate with constants, registers or memory cells

Control flow instructions

 jmp – jump to label or a cell in memory
jb – jump if condition is met
je - jump when equal
jne - jump when not equal
jz - jump when last result was zero
jg - jump when greater than
jge - jump when greater than or equal to
jl - jump when less than
jle - jump when less than or equal to
cmp – compare the values of the two specified operands
call/ret – these two implement the routine call and return

The Control flow instructions are what we are most interested in here. For a complete tutorial on the x86 assembly language, check this article.

 

Disassembling and modifying a C++ executable

For our example I’ve created a simple C++ application with basic I/O.

We’ll need to disassemble, debug and optionally decompile our example. Download the following tools that will help us to do that :

I’ve compiled this example which you can download from here. When we start it we see the following simple console application :

decompilation-console-app

It asks for some predefined input. If the wrong code is entered, the following output is presented :

“Try again”

Let’s pretend that we don’t have the source code and we don’t know the code. So what can we do ? Obviously, we have a loop here with some check inside which determines if the program should break from the loop or not.

We also got few strings :

“Please enter the code :”
“Try again”

 

 

Debug the executable

 

Start the OllyDbg debugger (with administrator privileges) and open the exe. (click to enlarge)

OllyDbg-tutorial-01

What we see in the upper-left window is the disassembled machine code. In other words, you see the instructions written in the Assembly Language. Below that we see the window with the binary code presented in hexademical values, and on the right we see the window with the CPU registers.

 

 

Locate the loop conditions

 

So now that our exe is loaded, started, and the debugger is attached, we have to find the exact place in the assembly code where the check is made. To do that we can use the strings that the UI shows us. Right-click on the assembly code view > Search For > All Referenced Strings . Find the “Try again” string and double-click it. The assembly view will locate the exact instruction which prints that string on the console. We can also see the “Code accepted” related instructions few rows below. It is clear where the loop resides.

 

 

Modify the assembly instructions

 

The next step is to modify some assembly instructions. We see a lot of instructions, but we are most interested in the jmp-related ones that control the position of the stack pointer. If we scroll a little bit up we can see “Please enter the following code…” instruction. In order to escape the loop, we need to change the target address of one of the jmp instructions that we run through.

Let’s take the jb at “00D613A4”, click it twice and change the target memory address to “00D613C7” – the one just before the “Code accepted” ASCII text, which obviously opens a stream.

In order to save it, right-click on the assembly window and press “Copy to executable” -> “Selection” while you’re on the modified row.

 

 

An alternative to OllyDbg. What is IDA ?

 

IDA is a debugger and a disassembler like OllyDbg. But it provides a more user-friendly view of the assembly code, and it can also act as a decompiler. For example, check the following screenshot of its assembly view :

IDA-graph

As you can see it is more structured, the various jumps are visualized like graph nodes which facilitates navigation.

Read more: The compilation process in C++, Java and .Net

 

Decompiling a C++ executable using IDA

Which brings us to the question “Is it possible to decompile native image in a way that an understandable source code can be generated ?”. The short answer is no.

What it generates is pseudo C code. Let me show you the output of the small example program :

So, can we decompile a native image into an understandable source code ? Depends on your idea of "understandable". You have to devote a lot of time and you need to posses serious knowledge of the APIs your operation system use, along with understanding of the C and Assembly syntax.

 

 

Decompiling applications written in managed environments

Decompiling .Net apps is also done with debuggers and decompilers for .Net like Reflector, for example (which is actually paid from some time on).

But the exe or dll you see on your desktop is intermediate, not binary code (assuming you do not use NGen). Decompiling C++ apps is hard because the compiler first produces Assembly language code targeted to the specific processor architecture, and next the Assembler gets that code and produces the actual native image. And as we saw, decompiling assembly code is hard.

The MSIL, at the other hand, is very close to the actual source code of your app, e.g. written with C#. You can use programs like Reflector to decompile them, along with some plugins to actually modify them.

 

So it is actually not so hard to crack an application

Yes, it’s not. With the difference that this process in an actual application will be more time-consuming. Do you know a single popular stand-alone application that has not been cracked ? That is why you need to think of better ways of protecting your software. Understand one simple thing :

Every application can be cracked, if you have access to its native image, just like every computer password can be broken, if you have physical access to the machine.

Of course, there are techniques that allows us to slow an attacker down, which might or might not be enough. But "slowing" doesn't mean "preventing", and that's a topic of another article.
 


That's from me regarding the topic of decompilation, I hope you learned something new today and, hopefully, this knowledge will help you to better protect your software. Know your enemy before going into battle. Because it's the battle for your own time.


About the author:
Kosta Hristov (34 Posts)

Hi there ! My name is Kosta Hristov and I currently live in London, England. I've been working as a software engineer for the past 6 years on different mobile, desktop and web IT projects. I started this blog almost one year ago with the idea of helping developers from all around the world in their day to day programming tasks, sharing knowledge on various topics. If you find my articles interesting and you want to know more about me, feel free to contact me via the social links below. ;)




Like the article ? Share it ! ;)


  1. Dimitar
    March 7th, 2013 at 20:42 | #1

    Great article, very helpful for every software developer. Thanks for the post.

  2. May 31st, 2013 at 19:16 | #2

    Hi Dimitar, 

    I'm glad you like it. ;) If you have some notes or recommendations, don't hesitate to write them down.  

  3. June 17th, 2013 at 19:32 | #3

    This is exactly what I was looking for. What is the best/practical software out there to decompile/disassemble an executable? Which one do you use?

  4. June 22nd, 2013 at 18:31 | #4

    Hi Volodymyr, 

    Depends on the compilation unit. If it's Java/.Net it will most probably be bytecode/MSIL so it'll be quite easy to decompile, unless obfuscated. You can use tools like Reflector for .Net. If it is C/C++, OllyDbg and IDA are one of the best, nonetheless you don't have a lot of choices here. 

    Regards, 
    Kosta

  5. Alireza
    July 11th, 2013 at 09:36 | #5

    hi dear

    i have a executable file with photo and i have to save this photo on pc please help me

    note: exe file create by deluxe picture

  6. July 11th, 2013 at 12:52 | #6

    Hi Alireza, 

    The methods I've described in this tutorial are a little bit low-level for your task. I guess you're talking about PicturesToExe deluxe ?
    In that case, you've probably tried to use the same program to extract the photos. You got two options:
    1. Download this tool and try it on the executable with the pictures
    http://www.picturestoexe.com/forums/index.php?app=core&module=attach&section=attach&attach_id=3655
    2. Simply open the slideshow in fullscreen and make a screenshot of the screen. Then use paint to paste it. You might have some quality lost but it depends on the picture itself. 

    Good luck

  7. jay
    July 21st, 2013 at 23:12 | #7

    Sir can you decompile  exe file for me? 

  8. July 22nd, 2013 at 00:08 | #8

    I'm afraid that might not be possible. :) But if you have more specific questions, I'll be happy to help. 

  9. herry
    September 19th, 2013 at 21:39 | #9

    sir, 

    i have expert advisor, i want to decompile and then i will repair that ea

    maybe you can help me to decompile that ea…and if i have to pay..how much ?

    thank you

     

  10. September 21st, 2013 at 18:55 | #10

    Harry, I'm sorry but I can't do that for you. Nonetheless, if you you have some more specific questions, I'll be happy to help you. 

    Regards

  11. Gnamu
    December 5th, 2013 at 02:33 | #11

    Hello kosta, i want to know about a way to decompress an exe file… I have one and no software works on it..it is kind of combination of many compressing softwares like upx, windows generic…and something like netopsystems fead package on which there is no info that i have .. So kindly if u can help it would be great….thanks

  12. December 5th, 2013 at 20:05 | #12

    Hi Gnamu,

    If you have some more-specific questions, I might be able to answer them. ;]

    Good luck !

  13. Gnamu
    December 6th, 2013 at 01:29 | #13

    Well kosta..then can u name me a software so that i can decompress it myself… I am in serious need to decompress that file.. Your help would be appreciated :)

  14. December 6th, 2013 at 02:00 | #14

    What exactly do you want with that exe ? Do you need to extract some information from it, decompile the source code or bypass a validation step ?

  15. Gnamu
    December 6th, 2013 at 16:02 | #15

    Well…actually i want to do all the things but to bypass i need to unpack it(as i told its multipacked) so that i can know which part of source code takes the password for validation and nop’s it(as it is wrong, obviously)….. Thanx for your attention..

  16. December 9th, 2013 at 12:56 | #16

    nice tutorial :)
    by the way, can i have the source code of that program ?
    *the input number program that you used in the tutorial above.

    i need it to do my school works. if so then email :D

    regards,

  17. December 9th, 2013 at 20:05 | #17

    @hackazer

    Feel free to take the source from the page, I don’t keep the project anymore. ;)

    @Gnamu

    Effectively decompiling an executable and extracting readable code is not an easy task and requires substantial amount of time and expertise related to the operating system, platform and language used during the development.

    Disassembling is easier, but it’s limited enough. The example I’ve provided is very simple and forms the basics used when cracking a game, for example. But in a real situation this would not be that easy.

    If you really want to decompile your exe, I would advice you to hire specialized professionals to do that.

    Good luck !

  18. MyName
    December 12th, 2013 at 18:04 | #18

    Hi! Thanks for your tutorial! I have a couple of simple questions:
    How do I exactly know if the application I am using is a .Net application (given you don’t have any info from the developer or application website) ?

    Can I still ry to decompile a .Net application with Ollydbg? What important parts of the code will I not be able to see? I’m asking because ve seen many Ollydbg tutorials not making any distinction on wheter the application is a .Net application or not?

    Thanks for you help!

  19. December 12th, 2013 at 18:51 | #19

    Hi there,

    Well, the simplest way I can think of is to try opening it with a .Net decompiler like Reflector. Or ILDASM since it’s free and you already have it installed. That way you’ll know if it’s an assembly.

    Regarding OllyDbg, no you can’t. And you don’t need to, OllyDbg is a disassembler, not a decompiler. If you open a .Net assembly with OllyDbg you’ll see absolutely the same as you would have seen with a native C++ application – assembly code.

    Hope this answers you questions.

    Cheers

  20. MyName
    December 12th, 2013 at 19:30 | #20

    @Kosta Hristov

    Wait, I’ve probably missed something here. If decompiling means getting back to something close to the original source code, and disassemble means just looking at the assembly code, then in this case I am picturing I am not interested in decompiling but just disassembling.

    So to this purpose, and to one of trying to alter a .Net application behaviour like you show for non .Net apps, is Ollydbg still the right tool?

    To this purpose does it make a difference to use Ollydbg or say Reflector?
    Hope this clarify my questions! Thanks

  21. December 12th, 2013 at 22:17 | #21

    Yes decompiling usually means getting back to the original code. Disassembling means getting the assembly instructions.

    “you’ll see absolutely the same as you would have seen with a native C++ application – assembly code.”

    This was actually not entirely correct.

    The .Net exe (or .Net assembly) does not contain assembly code. It contains something called MSIL (Microsoft Intermediate Language), the equivalent of the Bytecode in Java. So when you look at a .Net assembly you see a .Net compliant language (like C# and Visual Basic) translated into MSIL. When this assembly gets executed, the JIT (Just in time) compiler creates the actual assembler code and the binary instructions for the processor to execute. But this happens runtime.

    However, you can still see assembly code if the .Net assembly is compiled using something called Native Image Generator (NGen). Then you skip the intermediate language step. But most of the .net assemblies are actually MSIL (the new name for which is Common Intermediate Language (CIL), but I prefer the old one).

    Therefore you can’t “disassemble” a .Net exe with OllyDbg, because OllyDbg is a disassembler and the .Net assembly doesn’t contain assembler code but rather MSIL.

    ILDASM and Reflector are tools to decompile the MSIL code.

    So in a nutshell:

    In C++ you have:

    1. Source code.
    2. Assembly code.
    3. Binary code.

    In Java & .Net you have:

    1. Source code.
    2. Intermediate language (or bytecode).
    3. Assembly code (after executed, produced by the JIT)
    4. Binary code.

    You can read more about that here
    http://www.developingthefuture.net/compilation-process-and-jit-compiler/

    Hope this helps.

  22. MyName
    December 13th, 2013 at 05:14 | #22

    @Kosta Hristov

    Wow now that was a great answer :) Thanks for the time you spent! Appreciate!

  23. MyName
    December 13th, 2013 at 05:47 | #23

    @Kosta Hristov

    …I knew I’d have a second thought on this.. or I should rather call it a bigger doubt?

    You say:

    “When this assembly gets executed, the JIT (Just in time) compiler creates the actual assembler code and the binary instructions for the processor to execute. But this happens runtime.”

    And the question is.. doesn’t Ollydbg operates at application runtime?
    I’ve actually attached Ollydbg to a running .Net application, and of course I could see assembly code for it… and seeing the app calling modules, performing jumps and so on.., so isn’t it wrong to say that Ollydbg can’t be used to see and work with a .Net application assembly code?

    Perhaps I should do more homework and read more, which I am trying to do.. but.. if you had a quick answer on this… I’d appreciate it again! (don’t have to be a long answer, just tell me “no you are wrong” (if you are 100% sure ;) ) I’ll figure out the details by myself then.
    Thanks

  24. December 13th, 2013 at 12:12 | #24

    Hi,

    There’s no short answer, dude. Software engineering is a game of guessing sometimes, it’s not an exact science.

    You can see it, but you can’t work with it. Think about it. In a C++ application, the physical executable file can easily be read because OllyDbg can understand the binary instructions. You can modify the instructions and save an executable. That’s how cracks work.

    In .Net, you have a managed environment. A virtual machine in between. So yes, OllyDbg works at runtime. But the assembler code for your .Net executable is digestible only at runtime, as opposite to a C++ exe, for instance.

    Every time you start a .Net exe, you might get a different piece of assembler instructions. Or at least partially. And at least in theory.

    So I said you can’t debug it, because you can’t actually do anything useful with it. Because if you modify the assembler instructions you see in OllyDbg, even save them they are no use. Because you need the virtual environment in order to run it. And pure assembler instructions doesn’t give you much information.

    Again, it depends what you want. If you simply want to “see” the produced assembler code from the CLR, that’s ok. But you don’t know if the virtual machine will produce the same code next time and you can’t modify anything. But if that’s what you want, then yes, you can use OllyDbg for that.

  25. A.P.||.BaBa
    December 30th, 2013 at 08:46 | #25

    i want to disassemble Internet Download Manager(IDM). I used W32DASM for that. but it shows symbol codes . How to know that code or some other software to do that.
    Thanks in advance Sir.

  26. MyName
    January 6th, 2014 at 15:19 | #26

    @Kosta Hristov

    Thanks for your comprehensive answer! It was exactly what I needed to know!

  27. January 6th, 2014 at 16:18 | #27

    Welcome. ;]

  28. Vince
    January 13th, 2014 at 11:02 | #28

    I’m trying to create a simple C++ program like you did to disassemble myself using Visual Studio 2010 or 2013 desktop for Windows. So I have a simple if statement that checks a boolean value.

    For the life of me, when I try to run those programs through IDA 5.0 free or Ollydbg, I get software breakpoint exceptions. Do you know what would cause those issues?

    • January 13th, 2014 at 14:27 | #29

      Hi Vince,

      Are you using CLR C++ instead of native C++ ? Are you using debug instead of release configuration ?

  29. Vince
    January 14th, 2014 at 03:05 | #30

    I found the issue, but to clarify I am using the native c++, specifically win32 console application in Visual Studio 2013 desktop edition. Both debug and release give me the software break exception, but I’m using release now.

    I tried a normal crackme that I know has been run through IDA 5.0 before and it also received a breakpoint exception, so it seems to be normal.

    I thought that because I was receiving this exception that it would not stop properly at the beginning of the module entry point but apparently that was a mistaken idea of mine. I needed to place a breakpoint at the interesting point of code, otherwise IDA will just plow through the program without stopping. Does this sound right to you?

  30. January 14th, 2014 at 12:16 | #31

    Yep, thanks for sharing. :)

  31. January 14th, 2014 at 17:29 | #32

    hello thanks for the guide
    but it does not always work
    would like to try to crack a program because I can not
    thanks
    http://www.mediafire.com/download/ez95i0c99m28xuw/ossigeno.rar

  32. January 15th, 2014 at 12:07 | #33

    No because cracking someone else’s work is not a good thing. I wrote the tutorial so people can understand better how to protect their code, there are enough tutorials how to do the opposite.

  33. January 15th, 2014 at 13:42 | #34

    thanks hallo

  34. Steven Klein
    March 25th, 2014 at 19:18 | #35

    Hello,
    Thank you for the article. I have a few question about the C code that I do not understand.
    1) what do lines 36 – 42 do? It seems like a long if statement to compare 2 variables
    2) why does like 53 have 2 conditions, one of them redundant? Is this in the assembly and if so, why would the compiler write both those conditions?

    Thanks!

  35. March 26th, 2014 at 15:32 | #36

    Hi Steven,

    The C code is automatically generated by IDA and is quite a low level assembler decomposition. It’s just meant to make it somehow more readable.

    Regards

  36. June 28th, 2014 at 21:45 | #37

    Sir Please Give Me Video Tutorial For How TO Disassembling, Decompiling and Modifying executables

  1. No trackbacks yet.

Current month ye@r day *


Copyright © Developing the future 2013. Licensed under the CC BY-NC-ND 3.0 Creative Commons license.