This repository has been archived on 2022-08-05. You can view files and clone it, but cannot push or open issues or pull requests.
CC-Functions/Misc/HID.cs

107 lines
4.1 KiB
C#
Raw Normal View History

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
{
public static class HID
{
2019-12-30 15:09:16 +01:00
public static bool forceWindows = false;
2019-11-29 21:27:24 +01:00
private static byte[] _fingerPrint;
2019-11-29 21:27:24 +01:00
private static readonly string HIDClasses = @"Win32_Processor:UniqueId
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";
2019-11-29 21:27:24 +01:00
public static byte[] Value
{
get
{
2019-11-29 21:27:24 +01:00
if (_fingerPrint == null)
{
2019-12-22 16:58:16 +01:00
string fingerprintTmp = "";
2019-12-30 15:09:16 +01:00
if (forceWindows || Type.GetType("Mono.Runtime") == null)
2019-11-29 21:27:24 +01:00
{
2019-12-30 15:09:16 +01:00
HIDClasses.Split(new[] {"\r\n"}, StringSplitOptions.None).Select(s =>
{
if (s.StartsWith("\n"))
s = s.Remove(0, 1);
return s.Split(':');
}).ToList().ForEach(s =>
2019-11-29 21:27:24 +01:00
{
2019-12-22 16:58:16 +01:00
using (ManagementClass mc = new ManagementClass(s[0]))
using (ManagementObjectCollection moc = mc.GetInstances())
2019-11-29 21:27:24 +01:00
{
2019-12-22 16:58:16 +01:00
ManagementBaseObject[] array = moc.OfType<ManagementBaseObject>().ToArray();
for (int j = 0; j < array.Length; j++)
try
{
2019-12-22 16:58:16 +01:00
fingerprintTmp += array[j][s[1]].ToString();
break;
}
catch
{
2019-12-22 16:58:16 +01:00
Console.WriteLine("Failed to read property");
}
2019-11-29 21:27:24 +01:00
}
});
}
2019-12-30 15:09:16 +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
{
2019-12-22 16:58:16 +01:00
Process p = new Process
{
StartInfo =
{
UseShellExecute = false,
RedirectStandardOutput = true,
FileName = "uname",
Arguments = "-nmpio"
}
};
p.Start();
2019-12-22 16:58:16 +01:00
fingerprintTmp = p.StandardOutput.ReadToEnd();
p.WaitForExit();
p.StartInfo.FileName = "lscpu";
2019-12-22 16:58:16 +01:00
p.StartInfo.Arguments = "-ap";
p.Start();
2019-12-22 16:58:16 +01:00
fingerprintTmp += p.StandardOutput.ReadToEnd();
p.WaitForExit();
2019-12-30 15:09:16 +01:00
p.StartInfo.FileName = "ip";
p.StartInfo.Arguments = "link";
p.Start();
fingerprintTmp += p.StandardOutput.ReadToEnd();
p.WaitForExit();
}
2019-11-29 21:27:24 +01:00
using (MD5 sec = new MD5CryptoServiceProvider())
{
2019-12-22 16:58:16 +01:00
byte[] bt = Encoding.ASCII.GetBytes(fingerprintTmp);
2019-11-29 21:27:24 +01:00
_fingerPrint = sec.ComputeHash(bt);
}
}
2019-11-29 21:27:24 +01:00
return _fingerPrint;
}
}
2019-12-22 16:58:16 +01:00
public static byte[] EncryptLocal(byte[] unencrypted) =>
ProtectedData.Protect(unencrypted, Value, DataProtectionScope.CurrentUser);
2019-11-29 21:27:24 +01:00
2019-12-22 16:58:16 +01:00
public static byte[] DecryptLocal(byte[] encrypted) =>
ProtectedData.Unprotect(encrypted, Value, DataProtectionScope.CurrentUser);
2019-11-29 21:27:24 +01:00
}
}