如何在C#中快速获取硬件ID?

50

我需要在我的程序中将许可证与硬件ID绑定。我尝试使用WMI,但它仍然较慢。

例如,我需要CPU、硬盘和主板信息。


6
这样的许可证方案似乎更像是对诚实客户的麻烦,而不是对破解者的威慑。 - CodesInChaos
请在此处查看答案:https://stackoverflow.com/a/50907399/3057246 - Vinod Srivastav
6个回答

80

要了解更多详细信息,请参考此链接

以下代码将提供您的CPU ID:

使用System.Management命名空间

var mbs = new ManagementObjectSearcher("Select ProcessorId From Win32_processor");
ManagementObjectCollection mbsList = mbs.Get();
string id = "";
foreach (ManagementObject mo in mbsList)
{
    id = mo["ProcessorId"].ToString();
    break;
}

要获取硬盘ID和主板ID的详细信息,请参考此链接

为了加快这个过程,请确保不要使用 SELECT *,而只选择你真正需要的内容。只有在开发阶段尝试找到需要使用的内容时才使用 SELECT *,因为这样查询将需要更长的时间来完成。


19
请注意,这些ID可能并不唯一,甚至可能不存在。processorID仅标识处理器设计,VolumeSerialnumber可以被用户更改,而有些主板制造商则没有设置序列号。 - Jens
1
生成唯一标识的最佳方法是通过组合所有这些标识符,即CPU标识符、主板标识符和硬盘标识符来创建一个标识符。 - HotTester
1
谢谢您的回复,但我认为 WMI 仍然很慢,您有没有其他不需要使用 WMI 的方法? - guaike
你说 WMI 很慢,但是你有多频繁地运行这些检查呢?实际上,你只需要在启动时进行检查,然后从不同的位置以随机间隔进行检查。如果它阻塞了你的应用程序,你可以将检查移到后台线程中,只需确保所有对例程的调用都在同一线程上,并适当地释放你的对象(根据 COM 规则)。 - Garo Yeriazarian
2
原帖显示安全(许可证)系统需要ID。然而,答案中提供的代码是从MS Windows读取信息的。修改这些信息非常容易。但是,硬件ID提取器DLL直接从CPU(硬件级别)获取信息。您无法更改CPU的硬件ID(除非您熔化CPU并重新构建它 :))。 - thelight
2
基准测试:当我的代码使用“Select * From Win32_processor”时,运行时间约为1秒,而当它使用“Select ProcessorId From Win32_processor”时,只需约0.01秒! - ANeves

27

我也曾经寻找同样的内容,不过我找到了另一种解决方案。如果你们感兴趣的话,我可以分享这个类:

