How to detect the original MAC address after it has been spoofed?

前端 未结 4 832
慢半拍i
慢半拍i 2020-11-28 08:47

We are using the following code for retrieving active MAC address of a windows pc.

private static string macId()
{
    return identifier(\"Win32_NetworkAdapt         


        
4条回答
  •  余生分开走
    2020-11-28 09:34

    Well, I wouldn't bet all my money on the order in which NetworkInterface class lists NetworkInterfaces. My mainboard has 2 adapters and the order seems to switch every time I reboot.

    So here is a suggestion, which worked for me (BTW : credits goes probably to another awesome stackoverflow contributer, ty) :

        public static string GetMACAddress()
        {
            NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
            //for each j you can get the MAC 
            PhysicalAddress address = nics[0].GetPhysicalAddress();
            byte[] bytes = address.GetAddressBytes();
    
            string macAddress = "";
    
            for (int i = 0; i < bytes.Length; i++)
            {
                // Format the physical address in hexadecimal. 
                macAddress += bytes[i].ToString("X2");
                // Insert a hyphen after each byte, unless we are at the end of the address. 
                if (i != bytes.Length - 1)
                {
                    macAddress += "-";
                }
            }
    
            return macAddress;
        }
    

提交回复
热议问题