Native Code To Be Embedded In The Net Pe File

Environment: C# IntroductionIn this article, I would like to explain how to create and use DLLs in C Sharp. Before writing code, we will examine some basics of a DLL in the.NET framework; for example, how it's loaded in the process address space, how CLR is initialized, and the mechanism behind the screen. First, we will talk about MSCorEE.dll; you will find this guy in the%windir%system32 directory. MSCorEE.dll stands for Micro soft Component object runtime Execution Engine. The existence of this file tells you that you have installed the.NET framework in your system.

This DLL is responsible for initializing the CLR.Whenever you create an assembly, the compiler/linker emits the following 6-byte x86 stub function into the PE file's.text section.

Motivation for writingAs 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 librariesHistorically, the 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 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 registersare the fastest memory located in the CPU itself.

Native Code To Be Embedded In The Net Pe File

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.Quick overview of the Assembly languageFor this article we need to know few basic things about the assembly language so we can actually understand what we are doing.

The is and is based on very primitive instructions, which are divided in the following general types (I’ll describe only the basic operations): Data movement instructionsmov – used to copy data from one cell to another, between registers, or between a register and a cell in the memorypush/pop – operates on the memory supported stack Arithmetic instructionsadd/sub/inc – arithmetic operations. Can operate with constants, registers or memory cellsControl flow instructionsjmp – jump to label or a cell in memoryjb – jump if condition is metje - jump when equaljne - jump when not equaljz - jump when last result was zerojg - jump when greater thanjge - jump when greater than or equal tojl - jump when less thanjle - jump when less than or equal tocmp – compare the values of the two specified operandscall/ret – these two implement the routine call and returnThe Control flow instructions are what we are most interested in here.

File

For a complete tutorial on the x86 assembly language, check.Disassembling and modifying a C executableFor 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. When we start it we see the following simple console application: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 executableStart the OllyDbg debugger (with administrator privileges) and open the exe.

Native Code To Be Embedded In The Net Pe File 2016

(click to enlarge)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 conditionsSo 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 instructionsThe 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?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:As you can see it is more structured, the various jumps are visualized like graph nodes which facilitates navigation.Read more:Decompiling a C executable using IDAWhich 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 environmentsDecompiling.Net apps is also done with debuggers and decompilers for.Net like, 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 applicationYes, 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 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.;). 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 pictures2. 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. @hackazerFeel free to take the source from the page, I don’t keep the project anymore.;)@GnamuEffectively 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! 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! 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.

Native Code To Be Embedded In The Net Pe File Free

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.

Embedded

Binary code.You can read more about that hereHope this helps. 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. If you had a quick answer on this I’d appreciate it again! Universal patch and serial for idm all versions. (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. Hi,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. 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?

Posted on  by  admin