using System;
using System.Management;
using System.Security.Cryptography;
using System.Security;
using System.Collections;
using System.Text;
namespace Security
{
    /// <summary>
    /// Generates a 16 byte Unique Identification code of a computer
    /// Example: 4876-8DB5-EE85-69D3-FE52-8CF7-395D-2EA9
    /// </summary>
    public class FingerPrint  
    {
        private static string fingerPrint = string.Empty;
        public static string Value()
        {
            if (string.IsNullOrEmpty(fingerPrint))
            {
                fingerPrint = GetHash("CPU >> " + cpuId() + "\nBIOS >> " + 
            biosId() + "\nBASE >> " + baseId() +
                            //"\nDISK >> "+ diskId() + "\nVIDEO >> " + 
            videoId() +"\nMAC >> "+ macId()
                                     );
            }
            return fingerPrint;
        }
        private static string GetHash(string s)
        {
            MD5 sec = new MD5CryptoServiceProvider();
            ASCIIEncoding enc = new ASCIIEncoding();
            byte[] bt = enc.GetBytes(s);
            return GetHexString(sec.ComputeHash(bt));
        }
        private static string GetHexString(byte[] bt)
        {
            string s = string.Empty;
            for (int i = 0; i < bt.Length; i++)
            {
                byte b = bt[i];
                int n, n1, n2;
                n = (int)b;
                n1 = n & 15;
                n2 = (n >> 4) & 15;
                if (n2 > 9)
                    s += ((char)(n2 - 10 + (int)'A')).ToString();
                else
                    s += n2.ToString();
                if (n1 > 9)
                    s += ((char)(n1 - 10 + (int)'A')).ToString();
                else
                    s += n1.ToString();
                if ((i + 1) != bt.Length && (i + 1) % 2 == 0) s += "-";
            }
            return s;
        }
        #region Original Device ID Getting Code
        //Return a hardware identifier
        private static string identifier
        (string wmiClass, string wmiProperty, string wmiMustBeTrue)
        {
            string result = "";
            System.Management.ManagementClass mc = 
        new System.Management.ManagementClass(wmiClass);
            System.Management.ManagementObjectCollection moc = mc.GetInstances();
            foreach (System.Management.ManagementObject mo in moc)
            {
                if (mo[wmiMustBeTrue].ToString() == "True")
                {
                    //Only get the first one
                    if (result == "")
                    {
                        try
                        {
                            result = mo[wmiProperty].ToString();
                            break;
                        }
                        catch
                        {
                        }
                    }
                }
            }
            return result;
        }
        //Return a hardware identifier
        private static string identifier(string wmiClass, string wmiProperty)
        {
            string result = "";
            System.Management.ManagementClass mc = 
        new System.Management.ManagementClass(wmiClass);
            System.Management.ManagementObjectCollection moc = mc.GetInstances();
            foreach (System.Management.ManagementObject mo in moc)
            {
                //Only get the first one
                if (result == "")
                {
                    try
                    {
                        result = mo[wmiProperty].ToString();
                        break;
                    }
                    catch
                    {
                    }
                }
            }
            return result;
        }
        private static string cpuId()
        {
            //Uses first CPU identifier available in order of preference
            //Don't get all identifiers, as it is very time consuming
            string retVal = identifier("Win32_Processor", "UniqueId");
            if (retVal == "") //If no UniqueID, use ProcessorID
            {
                retVal = identifier("Win32_Processor", "ProcessorId");
                if (retVal == "") //If no ProcessorId, use Name
                {
                    retVal = identifier("Win32_Processor", "Name");
                    if (retVal == "") //If no Name, use Manufacturer
                    {
                        retVal = identifier("Win32_Processor", "Manufacturer");
                    }
                    //Add clock speed for extra security
                    retVal += identifier("Win32_Processor", "MaxClockSpeed");
                }
            }
            return retVal;
        }
        //BIOS Identifier
        private static string biosId()
        {
            return identifier("Win32_BIOS", "Manufacturer")
            + identifier("Win32_BIOS", "SMBIOSBIOSVersion")
            + identifier("Win32_BIOS", "IdentificationCode")
            + identifier("Win32_BIOS", "SerialNumber")
            + identifier("Win32_BIOS", "ReleaseDate")
            + identifier("Win32_BIOS", "Version");
        }
        //Main physical hard drive ID
        private static string diskId()
        {
            return identifier("Win32_DiskDrive", "Model")
            + identifier("Win32_DiskDrive", "Manufacturer")
            + identifier("Win32_DiskDrive", "Signature")
            + identifier("Win32_DiskDrive", "TotalHeads");
        }
        //Motherboard ID
        private static string baseId()
        {
            return identifier("Win32_BaseBoard", "Model")
            + identifier("Win32_BaseBoard", "Manufacturer")
            + identifier("Win32_BaseBoard", "Name")
            + identifier("Win32_BaseBoard", "SerialNumber");
        }
        //Primary video controller ID
        private static string videoId()
        {
            return identifier("Win32_VideoController", "DriverVersion")
            + identifier("Win32_VideoController", "Name");
        }
        //First enabled network card ID
        private static string macId()
        {
            return identifier("Win32_NetworkAdapterConfiguration", 
                "MACAddress", "IPEnabled");
        }
        #endregion
    }
}

我不会为此负责,因为我在这里找到了它对于我来说,它的工作速度比我预期的要快。没有显卡、Mac和驱动程序ID,我在大约2-3秒钟内得到了唯一的ID。如果包括上述内容,我需要4-5秒钟才能得到它。
注意:添加对System.Management的引用。

下面呈现的DLL在不到1微秒的时间内检索信息。 - thelight
这段代码很好。谢谢。 - Majid Sabzalian
  1. 很好..
  2. 如果我使用这段代码1或2个月后,它是否仍会生成相同的ID?这是否会排除可移动USB硬盘、USB WiFi适配器、虚拟化软件中的虚拟网络卡?
- bh_earth0
1
使用这段代码一段时间后,出现了问题:插入USB驱动器会更改密钥,因为它从任何没有空值的项目中提取信息。实际上,整个标识符方法编写得很糟糕,每个属性调用多次必须每次创建管理类... - Mladen Mihajlovic
当超频时,MaxClockSpeed会改变吗? - LostPhysx

