许可和授权的研究及其破解

一个非常不错的office 07的界面控件。可以浏览下:

 http://www.divelements.com/net/

非注册版本有30天的使用限制。使用.net reflector,可以看到关键地方:

 namespace Divelements.Util.Registration

{

。。。

复制代码
代码
private bool xa1d7cab22b1cb36a()
        {
            string str2;
            AssemblyName name = Assembly.GetExecutingAssembly().GetName();
            string assemblyProductName = x294bd621a33dc533.GetAssemblyProductName(Assembly.GetExecutingAssembly());
            using (SHA1 sha = SHA1.Create())
            {
                string s = string.Concat(new object[] { assemblyProductName, name.Version.Major, ".", name.Version.Minor, ".", name.Version.Build });
                str2 = Convert.ToBase64String(sha.ComputeHash(Encoding.Default.GetBytes(s)));
            }
            RegistryKey key = Registry.CurrentUser.OpenSubKey(@"SoftwareMicrosoft.NETFramework", true);
            if (key == null)
            {
                try
                {
                    key = Registry.CurrentUser.CreateSubKey(@"SoftwareMicrosoft.NETFramework");
                }
                catch
                {
                    return true;
                }
            }
            DateTime minValue = DateTime.MinValue;
            try
            {
                string str4 = (string) key.GetValue(str2);
                if (str4 == null)
                {
                    key.SetValue(str2, DateTime.Now.ToFileTime().ToString());
                    return false;
                }
                minValue = DateTime.FromFileTime(long.Parse(str4));
            }
            finally
            {
                key.Close();
            }
            return (DateTime.Now > (minValue + new TimeSpan(30, 0, 0, 0)));
        }
复制代码

这个公司使用了.net自带的版权控制机制,所以搞起来即麻烦又简单。

本来以为是通过网络去验证是否过期,看来原来是利用了.net的机制操作了注册表。

反正只要上面的方法返回 false,就能够绕过。如果返回true,则过期。

破解方法是直接修改IL。如果反编译,会出现错误。毕竟使用了 Xenocode。

1. 首先使用ildasm.exe 解压dll为il。命令为:

   ildasm Divelements.SandRibbon.dll /out=Divelements.SandRibbon.il

2. 然后寻找类: xa1d7cab22b1cb36a。搜一下就有了。要找到里面的方法:有个设置timespan的地方:

 ldc.i4.s 30

 修改为

 ldc.i4 3650

3. 再打包:

 C:WINDOWSMicrosoft.NETFrameworkv2.0.50727ilasm /res:Divelements.SandRibbon.res /RESOURCE=Divelements.SandRibbon.ClientPanel.png /RESOURCE=Divelements.SandRibbon.ContextPopup.png Divelements.SandRibbon.il /RESOURCE=Divelements.SandRibbon.Resources.check.png /RESOURCE=Divelements.SandRibbon.Resources.defaultimage.png /RESOURCE=Divelements.SandRibbon.Resources.exit.png /RESOURCE=Divelements.SandRibbon.Resources.furtheroptions.png /RESOURCE=Divelements.SandRibbon.Resources.help.png /RESOURCE=Divelements.SandRibbon.Resources.Messages.resources /RESOURCE=Divelements.SandRibbon.Resources.options.png /RESOURCE=Divelements.SandRibbon.Ribbon.png /RESOURCE=Divelements.SandRibbon.RibbonManager.png /RESOURCE=Divelements.SandRibbon.StatusBar.png /RESOURCE=Divelements.SandRibbon.ToolBar.png /RESOURCE=Divelements.SandRibbon.x1818ca8d87654aad.resources  /out:Divelements.SandRibbon.dll /dll

之后就可以用了。至于网上下载的dll是否真的被限制了。。。。我还是说不清楚。因为要去看微软如何实现license的。比较麻烦。。 

参考文献:

http://www.cnblogs.com/jianggest/archive/2009/05/04/ldc.html

 http://www.cnblogs.com/midea0978/articles/81072.html

-------------------------------------------------

Posted on 2006-09-12 10:01 hcfalan 阅读(1152) 评论(0) 编辑 收藏
源代码下载:LicenseDemo.rar

首先,需要实现一个LicenseProvider
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Management;
using System.Security.Cryptography;
using System.Security.Permissions;

namespace LicenseDemo
{
    [ReflectionPermission(SecurityAction.Deny, MemberAccess=false, ReflectionEmit=false)]
    internal class MyLicenseProvider : LicenseProvider
    {
        Define MyLicense

        public MyLicenseProvider()
        { }

        /// <summary>
        /// 获取本机MAC地址
        /// </summary>
        private String GetMacAddress()
        {
            String macAddr = null;
            ManagementClass inetAdapter = new ManagementClass("WIN32_NetworkAdapterConfiguration");
            ManagementObjectCollection objList = inetAdapter.GetInstances();
            foreach (ManagementObject mobj in objList)
            {
                if ((bool)mobj["IPEnabled"])
                {
                    macAddr = mobj["MacAddress"].ToString().Replace(":", "-");
                    break;
                }
            }
            return macAddr;
        }


        /// <summary>
        /// 获取Assembly所在目录
        /// </summary>
        private String GetAssemblyPath(LicenseContext context)
        {
            String fileName = null;
            Type type = this.GetType();
            ITypeResolutionService service = (ITypeResolutionService)context.GetService(typeof(ITypeResolutionService));
            if (service != null)
            {
                fileName = service.GetPathOfAssembly(type.Assembly.GetName());
            }
            if (fileName == null)
            {
                fileName = type.Module.FullyQualifiedName;
            }
            return Path.GetDirectoryName(fileName);
        }


