In the scenario where the computer needs to be identified, we usually choose to obtain some hardware information. However, the CPU serial number may be duplicated, the MAC address of the network card and the computer name can be easily modified, so the most reliable one should be the hard disk serial number. The acquisition of hard disk serial number in Windows is not complicated, but the key is how to obtain the hard disk serial number without the need for privilege escalation. In order to solve this problem, I also checked a lot of information, basically the major blogs are posted in the need to upgrade the privileges of the code.
Finally, in the source code of DiskID32, I found a way to get the serial number of the hard disk without right. However, the source code can only be compiled by MBCS multi-byte character set, but not by Unicode character set. Therefore, I made some modifications to make it compatible with both methods. Next, I will revise and test the code pasted out for your reference.
#include <windows.h>
#include <intrin.h>
#define MAX_IDE_DRIVES 16
char HardDriveSerialNumber[1024];
char HardDriveModelNumber[1024];
char * flipAndCodeBytes(const char * str,
int pos,
int flip,
char * buf)
{
int i;
int j = 0;
int k = 0;
buf[0] = '\0';
if (pos <= 0)
return buf;
if (!j)
{
char p = 0;
// First try to gather all characters representing hex digits only.
j = 1;
k = 0;
buf[k] = 0;
for (i = pos; j && str[i] != '\0'; ++i)
{
char c = tolower(str[i]);
if (isspace(c))
c = '0';
++p;
buf[k] <<= 4;
if (c >= '0' && c <= '9')
buf[k] |= (unsigned char)(c - '0');
else if (c >= 'a' && c <= 'f')
buf[k] |= (unsigned char)(c - 'a' + 10);
else
{
j = 0;
break;
}
if (p == 2)
{
if (buf[k] != '\0' && !isprint(buf[k]))
{
j = 0;
break;
}
++k;
p = 0;
buf[k] = 0;
}
}
}
if (!j)
{
// There are non-digit characters, gather them as is.
j = 1;
k = 0;
for (i = pos; j && str[i] != '\0'; ++i)
{
char c = str[i];
if (!isprint(c))
{
j = 0;
break;
}
buf[k++] = c;
}
}
if (!j)
{
// The characters are not there or are not printable.
k = 0;
}
buf[k] = '\0';
if (flip)
// Flip adjacent characters
for (j = 0; j < k; j += 2)
{
char t = buf[j];
buf[j] = buf[j + 1];
buf[j + 1] = t;
}
// Trim any beginning and end space
i = j = -1;
for (k = 0; buf[k] != '\0'; ++k)
{
if (!isspace(buf[k]))
{
if (i < 0)
i = k;
j = k;
}
}
if ((i >= 0) && (j >= 0))
{
for (k = i; (k <= j) && (buf[k] != '\0'); ++k)
buf[k - i] = buf[k];
buf[k - i] = '\0';
}
return buf;
}
int ReadPhysicalDriveInNTWithZeroRights(void)
{
int done = FALSE;
int drive = 0;
for (drive = 0; drive < MAX_IDE_DRIVES; drive++)
{
HANDLE hPhysicalDriveIOCTL = 0;
// Try to get a handle to PhysicalDrive IOCTL, report failure
// and exit if can't.
TCHAR driveName[256];
wsprintf(driveName, TEXT("\\\\.\\PhysicalDrive%d"), drive);
// Windows NT, Windows 2000, Windows XP - admin rights not required
hPhysicalDriveIOCTL = CreateFile(driveName, 0,
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
OPEN_EXISTING, 0, NULL);
if (hPhysicalDriveIOCTL == INVALID_HANDLE_VALUE)
{
return FALSE;
}
else
{
STORAGE_PROPERTY_QUERY query;
DWORD cbBytesReturned = 0;
char local_buffer[10000];
memset((void *)& query, 0, sizeof(query));
query.PropertyId = StorageDeviceProperty;
query.QueryType = PropertyStandardQuery;
memset(local_buffer, 0, sizeof(local_buffer));
if (DeviceIoControl(hPhysicalDriveIOCTL, IOCTL_STORAGE_QUERY_PROPERTY,
&query,
sizeof(query),
&local_buffer[0],
sizeof(local_buffer),
&cbBytesReturned, NULL))
{
STORAGE_DEVICE_DESCRIPTOR * descrip = (STORAGE_DEVICE_DESCRIPTOR *)& local_buffer;
char serialNumber[1000];
char modelNumber[1000];
char vendorId[1000];
char productRevision[1000];
flipAndCodeBytes(local_buffer,
descrip->VendorIdOffset,
0, vendorId);
flipAndCodeBytes(local_buffer,
descrip->ProductIdOffset,
0, modelNumber);
flipAndCodeBytes(local_buffer,
descrip->ProductRevisionOffset,
0, productRevision);
flipAndCodeBytes(local_buffer,
descrip->SerialNumberOffset,
1, serialNumber);
if (isalnum(serialNumber[0]))
{
// This means that a legitimate hard disk serial number has been obtained.
// The serial number of the hard disk is in the serialNumber array
// The hard disk model is in the modelNumber array
// Hard disk manufacturer number in vendorId
// Hard disk firmware version number in product review
}
}
else
{
DWORD err = GetLastError();
}
CloseHandle(hPhysicalDriveIOCTL);
}
}
return done;
}