The.NET core code protection strategy - the hidden core assembly

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

  After the moral blame after the storm was also a period of time not to write a blog, of course I am not guilt is so long not to write, is purely a programmer's disease.. What a lazy word, originally wanted to write a long and minute statement about the moral superiority of irony. Or just think, that may lead to a new wave of offensive, does not have any practical significance, the climate, we can also upset, the write something meaningful, I talk about is how to protect my code, personal experiences, such as the wrong place please forgive me.

  We all know that.NET code is easy to decompile, standing in the user's point of view, crack the software can be divided into several:

  The 1 amendment registration verification code, to bypass the registration effect,

  2 break out the registration algorithm, write register,

  3 complete duplication of code, into a compiler can modify project,

  By the look of harm is, the third is the most serious, if your software is copied more powerful competitors, it is a devastating blow,.NET theoretically no software is not cracked copy, as long as the master died of heart to break your software, protection and good is meaningless of course, it can only be reconciled, general master also disdain to crack some meaningless things. Although we do not completely protect the code, but we can increase their difficulty of cracking, not anti master, at least we have to defend as I this ordinary programmers, not any Tom, Dick and Harry can meddle in our code, it is the programmer IQ insult.

  In general we protect some important process software:

  1 the design of machine code,

  2 according to the machine code generated code,

  3 verification code (local and remote authentication),

  4 to strengthen the program set name,

  5 the assembly code obfuscation,

    Of course, so many steps once also said not to finish, I mainly talk about how to hide the core program their own set, namely.DLL dynamic library. When we write a project, Dynamic pool will have a variety of, Dynamic library when we compile will have a *.DLL appears, The DLL contains a lot of metadata, It is easy to be used in anti compiled software fully see the inside of the code, So the hidden assembly is very important, Idea is as follows: the assembly in the memory, When the will to use it, This decompiler software cannot easily see our code., But in a large project, All the procedures set in the memory is not realistic, We can only put a few core assembly, Such as the registration and verification process, Landing process, The core algorithm process., Because you're hiding some important assemblies, Some people even get your other code, Want a software properly still need a lot of time. by Zengg

  Spend a little time to write a hidden assembly of small DEMO, as shown below:


  

  

  

  The main function of this DEMO is:

  1 from the outside to open a DLL, and list the DLL. There are many kinds,

  2 lists how many public method may perform a class,

  3 to execute a method, will show the implementation of the results returned, the premise is the method with the return value,

  4 from the outside to turn DLL into C# available code, we facilitate memory,

  5 from memory read already stored in the memory set procedures, methods and implementation of a class of it,

Below we have a look the VS structure of the project:

  

  We hypothesized that AssemblyTest the library is stored our registration place, AssemblyWPFDemo is our main program, generally speaking, if we want to use the AssemblyTest library function, must the library by the Lord Cheng Xuxian, references, to the use of the library functions, there exists a problem, as long as the reference library, the the corresponding DLL in the local library will, this is the exposure of our code, the following is the process of interpretation, how to use the library is not referenced by the function. by Zengg

  The AssemblyTest class library situation:

  There are two test class, and a WPF form.  

namespace AssemblyTest
{
    public class AssemblyTest1
    {
        public string AssemblyTest1GetString()
        {
            return "This is Assembly1";
        }
    }
}

AssemblyTest1

namespace AssemblyTest
{
    public class AssemblyTest2
    {
        public string AssemblyTest2GetString()
        {
            return "This is Assembly2";
        }
    }
}

AssemblyTest2

  WPF forms Preview

  The window that this form is above DEMO diagram pop-up.

Below we have a look the main code is how to achieve, before this, you may have to understand the application of C# Assembly, which I just write some simple notes, you can go to see:

The core code:

// ***********************************************************************
// Assembly         : AssemblyWPFDemo
// Author: once Yiheng
// Created          : 09-25-2013
//
// Last Modified By: once Yiheng
// Last Modified On : 09-25-2013
// ***********************************************************************
// <copyright file="AssemblyControl.cs" company="">
//     Copyright 2013(c) . All rights reserved. 
// </copyright>
// <summary>The assembly operations</summary>
// ***********************************************************************
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;