        private String Encrypt(String source)
        {
            /**
             * 加密算法
             */
            //byte[] keyData = Encoding.ASCII.GetBytes("Zx2@Yt8P");
            //byte[] ivData =  Encoding.ASCII.GetBytes("4iJ9Qw#L");
            //MemoryStream stream = new MemoryStream();
            //DES desProvider = new DESCryptoServiceProvider();
            //CryptoStream cs = new CryptoStream(stream,
            //    desProvider.CreateEncryptor(keyData, ivData),
            //    CryptoStreamMode.Write);
            //byte[] buffer = Encoding.ASCII.GetBytes(source);
            //cs.Write(buffer, 0, buffer.Length);
            //cs.FlushFinalBlock();
            //cs.Close();

            //buffer = stream.GetBuffer();
            //stream.Close();
            //return Convert.ToBase64String(buffer);

            return source;
        }


        public override License GetLicense(LicenseContext context, Type type, object instance, bool allowExceptions)
        {
            MyLicense license = null;
            
            // 计算MAC地址加密串
            String macAddr = this.GetMacAddress();
            String encrypt = this.Encrypt(macAddr);

            if (context != null)
            {
                if (context.UsageMode == LicenseUsageMode.Runtime)
                {
                    String savedLicenseKey = context.GetSavedLicenseKey(type, null);
                    if (encrypt.Equals(savedLicenseKey))
                    {
                        return new MyLicense(this, encrypt);
                    }
                }
                if (license != null)
                {
                    return license;
                }
                
                // 打开License文件 'license.dat'
                String path = this.GetAssemblyPath(context);
                String licFile = Path.Combine(path, "license.dat");
                if (File.Exists(licFile))
                {
                    Stream fs = new FileStream(licFile, FileMode.Open, FileAccess.Read);
                    StreamReader sr = new StreamReader(fs);
                    String readedLicenseKey = sr.ReadToEnd();
                    sr.Close();
                    fs.Close();

                    if (encrypt.Equals(readedLicenseKey))
                    {
                        license = new MyLicense(this, encrypt);
                    }
                }

                if (license != null)
                {
                    context.SetSavedLicenseKey(type, encrypt);
                }
            }

            if (license == null)
            {
                System.Windows.Forms.MessageBox.Show("!!!尚未注册!!!");
                return new MyLicense(this, "evaluate");
            }

            return license;
        }
    }
}

然后,在需要许可控制的组件上使用该LicenseProvider:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace LicenseDemo
{
    [LicenseProvider(typeof(MyLicenseProvider))]
    public partial class Form1 : Form
    {
        private License mLicense = null;
        
        public Form1()
        {
            this.mLicense = LicenseManager.Validate(typeof(Form1), this);
            InitializeComponent();
        }

        ~Form1()
        {
            if (this.mLicense != null)
            {
                this.mLicense.Dispose();
                this.mLicense = null;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            System.Windows.Forms.MessageBox.Show("Hello, world!");
        }
    }
}

直接运行,在Form1窗口出来之前,先会Show出一个“!!!尚未注册!!!”的对话框,这是因为还没有提供License文件的缘故。然后在程序exe文件目录创建一个license.dat文件,使用notepad编辑,直接将本机Mac地址复制进来,然后保存该文件,此时相当于用户已经提供了正确的许可文件,再运行,就不会提示尚未注册的对话框了。
 
---------------------------------

Ldc.i4 num  ldc.i4.s num 的区别

       近日开始除了看除了看《入门经典》外,还在看《你必须知道的.net》。在看到《你必须知道的.net》的第二部分--本质  的时候,他开始讲“IL”了,看了其中的送数据到堆栈中,用到了ldc.i4 num 和 ldc.i4.s num 。

       初看不懂,就查了MSDN ,解释如下;

下表列出了指令的十六进制和 Microsoft 中间语言 (MSIL) 汇编格式,以及简短的参考摘要:

格式

汇编格式

说明

20 < int32 >

ldc.i4 num

将值 num 推送到堆栈上。

堆栈转换行为依次为:

1.        将值 num 推送到堆栈上。

请注意,对于整数 -128 到 127 有特殊的简短编码(并因此更有效),对于整数 -1 到 8 尤其有特殊的简短编码。所有简短编码都将 4 字节整数推送到堆栈上。较长的编码用于 8 字节整数以及 4 和 8 字节浮点数,并且用于不适合短格式的 4 字节值。有三种方法可以将 8 字节整数常数推送到堆栈上

1. 使用 Ldc_I8 指令用于必须以超过 32 位表示的常数。

2. 使用 Ldc_I4 指令(后跟 Conv_I8)用于需要 9 到 32 位的常数。

3. 使用短格式指令(后跟 Conv_I8)用于可以 8 位或更少位表示的常数。

 

下表列出了指令的十六进制和 Microsoft 中间语言 (MSIL) 汇编格式,以及简短的参考摘要:

格式

汇编格式

说明

1F < int8 >

ldc.i4.s num

将 num 作为 int32 推送到堆栈上(短格式)。

堆栈转换行为依次为:

1.        将值 num 推送到堆栈上。

对于将从 -127 到 128 的整数推送到计算堆栈,ldc.i4.s 是更有效的编码。

       但是看了这东西后,感觉还是不是很清楚,就想直接理解,直接的理解就是在小于8大于0的情况下用的是ldc.i4 num ,而大于和小于0又是针对32位(之所以说是对于32位,是因为在64 位下还有ldc.i8 num )的机器编程就用 ldc.i4.s num 。

       此外针对float32还有ldc.r4 num , 针对float64 还有ldc.r8 num ,最后一个是针对 -1用的是ldc.i4.m1。

原文地址:https://www.cnblogs.com/jumahe/p/4654639.html