Jump to content
alien

[C#] HWID made easy

Recommended Posts

This tutorial will explain the basics of generating and verifying of HWID's.

First things first, "What the hell is a HWID??" Most of you already know this so I will keep it short. Ever since Windows XP Product Activation came around, when the OS is installed an unique ID is generated for the basic hardware components in the system. This ensures that a software will be bound to those unique ID, so it can't be moved/run on other machines. Of course there are methods to bypass this, but this is not the scope of this tutorial.

Also to make it more "secure" we will be using the triple DES algorithm to crypt the generated CPU and HDD Id's.

A short overview:

The class will be called Hwid and will contain 2 properties(CpuId,HddId plain text), Generate, Verify and EncryptString methods.

The Verify method takes 2 parameters: password(used for DES) and path. The path can be also an URL to a website hosting the file with the HWID's.

The void Main() contains a simple console application to see how Hwid class works.

The code is commented and self explanatory I would say.

Source code: hg clone https://bitbucket.org/rokill3r/hwid

Hwid class


class Hwid
{
private string _cpuId = string.Empty;
private string _hddId = string.Empty;

public Hwid()
{
_cpuId = GetCpuId();
_hddId = GetHddId("C");
}

/// <summary>
/// Gets first CPU unique ID
/// </summary>
/// <returns></returns>
private string GetCpuId()
{
ManagementClass cpuManager = new ManagementClass("win32_processor");
ManagementObjectCollection cpuCollection = cpuManager.GetInstances();

foreach (ManagementObject cpu in cpuCollection)
{
// Return first CPU Id
return cpu.Properties["processorID"].Value.ToString();
}
return string.Empty;
}

/// <summary>
/// Gets the drives unique ID
/// </summary>
/// <param name="drive">Drive name(eg: C,D..)</param>
/// <returns></returns>
private string GetHddId(string drive)
{
ManagementObject dsk = new ManagementObject(@"win32_logicaldisk.deviceid=""" + drive + @":""");
dsk.Get();
return dsk["VolumeSerialNumber"].ToString();
}
/// <summary>
/// Encrypts the HWID using tripple DES algorithm
/// </summary>
/// <param name="Message">String to crypt</param>
/// <param name="Passphrase">Password to use when crypting</param>
/// <returns></returns>
private string EncryptString(string Message, string Passphrase) ...

/// <summary>
/// Generates an encrypted HWID
/// </summary>
/// <param name="password">Password for the DES crypt algorith</param>
/// <returns>Encrypted HWID</returns>
public string Generate(string password)
{
return EncryptString(_cpuId + _hddId, password);
}
public string Generate(string password, string path)
{
try
{
string cryptedHwid = EncryptString(_cpuId + _hddId, password);

// Write the crypted Hwid to file
System.IO.StreamWriter file = new System.IO.StreamWriter(path);
file.WriteLine(cryptedHwid);
file.Close();
return cryptedHwid;
}
catch (Exception ex)
{
return ex.Message;
}
}
/// <summary>
/// Generates an encrypted HWID using the tripple DES algorithm
/// </summary>
/// <param name="password">Password for the DES crypt algorithm</param>
/// <param name="path">Path where the HWID will be saved</param>
/// <returns>Encrypted HWID</returns>
public string Verify(string password, string path)
{
try
{
string db = string.Empty;
if (path.Contains("http"))
{
WebClient webClient = new WebClient();
db = webClient.DownloadString(path);
webClient.Dispose();
}
else
{
System.IO.StreamReader file = new System.IO.StreamReader(path);
db = file.ReadToEnd();
file.Close();
}
if (db.Contains(EncryptString(_cpuId + _hddId, password)))
return "HWID OK";
else
return "HWID NOT FOUND";
}
catch (Exception ex)
{
return ex.Message;
}
}

void Main()


static void Main(string[] args)
{

Hwid hwid = new Hwid();
// Change this to your desired password;
string password = "secret1234";

if (args.Length < 1)
PrintHelp();
else
switch (args[0])
{
case "--display":
{
Console.WriteLine("CPU ID: " + hwid.CpuId);
Console.WriteLine("HDD ID: " + hwid.HddId);
Console.WriteLine();
break;
}
case "--generate":
{
if (args.Length < 2)
{
Console.WriteLine("Path not specified!");
break;
}
string path = args[1];
Console.WriteLine("Encrypted HWID: " + hwid.Generate(password, path));
// If we don't specify the path it will only return the encrypted string
//Console.WriteLine("Encrypted HWID: " + hwid.Generate(password));
break;
}
case "--verify":
{
if (args.Length < 2)
{
Console.WriteLine("Path not specified!");
break;
}
string path = args[1];
Console.WriteLine(hwid.Verify(password, path));
break;
}
default:
{
PrintHelp();
break;
}

}
Console.WriteLine("Press any key to continue . . .");
Console.ReadLine();
}

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...