c# - 被欺骗后如何检测原始MAC地址?

标签 c# windows networking uniqueidentifier spoofing

我们使用以下代码来检索 Windows PC 的事件 MAC 地址。

private static string macId()
{
    return identifier("Win32_NetworkAdapterConfiguration", "MACAddress", "IPEnabled");
}

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;
}

它可以很好地检索 MAC 地址。问题是当 MAC 地址被欺骗时,它会返回被欺骗的 MAC 地址。我们想以某种方式检索在工厂分配的唯一的原始 MAC 地址。有什么办法吗?

最佳答案

我想提供一个替代方案。我不知道它是否真的回答了“一种唯一标识任何计算机的方法”。
但是,此方法查询 System.Management 中的 Win32_BIOS 类并返回一个很有可能是唯一的字符串。 (等待被拒绝!!)

/// <summary>
/// BIOS IDentifier
/// </summary>
/// <returns></returns>
public static string BIOS_ID()
{
    return    GetFirstIdentifier("Win32_BIOS", "Manufacturer")
            + GetFirstIdentifier("Win32_BIOS", "SMBIOSBIOSVersion")
            + GetFirstIdentifier("Win32_BIOS", "IdentificationCode")
            + GetFirstIdentifier("Win32_BIOS", "SerialNumber")
            + GetFirstIdentifier("Win32_BIOS", "ReleaseDate")
            + GetFirstIdentifier("Win32_BIOS", "Version");
}

/// <summary>
/// ManagementClass used to read the first specific properties
/// </summary>
/// <param name="wmiClass">Object Class to query</param>
/// <param name="wmiProperty">Property to get info</param>
/// <returns></returns>
private static string GetFirstIdentifier(string wmiClass, string wmiProperty)
{
    string result = string.Empty;
    ManagementClass mc = new System.Management.ManagementClass(wmiClass);
    ManagementObjectCollection moc = mc.GetInstances();
    foreach (ManagementObject mo in moc)
    {
        //Only get the first one
        if (string.IsNullOrEmpty(result))
        {
            try
            {
                if (mo[wmiProperty] != null) result = mo[wmiProperty].ToString();
                break;
            }
            catch
            {
            }
        }
    }
    return result.Trim();
}

关于c# - 被欺骗后如何检测原始MAC地址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9546228/

相关文章:

c# - LINQ to Entities 不支持扩展方法?

c# - 使用 C# WebBrowser 插入 CSS

c# - Microsoft.Azure.Cosmos.Table 和 Microsoft.WindowsAzure.Storage 之间的 ExecuteQuerySegmentedAsync 性能显着下降

windows - 无论如何我可以在 Mac 上创建 Windows 8 应用程序(适用于 PC 和移动设备)

java - 当 Windows 连接到网络时收到通知

c# - 事件 EventHandler 分配给空委托(delegate)

windows - 如何使用 Perl 的 POE 识别 Windows PnP 事件?

Windows命令行字符串解析: folder and filename in string

python - 不阻塞地服务单个 HTTP 请求

c++ - 如何在 C 中实现 RFC 3393(Ipdv 数据包延迟变化)?