x86 环境下获取 cpuid 参考 https://blog.csdn.net/liulilittle/article/details/80958926
x86/x64 环境下获取 cpuid 参考
https://github.com/liulilittle/nsjs/blob/c11a4377b1b3da7376bc5d1ecdb5b1afb2759e1e/nsjsdotnet/Core/Utilits/Hardware.cs
本文代码摘要自 nsjs::nsjsdotnet ,auther git:liulilittle。
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()
{
/*
mov dword ptr [rsp+10h],edx
mov dword ptr [rsp+8],ecx
push rbp
push rdi
sub rsp,0C8h
mov rbp,rsp
mov rdi,rsp
mov ecx,32h
mov eax,0CCCCCCCCh
rep stos dword ptr [rdi]
mov eax, 01h
xor ecx, ecx
xor edx, edx
cpuid
mov ecx,dword ptr [rbp+00000000000000E0h]
mov dword ptr[ecx], edx
mov ecx,dword ptr [rbp+00000000000000E8h]
mov dword ptr[ecx], eax
lea rsp,[rbp+00000000000000C8h]
pop rdi
pop rbp
ret
*/
byte[] shellcode =
{
137,84,36,16,137,76,36,8,85,87,72,129,236,200,0,0,0,72,139,236,72,139,252,185,
50,0,0,0,184,204,204,204,204,243,171,184,1,0,0,0,51,201,51,210,15,162,139,141,
224,0,0,0,103,137,17,139,141,232,0,0,0,103,137,1,72,141,165,200,0,0,0,95,93,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;
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);
}
}