24

以下的方法受到这个答案对一个相关(更加普遍)问题的启发。

该方法是读取注册表键值 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Cryptography 中的 MachineGuid 值。此值是在操作系统安装期间生成的。

使用该方法,可以绕过硬件 ID 每台机器的唯一性。其中一种方法是编辑注册表值,但这将导致用户的机器之后出现复杂问题。另一种方法是克隆驱动器映像,这将复制 MachineGuid 值。

然而,没有任何方法是完全防篡改的,但对于普通用户而言,这肯定足够好了。而且,这种方法在性能上快速且易于实现。

public string GetMachineGuid()
{
   string location = @"SOFTWARE\Microsoft\Cryptography";
   string name = "MachineGuid";

   using (RegistryKey localMachineX64View = 
       RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64))
   {
       using (RegistryKey rk = localMachineX64View.OpenSubKey(location))
       {
           if (rk == null)
               throw new KeyNotFoundException(
                   string.Format("Key Not Found: {0}", location));

           object machineGuid = rk.GetValue(name);
           if (machineGuid == null)
               throw new IndexOutOfRangeException(
                   string.Format("Index Not Found: {0}", name));

           return machineGuid.ToString();
       }
   }
}

编辑注册表值是很快的,而且“不一定会在用户机器上产生问题” - 你可以编辑它,想做什么就做什么(在软件中),然后将值恢复回来。 - NSGaga-mostly-inactive
当您通过操作系统映像安装批量系统时,这并不是很有用。您将不得不为每台机器手动设置GUID。 - Lama

2

我们使用来自Win32_processor的处理器ID号(ProcessorID)和来自Win32_ComputerSystemProduct的通用唯一标识符(UUID)的组合:

ManagementObjectCollection mbsList = null;
ManagementObjectSearcher mos = new ManagementObjectSearcher("Select ProcessorID From Win32_processor");
mbsList = mos.Get();
string processorId = string.Empty;
foreach (ManagementBaseObject mo in mbsList)
{
    processorId = mo["ProcessorID"] as string;
}

mos = new ManagementObjectSearcher("SELECT UUID FROM Win32_ComputerSystemProduct");
mbsList = mos.Get();
string systemId = string.Empty;
foreach (ManagementBaseObject mo in mbsList)
{
    systemId = mo["UUID"] as string;
}

var compIdStr = $"{processorId}{systemId}";

以前,我们使用了一个组合:处理器ID("Select ProcessorID From Win32_processor")和主板序列号("SELECT SerialNumber FROM Win32_BaseBoard"),但后来我们发现主板序列号可能没有填写,或者填写了统一的值:

  • To be filled by O.E.M.
  • None
  • Default string

因此,需要考虑这种情况。

还要记住,ProcessorID号码在不同的计算机上可能是相同的。


1
我已经重构了Alex Sutu的方法,使代码更快且更简单:
using System;
using System.Collections.Generic;
using System.Text;
using System.Management;
using System.Security.Cryptography;

namespace Test
{
    /// <summary>
    /// Generates a Guid based on the current computer hardware
    /// Example: C384B159-8E36-6C85-8ED8-6897486500FF
    /// </summary>
    public class SystemGuid
    {
        private static string _systemGuid = string.Empty;
        public static string Value()
        {
            if (string.IsNullOrEmpty(_systemGuid))
            {
                var lCpuId = GetCpuId();
                var lBiodId = GetBiosId();
                var lMainboard = GetMainboardId();
                var lGpuId = GetGpuId();
                var lMac = GetMac();
                var lConcatStr = $"CPU: {lCpuId}\nBIOS:{lBiodId}\nMainboard: {lMainboard}\nGPU: {lGpuId}\nMAC: {lMac}";
                _systemGuid = GetHash(lConcatStr);
            }
            return _systemGuid;
        }
        private static string GetHash(string s)
        {
            try
            {
                var lProvider = new MD5CryptoServiceProvider();
                var lUtf8 = lProvider.ComputeHash(ASCIIEncoding.UTF8.GetBytes(s));
                return new Guid(lUtf8).ToString().ToUpper();
            }
            catch (Exception lEx)
            {
                return lEx.Message;
            }
        }

        #region Original Device ID Getting Code
    
