Write your own assembly & amp; msil literacy

Source: Internet
Author: User
Tags mscorlib

I don't know what to say, but I want to say that MD, a guy who has been installed in the blog Park for so long, won't even be able to do this. I am going to OH.

. Net, what is the "assembly" stuff? It sounds pretty cool, in fact, it refers to a "bunch of programs". From our traditional C ++ encapsulation dll recognition, it is a dll file name and then a lib file contains the corresponding function list, after adding the lib file, it will be OK.

Now let's create a blank Asp.net website.
Open web. config and some default referenced assembly will appear.

                            

No System is available for the fake attributes. core, Version = 3.5.0.0, Culture = neutral, PublicKeyToken = B77A5C561934E089 what is this? This is the assembly information. the definition of the Assembly under. net has exceeded the previously recognized category of the dll file. The Assembly information refers to the unique electronic tag information signed by "a bunch of available program functions.

The assembly in the system directory is called GAC and global assembly cache, which is the console. write () and messageBox. show. He put it in the C: \ WINDOWS \ assembly directory and found that the resource manager view became very special. We found that only the attributes can be viewed, but the files in the attributes cannot be copied. To copy the file, you can also use the doscommand line to store it in the namespace directory tree.

Our own assembly can also be placed in the GAC. We need to do this:
The assembly to be put under GAC must be signed with a private key (with a reinforced name) as follows:

A common assembly can run after modification without damaging the pe file structure. However, a signature with a strong name is not acceptable, effectively ensuring program integrity ..
Note that the. net3.5 or later versions can be changed no matter which version is selected with a strong name. Because. net thinks that your program is running in a "trusted environment", strong name verification is passed. However, adding GAC should not be added to GAC, so it will certainly undergo strong name verification. To perform strong name verification without being passed, add the following configuration in app. config:

                 

Appendix:
Uninstall using gacutil/I function. dll
You can also right-click a collection point in the C: \ WINDOWS \ assembly directory to uninstall it.
Csc command usage:
Generate the dll signature file:
D: \> sn-k keyfor_function.snk
Compile the signature to dll:
D: \> csc/keyfile: keyfor_function.snk/t: library function. cs
Additional dll compilation main program source code:
D: \> csc/reference: function. dll kit. cs

 

When. net is running, most people will not learn it intentionally. What about CLR? JIT? I don't know how to install a master, or how to blow it out. You still have to get started with it .. Net exe is the intermediate language code, which must be called ilasm and ildasm. One is to compile and the other is to decompile. Let's demonstrate how to simply "crack" a small program. By the way, let's also play ildsam.

Create a console Program
Write a few codes:

                    Main(                            (licence() ==                   Console.WriteLine(                                                  Console.WriteLine(                                                                                   }

On the project properties page, right-click and choose Properties.
Add a program signature:

Project-> right-click-> Add-> new item-> application configuration file
App. config is added to the project.
Edit it

              

Compile and run now

Invalid licence

Obviously, the verification fails.

Now let's crack him and let him pass the verification.

Then open the il dasm tool.
If you do not know where to open it, enter ildasm in the vs command line.
Then open the exe file compiled in the debug directory.

Represents the test namespace
Represents the program class
I didn't write this method in the code.

I have learned in books that the default constructor is provided during compilation if no constructor is written.
This means that the ctor operator may be called constructor.

The code for opening the main function is as follows:

// Code size. maxstack. locals init ([bool. stloc. ldloc. ldstr void [mscorlib] System. valuetype [mscorlib] System. consoleKeyInfo [mscorlib] System. ldstr void [mscorlib] System. valuetype [mscorlib] System. consoleKeyInfo [mscorlib] System .} // end of method: Main

The code for enabling the licence function is as follows:

// Code size. maxstack. locals init ([ldc. i4. stloc. ldloc.} // end of method: licence

Click file menu> dump
Save as il File
We only need to change the condition judgment in the main function so that it can pass the verification.
Use NotePad to open the saved il File
Find this row and see no
IL_0008: brtrue. s IL_001e

If so
IL_0008: brfalse. s IL_001e
Try it.

After modification, use ilasm test. il on the vs command line to recompile the il file into an exe file.

Run test.exe again.
Failed

Unprocessed exception: System. IO. FileLoadException: failed to Load file or assembly "test, Version =., Culture = neutral, PublicKeyToken = strong name verification failed. (Exception from HRESULT: file name: "test, Version =., Culture = neutral, PublicKeyToken =" ---> MyComputer

Because there is a program signature, we have to remove the strong name.
Open the il file again
We can see that there are three starting points:
. Publickeytoken
. Publickey
. Hash

Delete them
Then use ilasm to compile and run the program. The verification is successful.

Note:
If the Assembly has a strong name and changed any content of the pe file, it cannot be started, just as it has bound MD5 verification. To protect programs, obfuscators are used for processing, and obfuscators change the file content.

In addition:
In other words, advanced languages are nothing more
Int var1 = 123
If (){
}
Else {}

For (){}
And so on.
I have read the online il code parsing and previously read some understanding of the Assembly, although I do not need to develop the assembly.
Assembly is a stack-based pipeline-like operation. The variables initialized in advance are determined during the compilation, and each time an instruction is executed to assign values to the registers or perform Operation address jump.

Think about these basic features. That's enough. the syntax of advanced languages can basically be implemented, but it will be a little complicated.

Int var1 = 123
Leave this blank.

If (){
}
Else {}
In the Assembly, it is estimated that it is implemented through the conditional address jump.

For (){}
It's actually quite simple.
After reading a bit of il code, we found that a continuous if process condition could not reach the jump to the previous address and continued n ++
Like this
Int n = 1;
Loop:
N ++;
Console. writeline ("hi ");
If (n <50)
Goto loop;

In fact, I do not understand assembly language.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.