Introduction
Many of us programmers are constantly banging our heads to the wall – how to positively and efficiently protect our project?
Many keep asking about how to identify the machine and the answers are very creative.
Since I had to work on such a solution for a sensitive project, I had to look into the matter myself.
This is my first contribution to this great community and I hope it will solve an issue for anyone who finds it relevant.
In this article, I will explain the steps, the how-to and provide a very simple code sample that works.
Background
Let's put first in first place and get the background clear.
For most machines, the CPU id is no real element to rely on as it is not there, or not unique, etc. so no CPU id in this article.
Mother board serial is also not a very reliable element for the very same reasons. Naturally MAC address is also not a good factor as it may be changed and we wouldn't want the hassle to re-license on every simple hardware modification like network card and we should also take into consideration that we will most probably have 2 addresses for laptops…
Volume serial? naaa… nor reliable and may change.
Windows product key? maybe, but for many large organizations, it is not unique as they have site or large account license – so for us, not good enough.
There are only 2 elements that are constant, stable, unique, reliable and easy to handle. They are Machine UUID and Windows Machine GUID. These elements won't change or modify unless Windows is re-installed or a hardware basic change is made.
So we can rely on them and forget the other elements.
Using the Code
The sample code is in simple VB.NET.
The code is NOT the most efficient and may be improved, it is presented this way to ensure it is kept easy and simple to follow and understand.
The code was tested on both 64 and 32 bits machines.
Firstly – import the following:
Imports System.Management
Imports Microsoft.Win32
We will have 3 functions:
- p>The first one is
Get_WindowsGUID
. This one retries the windows GUID created when Windows was installed and will be fixed until it is being re-installed.
Public Function Get_WinGUID() As String
Dim sRet As String = ""
Try
Dim readValue = My.Computer.Registry.GetValue_
("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Cryptography", "MachineGUID", Nothing)
sRet = readValue
Catch ex As Exception
sRet = ""
End Try
Return sRet
End Function
- The second one is
Get_MachineUUID
. This one gets the machine unique UUID
.
Public Function Get_PC_UUID() As String
Dim UUID As String = ""
Try
Dim searcher As New ManagementObjectSearcher(
"root\CIMV2",
"SELECT * FROM Win32_ComputerSystemProduct")
For Each queryObj As ManagementObject In searcher.Get()
UUID += queryObj("UUID")
Next
Catch err As ManagementException
End Try
Return UUID
End Function
- The last one is
MD5Hash
. This one gets a string
and MD5Hash it = one way crypto to create a "simple" string
in a fixed length, so we can use it as a standard outcome of our process. This function is not mandatory and if one chooses to use the plain raw IDs as they are generated – that's fine too.
Public Function GET_MD5Hash(ByVal strToHash As String) As String
Dim md5Obj As New Security.Cryptography.MD5CryptoServiceProvider
Dim bytesToHash() As Byte = System.Text.Encoding.ASCII.GetBytes(strToHash)
bytesToHash = md5Obj.ComputeHash(bytesToHash)
Dim strResult As String = ""
For Each b As Byte In bytesToHash
strResult += b.ToString("x2")
Next
Return strResult
End Function
The technique is very simple.
Get the windowsGUID
into a string
and the UUID into a second string
.
Add them into one string
.
Remove dashes (-) and spaces to create one simple string
.
Send it to the MD5Hash
function and you've got a returned string
to be used as the machine unique ID / finger Print.
I hope it did help those who struggled with this topic.
The use of this code is free and subject to no restrictions, but one only – I take no liability, so use it with caution…
Enjoy!