获取CPUID序列号的两种办法

53 篇文章 2 订阅

    Win32k 平台上,获取CPUID的办法主要有两种,一种是利用 WMI 另一种是利用 x86 汇编的 cpuid 指令,而最快的办法就是通过汇编了,而且 WMI 与汇编之间效率上的差距的确有点让人难以忍受,WMI 获取 CPUID 的效率几乎接近了一秒钟,而利用 cpuid 指令的办法,大概是几个 us 时间的问题,这种令人咋舌的巨大差异,让人有些难以忍受。

using System;
using System.Management;
using System.Runtime.InteropServices;

static class Program
{
    [DllImport("kernel32.dll", SetLastError = false)]
    private static extern bool VirtualProtect(IntPtr lpAddress, uint dwSize, uint flNewProtect, out uint lpflOldProtect);

    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
    public delegate int __cpuid(ref int s1, ref int s2);

    static void Main()
    {
        /*
                    pushad
		    mov eax, 01h
		    xor ecx, ecx
		    xor edx, edx
		    cpuid
		    mov ecx, dword ptr[ebp + 8]
		    mov dword ptr[ecx], edx
		    mov ecx, dword ptr[ebp + 0Ch]
		    mov dword ptr[ecx], eax
		    popad
        */
        byte[] shellcode = { 96, 184, 1, 0, 0, 0, 51, 201, 51, 210, 15, 162, 139, 77, 8, 137, 17, 139, 77, 12, 137, 1, 97, 195 };
        IntPtr address = GCHandle.Alloc(shellcode, GCHandleType.Pinned).AddrOfPinnedObject();
        VirtualProtect(address, (uint)shellcode.Length, 0x40, out uint lpflOldProtect);
        __cpuid cpuid = (__cpuid)Marshal.GetDelegateForFunctionPointer(address, typeof(__cpuid));

        int s1 = 0;
        int s2 = 0;
        for (int i = 0; i < 100000; i++)
        {
            cpuid(ref s1, ref s2);
        }
        Console.Write("asm: {0}", s1.ToString("X2") + s2.ToString("X2"));
        using (ManagementClass mc = new ManagementClass("Win32_Processor"))
        {
            ManagementObjectCollection moc = mc.GetInstances();
            foreach (ManagementObject mo in moc)
            {
                Console.WriteLine(", wmi: {0}", mo.Properties["ProcessorId"].Value.ToString());
            }
        }
        Console.ReadKey(false);
    }
}

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值