        //Return a hardware identifier
        private static string GetIdentifier(string pWmiClass, List<string> pProperties)
        {
            string lResult = string.Empty;
            try
            {
                foreach (ManagementObject lItem in new ManagementClass(pWmiClass).GetInstances())
                {
                    foreach (var lProperty in pProperties)
                    {
                        try
                        {
                            switch(lProperty)
                            {
                                case "MACAddress":
                                    if (string.IsNullOrWhiteSpace(lResult) == false)
                                        return lResult; //Return just the first MAC

                                    if (lItem["IPEnabled"].ToString() != "True")
                                        continue;
                                break;
                            }                                

                            var lItemProperty = lItem[lProperty];
                            if (lItemProperty == null)
                                continue;

                            var lValue = lItemProperty.ToString();
                            if (string.IsNullOrWhiteSpace(lValue) == false)
                                lResult += $"{lValue}; ";
                        }
                        catch { }
                    }

                }
            }
            catch{}
            return lResult.TrimEnd(' ', ';');
        }

        private static List<string> ListOfCpuProperties = new List<string>{ "UniqueId", "ProcessorId", "Name", "Manufacturer" };

        private static string GetCpuId()
        {
            return GetIdentifier("Win32_Processor", ListOfCpuProperties);
        }

        private static List<string> ListOfBiosProperties = new List<string> { "Manufacturer", "SMBIOSBIOSVersion", "IdentificationCode", "SerialNumber", "ReleaseDate", "Version" };
        //BIOS Identifier
        private static string GetBiosId()
        {
            return GetIdentifier("Win32_BIOS", ListOfBiosProperties);
        }

        private static List<string> ListOfMainboardProperties = new List<string> { "Model", "Manufacturer", "Name", "SerialNumber" };
        //Motherboard ID
        private static string GetMainboardId()
        {
            return GetIdentifier("Win32_BaseBoard", ListOfMainboardProperties);
        }

        private static List<string> ListOfGpuProperties = new List<string> { "Name" };
        //Primary video controller ID
        private static string GetGpuId()
        {
            return GetIdentifier("Win32_VideoController", ListOfGpuProperties);
        }

        private static List<string> ListOfNetworkProperties = new List<string> { "MACAddress" };
        private static string GetMac()
        {
            return GetIdentifier("Win32_NetworkAdapterConfiguration", ListOfNetworkProperties);
        }

        #endregion
    }
}

-1

这里有一个DLL,可以显示以下信息:
* 硬盘ID(写在驱动器IDE电子芯片中的唯一硬件序列号)
* 分区ID(卷序列号)
* CPU ID(唯一的硬件ID)
* CPU供应商
* CPU运行速度
* CPU理论速度
* 内存负载(以百分比(%)表示的总内存使用量)
* 总物理内存(以字节为单位的总物理内存)
* 可用物理内存(以字节为单位的剩余物理内存)
* 总页面文件(以字节为单位的总页面文件)
* 可用页面文件(以字节为单位的剩余页面文件)
* 总虚拟内存(以字节为单位的总虚拟内存)
* 可用虚拟内存(以字节为单位的剩余虚拟内存)
* BIOS唯一识别号BiosDate
* BIOS唯一识别号BiosVersion
* BIOS唯一识别号BiosProductID
* BIOS唯一识别号BiosVideo

(文本来自原始网站)
它可以与C#一起使用。


2
想法是用C#来完成。 - Fredrik
2
@ Frederic - 原始帖子显示安全(许可证)系统需要ID。然而,答案中提供的代码是从MS Windows读取信息的。修改该信息非常容易。但是,硬件ID提取器DLL直接从CPU(硬件级别)获取信息。您无法更改CPU的硬件ID(除非您熔化CPU并重新构建它 :)) - thelight
1
此外,它可以在不到1微秒的时间内获取信息。而从MS Windows检索信息需要长达5秒钟! - thelight
1
@FredrikRedin - C#不能导入DLL吗? - thelight
2
这是来自Google的VirusTotal.com的扫描结果。62个杀毒软件中,只有3个(不太出名的)杀毒软件显示出(虚假的)阳性警报。所有大型(卡巴斯基、诺顿、熊猫等)杀毒软件都显示该文件为安全的。https://virustotal.com/en/file/ff46403f20b7434f8da6e68d6c51ea0ad9acea39b5e0f1be613d06b2461d69f7/analysis/ - Gabriel
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接