namespace AssemblyWPFDemo
{
    /// <summary>
    /// Class AssemblyControl
    /// </summary>
    public class AssemblyControl
    {
        /// <summary>
        /// To specify the assembly into a binary stream
        /// </summary>
        /// <param name="assemblyFileName">Path set</param>
        /// <returns>Binary source</returns>
        public byte[] GetAssemblyToByte(string assemblyFileName)
        {
            byte[] assemblySource=null;
            if (File.Exists(assemblyFileName))
            {
                FileStream fStream = new FileStream(assemblyFileName, FileMode.Open);
                BinaryReader bReader = new BinaryReader(fStream);
                assemblySource = bReader.ReadBytes(Convert.ToInt32(fStream.Length));
                fStream.Close();
                bReader.Close();
            }
            return assemblySource;
        }
        /// <summary>
        /// Binary source is converted to C# code, we can create a static class data
        /// </summary>
        /// <param name="memberNmae">The field name</param>
        /// <param name="assemblySource">Binary source</param>
        /// <returns>System.String.</returns>
        public string ByteConverToString(string memberNmae, byte[] assemblySource)
        {
            string strAssemblySource = "public byte[] " + memberNmae + "={";
            foreach (byte b in assemblySource)
            {
                strAssemblySource += b.ToString() + ",";
            }
            strAssemblySource = strAssemblySource.Substring(0, strAssemblySource.Length - 1);
            strAssemblySource += "};\n";
            return strAssemblySource;
        }
        /// <summary>
        /// The binary data source into the assembly
        /// </summary>
        /// <param name="assemblySource">Binary source</param>
        /// <returns>The assembly</returns>
        /// <exception cref="System.NullReferenceException">The assembly is empty, please check the binary data source</exception>
        public Assembly GetAssemblyBySource(byte[] assemblySource)
        {
            Assembly assembly = Assembly.Load(assemblySource);
            if (assembly == null)
                throw new NullReferenceException("The assembly is empty, please check the binary data source");
            return assembly;
        }
        /// <summary>
        /// Get the number of classes in the program
        /// </summary>
        /// <param name="assembly">The assembly</param>
        /// <returns>Class list</returns>
        public Type[] GetTypesByAssembly(Assembly assembly)
        {
            if (assembly == null)
                return null;
            Type[] types = assembly.GetTypes();
            return types;
        }
        /// <summary>
        /// Get the class name
        /// </summary>
        /// <param name="type">Class</param>
        /// <returns>The class name</returns>
        public string GetTypeName(Type type)
        {
            if (type == null)
                return "";
            return type.Name;
        }
        /// <summary>
        /// Access to all public method class information
        /// </summary>
        /// <param name="type">The type.</param>
        /// <returns>MemberInfo[][].</returns>
        public MemberInfo[] GetMemberInfosByType(Type type)
        {
            if (type == null)
                return null;
            return type.GetMethods();
        }
        /// <summary>
        /// Gets the name of the current method
        /// </summary>
        /// <param name="menberInfo">The menber info.</param>
        /// <returns>System.String.</returns>
        public string GetMemberInfoName(MemberInfo menberInfo)
        {
            return menberInfo.Name;
        }
        /// <summary>
        /// Execution method does not return a value
        /// </summary>
        /// <param name="menberInfo">The method body</param>
        /// <param name="obj">Such entities</param>
        public void InvokeMember(MemberInfo menberInfo,object obj)
        {
            obj.GetType().InvokeMember(menberInfo.Name, BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance, null, obj, null);
        }
        /// <summary>
        /// A return value of the method of execution
        /// </summary>
        /// <param name="menberInfo">The method body</param>
        /// <param name="obj">Such entities</param>
        /// <returns>Return result</returns>
        public object InvokeMemberHaveResult(MemberInfo menberInfo, object obj)
        {
            return obj.GetType().InvokeMember(menberInfo.Name, BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance, null, obj, null);
        }
    }
}

AssemblyControl

  Storage is our AssemblyTest class library binary stream AssemblyWPFDemo AssemblyTestSource class, this binary stream through the DEMO conversion I write here, can see chart, which has a &ldquo code conversion; ” button, when you open a DLL it can be converted to C# code, get AssemblyTest by loading the data stream can assembly, the assembly you can use the assembly function. Because the AssemblyTestSource code more, post will affect the layout, you download DEMO.

  The other is some logic processing simple, not one one stick up, here I just play a valuable role, written in relatively rough, we should pay attention to is, binary assembly flow for covert, to art, or others or the binary flow out into the file, which also to DLL, a research can go to look at the binary data is encrypted, as far as possible to increase the difficulty of Decompilation.

  Here I have a nagging crackers and software developers, we write software protection is and the process of crack of wits, the two is a healthy competitive relationship, if you lose only shows that you are inferior, back down a book back, blindly blame doesn't make your technology to improve you know, people's thinking is the means to protect it better to design how to think, I believe that a good break, must also be a good software developers, the relationship between these two and no clear boundaries. . . Another early to wish you a happy National Day. . . The blog do not know when to come out. . . After a few I may be around to protect this theme to write..

Source code: http://pan.baidu.com/s/1xc9aW

If you read this blog, feel something for you, please click on the lower right corner of the [recommended]

If you want to reprint this blog, please indicate the source

If you have any opinions or suggestions for this paper, welcome message

Thank you for reading, please pay attention to the following blog of my Zengg

Author: Zengg source: This article copyright belongs to the author and blog park there, welcome to reprint, but without the permission of the author must keep this statement, and the connection in the article page is the obvious position, or retain the right to pursue legal responsibilities.


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Darren at November 14, 2013 - 4:23 PM