2019-12-22 00:29:53 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.Linq;
|
2019-11-29 21:27:24 +01:00
|
|
|
|
using System.Management;
|
|
|
|
|
using System.Security.Cryptography;
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
2019-12-22 16:58:16 +01:00
|
|
|
|
namespace CC_Functions.Misc
|
2019-11-29 21:27:24 +01:00
|
|
|
|
{
|
2020-05-23 15:32:24 +02:00
|
|
|
|
/// <summary>
|
2020-05-23 17:22:45 +02:00
|
|
|
|
/// Functions for hardware identidiers
|
2020-05-23 15:32:24 +02:00
|
|
|
|
/// </summary>
|
|
|
|
|
public static class Hid
|
2019-11-29 21:27:24 +01:00
|
|
|
|
{
|
2020-05-23 15:32:24 +02:00
|
|
|
|
/// <summary>
|
2020-05-23 17:22:45 +02:00
|
|
|
|
/// Whether to force Win32-based operation
|
2020-05-23 15:32:24 +02:00
|
|
|
|
/// </summary>
|
|
|
|
|
public static bool ForceWindows = false;
|
2020-05-23 17:22:45 +02:00
|
|
|
|
|
2019-11-29 21:27:24 +01:00
|
|
|
|
private static byte[] _fingerPrint;
|
2020-01-07 17:59:32 +01:00
|
|
|
|
|
2020-05-23 17:22:45 +02:00
|
|
|
|
private static readonly string HidClasses = @"Win32_Processor:UniqueId
|
2019-11-29 21:27:24 +01:00
|
|
|
|
Win32_Processor:ProcessorId
|
|
|
|
|
Win32_Processor:Name
|
|
|
|
|
Win32_Processor:Manufacturer
|
|
|
|
|
Win32_BIOS:Manufacturer
|
|
|
|
|
Win32_BIOS:SMBIOSBIOSVersion
|
|
|
|
|
Win32_BIOS:IdentificationCode
|
|
|
|
|
Win32_BIOS:SerialNumber
|
|
|
|
|
Win32_BIOS:ReleaseDate
|
|
|
|
|
Win32_BIOS:Version
|
|
|
|
|
Win32_BaseBoard:Model
|
|
|
|
|
Win32_BaseBoard:Manufacturer
|
|
|
|
|
Win32_BaseBoard:Name
|
|
|
|
|
Win32_BaseBoard:SerialNumber
|
2019-12-30 15:09:16 +01:00
|
|
|
|
Win32_NetworkAdapterConfiguration:MACAddress";
|
2020-05-23 17:22:45 +02:00
|
|
|
|
|
2020-05-23 15:32:24 +02:00
|
|
|
|
/// <summary>
|
2020-05-23 17:22:45 +02:00
|
|
|
|
/// The HID for this machine
|
2020-05-23 15:32:24 +02:00
|
|
|
|
/// </summary>
|
2019-11-29 21:27:24 +01:00
|
|
|
|
public static byte[] Value
|
|
|
|
|
{
|
2019-12-22 00:29:53 +01:00
|
|
|
|
get
|
|
|
|
|
{
|
2020-03-13 19:19:55 +01:00
|
|
|
|
if (_fingerPrint != null) return _fingerPrint;
|
|
|
|
|
string fingerprintTmp = "";
|
2020-05-23 17:22:45 +02:00
|
|
|
|
if (ForceWindows ||
|
|
|
|
|
new[]
|
|
|
|
|
{
|
|
|
|
|
PlatformID.Xbox, PlatformID.Win32S, PlatformID.Win32Windows, PlatformID.Win32NT,
|
|
|
|
|
PlatformID.WinCE
|
|
|
|
|
}.Contains(Environment.OSVersion.Platform))
|
|
|
|
|
HidClasses.Split(new[] {"\r\n"}, StringSplitOptions.None).Select(s =>
|
2019-11-29 21:27:24 +01:00
|
|
|
|
{
|
2020-03-13 19:19:55 +01:00
|
|
|
|
if (s.StartsWith("\n"))
|
|
|
|
|
s = s.Remove(0, 1);
|
|
|
|
|
return s.Split(':');
|
|
|
|
|
}).ToList().ForEach(s =>
|
|
|
|
|
{
|
|
|
|
|
using ManagementClass mc = new ManagementClass(s[0]);
|
|
|
|
|
using ManagementObjectCollection moc = mc.GetInstances();
|
|
|
|
|
ManagementBaseObject[] array = moc.OfType<ManagementBaseObject>().ToArray();
|
|
|
|
|
for (int j = 0; j < array.Length; j++)
|
|
|
|
|
try
|
2019-11-29 21:27:24 +01:00
|
|
|
|
{
|
2020-03-13 19:19:55 +01:00
|
|
|
|
fingerprintTmp += array[j][s[1]].ToString();
|
|
|
|
|
break;
|
2019-11-29 21:27:24 +01:00
|
|
|
|
}
|
2020-03-13 19:19:55 +01:00
|
|
|
|
catch
|
2019-12-22 00:29:53 +01:00
|
|
|
|
{
|
2020-03-13 19:19:55 +01:00
|
|
|
|
Console.WriteLine("Failed to read property");
|
2019-12-22 00:29:53 +01:00
|
|
|
|
}
|
2020-03-13 19:19:55 +01:00
|
|
|
|
});
|
|
|
|
|
else //Linux implementation. This will not work if you are using Mono on windows or do not have "uname", "lscpu" and "id" available
|
|
|
|
|
{
|
|
|
|
|
Process p = new Process
|
2019-11-29 21:27:24 +01:00
|
|
|
|
{
|
2020-03-13 19:19:55 +01:00
|
|
|
|
StartInfo =
|
|
|
|
|
{
|
|
|
|
|
UseShellExecute = false,
|
|
|
|
|
RedirectStandardOutput = true,
|
|
|
|
|
FileName = "uname",
|
|
|
|
|
Arguments = "-nmpio"
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
p.Start();
|
|
|
|
|
fingerprintTmp = p.StandardOutput.ReadToEnd();
|
|
|
|
|
p.WaitForExit();
|
|
|
|
|
p.StartInfo.FileName = "lscpu";
|
|
|
|
|
p.StartInfo.Arguments = "-ap";
|
|
|
|
|
p.Start();
|
|
|
|
|
fingerprintTmp += p.StandardOutput.ReadToEnd();
|
|
|
|
|
p.WaitForExit();
|
|
|
|
|
p.StartInfo.FileName = "ip";
|
|
|
|
|
p.StartInfo.Arguments = "link";
|
|
|
|
|
p.Start();
|
|
|
|
|
fingerprintTmp += p.StandardOutput.ReadToEnd();
|
|
|
|
|
p.WaitForExit();
|
2019-11-29 21:27:24 +01:00
|
|
|
|
}
|
2020-03-13 19:19:55 +01:00
|
|
|
|
using MD5 sec = new MD5CryptoServiceProvider();
|
|
|
|
|
byte[] bt = Encoding.ASCII.GetBytes(fingerprintTmp);
|
|
|
|
|
_fingerPrint = sec.ComputeHash(bt);
|
2019-12-22 00:29:53 +01:00
|
|
|
|
|
2019-11-29 21:27:24 +01:00
|
|
|
|
return _fingerPrint;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-05-23 17:22:45 +02:00
|
|
|
|
|
2020-05-23 15:32:24 +02:00
|
|
|
|
/// <summary>
|
2020-05-23 17:22:45 +02:00
|
|
|
|
/// Encrypts data using <see cref="Crypto">Crypto's</see> Encrypt and the HID
|
2020-05-23 15:32:24 +02:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="unencrypted">The data to encrypt</param>
|
|
|
|
|
/// <returns>The encrypted data</returns>
|
2019-12-22 16:58:16 +01:00
|
|
|
|
public static byte[] EncryptLocal(byte[] unencrypted) =>
|
2020-04-03 17:32:22 +02:00
|
|
|
|
Crypto.Encrypt(unencrypted, Value);
|
2020-05-23 17:22:45 +02:00
|
|
|
|
|
2020-05-23 15:32:24 +02:00
|
|
|
|
/// <summary>
|
2020-05-23 17:22:45 +02:00
|
|
|
|
/// Decrypts data using <see cref="Crypto">Crypto's</see> Decrypt and the HID
|
2020-05-23 15:32:24 +02:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="encrypted">The data to decrypt</param>
|
|
|
|
|
/// <returns>The decrypted data</returns>
|
2019-12-22 16:58:16 +01:00
|
|
|
|
public static byte[] DecryptLocal(byte[] encrypted) =>
|
2020-04-03 17:32:22 +02:00
|
|
|
|
Crypto.Decrypt(encrypted, Value);
|
2019-11-29 21:27:24 +01:00
|
|
|
|
}
|
|
|
|
|
}
|