C # Assembly generation 09, assembly signature,

Source: Internet
Author: User
Tags mscorlib

C # Assembly generation 09, assembly signature,

In "C # Assembly generation 08, setting the assembly version", you can set the version for the Assembly, but it is far from enough for the Assembly Security. This document describes how to use the Assembly signature.

 

□Assembly Signature

→ The as folder of drive F contains multiple files

→ Create a key in the folder where the Assembly is located

→ Print the key

The keys are a bunch of garbled characters, which are encrypted.
→ Create a Public Key based on the Key

→ Print Public Key

Note: The public token here is obtained by using the hash algorithm of the public key. When an assembly is referenced, the Assembly exists in the form of public tokens.

→ Now re-compile Cow. cs in "C # Assembly generation 08, set assembly version", but this time the key is used

→ Re-compile MainClass. cs in "C # Assembly series 08, set assembly version", and reference the created Farm. dll.

→Run mainclass.exe

→ Simulate a virus assembly. First, create CowVirus. cs in the as folder of drive F.
→ Open CowVirus. cs in notepad, write as follows, and save

using System;
using System.Reflection;
[assembly: AssemblyVersion("3.3.3.3")]
public class Cow
{
    public static void Moo()
    {
Console. WriteLine ("I Am a virus ");
    }
}

→ Compile CowVirus. cs, generate a new Farm. dll, and rewrite the original Farm. dll without a private key.

Then run mainclass.exe.

It can be seen that, although a virus program simulates an assembly with the same name, the Assembly will not be recognized by the main program because there is no signature.

 

□Public Token

If an assembly A has A public key and is referenced by program B, if we decompile program B, we can see that Assembly A has A public token in the IL code of program B, this public token is obtained based on the hash algorithm of the public key of Assembly.

 

→ Rewrite Farm. dll again, compile the Cow. cs file, and use the key

→ Decompile Farm. dll

Let's take a look at the Farm assembly list:

.assembly Farm
{
  .custom instance void [mscorlib]System.Runtime.CompilerServices.CompilationRelaxationsAttribute::.ctor(int32) = ( 01 00 08 00 00 00 00 00 ) 
  .custom instance void [mscorlib]System.Runtime.CompilerServices.RuntimeCompatibilityAttribute::.ctor() = ( 01 00 01 00 54 02 16 57 72 61 70 4E 6F 6E 45 78   // ....T..WrapNonEx
                                                                                                             63 65 70 74 69 6F 6E 54 68 72 6F 77 73 01 )       // ceptionThrows.
  .publickey = (00 24 00 00 04 80 00 00 94 00 00 00 06 02 00 00   // .$..............
                00 24 00 00 52 53 41 31 00 04 00 00 01 00 01 00   // .$..RSA1........
                49 2C E7 BF EF 82 D2 44 A3 F4 AB 96 27 32 89 A4   // I,.....D....'2..
                77 E6 AD A6 A3 21 62 E3 0A DA 72 9D BA 0A 7C 59   // w....!b...r...|Y
                51 0C F6 63 22 ED E3 50 4B F5 61 E2 1A 7F ED 26   // Q..c"..PK.a....&
                78 BD 1B 99 E7 1C 91 7F 80 E6 3F 9B 1C F0 85 63   // x.........?....c
                58 5D 8B DC 60 41 69 2A F3 E6 EB 9B 42 4D D8 B6   // X]..`Ai*....BM..
                C5 B0 31 47 77 58 C6 53 65 2A B1 90 30 EA 24 EF   // ..1GwX.Se*..0.$.
                2A 1C 92 DD 62 D3 00 F6 F3 CA 0E 24 1C A8 F2 2E   // *...b......$....
                5D D3 06 41 A7 77 EE EB C0 2F 64 83 2A 59 00 AE ) // ]..A.w.../d.*Y..
  .hash algorithm 0x00008004
  .ver 3:3:3:3
}

Above, the Assembly Farm. dll contains the public key.

Re-compile mainclass.csand use the farm.dllprogram to create mainclass.exe.

→Run mainclass.exe

→Decompilation mainclass.exe

The IL code used to export data to mainclass.exe

.assembly extern mscorlib
{
  .publickeytoken = (B7 7A 5C 56 19 34 E0 89 )                         // .z\V.4..
  .ver 4:0:0:0
}
.assembly extern Farm
{
  .publickeytoken = (21 64 02 E8 98 B6 AC A9 )                         // !d......
  .ver 3:3:3:3
}
.assembly MainClass
{
  .custom instance void [mscorlib]System.Runtime.CompilerServices.CompilationRelaxationsAttribute::.ctor(int32) = ( 01 00 08 00 00 00 00 00 ) 
  .custom instance void [mscorlib]System.Runtime.CompilerServices.RuntimeCompatibilityAttribute::.ctor() = ( 01 00 01 00 54 02 16 57 72 61 70 4E 6F 6E 45 78   // ....T..WrapNonEx
                                                                                                             63 65 70 74 69 6F 6E 54 68 72 6F 77 73 01 )       // ceptionThrows.
  .hash algorithm 0x00008004
  .ver 0:0:0:0
}

Above, in the. assembly extern Farm statement block, the. publickeytoken value is obtained based on the public key hash algorithm of the Farm. dll assembly.

 

Summary:
○ By signing an assembly, that is, encrypting the Assembly key, the Assembly has a public key, which effectively prevents malicious Assembly tampering.
○ The Assembly is referenced by program A. If program A is decompiled, there is A public token in the assembly list. This value is obtained by the public key hashing algorithm.

 

"C # Assembly series" includes:

C # Assembly series 01, use NotePad to write C # And IL code, use the DOS command to compile the assembly, and run the C # Assembly series 02 program, use NotePad to view the IL code C # Assembly series 03 of the executable assembly, reference multiple module C # Assembly series 04, when an Assembly contains multiple modules, you can understand the keyword internal C # Assembly generation 05, so that the Assembly contains multiple modules C # Assembly generation 06, assembly list, differences between EXE and dll c # Assembly generation 07, tampered with assembly C # Assembly generation 08, set assembly version C # Assembly generation 09, assembly Signature

 

References:

Http://www.computersciencevideos.org/created by Jamie King


In C language-> what?

-> Is a whole. It is used to point to a struct, class in C ++, and other pointers containing sub-data to obtain sub-data. In other words, if we define a struct in C and declare a pointer pointing to this struct, we need to use "->" to retrieve the data in the struct using the pointer ".
For example:
Struct Data
{
Int a, B, c;
};/* Define struct */
Struct Data * p;/* define struct pointer */
Struct Data A = {1, 2, 3};/* declare variable */
Int x;/* declare a variable x */
P = & A;/* point p to */
X = p-> a;/* indicates that the data item a in the struct pointed to by p is assigned to x */
/* Because p points to A, p-> a = A. a, that is, 1 */

For the first problem, p = p-> next; this should appear in the linked list of C language. next here should be a struct pointer of the same type as p, and its definition format should be:
Struct Data
{
Int;
Struct Data * next;
};/* Define struct */
............
Main ()
{
Struct Data * p;/* declare the pointer Variable p */
......
P = p-> next;/* assign the value in next to p */
}
The linked list pointer is a difficulty in C language, but it is also the key. It is very useful to learn it. To be careful, you must first talk about variables and pointers.
What is a variable? The so-called variables should not be simply thought that the amount will become a variable. Let's use the question of our Dean: "Is the classroom changing ?" Change, because there are different people in the classroom every day, but they do not change, because the classroom is always there, and it does not become larger or smaller. This is the variable: There is a constant address and a variable storage space. Under normal circumstances, we only see the variable in the room, that is, its content, but do not pay attention to the variable address, but the C language pointer is the address of the room. We declare that variables are equivalent to building a house to store things. We can directly watch things in the house, while declaring pointers is equivalent to getting a positioner. When a pointer points to a variable, it is to use the pointer to locate the variable. Then we can use the pointer to find the variable "tracked" and get the content in it.
What about struct? The structure is equivalent to a villa composed of several houses, and several houses are bound for use together. Suppose there are many such villas distributed in a big maze, and each villa has a house. The location information of another villa is put in it. Now you have found the first villa with the positioner and obtained what you want from it (the data part of the linked list ), then, calculate the location of the next villa into your positioner (p = p-> next), and go down to the next villa ...... If you go on like this, you will know that the information of a villa on the ground is gone (p-> next = NULL), and your trip is over. This is the process of traversing a linked list. Now you can understand the meaning of p = p-> next!
Write so much. I hope you can understand.
If you want to learn c and C ++ well, you must be familiar with linked lists and pointers!

In C language-> what?

-> Is a whole. It is used to point to a struct, class in C ++, and other pointers containing sub-data to obtain sub-data. In other words, if we define a struct in C and declare a pointer pointing to this struct, we need to use "->" to retrieve the data in the struct using the pointer ".
For example:
Struct Data
{
Int a, B, c;
};/* Define struct */
Struct Data * p;/* define struct pointer */
Struct Data A = {1, 2, 3};/* declare variable */
Int x;/* declare a variable x */
P = & A;/* point p to */
X = p-> a;/* indicates that the data item a in the struct pointed to by p is assigned to x */
/* Because p points to A, p-> a = A. a, that is, 1 */

For the first problem, p = p-> next; this should appear in the linked list of C language. next here should be a struct pointer of the same type as p, and its definition format should be:
Struct Data
{
Int;
Struct Data * next;
};/* Define struct */
............
Main ()
{
Struct Data * p;/* declare the pointer Variable p */
......
P = p-> next;/* assign the value in next to p */
}
The linked list pointer is a difficulty in C language, but it is also the key. It is very useful to learn it. To be careful, you must first talk about variables and pointers.
What is a variable? The so-called variables should not be simply thought that the amount will become a variable. Let's use the question of our Dean: "Is the classroom changing ?" Change, because there are different people in the classroom every day, but they do not change, because the classroom is always there, and it does not become larger or smaller. This is the variable: There is a constant address and a variable storage space. Under normal circumstances, we only see the variable in the room, that is, its content, but do not pay attention to the variable address, but the C language pointer is the address of the room. We declare that variables are equivalent to building a house to store things. We can directly watch things in the house, while declaring pointers is equivalent to getting a positioner. When a pointer points to a variable, it is to use the pointer to locate the variable. Then we can use the pointer to find the variable "tracked" and get the content in it.
What about struct? The structure is equivalent to a villa composed of several houses, and several houses are bound for use together. Suppose there are many such villas distributed in a big maze, and each villa has a house. The location information of another villa is put in it. Now you have found the first villa with the positioner and obtained what you want from it (the data part of the linked list ), then, calculate the location of the next villa into your positioner (p = p-> next), and go down to the next villa ...... If you go on like this, you will know that the information of a villa on the ground is gone (p-> next = NULL), and your trip is over. This is the process of traversing a linked list. Now you can understand the meaning of p = p-> next!
Write so much. I hope you can understand.
If you want to learn c and C ++ well, you must be familiar with linked lists and pointers!

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.